Skip to main content
Version: Next

SQL Transforms

Starlake SQL transforms let you materialize any SELECT statement into a target table using a configurable write strategy. This page covers SELECT syntax, column documentation with attributesDesc, custom MERGE/INSERT via parseSQL: false, and incremental modelling with the sl_start_date / sl_end_date environment variables and sl_incremental. SQL files are standard SQL: there is no Jinja layer to learn. A task can still use a Jinja {% if %} block when part of its SQL depends on the run, as with sl_incremental.

Variable substitution syntax

The {{variable}} syntax used in SQL files (e.g. {{sl_start_date}}) is Starlake's environment variable substitution. These variables are resolved by Starlake before the SQL is sent to the database engine, and a {% if %} block is evaluated by Starlake when the task runs. You can use any environment variable with this syntax.

SELECT Statement Materialization​

A SQL transform is a SELECT statement that Starlake materializes into a target table using the write strategy defined in the companion YAML file. The SELECT can be as simple or as complex as needed -- single table queries, multi-table joins, subqueries, and window functions all work.

Schema matching rules:

  • If the target table exists, the SELECT result must match its schema.
  • If the target table does not exist, Starlake infers the schema (column names and types) from the SELECT result.
  • Column documentation from source tables propagates automatically to the target.

Document Calculated Columns​

Starlake infers column metadata from source tables. For calculated columns that do not exist in any source, add documentation using attributesDesc in the YAML configuration:

metadata/transform/<domain>/<table>.sl.yml
task:
...
attributesDesc:
- name: <column_name>
comment: <column_description>
...

This metadata appears in the data catalog and helps downstream consumers understand derived fields.

Custom Write Strategy with parseSQL​

If none of the predefined write strategies fits your use case, write your own MERGE, INSERT, or UPDATE statement directly in the SQL file. Set parseSQL to false so Starlake executes the SQL as-is instead of wrapping the SELECT in a materialization statement:

metadata/transform/<domain>/<table>.sl.yml
task:
...
parseSQL: false
...

With parseSQL: false, you have full control over the DML statement. Starlake does not modify or wrap the SQL.

Incremental Modelling​

When data arrives periodically, you often need to process only the new data for each interval. Starlake supports incremental transforms through two reserved environment variables:

  • sl_start_date -- start of the current processing interval
  • sl_end_date -- end of the current processing interval

Use these variables in a WHERE clause to filter input data:

metadata/transform/<domain>/daily_sales.sql
SELECT
product_id,
SUM(amount) AS daily_revenue,
COUNT(*) AS transaction_count
FROM
sales.transactions
WHERE
transaction_date >= '{{sl_start_date}}'
AND transaction_date < '{{sl_end_date}}'
GROUP BY
product_id

The orchestrator (Airflow, Dagster, or Snowflake Tasks) passes the correct values for each execution interval. The computed results append to the existing table, which greatly reduces the volume of data processed on each run.

Handle Missed Intervals with Catchup​

If an incremental job fails for a specific day, Starlake integrates with Airflow's Catchup mechanism. Add the catchup flag to your YAML DAG definition, and the orchestrator replays missed intervals with the correct sl_start_date and sl_end_date values.

Incremental SQL​

A task often loads everything on its first run and only the new rows afterwards. The sl_incremental variable tells the two apart: it is true when the target table already exists and the run is not --truncate.

SELECT id, amount, updated_at
FROM sales.orders
{% if sl_incremental %}
WHERE updated_at > (SELECT max(updated_at) FROM mart.orders)
{% endif %}

On the first run, mart.orders does not exist: the whole table is read. On the next runs, only the rows newer than the latest one already loaded are read, and the write strategy of the task (APPEND, UPSERT_BY_KEY...) writes them.

  • sl_incremental is available in the task's .sql file (the main SQL and its presql / postsql sections) and in the task YAML: presql, postsql and an inline sql:.
  • starlake transform --name mart.orders --truncate empties the table and reloads the full SELECT: sl_incremental is false.
  • starlake transform --compile prints both versions, table missing and table existing, of the main SQL and of presql / postsql. Each version renders sl_incremental from its own table state, without checking the target.
  • DAGs generated for the Snowflake and Databricks orchestrators carry both versions of the main SQL. Their presql / postsql are rendered with sl_incremental false, with a warning naming the task.
  • With transform --impacted, a task that uses sl_incremental only reads its new rows, so its history is not rebuilt. Reload it in full with starlake transform --name <task> --truncate (not --impacted --truncate, which truncates every impacted task).
  • Starlake computes the variable: a value passed with --options or set as an environment variable is ignored, with a warning.

In the task YAML, keep the tags inside a double-quoted or block (|) scalar:

task:
presql:
- |
{% if sl_incremental %}
DELETE FROM mart.orders WHERE updated_at >= current_date - 1
{% endif %}

A Jinja loop or {% set %} at YAML level that depends on sl_incremental makes the task fail to load, with an error that names the file and sl_incremental.

A use of sl_incremental reached only through {% include %} or a project macro is not detected: it renders as false, so the task always loads in full. Write the variable in the task's own SQL or YAML.

The SQL shown for such a task (in the site or a loaded project) keeps its {% if sl_incremental %} blocks, normalized by Starlake (for example not x is shown as !x).

Next Steps​

Frequently Asked Questions​

Does the SELECT result have to match the target table schema?​

If the target table already exists, the SELECT result must match its schema. Otherwise, Starlake automatically infers the schema from the result.

How do you document calculated columns in a SQL transform?​

Use the attributesDesc property in the transform YAML file to add a name and comment for each calculated column.

Can you write custom MERGE or INSERT instead of a SELECT?​

Yes. Set parseSQL: false in the YAML configuration. Starlake executes the SQL as-is without converting it into a write statement.

What are the sl_start_date and sl_end_date variables?​

They are reserved environment variables representing the current time interval. The orchestrator (Airflow, Dagster, or Snowflake Tasks) passes them automatically for incremental executions.

What happens if an incremental job fails for a day?​

Starlake integrates with Airflow's Catchup mechanism. Missed intervals are automatically replayed with the correct sl_start_date and sl_end_date values.

Does Starlake support Jinja templating in SQL files?​

SQL files are standard SQL with variables such as {{sl_start_date}}, resolved by Starlake before the SQL runs. There is no Jinja layer to learn, but a Jinja {% if %} block is supported for SQL that depends on the run, such as the sl_incremental condition.

How does schema inference work when the target table does not exist?​

Starlake executes the SELECT and infers the schema (types, column names) from the result. Source column documentation is automatically propagated.