Repository navigation
Text index: Add ICU tokenizer - #109940
Conversation
|
Workflow [PR], commit [5a013e3] Summary: ✅
AI ReviewSummaryThis PR adds an Findings
Final VerdictNeeds changes before merge: the explicit |
This if the key part of the implementation because the BreakIterator is statefull not thread safe
Initial tests now work
c0d84df to
01df3c6
Compare
| ITokenizer::Type::SplitByNonAlpha, | ||
| ITokenizer::Type::SplitByString, | ||
| ITokenizer::Type::AsciiCJK, | ||
| ITokenizer::Type::Icu, |
There was a problem hiding this comment.
Icu is accepted by the row-level evaluator now, but the text-index path still only recognizes 2-argument predicates: MergeTreeIndexConditionText::traverseAtomNode returns false as soon as function_arguments_size != 2, and TextIndexDAGReplacer::processTextIndexFunction still bails out unless children.size() == 2. As a result, hasPhrase(col, phrase, 'icu(''ja'')') on an indexed column never gets positional direct-read or the index's preprocessor / postprocessor normalization, so it can disagree with hasPhrase(col, phrase) as soon as the text index normalizes tokens. Please either route the explicit ICU form through the same text-index path as the inferred form, or reject/document it for indexed columns.
| tokens(value, 'ngrams'[, n]) | ||
| tokens(value, 'sparseGrams'[, min_length, max_length[, min_cutoff_length]]) | ||
| tokens(value, 'array') | ||
| )"; | ||
| FunctionDocumentation::Arguments arguments = { | ||
| {"value", "The input string.", {"String", "FixedString"}}, | ||
| {"tokenizer", "The tokenizer to use. Valid arguments are `splitByNonAlpha`, `splitByString`, `asciiCJK`, `ngrams`, `sparseGrams`, and `array`. Optional, if not set explicitly, defaults to `splitByNonAlpha`.", {"const String"}}, | ||
| {"tokenizer", "The tokenizer to use. Valid arguments are `splitByNonAlpha`, `splitByString`, `asciiCJK`, `icu`, `ngrams`, `sparseGrams`, and `array`. Optional, if not set explicitly, defaults to `splitByNonAlpha`.", {"const String"}}, | ||
| {"locale", "Only relevant if argument `tokenizer` is `icu`: The mandatory locale, for example `'ja'`.", {"const String"}}, |
There was a problem hiding this comment.
Btw., is there a good argument against making the locale argument optional and en the default locale?
There was a problem hiding this comment.
IIUC in some cases the the same characters segment differently under different locale (zh vs ja). So a default could give an inappropriate split for exactly the text that needs icu, and the user might not notice.
For plain space-separated Latin text the locale barely matters, but I won't expect anyone using ICU for it (splitByNonAlpha/asciiCJK already suffice).
Making it explicit is a tiny one-time cost that forces the correct choice, so I'd lean toward keeping it mandatory.
| auto it = iterators.find(locale); | ||
| if (it == iterators.end()) | ||
| { | ||
| if (iterators.size() >= max_cached_iterators) |
There was a problem hiding this comment.
Let's add your reply as a comment to iterators.clear().
| UErrorCode status = U_ZERO_ERROR; | ||
| UBreakIteratorPtr iterator(ubrk_open(UBRK_WORD, locale.c_str(), nullptr, 0, &status), &ubrk_close); | ||
|
|
||
| if (U_FAILURE(status)) |
There was a problem hiding this comment.
The locale contract is not actually enforced here. ICU reports unknown / unsupported locales through warning statuses such as U_USING_DEFAULT_WARNING / U_USING_FALLBACK_WARNING, and U_FAILURE(status) ignores those, so a typo like icu('zz') silently builds an iterator that tokenizes with ICU's fallback locale instead of raising BAD_ARGUMENTS.
That turns a misspelled index definition or function call into wrong tokenization and wrong search results with no user-visible error. This path needs explicit locale validation instead of treating every non-failure ICU status as success.
There was a problem hiding this comment.
Good catch but the suggested remedy doesn't work for word break iterators. ICU's word-break rules exist essentially only at root (brkitr/root.txt), so opening a UBRK_WORD iterator for almost any locale falls back to root and returns exactly U_USING_DEFAULT_WARNING/U_USING_FALLBACK_WARNING. Treating those as failure would reject legitimate locales, not just typos.
There was a problem hiding this comment.
Also, the practical impact is small here: word segmentation is driven by the script, not the locale, so a misspelled locale falls back to essentially the same tokenization rather than silently-wrong results.
There was a problem hiding this comment.
Thinking a bit more on this, if we do want to reject typos, it has to be an explicit locale-availability check and whether to do that is tied to the open question of whether the locale should stay mandatory or get a default.
We can add an availability check if we decide, but I don't think keying off the ICU status is correct.
| {"input", "The input column.", {"String", "FixedString", "Nullable(String)", "Nullable(FixedString)", "Array(String)", "Array(FixedString)", "Array(Nullable(String))", "Array(Nullable(FixedString))"}}, | ||
| {"needles", "Tokens to be searched.", {"String", "Array(String)"}}, | ||
| {"tokenizer", "The tokenizer to use. Valid arguments are `splitByNonAlpha`, `splitByString`, `asciiCJK`, `ngrams`, `sparseGrams`, and `array`. Optional, if not set explicitly, defaults to `splitByNonAlpha`.", {"const String"}}, | ||
| {"tokenizer", "The tokenizer to use. Valid arguments are `splitByNonAlpha`, `splitByString`, `asciiCJK`, `icu('<locale>')`, `ngrams`, `sparseGrams`, and `array`. Optional, if not set explicitly, defaults to `splitByNonAlpha`.", {"const String"}}, |
There was a problem hiding this comment.
These new explicit ICU forms are only equivalent on the brute-force path. On indexed columns the text-index side still only recognizes 2-argument predicates: MergeTreeIndexConditionText::traverseAtomNode returns false as soon as function_arguments_size != 2, and TextIndexDAGReplacer::processTextIndexFunction bails out unless children.size() == 2.
As a result, hasAnyTokens(col, '日本語', 'icu(''ja'')') / hasAllTokens(...) never get the index tokenizer, preprocessor, postprocessor, or direct-read handling. A text index with lowerUTF8, stemming, or stop-word postprocessing can therefore return different results between the explicit ICU form and the inferred 2-argument form on the same indexed column. Please either route the 3-argument form through the same text-index path, or reject/document it for indexed columns.
There was a problem hiding this comment.
This is pre-existing and tokenizer-agnostic. Passing any explicit tokenizer as the 3rd argument to has*Token bypasses the index the same way, and only diverges from the inferred form when the index has a preprocessor/postprocessor. The docs already note this.
This exact area was reviewed earlier in this PR (the separate-argument form was dropped precisely so icu behaves like the others here).
03394_distributed_shuffle_join_with_aggregation under runtime-filter randomization
#112026
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 139/155 (89.68%) · Uncovered code |
485b16d
Add a new
icutokenizer for full-text search.It uses the ICU library's word
BreakIteratorto perform locale-aware Unicode word segmentation, so, languages that do not separate words by whitespace (e.g. Chinese, Japanese, Thai) are split into meaningful words.The locale is a mandatory parameter, e.g.
tokenizer = icu('ja')in a text index, ortokens(s, 'icu', 'ja').ICU is already a ClickHouse contrib, so no new dependency is added; the tokenizer is only registered when built with ICU.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added a new
icutokenizer for text indexes and thetokens,hasAnyTokens, andhasAllTokensfunctions, providing locale-aware Unicode word segmentation (including dictionary-based segmentation for languages such as Chinese, Japanese, and Thai).Version info
26.8.1.666(included in26.8and later)