Skip to content

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]'
# or
uv pip install 'sqlbuild[clickhouse]'
adapter = "clickhouse"
default_target = "dev"
[connections.clickhouse]
host = "localhost"
port = 8123
user = "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.

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.

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 append inserts new rows directly.
  • Incremental merge, delete_insert, cursor windows, and microbatches rebuild touched partitions and swap atomically (REPLACE PARTITION for one partition, or EXCHANGE TABLES for multiple). Partition large incremental models by their arrival key—without partition_by, every incremental run rewrites the whole table.
  • Snapshots rebuild a private copy and swap atomically via EXCHANGE TABLES.

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.

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).

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: true
  • 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 clone creates the table and attaches partitions via ATTACH PARTITION ALL FROM (zero-copy hard-linked parts, independent after origin drop).
  • Nullable columns: When an incremental run introduces NULL into a non-nullable column: append_new_columns and sync_all_columns alter the column to Nullable (unless it is in a key without allow_nullable_key, which fails with A712), fail aborts the schema change, and ignore fails with A713 rather than substituting ClickHouse defaults (0, '', epoch).

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.

  • 3+ table joins: Alias ambiguous selected columns (c.customer_id AS customer_id) so ClickHouse does not keep the dotted c.customer_id name (A711 when SQL analysis detects it).
  • CURRENT_TIMESTAMP(): Requires parentheses (or use now()).
  • Limits: Single server only (no ON CLUSTER, ReplicatedMergeTree, or Distributed); no transactions; snapshots use unsorted MergeTree; no retention/time travel; seeds load as CSV over HTTP.
  • Recall: Any project can share Recall history in ClickHouse regardless of its warehouse adapter.