Skip to content

fix: apply column codec and ttl to distributed tables - #741

Open
Romano-arist wants to merge 12 commits into
ClickHouse:mainfrom
Romano-arist:fix/distributed-column-codec-ttl
Open

Romano-arist wants to merge 12 commits into
ClickHouse:mainfrom
Romano-arist:fix/distributed-column-codec-ttl

Conversation

@Romano-arist

@Romano-arist Romano-arist commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Column-level codec and ttl were ignored for distributed_incremental models: they were not applied on table create, and on schema changes only codec was applied (never ttl). This fixes both paths so column codec/ttl from the model contract are applied on create and on add/modify column.

Limitations and out of scope for this PR

  • Local table gets codec and ttl, distributed tables get codec only (it's inherited via AS , so add/modify mirrors it for consistency). TTL is skipped on the distributed, ClickHouse doesn't apply column TTL to the Distributed engine.
  • On incremental runs a codec/ttl-only change (same data type) has no effect, schema-change detection compares data types only. They're applied on create, on type change, or with --full-refresh.
  • Removing codec/ttl isn't supported: MODIFY COLUMN changes only the type and keeps existing storage clauses, ClickHouse needs an explicit MODIFY COLUMN name REMOVE TTL/REMOVE CODEC. Dropping the clause from the contract won't delete it.

Checklist

  • Unit and integration tests covering the common scenarios were added
  • A changelog entry was added following Updating the Changelog (user-facing, concise, PR link)

Note

Medium Risk
Touches DDL for distributed tables and incremental ALTER paths; incorrect codec/TTL SQL could break model runs or leave schema drift, though changes are gated on enforced contracts and covered by new integration tests.

Overview
Column-level codec and ttl from the model contract are now applied when building distributed shard-local tables on create/rebuild, and on add/modify column during on_schema_change for incremental and distributed materializations. With contract enforced, DDL uses contract rendering (including codec/TTL); without it, schema-change add column no longer applies codec, matching documented behavior. TTL is emitted on the local/shard table only (not on the Distributed wrapper). Schema-change ALTERs are refactored to batch actions and separate local vs distributed DDL where TTL applies only locally.

Integration coverage expands for codec (including no-contract cases and distributed table rebuilds) and adds parallel TTL tests.

Reviewed by Cursor Bugbot for commit 624909a. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@Romano-arist

Copy link
Copy Markdown
Contributor Author

Hi @koletzilla! Could you take a look when you have a moment?

@canbekley canbekley self-assigned this Nov 12, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Adding a TTL-bearing column can fail when its referenced date column has not yet been added, and the sync tests do not cover codec/TTL updates alongside type changes on existing columns.

2 open issues
What changed in this PR

This PR updates the ClickHouse adapter to apply column-level codec and TTL settings to distributed models’ shard-local tables during creation, rebuilds, and incremental schema changes.

Changes:

  • Apply column storage clauses during table creation, with or without enforced contracts.
  • Include codec and TTL in column additions and modifications, omitting TTL from distributed-table ALTERs.
  • Add integration coverage and a user-facing changelog entry.
File Description
tests/​integration/​adapter/​incremental/​test_schema_change_ttl.py Adds TTL schema-change, rebuild, and creation tests.
tests/​integration/​adapter/​incremental/​test_schema_change_codec.py Checks shard-local codecs and adds rebuild and creation tests.
dbt/​include/​clickhouse/​macros/​materializations/​table.sql Adds a reusable TTL clause macro.
dbt/​include/​clickhouse/​macros/​materializations/​incremental/​schema_changes.sql Applies storage clauses through separate local and distributed ALTER actions.
dbt/​include/​clickhouse/​macros/​materializations/​distributed_table.sql Renders contract-aware column definitions with codec and TTL settings.
CHANGELOG.md Documents the distributed column-storage fix.

Review effort: Balanced

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit ddf0187. Configure here.

@canbekley

Copy link
Copy Markdown
Contributor

hi @Romano-arist, thank you first of all for the contribution! I'll review this PR today and come back to you with my feedback

@canbekley
canbekley self-requested a review November 13, 2026 16:36

@canbekley canbekley left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi again,
generally I like the changes as it fixes a real gap in the distributed_incremental and distributed_table materializations. I would only like to ask for a narrower scope of these changes. our docs (see https://clickhouse.com/docs/integrations/connectors/data-ingestion/etl-tools/dbt/materializations#column-configuration) currently specify that codec and ttl column configurations require contract enforcement to be enabled. for the moment, we wouldn't want to change this expectation (even though applying codec and ttl independently of contract enforcement might seem logical). we would like to change the behaviour eventually as part of separate work, also aligning any sort inconsistencies and also making sure that syncing of ttl/codec changes are being identified. I would share an issue for this work item soon.
would you be ok with keeping the rule of codec/ttl via enforced contracts for this PR, by gating ttl/codec settings at table creation time, column addition and column modification? I noticed that there is already preexisting breaches of this rule in the clickhouse__add_columns method, but I think that can also be aligned within the scope of this PR.
let me know if you have some other thoughts on this and whether you would like to work on the changes :)

@Flipmonster Flipmonster reopened this Nov 26, 2026
@Romano-arist

Copy link
Copy Markdown
Contributor Author

@canbekley Thanks for the review! Codec/ttl are now gated on enforced contracts, modify/drop are batched into a single ALTER, and is_distributed is dropped from the alter macros as you suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants