Skip to content

fix(function): reject binary regexp operands and add match type overloads - #26724

Closed
ck89119 wants to merge 18 commits into
matrixorigin:mainfrom
ck89119:issue-25299-main
Closed

ck89119 wants to merge 18 commits into
matrixorigin:mainfrom
ck89119:issue-25299-main

Conversation

@ck89119

@ck89119 ck89119 commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #25299

What this PR does / why we need it:

MySQL 8.4 rejects incompatible static binary/nonbinary operand combinations used by REGEXP, NOT REGEXP, and the REGEXP_* functions with error 3995 (HY000). It does not reject every binary regexp expression: binary/binary expressions are valid and use byte semantics, and parameter markers are exempt from the static incompatibility check. An ordinary untyped NULL is also exempt, while a statically binary expression such as CAST(NULL AS BINARY) is not.

MatrixOne previously let the generic fixed-type matcher cast BINARY, VARBINARY, and BLOB operands to VARCHAR before regexp compatibility validation, so incompatible static binary/nonbinary expressions executed instead of failing.

This change:

  • validates regexp operand character-set compatibility after overload resolution but before implicit regexp casts;
  • rejects only incompatible static binary/nonbinary combinations, preserving valid binary/binary and nonbinary/nonbinary combinations;
  • shares the validation across the regexp operators and REGEXP_LIKE, REGEXP_INSTR, REGEXP_SUBSTR, and REGEXP_REPLACE;
  • returns MySQL-compatible error 3995 / SQLSTATE HY000, including the correct function name and operand order;
  • follows MySQL's PARAM_ITEM and ordinary NULL_ITEM exceptions, so SQL/binary-protocol parameter markers and untyped NULL are not rejected by the static check;
  • still rejects statically binary expressions such as CAST(NULL AS BINARY) when paired with a nonbinary operand;
  • leaves valid binary regexp execution to the binary-string metadata and byte-semantics path, while ordinary text continues to use character/ICU semantics;
  • keeps prepared execution on the existing reusable-plan path without a regexp-specific plan scan or extra deep copy;
  • adds the missing match_type overloads for REGEXP_INSTR, REGEXP_SUBSTR, and REGEXP_REPLACE, including c, i, m, n, and u behavior;
  • aligns optional-argument edge behavior with MySQL for UTF-8 character positions, newline modes, empty patterns, position/occurrence boundaries, return_option, and replacement capture expansion;
  • covers static binary/nonbinary incompatibility in both operand orders, binary/binary execution, prepared binary parameters, ordinary and statically binary NULLs, empty/invalid-byte cases, and normal text controls.

Examples of the intended contract:

_binary'abc' REGEXP 'a'          -- ERROR 3995
'abc' REGEXP _binary'a'          -- ERROR 3995
_binary'abc' REGEXP _binary'a'   -- 1, byte semantics
NULL REGEXP 'a'                  -- NULL
CAST(NULL AS BINARY) REGEXP 'a'  -- ERROR 3995

An explicit empty match_type preserves MatrixOne's existing case-sensitive default. Matching MySQL's collation-derived default would require propagating collation metadata into the function executor and is outside this change.

Validation:

  • make build
  • .agents/skills/mo-dev/scripts/mo-cgo-test ./pkg/common/moerr ./pkg/sql/plan/function ./pkg/sql/plan ./pkg/frontend -count=1
  • .agents/skills/mo-dev/scripts/mo-cgo-test ./pkg/tests/issues -run '^TestIssue25299RegexpRejectsBinaryCharset$' -count=1 -timeout 5m
  • targeted distributed BVT: 114/114 passed
  • git diff --check
  • mo-self-review on the complete diff

@ck89119 ck89119 changed the title fix(plan): reject binary regexp operands fix(function): reject binary regexp operands and add match type overloads Aug 5, 2026

@XuPeng-SH XuPeng-SH 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.

Request changes.

The current UT is failing in the test added by this PR: TestImplicitStringCastPreservesBinaryCharsetMetadata. It panics with type mismatch: casting VARCHAR vector to []types.Type at func_cast_test.go:2737. Please fix the vector/test setup or implementation, then rerun UT/BVT. The Coverage failure is only a downstream cascade.

# Conflicts:
#	pkg/frontend/util.go
#	pkg/sql/plan/base_binder.go
#	pkg/sql/plan/function/list_builtIn.go
#	pkg/sql/plan/function/type_check.go

This branch had an error being deployed

1 failed deployment
ci — c0169b68 Deployed Aug 14, 2026 by ck89119 via Matrixone Utils CI / Coverage #46220
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants