Skip to content

Make Parquet dictionary-filter pruning cheaper: sorted hash vector, no per-value Field - #115744

Merged
alexey-milovidov merged 4 commits into
masterfrom
parquet-dictionary-filter-sorted-vector
Aug 29, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
parquet-dictionary-filter-sorted-vector

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 21, 2026 •

Copy link
Copy Markdown
Member

The exact dictionary filter of the native Parquet reader (input_format_parquet_dictionary_filter_push_down) evaluated each column chunk by hashing every dictionary value through the generic per-value Field path and inserting all the hashes into a per-chunk HashSet, probed by a handful of query constants. This did two kinds of avoidable work:

  • parquetTryHashColumn constructed a Field and re-dispatched on its type for every integer dictionary entry. It now hashes the data array directly, with the type check hoisted out of the loop; the digest is unchanged (widening to Int64/UInt64 and narrowing to the physical type keeps the same low bits as a direct cast), and columns without a fast path (e.g. IPv4) fall through to the Field path unchanged.
  • hashDictionaryValues now keeps the hash vector itself, sorts it in place, and binary-searches the query constants, instead of building a HashSet whose power-of-two buffer (maximum fill factor 0.5) held up to ~4 cells per value on top of that same vector. No extra allocation, a 3-5x smaller persistent footprint charged against input_format_parquet_memory_high_watermark, and an exact reservation instead of a modeled one (the HashSet growth rule was the trickiest term of the pruning-memory accounting). The extra default-value hash under input_format_null_as_default is kept outside the vector so the vector stays exactly the allocation the reservation covers.

Measured on a dictionary-heavy microbenchmark (64 row groups, 4M distinct UInt64 values, absent needle, so the filter prunes everything): ~18% fewer instructions for the query. The pruning also survives a ~35% tighter memory watermark: the behavior flip on the file of 04615_parquet_v3_dictionary_filter_hash_set_budget moves from between 6.5 and 7.0 MB down to between 4.0 and 4.5 MB. This matters on small machines, where the watermark is derived from the memory limit (about 200 MiB on a machine with 2 GiB of RAM, e.g. t3a.small in ClickBench): the smaller and exact footprint lets more chunks be pruned instead of falling back to a full scan, and avoids multi-megabyte transient allocations per column chunk during pruning.

04615_parquet_v3_dictionary_filter_hash_set_budget is retuned to pin the new footprint model from both sides: a watermark that affords the hash vectors must keep pruning, one just below must fall back to a full scan.

The probe side is an intersection, not a series of independent searches: KeyCondition::prepareBloomFilterData sorts and deduplicates each atom's probe hashes once, and the dictionary lookup walks its sorted vector along with them, so every probe continues where the previous one stopped. That matters because prepareBloomFilterCondition deliberately keeps hashing IN sets over the bloom filter's set-size cap so the exact dictionary filter can still prune them, so one column chunk can be probed with thousands of hashes. Measured per findAnyHash call with an absent needle (every probe searched), the merge is 1.5-5x cheaper than a binary search per probe, and once the probe set approaches the dictionary size it also beats the HashSet this change replaces (13.4 ms vs 27.2 ms for 1M probes against 1M values), whose per-value insert cost and 4x memory it does not pay either.

New test 05024_parquet_dictionary_filter_large_in_set covers that path: over-cap IN sets that miss every row group, hit exactly one, hit all of them, and match only the last dictionary value, each compared against the same query with the dictionary filter disabled.

Changelog category (leave one):

  • Performance Improvement

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

Parquet dictionary-based row group pruning (setting input_format_parquet_dictionary_filter_push_down) got cheaper: integer dictionaries are hashed without per-value Field conversions, and the value set is kept as a sorted vector instead of a hash table, several times reducing the memory footprint of the pruning stage.


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

…o per-value Field

Two changes to the exact dictionary filter of the native Parquet reader
(`input_format_parquet_dictionary_filter_push_down`):

1. `parquetTryHashColumn` hashed integer columns through the generic
   per-value `Field` path: construct a `Field`, re-dispatch on its type,
   re-check the logical type - for every dictionary entry of every row
   group. Hash the data array directly instead, with the type check
   hoisted out of the loop (the digest is unchanged: widening to
   `Int64`/`UInt64` and narrowing to the physical type keeps the same
   low bits as a direct cast). Columns without a fast path (e.g. `IPv4`)
   fall through to the `Field` path unchanged.

2. `hashDictionaryValues` built a `HashSet` of all dictionary-value
   hashes per column chunk, probed by a handful of query constants. The
   set's power-of-two buffer (maximum fill factor 0.5) held up to ~4
   cells per value on top of the hash vector it was built from, and its
   growth rule was the trickiest term of the pruning-memory reservation.
   Keep the vector itself, sort it in place, and binary-search the query
   constants: no extra allocation, a 3-5x smaller persistent footprint
   charged against `input_format_parquet_memory_high_watermark`, and an
   exact reservation instead of a modeled one. The extra default-value
   hash under `input_format_null_as_default` is kept outside the vector
   so the vector stays exactly the allocation the reservation covers.

On a 4M-value dictionary microbenchmark (64 row groups, all values
distinct, absent needle) this removes ~18% of the query's instructions;
the pruning now also survives a ~35% tighter memory watermark (the
behavior flip on the file of `04615_parquet_v3_dictionary_filter_hash_set_budget`
moves from between 6.5 and 7.0 MB down to between 4.0 and 4.5 MB), which
matters on small machines where the watermark is derived from the memory
limit (~200 MiB on a 2 GiB machine). The smaller footprint also avoids
multi-megabyte transient allocations per column chunk during pruning,
which showed up as kernel page-fault time under a tight cgroup.

`04615_parquet_v3_dictionary_filter_hash_set_budget` is retuned to pin
the new footprint model from both sides: a watermark that affords the
hash vectors must keep pruning, one just below must fall back to a full
scan.
@clickhouse-gh

clickhouse-gh Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [65aafa0]

Summary: ✅


AI Review

Summary

This PR replaces the Parquet dictionary-filter value set with a sorted hash vector, adds a direct integer-column hashing path, and retunes the related memory-budget and large-IN tests. I rechecked the current code against the prior review threads and do not see any remaining correctness, safety, or evidence gaps: the large-IN lookup path now has focused coverage, the input_format_null_as_default accounting invariant is guarded, and the PR metadata is consistent with the change.

Final Verdict

✅ No remaining review findings. The previously raised issues are fixed in the current revision.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.00% 87.00% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 68/72 (94.44%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Aug 21, 2026
robot-clickhouse and others added 2 commits August 21, 2026 09:50
The watermark of the string-rounding budget test sat in the floored-vs-exact
gap of the old footprint (which included the hash-table term this branch
removed). Re-pin it inside the same gap of the new, smaller reservation,
found by bisecting the behavior flip (~6.6 MB) on the patched build.

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 9d942237-a5b0-4700-9158-7e9c5b4e1cff
  • MIRAI run: 8f964257-3216-46f5-b407-beceb36631f1
  • PR check IDs:
    • clickbench_170161_1787558146
    • clickbench_170172_1787558146
    • clickbench_170179_1787558146
    • tpch_adapted_1_official_170191_1787558146
    • tpch_adapted_1_official_170217_1787558146
    • tpch_adapted_1_official_170231_1787558146

@clickhouse-gh

clickhouse-gh Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 65aafa09f with master 4faa0468e (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
Binary Master PR Δ
programs/clickhouse-stripped 706.02 MiB 703.00 MiB -3.03 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

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

Job report

…in one pass

The exact dictionary filter of the native Parquet reader is deliberately
exempt from the bloom filter's set-size cap (`bloom_filter_max_set_size`,
100): `prepareBloomFilterCondition` keeps hashing over-cap `IN` sets so
that the exact filter can still prune them. `findAnyHash` can therefore
be called with thousands of probe hashes for one column chunk, and after
the switch from a `HashSet` to a sorted vector each of them was a
full-height binary search over the whole vector.

Intersect the two sequences instead: `KeyCondition::prepareBloomFilterData`
now sorts and deduplicates the probe hashes of every atom once (which
also stops a duplicate constant from being probed twice on every granule
or row group), and the dictionary lookup walks its sorted vector along
with them, so each probe continues where the previous one stopped. A
handful of constants still costs a binary search each; a large probe set
degrades to a linear merge instead of `probes.size()` full searches. An
out-of-order probe merely restarts the window, so the result does not
depend on the probes being sorted.

Measured with an absent needle (the worst case: every probe is searched)
against a sorted vector of 8-byte hashes, per `findAnyHash` call:

    values   probes   binary search   merge     `HashSet` probe
    16384    1000         0.010 ms    0.007 ms      0.001 ms
    16384    100000       1.395 ms    0.723 ms      0.424 ms
    131072   100000       2.838 ms    1.290 ms      0.632 ms
    131072   1000000     20.338 ms   15.378 ms      5.769 ms
    1000000  100000       4.079 ms    2.423 ms      2.391 ms
    1000000  1000000     66.299 ms   13.376 ms     27.249 ms

So the large-`IN` path is 1.5-5x cheaper than searching for each probe,
and beyond ~1M probes it also beats the `HashSet` this change replaced,
whose build cost (an insert per dictionary value, ~10 ns each) it does
not pay either. For a handful of constants nothing changes.

Also pin the "the default value hash stays out of the vector" invariant
with a `chassert` in `hashDictionaryValues`: a watermark-based test
cannot detect an append there, because for a `FixedSize` dictionary the
reservation also covers the materialization terms, which leave enough
headroom for a doubled vector.

`04615_parquet_v3_dictionary_filter_hash_set_budget` gets the same
both-sides watermark check for the nullable file as for the non-null one,
and the new `05024_parquet_dictionary_filter_large_in_set` checks the
pruning decisions and the results of the intersection for over-cap `IN`
sets that miss every row group, hit one row group, or hit all of them.

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

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Aug 29, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 29, 2026
Merged via the queue into master with commit fbb2857 Aug 29, 2026
178 checks passed
@alexey-milovidov
alexey-milovidov deleted the parquet-dictionary-filter-sorted-vector branch August 29, 2026 15:14
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 29, 2026
alexey-milovidov pushed a commit to rorylshanks/ClickHouse that referenced this pull request Oct 3, 2026
…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]>
zvonand added a commit to Altinity/ClickHouse that referenced this pull request Nov 4, 2026
Antalya 26.8: Backport of ClickHouse#115744 - Replace parquet dictionary filter value with sorted hash vector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

2 participants