forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
478 lines (442 loc) · 13.6 KB
/
convert.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
// Copyright 2014 The ql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSES/QL-LICENSE file.
// Copyright 2015 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package types
import (
"math"
"strconv"
"strings"
"github.com/juju/errors"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/hack"
)
func truncateStr(str string, flen int) string {
if flen != UnspecifiedLength && len(str) > flen {
str = str[:flen]
}
return str
}
// UnsignedUpperBound indicates the max uint64 values of different mysql types.
var UnsignedUpperBound = map[byte]uint64{
mysql.TypeTiny: math.MaxUint8,
mysql.TypeShort: math.MaxUint16,
mysql.TypeInt24: mysql.MaxUint24,
mysql.TypeLong: math.MaxUint32,
mysql.TypeLonglong: math.MaxUint64,
mysql.TypeBit: math.MaxUint64,
mysql.TypeEnum: math.MaxUint64,
mysql.TypeSet: math.MaxUint64,
}
// SignedUpperBound indicates the max int64 values of different mysql types.
var SignedUpperBound = map[byte]int64{
mysql.TypeTiny: math.MaxInt8,
mysql.TypeShort: math.MaxInt16,
mysql.TypeInt24: mysql.MaxInt24,
mysql.TypeLong: math.MaxInt32,
mysql.TypeLonglong: math.MaxInt64,
}
// SignedLowerBound indicates the min int64 values of different mysql types.
var SignedLowerBound = map[byte]int64{
mysql.TypeTiny: math.MinInt8,
mysql.TypeShort: math.MinInt16,
mysql.TypeInt24: mysql.MinInt24,
mysql.TypeLong: math.MinInt32,
mysql.TypeLonglong: math.MinInt64,
}
// ConvertFloatToInt converts a float64 value to a int value.
func ConvertFloatToInt(fval float64, lowerBound, upperBound int64, tp byte) (int64, error) {
val := RoundFloat(fval)
if val < float64(lowerBound) {
return lowerBound, overflow(val, tp)
}
if val >= float64(upperBound) {
if val == float64(upperBound) {
return upperBound, nil
}
return upperBound, overflow(val, tp)
}
return int64(val), nil
}
// ConvertIntToInt converts an int value to another int value of different precision.
func ConvertIntToInt(val int64, lowerBound int64, upperBound int64, tp byte) (int64, error) {
if val < lowerBound {
return lowerBound, overflow(val, tp)
}
if val > upperBound {
return upperBound, overflow(val, tp)
}
return val, nil
}
// ConvertUintToInt converts an uint value to an int value.
func ConvertUintToInt(val uint64, upperBound int64, tp byte) (int64, error) {
if val > uint64(upperBound) {
return upperBound, overflow(val, tp)
}
return int64(val), nil
}
// ConvertIntToUint converts an int value to an uint value.
func ConvertIntToUint(val int64, upperBound uint64, tp byte) (uint64, error) {
if uint64(val) > upperBound {
return upperBound, overflow(val, tp)
}
return uint64(val), nil
}
// ConvertUintToUint converts an uint value to another uint value of different precision.
func ConvertUintToUint(val uint64, upperBound uint64, tp byte) (uint64, error) {
if val > upperBound {
return upperBound, overflow(val, tp)
}
return val, nil
}
// ConvertFloatToUint converts a float value to an uint value.
func ConvertFloatToUint(fval float64, upperBound uint64, tp byte) (uint64, error) {
val := RoundFloat(fval)
if val < 0 {
return uint64(int64(val)), overflow(val, tp)
}
if val > float64(upperBound) {
return upperBound, overflow(val, tp)
}
return uint64(val), nil
}
// StrToInt converts a string to an integer at the best-effort.
func StrToInt(sc *stmtctx.StatementContext, str string) (int64, error) {
str = strings.TrimSpace(str)
validPrefix, err := getValidIntPrefix(sc, str)
iVal, err1 := strconv.ParseInt(validPrefix, 10, 64)
if err1 != nil {
return iVal, ErrOverflow.GenByArgs("BIGINT", validPrefix)
}
return iVal, errors.Trace(err)
}
// StrToUint converts a string to an unsigned integer at the best-effortt.
func StrToUint(sc *stmtctx.StatementContext, str string) (uint64, error) {
str = strings.TrimSpace(str)
validPrefix, err := getValidIntPrefix(sc, str)
if validPrefix[0] == '+' {
validPrefix = validPrefix[1:]
}
uVal, err1 := strconv.ParseUint(validPrefix, 10, 64)
if err1 != nil {
return uVal, ErrOverflow.GenByArgs("BIGINT UNSIGNED", validPrefix)
}
return uVal, errors.Trace(err)
}
// StrToDateTime converts str to MySQL DateTime.
func StrToDateTime(sc *stmtctx.StatementContext, str string, fsp int) (Time, error) {
return ParseTime(sc, str, mysql.TypeDatetime, fsp)
}
// StrToDuration converts str to Duration. It returns Duration in normal case,
// and returns Time when str is in datetime format.
// when isDuration is true, the d is returned, when it is false, the t is returned.
// See https://dev.mysql.com/doc/refman/5.5/en/date-and-time-literals.html.
func StrToDuration(sc *stmtctx.StatementContext, str string, fsp int) (d Duration, t Time, isDuration bool, err error) {
str = strings.TrimSpace(str)
length := len(str)
if length > 0 && str[0] == '-' {
length--
}
// Timestamp format is 'YYYYMMDDHHMMSS' or 'YYMMDDHHMMSS', which length is 12.
// See #3923, it explains what we do here.
if length >= 12 {
t, err = StrToDateTime(sc, str, fsp)
if err == nil {
return d, t, false, nil
}
}
d, err = ParseDuration(str, fsp)
if ErrTruncatedWrongVal.Equal(err) {
err = sc.HandleTruncate(err)
}
return d, t, true, errors.Trace(err)
}
// NumberToDuration converts number to Duration.
func NumberToDuration(number int64, fsp int) (Duration, error) {
if number > TimeMaxValue {
// Try to parse DATETIME.
if number >= 10000000000 { // '2001-00-00 00-00-00'
if t, err := ParseDatetimeFromNum(nil, number); err == nil {
dur, err1 := t.ConvertToDuration()
return dur, errors.Trace(err1)
}
}
dur, err1 := MaxMySQLTime(fsp).ConvertToDuration()
terror.Log(err1)
return dur, ErrOverflow.GenByArgs("Duration", strconv.Itoa(int(number)))
} else if number < -TimeMaxValue {
dur, err1 := MaxMySQLTime(fsp).ConvertToDuration()
terror.Log(err1)
dur.Duration = -dur.Duration
return dur, ErrOverflow.GenByArgs("Duration", strconv.Itoa(int(number)))
}
var neg bool
if neg = number < 0; neg {
number = -number
}
if number/10000 > TimeMaxHour || number%100 >= 60 || (number/100)%100 >= 60 {
return ZeroDuration, errors.Trace(ErrInvalidTimeFormat.GenByArgs(number))
}
t := Time{Time: FromDate(0, 0, 0, int(number/10000), int((number/100)%100), int(number%100), 0), Type: mysql.TypeDuration, Fsp: fsp}
dur, err := t.ConvertToDuration()
if err != nil {
return ZeroDuration, errors.Trace(err)
}
if neg {
dur.Duration = -dur.Duration
}
return dur, nil
}
// getValidIntPrefix gets prefix of the string which can be successfully parsed as int.
func getValidIntPrefix(sc *stmtctx.StatementContext, str string) (string, error) {
floatPrefix, err := getValidFloatPrefix(sc, str)
if err != nil {
return floatPrefix, errors.Trace(err)
}
return floatStrToIntStr(floatPrefix)
}
// floatStrToIntStr converts a valid float string into valid integer string which can be parsed by
// strconv.ParseInt, we can't parse float first then convert it to string because precision will
// be lost.
func floatStrToIntStr(validFloat string) (string, error) {
var dotIdx = -1
var eIdx = -1
for i := 0; i < len(validFloat); i++ {
switch validFloat[i] {
case '.':
dotIdx = i
case 'e', 'E':
eIdx = i
}
}
if eIdx == -1 {
if dotIdx == -1 {
return validFloat, nil
}
return validFloat[:dotIdx], nil
}
var intCnt int
digits := make([]byte, 0, len(validFloat))
if dotIdx == -1 {
digits = append(digits, validFloat[:eIdx]...)
intCnt = len(digits)
} else {
digits = append(digits, validFloat[:dotIdx]...)
intCnt = len(digits)
digits = append(digits, validFloat[dotIdx+1:eIdx]...)
}
exp, err := strconv.Atoi(validFloat[eIdx+1:])
if err != nil {
return validFloat, errors.Trace(err)
}
if exp > 0 && int64(intCnt) > (math.MaxInt64-int64(exp)) {
// (exp + incCnt) overflows MaxInt64.
return validFloat, ErrOverflow.GenByArgs("BIGINT", validFloat)
}
intCnt += exp
if intCnt <= 0 {
return "0", nil
}
if intCnt == 1 && (digits[0] == '-' || digits[0] == '+') {
return "0", nil
}
var validInt string
if intCnt <= len(digits) {
validInt = string(digits[:intCnt])
} else {
// convert scientific notation decimal number
extraZeroCount := intCnt - len(digits)
if extraZeroCount > 20 {
// Return overflow to avoid allocating too much memory.
return validFloat, ErrOverflow.GenByArgs("BIGINT", validFloat)
}
validInt = string(digits) + strings.Repeat("0", extraZeroCount)
}
return validInt, nil
}
// StrToFloat converts a string to a float64 at the best-effort.
func StrToFloat(sc *stmtctx.StatementContext, str string) (float64, error) {
str = strings.TrimSpace(str)
validStr, err := getValidFloatPrefix(sc, str)
f, err1 := strconv.ParseFloat(validStr, 64)
if err1 != nil {
if err2, ok := err1.(*strconv.NumError); ok {
// value will truncate to MAX/MIN if out of range.
if err2.Err == strconv.ErrRange {
err1 = sc.HandleTruncate(ErrTruncatedWrongVal.GenByArgs("DOUBLE", str))
if math.IsInf(f, 1) {
f = math.MaxFloat64
} else if math.IsInf(f, -1) {
f = -math.MaxFloat64
}
}
}
return f, errors.Trace(err1)
}
return f, errors.Trace(err)
}
// ConvertJSONToInt casts JSON into int64.
func ConvertJSONToInt(sc *stmtctx.StatementContext, j json.BinaryJSON, unsigned bool) (int64, error) {
switch j.TypeCode {
case json.TypeCodeObject, json.TypeCodeArray:
return 0, nil
case json.TypeCodeLiteral:
switch j.Value[0] {
case json.LiteralNil, json.LiteralFalse:
return 0, nil
default:
return 1, nil
}
case json.TypeCodeInt64, json.TypeCodeUint64:
return j.GetInt64(), nil
case json.TypeCodeFloat64:
f := j.GetFloat64()
if !unsigned {
lBound := SignedLowerBound[mysql.TypeLonglong]
uBound := SignedUpperBound[mysql.TypeLonglong]
return ConvertFloatToInt(f, lBound, uBound, mysql.TypeDouble)
}
bound := UnsignedUpperBound[mysql.TypeLonglong]
u, err := ConvertFloatToUint(f, bound, mysql.TypeDouble)
return int64(u), errors.Trace(err)
case json.TypeCodeString:
return StrToInt(sc, hack.String(j.GetString()))
}
return 0, errors.New("Unknown type code in JSON")
}
// ConvertJSONToFloat casts JSON into float64.
func ConvertJSONToFloat(sc *stmtctx.StatementContext, j json.BinaryJSON) (float64, error) {
switch j.TypeCode {
case json.TypeCodeObject, json.TypeCodeArray:
return 0, nil
case json.TypeCodeLiteral:
switch j.Value[0] {
case json.LiteralNil, json.LiteralFalse:
return 0, nil
default:
return 1, nil
}
case json.TypeCodeInt64:
return float64(j.GetInt64()), nil
case json.TypeCodeUint64:
u, err := ConvertIntToUint(j.GetInt64(), UnsignedUpperBound[mysql.TypeLonglong], mysql.TypeLonglong)
return float64(u), errors.Trace(err)
case json.TypeCodeFloat64:
return j.GetFloat64(), nil
case json.TypeCodeString:
return StrToFloat(sc, hack.String(j.GetString()))
}
return 0, errors.New("Unknown type code in JSON")
}
// ConvertJSONToDecimal casts JSON into decimal.
func ConvertJSONToDecimal(sc *stmtctx.StatementContext, j json.BinaryJSON) (*MyDecimal, error) {
res := new(MyDecimal)
if j.TypeCode != json.TypeCodeString {
f64, err := ConvertJSONToFloat(sc, j)
if err != nil {
return res, errors.Trace(err)
}
err = res.FromFloat64(f64)
return res, errors.Trace(err)
}
err := sc.HandleTruncate(res.FromString([]byte(j.GetString())))
return res, errors.Trace(err)
}
// getValidFloatPrefix gets prefix of string which can be successfully parsed as float.
func getValidFloatPrefix(sc *stmtctx.StatementContext, s string) (valid string, err error) {
var (
sawDot bool
sawDigit bool
validLen int
eIdx int
)
for i := 0; i < len(s); i++ {
c := s[i]
if c == '+' || c == '-' {
if i != 0 && i != eIdx+1 { // "1e+1" is valid.
break
}
} else if c == '.' {
if sawDot || eIdx > 0 { // "1.1." or "1e1.1"
break
}
sawDot = true
if sawDigit { // "123." is valid.
validLen = i + 1
}
} else if c == 'e' || c == 'E' {
if !sawDigit { // "+.e"
break
}
if eIdx != 0 { // "1e5e"
break
}
eIdx = i
} else if c < '0' || c > '9' {
break
} else {
sawDigit = true
validLen = i + 1
}
}
valid = s[:validLen]
if valid == "" {
valid = "0"
}
if validLen == 0 || validLen != len(s) {
err = errors.Trace(handleTruncateError(sc))
}
return valid, err
}
// ToString converts an interface to a string.
func ToString(value interface{}) (string, error) {
switch v := value.(type) {
case bool:
if v {
return "1", nil
}
return "0", nil
case int:
return strconv.FormatInt(int64(v), 10), nil
case int64:
return strconv.FormatInt(v, 10), nil
case uint64:
return strconv.FormatUint(v, 10), nil
case float32:
return strconv.FormatFloat(float64(v), 'f', -1, 32), nil
case float64:
return strconv.FormatFloat(v, 'f', -1, 64), nil
case string:
return v, nil
case []byte:
return string(v), nil
case Time:
return v.String(), nil
case Duration:
return v.String(), nil
case *MyDecimal:
return v.String(), nil
case BinaryLiteral:
return v.ToString(), nil
case Enum:
return v.String(), nil
case Set:
return v.String(), nil
default:
return "", errors.Errorf("cannot convert %v(type %T) to string", value, value)
}
}