Repository navigation
Conversation
Without a unique_key (or with inserts_only) the incremental materialization inserts the new rows straight into the existing table and never looked at on_schema_change, so adding a column failed with NUMBER_OF_COLUMNS_DOESNT_MATCH even with append_new_columns, and fail never reported the drift. Closes ClickHouse#258.
…nto fix/258-schema-change-without-unique-key # Conflicts: # CHANGELOG.md
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 5974f4d. Configure here.
There was a problem hiding this comment.
🟡 Changes recommended
A critical positional-insert issue in both incremental materializations can silently swap values or fail when a column is added in the middle, requiring destination-column ordering and a regression test.
1 open issue
What changed in this PR
Extends schema-change handling to insert-only incremental models in dbt-clickhouse, addressing the column-addition failure reported in #258.
Changes:
- Checks and applies schema changes before inserts in both incremental materializations, leaving
ignorebehavior unchanged. - Adds integration coverage for column additions and schema-drift failures.
- Documents the fix in the changelog.
| File | Description |
|---|---|
tests/integration/adapter/incremental/test_schema_change.py |
Adds no-key, insert-only, and distributed schema-change regression cases. |
dbt/include/clickhouse/macros/materializations/incremental/incremental.sql |
Applies schema-change handling before direct inserts. |
dbt/include/clickhouse/macros/materializations/incremental/distributed_incremental.sql |
Adds schema-change handling to distributed insert-only runs. |
CHANGELOG.md |
Describes the user-visible fix and links the PR. |
Review effort: Balanced
| {% endif %} | ||
| {% endif %} | ||
| {% call statement('main') %} | ||
| {{ clickhouse__insert_into(target_relation, sql, has_contract) }} |
|
hi @sshari94, thank you for opening this PR. the |
|
hi again @sshari94, I did initially underestimate the scope if this issue. after having a further look into #258 I noticed the root cause being the unintended fall back to |


Summary
Closes #258.
Incremental models without a
unique_key(or withinserts_only=True) insert the new rows straight into the existing table, and that path never looked aton_schema_change. Adding a column to such a model therefore failed withNUMBER_OF_COLUMNS_DOESNT_MATCH(code 20) even withon_schema_change='append_new_columns', andon_schema_change='fail'never reported the drift.The insert-only paths of
incrementalanddistributed_incrementalnow run the samecheck_incremental_schema_changes/clickhouse__apply_column_changesstep as the other strategies before inserting.ignorebehaves as before.Checklist
TestOnSchemaChangeWithoutUniqueKey:append_new_columnswith no key, withinserts_only, and ondistributed_incremental, plusfail). Against ClickHouse 25.8 the new tests fail with code 20 before the change and pass after it. The rest of the integration suite has the same results with and without the change. I could not run the cluster variant locally, so that one relies on CI.Note
Medium Risk
Changes when incremental runs ALTER tables vs insert for a common model configuration; fixes a bug but can alter behavior for schemas that drift under insert-only models.
Overview
Incremental models with no
unique_keyorinserts_only=Trueused to skipon_schema_changeand insert straight into the existing table, which causedNUMBER_OF_COLUMNS_DOESNT_MATCHwhen adding columns withappend_new_columnsand leftfailsilent.The insert-only branches in
incrementalanddistributed_incrementalnow runcheck_incremental_schema_changesandclickhouse__apply_column_changesbefore the insert whenon_schema_changeis notignore, matching the behavior of keyed strategies.ignoreis unchanged.Integration coverage adds
TestOnSchemaChangeWithoutUniqueKeyfor append and fail on plain incremental,inserts_only, and distributedinserts_only, plus a CHANGELOG bug note.Reviewed by Cursor Bugbot for commit de38a17. Bugbot is set up for automated code reviews on this repo. Configure here.