Skip to content

Commit

Permalink
Merge branch 'release-6.3' into release-6.3-5aab87679fde
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored Sep 21, 2022
2 parents 8506e69 + 77b175e commit 71e5453
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
22 changes: 20 additions & 2 deletions expression/builtin_regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
invalidIndex = "Index out of bounds in regular expression search"
invalidReturnOption = "Incorrect arguments to regexp_instr: return_option must be 1 or 0"
binaryCollateErr = "Not support binary collation so far"
emptyPatternErr = "Empty pattern is invalid"
)

var validMatchType = set.NewStringSet(
Expand Down Expand Up @@ -137,6 +138,10 @@ func (re *regexpBaseFuncSig) genCompile(matchType string) (func(string) (*regexp
}

func (re *regexpBaseFuncSig) genRegexp(pat string, matchType string) (*regexp.Regexp, error) {
if len(pat) == 0 {
return nil, ErrRegexp.GenWithStackByArgs(emptyPatternErr)
}

if re.isMemorizedRegexpInitialized() {
return re.memorizedRegexp, re.memorizedErr
}
Expand All @@ -162,14 +167,19 @@ func (re *regexpBaseFuncSig) canMemorize(matchTypeIdx int) bool {
}

func (re *regexpBaseFuncSig) initMemoizedRegexp(params []*regexpParam, matchTypeIdx int) error {
pat := params[patternIdx].getStringVal(0)
if len(pat) == 0 {
return ErrRegexp.GenWithStackByArgs(emptyPatternErr)
}

// Generate compile
compile, err := re.genCompile(params[matchTypeIdx].getStringVal(0))
if err != nil {
return ErrRegexp.GenWithStackByArgs(err)
}

// Compile this constant pattern, so that we can avoid this repeatable work
re.memorize(compile, params[patternIdx].getStringVal(0))
// Compile this constant pattern, so that we can avoid this repeated work
re.memorize(compile, pat)

return re.memorizedErr
}
Expand Down Expand Up @@ -251,6 +261,8 @@ func (re *builtinRegexpLikeFuncSig) evalInt(row chunk.Row) (int64, bool, error)
pat, isNull, err := re.args[1].EvalString(re.ctx, row)
if isNull || err != nil {
return 0, true, err
} else if len(pat) == 0 {
return 0, true, ErrRegexp.GenWithStackByArgs(emptyPatternErr)
}

matchType := ""
Expand Down Expand Up @@ -428,6 +440,8 @@ func (re *builtinRegexpSubstrFuncSig) evalString(row chunk.Row) (string, bool, e
pat, isNull, err := re.args[1].EvalString(re.ctx, row)
if isNull || err != nil {
return "", true, err
} else if len(pat) == 0 {
return "", true, ErrRegexp.GenWithStackByArgs(emptyPatternErr)
}

occurrence := int64(1)
Expand Down Expand Up @@ -751,6 +765,8 @@ func (re *builtinRegexpInStrFuncSig) evalInt(row chunk.Row) (int64, bool, error)
pat, isNull, err := re.args[1].EvalString(re.ctx, row)
if isNull || err != nil {
return 0, true, err
} else if len(pat) == 0 {
return 0, true, ErrRegexp.GenWithStackByArgs(emptyPatternErr)
}

pos := int64(1)
Expand Down Expand Up @@ -1129,6 +1145,8 @@ func (re *builtinRegexpReplaceFuncSig) evalString(row chunk.Row) (string, bool,
pat, isNull, err := re.args[1].EvalString(re.ctx, row)
if isNull || err != nil {
return "", true, err
} else if len(pat) == 0 {
return "", true, ErrRegexp.GenWithStackByArgs(emptyPatternErr)
}

repl, isNull, err := re.args[2].EvalString(re.ctx, row)
Expand Down
8 changes: 6 additions & 2 deletions expression/builtin_regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,9 @@ func TestRegexpLike(t *testing.T) {
{"..", "b", 0, nil},
{".ab", "aab", 1, nil},
{".*", "abcd", 1, nil},
{"(", "", 0, ErrRegexp},
{"(*", "", 0, ErrRegexp}, // index 10
{"", "a", 0, ErrRegexp}, // issue 37988
{"(", "", 0, ErrRegexp}, // index 10
{"(*", "", 0, ErrRegexp},
{"[a", "", 0, ErrRegexp},
{"\\", "", 0, ErrRegexp},
}
Expand Down Expand Up @@ -368,6 +369,7 @@ func TestRegexpSubstr(t *testing.T) {
{"abc", nil, nil, nil, nil},
{nil, "bc", nil, nil, nil},
{nil, nil, nil, nil, nil},
{"a", "", nil, nil, ErrRegexp}, // issue 37988
}

for charsetAndCollateTp := 0; charsetAndCollateTp < testCharsetAndCollateTpNum; charsetAndCollateTp++ {
Expand Down Expand Up @@ -623,6 +625,7 @@ func TestRegexpInStr(t *testing.T) {
{"abc", nil, nil, nil, nil},
{nil, "bc", nil, nil, nil},
{nil, nil, nil, nil, nil},
{"a", "", nil, nil, ErrRegexp}, // issue 37988
}

for charsetAndCollateTp := 0; charsetAndCollateTp < testCharsetAndCollateTpNum; charsetAndCollateTp++ {
Expand Down Expand Up @@ -935,6 +938,7 @@ func TestRegexpReplace(t *testing.T) {
{"abc", nil, nil, nil, nil, nil},
{nil, "bc", nil, nil, nil, nil},
{nil, nil, nil, nil, nil, nil},
{"a", "", "a", nil, nil, ErrRegexp}, // issue 37988
}

for charsetAndCollateTp := 0; charsetAndCollateTp < testCharsetAndCollateTpNum; charsetAndCollateTp++ {
Expand Down

0 comments on commit 71e5453

Please sign in to comment.