This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathregexp_matches.go
204 lines (167 loc) · 4.08 KB
/
regexp_matches.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package function
import (
"fmt"
"regexp"
"strings"
"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/expression"
errors "gopkg.in/src-d/go-errors.v1"
)
// RegexpMatches returns the matches of a regular expression.
type RegexpMatches struct {
Text sql.Expression
Pattern sql.Expression
Flags sql.Expression
cacheable bool
re *regexp.Regexp
}
// NewRegexpMatches creates a new RegexpMatches expression.
func NewRegexpMatches(args ...sql.Expression) (sql.Expression, error) {
var r RegexpMatches
switch len(args) {
case 3:
r.Flags = args[2]
fallthrough
case 2:
r.Text = args[0]
r.Pattern = args[1]
default:
return nil, sql.ErrInvalidArgumentNumber.New("regexp_matches", "2 or 3", len(args))
}
if canBeCached(r.Pattern) && (r.Flags == nil || canBeCached(r.Flags)) {
r.cacheable = true
}
return &r, nil
}
// Type implements the sql.Expression interface.
func (r *RegexpMatches) Type() sql.Type { return sql.Array(sql.Text) }
// IsNullable implements the sql.Expression interface.
func (r *RegexpMatches) IsNullable() bool { return true }
// Children implements the sql.Expression interface.
func (r *RegexpMatches) Children() []sql.Expression {
var result = []sql.Expression{r.Text, r.Pattern}
if r.Flags != nil {
result = append(result, r.Flags)
}
return result
}
// Resolved implements the sql.Expression interface.
func (r *RegexpMatches) Resolved() bool {
return r.Text.Resolved() && r.Pattern.Resolved() && (r.Flags == nil || r.Flags.Resolved())
}
// WithChildren implements the sql.Expression interface.
func (r *RegexpMatches) WithChildren(children ...sql.Expression) (sql.Expression, error) {
required := 2
if r.Flags != nil {
required = 3
}
if len(children) != required {
return nil, sql.ErrInvalidChildrenNumber.New(r, len(children), required)
}
return NewRegexpMatches(children...)
}
func (r *RegexpMatches) String() string {
var args []string
for _, e := range r.Children() {
args = append(args, e.String())
}
return fmt.Sprintf("regexp_matches(%s)", strings.Join(args, ", "))
}
// Eval implements the sql.Expression interface.
func (r *RegexpMatches) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
span, ctx := ctx.Span("function.RegexpMatches")
defer span.Finish()
var re *regexp.Regexp
var err error
if r.cacheable {
if r.re == nil {
r.re, err = r.compileRegex(ctx, nil)
if err != nil {
return nil, err
}
if r.re == nil {
return nil, nil
}
}
re = r.re
} else {
re, err = r.compileRegex(ctx, row)
if err != nil {
return nil, err
}
if re == nil {
return nil, nil
}
}
text, err := r.Text.Eval(ctx, row)
if err != nil {
return nil, err
}
if text == nil {
return nil, nil
}
text, err = sql.Text.Convert(text)
if err != nil {
return nil, err
}
matches := re.FindAllStringSubmatch(text.(string), -1)
if len(matches) == 0 {
return nil, nil
}
var result []interface{}
for _, m := range matches {
for _, sm := range m {
result = append(result, sm)
}
}
return result, nil
}
func (r *RegexpMatches) compileRegex(ctx *sql.Context, row sql.Row) (*regexp.Regexp, error) {
pattern, err := r.Pattern.Eval(ctx, row)
if err != nil {
return nil, err
}
if pattern == nil {
return nil, nil
}
pattern, err = sql.Text.Convert(pattern)
if err != nil {
return nil, err
}
var flags string
if r.Flags != nil {
f, err := r.Flags.Eval(ctx, row)
if err != nil {
return nil, err
}
if f == nil {
return nil, nil
}
f, err = sql.Text.Convert(f)
if err != nil {
return nil, err
}
flags = f.(string)
for _, f := range flags {
if !validRegexpFlags[f] {
return nil, errInvalidRegexpFlag.New(f)
}
}
flags = fmt.Sprintf("(?%s)", flags)
}
return regexp.Compile(flags + pattern.(string))
}
var errInvalidRegexpFlag = errors.NewKind("invalid regexp flag: %v")
var validRegexpFlags = map[rune]bool{
'i': true,
}
func canBeCached(e sql.Expression) bool {
var hasCols bool
expression.Inspect(e, func(e sql.Expression) bool {
if _, ok := e.(*expression.GetField); ok {
hasCols = true
}
return true
})
return !hasCols
}