Skip to content

Pin prewhere settings in 04051_pk_analysis_stats - #104272

Merged
nickitat merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-04051-pk-analysis-stats-flaky
May 7, 2026
Merged

nickitat merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-04051-pk-analysis-stats-flaky

Conversation

@groeneai

@groeneai groeneai commented May 6, 2026 •

Copy link
Copy Markdown
Collaborator

The test 04051_pk_analysis_stats verifies that PK analysis stats correctly account for granules already dropped by the QueryConditionCache (QCC). The docstring at the top of the test references a specific log-line example where the QCC drops granules for the PREWHERE condition first and PK analysis then runs on the remaining marks.

Without explicit pins, the test runner's --random-settings injection can flip optimize_move_to_prewhere and/or query_plan_optimize_prewhere off. The QCC's verdict then ends up keyed by the WHERE-side hash instead of the PREWHERE-side hash documented in the comment block, exercising a different code path on every run.

The assertion ProfileEvents['FilteringMarksWithPrimaryKeyProcessedMarks'] < total_marks still holds in either path (both sides drop marks before PK analysis runs in MergeTreeDataSelectExecutor::filterPartsByQueryConditionCache). But this variability has been a recurring source of confusion: when an unrelated PR's CI surfaces a transient or PR-caused failure for this test, --diagnose-random-settings minimizes the culprit set down to a single non-causal session setting (e.g. --session_timezone), and the failure then gets misattributed to settings that have no semantic relationship with the test.

Pin both prewhere settings inside the SQL so the test always exercises the documented PREWHERE QCC drop path. SETs inside the test SQL override the CLI flags injected by the runner, so this is robust against any future random-settings expansion that touches prewhere.

Verified locally on a debug build:

  • 50/50 passes with default --random-settings randomisation.
  • 100/100 passes with -j 4 --test-runs 100.
  • 50/50 passes with adversarial overrides (--use_query_condition_cache 0 --optimize_move_to_prewhere False --query_plan_optimize_prewhere False --session_timezone America/Mazatlan --enable_multiple_prewhere_read_steps 0 --max_threads 1 --max_block_size 17886).

CI report that motivated the fix: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104225&sha=ef78dcecf6d3c6a1ccad2481e090794460e778cb&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29

Related: #103857 (comment) (@alexey-milovidov directive to fix this test in a separate PR).

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.406

The test verifies that PK analysis stats correctly account for granules
that the QueryConditionCache already dropped before PK analysis runs.
The docstring at the top of the test shows the expected log lines: the
QCC drops granules for the PREWHERE condition first, then PK analysis
runs on the remaining marks.

Without explicit pins, the test runner's random-settings injection can
flip `optimize_move_to_prewhere` and/or `query_plan_optimize_prewhere`
off, which moves the QCC drop from the PREWHERE-side hash to the
WHERE-side hash. The assertion still holds in that path (the WHERE-side
QCC drop also runs before PK analysis), but the test then exercises a
different code path than the regression comment describes, which has
been a recurring source of confusion when the test fails on unrelated
PRs (the random-settings diagnoser narrows the culprit to a single
session setting that is not actually causal).

Pin both prewhere settings to keep the test exercising the documented
PREWHERE QCC code path. SETs inside the SQL override the CLI flags
injected by the runner.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104225&sha=ef78dcecf6d3c6a1ccad2481e090794460e778cb&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29
@groeneai

groeneai commented May 6, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate self-check (worker session: cron:clickhouse-ci-task-worker:20260506-231500)

a) Deterministic repro? Yes for the FAILURE: PR #104225 commit ef78dcecf6d reproduced 117/117 in CI's --diagnose-random-settings step 1 across 6 build variants. The root cause was that PR #104225's earlier gate blocked QCC writes for any changed Beta-tier setting, and the test runner injects --session_timezone <Beta-tier-zone> on most runs, which made the warmup query unable to populate the cache. The latest PR #104225 commit 1860c812f0f already replaces the tier-based gate with name-pattern matching (allowing Beta-tier settings like session_timezone to remain writable), so the original failure mode is no longer reproducible on PR #104225 HEAD either.

b) Root cause explained? Yes — two layers:

  • Layer 1 (already fixed in PR Refuse QueryConditionCache writes from contexts with relaxed/experimental settings #104225): The buggy commit's gate if (tier == BETA || tier == EXPERIMENTAL) return false rejected QCC writes when any Beta-tier session setting was non-default. The runner injects --session_timezone (Beta) on every run, so the warmup query always failed to populate the cache, the measurement query found no entry, PK analysis ran on all 25 marks, and throwIf(processed >= total_marks) fired.
  • Layer 2 (this PR): Even after Layer 1 is fixed, the test is sensitive to which prewhere setting the runner randomizes. With optimize_move_to_prewhere=0 and query_plan_optimize_prewhere=0, the QCC entry is keyed by the WHERE-side hash instead of the PREWHERE-side hash that the test's docstring describes. The assertion still holds (both sides drop before PK analysis), but the test exercises a code path different from what the comment documents — and --diagnose-random-settings minimizes the culprit to non-causal settings on subsequent runs, which has wasted CI cycles and triage attention.

c) Fix matches root cause? Yes for Layer 2. Pinning optimize_move_to_prewhere=1 and query_plan_optimize_prewhere=1 keeps the test exercising the documented PREWHERE QCC drop path. SETs inside the SQL override CLI flags injected by the runner, so the pin is robust against future random-settings expansion. Layer 1 was fixed independently in PR #104225 itself.

d) Test intent preserved? Yes — the assertion is unchanged; the test still verifies that PK analysis stats correctly account for granules already dropped by the QCC. Pinning prewhere does not weaken any assertion; it just removes a source of test/code-path drift that has historically misled triage.

e) Both directions demonstrated? Yes. With the pin (this PR's change) on a fresh debug build:

  • 50/50 passes with default --random-settings randomisation.
  • 100/100 passes with -j 4 --test-runs 100.
  • 50/50 passes with adversarial CLI flags that previously surfaced as the diagnoser's "minimal culprit": --use_query_condition_cache 0 --optimize_move_to_prewhere False --query_plan_optimize_prewhere False --session_timezone America/Mazatlan --enable_multiple_prewhere_read_steps 0 --max_threads 1 --max_block_size 17886. The pin's SETs override the CLI flags and the test runs on the documented PREWHERE path.

f) Fix is general? This is a test-stability change rather than a real bug fix, so the "search for sibling defects" criterion is N/A. There are no other tests with the same set use_query_condition_cache=1 + reliance on PREWHERE QCC drops shape — this is the only one. (Most other QCC tests carry no-parallel because they read system.query_condition_cache or call SYSTEM CLEAR QUERY CONDITION CACHE; 04051 doesn't.)

@groeneai

groeneai commented May 6, 2026

Copy link
Copy Markdown
Collaborator Author

cc @nickitat — could you take a look? You authored 04051_pk_analysis_stats and are the most active QueryConditionCache contributor; this is a small defensive pin to keep the test on the documented PREWHERE QCC drop path so random-settings noise stops misleading triage. Per @alexey-milovidov's directive on #103857.

@Algunenano Algunenano added the can be tested Allows running workflows for external contributors label May 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [63e5e0d]

Summary: ✅


AI Review

Summary

This PR pins optimize_move_to_prewhere and query_plan_optimize_prewhere in 04051_pk_analysis_stats.sql so the test consistently exercises the documented PREWHERE query-condition-cache path under random settings. The change is narrow, test-only, and aligns with the stated CI-stability goal; no correctness or safety issues were identified.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label May 7, 2026
@nickitat nickitat self-assigned this May 7, 2026
@nickitat
nickitat enabled auto-merge May 7, 2026 11:39
@nickitat
nickitat added this pull request to the merge queue May 7, 2026
Merged via the queue into ClickHouse:master with commit 0a6b8b3 May 7, 2026
165 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label May 7, 2026
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 pr-ci 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