Repository navigation
Text Search: Consistent behavior for short ngrams and empty tokens - #89757
Conversation
|
Workflow [PR], commit [47f5d6e] Summary: ❌
|
c984917 to
7e8213b
Compare
rschu1ze
left a comment
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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 |
7e8213b to
9273b3a
Compare
9273b3a to
d7266bf
Compare
be5b51a to
4d228ba
Compare
|
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. |
4d228ba to
342845e
Compare
| (2, 'def', ['def', 'abc'], {'abc':'foo', 'def':'foo'}); | ||
| EOF | ||
|
|
||
| for direct_read_setting in 0 1; do |
There was a problem hiding this comment.
@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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.)
342845e to
47f5d6e
Compare
|
Stateless tests (amd_binary, ParallelReplicas, s3 storage, parallel
Stateless tests (arm_asan, targeted):
BuzzHouse:
|
|
02346_text_index_function_hasAnyAllTokens fails in the flaky check in the private repository with We'll fix that separately. |
|
Require compatibility with ClickHouse#89757 See comment next to code
Changelog category (leave one):
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
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.