Skip to content

fix: apply on_schema_change to incremental models without a unique_key - #763

Closed
sshari94 wants to merge 4 commits into
ClickHouse:mainfrom
sshari94:fix/258-schema-change-without-unique-key
Closed

sshari94 wants to merge 4 commits into
ClickHouse:mainfrom
sshari94:fix/258-schema-change-without-unique-key

Conversation

@sshari94

@sshari94 sshari94 commented Oct 26, 2026 •

Copy link
Copy Markdown

Summary

Closes #258.

Incremental models without a unique_key (or with inserts_only=True) insert the new rows straight into the existing table, and that path never looked at on_schema_change. Adding a column to such a model therefore failed with NUMBER_OF_COLUMNS_DOESNT_MATCH (code 20) even with on_schema_change='append_new_columns', and on_schema_change='fail' never reported the drift.

The insert-only paths of incremental and distributed_incremental now run the same check_incremental_schema_changes / clickhouse__apply_column_changes step as the other strategies before inserting. ignore behaves as before.

Checklist

  • Unit and integration tests covering the common scenarios were added (TestOnSchemaChangeWithoutUniqueKey: append_new_columns with no key, with inserts_only, and on distributed_incremental, plus fail). 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.
  • A changelog entry will follow once this PR has a number.

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_key or inserts_only=True used to skip on_schema_change and insert straight into the existing table, which caused NUMBER_OF_COLUMNS_DOESNT_MATCH when adding columns with append_new_columns and left fail silent.

The insert-only branches in incremental and distributed_incremental now run check_incremental_schema_changes and clickhouse__apply_column_changes before the insert when on_schema_change is not ignore, matching the behavior of keyed strategies. ignore is unchanged.

Integration coverage adds TestOnSchemaChangeWithoutUniqueKey for append and fail on plain incremental, inserts_only, and distributed inserts_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.

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.
@CLAassistant

CLAassistant commented Oct 26, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

…nto fix/258-schema-change-without-unique-key

# Conflicts:
#	CHANGELOG.md

@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 5974f4d. Configure here.

@canbekley canbekley self-assigned this Nov 13, 2026
@canbekley
canbekley requested a balanced review from Copilot November 13, 2026 20:33
@canbekley
canbekley self-requested a review November 13, 2026 20:34

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

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 ignore behavior 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) }}
@canbekley

canbekley commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

hi @sshari94, thank you for opening this PR. the inserts_only model configuration has been deprecated in favor of the standard append incremental strategy (see docs). it does exactly what you were aiming for in this PR: applying schema changes before the insert.
I would close this PR, but I will also follow up with a some changes to add clear warning logs (with pointers to append strategy). I think the issue in #258, but will have a look.

@canbekley canbekley closed this Nov 13, 2026
@canbekley

Copy link
Copy Markdown
Contributor

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 inserts_only when no unique_key was set. I have created #802 to fix this issue fundamentally by changing the default incremental materialization and removing the inserts_only fall back. do you think it will solve your issue?
I would still keep the explicit inserts_only configuration deprecated and without proper schema change handling.

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.

Cannot add new columns to incremental models

4 participants