Skip to content

Fix primary key pruning for reversed key columns in parts without a final mark - #109901

Merged
nihalzp merged 6 commits into
ClickHouse:masterfrom
nihalzp:fix-reverse-key-pruning-without-final-mark
Jul 17, 2026
Merged

nihalzp merged 6 commits into
ClickHouse:masterfrom
nihalzp:fix-reverse-key-pruning-without-final-mark

Conversation

@nihalzp

@nihalzp nihalzp commented Jul 9, 2026 •

Copy link
Copy Markdown
Member

This bug fix is needed for #96225 which will amplify any bug present in KeyCondition.

When a table has a reversed (DESC) key column and its parts have no final mark (non-adaptive granularity, index_granularity_bytes = 0), the primary key index builds the last granule's range with the unknown side substituted as +Inf. For a reversed column the range end extends toward the smaller values, so the substituted bound must be -Inf on the value-ascending left side instead. The malformed range (+Inf, mark] made the boundary search silently prune matching granules, returning wrong results:

CREATE TABLE t (x UInt64) ENGINE = MergeTree ORDER BY x DESC
SETTINGS index_granularity = 1, index_granularity_bytes = 0;

INSERT INTO t SELECT number FROM numbers(24);

SELECT count() FROM t WHERE x > 11; -- returns 1 instead of 12

Equality and IN predicates are affected the same way, on both the lightweight and the full primary key analysis paths. In debug builds the malformed range can also reach the Invalid binary search result in MergeTreeSetIndex logical error.

The PR also includes, currently dormant, wrong monotonic chain positivity validation.

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):

Fix wrong query results caused by the primary key index incorrectly pruning granules for tables with a reversed (DESC) key column when parts have no final mark (non-adaptive granularity, index_granularity_bytes = 0).

Version info

  • Merged into: 26.7.1.1096 (included in 26.7 and later)

@clickhouse-gh

clickhouse-gh Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e9af7ad]

Summary: ✅


AI Review

Summary

This PR fixes wrong primary-key pruning for DESC sorting keys in parts without a final mark by using the correct virtual boundary for reversed columns, and it adds focused stateless coverage for both plain and Nullable reversed keys. The current diff also addresses the previously raised nullable-edge-case concern in MergeTreeDataSelectExecutor, and I did not find remaining correctness, metadata, or test-evidence gaps in the PR as it stands.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.80% 85.80% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 77.90% 77.90% +0.00%

Changed lines: Changed C/C++ lines covered: 26/27 (96.30%) · Uncovered code

Full report · Diff report

@nihalzp
nihalzp added this pull request to the merge queue Jul 17, 2026
Merged via the queue into ClickHouse:master with commit 235e9ea Jul 17, 2026
176 checks passed
@nihalzp
nihalzp deleted the fix-reverse-key-pruning-without-final-mark branch July 17, 2026 14:01
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 17, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.

TL;DR

Fix primary key pruning for reversed key columns in parts without a final mark

ClickGap verified: reproduced it with the script below on 26.6; tested 2 version(s) — 1 affected, 1 clean (see Affects); bisected the regression to a single PR (#106440).

Reproduction

CREATE TABLE t (x UInt64) ENGINE = MergeTree ORDER BY x DESC
SETTINGS index_granularity = 1, index_granularity_bytes = 0;
INSERT INTO t SELECT number FROM numbers(24);
-- expected 12, returns 1 before the fix:
SELECT count() FROM t WHERE x > 11;

Bisect

Affects

  • Verified against master build 5a90b3bbc0fa (aarch64)
  • ✓ Fix is on master — this PR's change fixes the bug on master. Branches below still have the bug and need the backport.
  • Reproduces on: 26.6
  • Does NOT reproduce on: master
  • Feature not present (repro inconclusive — the affected code path postdates these releases): 26.5, 26.4, 26.3, 26.2, 26.1
  • Backport needed: 26.6

Suggested next step

Review PR #106440 for the introducing change in comp-mergetree. If a forward-fix or revert is intended, the bisect data above narrows the search space.

Component: comp-mergetree
Severity: P2

cc introducing PR author: @nikitamikhaylov
cc introducing PR reviewer: @clickhouse-gh @Algunenano
cc fix PR author: @nihalzp
cc fix PR reviewer: @yariks5s


ClickGap · Finding: phase_d_pr109901

Triage metadata

zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ning-without-final-mark

Fix primary key pruning for reversed key columns in parts without a final mark
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants