Skip to content

Introduce unicode_word tokenizer and refactor tokenization to foreach-callback API - #90268

Closed
amosbird wants to merge 3 commits into
ClickHouse:masterfrom
amosbird:unicode-tokenizer
Closed

amosbird wants to merge 3 commits into
ClickHouse:masterfrom
amosbird:unicode-tokenizer

Conversation

@amosbird

@amosbird amosbird commented Nov 18, 2025 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Introduce unicode_word tokenizer and refactor tokenization to a new high-performance forEachToken callback interface, replacing the old iterator-style API to support SIMD and stateful tokenizers.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Details

  1. UnicodeWordTokenExtractor

A new tokenizer implementing a simplified UAX #29–like word segmentation optimized for mixed Latin + CJK text:

  • ASCII letters and digits form multi-character tokens
  • CJK characters are split into single-character tokens
  • _ may appear at token boundaries but cannot form a token alone
  • : connects letters; . and quotes connect characters of the same type
  • Configurable stop words (default: common CJK punctuation)

Example:

SELECT tokens('Hello世界', 'unicode_word');
-- ['Hello', '世', '界']

This provides predictable, index-friendly tokenization while avoiding the complexity and cost of full UAX #29.

  1. tokensForLikePattern

Introduces a new function tokensForLikePattern to tokenize LIKE patterns while respecting wildcard semantics:

  • % and _ are treated as wildcards
  • Escaped wildcards (\%, \_) are treated as literals
  • Tokens adjacent to unescaped wildcards are discarded, since their boundaries are ambiguous

Examples:

SELECT tokens('hello_world'), tokensForLikePattern('hello\_world');
-- ['hello','world']  ['hello','world']

SELECT tokens('hello'), tokensForLikePattern('%hello%');
-- ['hello']  []

This function is primarily intended for debugging and testing purposes, and is used internally to analyze and validate tokenization behavior for LIKE patterns.

  1. Tokenization Pipeline Refactor

The existing iterator-style API (nextInString, nextInStringPadded) is not suitable for:

This PR replaces it with a new forEachToken callback interface, which:

  • Avoids per-token iterator overhead
  • Naturally supports SIMD and stateful implementations
  • Simplifies future tokenizer extensions

All Padded variants are removed. High-performance tokenization in ClickHouse is only used on column data, which is already padded, so the new API assumes padding by design.

  1. Performance Impact

Benchmark:

clickhouse-benchmark \
  --query "SELECT tokens(URL) FROM hits_10m_single FORMAT Null"

Before

  • QPS: 4.28
  • RPS: 42.8M
  • MiB/s: 3584

After

  • QPS: 4.70
  • RPS: 47.0M
  • MiB/s: 3937

Latency improves across all percentiles, with ~10–12% higher throughput and lower median/ tail latency.

@clickhouse-gh

clickhouse-gh Bot commented Nov 18, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7705e5f]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_ubsan) failure
Logical error: Not-ready Set is passed as the second argument for function 'A (STID: 0250-41a5) FAIL cidb

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Nov 18, 2025
@EmeraldShift

Copy link
Copy Markdown
Contributor

Nice! IIRC sparseGrams is also a stateful tokenizer, and I've seen it as a hotspot in some profiles while ingesting into a table with a sparse-ngram text index, so this is a very welcome improvement! Do you think with this refactoring that the patch in #89665 will no longer be necessary?

@amosbird

Copy link
Copy Markdown
Collaborator Author

Do you think with this refactoring that the patch in #89665 will no longer be necessary?

It's still necessary. The high-performance path is only for column data which directly uses the SIMD-friendly foreach API, while literal/constant tokenization keeps the lightweight iterator path.

@rschu1ze

This comment was marked as resolved.

@george-larionov george-larionov self-assigned this Jan 23, 2026
@george-larionov

Copy link
Copy Markdown
Member

Hi @amosbird, as per @rschu1ze's comment we missed this PR. I notice it has some changes that have already been introduced, such as the improved callback interface. Could you pull in the new changes from master? Otherwise git does not show an accurate diff. Then I will review it. Thank you!

Introduce a new tokenizer 'unicode_word' that splits text into tokens
following Unicode word boundary rules (similar to UAX ClickHouse#29):

- ASCII alphanumeric characters form tokens
- Underscore can appear at start/middle/end but cannot be the only char
- Colon connects letters only; dot and single quote connect same types
- Chinese characters are split into single-character tokens
- Supports configurable stop words (defaults to common CJK punctuation)

Also adds tokensForLikePattern() function for LIKE pattern tokenization.

Refactor: Replace nextInStringPadded/stringPaddedToBloomFilter with
template-based forEachToken/forEachTokenToBloomFilter for cleaner code
and better SSE optimization in SplitByNonAlphaTokenExtractor.

The tokenizer can be used with:
- tokens() function: tokens(str, 'unicode_word')
- Full-text index: INDEX idx(col) TYPE text(tokenizer = unicode_word)
@amosbird amosbird changed the title Introduce standard tokenizer and refactor tokenization to foreach-callback API Introduce unicode_word tokenizer and refactor tokenization to foreach-callback API Feb 3, 2026
@amosbird

This comment was marked as resolved.

@george-larionov george-larionov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partial review, I will finish next week. Very nice PR overall, thanks.

Comment thread src/Interpreters/ITokenExtractor.cpp Resolved
Comment thread src/Interpreters/ITokenExtractor.cpp Resolved
Comment thread src/Interpreters/ITokenExtractor.cpp Resolved
Comment thread src/Functions/tokens.cpp Resolved
Comment thread src/Interpreters/ITokenExtractor.cpp Resolved
Comment thread src/Interpreters/ITokenExtractor.cpp Resolved
Comment thread src/Interpreters/ITokenExtractor.cpp Resolved
@rschu1ze

rschu1ze commented Feb 9, 2026 •

Copy link
Copy Markdown
Member

@amosbird @george-larionov Would it be possible to split this PR in three PRs?

  1. One with only the refactorings
  2. One on top of 1. with the new tokenizer (note we'll also need to update this page in this PR)
  3. One on top of 2. with tokensForLikePattern

@amosbird

Copy link
Copy Markdown
Collaborator Author

Sure, that makes sense. This review has already started though, so I’d prefer to let it finish first. Once it’s done, I’ll split this into three PRs as you suggested.

@george-larionov george-larionov left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is my second round of review, not approving only because of @rschu1ze's comment about splitting the PR into multiple, which I think is a good idea. Once you finish any other changes due to comments I can approve the new PRs. Thanks!

Comment thread src/Interpreters/ITokenExtractor.cpp Resolved
Comment thread src/Interpreters/ITokenExtractor.cpp Resolved
Comment thread src/Interpreters/ITokenExtractor.cpp Resolved
Comment thread src/Interpreters/ITokenExtractor.h Resolved
Comment thread src/Interpreters/ITokenExtractor.h Resolved
SELECT tokensForLikePattern('你', 'unicode_word'); -- single Unicode char
SELECT tokensForLikePattern('abc你好', 'unicode_word'); -- ASCII then Unicode
SELECT tokensForLikePattern(',。你好', 'unicode_word'); -- punctuation stop words skipped
SELECT tokensForLikePattern('你好%世界', 'unicode_word'); -- Unicode token with wildcards

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be added to the documentation of this function that the wildcards are essentially meaningless for non-ASCII unicode characters

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is tokenizer-specific. unicode_word splits non-ASCII characters into single-char tokens, so adjacent wildcards discard them. Other tokenizers might have different semantic.

@rschu1ze

This comment was marked as resolved.

@amosbird amosbird mentioned this pull request Mar 13, 2026
1 task done
amosbird added a commit to amosbird/ClickHouse that referenced this pull request Mar 13, 2026
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.
@rschu1ze

Copy link
Copy Markdown
Member

@amosbird I think the only part of this PR which is not merged yet is #99357. Can we continue in the other PR and close this one?

@amosbird amosbird closed this Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants