Repository navigation
Compare token sequences for hasPhrase over a postprocessed text index - #119367
alexey-milovidov wants to merge 4 commits into
Conversation
`hasPhrase` on a column indexed with a `text` index that has a postprocessor returned wrong results on
every execution path - with the index, with `use_skip_indexes = 0` and with
`query_plan_direct_read_from_text_index = 0` - in both directions:
CREATE TABLE tab (id UInt64, doc String,
INDEX idx doc TYPE text(tokenizer = splitByString(['()']), postprocessor = lower(doc)) GRANULARITY 1)
ENGINE = MergeTree ORDER BY id;
INSERT INTO tab VALUES (1, 'a()bc()d');
SELECT count() FROM tab WHERE hasPhrase(doc, 'bc()d'); -- 0, the phrase is right there
and, with a tokenizer whose separators include `x`, a document `'A X B'` matched the phrase `'a b'`
although the index keeps `a` and `b` two positions apart.
To give the row-level function the index's analyzer semantics, the rewrite postprocessed the tokens of
both sides and rejoined them with a space for `hasPhrase` to re-tokenize. That assumed a space is a
separator of the index tokenizer. For `splitByString(['()'])` it is not: the rejoined haystack came
back as the single token `'a bc d'`, so no multi-token phrase could match. And when a postprocessed
token is itself a separator, the re-split dissolved it and made tokens adjacent that the index keeps
apart. The validation next to the rejoin checked tokens against the `splitByNonAlpha` alphabet rather
than the tokenizer's own separators, so neither case was noticed.
Compare the two token sequences directly instead: the haystack's postprocessed tokens against the
phrase's postprocessed tokens with `hasSubstr`, which is true exactly when the second array occurs in
the first as a contiguous run. No string is rebuilt and nothing is re-tokenized, so the tokenizer's
separators no longer matter. The tokens the postprocessor emptied are filtered out of the haystack
array (the index has no position for them), which is what the rejoin achieved by collapsing them into
adjacent separators. A phrase the postprocessor empties entirely still matches nothing, as before.
Two existing tests recorded the restriction that the rejoin imposed and are updated: a postprocessor
that emits a separator (`concat(message, ' x')`) in
`02346_text_index_function_hasPhrase_postprocessor` and a gram that spans a separator in
`04353_text_index_explicit_tokenizer_argument` were rejected with `BAD_ARGUMENTS`; both now answer,
consistently on every read path and in agreement with the same data without an index.
Closes: #117075
|
Workflow [PR], commit [b2942ee] Summary: ❌
AI ReviewSummaryThis PR fixes the Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit b2942ee.
Changed lines: Changed C/C++ lines covered: 57/58 (98.28%) · Uncovered code |
Build profile diff (arm_release)Comparing commit See the job log for details. |
…se-postprocessor-117075
`MergeTreeIndexConditionText::traverseFunctionNode` rejected `hasPhrase` on a `splitByRegexp` text
index that also has a postprocessor with `BAD_ARGUMENTS`. The rejection existed for exactly the two
assumptions this pull request removed from the row-level rewrite: it rejoined the postprocessed
tokens with a space for `hasPhrase` to re-tokenize (a regexp separator need not be a space, so the
rejoin collapsed tokens) and it validated every token against the `splitByNonAlpha` alphabet (which
rejects a token such as `c#`). Because `selectConditions` builds the search query before
`optimizeDirectReadFromTextIndex` runs, those queries never reached the new token-sequence path on
any read path, so this one supported tokenizer family still could not use `hasPhrase`.
The rewrite compares the two postprocessed token sequences directly with `hasSubstr` now, so no
separator and no alphabet is assumed and the tokenizer needs no special case - in `match_tokens`
mode either, where the index positions are exactly the matches the rewrite reproduces. Remove the
guard, and with it the footnote in the text index documentation and the note in the `hasPhrase`
`FunctionDocumentation` that declared the combination unsupported.
The two tests that recorded the rejection now assert real answers, cross-checked on the index path,
with `use_skip_indexes = 0` and with `query_plan_direct_read_from_text_index = 0`:
`02346_text_index_SplitByRegexp_tokenizer` matches the phrase `C# and` only in the row that has the
token `c#` (not the one with `c++`) and lowercases the phrase too, and
`02346_text_index_split_by_regexp_extract` matches `tag:RED tag:GREEN` against a document indexed
with `splitByRegexp('tag:(\w+)', 1)`, while the reversed phrase and a phrase the tokenizer finds no
token in match nothing.
A differential run of the `text_index`/`hasphrase`/`hastoken`/`tokenizer`/`postprocessor`/`full_text`
stateless tests (230 tests) passes, except for two failures that are local-environment only (an
interserver connection over the loopback cluster and the missing `test.hits` dataset).
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
|
I suppose this case would be solved by allowing |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fix wrong results of
hasPhraseon a column indexed with atextindex that has a postprocessor: a phrase present in the document was not matched (and one whose tokens are not adjacent was), on every execution path, when the index tokenizer does not split on spaces or a postprocessed token is itself a separator.Documentation entry for user-facing changes
...
hasPhraseon a column indexed with atextindex that has a postprocessor returned wrong results on every execution path — with the index, withuse_skip_indexes = 0and withquery_plan_direct_read_from_text_index = 0— in both directions:and, with a tokenizer whose separators include
x, a document'A X B'matched the phrase'a b'although the index keepsaandbtwo positions apart.To give the row-level function the index's analyzer semantics, the rewrite postprocessed the tokens of both sides and rejoined them with a space for
hasPhraseto re-tokenize. That assumed a space is a separator of the index tokenizer. ForsplitByString(['()'])it is not: the rejoined haystack came back as the single token'a bc d', so no multi-token phrase could match. And when a postprocessed token is itself a separator, the re-split dissolved it and made tokens adjacent that the index keeps apart. The validation next to the rejoin checked tokens against thesplitByNonAlphaalphabet rather than the tokenizer's own separators, so neither case was noticed.Compare the two token sequences directly instead: the haystack's postprocessed tokens against the phrase's postprocessed tokens with
hasSubstr, which is true exactly when the second array occurs in the first as a contiguous run. No string is rebuilt and nothing is re-tokenized, so the tokenizer's separators no longer matter. The tokens the postprocessor emptied are filtered out of the haystack array (the index has no position for them), which is what the rejoin achieved by collapsing them into adjacent separators. A phrase the postprocessor empties entirely still matches nothing, as before.Two existing tests recorded the restriction that the rejoin imposed and are updated: a postprocessor that emits a separator (
concat(message, ' x')) in02346_text_index_function_hasPhrase_postprocessorand a gram that spans a separator in04353_text_index_explicit_tokenizer_argumentwere rejected withBAD_ARGUMENTS; both now answer, consistently on every read path and in agreement with the same data without an index.Also,
MergeTreeIndexConditionText::traverseFunctionNoderejectedhasPhraseon asplitByRegexpindex with a postprocessor withBAD_ARGUMENTS, for exactly the two assumptions above: the rejoin collapsed tokens when the regexp separator is not a space, and the validation rejected a token such asc#.selectConditionsbuilds the search query before the rewrite runs, so those queries never reached the new token-sequence path on any read path. The guard is removed, and with it the footnote in the text index documentation and the note in thehasPhraseFunctionDocumentationthat declared the combination unsupported. The index side needed no change - it tokenizes and postprocesses the phrase with the index's own tokenizer and postprocessor, and positions are dense on both sides, so it is symmetric with every other tokenizer, inmatch_tokensmode too.02346_text_index_SplitByRegexp_tokenizerand02346_text_index_split_by_regexp_extractrecorded the rejection and now assert real answers on all three read paths.New test:
05204_text_index_phrase_postprocessor. Verified in both directions, and a differential run of thetext_index/hasphrase/hastoken/tokenizer/postprocessor/full_textstateless tests (221 tests) shows no failures other than the two references updated here.Closes: #117075
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/119367]