Skip to content

Number the monotonic chain functions in KeyCondition instead of keying their cached results by address - #127658

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:key-condition-number-chain-functions
Oct 24, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:key-condition-number-chain-functions

Conversation

@groeneai

@groeneai groeneai commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #127495
Related: #68354

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Index analysis names the cached results of monotonic key functions by a number assigned when the condition is built, instead of by the function address.

Description

Follow-up to #127495, as asked in #127495 (comment).

applyFunction caches the result of a monotonic key function, computed over a whole index column, in the block the FieldRef points to, under the name _<function address>_<column>. The address came from #68354: the earlier _<function name>_<column> let intDiv(a, 50) and intDiv(a, 50000) share an entry, because a function with a constant argument reports only its bare name.

Now every function of every chain gets a number when the KeyCondition is built (one pass at the end of the constructor; the chains of a set atom are numbered after it, so MergeTreeSetIndex::checkInRange takes the first number), and the name is _<function number>_<column>, still formatted on the stack.

A short numbered name can equal a key column name, for example a key column named _0_1, which the address could not. So the columns that markRangesFromPKRange and the text index put into the block are now unnamed; nothing reads those names.

Validation: the new test has a key column named _0_1 and two chains with different constants on one key column (a range and a set), on both primary key analysis paths. It passes on master, and fails if either block keeps its column names, if the set chains are numbered from 0, or if every chain is numbered from 0. In a RelWithDebInfo build, the primary key analysis time of the long_primary_key queries is within 2% of the base.


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

Version info

  • Merged into: 26.11.1.725-master (included in 26.11 and later)

groeneai and others added 2 commits October 23, 2026 21:36
…g their cache by address

`applyFunction` caches the result of a chain function, computed over a whole
column of the in-memory primary index, in the block the `FieldRef` points to,
under the name `_<function address>_<column>`. Every function of every chain
now gets a number when the `KeyCondition` is built (one pass at the end of the
constructor; the chains of a set index are numbered after its atom, so
`MergeTreeSetIndex::checkInRange` takes the first number), and the name is
`_<function number>_<column>`.

A short numbered name could match a key column name (a key column named
`_0_1`), which the address could not, so the columns that
`markRangesFromPKRange` and the text index put into the block are now unnamed.

Follow-up to ClickHouse#127495.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Remove the two parameter comments on first_function_number; the numbering is documented on RPNElement::first_function_number.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 findings; 1 full pass), plus my own review (0 findings).

# Sev Finding Verdict Evidence / action
1 ⚠️ The new test passes on master, and the IN chain could return unknown without changing the counts DISAGREE Numbering the set chains from 0 alone turns both set queries (full and lightweight path) to 0, so the set chain does go through the cache. The change alters no behavior, so no SQL test can fail on revert. The test catches the two hazards a numbered key creates, which 03221_key_condition_bug does not.
2 ⚠️ long_primary_key has no key function in WHERE, so it does not measure the change DISAGREE toDate(dt) AS dt makes the WHERE dt a toDate chain (#127495 halved this workload by changing only this key). Queries with 2 and 5 explicit chain atoms show the same flat ~1.5 ms offset. No speed claim beyond the measured queries.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20261023-222600

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Not a bug fix. The failing side is four mutations of this change (block columns named at either PK analysis site, set chains numbered from 0, every chain numbered from 0); each fails the new test on every run.
b Root cause explained? The cache needs a name unique per (function, argument column) within one block. The address was such a name; a number assigned when the condition is built is one too, as long as the block's own columns have no names a number could match.
c Fix matches root cause? The number replaces the address at the single key site, and the possible clash with key column names is removed where the blocks are built.
d Test intent preserved / new tests added? New 05446_key_condition_function_cache_key: a key column named _0_1, a range and a set chain with different constants on one key column, both PK analysis paths. 03221_key_condition_bug unchanged.
e Both directions demonstrated? Passes on master and with the change (50/50 randomized); fails under each of the four mutations.
f Fix is general across code paths? Both chain call sites in KeyCondition, both MergeTreeSetIndex::checkInRange overloads, both markRangesFromPKRange blocks and the text index block (the only column-backed FieldRef creators).
g Fix generalizes across inputs (params/datatypes/wrappers)? Chains of any length, atom groups, multi-column sets, atoms without chains; type handling in applyFunction is untouched.
h Backward compatible? (maintainer-approved exception only) Yes: in-memory and per query, no setting, format or on-disk change.
i Invariants and contracts preserved? Within a block a name identifies one (function, argument column) pair and never a caller's column; numbers are written once in the constructor and only read afterwards; the failure sentinel keeps its meaning.

Session id: cron:clickhouse-impl-slot-6:20261023-202900

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @nihalzp @shankar-iyer, could you review this and add the can be tested label so CI runs? It replaces the function address in the cache key of KeyCondition::applyFunction with a number given to each monotonic chain function when the KeyCondition is built, and leaves the index block columns unnamed so a numbered key cannot match a key column name.

@clickhouse-gh

clickhouse-gh Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [37cad99]

Summary: ✅


AI Review

Summary

KeyCondition replaces function addresses in its result-cache keys with per-condition function numbers and leaves source index columns unnamed to prevent collisions. No actionable findings at commit 37cad994139d18c8279a7ea307f018f7461cf3e9. The focused regression test 05446_key_condition_function_cache_key passed in Fast test.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Oct 23, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 37cad9941 with master eddf9eec0 (stripped binary size, per-symbol sizes and ThinLTO time; 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

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

Job report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All good.

@alexey-milovidov alexey-milovidov self-assigned this Oct 24, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 24, 2026
Merged via the queue into ClickHouse:master with commit 9a15930 Oct 24, 2026
352 of 353 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 Oct 24, 2026
@clickhouse-gh clickhouse-gh Bot added comp-key-condition Analysis of a filter against a key. KeyCondition turns a WHERE condition into ranges over the pri... and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-key-condition Analysis of a filter against a key. KeyCondition turns a WHERE condition into ranges over the pri... pr-not-for-changelog This PR should not be mentioned in the changelog 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.

3 participants