Skip to content

Use ifNull(y, c) and coalesce(y, c) keys for comparisons, patterns and IS NULL on y - #123858

Open
raimannma wants to merge 11 commits into
ClickHouse:masterfrom
raimannma:key-condition-ifnull-constant
Open

raimannma wants to merge 11 commits into
ClickHouse:masterfrom
raimannma:key-condition-ifnull-constant

Conversation

@raimannma

@raimannma raimannma commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

A predicate on a column y can now use a primary key, partition key or skipping index on ifNull(y, c) or coalesce(y, c):

  • comparisons (y >= 5, y BETWEEN ...), when converting y to the key type preserves order (same type, or numbers);
  • LIKE / startsWith / match, when y and the key are String;
  • isNull(y) and y <=> NULL, as key = c.

A row satisfying the predicate has a non-NULL y, for which the key value is y itself; NULL rows only cause extra reading, never wrong results.

Should be merged after #123868, which fixes a FixedString conversion bug that this change would otherwise expose for ifNull keys.

Related: #123868

Changelog category (leave one):

  • Performance Improvement

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

Comparisons, LIKE/startsWith/match, IS NULL and <=> NULL on a Nullable column y can now use a primary key, partition key or skipping index on ifNull(y, c) or coalesce(y, c).


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

raimannma and others added 6 commits October 4, 2026 14:05
A comparison such as `y >= X` could not use a sorting or partition key on
`ifNull(y, c)` or `coalesce(y, c)`, because these functions are not monotonic on
`Nullable` input in general. Pushing the constant through them for a relaxed atom
is sound, by the same argument already used for `assumeNotNull`: a row that
satisfies the predicate has a non-NULL `y`, for which the key value is `y`
itself (possibly converted to the common supertype, which preserves order), and
a NULL row gets the key value `c`, which can only keep a granule as a false
positive.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The common type of `ifNull(y, c)` may order the values of `y` differently: for
`y` of type `Enum8('z' = 1, 'a' = 2)` and `c = ''` the key is a `String`, so
`y > 'z'` was pushed through as `ifNull(y, '') >= 'z'` and pruned the parts with
`'a'`. Accept these functions only when the common type is the type of `y` or
both are numbers.

Found by a local codex review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
For pattern predicates (`LIKE`, `startsWith`, `match`) the constant is not a
value of the key domain: converting it to the argument type can change it (a
prefix `'a'` becomes `'a\0\0'` for `FixedString(3)`), and in `ifNull(c, y)` it is
replaced by `c`. Both pruned matching rows. Restrict the `ifNull` / `coalesce`
case to order comparisons, which is what it was added for.

Found by a local codex review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ll` on `y`

A pattern on `y` is pushed through `ifNull(y, c)` / `coalesce(y, c)` when both
`y` and the key are `String`, so the pattern passes through unchanged. The
function must have `y` as its first argument: `ifNull(c, y)` is `c`.

`isNull(y)` implies `key = c` for such a key, so it becomes a relaxed atom
`key = c` instead of a full scan.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nsform

Casting a `FixedString` to `String` drops its trailing zero bytes, so
`y <= toFixedString('abc', 4)` became `ifNull(y, '') <= 'abc'` and pruned rows
with `y = 'abc\0'`. The same happened for an `assumeNotNull(y)` key.

Found by a local codex review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Oct 4, 2026
`y <=> NULL` is the same as `isNull(y)`, so it gets the same relaxed `key = c`
atom.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@raimannma raimannma changed the title Use ifNull(y, c) and coalesce(y, c) keys for comparisons on y Use ifNull(y, c) and coalesce(y, c) keys for comparisons, patterns and IS NULL on y Oct 4, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [16747ea]

Summary: ✅


AI Review

Summary

This PR lets comparisons, string patterns, and null checks use index keys built with ifNull or coalesce. The implementation conservatively retains possible matches, and no actionable findings remain.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Oct 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 16747ea2e with master 0a3052562 (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

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

Job report

@clickhouse-gh

clickhouse-gh Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

⚠️ AI verdict: not_sure — inconclusive across 31 queries analysed

Baseline selected using StressHouse version hint 26.10.1.41020; measured build ancestry could not be verified. Measured differences are not attributed to this PR. ⚙️ 1 flagged delta falls inside the range master itself produced for the same query over the last 7 days — environment variance, not attributable to this PR.

PR verdict: not_sure. 2 measured difference(s) not attributable to this PR. clickbench Q20: The measured difference cannot be attributed to this PR from the available source evidence. clickbench Q32: The measured difference cannot be attributed to this PR from the available source evidence.

clickbench

⚠️ 3 inconclusive

Flagged queries (3 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 16 not_sure 528 610 +15.5% <0.0001
⚠️ 20 not_sure 222 128 -42.3% <0.0001 The measured difference cannot be attributed to this PR from the available source evidence.
⚠️ 32 not_sure 711 948 +33.4% <0.0001 The measured difference cannot be attributed to this PR from the available source evidence.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 825e1eef-9c2e-4aa1-a1da-4c9c1b1477a9
  • MIRAI run: 51ba8fac-94b9-4247-b46a-db6655f277f8
  • PR check IDs:
    • clickbench_241360_1792242192
    • clickbench_241380_1792242192
    • clickbench_241395_1792242192
    • tpch_adapted_1_official_241425_1792242192
    • tpch_adapted_1_official_241447_1792242192
    • tpch_adapted_1_official_241468_1792242192

@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Oct 5, 2026
raimannma and others added 2 commits October 17, 2026 13:17
…ull-constant

# Conflicts:
#	src/Storages/MergeTree/KeyCondition.cpp
#	src/Storages/MergeTree/KeyCondition.h
… strict bound master now tightens

Master pushes a strict integer bound through the key chain as the inclusive bound next to it, so
`y < 3` now shows as `(-Inf, 2]`. The selected granules are the same.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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 19, 2026

This branch has not been deployed

No deployments
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... external Author is not a member of the ClickHouse organization pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants