Skip to content

Commit f0a4359

Browse files
author
James Cor
committed
cache regex
1 parent 9d8e43b commit f0a4359

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

sql/expression/function/regexp_like.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ type RegexpLike struct {
3434
Pattern sql.Expression
3535
Flags sql.Expression
3636

37-
cachedVal any
38-
cacheable bool
39-
re regex.Regex
40-
compileOnce sync.Once
41-
compileErr error
37+
cacheableResult bool
38+
cachedVal any
39+
cacheableRegex bool
40+
re regex.Regex
41+
compileOnce sync.Once
42+
compileErr error
4243
}
4344

4445
var _ sql.FunctionExpression = (*RegexpLike)(nil)
@@ -136,12 +137,13 @@ func (r *RegexpLike) String() string {
136137
// compile handles compilation of the regex.
137138
func (r *RegexpLike) compile(ctx *sql.Context, row sql.Row) {
138139
r.compileOnce.Do(func() {
139-
r.cacheable = canBeCached(r.Text, r.Pattern, r.Flags)
140-
if r.cacheable {
140+
r.cacheableRegex = canBeCached(r.Pattern, r.Flags)
141+
r.cacheableResult = canBeCached(r.Text) && r.cacheableRegex
142+
if r.cacheableRegex {
141143
r.re, r.compileErr = compileRegex(ctx, r.Pattern, r.Text, r.Flags, r.FunctionName(), row)
142144
}
143145
})
144-
if !r.cacheable {
146+
if !r.cacheableRegex {
145147
if r.re != nil {
146148
if r.compileErr = r.re.Close(); r.compileErr != nil {
147149
return
@@ -199,7 +201,7 @@ func (r *RegexpLike) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
199201
outVal = int8(0)
200202
}
201203

202-
if r.cacheable {
204+
if r.cacheableResult {
203205
r.cachedVal = outVal
204206
}
205207
return outVal, nil

0 commit comments

Comments
 (0)