Repository navigation
Do not move conditions to PREWHERE when PREWHERE is deferred after FINAL - #109703
Conversation
|
Workflow [PR], commit [7c6e042] Summary: ✅
AI ReviewSummaryThis PR makes deferred- Tests
Final Verdict
|
| SELECT count() FROM (EXPLAIN actions=1 SELECT k FROM t_text_defer_final FINAL PREWHERE hasPhrase(text, 'hello')) WHERE explain LIKE '%__text_index_idx%'; | ||
| SELECT count() FROM (EXPLAIN actions=1 SELECT k FROM t_text_defer_final FINAL PREWHERE hasPhrase(text, 'hello')) WHERE explain LIKE '%Deferred prewhere filter column%'; | ||
|
|
||
| -- whole-token needles: the deferred filter currently runs without the index tokenizer rewrite, |
There was a problem hiding this comment.
These whole-token needles no longer exercise the tokenizer/preprocessor rewrite that motivated the deferred-PREWHERE sync. With hello / goodbye, both the stale deferred DAG and the rewritten DAG produce the same rows, so this test now only proves that direct index read is disabled and that filtering happens after FINAL.
Please keep one tokenizer-sensitive case here (or assert the rewritten deferred filter expression in EXPLAIN) so the updatePrewhereInfo sync in ReadFromMergeTree is actually covered.
There was a problem hiding this comment.
Checked this — the suppression doesn't actually happen. For a deferred PREWHERE the rewrite is a no-op: the deferred filter is excluded from index analysis, so its search queries are never registered in MergeTreeIndexConditionText and the replacer finds nothing to rewrite (not even the tokenizer injection). So optimized stays false and the WHERE path is unaffected.
Ran your exact shape to confirm:
SELECT k FROM t3 FINAL PREWHERE hasPhrase(text, 'hello wor') WHERE hasToken(msg, 'foo')
SETTINGS apply_prewhere_after_final = 1, query_plan_direct_read_from_text_index = 1;
Filter column: __text_index_midx_hasToken_2300886… <- WHERE direct read is intact
Prewhere filter column: hasPhrase(text, 'hello wor')
Deferred filters (applied after FINAL)
Deferred prewhere filter column: hasPhrase(text, 'hello wor')
…final-defer-escape # Conflicts: # src/Processors/QueryPlan/Optimizations/optimizeDirectReadFromTextIndex.cpp # src/Processors/QueryPlan/Optimizations/optimizePrewhere.cpp
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 39/41 (95.12%) · Uncovered code |
|
📊 Cloud Performance Report ✅ AI verdict: This PR is a query-planner correctness fix that only changes behavior when PREWHERE or a row policy is deferred after FINAL (the apply_prewhere_after_final / apply_row_policy_after_final paths); the guards short-circuit for any non-FINAL read. The three flagged ClickBench queries (Q4, Q15, Q30) are plain SELECTs that use none of those features, and each shows flat CPU time against its wall-clock delta, confirming the movements are within-baseline variance rather than a PR effect. All three were deterministically suppressed as within-envelope, and that is the correct call. No action needed. clickbenchFlagged queries (1 of 43)
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
|
@groeneai please create a new PR which adds this test after the current PR is merged. |
|
Acknowledged. I will open a follow-up pull request adding that focused case once #109703 is merged, and I will post the link here. I am not opening it before the merge: the case has to exercise The follow-up will extend |
|
Follow-up is open: #112507 It extends Your acceptance criterion is met. With One correction to what I promised. I said I would also add an The test is tag-free and passed 50 of 50 runs at four-way parallelism. |
|
Hi @yariks5s @nihalzp — a couple of suggestions to make this PR easier for reviewers and the changelog reader. Description (currently
Ignore this if the current title/body is intentional. |
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
…p-deferred-prewhere-test Add a test pinning the deferred-PREWHERE sync from ClickHouse#109703
Before this change, when PREWHERE is applied after FINAL (because apply_prewhere_after_final = 1, or apply_row_policy_after_final = 1 with a non-sorting-key row policy), optimizePrewhere could still move WHERE conditions into PREWHERE and optimizeDirectReadFromTextIndex could rewrite the deferred PREWHERE to a direct index read. Both escape the deferral: the moved/rewritten condition runs during reading, before the deferred row policy, which can expose policy-hidden rows to the predicate (e.g. intDiv(1, k-2) throwing ILLEGAL_DIVISION) or produce results that ignore FINAL deduplication. This PR skips those two optimizations whenever the PREWHERE is deferred after FINAL, and keeps deferred_prewhere_info in sync in updatePrewhereInfo.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Prevents bugs when using deferred prewhere with
optimise_move_to_prewhere=1. When PREWHERE is applied after FINAL, moving WHERE conditions to PREWHERE must be skipped, otherwise they escape the deferral and run before a deferred row policyVersion info
26.8.1.389(included in26.8and later)