Repository navigation
Speed up reading Parquet columns after a PREWHERE step - #124186
grantholly-clickhouse merged 3 commits into
Conversation
After a PREWHERE step the native Parquet reader decodes the remaining columns with the step's row filter. Two things made that slow: 1. `PlainFixedSizeDecoder::decode` converted a PLAIN page with one `convertColumn` call without a filter, but with a filter it called `convertColumn` once per passing value (a virtual call each). Writers fall back from dictionary to PLAIN pages for high-cardinality columns, so e.g. `WatchID` in ClickBench takes this path. It now counts the passing values, converts the whole page at once when all of them pass, and otherwise gathers the passing values with 64-row masks (one memcpy per all-passing block) directly into the column, or into a buffer that is converted with one call when a conversion is needed. This is what the Boolean, DeltaBinaryPacked, ByteStreamSplit and DeltaByteArray decoders already do. 2. `Reader::applyPrewhere` filtered every column read so far and installed the step's filter even when every row passed. It now returns early in that case, so the columns are not copied and later columns are read through the unfiltered paths. Measured on x86 (Xeon 6975P-C, release build, 100M-row ClickBench parquet files, max_threads = max_parsing_threads = 4, 11 alternating pairs, servers pinned to one NUMA node), patched/base: all rows pass 0.69, about half pass 0.76, `IsRefresh = 0` 0.77, `AdvEngineID <> 0` (2% pass) 0.95, ClickBench q32 0.96, `sum(length(Title))` 0.98; no query slower. The early return alone is about 0.95 on the all-pass query. Analysis and patch by snelheidai in ClickHouse#124145. Closes ClickHouse#124145 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop two comments that narrate the new code and merge the copyPassingValues doc into 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 review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261006-090700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261006-065700 |
|
Workflow [PR], commit [813572f] Summary: ✅
AI ReviewSummaryThis PR speeds up Parquet reads after Final Verdict
LLVM Coverage ReportMeasured on commit 813572f.
Changed lines: Changed C/C++ lines covered: 52/52 (100.00%) · Uncovered code |
The flaky check under MSan failed it 6 of 7 times for running longer than 180 s (213-244 s; TSan 50/50 OK but up to 174 s), while it takes about 1 s in the debug lanes. In the CI query_log almost all of that time is system time spent on page faults: the INSERT took 60 s of wall time for 5 s of user time with 10.4M soft page faults, and each Parquet read about 20 s for 1.9 s of user time. The cost scales with the number of small Parquet pages, so it scales with the row count. Reduce the fixture from 30011 to 5003 rows and adapt the constants of the filters that depend on it. The file keeps the same shape: one row group, all 26 columns PLAIN without a dictionary page or offset index, pages smaller than the 1000-row blocks. All seven mutations of the new decoder and early-return code still fail the test. Local flaky-check simulation (ASan, 12 pinned cores, 11 jobs, 22 runs): 30.5 s average / 61.1 s max before, 6.0 s / 11.2 s after. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: 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
|
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, 29 s compile time in total, 8 of them have a recent master baseline. |
| filter += filter_offset; | ||
| pass_count = countBytesInFilter(filter, 0, num_values); | ||
| if (pass_count == 0) | ||
| return; |
There was a problem hiding this comment.
It looks like we have an opportunity to early return and skip work here
| return; | ||
| } | ||
| const size_t value_size = converter->input_size; | ||
| if (pass_count == num_values) |
There was a problem hiding this comment.
Here there's no filter and every value passes
|
|
||
| const bool direct = converter->isTrivial(); | ||
| char * to = nullptr; | ||
| if (direct) |
There was a problem hiding this comment.
We now wait until here where we have no early returned, and we are not in the case where every value passes the filter.
It looks like we also don't convert the column unless it is a non-trivial conversion
| } | ||
|
|
||
| if (rows_pass == filter.size()) | ||
| return; |
There was a problem hiding this comment.
Ah, so if all rows pass we can return early 👍 This compliments the earlier existing check if no rows passed
|
|
||
| /// Appends the passing values to `to` and returns the new end; `static_value_size` 0 means `value_size`. | ||
| template <size_t static_value_size> | ||
| static char * copyPassingValues(const char * from, const UInt8 * filter, size_t num_values, size_t value_size, char * to) |
There was a problem hiding this comment.
Looks like we walk the bit mask and copy the matching values where the bit is set and copy the values into the destination buffer.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Reading Parquet columns after a
PREWHEREstep is faster, about 1.4x on ClickBench data when most rows pass: values of PLAIN-encoded fixed-size columns are copied in bulk instead of one virtual call per value, and a step that every row passes no longer filters the columns read so far. Closes #124145.Description
Analysis and patch by @ snelheidai in #124145, re-measured on x86.
Root cause. After a
PREWHEREstep the remaining columns are decoded with the step's row filter.PlainFixedSizeDecoder::decodethen calledconvertColumnonce per passing value (a virtual call each), while the Boolean, DeltaBinaryPacked, ByteStreamSplit and DeltaByteArray decoders already convert once. High-cardinality numeric columns fall back to PLAIN pages (ClickBenchWatchID). Also,applyPrewherefiltered every column read so far and installed the filter even when every row passed.Change. The decoder counts the passing values: all pass, one
convertColumn; otherwise it gathers them with 64-row masks (onememcpyper all-passing block) directly into the column, or into a buffer converted once.applyPrewherereturns early when every row passes.Performance (Xeon 6975P-C, Release, 100M-row ClickBench files,
max_threads = max_parsing_threads = 4, 11 pairs, 95% CI):sum(UserID), sum(WatchID), all rows passAdvEngineID <> 0(2% pass)sum(toUInt32(EventTime)), sum(WatchID), all passsum(length(URL)), sum(length(Title))The early return alone gives about 0.95 on the all-pass query. Results are identical; peak memory within 7%.
Tests.
05331_parquet_prewhere_plain_fixed_sizecomparesPREWHEREreads of PLAIN pages with the source table for 1- to 32-byte values, with and without conversion, many filter shapes and two-stepPREWHERE; each of seven mutations of the new code fails it. A performance test is added. ASan clean.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/124186]
Version info
26.11.1.99-master(included in26.11and later)