Skip to content

Enable apply_string_filters_during_scan by default - #124507

Merged
alexey-milovidov merged 16 commits into
masterfrom
enable-apply-string-filters-during-scan
Oct 13, 2026
Merged

alexey-milovidov merged 16 commits into
masterfrom
enable-apply-string-filters-during-scan

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Enable the pushdown of substring search conditions on String columns from PREWHERE into the column scan, added in #115848. Until now it was off by default. The setting has been randomized in clickhouse-test since that PR. The tests that depend on it already pin its value.

The performance comparison showed confirmed regressions with the setting enabled (text_index_like, tpch #9 and #18). They are fixed by making the adaptive disabling of StringValueFilter stricter: the selectivity is evaluated after 8192 values instead of 65536, and the filter is also disabled when it skips less than 16 bytes per checked value on average (e.g. equality on short String columns, like l_returnflag = 'R'), because then the check of every value costs more than the skipped copy. For rows that are not checked one by one, the cost is the number of checks actually done. For dictionary-encoded Parquet pages that is one check per distinct dictionary entry, credited with the bytes the mask is expected to skip across the rows of the column chunk. For a bulk scan of MergeTree values, it is the candidates re-checked against the other conditions of the filter. The Parquet decoders also update the statistics every 8192 values instead of once per page. Before, the pages of all row groups were decoded in parallel and each was checked completely before the filter could disable itself. This fixes the clickbench_parquet_short #6 regression (URL LIKE '%google%' on short, almost unique values).

Related: #115848

Changelog category (leave one):

  • Performance Improvement

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

Enabled the setting apply_string_filters_during_scan by default. Substring search conditions on String columns (LIKE, position, startsWith, endsWith) from PREWHERE are pushed down into the column scan, and non-matching values are read as empty strings without copying their data. This makes such queries faster and lowers their memory usage when reading from MergeTree tables and from Parquet pages whose string values use the PLAIN, DELTA_LENGTH_BYTE_ARRAY, DELTA_BYTE_ARRAY, or dictionary encodings.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.2311-master (included in 26.10 and later)

The optimization was added in #115848.

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

clickhouse-gh Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [21e004a]

Summary: ✅


AI Review

Summary

This PR enables apply_string_filters_during_scan by default and tightens the adaptive disable heuristics for MergeTree and Parquet string scans. Most earlier review points are fixed, but one regression surface remains in the Parquet dictionary heuristic: step-1 chunks are still credited as if every non-null row will consult the mask, even when later pruning means most of those rows are never read.

Findings

⚠️ Majors

  • [src/Processors/Formats/Impl/Parquet/Reader.cpp:2035] The new non_null_values credit is still based on the whole chunk for every step-1 string column. decodeDictionaryPage runs before ColumnIndexAndOffsetIndex in the pruning path, and the same helper is reused after row_ranges_after_column_index / rows_to_read may already have shrunk the chunk. As a result, buildStringValueFilterMask can keep the short-value heuristic enabled by charging skipped bytes for rows that will never consult the mask, re-opening the short-string regression surface on dictionary-encoded Parquet.
    Suggested fix: only credit dictionary-entry checks with rows that are actually scheduled to read the column; when that count is not known yet, fail closed with one row per entry (or defer the byte credit until appendIndexes / appendRepeated observe real rows).
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required: stop crediting buildStringValueFilterMask with full-chunk row counts when the chunk may still be reduced by page pruning or rows_to_read.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Oct 7, 2026
Comment thread src/Core/Settings.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Oct 7, 2026
…ad without the string filter

`PageDecoderInfo::makeDecoder` does not pass the `StringValueFilter` to
`DeltaByteArrayDecoder`, so the setting is a no-op for such pages until
#124503 is merged. Narrow the
advertised support accordingly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Core/Settings.cpp Resolved
…erformance regressions

The performance comparison of this PR showed confirmed slowdowns on both
architectures when `apply_string_filters_during_scan` is enabled:
`text_index_like` (+40..60%), `tpch` #9 and #18 (+20..25%).
https://s3.amazonaws.com/clickhouse-test-reports/PRs/124507/39c02e6cc3685359d573bfcec24d5f6e1c9c05fd/pr/performance_comparison_arm_release_master_head_6_6/report.html

Two causes:
- The filter was evaluated on the first 65536 values before deciding whether
  it pays off. For small queries (e.g. a text index leaves ~100k rows that all
  match) this covers almost the whole query, so the non-selective filter only
  added the per-value check. The window is now 8192 values.
- For short values (e.g. `l_returnflag = 'R'`, `l_shipinstruct = '...'`),
  the filter is selective, but skipping the copy of a few bytes does not
  compensate for checking every value. The filter is now also disabled when
  it skips less than 16 bytes per checked value on average.

Measured locally (CPU cycles, setting off -> on): the `text_index_like`
query went from +19% to +5%, equality on short `String` columns went from
+26..37% to +0.5%. The benefit for selective conditions on long values is kept.

The non-matching values in the dictionary pruning case of
`05055_string_filters_during_scan_parquet` are made longer, so that the test
still checks what it intends to (dictionary materialization must not feed the
selectivity statistics) instead of hitting the new short value threshold.

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

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=124507&sha=39c02e6cc3685359d573bfcec24d5f6e1c9c05fd&name_0=PR&name_1=Integration%20tests%20%28amd_llvm_coverage%2C%204%2F8%29 (test_mask_sensitive_info/test.py::test_iceberg_cluster_function, the secret is found in the server log; in the last 30 days it failed in 10 different PRs across several job types) and provide a fix in a separate PR. If the fix is already in progress, link it here.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The performance comparison showed confirmed slowdowns (text_index_like +40..60%, tpch #9/#18 +20..25%) with the setting enabled. They come from the adaptive heuristic of StringValueFilter: it ran on the first 65536 values before deciding (which is the whole query for small reads), and it kept running for short values, where checking every value costs more than skipping the copy of a few bytes. c23830d evaluates selectivity after 8192 values and also disables the filter when it skips less than 16 bytes per value on average. Local measurement (CPU cycles, off -> on): text_index_like +19% -> +5%; equality on short strings +26..37% -> +0.5%; LIKE '%needle%' on long values still about -11%.

Comment thread src/Common/StringValueFilter.cpp Resolved
@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

Acknowledged. I'm investigating test_mask_sensitive_info/test.py::test_iceberg_cluster_function (the Azurite account key found in the server log); I'll follow up here with a PR (or my findings) shortly.

@clickhouse-gh

clickhouse-gh Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

🟢 AI verdict: improvement — 1 query(s) improved out of 31 analysed

One query was flagged: clickbench Q20 (count(*) ... WHERE URL LIKE '%google%') is -42.2% faster, with the median going from 223 ms to 129 ms. That fits what this PR changes. It turns apply_string_filters_during_scan on by default, so the substring condition on URL is checked while the column is read, and the bulk-scan path reads non-matching URLs as empty strings without copying them. The large drop in CPU time (2906 ms to 1552 ms) shows the speedup comes from less work, not from timing jitter. No regressions were flagged, and the new rules that turn the filter off for short values did not slow down any query in either benchmark.

clickbench

🟢 1 improved

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
🟢 20 improvement 223 129 -42.2% <0.0001 io: Q20 filters URL LIKE '%google%', which this PR now pushes into the column scan by default, so non-matching URLs are no longer copied. CPU time fell from 2906 to 1552 ms, which backs the -42.2% speedup.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 87d93483-c2d5-43d0-9308-b12702506039
  • MIRAI run: 75921e4e-f3b8-4017-8e0d-e44e5cc39cee
  • PR check IDs:
    • clickbench_1106446_1791831765
    • clickbench_1106452_1791831765
    • clickbench_1106458_1791831765
    • tpch_adapted_1_official_1106469_1791831765
    • tpch_adapted_1_official_1106480_1791831765
    • tpch_adapted_1_official_1106516_1791831765

@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

The fix is in #124691 (test-only).

The secret in the log comes from the test's own system.query_log probe, not from a server leak. With apply_string_filters_during_scan = 1, the LIKE half of the probe is moved to PREWHERE alone, and its trace lines and the multi-line Original PREWHERE DAG: dump print the Azurite key without the probe text that the log check excludes. So it fails on every run of this PR and never on master (0 failures in 6455 master runs over 30 days). The other failures in the last 30 days have different causes: inverted Bugfix validation rows, and two PRs with genuine leaks of their own.

#124691 makes the server log checks skip every log entry of the probe queries (matched by query id, including continuation lines), and the probes now pin the setting, so the module covers this path on master too.

alexey-milovidov and others added 2 commits October 8, 2026 21:51
…oded `Parquet` rows

The rule that disables `StringValueFilter` when it skips less than 16 bytes
per checked value assumes that every value was checked against the filter.
For dictionary-encoded `Parquet` pages, every distinct dictionary entry is
checked once in `Dictionary::buildStringValueFilterMask`, and the rows only
look up the result in a mask, so the filter is cheap there even for short
low-cardinality values (e.g. `l_returnflag`).

Add `StringValueFilter::updateStatsForMaskedValues`, used by
`Dictionary::appendIndexes` and `Dictionary::appendRepeated`: such rows
participate in the selectivity rule, but not in the minimum average size of
skipped values, which is now evaluated only over individually checked values.

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

@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 Oct 8, 2026
…rging master

Master now applies the string filter to `DELTA_BYTE_ARRAY` Parquet pages
(#124503), so the setting
description no longer says that such pages are read without it.

The dictionary pruning case of `05055_string_filters_during_scan_parquet` is
restored to the version from master: dictionary-encoded rows no longer
participate in the short value cutoff, so the longer non-matching values are
not needed.

The new test is renumbered to `05339`, because master added another `05338` test.

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

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failures: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=124507&sha=c23830da59a1b40c99e7f571ab0713e737009542&name_0=PR and provide a fix in a separate PR. If the fix is already in progress, link it here.

Also merged master and addressed the review: rows of dictionary-encoded Parquet pages no longer count toward the short value cutoff of StringValueFilter. Updated the setting description, since master now applies the filter to DELTA_BYTE_ARRAY pages too.

Comment thread src/Common/StringValueFilter.cpp Resolved
@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

I checked all three on c23830d:

@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 21e004ae8 with master b4ac7be09 (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

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

Job report

alexey-milovidov and others added 2 commits October 9, 2026 11:50
…MergeTree` values

In `deserializeBinaryFilteredWithKnownSizes`, a substring condition is checked
by a single scan of the buffer of concatenated values (`findBulkScanMatches`),
and only the candidate rows are checked with `matchOtherConditions`. Such values
are not checked one by one, so the minimum average size of skipped values must
not disable the filter for them: selective `LIKE` / `position` on short strings
is exactly the case the bulk scan is for.

Report them with `updateStatsWithoutPerValueChecks` (renamed from
`updateStatsForMaskedValues`, which is also used by the dictionary-encoded
`Parquet` pages), so they only participate in the selectivity rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov pushed a commit to fastio/ClickHouse that referenced this pull request Oct 10, 2026
check_logs() grepped the whole server log for each checked string and
skipped only lines containing the probe's SQL prefix. The probe itself
logs the string, and with apply_string_filters_during_scan = 1 the LIKE
conjunct alone is moved to PREWHERE: the PREWHERE trace lines and the
multi-line "Original PREWHERE DAG:" dump print it without the prefix
(continuation lines carry no query id at all). The Azurite account key
is shared by several tests, so the connection-string probe of
test_table_functions_object_storage_cluster failed the later
test_backup_table_azure_named_collection and test_iceberg_cluster_function
(seen on ClickHouse#124507).

Run the probes with a fixed query id prefix and drop every log entry of
such a query, header and continuation lines, before grepping. All other
log lines are still checked.

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

In a bulk scan of a buffer of concatenated `String` values, the candidates found by
the scan are checked one by one against the other conditions of the filter (e.g. for
`LIKE '%a%b%'`). Such checks now participate in the minimum average size of skipped
values, so the filter disables itself when there are many short candidates. The bytes
skipped by the scan itself are attributed to these checks, so the filter stays enabled
when the scan rejects most of the values before the per-value checks.

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

@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 one more time.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 13, 2026
Merged via the queue into master with commit 317aa94 Oct 13, 2026
176 checks passed
@alexey-milovidov
alexey-milovidov deleted the enable-apply-string-filters-during-scan branch October 13, 2026 17:30
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 13, 2026
alexey-milovidov pushed a commit that referenced this pull request Oct 17, 2026
Since #124131, the only input of the last PREWHERE step on a dictionary-encoded
Parquet `String` column is read as dictionary indexes when no earlier step
reads it (`decode_dictionary_as_replicated`), and its dictionary values come
from `Reader::getDictionaryValues` with `use_string_value_filter = false`. So a
single-column `PREWHERE s LIKE '%R%'` no longer passes through
`StringValueFilter` and `StringValueFilterValuesReplaced` stays 0, which made
the last line of 05339 print 0 on every master run since d64fd50.

#124131 adapted 05055 for the same reason (32e261a), but 05339 was added
by #124507 after #124131's last CI run, so it was never adapted.

Add the same always-true conjunct `AND id < 100000` to the three PREWHERE
queries: the conjunct order is kept, so `s` is read by the first of two steps
and goes through the masked `Dictionary::appendIndexes` path the test checks.
It goes on the two `apply_string_filters_during_scan = 0/1` comparison queries
too, because with the single-column shape both arms read indexes and the
comparison compared nothing. The reference does not change.

Validated on a debug build of d64fd50: the old test fails 6/6, the new one
passes 50/50 randomized (66667 replaced, 0 rows read as indexes). Reporting the
mask lookups through `updateStats` instead of `updateStatsWithoutPerValueChecks`
makes the new test fail 8/8 (5712-6384 replaced).

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

Copy link
Copy Markdown

The ClickBench nightly shows single-file Parquet Q21 (SELECT COUNT(*) FROM hits WHERE URL LIKE '%google%') slower since the 2026-10-14 run, the first after this PR was merged, on machines with 4+ vCPUs, by up to 3x. On 2-vCPU machines it became about 1.5x faster.

Hot time in seconds (best of tries 2–3, range over runs), from the nightly results:

Machine vCPUs 10-11, 10-13 10-14 to 10-17
c6a.large 2 10.5–10.6 6.8–7.1
c6a.xlarge 4 3.39–3.45 3.90–3.97
c6a.2xlarge 8 1.63–1.72 2.36–2.41
c6a.4xlarge 16 1.12 1.20–1.22
c8g.4xlarge 16 0.67–0.68 0.93–0.96
c7a.metal-48xl 192 0.44–0.46 0.78–0.90
c8g.metal-48xl 192 0.29–0.30 0.69–0.91

Q22 (same LIKE) is 28–35% slower on c6a.xlarge and c6a.2xlarge and 14% slower on c8g.4xlarge. With 100 files, Q21 is faster on c6a.4xlarge (1.15 → 0.65 s) and on the metal machines (28–42% less time), but slower on c6a.xlarge (1.66 → 2.21 s). MergeTree Q21 is unchanged or faster.

Toggling the setting with the same binary does not reproduce it on 4-vCPU hosts. With official master builds (warm page cache, clickhouse local --time), single-file Q21 takes 3.17 s with apply_string_filters_during_scan = 1 and 4.06 s with 0 on a Xeon 8375C (1.28x, 95% CI 1.22–1.34), and 3.20 vs 3.66 s on Graviton3; results are identical. On the Xeon, the gain mostly disappears with max_threads = max_parsing_threads = 16 (2.13 vs 2.19 s), so the slowdown may need more cores, or the CPUs of the nightly machines (Zen 3/4, Graviton4). We have no such host. Does single-file Q21 on c8g.4xlarge go back to about 0.68 s with apply_string_filters_during_scan = 0?

alexprabhat99 pushed a commit to alexprabhat99/ClickHouse that referenced this pull request Oct 19, 2026
The merge-queue flaky check reran only the PR's own new/changed tests. That
catches a `master` change breaking the PR's tests, but not the reverse: the PR
breaking a test that landed on `master` after the PR's last CI run. This is how
ClickHouse#124131 broke
`05339_string_filters_during_scan_parquet_short_dictionary_values` from
ClickHouse#124507 and was reverted by
ClickHouse#126396.

The `Config Workflow` job of the merge queue, which has the commit history, now
finds the tests changed on the first-parent history of the queue base after the
PR head commit time (or after the merge base, if the PR merged `master` since),
and stores them for the flaky check. When there are more than 30, the ones whose
names share more words with the names of the files changed by the PR come first.

For the merge-queue commit of ClickHouse#124131 (`d64fd503b42e`), the range has 819 tests,
and 05339 is selected second.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. 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.

4 participants