Skip to content

[Bug]: LIKE ignores an escaped backslash immediately before a trailing % wildcard #29951

Closed

Description

@loveRhythm1990

Is there an existing issue for the same bug?

Branch Name

main (source below is from main; reproduced on the v4.2.5 and v4.1.2 release binaries)

Commit ID

f79c6cdfad0ad89ceace6a0bdd568c03b9a295c3 (tag v4.2.5), docker image matrixorigin/matrixone@sha256:4455f6904a0cca5bb0f876d3026c407755216f91a4413a623c028495e0bba20f

Other Environment Information

  • Hardware parameters: single-node docker container, Apple M-series (arm64), 8 vCPU
  • OS type: macOS 25.6.0 host, container matrixorigin/matrixone:latest (8.0.30-MatrixOne-v4.2.5)
  • Others: also reproduced on matrixorigin/matrixone:4.1.2; compared against MySQL 8.0.46 (mysql:8.0 docker image)

Actual Behavior

When a LIKE pattern contains an escaped backslash (\\) immediately before a trailing % wildcard, the pattern never matches. MatrixOne returns 0 where MySQL returns 1.

select 'path ab\\ here' like '%ab\\\\%';
-- MatrixOne: 0
-- MySQL:     1

The pattern %ab\\% decodes to: % wildcard, literal a, literal b, literal \ (escaped), % wildcard — so it should match any string containing ab\.

Expected Behavior

Same result as MySQL: 1. An escaped backslash is a literal backslash, and the % that follows it is still a wildcard.

Steps to Reproduce

-- the two failing cases
select 'path ab\\ here'   like '%ab\\\\%'     as case1;  -- MO: 0, MySQL: 1
select 'path ab\\\\ here' like '%ab\\\\\\\\%' as case2;  -- MO: 0, MySQL: 1

-- controls that already behave correctly
select 'path ab\\c here'  like '%ab\\\\c%'    as case3;  -- MO: 1, MySQL: 1
select 'discount 50%'     like '%50\\%'       as case4;  -- MO: 1, MySQL: 1
select 'path a_b\\ here'  like '%a\\_b\\\\%'  as case5;  -- MO: 1, MySQL: 1
select 'path abX here'    like '%ab\\\\%'     as case6;  -- MO: 0, MySQL: 0
# pattern (after SQL literal decoding) value MySQL 8.0.46 MO 4.2.5 MO 4.1.2
1 %ab\\% path ab\ here 1 0 0
2 %ab\\\\% path ab\\ here 1 0 0
3 %ab\\c% path ab\c here 1 1 1
4 %50\% discount 50% 1 1 1
5 %a\_b\\% path a_b\ here 1 1 1
6 %ab\\% path abX here 0 0 0

It reproduces identically through a prepared statement, which is how an application hits it:

prepare s from 'select ? like ?';
set @c = unhex('706174682061625C2068657265');  -- 'path ab\ here'
set @p = unhex('2561625C5C25');                -- '%ab\\%'
execute s using @c, @p;                        -- MO: 0, MySQL: 1

(hex is used only to pin the exact bytes past two layers of literal escaping; select hex(@c), hex(@p) confirms …61625C20… and 2561625C5C25.)

Additional information

Root cause looks like the fast-path heuristic in pkg/sql/plan/function/func_builtin_regexp.go (optimizeRuleForLike, lines 803-809 on main):

// opt rule #3: [_%]somethingInBetween[_%]
if n > 1 {
    c0, c1 := pat[0], pat[n-1]
    if !bytes.ContainsAny(pat[1:len(pat)-1], "_%") {
        if n > 2 && pat[n-2] == DefaultEscapeChar {
            c1 = DefaultEscapeChar
        }

That override is correct for a pattern ending in an escaped % (case 4, %50\%, where the trailing % really is a literal and the suffix fast path is right). But it only inspects the single byte at pat[n-2] and does not count the parity of the trailing backslash run: in %ab\\% that backslash is itself escaped by the one before it, so the final % is still a wildcard. With c1 forced to \, the switch falls into case c0 == '%' && !(c1 == '%' || c1 == '_') — Rule 4.3, suffix match — and evaluates bytes.HasSuffix(value, RemoveEscapeChar("ab\\%")) = HasSuffix(value, "ab\%"), i.e. "ends with ab\%" instead of "contains ab\".

Two observations that support this reading:

  • The tokenizer itself (nextByteLikeToken) handles \\ correctly, so the compiled matcher gives the right answer — the problem is only the fast path in front of it.
  • Case 5 passes because the pattern's middle contains a raw _, so bytes.ContainsAny(pat[1:n-1], "_%") is true, opt rule Add PR template file #3 is skipped entirely, and the correct path runs. That is exactly what the code above predicts.

So the trigger is: the pattern's inner part contains no bare _/%, and the pattern ends with an even-length run of backslashes followed by %.

A fix would be to count the trailing backslash run and only treat the final %/_ as a literal when that run has odd length (and to apply the same parity rule wherever pat[n-2] is inspected).

Found while reviewing a purge ... WHERE content LIKE ? code path in an application that escapes %, _ and \ with \ to get literal substring matching: a topic ending in a backslash silently matched nothing. Not urgent for us — the failure direction is a non-match, so nothing is over-deleted — but the deviation from MySQL is worth fixing.

Activity

  1. added
    kind/bug
    severity/s2
    on Oct 12, 2026
  2. assigned and unassigned on Oct 12, 2026
  3. slyunnn commented on Oct 14, 2026

    @slyunnn

    Regression and extended verification for #29951 (2026-10-14)

    Verification Environment

    • MatrixOne origin/main: fcad62a8ff06000e682824802f1155cbeea02a2f — fix(frontend): accept parenthesized ALTER ADD COLUMN lists (#29963). The running Proxy's mo-service -version reported this exact main commit after restart.
    • Fix: PR #29976, merge commit f02abe398315871723a8ea913a94c7790f4e9160 — fix(sql): honor escape parity in LIKE fast paths. This commit is included in the verified main.
    • Isolated local topology: 2 CN / 1 TN / 1 Log service / Proxy, all using the same exact-main binary. Connected endpoints: Proxy 127.0.0.1:6001 (container SQL port 6009), CN1 127.0.0.1:16001, and CN2 127.0.0.1:16002.
    • Clients: MySQL command-line client over the MySQL text protocol for the original SQL and existing BVT; Go database/sql with go-sql-driver/mysql, interpolateParams=false, and wire-command assertions for actual COM_STMT_PREPARE / COM_STMT_EXECUTE. MySQL 8.4.11 was used as the compatibility oracle during recording and comparison.

    What Was Covered

    • OK — Replayed the issue's original six LIKE expressions and its UNHEX-based SQL PREPARE / EXECUTE on each of Proxy, CN1, and CN2. Each endpoint returned 1, 1, 1, 1, 1, 0, 1; MySQL 8.4.11 returned the same values.
    • OK — Replayed every statement in the merged BVT test/distributed/cases/operator/like_escape_parity.sql on each endpoint and diffed the result rows against the expected values. Coverage includes even/odd backslash runs, escaped % and _, positive and negative matches, ILIKE, NOT LIKE, short patterns, Unicode, and SQL prepared-statement reuse. All three diffs were empty.
    • OK — Recorded a new MOTR protocol case against MySQL 8.4.11, then replayed it successfully against MySQL and each MatrixOne endpoint. It checks textual bound parameters, real binary prepared commands, reuse, a negative control, NOT LIKE, NULL, and post-query session health. Every MOTR replay reported total=1 success=1 failed=0 skipped=0.
    • OK — Confirmed the runtime remained responsive after the original and extended SQL. The three endpoint validations were independent; the single MOTR case is eligible for the runner's parallel phase but used -j 1 because there is only one case. No MatrixOne engine-parallel claim is inferred from MOTR scheduling.
    • Not covered — A strict mo-tester golden/metadata run of the existing BVT was not performed here; the BVT statements and result values were executed and compared through the real MySQL protocol. No production/TKE deployment was used.

    Findings

    The reported trailing-wildcard escape-parity failure did not recur on the exact latest main. The repaired fast path distinguishes an escaped backslash followed by wildcard % from an escaped wildcard itself; the negative controls still reject incorrect matches. The actual binary prepared-statement route agrees with the SQL route and MySQL 8.4.11.

    Large-data regression is not needed: the defect is in scalar LIKE pattern interpretation, not a row-count, batch/block, spill, concurrency, data-skew, network-transfer, or resource-size boundary. Increasing data volume would add CI cost without exercising a new relevant boundary.

    MOTR / BVT Coverage

    • Existing MatrixOne BVT from PR fix(sql): honor escape parity in LIKE fast paths #29976: test/distributed/cases/operator/like_escape_parity.sql and its golden result. Its SQL output was independently replayed and value-compared on Proxy, CN1, and CN2.
    • New MOTR protocol-only case: script/14_issue_regression/issue_29951_like_escape_parity_protocol.go, launcher script/14_issue_regression/issue_29951_like_escape_parity_protocol.sh, and recorded output script/14_issue_regression/out/issue_29951_like_escape_parity_protocol.out. No duplicate SQL .test / .result was added because the merged BVT already covers that layer; the MOTR addition covers the previously missing wire-protocol boundary.
    • MOTR branch test/issue-29951-like-escape-parity was pushed to matrixorigin/motr; source commit 9faa4541b819f23b54d4ac3fa05dda90e9dcf841 (test: add MOTR regression for #29951). The remote branch was verified to contain this exact commit. No issue-specific PR was created.
    • Daily summary MOTR PR #238 includes the verified case on test/daily-2026-10-14-regressions as cherry-pick commit 0260237c960e86d8a8c80021ef6dc5d1e9e3a4b6; its tree is identical to the independent source commit.

    Final Conclusion

    Fixed on the verified latest MatrixOne main, across Proxy, CN1, and CN2 for the original SQL, extended BVT values, and real binary prepared-statement protocol. Large-data regression is not needed for this scalar pattern-parsing issue.

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s2

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions