/
reverse_repeat_replace.go
263 lines (215 loc) · 6.58 KB
/
reverse_repeat_replace.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
// 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"
"strings"
"gopkg.in/src-d/go-errors.v1"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/expression"
)
// Reverse is a function that returns the reverse of the text provided.
type Reverse struct {
expression.UnaryExpression
}
var _ sql.FunctionExpression = (*Reverse)(nil)
// NewReverse creates a new Reverse expression.
func NewReverse(e sql.Expression) sql.Expression {
return &Reverse{expression.UnaryExpression{Child: e}}
}
// FunctionName implements sql.FunctionExpression
func (r *Reverse) FunctionName() string {
return "reverse"
}
// Description implements sql.FunctionExpression
func (r *Reverse) Description() string {
return "returns the string str with the order of the characters reversed."
}
// Eval implements the Expression interface.
func (r *Reverse) Eval(
ctx *sql.Context,
row sql.Row,
) (interface{}, error) {
//TODO: handle collations
v, err := r.Child.Eval(ctx, row)
if v == nil || err != nil {
return nil, err
}
v, err = sql.LongText.Convert(v)
if err != nil {
return nil, err
}
return reverseString(v.(string)), nil
}
func reverseString(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
func (r *Reverse) String() string {
return fmt.Sprintf("reverse(%s)", r.Child)
}
// WithChildren implements the Expression interface.
func (r *Reverse) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(r, len(children), 1)
}
return NewReverse(children[0]), nil
}
// Type implements the Expression interface.
func (r *Reverse) Type() sql.Type {
return r.Child.Type()
}
var ErrNegativeRepeatCount = errors.NewKind("negative Repeat count: %v")
// Repeat is a function that returns the string repeated n times.
type Repeat struct {
expression.BinaryExpression
}
var _ sql.FunctionExpression = (*Repeat)(nil)
// NewRepeat creates a new Repeat expression.
func NewRepeat(str sql.Expression, count sql.Expression) sql.Expression {
return &Repeat{expression.BinaryExpression{Left: str, Right: count}}
}
// FunctionName implements sql.FunctionExpression
func (r *Repeat) FunctionName() string {
return "repeat"
}
// Description implements sql.FunctionExpression
func (r *Repeat) Description() string {
return "returns a string consisting of the string str repeated count times."
}
func (r *Repeat) String() string {
return fmt.Sprintf("repeat(%s, %s)", r.Left, r.Right)
}
// Type implements the Expression interface.
func (r *Repeat) Type() sql.Type {
return sql.LongText
}
// WithChildren implements the Expression interface.
func (r *Repeat) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(r, len(children), 2)
}
return NewRepeat(children[0], children[1]), nil
}
// Eval implements the Expression interface.
func (r *Repeat) Eval(
ctx *sql.Context,
row sql.Row,
) (interface{}, error) {
//TODO: handle collations
str, err := r.Left.Eval(ctx, row)
if str == nil || err != nil {
return nil, err
}
str, err = sql.LongText.Convert(str)
if err != nil {
return nil, err
}
count, err := r.Right.Eval(ctx, row)
if count == nil || err != nil {
return nil, err
}
count, err = sql.Int32.Convert(count)
if err != nil {
return nil, err
}
if count.(int32) < 0 {
return nil, ErrNegativeRepeatCount.New(count)
}
return strings.Repeat(str.(string), int(count.(int32))), nil
}
// Replace is a function that returns a string with all occurrences of fromStr replaced by the
// string toStr
type Replace struct {
str sql.Expression
fromStr sql.Expression
toStr sql.Expression
}
var _ sql.FunctionExpression = (*Replace)(nil)
// NewReplace creates a new Replace expression.
func NewReplace(str sql.Expression, fromStr sql.Expression, toStr sql.Expression) sql.Expression {
return &Replace{str, fromStr, toStr}
}
// FunctionName implements sql.FunctionExpression
func (r *Replace) FunctionName() string {
return "replace"
}
// Description implements sql.FunctionExpression
func (r *Replace) Description() string {
return "returns the string str with all occurrences of the string from_str replaced by the string to_str."
}
// Children implements the Expression interface.
func (r *Replace) Children() []sql.Expression {
return []sql.Expression{r.str, r.fromStr, r.toStr}
}
// Resolved implements the Expression interface.
func (r *Replace) Resolved() bool {
return r.str.Resolved() && r.fromStr.Resolved() && r.toStr.Resolved()
}
// IsNullable implements the Expression interface.
func (r *Replace) IsNullable() bool {
return r.str.IsNullable() || r.fromStr.IsNullable() || r.toStr.IsNullable()
}
func (r *Replace) String() string {
return fmt.Sprintf("replace(%s, %s, %s)", r.str, r.fromStr, r.toStr)
}
// Type implements the Expression interface.
func (r *Replace) Type() sql.Type {
return sql.LongText
}
// WithChildren implements the Expression interface.
func (r *Replace) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 3 {
return nil, sql.ErrInvalidChildrenNumber.New(r, len(children), 3)
}
return NewReplace(children[0], children[1], children[2]), nil
}
// Eval implements the Expression interface.
func (r *Replace) Eval(
ctx *sql.Context,
row sql.Row,
) (interface{}, error) {
//TODO: handle collations
str, err := r.str.Eval(ctx, row)
if str == nil || err != nil {
return nil, err
}
str, err = sql.LongText.Convert(str)
if err != nil {
return nil, err
}
fromStr, err := r.fromStr.Eval(ctx, row)
if fromStr == nil || err != nil {
return nil, err
}
fromStr, err = sql.LongText.Convert(fromStr)
if err != nil {
return nil, err
}
toStr, err := r.toStr.Eval(ctx, row)
if toStr == nil || err != nil {
return nil, err
}
toStr, err = sql.LongText.Convert(toStr)
if err != nil {
return nil, err
}
if fromStr.(string) == "" {
return str, nil
}
return strings.Replace(str.(string), fromStr.(string), toStr.(string), -1), nil
}