Skip to content

Fix StringValueFilter turning itself off when several threads update its statistics at once - #128277

Merged
devcrafter merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-string-value-filter-stats-race
Nov 1, 2026
Merged

devcrafter merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-string-value-filter-stats-race

Conversation

@groeneai

@groeneai groeneai commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

...

Description

05339_string_filters_parquet_delta_length_byte_array (added in #124521) sometimes reports StringValueFilterValuesChecked: 24576 or 26384 instead of 30000, while the query result stays correct: #127845, #111218.

The filter object is shared by all readers of a column. updateStatsImpl added a batch to values_checked and then to values_replaced with two independent relaxed fetch_adds, and used the two results as a consistent pair. When two row groups update at the same time, one thread can see the other's checked without its replaced (7372 * 2 < 16384), so it disables the filter for the rest of the query. The CI numbers are exact multiples of the 8192-value update batches. updateStatsOfIndividualChecks had the same race on the bytes-skipped pair.

Now the dependent counter is added first, the count with acq_rel, and the dependent counter is re-read afterwards, so it always covers every batch included in the count. Only the moment the optimization switches off changes; results are unaffected.

Validation: with a temporary 200 ms sleep inside the race window, the base build undercounts in 10/10 runs (9 with the exact CI numbers), and the fix gives 30000 / 27000 in 90/90 runs with the sleep in each window of the new code. Without any sleep, 16 parallel loops on x86 undercount 4 times in 4163 base runs and 0 times in 4485 fixed runs. The string_filters test family passes.


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

Version info

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

groeneai and others added 2 commits October 30, 2026 18:24
…atistics at once

`updateStatsImpl` added a batch to `values_checked` and then to `values_replaced`
with two independent relaxed `fetch_add`s and used the returned totals as a
consistent pair. When two row-group decoders update at the same time, one can
see the other's `checked` without its `replaced` (e.g. 7372 * 2 < 16384) and
disable the filter for the rest of the query. That is what makes
`05339_string_filters_parquet_delta_length_byte_array` report
`StringValueFilterValuesChecked: 24576` or `26384` instead of `30000`
(exact multiples of the 8192-value update batches) on ARM CI lanes.
`updateStatsOfIndividualChecks` had the same race on the
`values_checked_individually` / `bytes_skipped_individually` pair.

Add the dependent counter first, the count with `acq_rel`, and re-read the
dependent counter afterwards. All modifications of the count are RMWs, so an
updater whose RMW includes a batch synchronizes with that batch's update and
its later load of the dependent counter includes it. The dependent total can
only run ahead of the count, which errs towards keeping the filter enabled
until the next update. Query results are unaffected either way.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the two-line ordering comment in updateStatsImpl to one invariant line.

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; 0 findings; bounded: 1 full pass).
It read StringValueFilter.cpp/.h in full, every statistics caller in MergeTree and Parquet, the string_filters tests and both cited CI reports.

# Sev Finding Verdict Evidence / action
- - No findings - -

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

Session id: cron:clickhouse-review-slot-9:20261030-182700

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. A temporary 200 ms sleep between the first updater's two fetch_adds makes the base build print the CI numbers 24576 / 22116 in 9/10 runs.
b Root cause explained? updateStatsImpl uses two independent relaxed RMW results as a pair, so a concurrent batch can be in one total but not the other and the filter is disabled spuriously; updateStatsOfIndividualChecks has the same split pair.
c Fix matches root cause? Yes. The dependent counter is added first, the count with acq_rel, then the dependent counter is reloaded, in the shared statistics object all 9 call sites use.
d Test intent preserved / new tests added? The test is unchanged; its exact-count oracle is the regression test (fails on base under the probes and about 1 in 1000 runs naturally).
e Both directions demonstrated? Yes. Base + sleep: 10/10 undercount; fix + sleep in each of the 4 new windows: 90/90 30000 / 27000; no probe, x86, 16 loops: base 4/4163, fix 0/4485.
f Fix is general across code paths? Yes. Both decision pairs are fixed; the four counters are used only in these two functions, which MergeTree and every Parquet string decoder go through.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. The statistics layer is type-agnostic; the string_filters family passes 116/116, the target test 50/50.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, default or format change.
i Invariants and contracts preserved? Yes. A disable decision uses only a consistent pair; disabled stays one-way; no lock or allocation is added.

Session id: cron:clickhouse-impl-slot-6:20261030-180000

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov @scanhex12, could you review this and add the can be tested label so CI runs? StringValueFilter::updateStatsImpl used two independent relaxed fetch_add results as a consistent pair, so concurrent row-group decoders could see a batch's checked count without its replaced count and switch the filter off mid-query (the flaky 05339_string_filters_parquet_delta_length_byte_array counters); the dependent counter is now added first and re-read after an acq_rel count.

@clickhouse-gh

clickhouse-gh Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a162313]

Summary: ✅


AI Review

Summary

This PR fixes concurrent statistics updates that could spuriously disable StringValueFilter, covering both selectivity and skipped-byte decisions. The existing regression test and reported before/after reproductions support the fix. No actionable findings remain for commit a16231335b42b854a5a73931b6065f19336e4d27.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-parquet-reader-v3 Issues related to the native Parquet reader. labels Oct 30, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 31, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a16231335 with master 52529f0c7 (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

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

Job report

@devcrafter
devcrafter enabled auto-merge October 31, 2026 20:40
@devcrafter
devcrafter added this pull request to the merge queue Nov 1, 2026
Merged via the queue into ClickHouse:master with commit 129258f Nov 1, 2026
179 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 1, 2026
alexey-milovidov pushed a commit to Onyx2406/ClickHouse that referenced this pull request Nov 1, 2026
The merge queue fast-forwards master over up to 5 merges in one push and
MasterCI runs once, for the push head. For a push, praktika read the changed
files of the head commit only (`gh api commits/{sha}`), so a job affected
only by an earlier commit of the same push was filtered as "Not affected by
the changed files and not required", and functional/integration shards
skipped themselves as "Only test files changed". At master c189bd9 (ClickHouse#128277
in `src/` + ClickHouse#128142 tests only) this skipped 95 jobs, including both
`Build (*_release_performance_reference)`, so PR performance runs based on it
fell back to the official build (`long_primary_key` +16% on amd, ClickHouse#127846).
At f760cbb all 32 builds were skipped.

Now a push event records its `before` (`_Environment.PUSH_BEFORE_SHA`), and
`GH.get_changed_file_statuses` walks first parents from the head down to it,
reading all pages of each commit's file list (`gh api --paginate`), and
unions them (the newest commit's status wins). If the files cannot all be
known - an all-zero or malformed `before` (created branch), a failed or
malformed read, a commit at GitHub's 3000-file listing limit, a walk that
does not reach `before` within 50 commits - the result is `None` with a
warning, never a partial list, and it is not cached. `store_data.py` keeps
`None` for push events, so no job is filtered by it (`Job.is_affected_by`);
before, a failed read stored `[]`, which filtered every non-required job.
Without `before` (local runs, the native orchestrator) the head-only read
is unchanged; PR, merge queue, schedule and dispatch are unchanged.

Validated locally against the real GitHub API by replaying the 404 batched
master pushes of October: the result equals the union of every pushed
commit's files (re-parsed independently, commit lists from git) on 402, and
is unknown on the 2 that contain a 3000-file commit (ClickHouse#126194, and the docs
translation ClickHouse#124860); the job filter keeps 14,871 more job-runs on 131
pushes, plus 952 test shards that self-skipped now run in full, ~+7.8% of
October's 203,420 master job-runs, including both reference builds on 106
pushes. 100 single-commit pushes give identical statuses, with one request
for heads of up to 100 files.

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

can be tested Allows running workflows for external contributors comp-parquet-reader-v3 Issues related to the native Parquet reader. pr-not-for-changelog This PR should not be mentioned in the changelog 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