Repository navigation
Description
Is there an existing issue for the same bug?
- I have checked the existing issues.
#13659 concerned a column-parameter error and was closed in 2023. PR #25372
concerns an empty regexp pattern; this report concerns an empty subject with a
nonempty valid pattern. #25299 and #27217 concern binary operand compatibility
and parameter provenance.
Branch Name
main
Commit ID
131ca9c89d1e90f7820a69433bc96edc9f11d0e4
This is the runtime-tested build commit. The current public main head at
submission is 249a458ba567b26d1818fb0496b449135cb05ef2. The relevant checks in
pkg/sql/plan/function/func_builtin_regexp.go are unchanged on the current
public main.
Other Environment Information
- Hardware parameters: local development machine
- OS type: macOS arm64
- Deployment: local standalone MatrixOne, 1 CN / 1 TN / 1 Log service
- Client: MySQL text protocol at 127.0.0.1:16001
- Server version: 8.0.30-MatrixOne-v1.3.0
Actual Behavior
REGEXP_INSTR and REGEXP_SUBSTR reject an empty subject before regexp
matching:
SELECT REGEXP_INSTR('', '^$');
-- ERROR 20301 (HY000): invalid input:
-- regexp_instr: Index out of bounds in regular expression search.
-- Search start position: 1, Search string length: 0
SELECT REGEXP_SUBSTR('', '^$');
-- ERROR 20301 (HY000): invalid input:
-- regexp_substr: Index out of bounds in regular expression search.
-- Search start position: 1, Search string length: 0The equivalent match predicate succeeds:
SELECT REGEXP_LIKE('', '^$');
-- 1Thus REGEXP_LIKE recognizes that ^$ matches the empty string while the
position and substring functions reject the same valid subject and pattern.
Expected Behavior
The empty string is a valid regexp subject. ^$ matches at its beginning, so
REGEXP_INSTR('', '^$') should return position 1 and
REGEXP_SUBSTR('', '^$') should return the empty match rather than an
out-of-bounds error.
More generally, MySQL documents that REGEXP_INSTR returns 0 when there is
no match and REGEXP_SUBSTR returns NULL when there is no match; an empty
subject is not an invalid input.
References:
- https://dev.mysql.com/doc/refman/8.4/en/regexp.html#function_regexp-instr
- https://dev.mysql.com/doc/refman/8.4/en/regexp.html#function_regexp-substr
Steps to Reproduce
No database, table, or data load is required.
SELECT REGEXP_LIKE('', '^$') AS like_control;
SELECT REGEXP_INSTR('', '^$') AS match_position;
SELECT HEX(REGEXP_SUBSTR('', '^$')) AS matched_bytes;Expected:
like_control = 1
match_position = 1
matched_bytes = ''
Actual: the first expression returns 1; the latter two raise error 20301.
Possible Root Cause
In pkg/sql/plan/function/func_builtin_regexp.go, both
regexpSet.regularInstr and regexpSet.regularSubstr reject the call when:
pos < 1 || pos > int64(len(str))For an empty string, the default search position is 1 while len(str) is 0,
so validation fails before the regexp engine can evaluate an empty-string
match. The validation should retain normal invalid-position checks while
allowing the initial search position for an empty subject.
Activity
fix: support empty subjects in regexp functions (matrixorigin#27573)
fix: support empty subjects in regexp functions (matrixorigin#27573)
slyunnn commentedon Sep 8, 2026
Regression and extended verification for #27573 (2026-09-08)
Verification Environment
origin/mainat80d214bef28a33f81020b76b2ed9280a6e7654b9—fix: return NULL for POWER EXP and COT arithmetic failures (#28375).a5877b6126c949e6c9f9e71a899d08e229cf7476—fix: support empty subjects in regexp functions (#27579). This commit is contained in the verifiedorigin/main.e60b3eb3bdfd8ad5f1b5ae3eb8745037371f44d8—fix(sql): align ORD, BIT_COUNT and REGEXP UTF-8 semantics. It defines the current empty-subjectREGEXP_INSTRrule: every positiveposis valid and a zero-width match returns that position.127.0.0.1:26001. No Proxy, multi-CN routing, or external MySQL instance was used.1: focused CGo unit testTest_BuiltIn_RegexpEmptySubjectinpkg/sql/plan/function.2: directmysqlCLI SQL verification against127.0.0.1:26001.3: MOTR record and comparison replay of the new issue case against127.0.0.1:26001.COM_QUERY) withutf8mb4; no prepared-statement binary protocol was required for this scalar SQL-function regression.What Was Covered
REGEXP_LIKE('', '^$') = 1,REGEXP_INSTR('', '^$') = 1, andREGEXP_SUBSTR('', '^$') = ''.REGEXP_INSTR('Cat Cat', 'Cat', 2) = 5,REGEXP_SUBSTR('Cat Cat', 'Cat', 2) = 'Cat', andREGEXP_LIKE('Cat Cat', 'Cat') = 1.pos = 1; current positive-position behavior withREGEXP_INSTR('', '^$', 2) = 2; and second occurrenceREGEXP_INSTR('', '^$', 1, 2, 0) = 0.REGEXP_INSTR('', '^$', 0),REGEXP_SUBSTR('', '^$', 2), andREGEXP_INSTR('', '^$', 1, 0, 0)each return error20301.0forREGEXP_INSTRandNULLforREGEXP_SUBSTR; both functions propagate aNULLsubject.Test_BuiltIn_RegexpEmptySubjectpassed with-count=1.Findings
The original empty-subject failure is not reproducible on current
origin/main: the functions return the expected values without an error. The later PR#28267intentionally supersedes the originalREGEXP_INSTRposition detail: positive positions on an empty subject are accepted and a zero-width anchor returns the supplied position, whileREGEXP_SUBSTRretains its normal out-of-range check.Large-data regression is not needed. The defect is a deterministic scalar regular-expression boundary condition; it does not depend on row count, block or batch boundaries, memory or spill, concurrency, data skew, network transfer, or resource scale. Increasing data volume would add CI cost without exercising another relevant execution boundary.
MOTR / BVT Coverage
suites/14_issue_regression/t/issue_27573_empty_regexp_subject.testand recorded resultsuites/14_issue_regression/r/issue_27573_empty_regexp_subject.result.7statements,7expected outcomes, passed.7statements,7expected outcomes, passed.eb8b77ad523bc13e1f4e02dfc3febcbd86fe5c2f—test: add MOTR regression for #27573.test/issue-27573-empty-regexp-subjectinmatrixorigin/motr.Final Conclusion
Fixed. Current
origin/maincorrectly handles an empty subject forREGEXP_INSTRandREGEXP_SUBSTR, including the original'^$'reproduction, with a focused MOTR regression recorded and replayed successfully. No large-data regression is required for the reasons above.