Skip to content

Add a test pinning the deferred-PREWHERE sync from #109703 - #112507

Merged
PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-109703-followup-deferred-prewhere-test
Jul 30, 2026
Merged

PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-109703-followup-deferred-prewhere-test

Conversation

@groeneai

@groeneai groeneai commented Jul 30, 2026 •

Copy link
Copy Markdown
Collaborator

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):

...

Description

Related: #109703

Follow-up requested by @ yariks5s in #109703 (comment). Test-only, no source change.

#109703 added a sync in ReadFromMergeTree::updatePrewhereInfo that carries a later PREWHERE rewrite into deferred_prewhere_info. Neither test it shipped pins that line: deleting it leaves both green. 04653 says so itself, that the deferred filter runs without the tokenizer rewrite so its results must not depend on it, which is exactly the property the sync controls.

deferFiltersAfterFinalIfNeeded snapshots deferred_prewhere_info, and only afterwards does the text-index pass rewrite the PREWHERE and call updatePrewhereInfo. The isPrewhereDeferredAfterFinal gate there suppresses only the direct-read half, so the preprocessor rewrite still fires on a deferred PREWHERE and the sync is what makes the post-FINAL filter use it.

The new case uses preprocessor = lower(text) with mixed-case data and a lower-case needle, so only the rewritten predicate matches. The predicate is duplicated in WHERE because a deferred PREWHERE is excluded from index analysis, making the WHERE copy the only registrant of the text index; without it the rewrite never fires. use_skip_indexes and use_skip_indexes_if_final are pinned because the runner randomizes the latter 50/50 and a false draw unregisters the index and makes the case vacuous.

Validation: with the sync reverted the new query returns nothing instead of one row, while both controls and every pre-existing assertion in 04653 and 04652 stay green. Tag-free 50 of 50 runs pass.

One note on the request: asserting the rewritten deferred filter in EXPLAIN is not achievable. The deferred block prints only the filter's column name, and the rewrite preserves that name, so EXPLAIN actions=1 is byte-identical with and without the sync. The result-based case is shipped instead.

Version info

  • Merged into: 26.8.1.420 (included in 26.8 and later)

PR ClickHouse#109703 added a sync in ReadFromMergeTree::updatePrewhereInfo that copies a
later PREWHERE rewrite into deferred_prewhere_info when the PREWHERE is deferred
after FINAL. Neither test it shipped makes that line load-bearing: deleting it
leaves both green. 04653 states as much itself, that the deferred filter runs
without the index tokenizer rewrite so its expected results must not depend on
it, which is exactly the property the sync controls.

deferFiltersAfterFinalIfNeeded snapshots deferred_prewhere_info, and only after
that does the text-index pass rewrite the PREWHERE and call updatePrewhereInfo.
The isPrewhereDeferredAfterFinal gate in that pass suppresses only the
direct-read/virtual-column half, so the tokenizer/preprocessor rewrite still
fires on a deferred PREWHERE and the sync is what makes the post-FINAL filter
use the rewritten DAG.

The new case uses preprocessor = lower(text) with mixed-case data and a
lower-case needle so that only the rewritten predicate matches. The predicate is
duplicated in WHERE because a deferred PREWHERE is excluded from index analysis
and text is not a sorting-key column, which makes the WHERE copy the only
registrant of the text index; without it the rewrite never fires. use_skip_indexes
and use_skip_indexes_if_final are pinned per statement because the runner
randomizes the latter 50/50 and a false draw unregisters the index and would make
the case silently vacuous rather than failing.

With the sync reverted the new query returns nothing instead of one row while
both controls and every pre-existing assertion stay green. Asserting the
rewritten deferred filter in EXPLAIN is not possible: the deferred block prints
only the filter's column name and the rewrite preserves that name, so EXPLAIN
actions=1 is byte-identical with and without the sync.

Related: ClickHouse#109703
@groeneai

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

Two independent passes before publication: a cold code review by a reviewer that never saw the
implementation's own evidence, then a second-model gate over the frozen diff and PR body. Verdicts
are the reviewer's adjudication, each backed by a code citation or a measurement.

Outcome: 0 blockers, 0 majors. The second-model gate returned 0 findings over
2 files changed, 36 insertions(+). The cold pass raised 7 items, all confirmations rather than
defects. One informational item is carried into the description; one suggestion from the
implementation side was declined.

💡 The EXPLAIN half of the request is not achievable, and the description says so.
Independently verified by reading, not only by measuring: the deferred block in
ReadFromMergeTree::describeActions (:4711-4733, and its JSON twin at :4790-4796) prints only
deferred_prewhere_info->prewhere_column_name and never dumps prewhere_actions, unlike the
row_level_filter branch immediately above it (:4706-4707) which does. The rewrite also
re-aliases its result to the original name (optimizeDirectReadFromTextIndex.cpp:864, :720).
So no EXPLAIN string can separate a synced from an unsynced deferred filter, and
EXPLAIN actions=1 came back byte-identical on both arms with non-vacuity asserted. The
result-based case is shipped instead.

⚠️ Two extra controls were offered and declined. The implementation had measured a
without-FINAL variant and an apply_prewhere_after_final = 0 variant. Both return the same value
on both arms, so neither would fail if the sync were removed, and the deferral condition is already
pinned by 04652 and by 04653's existing line-25 control. Adding them would be decoration.

✅ Carriers checked independently and matched. All three updatePrewhereInfo callers reaching
ReadFromMergeTree were enumerated from scratch. optimizePrewhere.cpp:319 cannot be reached for a
deferred PREWHERE (early return at :222-223), and optimizeTopK.cpp:224 cannot be reached under
FINAL (tryOptimizeTopK returns at :95-96 on isQueryWithFinal, which
isPrewhereDeferredAfterFinal itself requires at :2503). The independent list matched the plan's.

✅ Liveness holds structurally, not just empirically. :3294 is the only writer of
deferred_prewhere_info downstream of the :2520 snapshot, and :4495-4499 is what feeds it into
the post-FINAL FilterTransform, so removing the sync necessarily runs the pre-rewrite DAG. The
measured arm agrees: the main SELECT moves from one row to none while both controls hold, which
rules out a collapsed fixture.

✅ Randomization exposure re-derived, including one axis the reviewer raised.
use_skip_indexes_if_final is drawn randint(0, 1) and forces use_skip_indexes false for a
FINAL query (ReadFromMergeTree.cpp:2311-2312), emptying useful_indices so
collectTextIndexReadInfos returns early (:162-163) and the case would be silently vacuous rather
than failing. The statement-level pin closes that. The reviewer additionally checked the
compatibility axis, since use_skip_indexes_if_final changed default in the 25.6 block and stress
runs inject a random compatibility: applyCompatibilitySetting skips any manually changed setting
(Settings.cpp:9011-9013), so a per-statement SETTINGS clause wins in either order. The same
protection covers the file's existing explain_query_plan_default pin, which both controls need.

✅ No test tag is owed. Parallel replicas is structurally unavailable here because
findParallelReplicasQuery.cpp:54-56 refuses a table expression carrying FINAL. The no-parallel
on a sibling preprocessor test is justified in that file by a UDF factory it creates, which this case
does not. A tag-free 50 of 50 run at four-way parallelism passed.

Session id: cron:clickhouse-review-slot-53:20260730-010200

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. With the sync at ReadFromMergeTree.cpp:3293-3294 deleted, the new query returns 0 rows instead of 1 on every run, and tests/clickhouse-test 04653_text_index_prewhere_deferred_after_final FAILs deterministically. Not a probabilistic repro.
b Root cause explained? Yes. deferFiltersAfterFinalIfNeeded snapshots deferred_prewhere_info; the text-index pass then rewrites the PREWHERE and calls updatePrewhereInfo. The isPrewhereDeferredAfterFinal gate in that pass suppresses only the direct-read half, so the preprocessor rewrite still fires on a deferred PREWHERE and the sync is the only thing carrying it into the filter that runs. The two shipped tests were built to be insensitive to that rewrite, so deleting the sync left both green.
c Fix matches root cause? Yes. No source change: the sync itself merged in #109703. What was missing is coverage, so this adds the case whose failure is caused by removing the sync, using a preprocessor that makes the rewrite change the answer.
d Test intent preserved / new tests added? Yes. Every pre-existing assertion in 04653 and 04652 is byte-unchanged and stays green on both arms; the new case is purely additive.
e Both directions demonstrated? Yes, through the real runner. Unmodified master: 04653 OK, 04652 OK. Sync reverted: 04653 FAIL, 04652 OK. The appended block is three identical 1 lines, so I re-measured each assertion individually: the main SELECT moves 1 to 0 while both controls stay at 1, proving the failure is the sync and not a collapsed fixture. Build ID moved between arms and the restore relinked bit-identically.
f Fix is general across code paths? Yes. All updatePrewhereInfo carriers were enumerated. optimizePrewhere.cpp:319 is unreachable for a deferred PREWHERE (early return at :222, already pinned by 04652), and optimizeTopK.cpp:224 is unreachable under FINAL (tryOptimizeTopK returns at :95-96 on isQueryWithFinal, which isPrewhereDeferredAfterFinal itself requires). The deferred-row-policy trigger reaches the same single line, so a second case would be redundant.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A for a test-only change, no new code path to matrix. The fixture's own axes were measured instead: both pinned settings at both values, PREWHERE-only vs PREWHERE plus WHERE, apply_prewhere_after_final 0 and 1, and with and without FINAL.
h Backward compatible? (maintainer-approved exception only) Yes, trivially. No setting, no format change, no SettingsChangesHistory.cpp entry, no user-visible behaviour change.
i Invariants and contracts preserved? Yes. Test-only, no code contract touched. The fixture keeps ReplacingMergeTree deduplication meaningful (k = 2 never matches) so the case cannot decay into a trivially true assertion, and the matching row is the FINAL winner so the answer is non-empty on a correct build.

Extra hygiene: no no-parallel tag, proven by a tag-free 50 of 50 run at 4-way parallelism; unique table name and a DROP IF EXISTS prologue; use_skip_indexes_if_final is randomized 50/50 by the runner, so it is pinned per statement, and forcing the hostile value globally still passes 6 of 6. use_skip_indexes_on_data_read measured as not load-bearing and was left unpinned rather than shipped as decoration.

Session id: cron:clickhouse-impl-slot-47:20260730-004200

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @CurtizJ, could you review this? It is test-only. #109703 added a sync in ReadFromMergeTree::updatePrewhereInfo that carries a later text-index rewrite into deferred_prewhere_info, but neither test it shipped pins that line, because the text-index pass rewrites the PREWHERE after deferFiltersAfterFinalIfNeeded has already snapshotted it. This adds the case that reddens when the sync is removed, using a lower preprocessor so only the rewritten predicate matches.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jul 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cd2247e]

Summary: ✅


AI Review

Summary

This PR adds a stateless regression test for the deferred-PREWHERE sync introduced in ReadFromMergeTree::updatePrewhereInfo by #109703. I traced the current deferred-filter path in ReadFromMergeTree, checked the sibling coverage in 04652_prewhere_final_move_skipped_when_deferred, and the new 04653 case looks genuinely load-bearing: the duplicated WHERE clause is what re-registers the text index, while the mixed-case fixture makes the deferred filter depend on the later text-index preprocessor rewrite. I did not find blockers or majors, and the PR's CI report is all green, including the targeted stateless shard.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 30, 2026
@PedroTadim PedroTadim self-assigned this Jul 30, 2026
@PedroTadim
PedroTadim enabled auto-merge July 30, 2026 08:52
@PedroTadim
PedroTadim added this pull request to the merge queue Jul 30, 2026
Merged via the queue into ClickHouse:master with commit 791808c Jul 30, 2026
179 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 30, 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