/
substring.go
483 lines (407 loc) · 12.2 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright 2020-2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package function
import (
"fmt"
"reflect"
"strings"
"github.com/Ciyfly/FakerMysql/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
}
var _ sql.FunctionExpression = (*Substring)(nil)
// 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
}
// FunctionName implements sql.FunctionExpression
func (s *Substring) FunctionName() string {
return "substring"
}
// 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.LongText }
// 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
}
var _ sql.FunctionExpression = (*SubstringIndex)(nil)
// NewSubstringIndex creates a new SubstringIndex UDF.
func NewSubstringIndex(str, delim, count sql.Expression) sql.Expression {
return &SubstringIndex{str, delim, count}
}
// FunctionName implements sql.FunctionExpression
func (s *SubstringIndex) FunctionName() string {
return "substring_index"
}
// 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.LongText.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.LongText.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.LongText }
// 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
}
// Left is a function that returns the first N characters of a string expression.
type Left struct {
str sql.Expression
len sql.Expression
}
var _ sql.FunctionExpression = Left{}
// NewLeft creates a new LEFT function.
func NewLeft(str, len sql.Expression) sql.Expression {
return Left{str, len}
}
// FunctionName implements sql.FunctionExpression
func (l Left) FunctionName() string {
return "left"
}
// Children implements the Expression interface.
func (l Left) Children() []sql.Expression {
return []sql.Expression{l.str, l.len}
}
// Eval implements the Expression interface.
func (l Left) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
str, err := l.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())
}
var length int64
runeCount := int64(len(text))
len, err := l.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)
if length > runeCount {
length = runeCount
}
if length <= 0 {
return "", nil
}
return string(text[:length]), nil
}
// IsNullable implements the Expression interface.
func (l Left) IsNullable() bool {
return l.str.IsNullable() || l.len.IsNullable()
}
func (l Left) String() string {
return fmt.Sprintf("LEFT(%s, %s)", l.str, l.len)
}
// Resolved implements the Expression interface.
func (l Left) Resolved() bool {
return l.str.Resolved() && l.len.Resolved()
}
// Type implements the Expression interface.
func (Left) Type() sql.Type { return sql.LongText }
// WithChildren implements the Expression interface.
func (l Left) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(l, len(children), 2)
}
return NewLeft(children[0], children[1]), nil
}
type Instr struct {
str sql.Expression
substr sql.Expression
}
var _ sql.FunctionExpression = Instr{}
// NewInstr creates a new instr UDF.
func NewInstr(str, substr sql.Expression) sql.Expression {
return Instr{str, substr}
}
// FunctionName implements sql.FunctionExpression
func (i Instr) FunctionName() string {
return "instr"
}
// Children implements the Expression interface.
func (i Instr) Children() []sql.Expression {
return []sql.Expression{i.str, i.substr}
}
// Eval implements the Expression interface.
func (i Instr) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
str, err := i.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())
}
substr, err := i.substr.Eval(ctx, row)
if err != nil {
return nil, err
}
var subtext []rune
switch substr := substr.(type) {
case string:
subtext = []rune(substr)
case []byte:
subtext = []rune(string(subtext))
case nil:
return nil, nil
default:
return nil, sql.ErrInvalidType.New(reflect.TypeOf(str).String())
}
return findSubsequence(text, subtext) + 1, nil
}
func findSubsequence(text []rune, subtext []rune) int64 {
for i := 0; i <= len(text)-len(subtext); i++ {
var j int
for j = 0; j < len(subtext); j++ {
if text[i+j] != subtext[j] {
break
}
}
if j == len(subtext) {
return int64(i)
}
}
return -1
}
// IsNullable implements the Expression interface.
func (i Instr) IsNullable() bool {
return i.str.IsNullable() || i.substr.IsNullable()
}
func (i Instr) String() string {
return fmt.Sprintf("INSTR(%s, %s)", i.str, i.substr)
}
// Resolved implements the Expression interface.
func (i Instr) Resolved() bool {
return i.str.Resolved() && i.substr.Resolved()
}
// Type implements the Expression interface.
func (Instr) Type() sql.Type { return sql.Int64 }
// WithChildren implements the Expression interface.
func (i Instr) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(i, len(children), 2)
}
return NewInstr(children[0], children[1]), nil
}