Skip to content

evalengine: report a bad regexp pattern when another argument is NULL - #22133

Open
Pranav-error wants to merge 2 commits into
vitessio:mainfrom
Pranav-error:fix-regexp-null-invalid-pattern
Open

Pranav-error wants to merge 2 commits into
vitessio:mainfrom
Pranav-error:fix-regexp-null-invalid-pattern

Conversation

@Pranav-error

@Pranav-error Pranav-error commented Nov 23, 2026 •

Copy link
Copy Markdown

Description

REGEXP, REGEXP_LIKE, REGEXP_INSTR, REGEXP_SUBSTR and REGEXP_REPLACE return NULL as soon as the subject, the replacement, the position, the occurrence or the return option is NULL, without looking at the pattern. MySQL compiles the pattern first and fails the statement when it is invalid. On MySQL 8.4.13, NULL REGEXP '[', REGEXP_INSTR('dog', '[', NULL), REGEXP_SUBSTR(NULL, '[', 1, 1, 'i'), REGEXP_REPLACE('a', '[', NULL) and REGEXP_REPLACE('dog cat dog', '[', 'bar', 1, NULL, 'i') all return ERROR 3696 (HY000): The regular expression contains an unclosed bracket expression., and NULL REGEXP '' returns ERROR 3685 (HY000): Illegal argument to a regular expression. This also happens when the pattern comes from a column or a prepared statement parameter. A NULL pattern or a NULL match type still gives NULL, for example REGEXP_LIKE(NULL, '[', NULL) and NULL REGEXP NULL.

The match type is parsed before NULL is returned as well, so a bad match type now fails with ERROR 1210 (HY000): Incorrect arguments to ... when the subject or another argument is NULL, where main returns NULL. For example REGEXP_LIKE(NULL, 'a', 'x') and REGEXP_INSTR(NULL, '[', 1, 1, 1, 'x'); the latter reports the match type, not the bracket, as on MySQL.

This PR checks those arguments for NULL only after the pattern and the match type, and compiles the pattern before returning NULL:

  • interpreter: the eval methods of the four builtins call a new regexpNullArg helper when one of those arguments is NULL.
  • compiled program: the early NULL jumps on those arguments are replaced by a new Fn_REGEXP_NULLCHECK instruction that runs once all arguments are on the stack. It compiles the pattern only when it could not be compiled ahead of time, so a constant valid pattern costs nothing extra. The integer conversions of position, occurrence and return option are now skipped for NULL values instead of jumping out.

Left out on purpose, since they are separate checks: MySQL also fails REGEXP_LIKE('a', NULL, 'x') (bad match type with a NULL pattern) and REGEXP_INSTR(NULL, 'a', 1, 1, 5) / REGEXP_SUBSTR(NULL, 'a', 0) (bad return option or position with a NULL subject), where the evalengine still returns NULL.

Motivation

How do you use Vitess? (pick one)

  • In production
  • In staging or development
  • Evaluating it
  • I don't use Vitess

Did you run this change against a real Vitess deployment? No. These inputs were among the cases hidden by knownErrors (#22107): 27 of them in RegexpLike, RegexpSubstr and RegexpReplace. I ran them against MySQL 8.4.13 to confirm the errors. The new TestRegexpNullArgumentWithBadPattern checks both the interpreter and the compiled path with bind variables, so nothing is folded away; it fails on the current code and passes with this change. The evalengine package tests and TestMySQL/Regexp* against MySQL 8.4 pass, and with the strict comparison from #22107 the 27 hidden cases go to 0.

Related Issue(s)

Found with the strict comparison described in #22107. Same kind of fix as #22130 and #22131.

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required

REGEXP, REGEXP_LIKE, REGEXP_INSTR, REGEXP_SUBSTR and REGEXP_REPLACE
returned NULL as soon as the subject, the replacement, the position,
the occurrence or the return option was NULL, without looking at the
pattern. MySQL compiles the pattern first and fails the statement when
it is invalid: on MySQL 8.4, NULL REGEXP '[' fails with
ER_REGEXP_MISSING_CLOSE_BRACKET, NULL REGEXP '' with
ER_REGEXP_ILLEGAL_ARGUMENT, and REGEXP_REPLACE('a', '[', NULL) and
REGEXP_SUBSTR('dog cat dog', '[', 1, NULL, 'i') fail the same way, also
when the pattern comes from a column. A NULL pattern or a NULL match type still
gives NULL.

Check those arguments for NULL only after the pattern and the match
type, and compile the pattern before returning NULL, in both the AST
evaluator and the compiled program. The compiled program only compiles
the pattern at run time when it could not be compiled ahead of time.

Signed-off-by: sai pranav <rajasaipranav0@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 09:29

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-28T17:09:55.559236Z 73c899b New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@vitess-bot vitess-bot Bot added NeedsWebsiteDocsUpdate What it says NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Nov 23, 2026
@github-actions github-actions Bot added this to the v25.0.0 milestone Nov 23, 2026
@vitess-bot

vitess-bot Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@github-actions github-actions Bot added Component: Evalengine changes to the evaluation engine Component: VTGate labels Nov 23, 2026

@mattlord mattlord left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking: The match type parse on the new NULL paths has no test. In go/vt/vtgate/evalengine/fn_regexp.go:293 (regexpNullArg) and go/vt/vtgate/evalengine/compiler_asm.go:4892 (Fn_REGEXP_NULLCHECK), the match type is now parsed before NULL is returned, so a bad match type with a NULL subject or another NULL argument is now an error where main returns NULL. That matches MySQL: on 8.0.46 and 8.4.9, REGEXP_LIKE(NULL, 'a', 'x'), REGEXP_INSTR(NULL, 'a', 1, 1, 1, 'x'), REGEXP_SUBSTR(NULL, 'a', 1, 1, 'x') and REGEXP_REPLACE(NULL, 'DOG', 'bar', 1, 1, 0) all fail with ERROR 1210 (HY000): Incorrect arguments to ..., and REGEXP_INSTR(NULL, '[', 1, 1, 1, 'x') reports the match type rather than the bracket. But removing either regexpFlags call leaves the whole evalengine package passing, so this second behavior change could regress without notice. I think that we should add a couple of cases to TestRegexpNullArgumentWithBadPattern that expect vterrors.WrongArguments, such as REGEXP_LIKE(:s, 'a', 'x') and REGEXP_INSTR(:s, '[', 1, 1, 1, 'x') (for the ordering), and mention the match type case in the description next to the pattern case. What do you think?

Locally, the evalengine package tests pass.

Thanks for the fix!

A bad match type is now reported when another argument is NULL, as on
MySQL 8.4, so cover it, including its order before a bad pattern.

Signed-off-by: sai pranav <rajasaipranav0@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 28, 2026 17:06
@Pranav-error

Copy link
Copy Markdown
Author

Thanks, agreed. I checked your cases on MySQL 8.4.13 and they all fail with 1210, including REGEXP_INSTR(NULL, '[', 1, 1, 1, 'x'). I added REGEXP_LIKE(:s, 'a', 'x'), REGEXP_INSTR(:s, 'a', 1, 1, 1, 'x'), REGEXP_INSTR(:s, '[', 1, 1, 1, 'x'), REGEXP_SUBSTR(:s, 'a', 1, 1, 'x') and REGEXP_REPLACE(:s, 'DOG', 'bar', 1, 1, 0) to TestRegexpNullArgumentWithBadPattern, expecting vterrors.WrongArguments (73c899b). Dropping the regexpFlags call in either regexpNullArg or Fn_REGEXP_NULLCHECK now fails all five, and I'll mention the match type case in the description.

One related difference I left out of this PR: with a NULL pattern, REGEXP_LIKE('a', NULL, 'x') fails with 1210 on MySQL but returns NULL here, on main too, because the NULL pattern returns before the match type is read. I can look at that separately if you think it's worth it.

Copilot AI 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.

🟢 Approval recommended

No active review issues are reported for the changes aligning regular-expression error handling with MySQL.

No open issues 🎉

Review effort: Balanced

@mattlord mattlord left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding these. The five new cases cover both NULL paths: removing the regexpFlags call in either regexpNullArg or Fn_REGEXP_NULLCHECK now fails them, and the evalengine package tests pass locally.

On the NULL pattern case, I see the same on MySQL 8.0.46 and 8.4.9: REGEXP_LIKE('a', NULL, 'x'), REGEXP_LIKE(NULL, NULL, 'x') and REGEXP_INSTR('a', NULL, 1, 1, 0, 'x') all fail with ERROR 1210, while REGEXP_LIKE('a', NULL, NULL) gives NULL. I think that a separate PR is the right place for it, along with the return option and position cases that you already listed, since it's a different check.

The description doesn't mention the match type change yet. It seems worth a sentence next to the pattern case saying that a bad match type now fails with ERROR 1210 when the subject or another argument is NULL, where main returns NULL.

@codecov

codecov Bot commented Nov 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.96774% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.81%. Comparing base (70c7a72) to head (73c899b).
⚠️ Report is 849 commits behind head on main.

Files with missing lines Patch % Lines
go/vt/vtgate/evalengine/fn_regexp.go 95.00% 5 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main   #22133       +/-   ##
===========================================
+ Coverage   69.67%   86.81%   +17.14%     
===========================================
  Files        1614       59     -1555     
  Lines      216793    15823   -200970     
===========================================
- Hits       151044    13737   -137307     
+ Misses      65749     2086    -63663     
Flag Coverage Δ
partial 86.81% <95.96%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

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

Labels

Component: Evalengine changes to the evaluation engine Component: VTGate NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants