Adapters
ClickHouse
ClickHouse adapter configuration for SQLBuild.
ClickHouse requires the optional clickhouse-connect extra (ClickHouse 25.8+ required; older
servers fail with A703):
pip install 'sqlbuild[clickhouse]'# oruv pip install 'sqlbuild[clickhouse]'Connection config
Section titled “Connection config”adapter = "clickhouse"default_target = "dev"
[connections.clickhouse]host = "localhost"port = 8123user = "default"password = "${ENV:CLICKHOUSE_PASSWORD}"
[targets.dev]connection = "clickhouse"schema = "analytics_dev"| Field | Description |
|---|---|
host, port |
Hostname (default localhost) and HTTP/HTTPS port (8123, or 8443 when secure = true) |
user, password |
Credentials (user defaults to default; also accepts username) |
database |
Default database for unqualified names (default default) |
secure, verify |
Enable HTTPS (default false) and TLS certificate verification (default true) |
settings |
Extra ClickHouse query settings table |
connect_timeout, send_receive_timeout |
Client timeouts in seconds |
Unknown keys fail with A702.
Schemas are databases
Section titled “Schemas are databases”ClickHouse has no namespace level between database and table, so a SQLBuild schema maps to a
ClickHouse database. Set schema on targets, models, and sources; setting database fails
with A704. Identifiers are always case-sensitive in ClickHouse.
How SQLBuild writes
Section titled “How SQLBuild writes”SQLBuild avoids in-place row mutations and relies on atomic swaps in Atomic-engine databases
(other database engines fail with A705):
- Tables build in a staging table and swap atomically via
EXCHANGE TABLES. - Views use
CREATE OR REPLACE VIEW. - Incremental
appendinserts new rows directly. - Incremental
merge,delete_insert, cursor windows, and microbatches rebuild touched partitions and swap atomically (REPLACE PARTITIONfor one partition, orEXCHANGE TABLESfor multiple). Partition large incremental models by their arrival key—withoutpartition_by, every incremental run rewrites the whole table. - Snapshots rebuild a private copy and swap atomically via
EXCHANGE TABLES.
One writer per model
Section titled “One writer per model”ClickHouse has no multi-statement transaction across a partition rewrite. SQLBuild serializes
rewrites within a single sqb process (--concurrency and batch_concurrency are safe), but two
concurrent sqb processes writing the same model can overwrite each other’s rows. Schedule one
non-overlapping writer per model.
Physical layout
Section titled “Physical layout”Configure ClickHouse engines, keys, and settings in a model’s clickhouse (...) block (ignored by
other adapters; see Table layout):
MODEL ( description 'One row per order event.', materialized incremental, incremental_strategy delete_insert, unique_key [event_id], cursor event_time, cursor_type timestamp, cursor_inputs (raw_events event_time), clickhouse ( engine 'ReplacingMergeTree(version)', order_by [event_id, event_time], partition_by 'toYYYYMM(event_time)', primary_key [event_id], table_settings (index_granularity 4096), ),);| Field | Value | Default |
|---|---|---|
engine |
Non-replicated MergeTree-family engine (or ClickHouse Cloud Shared* best effort) |
MergeTree |
order_by |
Sorting expression or list | tuple() |
partition_by |
Partitioning expression | None |
primary_key |
Prefix of order_by |
order_by |
table_settings |
Setting map (string, int, or bool values) | None |
Quote non-column expressions. Replicated* engines and clickhouse (...) on views/snapshots fail
compilation. Layout changes rebuild table models; incremental models warn PHYSICAL_LAYOUT_PENDING
until rebuilt with --full-refresh. For nullable key columns, wrap in assumeNotNull() /
coalesce() or set table_settings (allow_nullable_key true).
Reading ReplacingMergeTree sources
Section titled “Reading ReplacingMergeTree sources”Set final: true on a ReplacingMergeTree source so SQLBuild reads it with FINAL (one row per
key):
sources: - name: customers description: Customer master data. schema: raw table: customers final: trueFunctions, clones, and nullable columns
Section titled “Functions, clones, and nullable columns”- Functions: SQL scalar functions deploy globally as
<schema>__<function>(__udf(...)renders that name). Python functions (A707) and SQL table functions (A708) are not deployed. - Clones:
sqb clonecreates the table and attaches partitions viaATTACH PARTITION ALL FROM(zero-copy hard-linked parts, independent after origin drop). - Nullable columns: When an incremental run introduces
NULLinto a non-nullable column:append_new_columnsandsync_all_columnsalter the column toNullable(unless it is in a key withoutallow_nullable_key, which fails withA712),failaborts the schema change, andignorefails withA713rather than substituting ClickHouse defaults (0,'', epoch).
Settings SQLBuild pins
Section titled “Settings SQLBuild pins”SQLBuild sets these per-query settings for standard SQL semantics; overriding them in connection
settings fails with A710 (skipped only for readonly = 1 Recall readers):
| Setting | Value | Purpose |
|---|---|---|
join_use_nulls, cast_keep_nullable |
1 |
Outer joins and CAST preserve NULL |
union_default_mode, except_default_mode, intersect_default_mode |
DISTINCT |
Standard set-operation semantics |
default_table_engine |
MergeTree |
Default engine for tables |
mutations_sync, lightweight_deletes_sync |
2 |
Synchronous statement completion |
date_time_input_format, cast_string_to_date_time_mode |
best_effort |
ISO 8601 timestamp parsing |
insert_null_as_default |
0 |
Reject NULL into non-nullable columns |
Wrap lag/lead arguments in toNullable(...) in custom models when you expect NULL at window
edges.
SQL differences and limits
Section titled “SQL differences and limits”- 3+ table joins: Alias ambiguous selected columns (
c.customer_id AS customer_id) so ClickHouse does not keep the dottedc.customer_idname (A711when SQL analysis detects it). CURRENT_TIMESTAMP(): Requires parentheses (or usenow()).- Limits: Single server only (no
ON CLUSTER,ReplicatedMergeTree, orDistributed); no transactions; snapshots use unsortedMergeTree; no retention/time travel; seeds load as CSV over HTTP. - Recall: Any project can share Recall history in ClickHouse regardless of its warehouse adapter.