Transforms
Mirror pipelines support two types of transforms to shape and enrich your data as it flows from source to destination: SQL transforms and external handler transforms. This flexibility allows for greater control over data transformation, enabling both in-pipeline data processing using SQL and the integration of external services through HTTP requests.
1. SQL Transforms
SQL transforms allow you to write SQL queries to modify and shape data from multiple sources within the pipeline. This is ideal for operations that need to be performed within the data pipeline itself, such as filtering, aggregating, or joining datasets.
Depending on how you choose to source your data, you might find that you run into 1 of 2 challenges:
-
You only care about a few contracts
Rather than fill up your database with a ton of extra data, you’d rather filter down your data to a smaller set. -
The data is still a bit raw
Maybe you’d rather track gwei rounded to the nearest whole number instead of wei. You’re looking to map data to a different format so you don’t have to run this calculation over and over again.
The SQL Solution
You can use SQL-based transforms to solve both of these challenges that normally would have you writing your own indexer or data pipeline. Instead, Goldsky can automatically run these for you using just 3 pieces of info:
name
: A shortname for this transform
You can refer to this from sinks viafrom
or treat it as a table in SQL from other transforms.sql
: The actual SQL
To filter your data, use aWHERE
clause, e.g.WHERE liquidity > 1000
.
To map your data, use anAS
clause combined withSELECT
, e.g.SELECT wei / 1000000000 AS gwei
.primary_key
: A unique ID
This should be unique, but you can also use this to intentionally de-duplicate data - the latest row with the same ID will replace all the others.
Combine them together into your config:
That’s it. You can now filter and map data to exactly what you need.
2. External Handler Transforms (New)
With external handler transforms, you can send data from your Mirror pipeline to an external service via HTTP and return the processed results back into the pipeline. This opens up a world of possibilities by allowing you to bring your own custom logic, programming languages, and external services into the transformation process.
Key Features of External Handler Transforms:
- Send data to external services via HTTP.
- Supports a wide variety of programming languages and external libraries.
- Handle complex processing outside the pipeline and return results in real time.
- Guaranteed at least once delivery and back-pressure control to ensure data integrity.
How External Handlers work
- The pipeline sends a POST request to the external handler with a mini-batch of JSON rows.
- The external handler processes the data and returns the transformed rows in the same format and order as received.
Example workflow
- The pipeline sends data to an external service (e.g. a custom API).
- The service processes the data and returns the results to the pipeline.
- The pipeline continues processing the enriched data downstream.
Example HTTP Request
Example HTTP Response
Example YAML config with an external transform
Schema override datatypes
When overriding the schema of the data returned by the handler it’s important to get the datatypes for each column right. The schema_override property is a map of column names to Flink SQL datatypes.
Key considerations
- Schema Changes: If the external handler’s output schema changes, you will need to redeploy the pipeline with the relevant schema_override.
- Failure Handling: In case of failures, the pipeline retries requests indefinitely with exponential backoff.
- Networking & Performance: For optimal performance, deploy your handler in a region close to where the pipelines are deployed (we use aws
us-west-2
). Aim to keep p95 latency under 100 milliseconds for best results.
Useful tips
Schema Changes: A change in the output schema of the external handler requires redeployment with schema_override.
- Failure Handling: The pipeline retries indefinitely with exponential backoff.
- Networking: Deploy the handler close to where the pipeline runs for better performance.
- Latency: Keep handler response times under 100ms to ensure smooth operation.
Example Implementation
In this repo you can see an example implementation of HTTP Transforms enriching ERC-20 Transfer Events.
Was this page helpful?