Skip to content

Speed up reading Parquet columns after a PREWHERE step - #124186

Merged
grantholly-clickhouse merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/parquet-prewhere-plain-batch
Oct 16, 2026
Merged

grantholly-clickhouse merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/parquet-prewhere-plain-batch

Conversation

@groeneai

@groeneai groeneai commented Oct 6, 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):

Reading Parquet columns after a PREWHERE step 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 PREWHERE step the remaining columns are decoded with the step's row filter. PlainFixedSizeDecoder::decode then called convertColumn once 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 (ClickBench WatchID). Also, applyPrewhere filtered 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 (one memcpy per all-passing block) directly into the column, or into a buffer converted once. applyPrewhere returns early when every row passes.

Performance (Xeon 6975P-C, Release, 100M-row ClickBench files, max_threads = max_parsing_threads = 4, 11 pairs, 95% CI):

query before after ratio
sum(UserID), sum(WatchID), all rows pass 265 ms 180 ms 0.69 (0.67-0.71)
same, 43% pass 183 ms 141 ms 0.76 (0.74-0.78)
same, AdvEngineID <> 0 (2% pass) 124 ms 117 ms 0.95 (0.92-0.97)
sum(toUInt32(EventTime)), sum(WatchID), all pass 397 ms 290 ms 0.74 (0.72-0.75)
ClickBench q32 686 ms 662 ms 0.96 (0.95-0.98)
sum(length(URL)), sum(length(Title)) 1874 ms 1874 ms 1.00 (0.99-1.01)

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_size compares PREWHERE reads of PLAIN pages with the source table for 1- to 32-byte values, with and without conversion, many filter shapes and two-step PREWHERE; 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

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

groeneai and others added 2 commits October 6, 2026 08:58
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]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-issue PR origin: picked up an external GitHub issue labels Oct 6, 2026
@groeneai

groeneai commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author
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).

# Sev Finding Verdict Evidence / action
1 ⚠️ 05331_parquet_prewhere_plain_fixed_size checks results only, so reverting either optimization keeps it green DISAGREE Speed is covered by the new parquet_prewhere_plain_columns.xml in Performance Comparison; each part was measured alone (decoder 0.72 of master, early return 0.94-0.96 on top); the .sql test guards correctness and fails on seven mutations.
2 💡 #124131 materializes replicated columns after the filter loop in applyPrewhere, which the all-pass return here skips; the two merge cleanly AGREE, noted Results stay correct since #124131 also materializes at delivery; whichever lands second moves the call before the early return.

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

Session id: cron:clickhouse-review-slot-9:20261006-090700

@groeneai

groeneai commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Performance only: PREWHERE on a Parquet file reading a PLAIN fixed-size column; release A/B, all-pass query 265 ms -> 180 ms (11 pairs).
b Root cause explained? With a row filter, PlainFixedSizeDecoder::decode made one virtual convertColumn call per passing value; applyPrewhere filtered even when every row passed.
c Fix matches root cause? Yes: passing values are gathered and converted in one call, like the other filtered decoders; an all-pass step returns early.
d Test intent preserved / new tests added? New 05331_parquet_prewhere_plain_fixed_size (fails on seven mutations of the new code) and perf test parquet_prewhere_plain_columns.xml.
e Both directions demonstrated? Speed: base vs patched release, alternating pairs. Correctness: test passes on master and patch, fails on each mutation.
f Fix is general across code paths? Other filtered decoders already batch; dictionary pages already skip an all-pass filter; nullable/array columns do not use this path.
g Fix generalizes across inputs (params/datatypes/wrappers)? 1- to 32-byte values with and without conversion, many filter shapes incl. UInt8 above 1 and two steps, block sizes 1..65409 on ASan and debug.
h Backward compatible? (maintainer-approved exception only) Yes: no setting, no format change.
i Invariants and contracts preserved? The decoder appends exactly the passing values in order and advances by num_values; after an all-pass step columns and row filter describe the same rows.

Session id: cron:clickhouse-impl-slot-5:20261006-065700

@clickhouse-gh

clickhouse-gh Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [813572f]

Summary: ✅


AI Review

Summary

This PR speeds up Parquet reads after PREWHERE in two places: PlainFixedSizeDecoder::decode now batches filtered PLAIN fixed-size values instead of invoking convertColumn once per passing row, and Reader::applyPrewhere now keeps the subgroup unfiltered when a PREWHERE step passes every row. I did not find an unresolved correctness, compatibility, or safety issue in the current diff. The performance claim is supported by the PR description's before/after measurements, and the added stateless plus performance tests cover the changed surfaces well enough for this review.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 813572f.

Metric Baseline Current Δ
Lines 88.30% 88.20% -0.10%
Functions 92.10% 92.10% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 52/52 (100.00%) · Uncovered code

Full report · Diff report

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]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 32 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: eb4ecee9-57e0-4d3a-a3ac-6a8ce8e37b1e
  • MIRAI run: bde07f00-acb8-406a-8892-745348131639
  • PR check IDs:
    • clickbench_12425_1791293440
    • clickbench_12431_1791293440
    • clickbench_12437_1791293440
    • tpch_adapted_1_official_12448_1791293440
    • tpch_adapted_1_official_12459_1791293440
    • tpch_adapted_1_official_12911_1791293457

@clickhouse-gh

clickhouse-gh Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 813572fdb with master 033ec6873 (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

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

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

Job report

filter += filter_offset;
pass_count = countBytesInFilter(filter, 0, num_values);
if (pass_count == 0)
return;

@grantholly-clickhouse grantholly-clickhouse Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here there's no filter and every value passes


const bool direct = converter->isTrivial();
char * to = nullptr;
if (direct)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

@grantholly-clickhouse grantholly-clickhouse Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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. groeneai-origin-issue PR origin: picked up an external GitHub issue pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

4 participants