Repository navigation
Restart the ngram walk of a LIKE pattern on a lexical unit - #118673
Conversation
An `ngrambf_v1` index silently dropped granules holding matching rows for a `LIKE` pattern with a run of escaped backslashes before a wildcard: ```sql SELECT count() FROM t WHERE s LIKE '%ab\\\\%cd%' SETTINGS use_skip_indexes = 0; -- 1 SELECT count() FROM t WHERE s LIKE '%ab\\\\%cd%'; -- 0 ``` After emitting an n-gram, `NgramsTokenizer::nextInStringLike` restarted one code point later, blindly. With a run of escaped backslashes the restart lands on the second byte of a `\\` pair; the re-parse from there pairs the remaining backslashes differently, reaches the pattern's genuine `%` while in the escaped state and consumes it as a literal percent. The required n-gram it emits then contains `%` - a substring no matching value has to contain - so the granule's bloom filter does not hold it and the granule is pruned. The restart now advances by one lexical unit of the pattern, where a backslash and the character it escapes are one unit. With an n-gram size of 1 a single literal backslash before the wildcard was already enough to lose rows. Closes: #117858 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [5bde148] Summary: ✅
AI ReviewSummaryThis PR fixes Missing context / blind spots
Tests
Final VerdictChanges requested. LLVM Coverage ReportMeasured on commit 5bde148.
Changed lines: Changed C/C++ lines covered: 8/8 (100.00%) · Uncovered code |
| SELECT count() FROM t_ngram_backslash WHERE s LIKE '%ab\\\\\\\\%cd%' SETTINGS use_skip_indexes = 0; | ||
| SELECT count() FROM t_ngram_backslash WHERE s LIKE '%ab\\\\\\\\%cd%'; |
There was a problem hiding this comment.
These two count() checks are weaker than the PR contract: they only prove result equality, not that the ngrambf_v1 index path is still exercised. The same reference would pass if the planner simply stopped using the index for backslash runs and fell back to a full scan.
Because the bug was in shared NgramsTokenizer::nextInStringLike, please tighten this by either forcing/observing index usage here (force_data_skipping_indices = 'i' or EXPLAIN indexes = 1) and add the same reproducer for text(tokenizer = ngrams), which reaches the same helper through MergeTreeIndexConditionText::stringLikeToTokens.
There was a problem hiding this comment.
The test is tighter than before, but the original gap is still present in the current file.
tests/queries/0_stateless/05147_ngrambf_like_escaped_backslash_run.sql:12-13 still leaves the original ngrambf_v1(2, ...) reproducer as a plain count comparison, so it would keep passing if the planner stopped using the index for the backslash-run pattern and fell back to a full scan. The new forced query at :31 only proves index usage for the separate n = 1 shape.
The same issue remains for the text-index sibling at :44-46: use_text_index_like_evaluation_by_dictionary_scan = 0 only disables the dictionary-scan branch. It does not force the text(tokenizer = ngrams(2)) index to be used, so that assertion can also still go green via a scan.
Please force or otherwise observe index usage on the original n = 2 reproducer and on the text-index variant as well.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 9 s compile time in total, 7 of them have a recent master baseline. |
…izer Comparing the result with `use_skip_indexes = 0` would also pass if the planner stopped using the index for these patterns, so the same query is asserted with `force_data_skipping_indices`. The tokenizer also answers the `LIKE` fallback of a text index with the `ngrams` tokenizer, which is covered now with the dictionary-scan evaluation turned off. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Addressed: the same query is now asserted with |
|
Hi — this looks like a backport candidate for |
Backport #118673 to 26.8: Restart the ngram walk of a `LIKE` pattern on a lexical unit
Backport #118673 to 26.6: Restart the ngram walk of a `LIKE` pattern on a lexical unit
Backport #118673 to 26.7: Restart the ngram walk of a `LIKE` pattern on a lexical unit
Closes: #117858
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix rows silently dropped by an
ngrambf_v1index for aLIKEpattern with a run of escaped backslashes before a wildcard.Description
After emitting an n-gram,
NgramsTokenizer::nextInStringLikerestarted one code point later, blindly. With a run of escaped backslashes the restart lands on the second byte of a\\pair; the re-parse from there pairs the remaining backslashes differently, reaches the pattern's genuine%while in the escaped state and consumes it as a literal percent. The required n-gram it emits contains%- a substring no matching value has to contain - so the bloom filter of the granule does not hold it and the granule is pruned.The restart now advances by one lexical unit of the pattern, where a backslash and the character it escapes are one unit. With an n-gram size of 1 a single literal backslash before the wildcard was already enough to lose rows; both shapes are covered by the new test.
Distinct from #51154, which is about the escape semantics assigned to unknown escapes and fires with a single backslash before an ordinary character.
Workflow [PR]
Sync PR [sync-upstream/pr/118673]
Version info
26.9.1.993(included in26.9and later)26.8.4.13,26.7.8.17,26.6.7.4