Skip to content

Text Search: Consistent behavior for short ngrams and empty tokens - #89757

Merged
rschu1ze merged 16 commits into
masterfrom
consistent-short-ngrams
Nov 21, 2025
Merged

rschu1ze merged 16 commits into
masterfrom
consistent-short-ngrams

Conversation

@george-larionov

@george-larionov george-larionov commented Nov 8, 2025 •

Copy link
Copy Markdown
Member
  1. Making ngram tokenizer behavior consistent, it will now not tokenize ngrams shorter than N
  2. Text search will return no rows when search tokens are empty. (Consistent with other implementations)

Changelog category (leave one):

  • Backward Incompatible Change

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

Ngram tokenizer will no longer return ngrams less than the length N it was set with. Text Search will return no rows when search tokens are empty.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Details

Fixing inconsistent behavior of ngram tokenizer, now it will not return ngrams less than the ngram size N it was set with in any scenario.

@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [47f5d6e]

Summary: ❌

job_name test_name status info comment
Build (amd_compat) failure
Cmake configuration failure cidb
Stateless tests (arm_asan, targeted) failure
01459_manual_write_to_replicas_quorum_detach_attach FAIL cidb, flaky
Stateless tests (amd_binary, ParallelReplicas, s3 storage, parallel) failure
02494_query_cache_ignore_output_settings FAIL cidb
BuzzHouse (amd_debug) failure
Logical error: 'Inconsistent AST formatting: the query: FAIL cidb
BuzzHouse (arm_asan) failure
DB::Exception: Found disallowed error code FAIL cidb
BuzzHouse (amd_tsan) failure
Logical error: 'false && "WriteBuffer is neither finalized nor canceled in destructor."'. FAIL cidb
BuzzHouse (amd_ubsan) failure
/home/ubuntu/actions-runner/_work/ClickHouse/ClickHouse/src/IO/VarInt.h:32:5: runtime error: store to null pointer of type 'char' FAIL cidb

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Nov 8, 2025
@george-larionov
george-larionov marked this pull request as ready for review November 14, 2025 22:43
@george-larionov george-larionov changed the title Making ngram tokenizer consistent on short ngrams Text Search: Consistent behavior with short ngrams and empty tokens Nov 14, 2025
@george-larionov george-larionov changed the title Text Search: Consistent behavior with short ngrams and empty tokens Text Search: Consistent behavior for short ngrams and empty tokens Nov 14, 2025
@rschu1ze rschu1ze self-assigned this Nov 16, 2025

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

That empty token arrays now match nothing (previously: everythig) is technically a backward-incompatible change ... could you add the corresponding label to the PR? (no need to add special compatibility code ... the functions are still experimental).

Comment thread src/Functions/tokens.cpp Resolved

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 pull request makes the ngram tokenizer behavior consistent by ensuring it does not tokenize strings shorter than the configured ngram size N. Additionally, text search functions now return no rows when search tokens are empty, aligning with other implementations.

  • Ngram tokenizer now correctly skips strings shorter than N instead of returning them as-is
  • Empty search tokens now result in no matches instead of matching all rows
  • Documentation updated to consistently use "ngrams" (plural) terminology

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/queries/0_stateless/02346_text_index_short_ngrams_and_empty_tokens.sh New comprehensive test for short ngrams and empty tokens with ngram(4) tokenizer
tests/queries/0_stateless/02346_text_index_short_ngrams_and_empty_tokens.reference Expected test output showing no results for short ngrams and empty tokens
tests/queries/0_stateless/02346_text_index_function_hasAnyAllTokens.sql Updated tests to reflect new behavior where empty tokens return no matches
tests/queries/0_stateless/02346_text_index_function_hasAnyAllTokens.reference Updated expected results showing empty arrays for short ngram searches
tests/queries/0_stateless/02346_text_index_bug86300.sql Removed test for old behavior where empty array matched all rows
tests/queries/0_stateless/02346_text_index_bug86300.reference Removed reference file for deleted test
src/Storages/MergeTree/MergeTreeIndexText.cpp Fixed hasAnyTokenFromQuery and hasAllTokensFromQuery to return false for empty tokens
src/Interpreters/ITokenExtractor.h Removed callback for strings shorter than ngram size N, ensuring no tokens are generated
src/Functions/tokens.cpp Documentation fixes changing "ngram" to "ngrams" for consistency
src/Functions/hasAnyAllTokens.cpp Removed early return for empty tokens that was causing all rows to match

Comment thread src/Functions/hasAnyAllTokens.cpp Resolved
Comment thread src/Functions/hasAnyAllTokens.cpp Resolved
@george-larionov george-larionov added the pr-backward-incompatible Pull request with backwards incompatible changes label Nov 17, 2025
@clickhouse-gh clickhouse-gh Bot removed the pr-backward-incompatible Pull request with backwards incompatible changes label Nov 17, 2025

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

LGTM

@george-larionov
george-larionov marked this pull request as draft November 19, 2025 03:55
@george-larionov

george-larionov commented Nov 19, 2025 •

Copy link
Copy Markdown
Member Author

It seems that some functions such as startsWith, endsWith, and match were relying on the hasAllTokensFromQuery function returning true when no false matches were found. So currently my proposed changes in this PR break these functions (unless we don't use the text index optimization for them). I will investigate a bit more to see if I can resolve the issue and mark the PR ready for review again once I do.

Update: I ended up adding a hasAllQueryTokensOrEmpty function so that some functions such as startWith, endsWith, etc can keep using the old code path. This is ok because they are different than the normal text search functions and cannot return all rows for an empty query in any case.

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

Overall LGTM

(2, 'def', ['def', 'abc'], {'abc':'foo', 'def':'foo'});
EOF

for direct_read_setting in 0 1; do

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.

@rschu1ze this scripting approach to test results with and without direct read is something I am also facing.

Could we avoid this and somehow rely on direct_read_setting randomization? That way this test will be a simple .sql

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.

Yes, we can rely on randomization. Just add "query_plan_direct_read_from_text_index": lambda: random.randint(0, 1) to SettingsRandomizer in tests/clickhouse-test.

I like such randomization but please let's do it in a follow-up PR - it may break unrelated tests.

@george-larionov george-larionov Nov 21, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I would add a vote against the randomization approach. If this test fails due to the setting in this script, it will be immediately clear what went wrong, but when tests fail due to the (already) randomized settings in the test-runner, it is much harder to narrow it down because you have to sift through a block of settings instead of just one.

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.

Fair enough, I don't have a strong opinion.

(For context, there are also settings which are intentionally not randomized because they affect the timing of internal operations in subtle ways and they would break too many tests. So not randomizing settings is fine.)

@Ergus Ergus added the pr-backward-incompatible Pull request with backwards incompatible changes label Nov 20, 2025
@clickhouse-gh clickhouse-gh Bot removed the pr-backward-incompatible Pull request with backwards incompatible changes label Nov 21, 2025
@rschu1ze

Copy link
Copy Markdown
Member

Stateless tests (amd_binary, ParallelReplicas, s3 storage, parallel

Stateless tests (arm_asan, targeted):

BuzzHouse:

  • ... is generally in a bad shape at the moment and the failures are not related

@rschu1ze
rschu1ze enabled auto-merge November 21, 2025 10:20
@rschu1ze

Copy link
Copy Markdown
Member

02346_text_index_function_hasAnyAllTokens fails in the flaky check in the private repository with

2025-11-21 17:21:07 Reason: Test runs too long (> 180s). Make it faster. 

We'll fix that separately.

@rschu1ze
rschu1ze added this pull request to the merge queue Nov 21, 2025
@rschu1ze

Copy link
Copy Markdown
Member

We'll fix that separately.

#90561

Merged via the queue into master with commit 30662c3 Nov 21, 2025
@rschu1ze
rschu1ze deleted the consistent-short-ngrams branch November 21, 2025 10:41
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 21, 2025
Ergus added a commit to Ergus/ClickHouse that referenced this pull request Apr 13, 2026
Require compatibility with ClickHouse#89757 See comment next to code
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 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.

6 participants