Skip to content

executor: Fix LIKE with non-ASCII patterns on unsigned-char platforms - #11038

Merged
ti-chi-bot[bot] merged 2 commits into
pingcap:masterfrom
ChangRui-Ryan:changrui_fix_like
Aug 14, 2026
Merged

executor: Fix LIKE with non-ASCII patterns on unsigned-char platforms#11038
ti-chi-bot[bot] merged 2 commits into
pingcap:masterfrom
ChangRui-Ryan:changrui_fix_like

Conversation

@ChangRui-Ryan

@ChangRui-Ryan ChangRui-Ryan commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: close #11037

Problem Summary

LIKE pattern matching can produce incorrect results for non-ASCII patterns on platforms where char is unsigned, such as Linux AArch64.

The implementation used char values as sentinel states while scanning UTF-8 characters. Bytes greater than 0x7F were interpreted differently when char is unsigned, causing the matcher to incorrectly handle escaped non-ASCII characters and patterns containing UTF-8 literals.

The initial correctness fix avoided the signedness-dependent behavior, but introduced an extra per-character decoding cost in the hot matching path. This could cause a measurable performance regression for common LIKE workloads on AArch64.

What is changed and how it works

  • Replace signedness-sensitive char sentinel handling in the LIKE matcher with an explicit representation that works consistently regardless of whether the compiler treats char as signed or unsigned.
  • Add regression test coverage for non-ASCII UTF-8 patterns, including escaped wildcard cases, to ensure the matcher produces the same result on x86 and AArch64.
  • Preserve the existing optimized ASCII fast path in the matcher. The revised implementation avoids unnecessary UTF-8 decoding for ordinary ASCII pattern matching while applying the correct handling only when non-ASCII bytes are encountered.

As a result, LIKE matching is correct for non-ASCII patterns on unsigned-char platforms, without giving up the substantial performance benefit of the prior optimized implementation.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

None

Summary by CodeRabbit

  • Bug Fixes

    • Improved case-insensitive pattern matching for ASCII text.
    • Correctly handles escaped wildcard characters and consecutive wildcard symbols.
    • Prevents invalid or non-ASCII bytes from causing incorrect matching results.
    • Preserves expected matching behavior when processing Chinese text and mixed wildcard patterns.
  • Tests

    • Added coverage for Chinese text, escaped percent signs, and repeated wildcard patterns.

@ti-chi-bot ti-chi-bot Bot added release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/needs-triage-completed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 12, 2026
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: dcafc3f2-de0f-406e-b2ff-2102d3825313

📥 Commits

Reviewing files that changed from the base of the PR and between 50a5c20 and 7948ea5.

📒 Files selected for processing (1)
  • dbms/src/TiDB/Collation/Collator.cpp
🚧 Files skipped from review as they are similar to previous changes (1)
  • dbms/src/TiDB/Collation/Collator.cpp

📝 Walkthrough

Walkthrough

The change updates ASCII case-insensitive collation pattern compilation and matching. It validates byte values, handles non-ASCII data safely, preserves wildcard types, and collapses consecutive % wildcards. Tests cover Chinese, escaped-percent, and consecutive-wildcard patterns.

Changes

ASCII CI matching

Layer / File(s) Summary
Byte-safe pattern compilation and matching
dbms/src/TiDB/Collation/Collator.cpp
tryCompileAsciiCi validates bytes, preserves wildcard types, and collapses consecutive unescaped % wildcards. Direct and backtracking matching reject non-ASCII bytes safely.
Collation wildcard regression tests
dbms/src/TiDB/tests/gtest_tidb_collator.cpp
Tests cover %报告%, escaped percent signs, consecutive percent wildcards, and matching outcomes across collations.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Poem

A rabbit checks each byte with care,
Wildcards keep their proper share.
Chinese patterns now match right,
Escaped signs retain their flight.
Safe bounds guide the collation hare.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address unsigned-byte detection, out-of-bounds prevention, non-ASCII fallback matching, wildcard handling, and related unit-test coverage for issue #11037.
Out of Scope Changes check ✅ Passed The modified collator logic and tests are directly related to correcting non-ASCII LIKE matching and do not introduce unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title clearly identifies the executor LIKE fix for non-ASCII patterns on unsigned-char platforms.
Description check ✅ Passed The description includes the required problem, solution, checklist, side effects, documentation, and release note sections.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
dbms/src/TiDB/Collation/Collator.cpp (1)

137-190: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use UInt8 for byte conversions.

Replace raw unsigned char with UInt8 for unsigned_escape and each converted input byte. This keeps byte-width types consistent with the repository type policy.

As per coding guidelines, “Use explicit width types from dbms/src/Core/Types.h: UInt8, UInt32, Int64, Float64, String.”

Proposed fix
-const auto unsigned_escape = static_cast<unsigned char>(escape);
+const auto unsigned_escape = static_cast<UInt8>(escape);

-auto c = static_cast<unsigned char>(pattern[i]);
+auto c = static_cast<UInt8>(pattern[i]);

-c = static_cast<unsigned char>(pattern[++i]);
+c = static_cast<UInt8>(pattern[++i]);

-const auto c = static_cast<unsigned char>(s[str_idx]);
+const auto c = static_cast<UInt8>(s[str_idx]);

-const auto c = static_cast<unsigned char>(s[str_idx]);
+const auto c = static_cast<UInt8>(s[str_idx]);

Also applies to: 319-325, 360-367

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@dbms/src/TiDB/Collation/Collator.cpp` around lines 137 - 190, In the collator
pattern-processing logic, replace the raw unsigned char types used for
unsigned_escape and converted pattern bytes with UInt8, including the
corresponding conversions in the additional affected sections. Keep the existing
escape handling and ASCII-range validation unchanged.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@dbms/src/TiDB/Collation/Collator.cpp`:
- Around line 137-190: In the collator pattern-processing logic, replace the raw
unsigned char types used for unsigned_escape and converted pattern bytes with
UInt8, including the corresponding conversions in the additional affected
sections. Keep the existing escape handling and ASCII-range validation
unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 45b56cd4-fc84-4379-883c-981bdbbd3806

📥 Commits

Reviewing files that changed from the base of the PR and between 5c72bfa and 50a5c20.

📒 Files selected for processing (2)
  • dbms/src/TiDB/Collation/Collator.cpp
  • dbms/src/TiDB/tests/gtest_tidb_collator.cpp

@ChangRui-Ryan

Copy link
Copy Markdown
Contributor Author

/retest

4 similar comments
@ChangRui-Ryan

Copy link
Copy Markdown
Contributor Author

/retest

@ChangRui-Ryan

Copy link
Copy Markdown
Contributor Author

/retest

@ChangRui-Ryan

Copy link
Copy Markdown
Contributor Author

/retest

@ChangRui-Ryan

Copy link
Copy Markdown
Contributor Author

/retest

@ChangRui-Ryan

Copy link
Copy Markdown
Contributor Author

/retest

@windtalker windtalker left a comment

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.

lgtm

@ti-chi-bot ti-chi-bot Bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Aug 14, 2026

@xzhangxian1008 xzhangxian1008 left a comment

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.

lgtm

@ti-chi-bot

ti-chi-bot Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: windtalker, xzhangxian1008

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot Bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Aug 14, 2026
@ti-chi-bot

ti-chi-bot Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

[LGTM Timeline notifier]

Timeline:

  • 2026-08-14 06:36:26.87493499 +0000 UTC m=+3373972.911030046: ☑️ agreed by windtalker.
  • 2026-08-14 08:12:04.131826037 +0000 UTC m=+3379710.167921083: ☑️ agreed by xzhangxian1008.

@ti-chi-bot
ti-chi-bot Bot merged commit c452de7 into pingcap:master Aug 14, 2026
10 of 11 checks passed
@ti-chi-bot

Copy link
Copy Markdown
Member

In response to a cherrypick label: cannot checkout release-26.3: error checking out release-26.3: exit status 1. output: error: pathspec 'release-26.3' did not match any file(s) known to git

@ti-chi-bot

Copy link
Copy Markdown
Member

In response to a cherrypick label: cannot checkout release-25.10: error checking out release-25.10: exit status 1. output: error: pathspec 'release-25.10' did not match any file(s) known to git

@jy007

jy007 commented Aug 14, 2026

Copy link
Copy Markdown

效率太高了,点赞。

@JaySon-Huang

Copy link
Copy Markdown
Contributor

/cherry-pick release-nextgen-202603

@ti-chi-bot

Copy link
Copy Markdown
Member

@JaySon-Huang: new pull request created to branch release-nextgen-202603: #11047.

Details

In response to this:

/cherry-pick release-nextgen-202603

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved lgtm needs-cherry-pick-release-25.10 needs-cherry-pick-release-26.3 release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Title: LIKE with CI collations silently returns wrong results for non-ASCII patterns on aarch64 builds (introduced by #10400)

6 participants