Skip to content

Fix sparseGram tokenizer bug in hasAny/AllTokens and tokens functions - #89665

Merged
ahmadov merged 5 commits into
masterfrom
ahmadov/has-any-all-tokens-sparse-grams
Nov 11, 2025
Merged

ahmadov merged 5 commits into
masterfrom
ahmadov/has-any-all-tokens-sparse-grams

Conversation

@ahmadov

@ahmadov ahmadov commented Nov 6, 2025 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix hasAnyTokens, hasAllTokens and tokens functions concurrent executions with the sparseGrams tokenizer. Resolves #89605

Details

The bug is only limited to the sparseGram tokenizer. The sparseGram tokenizer stores an internal state which is modified during the execution.

This led to an error when the functions are running concurrently, because the internal state of the sparseGram tokenizer is not thread-safe.

This change fixes that by cloning the sparseGram tokenizer for each function call to avoid sharing an internal state between runs.

Fixes #89605

The bug is only limited to the `sparseGram` tokenizer. The sparseGram
tokenizer stores an internal state which is modified during the execution.

This led an error when the functions are running concurrently, because
the internal state of the sparseGram tokenizer is not thread-safe.

This change fixes that by cloning the sparseGram tokenizer for each
function call to avoid sharing an internal state between runs.
@clickhouse-gh

clickhouse-gh Bot commented Nov 6, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d153132]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_tsan, s3 storage, parallel) failure
00938_fix_rwlock_segfault_long FAIL cidb, flaky
Upgrade check (amd_asan) failure
Killed by signal (in clickhouse-server.log) FAIL cidb
Fatal message in clickhouse-server.log (see fatal_messages.txt) FAIL cidb
Killed by signal (output files) FAIL cidb
Found signal in gdb.log FAIL cidb

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 6, 2025
@EmeraldShift

Copy link
Copy Markdown
Contributor

I don't know much about this implementation of sparseGrams, so I'm curious: why does it need a mutable internal state? Is the copying of that state any concern for memory usage?

@PedroTadim

Copy link
Copy Markdown
Member

Should this PR be backported?

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

It LGTM as a temporary fix while we decide how to fix the underlying issue.

@george-larionov

george-larionov commented Nov 7, 2025 •

Copy link
Copy Markdown
Member

Should this PR be backported?

Surprisingly there seems to be no issue in 25.10 and in 25.9 sparseGrams was not allowed as a tokenizer (i think it didn't exist). So I think no backport needed. @ahmadov ?

@ahmadov

ahmadov commented Nov 7, 2025 •

Copy link
Copy Markdown
Member Author

Should this PR be backported?

Surprisingly there seems to be no issue in 25.10 and in 25.9 sparseGrams was not allowed as a tokenizer (i think it didn't exist). So I think no backport needed. @ahmadov ?

@george-larionov that's interesting, it does not show any result with 25.10, but there should a single match. But I managed to reproduce with the tokens function in 25.10: https://fiddle.clickhouse.com/af12cfb9-a92f-414a-b4bd-0fa8ac5ee965

However, if I set the concurrency limit to 1, it does not produce the error anymore: https://fiddle.clickhouse.com/9495b543-a1b1-470a-a0ca-c6d536e0c7f8

@ahmadov

ahmadov commented Nov 7, 2025 •

Copy link
Copy Markdown
Member Author

Should this PR be backported?

@PedroTadim 25.10 is already released and the full-text index is still an experimental feature. So, I'm not totally sure if we need to patch the release version. Did you have the similar experience with other experimental features?

@ahmadov
ahmadov enabled auto-merge November 10, 2025 13:02
@Ergus Ergus self-assigned this Nov 10, 2025

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

Just added a comment

Comment thread src/Functions/tokens.cpp Resolved
Comment thread src/Functions/tokens.cpp Resolved
@ahmadov

ahmadov commented Nov 11, 2025

Copy link
Copy Markdown
Member Author

Stataless test failed build: #89106

@ahmadov
ahmadov added this pull request to the merge queue Nov 11, 2025
Merged via the queue into master with commit 6003f78 Nov 11, 2025
@ahmadov
ahmadov deleted the ahmadov/has-any-all-tokens-sparse-grams branch November 11, 2025 10:38
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 11, 2025
@rschu1ze

Copy link
Copy Markdown
Member

Thanks for fixing this. The fix copies the internal state once per processed chunk ... which is relatively expensive as SparseGramsImpl is quite large. There are two alternatives:

  • Instantiate hasAny/AllTokens and hasTokens once per query thread (right now, the same function object exists per query). That's a bigger refactoring of the query execution and it would only benefit the few text search functions with tha sparse grams tokenizer (all other SQL functions are stateless, the UUIDv7 functions are the only exception I could find).
  • Add optional locking to SparseGramTokenExtractor. Since this is a low-level function, this would be conceptually quite ugly. It would also mean that tokenization is now single-threaded regardless max_threads.

Long story short, in my view we can keep this PR as is (George mentioned it is a workaround).

@Ergus

Ergus commented Nov 14, 2025

Copy link
Copy Markdown
Member

If it is possible to do it per/thread and still get correct results, the most efficient approach will be to use a static thread local copy. It will be copied only once per thread and the execution will stay lock-free

@rschu1ze

Copy link
Copy Markdown
Member

Hmm, this will probably cause wrong results (not sure). Anyways, let's not overoptimize as long as we don't see sparseGrams as a hotspot in a CPU profile.

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

Labels

pr-bugfix Pull request with bugfix, not backported by default 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.

Segmentation fault with hasAllTokens and sparseGrams without direct read optimizatimization

7 participants