Repository navigation
Revert "Revert "Parquet: dictionary-based row group filtering"" - #110567
alexey-milovidov merged 41 commits into
Conversation
|
Workflow [PR], commit [21c7870] Summary: ✅
AI ReviewSummaryThis PR re-enables Parquet v3 dictionary-based row-group filtering, adds the new Documented surface
Findings
Final VerdictRequest changes. |
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
…t-106952-parquet-dictionary-filter-push-down
… in the Parquet dictionary filter `columnChunkCanUseDictionaryFilter` used `checkThriftEnum`, which throws `INCORRECT_DATA`, to validate `page_type`/`encoding` in each `PageEncodingStats` entry. But `encoding_stats` is only advisory metadata used to decide whether the dictionary-filter optimization is safe to apply - the page stream itself may still be perfectly readable via a full scan even if this metadata is garbage or carries a future/unrecognized enum value. Throwing turned a readable file into a hard failure under the now-default-on dictionary filter. Add a non-throwing `isValidThriftEnum` and use it here instead, so an unrecognized `page_type`/`encoding` just makes the chunk ineligible for the optimization - the same as a missing `encoding_stats` - matching every other soundness fallback in this function. Add a regression test with pyarrow-crafted files carrying out-of-range `page_type`/ `encoding` values in `encoding_stats`, verifying the read still succeeds (falling back to a full scan) instead of raising `INCORRECT_DATA`.
|
Both AST fuzzer failures on head 51e6ec5 are pre-existing
Neither fix has landed on |
A negative `PageEncodingStats.count` is corrupted metadata, but `encoding_stats` is only advisory input to `columnChunkCanUseDictionaryFilter`, so it must not turn an otherwise readable file into an `INCORRECT_DATA` exception under the default-on dictionary filter: treat it like the unrecognized enum values and make the chunk ineligible for the optimization instead, falling back to a full scan. Add a regression test that crafts Parquet files with a negative `count` on both the dictionary-page and the data-page `encoding_stats` entries and verifies they still read correctly via a full scan.
…t-106952-parquet-dictionary-filter-push-down
…t-106952-parquet-dictionary-filter-push-down
Address the AI review finding on `Reader.cpp`: the dictionary-filter path `hashDictionaryValues` bypassed the reader's memory budget. The eligibility check `columnChunkCanUseDictionaryFilter` gates only on the *compressed* on-disk dictionary page span (`dictionary_filter_limit_bytes`, 1 MiB by default), which does not bound the decoded value set that pruning then builds (a materialized column of all values, a vector of hashes, and a `HashSet` of them). A highly compressible dictionary could stay under the compressed limit yet decode to tens of MiB, with several row groups doing so in parallel during pruning, none of it charged to `input_format_parquet_memory_high_watermark`. Two changes in `hashDictionaryValues`: - Cap on the decoded value set, not just the compressed page: estimate the memory the value-set construction needs and skip the optimization (falling back to a full scan, reported as "can't rule out a match", the same as an unhashable type) when it would exceed the reader's per-file memory high watermark. The watermark scales down automatically when the query has little memory to spare, so pruning is given up only when it would genuinely threaten the budget. - Avoid the duplicate materialization: when the dictionary is already decoded into an `IColumn` (`Dictionary::Mode::Column`), hash that column directly instead of materializing an identical second copy via `Dictionary::index`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…oding Address the AI-review blocker on the memory-budget guard added earlier: the `memory_high_watermark` check in `hashDictionaryValues` runs only after the dictionary page has already been decompressed and decoded during the `BloomFilterBlocksOrDictionary` stage. `columnChunkCanUseDictionaryFilter` bounds only the *compressed* on-disk dictionary page (`dictionary_filter_limit_bytes`, 1 MiB by default), so a highly compressible dictionary can stay under that limit yet decode to tens of MiB. Because the pruning stage is not memory-throttled and runs for many row groups in parallel, the default-on optimization could overshoot `input_format_parquet_memory_high_watermark` before pruning even built its value set. Move the budget check ahead of the dictionary decode: `decodeDictionaryPage` now takes a `max_decoded_bytes` cap and, on the pruning path, rejects a dictionary whose decoded (uncompressed) page size would exceed the watermark before it is decompressed. The column then falls back to a full scan (a missed optimization, never a wrong result), and the dictionary is decoded later, throttled, on the data-read path if the column is actually read. The complementary decoded-value-set cap in `hashDictionaryValues` is unchanged. Add `04611_parquet_v3_dictionary_filter_memory_budget` covering the low-memory fallback: a file with a large decoded dictionary is pruned under a generous budget and read in full (with identical, correct results) under a tight one.
…remaining allowance The dictionary-filter pruning path (`BloomFilterBlocksOrDictionary` stage) decoded the dictionary and built a value set without charging either to the reader's per-stage memory accounting, and the earlier caps compared each allocation against the full `input_format_parquet_memory_high_watermark` rather than the memory still available. Several row groups pruning in parallel could therefore each allocate just under the watermark and collectively overshoot it. Charge the decoded dictionary to the `BloomFilterBlocksOrDictionary` stage via a `MemoryUsageToken` so it participates in the same per-reader budgeting `scheduleTasksIfNeeded` enforces, and cap both the decoded dictionary page (in `decodeDictionaryPage`) and the hashed value set (in `hashDictionaryValues`) against the remaining pruning budget (`ReadManager::pruningMemoryBudget`) instead of the full watermark. As each live dictionary shrinks the budget the scheduler throttles how many row groups prune concurrently, bounding the total pruning memory. The optimization stays fail-open: when the budget is exhausted pruning is skipped and the row group is scanned in full. Addresses the review feedback on #110567. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Address review on `Reader.cpp` (#110567): the dictionary-filter pruning path could still exceed `input_format_parquet_memory_high_watermark` in two ways. 1. `decodeDictionaryPage` gated only on the raw page payload (`max(uncompressed_page_size, compressed_page_size)`) before decoding, but `Dictionary::decode` allocates per-entry state on top of that - a `StringPlain` offsets array, or a fully decoded `col` for types that need conversion - so the true footprint (`Dictionary::allocatedBytes`) can be several times the page payload and a single decoded dictionary could overshoot the remaining budget before it was charged. The payload check is now a coarse pre-decode gate, and the exact decoded footprint is re-checked against the budget after decoding and before the dictionary is charged (in `ReadManager::runTask`) or used; if it does not fit, the dictionary is dropped and the caller falls back to a full scan. 2. `applyBloomAndDictionaryFilters` gave every `DictionaryLookup` the full budget, and each lookup's value set stays alive until the whole row-group filter evaluation finishes, so a predicate over several dictionary-filtered columns could build one full-budget value set per column and collectively overshoot. The budget is now a single counter shared by all lookups in the evaluation, decremented by each value set's persistent `HashSet` footprint as it is built, so their combined footprint stays within the watermark.
…umns `04612_parquet_v3_dictionary_filter_multi_column_budget` reads a file with two dictionary-encoded columns and a predicate over both, checking that pruning is correct and effective under a generous `input_format_parquet_memory_high_watermark` and stays correct (falling back to a full scan) as the budget shared across the two columns' value sets tightens.
The dictionary-filter pruning path capped the decoded dictionary only *after* `Dictionary::decode` had already materialized it: `decodeDictionaryPage` decoded the page and only then compared `Dictionary::allocatedBytes` against the budget, so a dictionary whose page bytes fit the budget but whose per-entry state (a `StringPlain` offsets array, or a decoded `col`) did not could still overshoot `input_format_parquet_memory_high_watermark` transiently before falling back to a full scan. Likewise `hashDictionaryValues` estimated the value set from `Dictionary::getAverageValueSize` alone, which understates the materialized `ColumnString` (its `UInt64` offsets and its geometrically grown `chars` buffer), so the estimate could pass while the actual materialization exceeded the budget. Predict the full decoded footprint from the page header before decoding (`Dictionary::decodedFootprintUpperBound`, mirroring the mode selection in `Dictionary::decode`) and reject an oversized dictionary before `decode` allocates anything, so the pruning path never overshoots the budget even momentarily. Make the value-set estimate a proper upper bound that also counts the materialized column's `UInt64` offsets and char-buffer growth, and skip the materialization terms for the `Mode::Column` path that hashes the already-decoded (already-charged) column in place. The post-decode `allocatedBytes` check is kept only as a defensive backstop against the prediction drifting out of sync with `decode`. Addresses the AI review blockers on `Reader.cpp` (the pre-decode footprint and the string value-set estimate) for #110567. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`04613_parquet_v3_dictionary_filter_short_string_budget` builds a dictionary of many short strings, where the per-entry `UInt32` offsets and the materialized `ColumnString` footprint dominate the raw page bytes - the case where accounting only the page bytes understates the decoded footprint. It checks that pruning still works under a generous budget and that results stay correct as the budget tightens to a full-scan fallback. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…pruning budget twice On the dictionary-filter pruning path `Reader::decodeDictionaryPage` bounds the decoded dictionary with `Dictionary::decodedFootprintUpperBound` and reserves it against `input_format_parquet_memory_high_watermark` before decoding. It passed `max(uncompressed_page_size, compressed_page_size)` as the page payload size, but the bytes the decoded dictionary owns are the decompressed ones: `decodeDictionaryPageImpl` sizes `decompressed_buf` at exactly `uncompressed_page_size` for a compressed chunk, and points the dictionary straight at the first `compressed_page_size` prefetched bytes for an `UNCOMPRESSED` one. The compressed frame lives in `dictionary_page_prefetch` and is already charged to the pruning stage there, so taking the larger of the two double-counted it whenever a codec expanded the page - which is legal, and our own writer does not fall back to `UNCOMPRESSED` when compression grows the payload. The consequence was a missed optimization: a row group whose decoded dictionary comfortably fits the budget fell back to a full scan. This is the same class of double-counting that `04616` fixed for the `UNCOMPRESSED` carrier, on the `compressed_page_size > uncompressed_page_size` side. Pass the size of the payload `Dictionary::decode` will actually see instead, and rename the parameter to `page_payload_size` to make the contract explicit. New test `04651_parquet_v3_dictionary_filter_expanding_codec_budget`: a real codec only expands an incompressible page by a fraction of a percent, too little to pin a watermark between the two bounds, so the test crafts the file - the `ZSTD` dictionary page is padded with bytes that belong to the page (`compressed_page_size` covers them and the reader does prefetch them) but that the frame does not need, which pushes `compressed_page_size` far past `uncompressed_page_size` while the file still reads back identically. With the watermark pinned between the two bounds the fixed reader prunes the row group; the old bound read all 120000 rows.
|
25th round. Addressed the AI Review "Request changes" verdict (
Regression test Also merged Verification on the merge result: full 0 unresolved review threads. Waiting on fresh CI and a human merge. |
|
26th round — merged The merge itself was clean, with no conflict markers; the only overlapping file is Review state on the previous head CI on Verification on the merge result: all 8 translation units this pull request touches compile with 0 errors and 0 warnings, and the full 0 unresolved review threads. Waiting on fresh CI and a human merge. |
…t-106952-parquet-dictionary-filter-push-down
|
Merged
The merge was clean (no conflicts, no manual resolution); the 60-file PR diff is intact and the Verification on the merge result: every translation unit compiles with 0 errors / 0 warnings and AI Review is |
| size_t page_bytes = column.meta->meta_data.codec == parq::CompressionCodec::UNCOMPRESSED | ||
| ? size_t(header.compressed_page_size) | ||
| : size_t(header.uncompressed_page_size); | ||
| reserved_bytes = Dictionary::decodedFootprintUpperBound( | ||
| column.meta->meta_data.codec, header.dictionary_page_header.encoding, column_info.decoder, |
There was a problem hiding this comment.
This new pre-decode sizing path loads column.meta->meta_data.codec before checkThriftEnum(column.meta->meta_data.codec, ...) runs in decodeDictionaryPageImpl. Thrift can deserialize an out-of-range compression codec here just like it can for PageHeader, so on a malformed file that reaches dictionary pruning the comparison on this line and the decodedFootprintUpperBound(...) call below hit enum undefined behavior under -fsanitize=enum instead of failing cleanly with INCORRECT_DATA.
Please validate the codec before branching on it here, or thread the raw underlying integer through the sizing logic until it is checked.
There was a problem hiding this comment.
@clickgapai, after this PR is merged, address the comment above.
There was a problem hiding this comment.
@alexey-milovidov I looked into #110567 but didn't land on a concrete change to make — the requested work looks like it may already be in the PR, or I couldn't pin down exactly what's still needed. If you have a specific case in mind (a query, a file, or the exact scenario to cover), point me to it and I'll follow up.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 345/368 (93.75%) · Uncovered code |
…rquet `input_format_parquet_dictionary_filter_push_down` (ClickHouse#106952, re-landed in ClickHouse#110567) lets the `Parquet` reader skip a whole row group for an equality or `IN` condition when the column chunk is fully dictionary-encoded and its dictionary page holds none of the values that are searched for. Nothing in `tests/performance` covered it. The fixture puts the reader in the situation the feature was written for, the one a real `hits.parquet` is in: a low-cardinality string column whose values are spread over the whole file, so min/max statistics prune nothing (every row group's range spans every value searched for), and no bloom filters, which is what most Parquet writers produce. The dictionary page is then the only per-row-group structure that can rule a value out. Measured locally on the fixture the test builds, at `max_threads = 1`, with `input_format_parquet_dictionary_filter_push_down` off and on: query off on rows read count(), value in 30 of 300 row groups 0.401s 0.040s 300M -> 30M count(), value in no row group 0.393s 0.004s 300M -> 0 count(), IN over both of them 1.107s 0.118s 300M -> 30M sum() of another column, same condition 0.060s 0.013s count(), value in every row group 0.421s 0.426s 300M -> 300M The last query is the counterweight: nothing can be skipped there, so the dictionary pages are read and hashed for nothing and the query must not become slower. Co-Authored-By: Claude Opus 5 <[email protected]>
When a column chunk carries both a bloom filter and a dictionary page, the reader read and decoded the dictionary page first and kept the bloom filter only as a runtime fallback. The dictionary page is by far the larger of the two - up to `input_format_parquet_dictionary_filter_push_down` bytes against one 32-byte bloom filter block per queried value - so for every row group the bloom filter alone could already rule out, the dictionary was read for nothing. Reading `hits.parquet` written by ClickHouse (which writes bloom filters by default) shows it: `SELECT UserID FROM file(...) WHERE UserID = <constant>` over a file of 16 row groups reads 4,608 bytes with `input_format_parquet_dictionary_filter_push_down = 0` and 8,017,209 bytes at the default - the 16 `UserID` dictionary pages - to arrive at the same answer. That is what made query no. 5 of the `clickbench_parquet_short` performance test slower in both ClickHouse#106952 and ClickHouse#110567, on `arm` and on `amd`: `DiskReadElapsedMicroseconds` +34..75%, `memory_usage` +17..22%, `OSCPUVirtualTimeMicroseconds` +9..10%. The bloom filter is now evaluated in its own stage, before the dictionary pages are fetched. It has no false negatives, so a row group it rules out is ruled out for good and no dictionary page is read. A row group it does not rule out goes on to the dictionary stage exactly as before, and the dictionary costs nothing extra there: either the row group is read anyway - which needs the dictionary page to decode it - or the dictionary rules it out exactly, which is the bloom filter's false positive. Pruning power is therefore unchanged, and files without bloom filters, which is what the feature was written for, take the same path as before. After the change the same query reads 4,608 bytes at the default setting, the same as with the filter turned off. `BloomFilterHeader` and `BloomFilterBlocks` share one stage's worth of the per-stage memory budget, so `Dictionary`, the index stages and `ColumnData` keep exactly the share they had when the bloom filter and the dictionary were one stage - `input_format_parquet_memory_high_watermark` means the same thing for the dictionary-pruning path as before. Co-Authored-By: Claude Opus 5 <[email protected]>
Reverts #110354