Skip to content

Apply optimizations to Nullable columns the same way as to non-Nullable - #125011

Merged
alexey-milovidov merged 5 commits into
masterfrom
nullable-generic-fixes
Oct 12, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
nullable-generic-fixes

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Several optimizations were applied only to non-Nullable columns, and this made Nullable columns much slower than non-Nullable ones in NullableClickBench (ClickBench with all columns Nullable and no NULLs):

  • The -Null combinator did not forward isParallelizeMergePrepareNeeded, so the states of uniqExact over Nullable arguments were not prepared for the parallel merge. Q5 (COUNT(DISTINCT UserID)): 0.113 sec. -> 0.057 sec.
  • length, empty, notEmpty of Nullable(String) and the comparisons of Nullable(String) with an empty string were not rewritten to read the size subcolumn. Q28 (AVG(length(URL)) ... WHERE URL <> ''): 0.176 sec. -> 0.039 sec.
  • sum(x + c) was not rewritten to sum(x) + c * count(x) for Nullable x. Q30 (90 sums of ResolutionWidth + k): 0.264 sec. -> 0.025 sec.
  • empty and notEmpty of a Nullable(String) primary key column were not analyzed by KeyCondition, which is needed after the rewrite of the comparisons with an empty string.

The results for the non-Nullable table are the same as before (0.058, 0.034, 0.018 sec.).

Related: #124762

Changelog category (leave one):

  • Performance Improvement

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

Queries over Nullable columns use the same optimizations as over non-Nullable ones: length, empty, notEmpty and comparisons with an empty string of Nullable(String) read the size subcolumn, sum(x + c) is rewritten to sum(x) + c * count(x), and uniqExact over Nullable is merged in parallel. This makes some queries several times faster.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.2214-master (included in 26.10 and later)

…lable`

Several optimizations were applied only to non-`Nullable` columns, which made
`Nullable` columns much slower in NullableClickBench (ClickBench with all
columns `Nullable`):
- the `-Null` combinator did not forward `isParallelizeMergePrepareNeeded`,
  so the states of `uniqExact` over `Nullable` were not prepared for the
  parallel merge;
- `length`, `empty`, `notEmpty` of `Nullable(String)` and the comparisons of
  `Nullable(String)` with an empty string were not rewritten to read the `size`
  subcolumn;
- `sum(x + c)` was not rewritten to `sum(x) + c * count(x)` for `Nullable` `x`;
- `empty` and `notEmpty` of a `Nullable(String)` key were not analyzed by the
  primary key, which is needed after the rewrite of the comparisons with an
  empty string.

#124762

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

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9551d18]

Summary: ✅


AI Review

Summary

This PR extends several existing non-Nullable optimizations to Nullable columns in a way that stays consistent with current analyzer and aggregation contracts: sum(x ± c) now preserves NULL-skipping semantics, Nullable(String) can use the size subcolumn rewrite when it is safe, uniqExact regains the parallel-merge prepare fast path through the Null and adapter combinators, and empty-string predicates on nullable string keys remain analyzable by the primary key. I did not find a remaining correctness, safety, or performance-contract issue in the current diff.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Oct 9, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Oct 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 31 queries analysed

This PR extends three query-tree rewrites to Nullable columns: the size-subcolumn rewrite for length/empty/notEmpty on Nullable(String), the sum(x + c) rewrite, and primary-key analysis of empty/notEmpty. It also makes the -If, -Null, -Array, -Merge and -State combinators pass isParallelizeMergePrepareNeeded through to the wrapped function. None of the flagged ClickBench or TPC-H queries reach this code: their columns are not Nullable and they use no combinators where this flag matters, so I downgraded the five improvements (ClickBench Q18, Q33, Q34 and TPC-H Q1, Q7) to not sure. In each of them the PR median is inside master's current range and the baseline sits at its slow end, which points to a slow baseline rather than a real speedup. ClickBench Q28 (-5.3%) stays not sure because the two tests disagree and the PR runs were noisy.

clickbench

⚠️ 4 inconclusive

Flagged queries (4 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 18 not_sure 891 790 -11.3% <0.0001 aggregation: The PR median of 790 ms is inside master's current range of 772-1062 ms. The hits table has no Nullable columns, so none of the new Nullable rewrites or the combinator change apply to this GROUP BY.
⚠️ 28 not_sure 1064 1007 -5.3% <0.0001 aggregation: At -5.3% the two tests disagree and the PR runs were noisy. length() and the <> '' filter on a non-Nullable String already used these rewrites before this PR.
⚠️ 33 not_sure 1153 972 -15.7% <0.0001 aggregation: The -15.7% result (972 ms) is inside master's current range of 962-1202 ms, so the baseline looks slow rather than the PR fast. This GROUP BY on a non-Nullable column does not reach the changed code.
⚠️ 34 not_sure 1148 972 -15.3% <0.0001 aggregation: The -15.3% result (972 ms) is inside master's current range of 966-1177 ms and matches Q33. Nothing in the diff affects a plain GROUP BY on a non-Nullable String.

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

⚠️ 2 inconclusive

Flagged queries (2 of 22)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 1 not_sure 65 53 -18.5% <0.0001 aggregation: The -18.5% delta is 12 ms on a 65 ms query, and the PR median is inside master's current range of 51-65 ms. The lineitem columns are not Nullable, so the extended sum(x + c) rewrite does not apply.
⚠️ 7 not_sure 81 62 -23.5% <0.0001 join_operator: The -23.5% delta is 19 ms, and the PR median of 62 ms is inside master's current range of 59-81 ms. This join-heavy query does not use the changed passes or combinators.

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.

Debug info
  • StressHouse run: 0081e889-c101-4478-8458-fe6099d7c0cf
  • MIRAI run: 1f0abf04-0289-4d91-8877-07c032de6fc4
  • PR check IDs:
    • clickbench_926407_1791752951
    • clickbench_926413_1791752951
    • clickbench_926435_1791752951
    • tpch_adapted_1_official_926450_1791752951
    • tpch_adapted_1_official_926464_1791752951
    • tpch_adapted_1_official_926479_1791752952

@clickhouse-gh

clickhouse-gh Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9551d18d4 with master 975652662 (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

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

Job report

alexey-milovidov and others added 2 commits October 10, 2026 21:05
…_fuse_sum_count`

The reference now expects `sum(a + 1)` over `Nullable` `a` to be rewritten to
`sum(a) + count(a)`, which depends on this setting, and the setting is randomized in CI.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=125011&sha=latest&name_0=PR
#125011

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… with an empty string

`empty` and `notEmpty` of `Nullable(FixedString)` are not read from a subcolumn
and not analyzed by `KeyCondition`, so the rewrite would lose the primary key analysis.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov and others added 2 commits October 11, 2026 19:06
…`, `State`, `Array`, `Merge` combinators

These adapters already forward `parallelizeMergePrepare` and `isAbleToParallelizeMerge`
to the nested function, but returned the default `false` from `isParallelizeMergePrepareNeeded`,
so e.g. `uniqExactIf` over a `Nullable` argument (built as `Null(If(uniqExact))`) still
skipped the prepare step.

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

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failures: 05317_local_read_throttler_filesystem_cache (flaky, 2/131 reruns failed) and 05210_lightweight_update_materialized_cte_in_ignore (return code 60, not reproducible) in Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) and provide a fix in a separate PR. If the fix is already in progress, link it here. (05339_parquet_prewhere_subcolumn_read_through_column was cleared by merging master, where its test was reverted.)

@groeneai

groeneai commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator

Neither is caused by this PR, and both already have a fix from me:

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, thanks. The current head 9551d18d451 includes #124920, and its CI shows no failed tests. 05210_lightweight_update_materialized_cte_in_ignore is tracked by #123121, so this PR needs no change for either failure.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Very good.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Very good.

@alexey-milovidov alexey-milovidov self-assigned this Oct 12, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 12, 2026
Merged via the queue into master with commit 19ddaa1 Oct 12, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the nullable-generic-fixes branch October 12, 2026 17:26
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 12, 2026
rorylshanks added a commit to rorylshanks/ClickHouse that referenced this pull request Nov 4, 2026
`ConvertEmptyStringComparisonToFunctionPass` rewrites `s = ''` to `empty(s)`.
Since ClickHouse#125011 it does so for
`Nullable(String)` too, and since ClickHouse#128092
for `LowCardinality(String)`, so comparisons of `Nullable(String)` typed paths and of
`Dynamic` subcolumns such as `j.x.:String` with an empty string no longer reached
the `equals` analysis of `jsonbf_v1`, and the index was not used for them.

Analyze `empty` of a string like the comparison with an empty string, as
`MergeTreeIndexBloomFilter` does. A missing non-`Nullable` typed path still reads
as its default, which is not indexed, so `j.s = ''` for such a path keeps
skipping the index.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=113022&sha=c543bed2acf878e6046ff4920f52b901e3d77d7f&name_0=PR&name_1=Fast%20test
PR: ClickHouse#113022

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01E5v7QTSR65LdYnQEv9HJVu
rorylshanks added a commit to rorylshanks/ClickHouse that referenced this pull request Nov 4, 2026
… = ''`

`FunctionToSubcolumnsPass` rewrites `empty(s)`, which is what
`ConvertEmptyStringComparisonToFunctionPass` makes of `s = ''`, to `s.size = 0`.
Since ClickHouse#125011 both passes also apply
to `Nullable(String)`, so `j.ns = ''` for a `Nullable(String)` typed path and
``j.x.:`String` = ''`` reach the index as `equals(j.ns.size, 0)` and
``equals(j.x.:`String`.size, 0)``, and `jsonbf_v1` was not used for them. The
previous commit handled only `empty(s)`, which remains with
`optimize_functions_to_subcolumns = 0`.

Analyze the comparison of the size subcolumn of a `JSON` string subcolumn with
zero as the comparison of the subcolumn with an empty string. A name that is
itself a `JSON` path, such as a path whose last key is `size`, is not a size
subcolumn.

Verified locally: `05101`, `05102`, `05103`, `05106`, `05244` and `05497` pass.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=113022&sha=c543bed2acf878e6046ff4920f52b901e3d77d7f&name_0=PR&name_1=Fast%20test
PR: ClickHouse#113022

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01E5v7QTSR65LdYnQEv9HJVu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). pr-performance Pull request with some performance improvements 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