-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
executable file
·423 lines (338 loc) · 9.57 KB
/
string.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
// 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 (
"encoding/hex"
"fmt"
"strconv"
"strings"
"time"
"unsafe"
"github.com/shopspring/decimal"
"github.com/Rock-liyi/p2pdb-store/sql"
)
// Ascii implements the sql function "ascii" which returns the numeric value of the leftmost character
type Ascii struct {
*UnaryFunc
}
var _ sql.FunctionExpression = (*Ascii)(nil)
func NewAscii(arg sql.Expression) sql.Expression {
return &Ascii{NewUnaryFunc(arg, "ASCII", sql.Uint8)}
}
// Description implements sql.FunctionExpression
func (a *Ascii) Description() string {
return "returns the numeric value of the leftmost character."
}
// Eval implements the sql.Expression interface
func (a *Ascii) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
val, err := a.EvalChild(ctx, row)
if err != nil {
return nil, err
}
if val == nil {
return nil, nil
}
switch x := val.(type) {
case bool:
if x {
val = 1
} else {
val = 0
}
case time.Time:
val = x.Year()
}
x, err := sql.Text.Convert(val)
if err != nil {
return nil, err
}
s := x.(string)
return s[0], nil
}
// WithChildren implements the sql.Expression interface
func (a *Ascii) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(a, len(children), 1)
}
return NewAscii(children[0]), nil
}
// Hex implements the sql function "hex" which returns the hexadecimal representation of the string or numeric value
type Hex struct {
*UnaryFunc
}
var _ sql.FunctionExpression = (*Hex)(nil)
func NewHex(arg sql.Expression) sql.Expression {
return &Hex{NewUnaryFunc(arg, "HEX", sql.Text)}
}
// Description implements sql.FunctionExpression
func (h *Hex) Description() string {
return "returns the hexadecimal representation of the string or numeric value."
}
// Eval implements the sql.Expression interface
func (h *Hex) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
arg, err := h.EvalChild(ctx, row)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
switch val := arg.(type) {
case string:
return hexForString(val), nil
case uint8, uint16, uint32, uint, int, int8, int16, int32, int64:
n, err := sql.Int64.Convert(arg)
if err != nil {
return nil, err
}
a := n.(int64)
if a < 0 {
return hexForNegativeInt64(a), nil
} else {
return fmt.Sprintf("%X", a), nil
}
case uint64:
return fmt.Sprintf("%X", val), nil
case float32:
return hexForFloat(float64(val))
case float64:
return hexForFloat(val)
case decimal.Decimal:
f, _ := val.Float64()
return hexForFloat(f)
case bool:
if val {
return "1", nil
}
return "0", nil
case time.Time:
s, err := formatDate("%Y-%m-%d %H:%i:%s", val)
if err != nil {
return nil, err
}
s += fractionOfSecString(val)
return hexForString(s), nil
case []byte:
return hexForString(string(val)), nil
default:
return nil, ErrInvalidArgument.New("crc32", fmt.Sprint(arg))
}
}
// WithChildren implements the sql.Expression interface
func (h *Hex) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(h, len(children), 1)
}
return NewHex(children[0]), nil
}
func hexChar(b byte) byte {
if b > 9 {
return b - 10 + byte('A')
}
return b + byte('0')
}
// MySQL expects the 64 bit 2s complement representation for negative integer values. Typical methods for converting a
// number to a string don't handle negative integer values in this way (strconv.FormatInt and fmt.Sprintf for example).
func hexForNegativeInt64(n int64) string {
// get a pointer to the int64s memory
mem := (*[8]byte)(unsafe.Pointer(&n))
// make a copy of the data that I can manipulate
bytes := *mem
// reverse the order for printing
for i := 0; i < 4; i++ {
bytes[i], bytes[7-i] = bytes[7-i], bytes[i]
}
// print the hex encoded bytes
return fmt.Sprintf("%X", bytes)
}
func hexForFloat(f float64) (string, error) {
if f < 0 {
f -= 0.5
n := int64(f)
return hexForNegativeInt64(n), nil
}
f += 0.5
n := uint64(f)
return fmt.Sprintf("%X", n), nil
}
func hexForString(val string) string {
buf := make([]byte, 0, 2*len(val))
// Do not change this to range, as range iterates over runes and not bytes
for i := 0; i < len(val); i++ {
c := val[i]
high := c / 16
low := c % 16
buf = append(buf, hexChar(high))
buf = append(buf, hexChar(low))
}
return string(buf)
}
// Unhex implements the sql function "unhex" which returns the integer representation of a hexadecimal string
type Unhex struct {
*UnaryFunc
}
var _ sql.FunctionExpression = (*Unhex)(nil)
func NewUnhex(arg sql.Expression) sql.Expression {
return &Unhex{NewUnaryFunc(arg, "UNHEX", sql.LongBlob)}
}
// Description implements sql.FunctionExpression
func (h *Unhex) Description() string {
return "returns a string containing hex representation of a number."
}
// Eval implements the sql.Expression interface
func (h *Unhex) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
arg, err := h.EvalChild(ctx, row)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
val, err := sql.LongBlob.Convert(arg)
if err != nil {
return nil, err
}
s := val.(string)
if len(s)%2 != 0 {
s = "0" + s
}
s = strings.ToUpper(s)
for _, c := range s {
if c < '0' || c > '9' && c < 'A' || c > 'F' {
return nil, nil
}
}
res, err := hex.DecodeString(s)
if err != nil {
return nil, err
}
return res, nil
}
// WithChildren implements the sql.Expression interface
func (h *Unhex) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(h, len(children), 1)
}
return NewUnhex(children[0]), nil
}
// MySQL expects the 64 bit 2s complement representation for negative integer values. Typical methods for converting a
// number to a string don't handle negative integer values in this way (strconv.FormatInt and fmt.Sprintf for example).
func binForNegativeInt64(n int64) string {
// get a pointer to the int64s memory
mem := (*[8]byte)(unsafe.Pointer(&n))
// make a copy of the data that I can manipulate
bytes := *mem
s := ""
for i := 7; i >= 0; i-- {
s += strconv.FormatInt(int64(bytes[i]), 2)
}
return s
}
// Bin implements the sql function "bin" which returns the binary representation of a number
type Bin struct {
*UnaryFunc
}
var _ sql.FunctionExpression = (*Bin)(nil)
func NewBin(arg sql.Expression) sql.Expression {
return &Bin{NewUnaryFunc(arg, "BIN", sql.Text)}
}
// FunctionName implements sql.FunctionExpression
func (b *Bin) FunctionName() string {
return "bin"
}
// Description implements sql.FunctionExpression
func (b *Bin) Description() string {
return "returns the binary representation of a number."
}
// Eval implements the sql.Expression interface
func (h *Bin) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
arg, err := h.EvalChild(ctx, row)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
switch val := arg.(type) {
case time.Time:
return strconv.FormatUint(uint64(val.Year()), 2), nil
case uint64:
return strconv.FormatUint(val, 2), nil
default:
n, err := sql.Int64.Convert(arg)
if err != nil {
return "0", nil
}
if n.(int64) < 0 {
return binForNegativeInt64(n.(int64)), nil
} else {
return strconv.FormatInt(n.(int64), 2), nil
}
}
}
// WithChildren implements the sql.Expression interface
func (h *Bin) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(h, len(children), 1)
}
return NewBin(children[0]), nil
}
// Bitlength implements the sql function "bit_length" which returns the data length of the argument in bits
type Bitlength struct {
*UnaryFunc
}
var _ sql.FunctionExpression = (*Bitlength)(nil)
func NewBitlength(arg sql.Expression) sql.Expression {
return &Bitlength{NewUnaryFunc(arg, "BIT_LENGTH", sql.Int32)}
}
// FunctionName implements sql.FunctionExpression
func (b *Bitlength) FunctionName() string {
return "bit_length"
}
// Description implements sql.FunctionExpression
func (b *Bitlength) Description() string {
return "returns the data length of the argument in bits."
}
// Eval implements the sql.Expression interface
func (h *Bitlength) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
arg, err := h.EvalChild(ctx, row)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
switch val := arg.(type) {
case uint8, int8, bool:
return 8, nil
case uint16, int16:
return 16, nil
case int, uint, uint32, int32, float32:
return 32, nil
case uint64, int64, float64:
return 64, nil
case string:
return 8 * len([]byte(val)), nil
case time.Time:
return 128, nil
}
return nil, ErrInvalidArgument.New("bit_length", fmt.Sprint(arg))
}
// WithChildren implements the sql.Expression interface
func (h *Bitlength) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(h, len(children), 1)
}
return NewBitlength(children[0]), nil
}