Repository navigation
Add splitByRegexp tokenizer for text indexes - #110002
Conversation
|
Workflow [PR], commit [016fee6] Summary: ✅
AI ReviewSummaryThis PR adds a Findings
Tests
Final Verdict❌ Changes requested. The new LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 138/145 (95.17%) · Uncovered code |
93d8a8f to
f4f34a1
Compare
| size_t & match_start, | ||
| size_t & match_length, | ||
| OptimizedRegularExpression::MatchVec & matches) | ||
| { |
There was a problem hiding this comment.
Can we make this a lambda or helper function inside ITokenizer.cpp please?
There was a problem hiding this comment.
This is intentional because this function will be used by the splitByRegex sql function. I just didn't want to add that change here to not mix changes
|
|
||
| DROP TABLE tab_regex_simple; | ||
|
|
||
| -- 2. Character class `[ ,;]`: split on a space, comma or semicolon. |
There was a problem hiding this comment.
Can we convert to
SELECT '2. Character class `[ ,;]`: split on a space, comma or semicolon.'
so that the reference file becomes structured by section?
| ( | ||
| id UInt64, | ||
| description String, | ||
| INDEX idx description TYPE text(tokenizer = splitByRegexp('[^\\p{L}\\p{N}#+]+'), support_phrase_search = 1) |
There was a problem hiding this comment.
The regression here only exercises the positional direct-read path. With support_phrase_search = 1 and a fully materialized index, hasPhrase is replaced by the virtual-column exact search, so this does not prove the default no-positions path that originally depended on injecting splitByRegexp(...) into the row-level hasPhrase call. Please add the same C# and / C++ and repro on a default support_phrase_search = 0 index (or split this into two sections) so the hint-mode path is covered as well.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 137/144 (95.14%) · Uncovered code |
| For most functions (`=`, `IN`, `startsWith`, `endsWith`, `LIKE`, `mapContains*`), the text index is used only to skip irrelevant data blocks; ClickHouse still verifies each surviving row using the original predicate against the original column data. | ||
| For token search functions (`hasToken`, `hasAllTokens`, `hasAnyTokens`), the text index is the primary evaluation path: ClickHouse normalizes the needle through the same preprocessor, tokenizer, and postprocessor that were applied at index build time, and uses this normalized form for both indexed and non-indexed table parts. With a postprocessor, the haystack tokens are also normalized at query time (for any tokenizer, not only `array`), so both sides of the comparison are consistently transformed and the result does not depend on whether the index is read directly (setting `query_plan_direct_read_from_text_index`) or whether a given part has a materialized index — e.g. enabling case-insensitive matching for `hasAllTokens(col, ['FOO'])` with a `lower` postprocessor. | ||
| Without `support_phrase_search`, `hasPhrase` uses the index only as a hint and verifies each surviving row with the original predicate; a postprocessor additionally normalizes both the phrase and the haystack tokens the same way, so the result is independent of the read path, and tokens the postprocessor drops do not break phrase adjacency. With `support_phrase_search = 1`, `hasPhrase` uses exact direct reads (still applying the postprocessor, if any). | ||
| Without `support_phrase_search`, `hasPhrase` uses the index only as a hint and verifies each surviving row with the original predicate; a postprocessor additionally normalizes both the phrase and the haystack tokens the same way, so the result is independent of the read path, and tokens the postprocessor drops do not break phrase adjacency. With `support_phrase_search = 1`, `hasPhrase` uses exact direct reads (still applying the postprocessor, if any). This postprocessor support does not extend to the `splitByRegexp` tokenizer: `hasPhrase` on a `splitByRegexp` index combined with a postprocessor is rejected (see footnote ³ below). |
There was a problem hiding this comment.
This compatibility note is still too narrow for the current implementation.
processTextIndexFunction still rewrites postprocessed hasPhrase inputs by arrayStringConcat(..., ' ') and then validates the resulting tokens with isTokenSeparator, so the same read-path divergence remains for other supported tokenizers whose tokens do not round-trip through that encoding. For example, splitByString(['::']) + postprocessor rewrites one::two::three to one two three, which false-negatives because re-tokenization no longer sees ::, and asciiCJK can still reject valid connector tokens like foo.bar or a:b.
Please either make the rewrite tokenizer-aware or narrow this prose/table to exclude every tokenizer whose hasPhrase + postprocessor path is still unsupported.
| /// `+` (which the validation would reject). Rather than bypassing the index - which would silently fall | ||
| /// back to the default `splitByNonAlpha` and produce false positives - reject this combination | ||
| /// explicitly. `splitByRegexp` + `hasPhrase` without a postprocessor is fully supported. | ||
| if (tokenizer->getType() == ITokenizer::Type::SplitByRegexp && has_postprocessor) |
There was a problem hiding this comment.
This rejection only runs when optimizeDirectReadFromTextIndex has a selected text-index condition. ReadFromMergeTree stops populating skip_indexes.useful_indices as soon as use_skip_indexes = 0 (src/Processors/QueryPlan/ReadFromMergeTree.cpp:2348-2353), and collectTextIndexReadInfos immediately bails out on an empty useful_indices set (src/Processors/QueryPlan/Optimizations/optimizeDirectReadFromTextIndex.cpp:162-163). In that mode processTextIndexFunction never injects the table tokenizer or throws here, so hasPhrase(description, 'C# and') on a splitByRegexp('[^\\p{L}\\p{N}#+]+') index still falls back to default splitByNonAlpha semantics and can false-positive on ... C++ and ....
If this combination is intentionally unsupported, the check needs to live somewhere that is not gated on skip-index analysis. Otherwise the new docs/tests should scope the exception to the indexed path instead of promising an unconditional rejection.
| static const std::unordered_set<std::string_view> supported_tokenizers = { | ||
| SplitByNonAlphaTokenizer::getExternalName(), | ||
| SplitByStringTokenizer::getExternalName(), | ||
| SplitByRegexpTokenizer::getExternalName(), |
There was a problem hiding this comment.
hasPhrase is only correct for splitByRegexp while the text-index rewrite is active. collectTextIndexReadInfos returns nothing as soon as there is no usable skip-index condition (for example SETTINGS use_skip_indexes = 0), so optimizeDirectReadFromTextIndex never injects the index tokenizer and standalone FunctionHasPhrase falls back to splitByNonAlpha. On a table with tokenizer = splitByRegexp('[^\\p{L}\\p{N}#+]+'), hasPhrase(description, 'C# and') SETTINGS use_skip_indexes = 0 still matches a row containing C++ and, which brings back the exact false positive this PR is trying to remove. Either the non-index path also needs to pick up the text-index tokenizer, or this support needs to be rejected when the rewrite is unavailable.
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 sizes20 object files changed (+82.40 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 units434 translation units recompiled, 3547 s compile time in total, 434 of them have a recent master baseline. |
Closes: #103783
The existing text-index tokenizers cannot preserve tokens that contain special
characters. For example,
splitByNonAlphatokenizesC++as justc, so atext index over a column of technology names (
C++,C#,F#, ...) producesfalse positives and there is no way to search for the original term:
hasTokenrejects needles with separator characters, and searching for the stripped token
matches unrelated rows.
Usage:
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added a
splitByRegexptokenizer for text indexes and thetokensfunction. It splits the input into tokens using a regular expression as the separator, for exampletokenizer = splitByRegexp('[^\p{L}\p{N}#+]+'), which allows preserving tokens containing special characters such asC++orC#that other tokenizers would break apart.Workflow [PR]
Sync PR [sync-upstream/pr/110002]
Version info
26.8.1.1967(included in26.8and later)