Repository navigation
Add unicode_word tokenizer - #99357
Conversation
|
Workflow [PR], commit [17a6953] Summary: ✅ AI ReviewSummaryThis PR adds a new Missing context
Findings❌ Blockers
ClickHouse Rules
Performance & Safety
Final Verdict
|
8b2a82c to
552c527
Compare
| select tokensForLikePattern('%abc', 'unicode_word'); -- wildcard before token | ||
| select tokensForLikePattern('abc%', 'unicode_word'); -- token before wildcard | ||
| select tokensForLikePattern('a_b%c_d', 'unicode_word'); -- wildcard in between | ||
| select tokensForLikePattern('a\%b a\_c', 'unicode_word'); -- escaped % and _ included as normal chars |
There was a problem hiding this comment.
💡 The inline comment says escaped % and _ are included as normal characters, but the expected output below shows escaped % is treated as a separator (a\%b becomes ["a","b"]). Please either update the comment text or adjust behavior/tests so they match.
552c527 to
ad181f0
Compare
Add a new unicode_word tokenizer that splits text using Unicode word boundary rules similar to UAX ClickHouse#29. ASCII alphanumeric characters form tokens with connectors (underscore, colon, dot, single quote), while non-ASCII Unicode characters become single-character tokens. Stop words (configurable, defaults to common CJK punctuation) are filtered out. The tokenizer is registered in TokenizerFactory and automatically available for text() indexes and the tokens()/tokensForLikePattern() functions. It supports LIKE pattern matching via nextInStringLike(). This is the third and final PR split from ClickHouse#90268.
ad181f0 to
507d206
Compare
| if ((token_start > 0 || is_prefix) && (token_start + token_len < length || is_suffix)) | ||
| tokens.push_back({data + token_start, token_len}); | ||
| } | ||
| } |
There was a problem hiding this comment.
Duplicated substring methods belong in base template
Low Severity
The substringToBloomFilter and substringToTokens implementations in UnicodeWordTokenizer are exact duplicates of the same methods in SplitByNonAlphaTokenizer. Both iterate tokens via nextInString and apply the same first-token/last-token prefix/suffix filtering logic. This identical logic could be provided as a default in the ITokenizerHelper base class template (alongside the existing stringToBloomFilter and stringToTokens defaults), eliminating the duplication and ensuring the filtering logic stays consistent if it ever changes.
Additional Locations (1)
Ergus
left a comment
There was a problem hiding this comment.
Hi @amosbird
Very thanks for this contribution. It looks good, but:
- I added two comments to check in
nextInStringLike. Just to be safe. - I suggested a little test cases to add for improvement.
- It worth comparing performance with
SplitByNonAlphaif possible
Only 1 needs your full confirmation or solution from your side in order to approve this.
…, add tests - Fix pos overflow for truncated UTF-8 in nextInStringLike - Fix escaped-state leak after stop word continue in nextInStringLike - Deduplicate substringToBloomFilter/substringToTokens via shared template helpers - Add edge case tests: empty string, dot as stop word, wrong argument type, stop word with escaped char in LIKE pattern - Add unicode_word tokenizer to performance benchmark
LLVM Coverage Report
PR changed lines: PR changed-lines coverage: 92.31% (288/312, 0 noise lines excluded) |
3a36431
| Compared to `ngrams(N)`, the `sparseGrams` tokenizer produces variable-length N-grams, allowing for a more flexible representation of the original text. | ||
| For example, `tokenizer = sparseGrams(3, 5, 4)` internally generates 3-, 4-, 5-grams from the input string but only the 4- and 5-grams are returned. | ||
| - `array` performs no tokenization, i.e. every row value is a token (see function [array](/sql-reference/functions/array-functions.md/#array)). | ||
| - `unicode_word` splits strings into tokens using Unicode word boundary rules (similar to UAX #29). ASCII alphanumeric characters and underscores form tokens with connectors (`:` for letters, `.` and `'` for same-type characters). Non-ASCII Unicode characters become single-character tokens. Stop words (configurable, defaults to common CJK punctuation) are skipped. An optional parameter `stop_words` can be specified as an array of strings, for example, `tokenizer = unicode_word([',', '。'])`. |
There was a problem hiding this comment.
unicode_word --> unicodeWord please for naming consistency with other tokenizers
There was a problem hiding this comment.
UAX #29 ... this is too cryptic and readers are unable to decipher that. Please add a link to a blog post or an article or something similar + a link to the Unicode spec.
There was a problem hiding this comment.
| Compared to `ngrams(N)`, the `sparseGrams` tokenizer produces variable-length N-grams, allowing for a more flexible representation of the original text. | ||
| For example, `tokenizer = sparseGrams(3, 5, 4)` internally generates 3-, 4-, 5-grams from the input string but only the 4- and 5-grams are returned. | ||
| - `array` performs no tokenization, i.e. every row value is a token (see function [array](/sql-reference/functions/array-functions.md/#array)). | ||
| - `unicode_word` splits strings into tokens using Unicode word boundary rules (similar to UAX #29). ASCII alphanumeric characters and underscores form tokens with connectors (`:` for letters, `.` and `'` for same-type characters). Non-ASCII Unicode characters become single-character tokens. Stop words (configurable, defaults to common CJK punctuation) are skipped. An optional parameter `stop_words` can be specified as an array of strings, for example, `tokenizer = unicode_word([',', '。'])`. |
There was a problem hiding this comment.
"Stopword" is technically correct but also a weird term when used exclusively for interpunctation symbols, e.g. .. Please change Stop words (configurable, default [...] to
An optional parameter (for example, `tokenizer = unicodeWord([', ', '。'])`) specifies character sequences that are ignored, i.e. stopwords. The default skipped characters are: < ... >
Please document in < ... > what characters exactly are skipped by default.
There was a problem hiding this comment.
Rewrote as suggested: parameter-first description with all 11 default skipped characters listed explicitly. #99837
| FunctionDocumentation::Arguments arguments = { | ||
| {"value", "The input string.", {"String", "FixedString"}}, | ||
| {"tokenizer", "The tokenizer to use. Valid arguments are `splitByNonAlpha`, `ngrams`, `splitByString`, `array`, and `sparseGrams`. Optional, if not set explicitly, defaults to `splitByNonAlpha`.", {"const String"}}, | ||
| {"tokenizer", "The tokenizer to use. Valid arguments are `splitByNonAlpha`, `ngrams`, `splitByString`, `array`, `sparseGrams`, and `unicode_word`. Optional, if not set explicitly, defaults to `splitByNonAlpha`.", {"const String"}}, |
There was a problem hiding this comment.
Minor: Please apply the same change in src/Functions/hasAnyAllTokens.cpp.
There was a problem hiding this comment.
Added unicodeWord to both doc strings. #99837
| { | ||
| } | ||
|
|
||
| static const char * getName() { return "unicode_word"; } |
| /// | ||
| /// 4. Token Validity | ||
| /// | ||
| /// * Tokens must contain at least **one ASCII letter or digit** to be valid. |
There was a problem hiding this comment.
In l. 383, we say that each Chinese character forms a single-character token.
In l. 388, we say that all tokens must contain an ASCII letter or digit.
This is a contradiction.
There was a problem hiding this comment.
Split into: ASCII tokens need at least one letter/digit; non-ASCII Unicode characters are valid on their own. #99837
| /// | ||
| /// 2. Connectors | ||
| /// | ||
| /// * `:` connects **letters only**, not digits. |
There was a problem hiding this comment.
For my understanding, do we mean Westerm : here or CJK :?
There was a problem hiding this comment.
Clarified as "ASCII : (U+003A)". #99837
There was a problem hiding this comment.
Please also add a handful of tests to 03403_function_tokens.sql
There was a problem hiding this comment.
And 02346_text_index_creation.sql needs adjustment as well.
…ickHouse#99357) Covers two gaps in PR ClickHouse#99357: 1. hasToken and equality predicates via MergeTreeIndexConditionText with the unicode_word tokenizer — neither code path had stateless test coverage. 2. Empty stop_words array (unicode_word([])) — exercises the zero-stop-words branch in TokenizerFactory, verifying CJK punctuation (e.g. ',') is indexed as a token and found via hasToken. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Follow-up to PR ClickHouse#99357 addressing post-merge review comments: - Rename `unicode_word` to `unicodeWord` for naming consistency with other tokenizers - Add link to Unicode Text Segmentation (UAX ClickHouse#29) spec in docs - Add link to CJK Wikipedia article in docs - Rewrite stop words documentation with explicit default character list - Add `unicodeWord` to valid tokenizer lists in `hasAnyTokens`/`hasAllTokens` docs - Clarify comments: ASCII-only connectors, CJK as single-char tokens, split validity rules - Add `unicodeWord` tests to 03403_function_tokens and 02346_text_index_creation


Add a new
unicode_wordtokenizer that splits text using Unicode word boundary rules similar to UAX #29.Tokenization rules:
:connects letters only;.and'connect same-type characters (letter-letter or digit-digit)The tokenizer is registered in
TokenizerFactoryand automatically available fortext()indexes and thetokens/tokensForLikePatternfunctions. It supports LIKE pattern matching vianextInStringLike.This is the third and final PR split from #90268, following:
forEachTokencallback APItokensForLikePatternfunction #97872 — AddtokensForLikePatternfunctionChangelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Add
unicode_wordtokenizer for full-text indexes and thetokensfunction. It splits text using Unicode word boundary rules: ASCII words are formed with connector characters (underscore, colon, dot, single quote), while non-ASCII Unicode characters become single-character tokens. Configurable stop words default to common CJK punctuation.Documentation entry for user-facing changes
Note
Medium Risk
Adds a new tokenizer used by full-text indexes and
tokens/tokensForLikePattern, which can change query results and index behavior for users who opt into it; the LIKE-pattern parsing and UTF-8 handling paths increase surface area for edge-case bugs.Overview
Adds a new
unicode_wordtokenizer for text indexes and thetokens/tokensForLikePatternfunctions, providing Unicode-aware tokenization with ASCII connector rules and single-character non-ASCII tokens.The tokenizer is registered in
TokenizerFactorywith configurablestop_words(defaulting to common CJK punctuation), wired intoforEachTokendispatch, and exposed via updated docs andsystem.tokenizersoutput.Extends function argument validation/docs for
tokens(...)to acceptunicode_wordand its optionalstop_words, and adds stateless tests covering tokenization, LIKE-pattern behavior, and text-indexLIKEselectivity with the new tokenizer.Written by Cursor Bugbot for commit 507d206. This will update automatically on new commits. Configure here.
Version info
26.3.1.755