Skip to content

Commit

Permalink
expression: Report error when empty pattern in regexp related function (
Browse files Browse the repository at this point in the history
#53230)

close #53221
  • Loading branch information
yibin87 authored May 14, 2024
1 parent 78549f3 commit 4b91fee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/executor/test/issuetest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go_test(
"main_test.go",
],
flaky = True,
shard_count = 20,
shard_count = 21,
deps = [
"//pkg/autoid_service",
"//pkg/config",
Expand Down
24 changes: 24 additions & 0 deletions pkg/executor/test/issuetest/executor_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,3 +676,27 @@ func TestIssue52978(t *testing.T) {
tk.MustQuery("select min(truncate(cast(-26340 as double), ref_11.a)) as c3 from t as ref_11;").Check(testkit.Rows("-26340"))
tk.MustExec("drop table if exists t")
}

func TestIssue53221(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a varchar(20))")
tk.MustExec("insert into t values ('')")
tk.MustExec("insert into t values ('')")
err := tk.QueryToErr("select regexp_like('hello', t.a) from test.t")
require.ErrorContains(t, err, "Empty pattern is invalid")

err = tk.QueryToErr("select regexp_instr('hello', t.a) from test.t")
require.ErrorContains(t, err, "Empty pattern is invalid")

err = tk.QueryToErr("select regexp_substr('hello', t.a) from test.t")
require.ErrorContains(t, err, "Empty pattern is invalid")

err = tk.QueryToErr("select regexp_replace('hello', t.a, 'd') from test.t")
require.ErrorContains(t, err, "Empty pattern is invalid")

tk.MustExec("drop table if exists t")
}
15 changes: 11 additions & 4 deletions pkg/expression/builtin_regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (re *regexpBaseFuncSig) canMemorizeRegexp(matchTypeIdx int) bool {

// buildRegexp builds a new `*regexp.Regexp` from the pattern and matchType
func (re *regexpBaseFuncSig) buildRegexp(pattern string, matchType string) (reg *regexp.Regexp, err error) {
if len(pattern) == 0 {
return nil, ErrRegexp.GenWithStackByArgs(emptyPatternErr)
}

matchType, err = getRegexpMatchType(matchType, re.collation)
if err != nil {
return nil, err
Expand Down Expand Up @@ -314,7 +318,8 @@ func (re *builtinRegexpLikeFuncSig) vecEvalInt(ctx EvalContext, input *chunk.Chu

if !memorized {
matchType := params[2].getStringVal(i)
reg, err = re.buildRegexp(params[1].getStringVal(i), matchType)
pattern := params[1].getStringVal(i)
reg, err = re.buildRegexp(pattern, matchType)
if err != nil {
return err
}
Expand Down Expand Up @@ -583,8 +588,8 @@ func (re *builtinRegexpSubstrFuncSig) vecEvalString(ctx EvalContext, input *chun

if !memorized {
// Get pattern and match type and then generate regexp
pattern := params[1].getStringVal(i)
matchType := params[4].getStringVal(i)
pattern := params[1].getStringVal(i)
if reg, err = re.buildRegexp(pattern, matchType); err != nil {
return err
}
Expand Down Expand Up @@ -915,7 +920,8 @@ func (re *builtinRegexpInStrFuncSig) vecEvalInt(ctx EvalContext, input *chunk.Ch
// Get match type and generate regexp
if !memorized {
matchType := params[5].getStringVal(i)
reg, err = re.buildRegexp(params[1].getStringVal(i), matchType)
pattern := params[1].getStringVal(i)
reg, err = re.buildRegexp(pattern, matchType)
if err != nil {
return err
}
Expand Down Expand Up @@ -1401,7 +1407,8 @@ func (re *builtinRegexpReplaceFuncSig) vecEvalString(ctx EvalContext, input *chu
// Get match type and generate regexp
if !memorized {
matchType := params[5].getStringVal(i)
reg, err = re.buildRegexp(params[1].getStringVal(i), matchType)
pattern := params[1].getStringVal(i)
reg, err = re.buildRegexp(pattern, matchType)
if err != nil {
return err
}
Expand Down

0 comments on commit 4b91fee

Please sign in to comment.