Repository navigation
Parquet dictionary filter: do not sort the dictionary value hashes for a few lookups - #123579
Conversation
…r a few lookups `hashDictionaryValues` sorted the hashes of every dictionary-filtered column chunk so that `DictionaryValueHashes::containsAny` could binary-search them (ClickHouse#115744). A chunk's value set lives for one row group filter evaluation and is usually probed once, by the 1-5 constants of one condition, so the O(n log n) sort dominated the cost of the dictionary filter. Keep the vector unsorted and let `containsAny` answer up to max(8, log2(n)) probes in total by a linear scan; the first call that would exceed that sorts the vector once, and the existing merge search is used from then on. A sort costs about log2(n) scans, so the scans never cost more than the sort they avoid, and a large probe set (an over-cap `IN` set) sorts on its first call exactly as before. Both paths compute membership in the same set, so pruning decisions are unchanged, and nothing new is allocated, so the pruning memory reservation still covers the vector. On 16 row groups of 100000 distinct UInt64 values (release build, 4 CPUs), one equality goes from 16 to 7 ms and from 43.5 to 2.9 ms of query CPU; ClickBench q20 on hits.parquet from 135.5 to 74.5 ms. The new test covers the scan, the switch to sorting and the sorted search, and the performance test gets a fixture with large dictionaries. Proposed by snelheidai in ClickHouse#123487 (analysis and patch). Closes: ClickHouse#123487 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the obvious field comment on DictionaryValueHashes and condense the scan-threshold comment to one line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own cold review (3 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261002-151300 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261002-133800 |
|
Workflow [PR], commit [40e4916] Summary: ✅
AI ReviewSummaryThis PR makes the Final Verdict
LLVM Coverage ReportMeasured on commit 40e4916.
Changed lines: Changed C/C++ lines covered: 21/22 (95.45%) · Uncovered code |
| size_t scanned_probes = 0; | ||
|
|
||
| /// Whether any of `probes` is among the dictionary's values. | ||
| /// Sorting costs about log2(n) scans, so the first max(8, log2(n)) probes scan the unsorted `hashes`. |
There was a problem hiding this comment.
I see, so if we have a small number of hashes to check, we aren't going to first sort the hashes before checking.
There was a problem hiding this comment.
@groeneai out of curiousity, how did you come up with https://github.com/ClickHouse/ClickHouse/pull/123579/changes#diff-73939db92723e0c4bb67cae34f52445d0817870a7ba7fc8f0fb8b03a4e11f437R1892 as a threshold before sorting the hashes?
There was a problem hiding this comment.
I did not tune it: the threshold comes from the patch in #123487, and I kept it after measuring. The reasoning is a comparison count. Sorting n hashes costs about n * log2(n) comparisons and one scan costs n, so for up to about log2(n) probes scanning is no dearer than sorting first; bit_width(n) is that log2, and 8 is a round minimum for small dictionaries, where both costs are negligible. The budget is per column chunk across all its lookups, so it also bounds the worst case: at most max(8, log2(n)) scans before a sort that happens anyway.
In wall time the bound is conservative. A standalone microbenchmark (clang-22 with ClickHouse's libc++, -O2, random 64-bit values, one core of a Xeon 6975P-C):
| n | sort | one scan | sort / scan | budget |
|---|---|---|---|---|
| 1024 | 5.0 us | 0.09 us | 54 | 11 |
| 16384 | 270 us | 1.6 us | 164 | 15 |
| 100000 | 1.81 ms | 10.0 us | 180 | 17 |
| 1000000 | 19.5 ms | 267 us | 73 | 20 |
So at n = 100000 the worst case adds about 17 / 180, i.e. 10% of a sort, consistent with the 20 OR'ed equalities row in the description (20 -> 22 ms). A budget at the real break-even would also scan IN lists of a few dozen constants, but would let that worst case approach 2x. I kept the conservative value; if mid-size IN lists matter more to you, it is a one-constant change.
|
📊 Cloud Performance Report
This PR changes only how the Parquet reader checks column-chunk dictionaries when pruning row groups on equality/IN filters: it now scans small probe sets and sorts only after several lookups. None of the flagged ClickBench or TPC-H queries run that code, and every flag stays not_sure. The ClickBench slowdowns (+5.1% to +15.4%, Q16/18/28/32/33/34) line up with two of the PR's runs sharing one machine, and four of them are still inside master's current range. The TPC-H Q1 (-18.5%) and Q7 (-16.0%) speedups are measured against unstable baselines, and the PR's own parquet_dictionary_filter perf test is the right place to judge its effect. clickbenchFlagged queries (6 of 43)
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_officialFlagged queries (2 of 22)
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
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 23 s compile time in total, 8 of them have a recent master baseline. |
Closes: #123487
Related: #115744
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Row group pruning by dictionaries in the
Parquetreader is faster for an equality or a shortINlist on a column with large dictionaries, such asWHERE UserID = ...over ClickBenchhits.parquet: the hashes of the dictionary values are no longer sorted unless a column chunk is checked against many values.Description
Proposed by @ snelheidai in #123487 (analysis and patch); the numbers below are my own.
hashDictionaryValuessorted the hashes of every dictionary-filtered column chunk (#115744) for binary search. A chunk is usually probed once, by the 1-5 constants of one condition, so the O(n log n) sort cost more than the lookups it served.Change. The vector stays unsorted:
DictionaryValueHashes::containsAnyanswers up tomax(8, log2(n))probes in total by a linear scan, then sorts once and uses the existing merge search. Pruning decisions are unchanged; nothing new is allocated.Measured (release,
clickhouse localon 4 CPUs, median of 15 (q20: 8) alternating processes of 5 runs each, identical results):UInt64,id = constid IN (5 constants)hits.parquetINof 5000 valuesOn 384 cores the first two go from 8 to 6 ms; q20's wall time is flat (row groups are pruned in parallel) but it uses 346 instead of 578 ms of CPU.
Validation. New test
05317_parquet_dictionary_filter_several_conditions_on_one_columnchecks results androws_readwith the dictionary filter on and off for 1, 2x10 and 20 conditions on one column; breaking the scan or the sorted search reddens it, and it passes on the old binary. Existing Parquet dictionary filter tests pass; the perf test gets a large-dictionary fixture with the first two queries.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/123579]
Version info
26.10.1.1455-master(included in26.10and later)