Repository navigation
Fix text-search functions outside the filter ignoring the text index tokenizer and pre/postprocessor under a WHERE clause - #124092
Conversation
…nder a WHERE processAndOptimizeTextIndexFunctions rewrites hasToken, hasAnyTokens, hasAllTokens and hasPhrase in the Filter/Expression steps above ReadFromMergeTree with the text index tokenizer and postprocessor. It analyzed each step's DAG on its own, so the haystack was matched against the index expression by its step-local name. The analyzer renames the storage column `s` to `__table1.s` in its first expression; with a WHERE (a filter or the PREWHERE companion expression), ORDER BY s, a subquery or query_plan_merge_expressions = 0 that rename lives in a lower step, so the SELECT-list step saw `INPUT __table1.s`, which never matched the index column `s`, and the function silently kept the default tokenizer and no postprocessor. The same row then returned a different value than the query without WHERE. The walk now keeps the composition of the Filter/Expression DAGs it has passed (the current step's inputs expressed over the scan's columns), and selectConditions analyzes each function merged onto that composition, the way RPNBuilder names lambda captures. The rewrite itself still edits the step's own DAG, so headers and output names are unchanged. The first step above the scan and PREWHERE read the scan directly and are analyzed as before. A column renamed from a non-indexed one composes to its real source and is still not matched. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The `tq where` query is the test's PREWHERE coverage (the next line is the explicit no-PREWHERE twin), but optimize_move_to_prewhere is randomized and drawn False in about 5% of runs, in which the line exercised only the Filter path. Pin optimize_move_to_prewhere and query_plan_optimize_prewhere on that query so it always covers the PREWHERE plan shape. Co-Authored-By: Claude Opus 5.5 <[email protected]>
State the dangling-pointer invariant instead of narrating why the DAG is re-read. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (2 rounds): 1 finding fixed, 2 nits noted
The re-review of the fix round found nothing new. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261005-183300 |
|
Workflow [PR], commit [305f449] Summary: ✅
AI ReviewSummaryThe PR fixes text-search functions outside the scan filter inheriting incomplete text-index semantics under Missing context / blind spots
Final VerdictStatus: ✅ Approve. No new inline findings to submit. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes3 object files changed (-22.48 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units7 translation units recompiled, 18 s compile time in total, 7 of them have a recent master baseline. |
|
Sure, I'll wait for #117774 to land, then rebase on it and check what is left. For that check: the CI build of #117774's current head ( |
# Conflicts: # src/Processors/QueryPlan/Optimizations/optimizeDirectReadFromTextIndex.cpp
…r-select-list-under-where Conflict in optimizeDirectReadFromTextIndex.cpp with ClickHouse#117774, which matched a step's inputs to scan columns by renaming them in place (`source_columns`) for the duration of the rewrite. Keep this branch's composed-DAG lookup instead and drop the renaming: it follows alias chains only, and the nodes the rewrite rebuilds above the function take their names from the renamed inputs, so on master `SELECT groupArray((id, hasAnyTokens(s, 'hello'))) FROM t WHERE id > 0` throws NOT_FOUND_COLUMN_IN_BLOCK, and a SELECT-list function above `ORDER BY id` still keeps the default tokenizer. The composed DAG renames nothing in the step's DAG. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since ClickHouse#117774 the preprocessor applies outside the filter too, and its check that the preprocessor expression contains the haystack compares names. Above a WHERE the haystack is an input renamed by a step below (`__table1.s`), so express it over the scan's columns first, like the index lookup does. Add to 05331 the shapes master still gets wrong after ClickHouse#117774: a SELECT-list function above `ORDER BY id`, a tuple built over it (throws NOT_FOUND_COLUMN_IN_BLOCK on master), and a preprocessor index, whose unmerged-expressions line fails without the haystack change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
#117774 has landed, so I merged master and re-checked (master
So the PR now replaces that renaming with the composed-DAG lookup, which also feeds the preprocessor's haystack check, and the test gained these shapes. The lines #117774 already fixed stay as a guard for the replacement. The description is updated. |
|
@ahmadov Would you like to check this one? |
…r-select-list-under-where
|
Dear @ahmadov, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
Related: #117077
Related: #117774
Related: #116958
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
hasToken,hasAnyTokens,hasAllTokensandhasPhraseoutside the filter (for example in theSELECTlist) ignoring the text index tokenizer, preprocessor and postprocessor when the query has aWHEREclause, which made the same row return a different result than withoutWHERE.Description
On 26.8.18.2, row 2 matches without the
WHEREand does not match with it:The rewrite that applies the index tokenizer and processors to these functions above
ReadFromMergeTreematched the haystack by its name in one step's DAG. With aWHERE, the analyzer's rename ofsto__table1.sis in the step below, so the function never matched the index.#117774 handles part of this by renaming a step's inputs to the scan columns during the rewrite. On master the second query above still returns 0, and
SELECT groupArray((id, hasAnyTokens(s, 'hello'))) FROM t WHERE id > 0now throwsNOT_FOUND_COLUMN_IN_BLOCK, because the nodes rebuilt above the function are named after the renamed inputs.This PR replaces the renaming: the walk composes the DAGs of the steps it passes, and the index match and the preprocessor's haystack check look at each function expressed over the scan's columns.
The test covers the four functions, preprocessor, postprocessor, tokenizer-only, expression,
mapValuesandArray(String)indexes, PREWHERE, a subquery,ORDER BY, unmerged expressions and a renamed-column negative control. ItsORDER BY id, tuple and preprocessor lines fail on master; the rest pass there since #117774 and guard the replacement. 267 of 268 text-index stateless tests pass locally (the other needs a CI-only server setting).The
AST fuzzer (amd_release, oracle)TLP WHERE mismatch from https://s3.amazonaws.com/clickhouse-test-reports/PRs/118490/38f06bfdb9d5fd15f4cee160f9f89004185a6182/pr/ast_fuzzer_amd_release_oracle/job.log still reproduces on master and is consistent with this PR.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/124092]