Skip to content

Fix deferred filters with partition pruning on non-sorting key - #98242

Merged
alexey-milovidov merged 5 commits into
masterfrom
yarik/fix-where-with-final-replacing-mt
Mar 1, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
yarik/fix-where-with-final-replacing-mt

Conversation

@yariks5s

@yariks5s yariks5s commented Feb 27, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

If the partition key columns are not covered by the sorting key, then partition pruning could incorrectly skip partitions containing rows that should "win" during FINAL deduplication

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.3.1.194
  • Backported to: 26.2.19.21

@clickhouse-gh

clickhouse-gh Bot commented Feb 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [11f0b3c]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Feb 27, 2026
@yariks5s yariks5s changed the title Yarik/fix where with final replacing mt Fix deferred filters with partition pruning on non-sorting key Feb 27, 2026
@alexey-milovidov alexey-milovidov self-assigned this Mar 1, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Mar 1, 2026
Merged via the queue into master with commit 75d8d22 Mar 1, 2026
144 checks passed
@alexey-milovidov
alexey-milovidov deleted the yarik/fix-where-with-final-replacing-mt branch March 1, 2026 04:36
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 1, 2026
alexey-milovidov added a commit that referenced this pull request Mar 1, 2026
…replacing-mt

Fix deferred filters with partition pruning on non-sorting key
clickgapai pushed a commit to clickgapai/ClickHouse that referenced this pull request Mar 30, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @yariks5s @alexey-milovidov — while reviewing this PR I found the following:

Could you review the test PR and add the can be tested label if it looks good?
Happy to discuss — close anything that's wrong or already addressed.

@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this PR may need backporting to release branches 26.3, 26.2, 26.1, 25.12, 25.11, 25.10, 25.9, 25.8, but no backport label was found.

Why: This fixes silent wrong results (P0) in a core query path (FINAL with partition pruning) that has existed since deferred filters were introduced. The affected code in ReadFromMergeTree is present in all active release branches.

If this should be backported, consider adding pr-must-backport or a version-specific label (e.g. v26.3-must-backport). Ignore this if backporting is not applicable.

robot-clickhouse added a commit that referenced this pull request May 12, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label May 12, 2026
yariks5s added a commit that referenced this pull request May 13, 2026
Backport #98242 to 26.2: Fix deferred filters with partition pruning on non-sorting key
pull Bot pushed a commit to mikeyhodl/ClickHouse that referenced this pull request May 15, 2026
PR ClickHouse#98242 (commit `ebc5cb49baa594b87f4631850ae9902424414113`, landed in 26.3)
disabled partition pruning unconditionally for any `FINAL` query whose
partition-key columns are not part of the sorting key. The intent was
correctness: a `FINAL` merge may need to deduplicate same-primary-key rows
that live in different partitions, so partition pruning would silently drop
input rows.

In practice this hits event-log workloads hard. Their tables are partitioned
by the event timestamp, which is set once at insert time and never changes —
same-PK rows cannot span partitions. Reported wall-clock and memory regressions
on real customer queries are around 6× and 4.4× respectively
(see ClickHouse#104263).

This commit makes the deferral opt-out via a new setting
`defer_partition_pruning_after_final`:

  - 1 (default): preserves the 26.3+ correctness behavior.
  - 0: restores pre-26.3 partition pruning before FINAL. Safe only when the
       user can assert same-PK rows cannot exist in different partitions.

The setting follows the same pattern as `apply_row_policy_after_final` and
`apply_prewhere_after_final`. `compatibility = '26.2'` flips it to 0 via
`SettingsChangesHistory`.

A stateless regression test covers both the issue's reproducer (pruning works
under opt-out) and the den-crane pathological case (same PK in two partitions
with reversed versions — default deduplicates correctly across partitions,
opt-out trusts the user's assertion and prunes first).

Issue: ClickHouse#104263
pull Bot pushed a commit to mikeyhodl/ClickHouse that referenced this pull request May 15, 2026
26.3 is the version where the silent behavior change happened (PR ClickHouse#98242).
Putting the entry under 26.3 makes `compatibility = '26.2'` correctly revert
the setting to 0 (pre-regression behavior); `compatibility = '26.3'+` keeps
the post-regression default of 1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v25.12-must-backport v26.2-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants