Skip to content

Fix patch pruning for parenthesized sorting keys - #118878

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
CurtizJ:fix-patch-key-parentheses
Sep 12, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
CurtizJ:fix-patch-key-parentheses

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

With ORDER BY (k), the stored patch key can be parsed as k. Comparing the formatted expressions treats these equivalent keys as different and discards their effective common prefix, causing unnecessary patch reads even when the sorting key is unique. Compare expressions with IAST::formatIgnoringRedundantParentheses to preserve the common prefix.

Closes: #118024
Related: #103182

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 excessive patch-part reads for lightweight updates on tables with redundant parentheses in sorting-key expressions, such as ORDER BY (k).


Workflow [PR]
Sync PR [sync-upstream/pr/118878]

Version info

  • Merged into: 26.9.1.1287 (included in 26.9 and later)

Use `IAST::formatIgnoringRedundantParentheses` when comparing effective
sorting-key expressions. Equivalent spellings such as `k` and `(k)` retain
the common prefix needed for patch range pruning.

Add a regression test covering pruning, key spelling changes, and patch
materialization.

Related: ClickHouse#118024
Related: ClickHouse#103182
@clickhouse-gh

clickhouse-gh Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0530c39]

Summary: ✅


AI Review

Summary

This PR fixes the v2 patch-key common-prefix comparison for single-element sorting keys written with redundant parentheses and adds a focused regression test around pruning and post-ALTER materialization. The parenthesis case is covered, but the comparison is still asymmetric between reparsed patch keys and the live table metadata, so equivalent function spellings can still collapse the effective prefix to zero and bring back the full-patch read path.

Findings

⚠️ Majors

  • [src/Storages/MergeTree/PatchParts/PatchPartsUtils.cpp:214] The effective patch-key comparison still depends on surface spelling for function names and aliases. patch_sorting_key comes from KeyDescription::parse, which canonicalizes function names, while storage_metadata->getSortingKey() is built from the original ORDER BY AST and preserves the user's spelling. A table declared as ORDER BY ABS(k) still compares against a reparsed patch key abs(k) as different, which drives effective_key_size to 0 and restores the same unpruned v2 patch reads for expression keys.
    Suggested fix: normalize both ASTs before formatting, for example by cloning and applying FunctionNameNormalizer, and add a focused regression that changes only function spelling or alias.
Final Verdict

Changes requested.

LLVM Coverage Report

Measured on commit 0530c39.

Metric Baseline Current Δ
Lines 88.80% 88.80% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.20% 81.20% +0.00%

Changed lines: Changed C/C++ lines covered: 3/3 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 8, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). label Sep 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 0530c39fb with master e614ecd58 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 417aa9966; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

8 translation units recompiled, 16 s compile time in total, 8 of them have a recent master baseline.

Job report

@Diskein Diskein self-assigned this Sep 11, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 12, 2026
Merged via the queue into ClickHouse:master with commit 458d09b Sep 12, 2026
174 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). 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.

Single-column ORDER BY (col) makes MergeOnKey patch application ~480x slower and disables patch range pruning

4 participants