Repository navigation
Description
Is there an existing issue for the same bug?
- I have checked the existing issues. (searched for LIKE / backslash / escape; fix: support explicit LIKE ESCAPE semantics #26429 and [Bug]: LIKE underscore wildcard does not match one multibyte utf8mb4 character #27389 are related but different)
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 MySQL8.0.46(mysql:8.0docker 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: 1The 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
_, sobytes.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
fix(sql): honor escape parity in LIKE fast paths (#29976)
slyunnn commentedon Oct 14, 2026
Regression and extended verification for #29951 (2026-10-14)
Verification Environment
origin/main:fcad62a8ff06000e682824802f1155cbeea02a2f—fix(frontend): accept parenthesized ALTER ADD COLUMN lists (#29963). The running Proxy'smo-service -versionreported this exactmaincommit after restart.f02abe398315871723a8ea913a94c7790f4e9160—fix(sql): honor escape parity in LIKE fast paths. This commit is included in the verifiedmain.2 CN / 1 TN / 1 Log service / Proxy, all using the same exact-main binary. Connected endpoints: Proxy127.0.0.1:6001(container SQL port6009), CN1127.0.0.1:16001, and CN2127.0.0.1:16002.database/sqlwithgo-sql-driver/mysql,interpolateParams=false, and wire-command assertions for actualCOM_STMT_PREPARE/COM_STMT_EXECUTE. MySQL8.4.11was used as the compatibility oracle during recording and comparison.What Was Covered
LIKEexpressions and itsUNHEX-based SQLPREPARE/EXECUTEon each of Proxy, CN1, and CN2. Each endpoint returned1, 1, 1, 1, 1, 0, 1; MySQL8.4.11returned the same values.test/distributed/cases/operator/like_escape_parity.sqlon 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.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 reportedtotal=1 success=1 failed=0 skipped=0.-j 1because there is only one case. No MatrixOne engine-parallel claim is inferred from MOTR scheduling.mo-testergolden/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 MySQL8.4.11.Large-data regression is not needed: the defect is in scalar
LIKEpattern 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
test/distributed/cases/operator/like_escape_parity.sqland its golden result. Its SQL output was independently replayed and value-compared on Proxy, CN1, and CN2.script/14_issue_regression/issue_29951_like_escape_parity_protocol.go, launcherscript/14_issue_regression/issue_29951_like_escape_parity_protocol.sh, and recorded outputscript/14_issue_regression/out/issue_29951_like_escape_parity_protocol.out. No duplicate SQL.test/.resultwas added because the merged BVT already covers that layer; the MOTR addition covers the previously missing wire-protocol boundary.test/issue-29951-like-escape-paritywas pushed tomatrixorigin/motr; source commit9faa4541b819f23b54d4ac3fa05dda90e9dcf841(test: add MOTR regression for #29951). The remote branch was verified to contain this exact commit. No issue-specific PR was created.test/daily-2026-10-14-regressionsas cherry-pick commit0260237c960e86d8a8c80021ef6dc5d1e9e3a4b6; 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.