Skip to main content
Version: Next

Testing on production data with data branches

Unit tests run your pipeline on DuckDB against fixture data. Sometimes you need more: validating a new transform on the real production volume, or checking that a schema change does not break a MERGE on last month's data. A data branch does that. Set SL_DATA_BRANCH and Starlake redirects every write of the job into a sandbox built from copies of your production tables, zero-copy clones on BigQuery and Snowflake. Production tables are never modified.

note

This page covers branches driven by SL_DATA_BRANCH, on BigQuery, Snowflake, DuckDB, DuckLake and QoD. DuckLake databases served by QoD also have their own branching, with zero-copy clones, row-level diffs and reviewed merges: see QoD branching.

How branches work​

A data branch is a named sandbox in the warehouse. The branch name becomes a BigQuery dataset or a schema (Snowflake, DuckDB, DuckLake, QoD), and production tables are copied into it one table at a time, the first time a job on the branch needs them: with the warehouse's own zero-copy CLONE on BigQuery and Snowflake, with a full copy on DuckDB and DuckLake. That is what "lazy snapshot" means: nothing is copied when you choose a branch name, and a table that no job touches is never copied.

QuestionAnswer
MechanismA copy of the table into the branch namespace (zero-copy CREATE TABLE ... CLONE on BigQuery and Snowflake, CREATE TABLE ... AS SELECT on DuckDB and DuckLake), then SQL rewriting so the job reads and writes the copies. There is no view layer.
GranularityOne namespace per branch (a dataset on BigQuery, a schema elsewhere). Tables are cloned individually, on first use. A branch is not tied to a domain: it holds whatever tables the jobs you run on it touch.
PromotionProduction never reuses the tables built on the branch. You promote by running the job again without SL_DATA_BRANCH. On Snowflake, DuckDB, DuckLake and QoD, BRANCH APPLY can instead replay the SQL statements recorded on the branch against production: the transform is recomputed either way.
Change impactReported before you run, not acted on. starlake impact compares your change with a Git ref and lists the load tables and transforms it touches, classified BREAKING, NON_BREAKING, ADDED or METADATA, then the downstream tasks it impacts, through column-level lineage. The branch runs only the tasks you ask for: run the reported ones, or the whole DAG, on the same branch.
EnginesBigQuery, Snowflake, DuckDB, DuckLake and QoD. Every other engine ignores the setting and writes to production.
LifecycleA branch is created on first use and lives until you drop it. There is no starlake command to list, refresh or drop branches: you do it on the warehouse, as shown in Lifecycle and cost.

The engines implement the clone step differently:

  • BigQuery: clone on read. Before a native transform runs, Starlake clones every table its SQL references, plus its target table, into the branch dataset. Sources are frozen at the moment they are first cloned.
  • Snowflake: clone on write. Starlake routes the connection through the Starlake JDBC proxy driver, which clones a table the first time a statement modifies it. Reads of tables the branch never modified go to production and see live data.
  • DuckDB, DuckLake and QoD: copy on write. The same proxy driver, the same behavior as on Snowflake, but the copy is a full CREATE TABLE ... AS SELECT: DuckDB and DuckLake have no zero-copy clone.

Supported engines​

EngineHow the branch is builtWhere the results land
BigQuery (native transforms and native loads)Source and target tables are copied with CREATE TABLE ... CLONE, and the task SQL is rewritten to use the copiesDataset <branch> in the target table's project
Snowflake (transforms and native loads)The Starlake JDBC proxy driver clones each table with CREATE TABLE ... CLONE the first time a statement modifies itSchema <branch>
DuckDB, DuckLake attached in preActions, and QoD over Flight SQL (transforms)The Starlake JDBC proxy driver copies each table with CREATE TABLE ... AS SELECT the first time a statement modifies itSchema <branch> in the catalog the connection uses
Any other engineNot supportedProduction: the setting is ignored
warning

On Databricks, Redshift, PostgreSQL and every other engine not listed above, SL_DATA_BRANCH has no effect and the job writes to production as usual. Do not rely on a branch there.

Enabling a branch​

The branch name is resolved in this order, first match wins:

  1. SL_DATA_BRANCH (or sl_data_branch) in the active environment: your shell, metadata/env.sl.yml or metadata/env.<ENV>.sl.yml
  2. sl_data_branch (or SL_DATA_BRANCH) in the options of the sink connection. BigQuery only: the other engines read the environment alone.

An empty value or None disables branching.

From the command line:

SL_DATA_BRANCH=test_order_summary starlake transform --name kpi.order_summary
SL_DATA_BRANCH=test_order_summary starlake load --domains sales

Or for a whole environment, in metadata/env.BRANCH.sl.yml, activated with SL_ENV=BRANCH:

version: 1
env:
SL_DATA_BRANCH: "test_order_summary"

The branch name becomes a BigQuery dataset or a schema, so use letters, digits and underscores only.

BigQuery​

When a native BigQuery transform runs on a branch, Starlake:

  1. Creates the dataset <branch> in the target table's project, in the location of the sink connection, with the description Starlake data branch: <branch>. location is required: the job fails without it.
  2. Parses the task SQL and clones every source table it references into <branch>, unless a table with that name already exists there.
  3. Clones the existing target table into <branch>, so MERGE and upsert write strategies start from the production rows.
  4. Rewrites the presql, main SQL and postsql to read from and write to the clones. The result lands in <branch>.<table>.

A native load on a branch clones the target table and loads the files into <branch>.<table>.

Expectations of a branched transform or native load check the branch copy, the table the job actually wrote. Their reports record the branch name as the domain, so the expectations audit table tells branch runs apart from production runs.

# metadata/application.sl.yml
application:
connectionRef: "bigquery"
connections:
bigquery:
type: "bigquery"
options:
location: "europe-west1" # required for branching
authType: "APPLICATION_DEFAULT"

What this means in practice:

  • The snapshot is taken on first use, table by table. A source table is cloned the first time a task on the branch reads it, and later runs reuse that clone. Two tables first read on different days are frozen at different points in time, so a branch is not a consistent snapshot of the whole warehouse. To pick up fresh production data, use a new branch name, or drop the branch dataset or the stale clones.
  • Chained tasks see each other's output. When a downstream task reads a table that an upstream task already wrote on the branch, it finds that table in <branch> and reads the branch version, not production. Running a whole DAG on the same branch therefore tests the chain end to end.
  • All domains share one dataset. sales.orders and kpi.orders would both map to <branch>.orders. Inside a single task the second one gets a _1 suffix, but across tasks the first table created under that name wins. Keep table names unique across the domains you run on a branch.
  • Spark-engine tasks are only half branched. A task that runs on Spark and writes to BigQuery writes to <branch>.<table>, but reads its sources from production: only native BigQuery SQL is rewritten.

Snowflake​

On a branch, Starlake routes Snowflake connections through the Starlake JDBC proxy driver, which ships with Starlake: the URL jdbc:snowflake:... becomes jdbc:starlake:snowflake:.... On every new connection, Starlake opens the branch with:

BRANCH START <branch> WITH PERSIST = TRUE, KEEP = TRUE

The first time, this creates schema <branch> in the connection's database, with two bookkeeping tables: sl_table_map, which maps each production table to its copy, and sl_session_hst, the history of the statements run on the branch. Both are Snowflake hybrid tables, so the account must support hybrid tables: where it does not, the branch cannot start and the job fails.

From then on:

  • Copy on write. The first statement that modifies a table (INSERT, UPDATE, DELETE, MERGE, TRUNCATE, ALTER, DROP, CREATE TABLE) clones it into schema <branch>. That statement and all the later ones are rewritten to use the copy. A table created on the branch is created in <branch> directly, with no clone.
  • Only changes are recorded. A statement is added to the history once it has run on the branch, and only if it changes data: queries, and statements that failed, are not recorded. Prepared statements and batches are redirected and recorded like any other statement, a prepared statement with the values of its parameters.
  • Reads of untouched tables hit production. A table the branch never modified is not copied, so reads see live production data, not a frozen snapshot.
  • The branch outlives the job. PERSIST keeps the history of the statements and the table map, and KEEP keeps the copies after the connection closes. A later run with the same branch name reloads both and picks up where the previous one stopped.
  • Name clashes get a suffix. If <branch>.orders is already taken by the copy of another schema's orders, the next copy is named orders_1. The table map records which copy belongs to which table.
note

The proxy driver can only redirect statements its SQL parser understands. While a branch is open, it refuses a statement it cannot parse, so that statement never reaches production: the job fails with SQLState 0A000. Statements that touch no table data still run as is: SHOW, DESCRIBE, EXPLAIN, LIST, USE, SET, UNSET, ALTER SESSION and transaction control.

It also refuses what it cannot redirect or record: stored procedure calls, and prepared statement parameters bound to a stream, a LOB, an array or bytes.

Copies are created in schema <branch> of each table's own database. Starlake creates that schema only in the connection's database, so keep the tables a branched job modifies in that database.

DuckDB, DuckLake and QoD​

Branches work on DuckDB connections (jdbc:duckdb:), on DuckLake catalogs attached in the preActions of a DuckDB connection, and on QoD through its Flight SQL url (jdbc:arrow-flight-sql:). They work as on Snowflake: Starlake wraps the connection in the Starlake JDBC proxy driver and opens the branch with BRANCH START <branch> WITH PERSIST = TRUE, KEEP = TRUE, the first statement that modifies a table copies it into schema <branch>, and the branch outlives the job.

The differences:

  • Copies are full copies. DuckDB and DuckLake have no zero-copy clone, so the first write to a table on the branch copies all its rows with CREATE TABLE ... AS SELECT. On DuckLake that writes new Parquet files, so the first write to a large table takes time and storage in proportion to its size.
  • The branch schema lands in the catalog the preActions select. Starlake opens the branch once the connection's preActions have run, so with ATTACH 'ducklake:...' AS lake; USE lake the branch is schema lake.<branch>. On QoD it is a schema of the database the pool serves.
  • The bookkeeping tables are plain tables. sl_table_map and sl_session_hst live in schema <branch>, with no primary key, since DuckLake refuses constraints. No hybrid tables are needed.
  • Transforms are covered. APPEND, OVERWRITE and UPSERT_BY_KEY transforms, and transforms that create their table, write to the branch. Native DuckDB loads on a branch have not been validated yet: run one on a throwaway branch and check where it wrote before you rely on it.
  • UPSERT_BY_KEY does not run on QoD, with or without a branch. QoD runs each statement in a session of its own, so the temporary view of the incoming rows is gone by the statement that reads it.

The proxy driver rules apply here too: a statement the driver cannot parse is refused, and only the statements that change data are recorded.

# metadata/application.sl.yml: a DuckLake catalog on a local DuckDB
application:
connectionRef: "ducklake"
connections:
ducklake:
type: "jdbc"
options:
url: "jdbc:duckdb:"
driver: "org.duckdb.DuckDBDriver"
preActions: "INSTALL ducklake;LOAD ducklake;ATTACH IF NOT EXISTS 'ducklake:/data/lake/meta.ducklake' AS lake (DATA_PATH '/data/lake/files/');USE lake;"
tip

On QoD, SL_DATA_BRANCH gives you the same branches as on the other engines, made by Starlake inside your database. QoD's own branching is the alternative when you want zero-copy clones, row-level diffs and merges reviewed by a second admin.

Walkthrough: change a transform on a branch​

This example changes kpi.order_summary on BigQuery, checks it against production, and ships it. The Snowflake, DuckDB and QoD variants differ only in steps 4 and 6.

1. Pick a branch name. Nothing is created yet.

export SL_DATA_BRANCH=feat_order_summary

2. Change the transform in metadata/transform/kpi/order_summary.sql, on a Git branch of your project as usual.

3. Run it on the data branch. The first run creates dataset feat_order_summary, clones the sources and the current kpi.order_summary, and writes the new result to feat_order_summary.order_summary. Expectations run against that copy.

starlake transform --name kpi.order_summary

Then run the tasks that read kpi.order_summary. They find the branch copy and read it instead of production:

starlake transform --name kpi.revenue_by_country

To find the tasks downstream of your change, run starlake impact before step 3. It compares the working tree with a Git ref, offline, and lists the impacted downstream tasks, column by column, with those likely to fail:

starlake impact --base origin/main

starlake transform --impacted --base origin/main runs them, upstream tasks first, on the branch set by SL_DATA_BRANCH. To run the same check on every pull request, see Check change impact.

4. Validate. Compare the branch output with production:

-- rows the branch produces that production does not have
SELECT * FROM `my-project.feat_order_summary.order_summary`
EXCEPT DISTINCT
SELECT * FROM `my-project.kpi.order_summary`;

-- and the reverse
SELECT * FROM `my-project.kpi.order_summary`
EXCEPT DISTINCT
SELECT * FROM `my-project.feat_order_summary.order_summary`;

On Snowflake, DuckDB and QoD, run the same queries on feat_order_summary.order_summary from a plain connection, without the Starlake driver. Through a connection opened with the Starlake driver on the branch, kpi.order_summary would itself be redirected to the copy.

5. Iterate. Edit the SQL and run step 3 again. On BigQuery the sources stay frozen at the first clone, so successive runs compare like with like. On both engines the target is not cloned again: a MERGE or upsert task now merges into the output of the previous branch run. On BigQuery, drop feat_order_summary.order_summary before the run to start again from the production rows.

6. Merge. Merge the Git branch, deploy the project, and let production run the transform without SL_DATA_BRANCH. Then drop the data branch:

unset SL_DATA_BRANCH
bq rm -r -f -d my-project:feat_order_summary

On Snowflake, DuckDB and QoD, drop it with BRANCH DROP feat_order_summary WITH PERSIST = TRUE from the Starlake driver (see Lifecycle and cost). You can also promote with BRANCH APPLY, which replays the branch history on production: read the caveats below first.

Lifecycle and cost​

Listing branches​

On BigQuery, branch datasets carry the description Starlake data branch: <branch>:

SELECT schema_name, option_value
FROM `my-project`.`region-europe-west1`.INFORMATION_SCHEMA.SCHEMATA_OPTIONS
WHERE option_name = 'description'
AND option_value LIKE '%Starlake data branch:%';

On Snowflake, every branch schema holds the history table:

SELECT table_catalog, table_schema
FROM information_schema.tables
WHERE table_name = 'SL_SESSION_HST';

On DuckDB, DuckLake and QoD, the same query with the name in lower case, 'sl_session_hst'.

Inspecting a Snowflake branch​

The BRANCH commands only work through a connection opened with the Starlake JDBC driver. In a SQL client, set the driver class to ai.starlake.jdbc.StarlakeDriver and prefix your usual Snowflake URL: jdbc:starlake:snowflake://<account>.snowflakecomputing.com/.... The same goes for a QoD url (jdbc:starlake:arrow-flight-sql://...) and a DuckDB file (jdbc:starlake:duckdb:/path/to/file.duckdb); a DuckLake catalog must be attached in that connection before you open the branch.

A branch must be opened in the connection before BRANCH DESCRIBE or BRANCH APPLY can see it. Always open it with PERSIST and KEEP: a branch opened without KEEP is dropped, copies included, when the connection closes.

BRANCH START feat_order_summary WITH PERSIST = TRUE, KEEP = TRUE;
BRANCH DESCRIBE feat_order_summary; -- statements recorded on the branch, with their timestamps
BRANCH SHOW feat_order_summary; -- status, start time and options

While a branch is open in your client, every statement you run there is rewritten to the copies, and the ones that change data are recorded in its history. Close the connection when you are done.

Refreshing against production​

There is no refresh command. On BigQuery, drop the clones you want refreshed (or the whole dataset) and run the job again: they are cloned anew from production. On Snowflake, tables the branch never modified are already live; a modified table stays at the state it was cloned in, plus the branch's changes, until you drop the branch.

Promoting or discarding​

EngineDiscardPromote
BigQueryDelete the dataset: bq rm -r -f -d my-project:feat_order_summaryNo apply step: run the job again without SL_DATA_BRANCH
Snowflake, DuckDB, DuckLake, QoDBRANCH DROP feat_order_summary WITH PERSIST = TRUE through the Starlake driver: drops the copies, the history and the schema. On DuckDB, DuckLake and QoD, DROP SCHEMA feat_order_summary CASCADE from a plain connection does the sameRun the job again without SL_DATA_BRANCH, or BRANCH START ... WITH PERSIST = TRUE, KEEP = TRUE then BRANCH APPLY feat_order_summary

BRANCH APPLY replays the statements in the branch history, in order, against production, then drops the branch. The history holds the statements that ran on the branch and changed data: queries and failed statements are left out. Before you use it, run BRANCH DESCRIBE and check what it will replay:

  • The history covers every run on the branch. If you ran a transform three times while iterating, its statements are replayed three times. With an append strategy, that inserts the rows three times.
  • A statement that fails during the replay stops it, with an error that says which statement failed. The statements before it stay applied, since nothing is rolled back, and the branch is kept. Fix the cause and run BRANCH APPLY again: it resumes at the failed statement, also from a new connection.

For anything but a single clean run, re-running the job in production is the safer promotion.

Storage and cost​

  • Creating a branch copies no data. A clone shares the storage of its base table on BigQuery and Snowflake, and only the tables a job touches are cloned. On DuckDB, DuckLake and QoD, the first write to a table copies it in full.
  • A clone is billed for the data that differs from its base table. That covers the rows the branch writes, and also the base table data that production later changes or deletes while the clone still references it. A branch kept for months next to a busy production table grows accordingly.
  • Queries on a branch cost the same compute as in production. A full run on a branch processes the full production volume.
  • Branches are never cleaned up automatically. Drop them when you are done.

Limitations​

  • Only BigQuery, Snowflake, DuckDB, DuckLake and QoD are supported. Every other engine ignores the setting.
  • Promotion recomputes: production never reuses the tables built on the branch.
  • A branch does not pick the tasks to run: starlake impact reports the impacted downstream tasks, and you run them.
  • On BigQuery, branch tables live in a single dataset, so table names must be unique across the domains you run on the branch, and sources are frozen at their first clone.
  • On Snowflake, the account must support hybrid tables. On Snowflake, DuckDB, DuckLake and QoD, the proxy driver refuses the statements it cannot parse, stored procedure calls, and prepared statement parameters it cannot record.
  • BRANCH APPLY replays every recorded run and has no rollback: it stops at the first failing statement, and the next BRANCH APPLY resumes there.
  • On DuckDB, DuckLake and QoD, copies are full copies, native loads on a branch are not validated, and UPSERT_BY_KEY does not run on QoD.
  • The connection-level sl_data_branch option is read on BigQuery only.

How this compares with SQLMesh virtual data environments: see Starlake vs SQLMesh.