Repository navigation
Text index: inject tokenizer properly to supported functions outside WHERE/PREWHERE - #113154
Conversation
…de WHERE/PREWHERE Previously, `hasToken`/`hasAnyTokens`/`hasAllTokens`/`hasPhrase` functions applied the text index tokenizer/preprocessor/postprocessor only in the `WHERE`/`PREWHERE` filter and only when skip-index analysis ran. In the SELECT list, in aggregate arguments, or with `use_skip_indexes = 0` they fell back to the default tokenizer, producing results inconsistent with the index path. The rewrite is now built from the index definition and applied to the Expression/Filter steps above the scan as well.
|
Workflow [PR], commit [5a3a788] Summary: ✅
AI ReviewSummaryThis PR extends text-index rewrite/injection outside Findings❌ Blockers
Final Verdict
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 209/213 (98.12%) · Uncovered code |
…dex-functions-tokenizer-pass
|
@Ergus Would you like to review this one - ideally today or tomorrow since it's a bug fix for a user. |
…functions-tokenizer-pass
…functions-tokenizer-pass
| SELECT '-- WHERE: preprocessor applied only with use_skip_indexes = 1'; | ||
|
|
||
| SELECT count() FROM tab WHERE hasAnyTokens(s, 'hello') SETTINGS use_skip_indexes = 0; | ||
| SELECT count() FROM tab WHERE hasAnyTokens(s, 'hello') SETTINGS use_skip_indexes = 1; |
There was a problem hiding this comment.
Looking at the reference these two queries are producing different results... that's what this PR is intended to fix right?
There was a problem hiding this comment.
actually this is already an existing index-only-preprocessor behavior: https://fiddle.clickhouse.com/a4d4f696-c26e-46bf-b788-5c1f4b4e2d4b. The PR just adds coverage for both use_skip_indexes on/off values.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Object file sizes8 object files changed (+40.93 KiB total), 0 added.
737 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 units290 translation units recompiled, 2769 s compile time in total, 290 of them have a recent master baseline. |
| const bool apply_preprocessor = is_filter_dag && condition.info->index != nullptr && condition.is_index_analyzed && needApplyPreprocessor(function_name) && preprocessor && preprocessor->hasActions(); | ||
| const bool apply_tokenizer = needApplyTokenizer(function_name) && tokenizer; | ||
| const bool apply_postprocessor = needApplyPostprocessor(function_name) && has_postprocessor; | ||
|
|
There was a problem hiding this comment.
condition.info->index != nullptr is too strong a gate for the preprocessor now. collectTextIndexReadInfos only fills info.index when MergeTreeDataSelectExecutor::canUseIndex succeeds for the union of updated columns across all queried parts, but the actual skip-index pruning still uses skip_indexes.useful_indices per part and will keep using the analyzed condition on unaffected parts.
That means a single patch part / on-the-fly update can make the whole step fall back to the inject-only entry here, so hasToken / hasAnyTokens / hasAllTokens / hasPhrase stop applying the preprocessor even though the query is still using the text index on other parts. A repro is an index TYPE text(tokenizer = splitByNonAlpha, preprocessor = lower(s)): SELECT count() FROM t WHERE hasToken(s, 'beta') is correct before a lightweight update, then flips back to the raw row-scan result after UPDATE t SET s = s WHERE ... until the patch is merged away.
Can we decouple “this predicate was analyzed by a skip index” from “the index can be direct-read on every queried part”? Preserving the analyzed condition_template even when direct read is disabled, and gating only the virtual-column path on direct-read eligibility, would keep the WHERE rewrite stable. This also needs a focused regression with a patch part / on-the-fly update.
| { | ||
| if (isRowScanPassThroughStep(step)) | ||
| continue; | ||
| break; |
There was a problem hiding this comment.
processAndOptimizeTextIndexFunctions still gives up as soon as the path hits a JOIN, but the legacy ExpressionAnalyzer computes the SELECT list only after appendJoin (src/Interpreters/ExpressionAnalyzer.cpp:2127-2133,2252-2253). A query like SELECT hasAnyTokens(t.s, ['foo']) FROM t INNER JOIN u ON t.id = u.id therefore keeps the text-search call above the join, hits this break, and never gets the tokenizer/postprocessor injection. That leaves the advertised outside-WHERE fix query-shape-dependent in a supported path.
Can we either rewrite read-side-only text-search expressions before the join, or keep traversing joins when the expression depends only on columns from this ReadFromMergeTree, and add a focused join regression?
Cherry pick #113154 to 26.8: Text index: inject tokenizer properly to supported functions outside WHERE/PREWHERE
…pported functions outside WHERE/PREWHERE
Backport #113154 to 26.8: Text index: inject tokenizer properly to supported functions outside WHERE/PREWHERE
Resolves one conflict in optimizeDirectReadFromTextIndex.cpp selectConditions(), where ClickHouse#113154 replaced the None-mode branch condition. Strict union: the canAnswerFunctionNode guard keeps its place ahead of the branch, and the new is_index_analyzed binding and widened condition are taken unchanged.
Merging master brings ClickHouse#113154, which applies the index tokenizer, preprocessor and postprocessor to the row-level evaluation as well, not only to the index read. Two expectations in this test were written against the earlier behaviour. The stop-word case asserted that hasAnyTokens for a term the postprocessor maps to the empty string returns rows when the index is not read. It now returns nothing in every combination, which is what the two-argument form returns too, so the parity this test exists to pin still holds and is no longer pinned on a value where the index and the scan disagreed with each other. Measured on the merge and on a pre-ClickHouse#113154 master: before, the two-argument form returned no rows with use_skip_indexes = 1 and two rows with use_skip_indexes = 0; after, both forms return nothing under either setting. A search for a term the postprocessor keeps is added next to it, so an empty result there cannot be mistaken for a table that matches nothing. The separator-spanning hasPhrase case used use_skip_indexes = 0 to show the phrase does not occur. The postprocessor is applied on that path now as well, so the query raises the same BAD_ARGUMENTS as the four assertions above it and no longer witnesses anything. position() is used instead, which reaches neither the index nor the postprocessor.
Cherry pick #113154 to 26.6: Text index: inject tokenizer properly to supported functions outside WHERE/PREWHERE
…pported functions outside WHERE/PREWHERE
Cherry pick #113154 to 26.7: Text index: inject tokenizer properly to supported functions outside WHERE/PREWHERE
…pported functions outside WHERE/PREWHERE
26.6 gained #115586 (`1cc90fa8b08`) after this backport branch was cut, so its `tryMergeExpressions` block collided with the loop #113154 restructures. Resolved to master's combined form: hoist the merge into a `walk_begin` iterator and keep the "parent is not a filter" LOG_TRACE inside the walk. The resolved `processAndOptimizeTextIndexFunctions` is identical to the one on master at #113154's merge commit `5c856f8790c`, except for the `isPrewhereDeferredAfterFinal` arguments, which this backport already omits because 26.6 has no such method. The DistinctStep.h include is kept: `isRowScanPassThroughStep` needs it. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… field
The cherry-pick brought master's 7-argument call, but the `deterministic_`
parameter comes from `8493efa7d20` ("Disable the query condition cache for any
non-deterministic virtual column"), which is on 26.8 only -- not on 26.6 or
26.7 -- and is unrelated to #113154. Dropping the argument is behaviour-neutral:
master passes `true`, which is also its default.
error: no matching constructor for initialization of 'VirtualColumnDescription'
note: candidate constructor not viable: requires 6 arguments, but 7 were provided
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
26.7 gained #115586 after this backport branch was cut, so its `tryMergeExpressions` block collided with the loop #113154 restructures. Resolved to master's combined form: hoist the merge into a `walk_begin` iterator and keep the "parent is not a filter" LOG_TRACE inside the walk. The resolved `processAndOptimizeTextIndexFunctions` is identical to the one on master at #113154's merge commit `5c856f8790c`, except for the `isPrewhereDeferredAfterFinal` arguments, which this backport already omits because 26.7 has no such method. The DistinctStep.h include is kept: `isRowScanPassThroughStep` needs it. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… field
The cherry-pick brought master's 7-argument call, but the `deterministic_`
parameter comes from `8493efa7d20` ("Disable the query condition cache for any
non-deterministic virtual column"), which is on 26.8 only -- not on 26.7 or
26.6 -- and is unrelated to #113154. Dropping the argument is behaviour-neutral:
master passes `true`, which is also its default.
error: no matching constructor for initialization of 'VirtualColumnDescription'
note: candidate constructor not viable: requires 6 arguments, but 7 were provided
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The backport keeps a `CAST` that drops `Nullable` under the preprocessor, so the preprocessor output is named after the `CAST` node, e.g. `lower(CAST(__table1.s, 'String'_String))`. The postprocessor step named its haystack input with `getNameWithoutAliases`, which rebuilds the name from the children (`lower(CAST(s, 'String'_String))`), so `mergeNodes` did not reuse the node and the query failed with `NOT_FOUND_COLUMN_IN_BLOCK` in test `05340_text_index_preprocessor_cast_drop_nullable`. Use the `result_name` of the haystack node, as master does since #113154 (its 26.7 backport is not merged). This also fixes the same failure for an expression haystack with a postprocessor, e.g. an index on `concat(s, '')`, which failed on 26.7 before this backport. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=125016&sha=cd400f8bcecb0c83df6515bba43d684b28066d67&name_0=BackportPR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%29 PR: #125016 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Resolves #108808
Previously,
hasToken/hasAnyTokens/hasAllTokens/hasPhrasefunctions applied the text index tokenizer/preprocessor/postprocessor only in theWHERE/PREWHEREfilter and only when skip-index analysis ran. In the SELECT list, in aggregate arguments, or withuse_skip_indexes = 0they fell back to the default tokenizer, producing results inconsistent with the index path.The rewrite is now built from the index definition and applied to the Expression/Filter steps above the scan as well.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Inject tokenizer to supported text index functions outside of
WHERE/PREWHERE.Workflow [PR]
Sync PR [sync-upstream/pr/113154]
Version info
26.8.1.2016