Skip to content

Do not drop unreferenced dictionary values in Parquet PREWHERE when the condition cannot throw - #128342

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/parquet-prewhere-dictionary-skip-compaction
Nov 9, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/parquet-prewhere-dictionary-skip-compaction

Conversation

@groeneai

@groeneai groeneai commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Performance Improvement

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

Faster PREWHERE over a dictionary-encoded String or FixedString column of a Parquet file when the condition cannot throw, for example s != '': the dictionary indexes of each block are no longer scanned and renumbered first. ClickBench Q11 and Q12 over Parquet are about 15% faster.

Description

Closes: #128322

Since #126397, the last PREWHERE step over one dictionary-encoded Parquet column runs once per dictionary value. Before running it, every row subgroup scanned and renumbered all its dictionary indexes (ColumnIndex::removeUnusedRowsInIndexedData), so that the step never sees a value that no read row references. That only matters when a function of the step can throw on such a value (addDays on 'garbage' in 05331). In ClickBench Q12 it took about 6% of the CPU samples.

Changes:

  • Reader::preparePrewhere asks IFunctionBase::canThrow for every function of the last step that is not constant-folded. If none can throw, the dictionary and the decoded indexes are used as they are. Steps with a function that does not describe whether it can throw (and, or, LIKE, IN, ...) keep dropping the unreferenced values.
  • empty and notEmpty implement canThrowIfKnown: for String, FixedString, Array, Map, UUID, IPv4 and IPv6 (also under Nullable and LowCardinality) they read only sizes or bytes. This is needed because optimize_empty_string_comparisons rewrites s != '' to notEmpty(s). It also lets the short-circuit reordering of WHERE atoms evaluate notEmpty before a throwing condition (new case in 05259).

Validation: release build, x86, 4 threads, ClickBench hits.parquet, 20 alternating pairs, 95% CI: Q11 284 -> 233 ms (0.84x [0.80, 0.87]), Q12 317 -> 274 ms (0.85x [0.83, 0.87]); an A/A run gives 0.995x [0.975, 1.015]. No ClickBench query is significantly slower, and all 43 return the same results with max_threads = 1. 05331 gets a case where every row subgroup leaves dictionary values unreferenced, and parquet_prewhere_dictionary_encoded.xml gets a sparse dictionary column queried with PREWHERE m != '' (0.88x locally).

The analysis comes from #128322 by @ snelheidai. Its patch (marking the used values while decoding) is not used: skipping the scan is simpler and removes all of the per-row work.


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

Version info

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

groeneai and others added 3 commits October 31, 2026 02:48
…REWHERE step can throw

The last PREWHERE step over a dictionary-encoded column is evaluated once per dictionary
value through a ColumnReplicated. Each row subgroup first dropped the unreferenced values
(buildUsedRowsFilter + compactIndexes over every index), so that the step never evaluates a
value no read row references. That is only needed when a function of the step can throw on
such a value. IFunctionBase::canThrow answers this conservatively, so when every function
node that is not constant-folded answers false, the dictionary values are kept as they are.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`empty` and `notEmpty` read only the sizes or the bytes of String, FixedString, Array, Map,
UUID, IPv4 and IPv6 values (Nullable and LowCardinality are unwrapped by the default
implementations), so they cannot throw on any value of these types. They did not describe it,
so IFunctionBase::canThrow answered true. The default optimize_empty_string_comparisons
rewrites `s != ''` to `notEmpty(s)`, so the rewrite dropped the property that notEquals has.

Every consumer of IFunctionBase::canThrow now treats them as harmless on extra rows: the
reordering of `and`/`or` arguments and of AND-chain atoms in WHERE, and the Parquet reader,
which can now skip the dictionary compaction for `PREWHERE s != ''`.

05259 gets a case where `notEmpty` and `s != ''` are moved in front of a throwing atom.

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; 3 findings) plus my own review (1 nit).

# Sev Finding Verdict Evidence / action
1 ⚠️ The s != '' case in 05259 does not pin optimize_empty_string_comparisons DISAGREE With the rewrite off, notEquals(String, String) cannot throw either, so the query still returns 50. The rewrite-independent notEmpty(s) query is the oracle (fails on master with ILLEGAL_DIVISION, 10/10 runs).
2 ⚠️ 05331 case D also passes with the compaction restored DISAGREE D checks the results when every row subgroup leaves dictionary values unreferenced. That the scan is skipped is shown by the perf test (PREWHERE m != '', 0.88x locally) and by gdb (Q11/Q12: 16/16 subgroups compact on master, 0/16 here). S5 keeps the throwing branch tested.
3 ⚠️ empty/notEmpty no-throw is tested only for String DISAGREE Dropping a type from canThrowIfKnown only restores the conservative answer: an optimization is lost, but results and exceptions never change. Each executeImpl branch reads only sizes or bytes.
4 💡 last_step_can_throw defaults to the skipping path DISAGREE Only one place enables the replicated decode, and the flag is computed right after it.

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

Session id: cron:clickhouse-review-slot-10:20261031-042600

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @grantholly-clickhouse @george-larionov, could you review this and add the can be tested label so CI runs? The Parquet reader no longer drops unreferenced dictionary values before the last PREWHERE step when IFunctionBase::canThrow is false for every function of that step, and empty/notEmpty now implement canThrowIfKnown so that s != '' qualifies (ClickBench Q11/Q12 about 15% faster locally).

@clickhouse-gh

clickhouse-gh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5b51d7e]

Summary: ✅


AI Review

Summary

The PR skips dictionary compaction for a final Parquet PREWHERE step only when every executed function is known not to throw, and supplies that information for empty and notEmpty. No actionable findings in the current diff. The reported paired benchmarks support the performance claim.

Missing context / blind spots
  • ⚠️ 05259_short_circuit_reorder_arguments passed in Fast test, but 05331_parquet_prewhere_dictionary_values_once was skipped there. Parquet-enabled stateless and performance CI jobs are pending. No local build was available for independent execution; completed CI runs would close this validation gap.
Final Verdict

✅ Approve — no code changes requested; pending CI validation remains as noted above.

@clickhouse-gh

clickhouse-gh Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 5b51d7ebc with master 6983f5a16 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of c4844a87e; 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

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

Job report

opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-ClickHouse__ClickHouse that referenced this pull request Nov 6, 2026
…s cannot throw

They read only the sizes or the bytes of the values. `URL <> ''` is
rewritten to `notEmpty(URL)` (`optimize_empty_string_comparisons`), so
without this a PREWHERE step before it keeps filtering its block.

Same change as in ClickHouse#128342, which needs it for Parquet; it drops out when
either PR is merged first.

Found, analysed and measured by @ snelheidai in ClickHouse#128442 and ClickHouse#128454.

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

@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.

Good.

@alexey-milovidov alexey-milovidov self-assigned this Nov 8, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 8, 2026
Merged via the queue into ClickHouse:master with commit 2fdb487 Nov 9, 2026
530 of 532 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 9, 2026
alexey-milovidov added a commit that referenced this pull request Nov 9, 2026
…uet reader

After merging master, `Reader::Step` no longer has the `actions` member (this PR
replaced it with `actions_pool`), but master added a new use of it in
`Reader::prepareDictionaryDecoding` (from #128342). Read the DAG from the
prototype of the pool, as the neighbouring check already does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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-parquet-reader-v3 Issues related to the native Parquet reader. pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

3 participants