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 pathsubstring.go
259 lines (222 loc) · 6.5 KB
/
substring.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package function
import (
"fmt"
"reflect"
"strings"
"github.com/src-d/go-mysql-server/sql"
)
// Substring is a function to return a part of a string.
// This function behaves as the homonym MySQL function.
// Since Go strings are UTF8, this function does not return a direct sub
// string str[start:start+length], instead returns the substring of rune
// s. That is, "á"[0:1] does not return a partial unicode glyph, but "á"
// itself.
type Substring struct {
str sql.Expression
start sql.Expression
len sql.Expression
}
// NewSubstring creates a new substring UDF.
func NewSubstring(args ...sql.Expression) (sql.Expression, error) {
var str, start, ln sql.Expression
switch len(args) {
case 2:
str = args[0]
start = args[1]
ln = nil
case 3:
str = args[0]
start = args[1]
ln = args[2]
default:
return nil, sql.ErrInvalidArgumentNumber.New("SUBSTRING", "2 or 3", len(args))
}
return &Substring{str, start, ln}, nil
}
// Children implements the Expression interface.
func (s *Substring) Children() []sql.Expression {
if s.len == nil {
return []sql.Expression{s.str, s.start}
}
return []sql.Expression{s.str, s.start, s.len}
}
// Eval implements the Expression interface.
func (s *Substring) Eval(
ctx *sql.Context,
row sql.Row,
) (interface{}, error) {
str, err := s.str.Eval(ctx, row)
if err != nil {
return nil, err
}
var text []rune
switch str := str.(type) {
case string:
text = []rune(str)
case []byte:
text = []rune(string(str))
case nil:
return nil, nil
default:
return nil, sql.ErrInvalidType.New(reflect.TypeOf(str).String())
}
start, err := s.start.Eval(ctx, row)
if err != nil {
return nil, err
}
if start == nil {
return nil, nil
}
start, err = sql.Int64.Convert(start)
if err != nil {
return nil, err
}
var length int64
runeCount := int64(len(text))
if s.len != nil {
len, err := s.len.Eval(ctx, row)
if err != nil {
return nil, err
}
if len == nil {
return nil, nil
}
len, err = sql.Int64.Convert(len)
if err != nil {
return nil, err
}
length = len.(int64)
} else {
length = runeCount
}
var startIdx int64
if start := start.(int64); start < 0 {
startIdx = runeCount + start
} else {
startIdx = start - 1
}
if startIdx < 0 || startIdx >= runeCount || length <= 0 {
return "", nil
}
if startIdx+length > runeCount {
length = int64(runeCount) - startIdx
}
return string(text[startIdx : startIdx+length]), nil
}
// IsNullable implements the Expression interface.
func (s *Substring) IsNullable() bool {
return s.str.IsNullable() || s.start.IsNullable() || (s.len != nil && s.len.IsNullable())
}
func (s *Substring) String() string {
if s.len == nil {
return fmt.Sprintf("SUBSTRING(%s, %s)", s.str, s.start)
}
return fmt.Sprintf("SUBSTRING(%s, %s, %s)", s.str, s.start, s.len)
}
// Resolved implements the Expression interface.
func (s *Substring) Resolved() bool {
return s.start.Resolved() && s.str.Resolved() && (s.len == nil || s.len.Resolved())
}
// Type implements the Expression interface.
func (*Substring) Type() sql.Type { return sql.Text }
/// WithChildren implements the Expression interface.
func (*Substring) WithChildren(children ...sql.Expression) (sql.Expression, error) {
return NewSubstring(children...)
}
// SubstringIndex returns the substring from string str before count occurrences of the delimiter delim.
// If count is positive, everything to the left of the final delimiter (counting from the left) is returned.
// If count is negative, everything to the right of the final delimiter (counting from the right) is returned.
// SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.
type SubstringIndex struct {
str sql.Expression
delim sql.Expression
count sql.Expression
}
// NewSubstringIndex creates a new SubstringIndex UDF.
func NewSubstringIndex(str, delim, count sql.Expression) sql.Expression {
return &SubstringIndex{str, delim, count}
}
// Children implements the Expression interface.
func (s *SubstringIndex) Children() []sql.Expression {
return []sql.Expression{s.str, s.delim, s.count}
}
// Eval implements the Expression interface.
func (s *SubstringIndex) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
ex, err := s.str.Eval(ctx, row)
if ex == nil || err != nil {
return nil, err
}
ex, err = sql.Text.Convert(ex)
if err != nil {
return nil, err
}
str, ok := ex.(string)
if !ok {
return nil, sql.ErrInvalidType.New(reflect.TypeOf(ex).String())
}
ex, err = s.delim.Eval(ctx, row)
if ex == nil || err != nil {
return nil, err
}
ex, err = sql.Text.Convert(ex)
if err != nil {
return nil, err
}
delim, ok := ex.(string)
if !ok {
return nil, sql.ErrInvalidType.New(reflect.TypeOf(ex).String())
}
ex, err = s.count.Eval(ctx, row)
if ex == nil || err != nil {
return nil, err
}
ex, err = sql.Int64.Convert(ex)
if err != nil {
return nil, err
}
count, ok := ex.(int64)
if !ok {
return nil, sql.ErrInvalidType.New(reflect.TypeOf(ex).String())
}
// Implementation taken from pingcap/tidb
// https://github.com/pingcap/tidb/blob/37c128b64f3ad2f08d52bc767b6e3320ecf429d8/expression/builtin_string.go#L1229
strs := strings.Split(str, delim)
start, end := int64(0), int64(len(strs))
if count > 0 {
// If count is positive, everything to the left of the final delimiter (counting from the left) is returned.
if count < end {
end = count
}
} else {
// If count is negative, everything to the right of the final delimiter (counting from the right) is returned.
count = -count
if count < 0 {
// -count overflows max int64, returns an empty string.
return "", nil
}
if count < end {
start = end - count
}
}
return strings.Join(strs[start:end], delim), nil
}
// IsNullable implements the Expression interface.
func (s *SubstringIndex) IsNullable() bool {
return s.str.IsNullable() || s.delim.IsNullable() || s.count.IsNullable()
}
func (s *SubstringIndex) String() string {
return fmt.Sprintf("SUBSTRING_INDEX(%s, %s, %d)", s.str, s.delim, s.count)
}
// Resolved implements the Expression interface.
func (s *SubstringIndex) Resolved() bool {
return s.str.Resolved() && s.delim.Resolved() && s.count.Resolved()
}
// Type implements the Expression interface.
func (*SubstringIndex) Type() sql.Type { return sql.Text }
// WithChildren implements the Expression interface.
func (s *SubstringIndex) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 3 {
return nil, sql.ErrInvalidChildrenNumber.New(s, len(children), 3)
}
return NewSubstringIndex(children[0], children[1], children[2]), nil
}