Repository navigation
Text index: allow hasPhrase to accept an array of strings as input - #120859
Conversation
Previously, the `hasPhrase` function only accepted a string input, so the text index postprocessor rewrite had to rejoin the postprocessed tokens into a string for `hasPhrase` to re-tokenize. The rejoin used a space, so a tokenizer that does not split on spaces got back a different token sequence and `hasPhrase` returned wrong results. `hasPhrase` now takes an `Array(String)` whose elements are the tokens, so the rewrite passes the index's token sequence directly and nothing is re-tokenized. Fixes #117075.
|
Workflow [PR], commit [0a4b268] Summary: ✅
AI ReviewSummaryThis PR fixes the Final Verdict
LLVM Coverage ReportMeasured on commit 0a4b268.
Changed lines: Changed C/C++ lines covered: 253/276 (91.67%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes39 object files changed (+172.97 KiB total), 1 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 units49 translation units recompiled, 463 s compile time in total, 49 of them have a recent master baseline. |
Ergus
left a comment
There was a problem hiding this comment.
With this PR the splitByRegexp + postprocessor rejection rationale changes.
The comment in MergeTreeIndexConditionText.cpp:1427 justifies the throw entirely in terms of a mechanism deleted in PR: "rejoins the normalized tokens with a space and re-tokenizes them, and validates each token with the splitByNonAlpha separator rule."
We can avoid all that now so please, update the comment ;)
| continue; | ||
|
|
||
| /// An empty element is not a token; the index has no position for one. | ||
| std::string_view element = col_elements.getDataAt(element_index); |
There was a problem hiding this comment.
IIUC FixedString array elements are now matched with their NUL padding. Is that intended?
There was a problem hiding this comment.
Yes. executeMatchPhraseOnArray currently feeds ColumnFixedString::getDataAt straight into the matcher, so an element like toFixedString('aa', 4) is compared as aa\0\0, not aa.
That means the row path returns the wrong answer for padded values, for example hasPhrase([toFixedString('aa', 4), toFixedString('bb', 4)], ['aa', 'bb']) stays false here, while the indexed rewrite tokenizes the same elements down to aa / bb and can return true. So trailing NUL padding still leaks into Array(FixedString) matching in the current code.
| /// Argument types shared by the token-search functions `hasAnyTokens`, `hasAllTokens` and `hasPhrase`. | ||
|
|
||
| /// Function input accepts string, fixed string, array of string or array of fixed strings. | ||
| inline bool isStringOrFixedStringOrArrayOfStringOrFixedString(const IDataType & type) |
There was a problem hiding this comment.
FWIS Array(Nothing) is accepted as a phrase but rejected as an input, so hasPhrase('a', []) should work while hasPhrase([], 'a') throws ILLEGAL_TYPE_OF_ARGUMENT
Ergus
left a comment
There was a problem hiding this comment.
Does a text(tokenizer = array) index can answer hasPhrase? Because the function now accepts the array tokenizer.
FWIS traverseFunctionNode returns false, so the query does a full scan and force_data_skipping_indices fails with INDEX_NOT_USED. The function has to accept array because the postprocessor rewrite passes it, so it cannot be hidden again.
I think that supporting it on the index side is one entry in the set; positions work for any tokenizer. Should we add it now with a force_data_skipping_indices test on an Array(String) column?
| Note that `splitByRegexp` is not supported for `hasPhrase` when the text index also defines a postprocessor. | ||
| The tokenizer argument must be one of `splitByNonAlpha`, `splitByString`, `splitByRegexp`, `array`, `ngrams`, `asciiCJK`, or `icu`. | ||
|
|
||
| If `input` is an [Array(String)](/reference/data-types/array), its elements are tokenized like any other input and their tokens form a single sequence per row, so a phrase may span two elements. With the `array` tokenizer each element is one token, which is how a column of already tokenized values is searched. |
There was a problem hiding this comment.
Can we constrain the sentence in l. 352 to String phrases and needles?
There was a problem hiding this comment.
I'm confused:
The PR message says "nothing gets re-tokenized".
L. 356 says "If input is an Array(String), its elements are tokenized like any other input [...]"
There was a problem hiding this comment.
I'm confused:
The PR message says "nothing gets re-tokenized".
L. 356 says "If
inputis an Array(String), its elements are tokenized like any other input [...]"
the PR message was wrong, I just added the support for array/keyword tokenizer recently, but it was not reflected on the PR message. I just updated the message to include the necessity of the array/keyword tokenizer.
| if (!drop_empty_needles || !element_value.empty()) | ||
| tokens.push_back(element_value); | ||
| } | ||
| /// Compaction is unsound after a postprocessor, and the functions collapse duplicates themselves. |
There was a problem hiding this comment.
"the functions collapse duplicates themselves" is not true for hasPhrase
| ErrorCodes::BAD_ARGUMENTS, | ||
| "Function 'hasPhrase' is not supported on a text index that uses the 'splitByRegexp' tokenizer and a postprocessor"); | ||
| /// Positions restart per element, so the granule token test plus the row-level function decide instead. | ||
| const bool use_positions = has_positions && !indexed_column_is_array; |
There was a problem hiding this comment.
Positions are also turned off for Array columns with a postprocessor, where they would be correct.
|
@ahmadov @Ergus — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
… arrays ClickHouse#120859 made hasPhrase accept an Array(String) haystack, so on a Variant or Dynamic path an Array alternative can satisfy a string-needle hasPhrase while the JSONAllValues index holds that value's text, not its elements. Measured with JSON(v Variant(Array(String), String)) and the array tokenizer: hasPhrase(j.v, 'abc', 'array') counted 0 with the index and 1 without it. The other kept string searches still reject an Array haystack, so only hasPhrase leaves the allow-list. On a bare Dynamic path hasPhrase returns Dynamic and is not a valid filter, so the cost is index use on Variant paths. The Variant index-use control moves to hasToken, and the array face joins the hasAllTokens one in the test.
Add the no hint, no direct read and use_skip_indexes = 0 variants to the default needle rows of 05315, and a hasPhrase(m['k'], repeat(char(0), 3)) cell on tab_post. That cell used to raise "Text index postprocessor produced an invalid token '#'"; since ClickHouse#120859 the hasPhrase rewrite compares token arrays and no longer throws, so it returns the absent-key row like use_skip_indexes = 0 does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Previously, the
hasPhrasefunction only accepted a string input, so the text index postprocessor rewrite had to rejoin the postprocessed tokens into a string forhasPhraseto re-tokenize. The rejoin used a space, so a tokenizer that does not split on spaces got back a different token sequence andhasPhrasereturned wrong results.hasPhrasenow takes anArray(String)whose elements would be tokenized same ashasAny/AllTokens.Additionally, the
array/keywordtokenizer support has been added with this PR. The reason for supporting it is due to how postprocessor rewrite thehasPhrasequeries.Suppose we have this text index definition:
and the query is:
hasPhrase(message, 'foo()bar');after the rewrite that query conceptually becomes:
since the
array/keywordtokenizer is no-op, we need it to avoid double-tokenization of array elements.Fixes #117075.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
On a column whose
textindex has a postprocessor,hasPhraserejoined the index tokens with a space before matching, so it returned wrong results unless that space matched how the tokenizer splits. AllowinghasPhraseto accept anArray(String)removes joining tokens completely.Workflow [PR]
Sync PR [sync-upstream/pr/120859]
Version info
26.10.1.1248-master(included in26.10and later)