Skip to content

feat: skip no-op deletes in delete+insert incremental strategy - #788

Draft
canbekley wants to merge 3 commits into
mainfrom
skip-noop-lightweight-delete
Draft

canbekley wants to merge 3 commits into
mainfrom
skip-noop-lightweight-delete

Conversation

@canbekley

@canbekley canbekley commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Replaces #370 with a narrower scope: skip lightweight deletes that would not delete anything, and leave the delete itself unchanged.
Thanks to @Magicbeanbuyer for the original idea and PR, credited as co-author.

Context

delete+insert runs DELETE FROM ... WHERE (unique_key) IN (SELECT unique_key FROM <new data>). By default, ClickHouse executes this as a mutation over every part of the table. Parts without matches are not rewritten, but each part is still checked, and a mutation cannot touch a part while it is being merged. Since 26.9, mutations on ReplicatedMergeTree skip partitions the condition rules out (ClickHouse#110968), but that only helps when the partition key can be derived from the unique key.

The adapter sets lightweight_deletes_sync=3 (dbclient.py), so the run waits for the mutation, and therefore for any ongoing merges, even when no row matches.

Change

Before the delete, check whether any existing row would be deleted, and skip the delete if not:

select 1 from <existing_relation>
where (<unique_key>) [global] in (select <unique_key> from <new data>) <incremental_predicates>
limit 1
  • the check uses the same predicates as the delete, including the microbatch time window.
  • distributed models need global in; plain in fails on multi-shard clusters with DISTRIBUTED_IN_JOIN_SUBQUERY_DENIED.
  • the workaround for ClickHouse#69559 from Skip lightweight delete when there is no data to delete #370 (inlining the key values) is not included; the delete query is unchanged.
  • a null incremental_predicates config is now treated as an empty list.

Cost and why there's no flag

This adds one query per run.

  • No match: the query replaces a mutation, and the mutation's wait on merges, with one read of roughly what the mutation would have checked part by part.
  • Matches: limit 1 stops at the first match, so the overhead is small compared to the delete that follows.

This also addresses the concern in this review. models whose increments always overlap pay the least, so I don't think a flag is worth the extra configuration.

Tests

New integration tests for regular and distributed models, checked against system.mutations: no delete when no keys match, one delete once they do. Both fail against main. Passing on a single node, test_replica and test_shard.

Checklist

Delete items not relevant to your PR:

  • Unit and integration tests covering the common scenarios were added
  • A changelog entry was added following Updating the Changelog (user-facing, concise, PR link)
  • For significant changes, documentation in docs was updated with further explanations or tutorials

Note

Medium Risk
Changes core incremental materialization behavior for delete+insert/microbatch runs (extra pre-delete query, conditional delete), though the delete SQL itself is unchanged when matches exist.

Overview
delete+insert now runs a cheap LIMIT 1 existence check (same unique_key, incremental predicates, and global in on distributed models) before issuing a lightweight delete; when nothing would be removed, it logs and skips the mutation while still inserting new rows.

Incremental macros also treat a null predicates / incremental_predicates config as an empty list, and build predicate SQL one clause per line so a trailing -- on one predicate cannot comment out the next.

Docs and the changelog note the skip behavior; integration tests cover no-match vs match paths (local and distributed, via system.mutations) and predicates that include inline comments.

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

canbekley and others added 2 commits November 10, 2026 11:04
…match new data

Co-authored-by: Xiatong Zheng <xiatong.zheng@deepl.com>

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

Joining predicates on one line lets SQL comments swallow subsequent filters, potentially causing DELETE to remove rows outside the intended scope, including microbatch time windows.

1 open issue
What changed in this PR

Optimizes dbt-clickhouse’s delete+insert incremental strategy by checking for matching rows before issuing a lightweight delete, avoiding unnecessary mutations.

Changes:

  • Adds an existence check using incremental predicates and GLOBAL IN for distributed models.
  • Treats null predicate configurations as empty lists.
  • Adds local and distributed mutation-count tests, documentation, and a changelog entry.
File Description
tests/​integration/​adapter/​incremental/​test_distributed_incremental.py Tests skipped and required deletes for distributed models.
tests/​integration/​adapter/​incremental/​test_base_incremental.py Adds local-model tests and a mutation-count helper.
docs/​materializations.mdx Documents skipping deletes when no rows match.
dbt/​include/​clickhouse/​macros/​materializations/​incremental/​incremental.sql Adds the existence check and normalizes null predicates.
dbt/​include/​clickhouse/​macros/​materializations/​incremental/​distributed_incremental.sql Normalizes null predicates for distributed models.
CHANGELOG.md Records the incremental-strategy optimization.

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

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 068169e. Configure here.

{% endcall %}
{%- endset -%}

{% if run_query(rows_to_delete_query).rows | length > 0 %}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Distributed skip check can miss rows

High Severity

The no-op probe reads existing_relation through the Distributed engine (one replica per shard), while the delete still runs ON CLUSTER against the local tables. A lagging replica can make the probe see no match even though local rows would have been deleted. The insert then proceeds and duplicates unique keys. insert_quorum is off unless cluster_mode or database_engine: Replicated is set, so this is the default distributed cluster shape.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 068169e. Configure here.

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.

🔵 Needs a closer look

The new probe can fail on multi-shard models under the default distributed_product_mode='deny' when incremental predicates contain distributed subqueries.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Use global mode for distributed subqueries in the probe

dbt/​include/​clickhouse/​macros/​materializations/​incremental/​incremental.sql:233

On a multi-shard model with the default distributed_product_mode = 'deny', an incremental predicate such as id IN (SELECT id FROM allowed_keys_distributed) makes the new probe fail with DISTRIBUTED_IN_JOIN_SUBQUERY_DENIED. The DELETE evaluates this predicate against the local table, but the probe reads the distributed table and only makes the generated key condition GLOBAL IN. Handle distributed subqueries in user predicates for the probe too, for example with a probe-specific distributed_product_mode = 'global' setting, while leaving the DELETE unchanged.

1 resolved since last review

Review effort: Balanced

@canbekley canbekley changed the title Skip no-op deletes in delete+insert incremental strategy feat: skip no-op deletes in delete+insert incremental strategy Nov 11, 2026
@canbekley
canbekley marked this pull request as draft November 11, 2026 11:33
@koletzilla
koletzilla removed their request for review November 11, 2026 16:16
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.

2 participants