Skip to content

Refactor tokenization to forEachToken callback API - #97871

Merged
george-larionov merged 4 commits into
ClickHouse:masterfrom
amosbird:refactor-tokenizer-includes
Mar 2, 2026
Merged

george-larionov merged 4 commits into
ClickHouse:masterfrom
amosbird:refactor-tokenizer-includes

Conversation

@amosbird

@amosbird amosbird commented Feb 24, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Performance Improvement

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

Refactor tokenization to a new high-performance forEachToken callback interface, replacing the old iterator-style API to support SIMD and stateful tokenizers. Part of #90268.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.3.1.270

Replace the iterator-style nextInStringPadded API with a high-performance
forEachToken callback interface:

- Remove nextInStringPadded, stringPaddedToBloomFilter, and forEachTokenPadded
- Move SSE optimization into SplitByNonAlphaTokenizer::forEachTokenImpl member
- forEachToken dispatch uses assert_cast for devirtualized calls
- Add forEachTokenToBloomFilter helper replacing stringPaddedToBloomFilter
- Update all call sites: hasAnyAllTokens, ngrams, tokens, MergeTree indexes
- Clean up includes in MergeTreeIndexConditionText and TextIndexCache

The new API assumes column data is always padded (which is guaranteed by
ClickHouse's Column types), enabling SIMD optimizations without a separate
padded code path. The callback interface avoids per-token virtual dispatch
overhead and naturally supports future SIMD and stateful tokenizers.
@amosbird
amosbird force-pushed the refactor-tokenizer-includes branch from d163670 to 189f1f0 Compare February 24, 2026 15:23
@clickhouse-gh

clickhouse-gh Bot commented Feb 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6360b17]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Feb 24, 2026
Replace the iterator-style nextInStringPadded API with a high-performance
forEachToken callback interface:

- Remove nextInStringPadded, stringPaddedToBloomFilter, and forEachTokenPadded
- Move SSE optimization into SplitByNonAlphaTokenizer::forEachTokenImpl member
- forEachToken dispatch uses assert_cast for devirtualized calls
- Add forEachTokenToBloomFilter helper replacing stringPaddedToBloomFilter
- Update all call sites: hasAnyAllTokens, ngrams, tokens, MergeTree indexes
- Clean up includes in MergeTreeIndexConditionText and TextIndexCache

The new API assumes column data is always padded (which is guaranteed by
ClickHouse's Column types), enabling SIMD optimizations without a separate
padded code path. The callback interface avoids per-token virtual dispatch
overhead and naturally supports future SIMD and stateful tokenizers.
@rschu1ze

Copy link
Copy Markdown
Member

@george-larionov may want to check (he's out at the moment).

@george-larionov

Copy link
Copy Markdown
Member

(curious if the AI can add any useful comments)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates ClickHouse’s tokenization pipeline to use the forEachToken callback-style API in several text-index and string-function call sites, removes the old padded iterator variants, and adds a SIMD-optimized callback implementation for SplitByNonAlphaTokenizer.

Changes:

  • Replace uses of nextInStringPadded / forEachTokenPadded / stringPaddedToBloomFilter with nextInString, forEachToken, and forEachTokenToBloomFilter.
  • Introduce SplitByNonAlphaTokenizer::forEachTokenImpl (SIMD-accelerated) and a shared forEachTokenToBloomFilter helper.
  • Adjust includes/forward declarations to match the refactor.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Storages/tests/gtest_SplitByNonAlphaTokenExtractor.cpp Switch test to nextInString now that padded iterator APIs are removed.
src/Storages/MergeTree/TextIndexCache.h Include cleanup/add ColumnString include for dictionary block token extraction.
src/Storages/MergeTree/MergeTreeIndexText.cpp Use forEachToken when building text-index granules.
src/Storages/MergeTree/MergeTreeIndexConditionText.h Minor include cleanup.
src/Storages/MergeTree/MergeTreeIndexConditionText.cpp Include reordering/adjustment after tokenizer refactor.
src/Storages/MergeTree/MergeTreeIndexBloomFilterText.cpp Replace per-tokenizer padded bloom-filter path with forEachTokenToBloomFilter.
src/Interpreters/ITokenizer.h Remove padded iterator APIs; add forEachToken dispatcher and SIMD callback implementation for SplitByNonAlpha.
src/Interpreters/ITokenizer.cpp Remove nextInStringPadded implementation; add forEachTokenToBloomFilter definition.
src/Functions/tokens.cpp Switch token extraction to forEachToken.
src/Functions/ngrams.cpp Switch ngram extraction loop to forEachToken.
src/Functions/hasAnyAllTokens.h Replace ITokenizer include with forward declaration (reduce coupling).
src/Functions/hasAnyAllTokens.cpp Switch token scanning to forEachToken.

Comment thread src/Interpreters/ITokenizer.h Resolved
@george-larionov

george-larionov commented Feb 27, 2026 •

Copy link
Copy Markdown
Member

I'm not sure why this test only fails on this PR, since it seems to have nothing to do with these changes.

Let's see if it is still there when we re-run the tests.

amosbird added 2 commits March 2, 2026 14:49
…ckHouse into refactor-tokenizer-includes

# Conflicts:
#	src/Functions/hasAnyAllTokens.cpp

@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.

All tests passed! LGTM.

@george-larionov
george-larionov added this pull request to the merge queue Mar 2, 2026
@amosbird

amosbird commented Mar 2, 2026

Copy link
Copy Markdown
Collaborator Author
copied-2026-03-02-22_50_09_210

Merged via the queue into ClickHouse:master with commit 6dbb5e6 Mar 2, 2026
145 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 2, 2026
const __m128i haystack = _mm_loadu_si128(reinterpret_cast<const __m128i *>(pos));
const size_t haystack_length = 16;

#if defined(__SSE4_2__)

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.

We'll soon move to x86-v3: #90043

I think we can simplify the ifdefs here a bit,. i.e. assume SSE4.2 & remove the SSE 2 path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants