Custom Query (SQL Sources)
Configuration reference for Custom Query batch sources: writing a query:-prefixed SQL source, incremental-column requirements, and common pitfalls.
Custom Query sources let a batch pipeline read from the result of a SQL query instead of reading one table directly. The query: form works across supported SQL batch sources, not only SQL Server. Use it when the required dataset needs filtering, joins, computed columns, or a governance-friendly query surface that cannot be modeled as a simple source_table.
Custom queries are an advanced batch-source path. Prefer a first-party table connector or a database view when that gives the same result with simpler operational behavior.
Requirements
Connectivity and credentials must both be in place before the pipeline can run.
Connectivity
The Nilus runtime must reach the SQL source through a supported SQL connector or DataOS depot.
The source role/user must be allowed to run the query and read every referenced table or view.
The query should be tested directly against the source database before it is added to a Nilus manifest.
Query requirements
query: prefix
source.options.source_table must start with query: so Nilus treats the value as SQL rather than a table name.
Stable column names
The query result should return stable, explicitly named columns. Avoid ambiguous duplicate names from joins.
Incremental column
For incremental loads, the query must return incremental_key with that exact output alias.
Merge key
A merge load requires source.options.primary_key. The query must return that key with the exact output alias.
Source-side filtering
Nilus sends the SQL as authored. It does not add, rewrite, or optimize filters. Put joins and predicates in the SQL.
Source options
source_table
Yes
SQL string prefixed with query:.
primary_key
Conditional
Column returned by the query and used as the merge key. Required for incremental_strategy: merge. It is not inferred from an underlying table.
incremental_key
No
Exact output-column alias. It must be a native timestamp or numeric value. Cast timestamp strings in the SQL.
interval_start
No
Optional lower bound passed into the extraction window.
interval_end
No
Optional upper bound passed into the extraction window.
sql_limit
Unsupported
Not supported on query:. The run fails with sql_limit is not supported for custom queries. Put LIMIT or TOP in the SQL instead.
type_hints
No
Object map of column_name: <type> to override inferred types when query inference is ambiguous.
Sample Nilus configs
Each example below is self-contained and uses the current Nilus pipeline shape.
Append from a join query
Incremental custom query
Keys, incrementality, and output columns
query: reads do not inherit a table's primary key.
Set
source.options.primary_keyto a column the query returns wheneversink.options.incremental_strategyismerge.incremental_keyandprimary_keymust match the query's column names exactly. They are not snake_cased.sql_limitis rejected withsql_limit is not supported for custom queries. Bound the result in the SQL (LIMIT,TOP, or aWHEREon:interval_start).:interval_startand:interval_endare bound parameters. Write them bare in the SQL (where o.updated_at > :interval_start), never in quotes. This is the opposite of MongoDB, which uses the quoted string":interval_start".With
incremental_keyset,:interval_starttakes the progress saved by the last run. On the first run it usesinterval_start, or0001-01-01if that isn't set.:interval_endis only filled wheninterval_endis set, so don't reference it in the query without settinginterval_end.Without
incremental_key, the placeholders takeinterval_startandinterval_endfrom the config, falling back to the earliest and latest possible dates when unset.
query: always adds the load-metadata columns _nilus_load_id and _nilus_id to the destination tables it creates. Default table reads (pyarrow) do not. Because of that, switching an existing table-read destination to query: on the same dest_table can fail at the destination with cannot add required column: _nilus_load_id. Point dest_table at a new table, or recreate the existing one. See Schema evolution and MS SQL Server → Keys, incrementality, and output columns.
On a DataOS Lakehouse destination this is the same for every SQL batch source. query: stores decimal columns as Iceberg decimal(38,9) instead of the source precision and scale. A table read of DECIMAL(20,4) or NUMERIC(20,4) writes decimal(20,4). Mixing those styles on the same dest_table fails with Cannot serialize value, scale of value does not match type decimal(38, 9). Keep one extract style per table. See Schema evolution.
Behavior and capabilities
Pipeline type — custom queries run as
spec.type: batchpipelines.Query execution — Nilus sends the SQL to the source as authored. Database-specific SQL syntax applies.
Schema inference — Nilus infers the output schema from the query result. That inferred schema does not evolve onto a destination table created by a previous table-name load. Computed / on-the-fly columns (aliases, literals, expressions) are not entitled to schema evolution — point
dest_tableat a new table, or drop the existing dest and reload. See Schema Evolution.Incrementality — incremental state only works when the query returns
incremental_keyand filters with the current interval.Lakehouse decimals:
query:stores decimals as Icebergdecimal(38,9)for every SQL batch source. Table reads preserve source precision and scale. Do not switch those styles on the samedest_table. See Schema evolution.CDC support — custom queries are not supported for CDC sources.
Troubleshooting
Query fails before extraction
SQL syntax is invalid for the source database, or referenced objects are not visible to the source role.
Test the query directly in the database using the same role/user.
Incremental run reprocesses old rows
The query does not filter with :interval_start, or the returned incremental_key is not stable.
Return the incremental column and add an explicit interval predicate to the query.
Merge behaves like append
primary_key is missing, is under sink.options, or does not match a query output alias.
Set source.options.primary_key to the exact selected key alias.
Query validation rejects the incremental key
The selected alias differs from incremental_key, or the result is neither numeric nor a native timestamp.
Alias the output exactly and cast timestamp strings in the SQL.
sql_limit is not supported for custom queries
sql_limit was set on a query: source.
Remove sql_limit and put LIMIT or source-specific TOP in the SQL query.
Schema changes unexpectedly
The query uses SELECT *, ambiguous aliases, or source tables changed shape.
Select explicit columns and alias computed expressions.
Load fails with cannot add required column: _nilus_load_id (or column "_nilus_load_id" … contains null values) after switching a table-name source to query: on the same dest table
Overlaying a query: extract onto a dest table that was created by a table read is not additive schema evolution. Nilus-managed columns such as _nilus_load_id are not reconciled as user schema.
Point dest_table at a new table, or drop the existing dest table and reload. See Schema Evolution.
Lakehouse load fails on decimal precision or scale
The query's inferred decimal type differs from the existing destination column.
Keep one extract style per dest_table. Point dest_table at a new table, or drop the existing table and reload.
Extraction is slow
Query plan is expensive or lacks indexes on join/filter columns.
Review the source database query plan and index the incremental/filter columns.
CDC manifest rejects the query
Custom queries are batch-only.
Use a batch pipeline for custom queries, or use a CDC source page for log-based change capture.
Best practices
Prefer database views when the query is stable and can be governed upstream.
Keep query logic deterministic; avoid non-deterministic functions such as
NOW()orRANDOM()in incremental predicates.Explicitly alias computed columns.
Validate row counts and schema in a non-production sink before scheduling production runs.
Keep queries narrow and bounded. Large cross-source joins should be modeled upstream or in a transformation layer.
Related Docs
Last updated
Was this helpful?