forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.go
608 lines (544 loc) · 19.1 KB
/
strings.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// Copyright 2022 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 types
import (
"fmt"
"reflect"
"strconv"
strings2 "strings"
"time"
"unicode/utf8"
"github.com/dolthub/vitess/go/sqltypes"
"github.com/dolthub/vitess/go/vt/proto/query"
"github.com/shopspring/decimal"
"gopkg.in/src-d/go-errors.v1"
"github.com/gabereiser/go-mysql-server/internal/strings"
"github.com/gabereiser/go-mysql-server/sql"
"github.com/gabereiser/go-mysql-server/sql/encodings"
)
const (
charBinaryMax = 255
varcharVarbinaryMax = 65_535
TinyTextBlobMax = charBinaryMax
TextBlobMax = varcharVarbinaryMax
MediumTextBlobMax = 16_777_215
LongTextBlobMax = int64(4_294_967_295)
)
var (
// ErrLengthTooLarge is thrown when a string's length is too large given the other parameters.
ErrLengthTooLarge = errors.NewKind("length is %v but max allowed is %v")
ErrLengthBeyondLimit = errors.NewKind("string '%v' is too large for column '%v'")
ErrBinaryCollation = errors.NewKind("binary types must have the binary collation")
TinyText = MustCreateStringWithDefaults(sqltypes.Text, TinyTextBlobMax)
Text = MustCreateStringWithDefaults(sqltypes.Text, TextBlobMax)
MediumText = MustCreateStringWithDefaults(sqltypes.Text, MediumTextBlobMax)
LongText = MustCreateStringWithDefaults(sqltypes.Text, LongTextBlobMax)
TinyBlob = MustCreateBinary(sqltypes.Blob, TinyTextBlobMax)
Blob = MustCreateBinary(sqltypes.Blob, TextBlobMax)
MediumBlob = MustCreateBinary(sqltypes.Blob, MediumTextBlobMax)
LongBlob = MustCreateBinary(sqltypes.Blob, LongTextBlobMax)
stringValueType = reflect.TypeOf(string(""))
byteValueType = reflect.TypeOf(([]byte)(nil))
)
type StringType struct {
baseType query.Type
maxCharLength int64
maxByteLength int64
maxResponseByteLength uint32
collation sql.CollationID
}
var _ sql.StringType = StringType{}
var _ sql.TypeWithCollation = StringType{}
// CreateString creates a new StringType based on the specified type, length, and collation. Length is interpreted as
// the length of bytes in the new StringType for SQL types that are based on bytes (i.e. TEXT, BLOB, BINARY, and
// VARBINARY). For all other char-based SQL types, length is interpreted as the length of chars in the new
// StringType (i.e. CHAR, and VARCHAR).
func CreateString(baseType query.Type, length int64, collation sql.CollationID) (sql.StringType, error) {
// TODO: remove character set and collation validity checks once all collations have been implemented (delete errors as well)
if collation.CharacterSet().Encoder() == nil {
return nil, sql.ErrCharSetNotYetImplementedTemp.New(collation.CharacterSet().Name())
} else if collation.Sorter() == nil {
return nil, sql.ErrCollationNotYetImplementedTemp.New(collation.Name())
}
// Check the base type first and fail immediately if it's unknown
switch baseType {
case sqltypes.Char, sqltypes.Binary, sqltypes.VarChar, sqltypes.VarBinary, sqltypes.Text, sqltypes.Blob:
default:
return nil, sql.ErrInvalidBaseType.New(baseType.String(), "string")
}
// We accept a length of zero, but a negative length is not valid
if length < 0 {
return nil, fmt.Errorf("length of %v is less than the minimum of 0", length)
}
switch baseType {
case sqltypes.Binary, sqltypes.VarBinary, sqltypes.Blob:
if collation != sql.Collation_binary {
return nil, ErrBinaryCollation.New(collation.Name, sql.Collation_binary)
}
}
// If the CharacterSet is binary, then we convert the type to the binary equivalent
if collation.Equals(sql.Collation_binary) {
switch baseType {
case sqltypes.Char:
baseType = sqltypes.Binary
case sqltypes.VarChar:
baseType = sqltypes.VarBinary
case sqltypes.Text:
baseType = sqltypes.Blob
}
}
// Determine the max byte length and max char length based on whether the base type is byte-based or char-based
charsetMaxLength := collation.CharacterSet().MaxLength()
maxCharLength := length
maxByteLength := length
switch baseType {
case sqltypes.Char, sqltypes.VarChar:
maxByteLength = length * charsetMaxLength
case sqltypes.Binary, sqltypes.VarBinary, sqltypes.Text, sqltypes.Blob:
maxCharLength = length / charsetMaxLength
}
maxResponseByteLength := maxByteLength
// Make sure that length is valid depending on the base type, since they each handle lengths differently
switch baseType {
case sqltypes.Char:
if maxCharLength > charBinaryMax {
return nil, ErrLengthTooLarge.New(length, charBinaryMax)
}
case sqltypes.VarChar:
if maxCharLength > varcharVarbinaryMax {
return nil, ErrLengthTooLarge.New(length, varcharVarbinaryMax/charsetMaxLength)
}
case sqltypes.Binary:
if maxByteLength > charBinaryMax {
return nil, ErrLengthTooLarge.New(length, charBinaryMax)
}
case sqltypes.VarBinary:
// VarBinary fields transmitted over the wire could be for a VarBinary field,
// or a JSON field, so we validate against JSON's larger limit (1GB)
// instead of VarBinary's smaller limit (65k).
if maxByteLength > MaxJsonFieldByteLength {
return nil, ErrLengthTooLarge.New(length, MaxJsonFieldByteLength/charsetMaxLength)
}
case sqltypes.Text, sqltypes.Blob:
if maxByteLength > LongTextBlobMax {
return nil, ErrLengthTooLarge.New(length, LongTextBlobMax)
}
if maxByteLength <= TinyTextBlobMax {
maxByteLength = TinyTextBlobMax
maxCharLength = TinyTextBlobMax / charsetMaxLength
} else if maxByteLength <= TextBlobMax {
maxByteLength = TextBlobMax
maxCharLength = TextBlobMax / charsetMaxLength
} else if maxByteLength <= MediumTextBlobMax {
maxByteLength = MediumTextBlobMax
maxCharLength = MediumTextBlobMax / charsetMaxLength
} else {
maxByteLength = LongTextBlobMax
maxCharLength = LongTextBlobMax / charsetMaxLength
}
maxResponseByteLength = maxByteLength
if baseType == sqltypes.Text && maxByteLength != LongTextBlobMax {
// For TEXT types, MySQL returns the maxByteLength multiplied by the size of the largest
// multibyte character in the associated charset for the maximum field bytes in the response
// metadata. It seems like returning the maxByteLength would be sufficient, but we do this to
// emulate MySQL's behavior exactly.
// The one exception is LongText types, which cannot be multiplied by a multibyte char multiplier,
// since the max bytes field in a column definition response over the wire is a uint32 and multiplying
// longTextBlobMax by anything over 1 would cause it to overflow.
maxResponseByteLength = maxByteLength * charsetMaxLength
}
}
return StringType{baseType, maxCharLength, maxByteLength, uint32(maxResponseByteLength), collation}, nil
}
// MustCreateString is the same as CreateString except it panics on errors.
func MustCreateString(baseType query.Type, length int64, collation sql.CollationID) sql.StringType {
st, err := CreateString(baseType, length, collation)
if err != nil {
panic(err)
}
return st
}
// CreateStringWithDefaults creates a StringType with the default character set and collation of the given size.
func CreateStringWithDefaults(baseType query.Type, length int64) (sql.StringType, error) {
return CreateString(baseType, length, sql.Collation_Default)
}
// MustCreateStringWithDefaults creates a StringType with the default CharacterSet and Collation.
func MustCreateStringWithDefaults(baseType query.Type, length int64) sql.StringType {
return MustCreateString(baseType, length, sql.Collation_Default)
}
// CreateBinary creates a StringType with a binary collation and character set of the given size.
func CreateBinary(baseType query.Type, lengthHint int64) (sql.StringType, error) {
return CreateString(baseType, lengthHint, sql.Collation_binary)
}
// MustCreateBinary is the same as CreateBinary except it panics on errors.
func MustCreateBinary(baseType query.Type, lengthHint int64) sql.StringType {
return MustCreateString(baseType, lengthHint, sql.Collation_binary)
}
// CreateTinyText creates a TINYTEXT with the given collation.
func CreateTinyText(collation sql.CollationID) sql.StringType {
return MustCreateString(sqltypes.Text, TinyTextBlobMax/collation.CharacterSet().MaxLength(), collation)
}
// CreateText creates a TEXT with the given collation.
func CreateText(collation sql.CollationID) sql.StringType {
return MustCreateString(sqltypes.Text, TextBlobMax/collation.CharacterSet().MaxLength(), collation)
}
// CreateMediumText creates a MEDIUMTEXT with the given collation.
func CreateMediumText(collation sql.CollationID) sql.StringType {
return MustCreateString(sqltypes.Text, MediumTextBlobMax/collation.CharacterSet().MaxLength(), collation)
}
// CreateLongText creates a LONGTEXT with the given collation.
func CreateLongText(collation sql.CollationID) sql.StringType {
return MustCreateString(sqltypes.Text, LongTextBlobMax/collation.CharacterSet().MaxLength(), collation)
}
// MaxTextResponseByteLength implements the Type interface
func (t StringType) MaxTextResponseByteLength() uint32 {
return t.maxResponseByteLength
}
func (t StringType) Length() int64 {
return t.maxCharLength
}
// Compare implements Type interface.
func (t StringType) Compare(a interface{}, b interface{}) (int, error) {
if hasNulls, res := CompareNulls(a, b); hasNulls {
return res, nil
}
var as string
var bs string
var ok bool
if as, ok = a.(string); !ok {
ai, err := t.Convert(a)
if err != nil {
return 0, err
}
if IsBinaryType(t) {
as = encodings.BytesToString(ai.([]byte))
} else {
as = ai.(string)
}
}
if bs, ok = b.(string); !ok {
bi, err := t.Convert(b)
if err != nil {
return 0, err
}
if IsBinaryType(t) {
bs = encodings.BytesToString(bi.([]byte))
} else {
bs = bi.(string)
}
}
encoder := t.collation.CharacterSet().Encoder()
getRuneWeight := t.collation.Sorter()
for len(as) > 0 && len(bs) > 0 {
ar, aRead := encoder.NextRune(as)
br, bRead := encoder.NextRune(bs)
if aRead == 0 || bRead == 0 || aRead == utf8.RuneError || bRead == utf8.RuneError {
// TODO: return a real error
return 0, fmt.Errorf("malformed string encountered while comparing")
}
aWeight := getRuneWeight(ar)
bWeight := getRuneWeight(br)
if aWeight < bWeight {
return -1, nil
} else if aWeight > bWeight {
return 1, nil
}
as = as[aRead:]
bs = bs[bRead:]
}
// Strings are equal up to the compared length, so shorter strings sort before longer strings
if len(as) < len(bs) {
return -1, nil
} else if len(as) > len(bs) {
return 1, nil
} else {
return 0, nil
}
}
// Convert implements Type interface.
func (t StringType) Convert(v interface{}) (interface{}, error) {
if v == nil {
return nil, nil
}
val, err := ConvertToString(v, t)
if err != nil {
return nil, err
}
if IsBinaryType(t) {
return []byte(val), nil
}
return val, nil
}
func ConvertToString(v interface{}, t sql.StringType) (string, error) {
var val string
switch s := v.(type) {
case bool:
val = strconv.FormatBool(s)
case float64:
val = strconv.FormatFloat(s, 'f', -1, 64)
case float32:
val = strconv.FormatFloat(float64(s), 'f', -1, 32)
case int:
val = strconv.FormatInt(int64(s), 10)
case int8:
val = strconv.FormatInt(int64(s), 10)
case int16:
val = strconv.FormatInt(int64(s), 10)
case int32:
val = strconv.FormatInt(int64(s), 10)
case int64:
val = strconv.FormatInt(s, 10)
case uint:
val = strconv.FormatUint(uint64(s), 10)
case uint8:
val = strconv.FormatUint(uint64(s), 10)
case uint16:
val = strconv.FormatUint(uint64(s), 10)
case uint32:
val = strconv.FormatUint(uint64(s), 10)
case uint64:
val = strconv.FormatUint(s, 10)
case string:
val = s
case []byte:
val = string(s)
case time.Time:
val = s.Format(sql.TimestampDatetimeLayout)
case decimal.Decimal:
val = s.StringFixed(s.Exponent() * -1)
case decimal.NullDecimal:
if !s.Valid {
return "", nil
}
val = s.Decimal.String()
case JSONValue:
str, err := s.ToString(nil)
if err != nil {
return "", err
}
val, err = strings.Unquote(str)
if err != nil {
return "", err
}
case GeometryValue:
return string(s.Serialize()), nil
default:
return "", sql.ErrConvertToSQL.New(t)
}
s := t.(StringType)
if s.baseType == sqltypes.Text {
// for TEXT types, we use the byte length instead of the character length
if int64(len(val)) > s.maxByteLength {
return "", ErrLengthBeyondLimit.New(val, t.String())
}
} else {
if t.CharacterSet().MaxLength() == 1 {
// if the character set only has a max size of 1, we can just count the bytes
if int64(len(val)) > s.maxCharLength {
return "", ErrLengthBeyondLimit.New(val, t.String())
}
} else {
// TODO: this should count the string's length properly according to the character set
// convert 'val' string to rune to count the character length, not byte length
if int64(len([]rune(val))) > s.maxCharLength {
return "", ErrLengthBeyondLimit.New(val, t.String())
}
}
}
if s.baseType == sqltypes.Binary {
val += strings2.Repeat(string([]byte{0}), int(s.maxCharLength)-len(val))
}
return val, nil
}
// ConvertToCollatedString returns the given interface as a string, along with its collation. If the Type possess a
// collation, then that collation is returned. If the Type does not possess a collation (such as an integer), then the
// value is converted to a string and the default collation is used. If the value is already a string then no additional
// conversions are made. If the value is a byte slice then a non-copying conversion is made, which means that the
// original byte slice MUST NOT be modified after being passed to this function. If modifications need to be made, then
// you must allocate a new byte slice and pass that new one in.
func ConvertToCollatedString(val interface{}, typ sql.Type) (string, sql.CollationID, error) {
var content string
var collation sql.CollationID
var err error
if typeWithCollation, ok := typ.(sql.TypeWithCollation); ok {
collation = typeWithCollation.Collation()
if strVal, ok := val.(string); ok {
content = strVal
} else if byteVal, ok := val.([]byte); ok {
content = encodings.BytesToString(byteVal)
} else {
val, err = LongText.Convert(val)
if err != nil {
return "", sql.Collation_Unspecified, err
}
content = val.(string)
}
} else {
collation = sql.Collation_Default
val, err = LongText.Convert(val)
if err != nil {
return "", sql.Collation_Unspecified, err
}
content = val.(string)
}
return content, collation, nil
}
// MustConvert implements the Type interface.
func (t StringType) MustConvert(v interface{}) interface{} {
value, err := t.Convert(v)
if err != nil {
panic(err)
}
return value
}
// Equals implements the Type interface.
func (t StringType) Equals(otherType sql.Type) bool {
if ot, ok := otherType.(StringType); ok {
return t.baseType == ot.baseType && t.collation == ot.collation && t.maxCharLength == ot.maxCharLength
}
return false
}
// Promote implements the Type interface.
func (t StringType) Promote() sql.Type {
switch t.baseType {
case sqltypes.Char, sqltypes.VarChar, sqltypes.Text:
return MustCreateString(sqltypes.Text, LongTextBlobMax, t.collation)
case sqltypes.Binary, sqltypes.VarBinary, sqltypes.Blob:
return LongBlob
default:
panic(sql.ErrInvalidBaseType.New(t.baseType.String(), "string"))
}
}
// SQL implements Type interface.
func (t StringType) SQL(ctx *sql.Context, dest []byte, v interface{}) (sqltypes.Value, error) {
if v == nil {
return sqltypes.NULL, nil
}
var val []byte
if IsBinaryType(t) {
v, err := t.Convert(v)
if err != nil {
return sqltypes.Value{}, err
}
val = AppendAndSliceBytes(dest, v.([]byte))
} else {
v, err := ConvertToString(v, t)
if err != nil {
return sqltypes.Value{}, err
}
resultCharset := ctx.GetCharacterSetResults()
if resultCharset == sql.CharacterSet_Unspecified || resultCharset == sql.CharacterSet_binary {
resultCharset = t.collation.CharacterSet()
}
encodedBytes, ok := resultCharset.Encoder().Encode(encodings.StringToBytes(v))
if !ok {
return sqltypes.Value{}, sql.ErrCharSetFailedToEncode.New(t.collation.CharacterSet().Name())
}
val = AppendAndSliceBytes(dest, encodedBytes)
}
return sqltypes.MakeTrusted(t.baseType, val), nil
}
// String implements Type interface.
func (t StringType) String() string {
var s string
switch t.baseType {
case sqltypes.Char:
s = fmt.Sprintf("char(%v)", t.maxCharLength)
case sqltypes.Binary:
s = fmt.Sprintf("binary(%v)", t.maxCharLength)
case sqltypes.VarChar:
s = fmt.Sprintf("varchar(%v)", t.maxCharLength)
case sqltypes.VarBinary:
s = fmt.Sprintf("varbinary(%v)", t.maxCharLength)
case sqltypes.Text:
if t.maxByteLength <= TinyTextBlobMax {
s = "tinytext"
} else if t.maxByteLength <= TextBlobMax {
s = "text"
} else if t.maxByteLength <= MediumTextBlobMax {
s = "mediumtext"
} else {
s = "longtext"
}
case sqltypes.Blob:
if t.maxByteLength <= TinyTextBlobMax {
s = "tinyblob"
} else if t.maxByteLength <= TextBlobMax {
s = "blob"
} else if t.maxByteLength <= MediumTextBlobMax {
s = "mediumblob"
} else {
s = "longblob"
}
}
if t.CharacterSet() != sql.CharacterSet_binary {
if t.CharacterSet() != sql.Collation_Default.CharacterSet() {
s += " CHARACTER SET " + t.CharacterSet().String()
}
if t.collation != sql.Collation_Default {
s += " COLLATE " + t.collation.Name()
}
}
return s
}
// Type implements Type interface.
func (t StringType) Type() query.Type {
return t.baseType
}
// ValueType implements Type interface.
func (t StringType) ValueType() reflect.Type {
if IsBinaryType(t) {
return byteValueType
}
return stringValueType
}
// Zero implements Type interface.
func (t StringType) Zero() interface{} {
return ""
}
func (t StringType) CharacterSet() sql.CharacterSetID {
return t.collation.CharacterSet()
}
func (t StringType) Collation() sql.CollationID {
return t.collation
}
// WithNewCollation implements TypeWithCollation interface.
func (t StringType) WithNewCollation(collation sql.CollationID) (sql.Type, error) {
// Blobs are special as, although they use collations, they don't change like a standard collated type
if t.baseType == sqltypes.Blob || t.baseType == sqltypes.Binary || t.baseType == sqltypes.VarBinary {
return t, nil
}
return CreateString(t.baseType, t.maxCharLength, collation)
}
// MaxCharacterLength is the maximum character length for this type.
func (t StringType) MaxCharacterLength() int64 {
return t.maxCharLength
}
// MaxByteLength is the maximum number of bytes that may be consumed by a string that conforms to this type.
func (t StringType) MaxByteLength() int64 {
return t.maxByteLength
}
// TODO: move me
func AppendAndSliceString(buffer []byte, addition string) (slice []byte) {
stop := len(buffer)
buffer = append(buffer, addition...)
slice = buffer[stop:]
return
}
func AppendAndSliceBytes(buffer, addition []byte) (slice []byte) {
stop := len(buffer)
buffer = append(buffer, addition...)
slice = buffer[stop:]
return
}