Skip to content

[Bug]: REGEXP_INSTR and REGEXP_SUBSTR reject an empty subject string #27573

Closed

Description

@Lundomn

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

The equivalent match predicate succeeds:

SELECT REGEXP_LIKE('', '^$');
-- 1

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

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

  1. self-assigned this
    on Aug 25, 2026
  2. reopened this on Sep 4, 2026
  3. assigned and unassigned on Sep 4, 2026
  4. slyunnn commented on Sep 8, 2026

    @slyunnn

    Regression and extended verification for #27573 (2026-09-08)

    Verification Environment

    • MatrixOne baseline: origin/main at 80d214bef28a33f81020b76b2ed9280a6e7654b9 — fix: return NULL for POWER EXP and COT arithmetic failures (#28375).
    • Related original fix: PR #27579, merge commit a5877b6126c949e6c9f9e71a899d08e229cf7476 — fix: support empty subjects in regexp functions (#27579). This commit is contained in the verified origin/main.
    • Related subsequent semantic update: PR #28267, merge commit e60b3eb3bdfd8ad5f1b5ae3eb8745037371f44d8 — fix(sql): align ORD, BIT_COUNT and REGEXP UTF-8 semantics. It defines the current empty-subject REGEXP_INSTR rule: every positive pos is valid and a zero-width match returns that position.
    • Topology: local standalone MatrixOne launch with one Log Service, one TN, and one CN; the CN SQL listener was 127.0.0.1:26001. No Proxy, multi-CN routing, or external MySQL instance was used.
    • Test entry point 1: focused CGo unit test Test_BuiltIn_RegexpEmptySubject in pkg/sql/plan/function.
    • Test entry point 2: direct mysql CLI SQL verification against 127.0.0.1:26001.
    • Test entry point 3: MOTR record and comparison replay of the new issue case against 127.0.0.1:26001.
    • Client protocol: MySQL text-query protocol (COM_QUERY) with utf8mb4; no prepared-statement binary protocol was required for this scalar SQL-function regression.

    What Was Covered

    • OK Original reproduction: REGEXP_LIKE('', '^$') = 1, REGEXP_INSTR('', '^$') = 1, and REGEXP_SUBSTR('', '^$') = ''.
    • OK Happy path: REGEXP_INSTR('Cat Cat', 'Cat', 2) = 5, REGEXP_SUBSTR('Cat Cat', 'Cat', 2) = 'Cat', and REGEXP_LIKE('Cat Cat', 'Cat') = 1.
    • OK Boundary: explicit empty-subject pos = 1; current positive-position behavior with REGEXP_INSTR('', '^$', 2) = 2; and second occurrence REGEXP_INSTR('', '^$', 1, 2, 0) = 0.
    • OK Unhappy path: REGEXP_INSTR('', '^$', 0), REGEXP_SUBSTR('', '^$', 2), and REGEXP_INSTR('', '^$', 1, 0, 0) each return error 20301.
    • OK Control semantics: no-match returns 0 for REGEXP_INSTR and NULL for REGEXP_SUBSTR; both functions propagate a NULL subject.
    • OK Focused internal regression: Test_BuiltIn_RegexpEmptySubject passed with -count=1.
    • Not covered External differential execution against a separately installed MySQL server, Proxy, multi-CN routing, binary/mixed string domains, and prepared-statement binary protocol; these are outside this empty text-subject issue contract.

    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 #28267 intentionally supersedes the original REGEXP_INSTR position detail: positive positions on an empty subject are accepted and a zero-width anchor returns the supplied position, while REGEXP_SUBSTR retains 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

    • Added MOTR case suites/14_issue_regression/t/issue_27573_empty_regexp_subject.test and recorded result suites/14_issue_regression/r/issue_27573_empty_regexp_subject.result.
    • MOTR record: 7 statements, 7 expected outcomes, passed.
    • MOTR replay: 7 statements, 7 expected outcomes, passed.
    • Commit: eb8b77ad523bc13e1f4e02dfc3febcbd86fe5c2f — test: add MOTR regression for #27573.
    • Backup branch: test/issue-27573-empty-regexp-subject in matrixorigin/motr.

    Final Conclusion

    Fixed. Current origin/main correctly handles an empty subject for REGEXP_INSTR and REGEXP_SUBSTR, including the original '^$' reproduction, with a focused MOTR regression recorded and replayed successfully. No large-data regression is required for the reasons above.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions