Skip to content

Search Parquet PLAIN string pages in bulk for a pushed-down substring filter - #126862

Open
groeneai wants to merge 9 commits into
ClickHouse:masterfrom
groeneai:parquet-plain-pages-bulk-substring-scan
Open

groeneai wants to merge 9 commits into
ClickHouse:masterfrom
groeneai:parquet-plain-pages-bulk-substring-scan

Conversation

@groeneai

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

Substring filters (LIKE '%...%', position) pushed into the scan of PLAIN-encoded String columns of Parquet files now search a batch of values at once instead of every value separately, as for MergeTree. Closes #126663.

Description

Closes #126663. Analysis and first patch: @ snelheidai; I narrowed where it applies and measured on x86.

Cost. Since #124507 made apply_string_filters_during_scan the default, PlainStringDecoder::decode checks a substring condition with one dispatched sz_find per value. With StringZilla's AVX2 kernel, master's Q21 uses 14-15% more CPU with the setting than without.

Change. The decoder runs one findBulkScanMatches over a batch of values with their 4-byte length prefixes, inserts defaults for runs without the needle and checks found values against other conditions. This applies only when the longest substring needle has at least 4 bytes and no zero byte, so no occurrence can start in the length prefix of a value below 16 MiB; found values of 16 MiB or more are re-checked. A page starts with a 128-value batch; once more than 1/8 of a batch is found, the rest of the page is checked per value. Decodes with a row filter or of fewer than 128 values (as after another PREWHERE step), DELTA_* pages and shorter or zero-containing needles keep the per-value check.

Validation. Xeon 6975P-C, Release, 100 ClickBench files, 4 threads, paired CPU time, 95% CI: Q21 / Q22 take 0.66 [0.65, 0.67] / 0.71 [0.70, 0.72] of master with the AVX2 kernel (built without AVX-512, as on AMD Zen 3) and 0.91 / 0.92 with the default AVX-512 kernel (the issue: 0.63-0.70 with NEON, 0.91-0.94 with SVE on Graviton3). Short needles, mostly matching values, no substring filter: no change beyond noise (an identical-binary A/A arm reached +4.5%), except %example% of the new performance test (every value matches): CPU +2.1% [+0.1%, +4.2%], and 256-row ranges after another PREWHERE step: +1.3% [-0.1%, +2.7%]. Results match in the issue's 138 differential checks and the new test 05407_string_filters_during_scan_parquet_plain_bulk_scan; parquet/string_filter stateless tests behave as on master; ASan clean.

🤖 Generated with Claude Code


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

groeneai and others added 7 commits October 19, 2026 17:13
… filter

With `apply_string_filters_during_scan` (on by default since ClickHouse#124507),
`PlainStringDecoder::decode` checked a substring condition with one
`StringValueFilter::match` per value, i.e. one dispatched `sz_find` call per
value. For short values the per-call setup dominates, so the pushed-down check
could cost more than the copy it saves (most visibly with the AVX2/NEON search
kernels). MergeTree already scans a whole buffer once with
`StringValueFilter::findBulkScanMatches`.

`decodeWithBulkScan` does the same for a PLAIN page when every value of the
decode call is read: it walks the 4-byte length prefixes of a batch, runs one
`findBulkScanMatches` over the batch bytes (values together with their
prefixes), replaces each run of values without the needle with one
`insertManyDefaults`, and checks the found values against the other
conditions of the filter.

The bulk path is used only when the needle of the bulk-scanned condition (the
longest substring needle of the filter) has at least 4 bytes and no zero byte.
The last byte of a length prefix is zero for a value below 16 MiB, so such a
needle cannot start in the prefix of such a value: every found value contains
the needle and no value is searched twice. A found value of at least 16 MiB is
re-checked against all conditions. Filters with a shorter or zero-containing
longest needle keep the per-value check.

The first batch of a page has 128 values; when more than 1/8 of the values of
a batch contain the needle, the rest of the page goes through the per-value
loop, since every found value restarts the scan.

Closes ClickHouse#126663

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Results with apply_string_filters_during_scan 0 and 1 on plain pages, including
length prefixes that contain the needle bytes, and the exact number of replaced
values for the two needles that only the length prefixes spell: a one-byte
needle equal to the low byte of every 103-byte value's length prefix, and a
needle with zero bytes equal to the prefix of the values 'zz' (all 6000 values
must be replaced in both cases, as with the per-value check).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…pages

A 20M-row Parquet file with one PLAIN-encoded String column (no dictionary),
queried with substring filters that apply_string_filters_during_scan pushes into
the scan: a rare needle (`%google%`), a rare needle together with a second
condition (`%google%` AND `%site1%`), a needle found in about 15% of the values
(`%/news%`) and a needle found in every value (`%example%`).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…checks 16 MiB values

The replaced count of the `LIKE '%needle%'` query is now exact (with one
thread): values decided by the bulk scan do not count towards the minimum
size of skipped values, so the filter stays applied to every value, while
checked one by one these short values turn it off after 8192 checks per row
group. The previous `> 0` check passed without the bulk scan as well.

A third file has a value of exactly 16 MiB, whose length prefix ends with the
first byte of the needle `'\x01xxx'`. The scan finds the needle there and only
the re-check of values of at least 16 MiB rejects it, so all 3 values must be
replaced.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…he other conditions

A value found by the scan is checked against the remaining conditions of the
filter; PREWHERE re-evaluates the whole condition, so a missing re-check does
not change the query result. Log a compound `LIKE '%needle%' AND LIKE '%1%'`
query and pin its replaced count: 198102 = 197310 values without the needle
plus 792 found values without '1'.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…AIN string pages

With a page index, a column of a later PREWHERE step is decoded one row range at a time, often a
single row. The bulk scan exempts its values from the minimum size of skipped values, so the filter
stayed applied to every short value there, while the per-value check turns it off. Decode calls of
fewer than 128 values now keep the per-value check; the test pins this and the 16 MiB re-check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Remove the comments on the bulk scan members and the probe batch size, and shorten the doc comment of decodeWithBulkScan.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 4 findings over 4 rounds, the last round found none) plus my own review of each round.

# Sev Finding Verdict Evidence / action
1 ⚠️ The test's "filter stays applied" check also passed on master AGREE, fixed @ 551d215 exact replaced count with max_threads = 1; master turns the filter off after 8192-16384 checks
2 ⚠️ The re-check of found values against the other conditions was not observed AGREE, fixed @ b0b1125 one logged compound query pins its exact replaced count
3 ⚠️ With a page index, a later PREWHERE step decodes one row range at a time without a row filter, so short ranges were bulk-scanned and short values never turned the filter off AGREE, fixed @ 81d59db decode calls of fewer than 128 values keep the per-value check; a two-step PREWHERE query pins it
4 ⚠️ The 16 MiB re-check would also pass if that needle fell back to the per-value check AGREE, fixed @ 81d59db the file now has more than 8192 short values before the 16 MiB one
5 💡 The 1/8 fallback is also evaluated on a short last batch of a decode call AGREE, noted at worst the rest of that one page uses the per-value check, as on master

Severity: ❌ blocker / ⚠️ major / 💡 nit.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes, for the cost: profile events on a PLAIN file with LIKE '%ogle%' show master checking each value separately (8192-9216 StringValueFilterValuesChecked) and the patch bulk-scanning all 300000. The CPU effect is a paired A/B with an identical-binary A/A arm: ClickBench Q21, 12 pairs, two CPU sets, two StringZilla kernels.
b Root cause explained? Since apply_string_filters_during_scan became the default (#124507), PlainStringDecoder::decode runs one dispatched sz_find per value. For short values the per-call setup dominates, so with the AVX2 kernel master's Q21 uses 14-15% more CPU with the setting than without it.
c Fix matches root cause? Yes: one findBulkScanMatches per batch of values (the primitive the MergeTree path already uses) replaces the per-value calls. The set of replaced values is the one the per-value check produces.
d Test intent preserved / new tests added? New 05407_string_filters_during_scan_parquet_plain_bulk_scan: setting 0 vs 1 on PLAIN pages, including length prefixes that spell the needle and a 16 MiB value whose prefix starts the needle, plus exact replaced counts: the filter stays applied to all 200000 values (master turns it off after 8192-16384 checks, so the test fails on master), found values are re-checked against the other AND condition and at 16 MiB, needles found only in prefixes are rejected, and 1-row ranges of a second PREWHERE step are checked one by one as on master. Fifteen mutations of the new code: fourteen fail the test, one (never falls back) is a performance-only survivor. Performance test parquet_string_filter_plain_pages.xml added.
e Both directions demonstrated? Q21 CPU patched/master 0.66 [0.65, 0.67] (AVX2 kernel) and 0.91 [0.90, 0.93] (default AVX-512 kernel); perf-test %google% 0.92 [0.87, 0.97]; a second PREWHERE step over 1-row and 256-row ranges 1.00 [0.98, 1.01] and 1.01 [1.00, 1.03]. Identical results in every arm; the mutants show the test catches a broken bulk path.
f Fix is general across code paths? The bulk path covers decodes of at least 128 values without a row filter and with a trivial converter. Row-filtered and shorter decodes (both happen after another PREWHERE step), non-trivial converters, DELTA_* string pages and dictionary pages keep their existing code, and MergeTree already scans in bulk.
g Fix generalizes across inputs (params/datatypes/wrappers)? Longest needle under 4 bytes or containing \0: per-value path; values of 16 MiB or more: fully re-checked (both pinned by exact counts in 05407). Empty values, AND/endsWith filters, 1 and 4 threads, two block sizes: same results as master in the issue's 138 differential checks. Nullable(String) on PLAIN pages: the existing 05055_string_filters_during_scan_parquet; the 272 parquet/string_filter stateless tests fail exactly the same set as master (environment-only failures).
h Backward compatible? (maintainer-approved exception only) Yes. No setting, default or format change; the same values are replaced.
i Invariants and contracts preserved? A value is replaced exactly when the per-value check would replace it, and is searched at most once (twice only at 16 MiB or more). Filter statistics are reported at least every 8192 values, and the per-value loop takes over as soon as the filter disables itself; decode calls of fewer than 128 values keep the per-value check, so the filter turns itself off there as on master. One value is inserted per decoded value, and the page pointer advances by exactly the decoded bytes. A malformed length prefix gives the same INCORRECT_DATA as master; ASan clean.

Session id: cron:clickhouse-impl-slot-6:20261020-001700

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @al13n321 @grantholly-clickhouse, could you review this and add the can be tested label so CI runs? It replaces the per-value sz_find of a pushed-down substring filter on Parquet PLAIN string pages with one findBulkScanMatches per batch of values (as for MergeTree), for needles of at least 4 bytes without a zero byte; ClickBench Q21 CPU is 0.66 of master with the AVX2 kernel and 0.91 with the default AVX-512 kernel.

@clickhouse-gh

clickhouse-gh Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0a845dd]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_release, oracle) FAIL
AST fuzzer oracle mismatch FAIL cidb

AI Review

Summary

PR #126862 batches substring searches over PLAIN-encoded Parquet String values, preserving per-value checks for unsupported needles, filtered reads, and short decode ranges. Reviewed commit 0a845ddf72e0dc69d73dd4100c64f9040abd188d, including the diff, surrounding decoder and filter code, tests, and all prior discussion. No actionable findings; recommend approval.

Missing context / blind spots
  • ⚠️ Stateless and performance CI jobs for this commit were still pending during review. No local ClickHouse binary or build directory was available, so runtime evidence relies on the author's reported regression, ASan, and paired performance runs in the PR description and discussion. Completed CI reports would provide independent confirmation.
Final Verdict

Status: ✅ Approve. No code changes requested and no inline findings to post.

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements comp-parquet-reader-v3 Issues related to the native Parquet reader. labels Oct 20, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 21, 2026
groeneai and others added 2 commits October 21, 2026 05:45
clang-tidy readability-container-contains (Build (arm_tidy)) rejects
`find(...) == npos`. Same semantics.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The bulk scan of a page falls back to the per-value check once more than
1/8 of a batch contains the needle. With 17 of the first 128 values, the
rest of the page is checked one by one and the filter turns itself off
after 8320 checks; with 16 of every 128 values, all 40000 values are
bulk-scanned and the 34992 without the needle are replaced. Removing the
fallback, `* 5`, `* 9`, `>=` and skipping the first batch each make the
test fail.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 0a845ddf7 with master 9de8a78df (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 8c48f1213; 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

314 translation units recompiled, 3177 s compile time in total, 313 of them have a recent master baseline.

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 0a845dd

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
AST fuzzer (amd_release, oracle) / AST fuzzer oracle mismatch wrong results, not from this PR (it changes only the Parquet PLAIN string decoder, and this is a MergeTree table): TLP Aggregate mismatch on hasToken(lc, 'v299') over the LowCardinality(Nullable(String)) text-indexed tab of 05258. The text-index direct read returns 0 instead of NULL for NULL rows, so NOT hasToken counts them as well #113332 (external, open)
Mergeable Check, PR praktika rollups of the row above #113332 (external, open)

CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261021-100006

This branch has not been deployed

No deployments
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. pr-performance Pull request with some performance improvements

Projects

None yet

2 participants