Skip to content

Do not rewrite end-anchored LIKE chains into multiMatchAny - #118766

Merged
alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
Ergus:fix-or-like-chain-end-anchor
Sep 12, 2026
Merged

alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
Ergus:fix-or-like-chain-end-anchor

Conversation

@Ergus

@Ergus Ergus commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

optimize_or_like_chain rewrites an OR chain of LIKE/ILIKE predicates into multiMatchAny, which runs the generated regexps through Vectorscan. There $ follows PCRE semantics and also matches immediately before a final newline, while the original like/ilike uses RE2, where $ means the absolute end of the string.

likePatternToRegexp anchors every pattern that does not end in an unescaped %, so the rewritten predicate matched more rows than the query as written: s LIKE 'tenant-a' also returned 'tenant-a\n'. With the optimization enabled by default this silently widens LIKE filters, including filters that implement row filtering in SQL SECURITY DEFINER views, so a user holding only SELECT on such a view could read rows the view was written to exclude.

Such chains now keep their original branches, exactly as the existing embedded-NUL and non-UTF-8 guards do. Prefix and pure-substring patterns emit no anchor and are still rewritten. One query in tests/performance/optimize_or_like_chain_hits.xml ended with an exact pattern and is adjusted so it keeps exercising the multiMatchAny path.

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

Fixed optimize_or_like_chain returning extra rows for OR chains of LIKE/ILIKE patterns that do not end in %: a value followed by a trailing newline wrongly matched the filter.


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

Version info

  • Merged into: 26.9.1.1312 (included in 26.9 and later)
  • Backported to: 26.8.4.11, 26.7.8.16, 26.6.9.10, 26.3.33.116

@clickhouse-gh

clickhouse-gh Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bd844b3]

Summary: ✅


AI Review

Summary

This PR fixes optimize_or_like_chain so end-anchored LIKE/ILIKE patterns stay on their original OR branches instead of being rewritten to multiMatchAny, avoiding the Vectorscan $-before-final-newline mismatch. The new guard in ConvertOrLikeChainPass, the likePatternToRegexp plumbing, the settings/docs updates, and the added regression coverage are consistent with that contract, and I did not find any new correctness or test gaps in the current diff.

Final Verdict

✅ No findings.

LLVM Coverage Report

Measured on commit bd844b3.

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

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

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). labels Sep 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing bd844b3a4 with master 4fc9c8ae1 (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

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

Job report

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

Ok. However, I'd search for a regexp modifier that makes anchors not match line boundaries, only the whole content boundaries.

@alexey-milovidov alexey-milovidov self-assigned this Sep 12, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 12, 2026
Merged via the queue into ClickHouse:master with commit 13d97bd Sep 12, 2026
174 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 12, 2026
@rschu1ze rschu1ze added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Sep 14, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 14, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 14, 2026
Backport #118766 to 26.8:  Do not rewrite end-anchored LIKE chains into `multiMatchAny`
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 #118766 to 26.7:  Do not rewrite end-anchored LIKE chains into `multiMatchAny`
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Sep 14, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Sep 17, 2026
Cherry pick #118766 to 26.6:  Do not rewrite end-anchored LIKE chains into `multiMatchAny`
robot-clickhouse added a commit that referenced this pull request Sep 17, 2026
PedroTadim added a commit that referenced this pull request Sep 17, 2026
Backport #118766 to 26.6:  Do not rewrite end-anchored LIKE chains into `multiMatchAny`
alexey-milovidov added a commit that referenced this pull request Sep 19, 2026
…into `multiMatchAny`

Resolve the conflicts of the automated cherry-pick. The 26.3 branch carries the
original, much simpler `ConvertOrLikeChainPass` (no `multiSearchAny` path, no
`PatternData`, no per-chain thresholds, and `optimize_or_like_chain` defaults to
`false`), so the master version of the pass, of the `optimize_or_like_chain`
description and the `tests/performance/optimize_or_like_chain_hits.xml` change
do not apply. The fix is adapted to the 26.3 pass instead.

Conflict resolutions:

* `src/Common/likePatternToRegexp.{h,cpp}` merged cleanly and is taken as is:
  `likePatternToRegexp` now reports through an optional `has_end_anchor`
  out-parameter whether it emitted a trailing `$`.
* `src/Analyzer/Passes/ConvertOrLikeChainPass.cpp` keeps the 26.3 pass and adds
  the end-anchor check to it: a `{i}like` branch whose regexp ends with `$` is
  left as the original branch instead of being folded into `multiMatchAny`.
  `multiMatchAny` runs the regexp through Vectorscan, where `$` also matches
  before a final newline, while `like`/`ilike` use RE2, where `$` is the
  absolute end - so the rewrite widened the filter and `LIKE 'a'` also matched
  `'a\n'`. In the 26.3 pass the grouping is per branch rather than per chain, so
  only the anchored branches are kept and the rest of the chain is still
  rewritten; the results are the same either way.
* `src/Analyzer/Passes/ConvertOrLikeChainPass.h` and `src/Core/Settings.cpp`
  keep their 26.3 text plus a note that end-anchored patterns are never
  rewritten.
* `tests/performance/optimize_or_like_chain_hits.xml` does not exist on 26.3;
  the deletion is kept.
* `tests/queries/0_stateless/05055_optimize_or_like_chain_end_anchor.sql` drops
  the `optimize_or_like_chain_min_patterns` setting, which 26.3 does not have,
  and the comment about whole-group rejection is adjusted to the per-branch
  behaviour. The expected results are unchanged.

Original pull-request: #118766

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
robot-ch-test-poll added a commit that referenced this pull request Sep 19, 2026
Cherry pick #118766 to 26.3:  Do not rewrite end-anchored LIKE chains into `multiMatchAny`
robot-clickhouse added a commit that referenced this pull request Sep 19, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 19, 2026
alexey-milovidov added a commit that referenced this pull request Sep 19, 2026
Backport #118766 to 26.3:  Do not rewrite end-anchored LIKE chains into `multiMatchAny`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). 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 Pull request should be backported intentionally. Use this label with great care! 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants