Skip to content

Restart the ngram walk of a LIKE pattern on a lexical unit - #118673

Merged
alexey-milovidov merged 3 commits into
masterfrom
ngrambf-like-backslash-run-117858
Sep 8, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
ngrambf-like-backslash-run-117858

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

Closes: #117858

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 rows silently dropped by an ngrambf_v1 index for a LIKE pattern with a run of escaped backslashes before a wildcard.

Description

CREATE TABLE t (s String, INDEX i s TYPE ngrambf_v1(2, 512, 2, 0) GRANULARITY 1)
ENGINE = MergeTree ORDER BY tuple() SETTINGS index_granularity = 2;
INSERT INTO t VALUES ('xxab\\\\cdyy'), ('other1'), ('other2'), ('other3');

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

  • Merged into: 26.9.1.993 (included in 26.9 and later)
  • Backported to: 26.8.4.13, 26.7.8.17, 26.6.7.4

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]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5bde148]

Summary: ✅


AI Review

Summary

This PR fixes NgramsTokenizer::nextInStringLike so an escaped backslash pair is treated as one lexical unit when generating overlapping n-grams for LIKE patterns, which is the right direction for the silent-pruning bug in ngrambf_v1 and the shared text(tokenizer = ngrams) path. I am still requesting changes because the updated regression test does not yet prove that the original n = 2 reproducer or the text-index sibling are actually exercising the index path.

Missing context / blind spots
  • ⚠️ Praktika only had the Build profile diff report available at review time, so I could not cross-check functional CI coverage for the new stateless test.
Tests
  • ⚠️ tests/queries/0_stateless/05147_ngrambf_like_escaped_backslash_run.sql:12-13,44-46 are still result-only assertions. If the planner stops using the index for %ab\\\\\\\\%cd%, both pairs can still pass via a full scan; use_text_index_like_evaluation_by_dictionary_scan = 0 disables the dictionary-scan branch, but it does not force the text index to be used. Please add force_data_skipping_indices = 'i' or an EXPLAIN indexes = 1 assertion to the original ngrambf_v1(2, ...) reproducer and to the text(tokenizer = ngrams(2)) sibling.
Final Verdict

Changes requested.

LLVM Coverage Report

Measured on commit 5bde148.

Metric Baseline Current Δ
Lines 88.80% 88.80% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.20% 81.10% -0.10%

Changed lines: Changed C/C++ lines covered: 8/8 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 7, 2026
Comment on lines +12 to +13
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%';

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.

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.

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.

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.

@clickhouse-gh clickhouse-gh Bot added the comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... label Sep 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 5bde1489d with master 6bdba314e (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 9 s compile time in total, 7 of them have a recent master baseline.

Job report

…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]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Addressed: the same query is now asserted with force_data_skipping_indices = 'i', so the test fails if the planner stops using the index instead of silently falling back to a scan. A text(tokenizer = ngrams(2)) reproducer with use_text_index_like_evaluation_by_dictionary_scan = 0 covers the sibling surface that shares nextInStringLike.

@alexey-milovidov alexey-milovidov left a comment

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.

Nice.

@alexey-milovidov alexey-milovidov self-assigned this Sep 8, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 8, 2026
Merged via the queue into master with commit 53a0583 Sep 8, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the ngrambf-like-backslash-run-117858 branch September 8, 2026 19:32
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 8, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this looks like a backport candidate for 26.6, 26.7, 26.8. I ran this PR's own regression test against each branch's binary and the bug reproduces there (the fix isn't present yet).

@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 14, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 14, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 14, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 14, 2026
Backport #118673 to 26.8: Restart the ngram walk of a `LIKE` pattern on a lexical unit
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 14, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 14, 2026
Backport #118673 to 26.6: Restart the ngram walk of a `LIKE` pattern on a lexical unit
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 14, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 14, 2026
Backport #118673 to 26.7: Restart the ngram walk of a `LIKE` pattern on a lexical unit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.6-must-backport

Projects

None yet

6 participants