Skip to content

Text index: fix wrong result when a LIKE pattern contains an invalid backslash escape - #115634

Merged
ahmadov merged 4 commits into
masterfrom
ahmadov/text-index-like-escape
Aug 24, 2026
Merged

ahmadov merged 4 commits into
masterfrom
ahmadov/text-index-like-escape

Conversation

@ahmadov

@ahmadov ahmadov commented Aug 20, 2026 •

Copy link
Copy Markdown
Member

The index-side tokenizers drop a backslash which does not form a valid LIKE escape sequence,
while row-level matching keeps it as a literal, so the index searched for a token the value
never produced and pruned granules holding matching rows. Index analysis now declines such
patterns and falls back to row-level evaluation.

Fixes #115578.

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 invalid backslash escape characters in text, tokenbf_v1 and ngrambf_v1 indexes.


Workflow [PR]
Sync PR [sync-upstream/pr/115634]

Version info

  • Merged into: 26.9.1.21 (included in 26.9 and later)

…backslash escape

The index-side tokenizers drop a backslash which does not form a valid LIKE escape sequence,
while row-level matching keeps it as a literal, so the index searched for a token the value
never produced and pruned granules holding matching rows. Index analysis now declines such
patterns and falls back to row-level evaluation.

Fixes #115578.
@clickhouse-gh

clickhouse-gh Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [35ce6ac]

Summary: ✅


AI Review

Summary

This PR stops text, tokenbf_v1, and ngrambf_v1 skip-index analysis from pruning granules for default-backslash LIKE patterns that the row-level matcher either treats differently (unknown escapes such as \b) or rejects at execution time (a trailing backslash). I did not find remaining correctness gaps in the current code or tests; the open thread about custom ESCAPE characters does not apply here because these skip-index analyzers only accept the 2-argument LIKE forms.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.00% 87.00% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 33/33 (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 Aug 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 35ce6ac65 with master 4399d6c6a (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of be87a5604; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 708.92 MiB 705.88 MiB -3.04 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

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

Job report

Comment thread src/Common/likePatternToRegexp.h Resolved

/// `nextInStringLike` tokenizes such a pattern differently than row-level matching and could prune a
/// granule holding matching rows. No tokens means "cannot prune", as for a pattern like `LIKE '%a%'`.
if (likePatternHasUnknownBackslashEscape(pattern))

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.

Function likePatternHasUnknownBackslashEscape was added in this PR. I wonder how the scan path determines that a pattern is invalid? Is there some existing function that we can call?

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.

unfortunately, there was no an existing functionality for that. I double -checked again, but couldn't find it.

return res;
}

bool likePatternHasUnknownBackslashEscape(std::string_view pattern)

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 won't work with custom escape symbols. Will that be a problem?

@ahmadov ahmadov Aug 24, 2026 •

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.

no, it shouldn't be a problem now. I'll handle that with #105848.

@rschu1ze rschu1ze self-assigned this Aug 20, 2026
@ahmadov
ahmadov added this pull request to the merge queue Aug 24, 2026
Merged via the queue into master with commit a15a242 Aug 24, 2026
178 checks passed
@ahmadov
ahmadov deleted the ahmadov/text-index-like-escape branch August 24, 2026 14:21
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 24, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Aug 24, 2026
The unknown-backslash-escape correctness fix this PR used to carry landed
separately in ClickHouse#115634, which is now on master: the guard sits in
MergeTreeIndexConditionText::stringLikeToTokens and in
MergeTreeConditionBloomFilterText::traverseTreeEquals, and 02346 covers it.

Drop the plain two-argument backslash arms from 04292 and 04351 so this PR's
tests only assert the ESCAPE handling it adds. The folded-pattern arms stay:
they exercise the 3-argument fold, which is this PR's code.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@telperions

Copy link
Copy Markdown

Hello @ahmadov, is it possible to backport it to 26.8 lts?
Thank you!

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.

Text index: wrong results for LIKE patterns containing a backslash that is not a valid escape

4 participants