-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
column.go
710 lines (654 loc) · 23.6 KB
/
column.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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
// 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,
// 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.
// Copyright 2016 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.
package table
import (
"fmt"
"strconv"
"strings"
"time"
"unicode"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/charset"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
field_types "github.com/pingcap/tidb/parser/types"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/hack"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/timeutil"
"go.uber.org/zap"
)
// Column provides meta data describing a table column.
type Column struct {
*model.ColumnInfo
// If this column is a generated column, the expression will be stored here.
GeneratedExpr ast.ExprNode
// If this column has default expr value, this expression will be stored here.
DefaultExpr ast.ExprNode
}
// String implements fmt.Stringer interface.
func (c *Column) String() string {
ans := []string{c.Name.O, types.TypeToStr(c.Tp, c.Charset)}
if mysql.HasAutoIncrementFlag(c.Flag) {
ans = append(ans, "AUTO_INCREMENT")
}
if mysql.HasNotNullFlag(c.Flag) {
ans = append(ans, "NOT NULL")
}
return strings.Join(ans, " ")
}
// ToInfo casts Column to model.ColumnInfo
// NOTE: DONT modify return value.
func (c *Column) ToInfo() *model.ColumnInfo {
return c.ColumnInfo
}
// FindCol finds column in cols by name.
func FindCol(cols []*Column, name string) *Column {
for _, col := range cols {
if strings.EqualFold(col.Name.O, name) {
return col
}
}
return nil
}
// FindColLowerCase finds column in cols by name. It assumes the name is lowercase.
func FindColLowerCase(cols []*Column, name string) *Column {
for _, col := range cols {
if col.Name.L == name {
return col
}
}
return nil
}
// ToColumn converts a *model.ColumnInfo to *Column.
func ToColumn(col *model.ColumnInfo) *Column {
return &Column{
col,
nil,
nil,
}
}
// FindCols finds columns in cols by names.
// If pkIsHandle is false and name is ExtraHandleName, the extra handle column will be added.
// If any columns don't match, return nil and the first missing column's name.
// Please consider FindColumns() first for a better performance.
func FindCols(cols []*Column, names []string, pkIsHandle bool) ([]*Column, string) {
var rcols []*Column
for _, name := range names {
col := FindCol(cols, name)
if col != nil {
rcols = append(rcols, col)
} else if name == model.ExtraHandleName.L && !pkIsHandle {
col := &Column{}
col.ColumnInfo = model.NewExtraHandleColInfo()
col.ColumnInfo.Offset = len(cols)
rcols = append(rcols, col)
} else {
return nil, name
}
}
return rcols, ""
}
// FindColumns finds columns in cols by names with a better performance than FindCols().
// It assumes names are lowercase.
func FindColumns(cols []*Column, names []string, pkIsHandle bool) (foundCols []*Column, missingOffset int) {
var rcols []*Column
for i, name := range names {
col := FindColLowerCase(cols, name)
if col != nil {
rcols = append(rcols, col)
} else if name == model.ExtraHandleName.L && !pkIsHandle {
col := &Column{}
col.ColumnInfo = model.NewExtraHandleColInfo()
col.ColumnInfo.Offset = len(cols)
rcols = append(rcols, col)
} else {
return nil, i
}
}
return rcols, -1
}
// FindOnUpdateCols finds columns which have OnUpdateNow flag.
func FindOnUpdateCols(cols []*Column) []*Column {
var rcols []*Column
for _, col := range cols {
if mysql.HasOnUpdateNowFlag(col.Flag) {
rcols = append(rcols, col)
}
}
return rcols
}
// truncateTrailingSpaces truncates trailing spaces for CHAR[(M)] column.
// fix: https://github.com/pingcap/tidb/issues/3660
func truncateTrailingSpaces(v *types.Datum) {
if v.Kind() == types.KindNull {
return
}
b := v.GetBytes()
length := len(b)
for length > 0 && b[length-1] == ' ' {
length--
}
b = b[:length]
str := string(hack.String(b))
v.SetString(str, v.Collation())
}
func handleWrongCharsetValue(ctx sessionctx.Context, col *model.ColumnInfo, str []byte, i int) error {
sc := ctx.GetSessionVars().StmtCtx
var strval strings.Builder
for j := 0; j < 6; j++ {
if len(str) > (i + j) {
if str[i+j] > unicode.MaxASCII {
fmt.Fprintf(&strval, "\\x%X", str[i+j])
} else {
strval.WriteRune(rune(str[i+j]))
}
}
}
if len(str) > i+6 {
strval.WriteString(`...`)
}
// TODO: Add 'at row %d'
err := ErrTruncatedWrongValueForField.FastGen("Incorrect string value '%s' for column '%s'", strval.String(), col.Name)
logutil.BgLogger().Error("incorrect string value", zap.Uint64("conn", ctx.GetSessionVars().ConnectionID), zap.Error(err))
err = sc.HandleTruncate(err)
return err
}
// handleZeroDatetime handles Timestamp/Datetime/Date zero date and invalid dates.
// Currently only called from CastValue.
// returns:
// value (possibly adjusted)
// boolean; true if break error/warning handling in CastValue and return what was returned from this
// error
func handleZeroDatetime(ctx sessionctx.Context, col *model.ColumnInfo, casted types.Datum, str string, tmIsInvalid bool) (types.Datum, bool, error) {
sc := ctx.GetSessionVars().StmtCtx
tm := casted.GetMysqlTime()
mode := ctx.GetSessionVars().SQLMode
var (
zeroV types.Time
zeroT string
)
switch col.Tp {
case mysql.TypeDate:
zeroV, zeroT = types.ZeroDate, types.DateStr
case mysql.TypeDatetime:
zeroV, zeroT = types.ZeroDatetime, types.DateTimeStr
case mysql.TypeTimestamp:
zeroV, zeroT = types.ZeroTimestamp, types.TimestampStr
}
// ref https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#sqlmode_no_zero_date
// if NO_ZERO_DATE is not enabled, '0000-00-00' is permitted and inserts produce no warning
// if NO_ZERO_DATE is enabled, '0000-00-00' is permitted and inserts produce a warning
// If NO_ZERO_DATE mode and strict mode are enabled, '0000-00-00' is not permitted and inserts produce an error, unless IGNORE is given as well. For INSERT IGNORE and UPDATE IGNORE, '0000-00-00' is permitted and inserts produce a warning.
// if NO_ZERO_IN_DATE is not enabled, dates with zero parts are permitted and inserts produce no warning
// if NO_ZERO_IN_DATE is enabled, dates with zero parts are inserted as '0000-00-00' and produce a warning
// If NO_ZERO_IN_DATE mode and strict mode are enabled, dates with zero parts are not permitted and inserts produce an error, unless IGNORE is given as well. For INSERT IGNORE and UPDATE IGNORE, dates with zero parts are inserted as '0000-00-00' and produce a warning.
ignoreErr := sc.DupKeyAsWarning
// Timestamp in MySQL is since EPOCH 1970-01-01 00:00:00 UTC and can by definition not have invalid dates!
// Zero date is special for MySQL timestamp and *NOT* 1970-01-01 00:00:00, but 0000-00-00 00:00:00!
// in MySQL 8.0, the Timestamp's case is different to Datetime/Date, as shown below:
//
// | | NZD | NZD|ST | ELSE | ELSE|ST |
// | ------------ | ----------------- | ------- | ----------------- | -------- |
// | `0000-00-01` | Truncate + Warning| Error | Truncate + Warning| Error |
// | `0000-00-00` | Success + Warning | Error | Success | Success |
//
// * **NZD**: NO_ZERO_DATE_MODE
// * **ST**: STRICT_TRANS_TABLES
// * **ELSE**: empty or NO_ZERO_IN_DATE_MODE
if tm.IsZero() && col.Tp == mysql.TypeTimestamp {
innerErr := types.ErrWrongValue.GenWithStackByArgs(zeroT, str)
if mode.HasStrictMode() && !ignoreErr && (tmIsInvalid || mode.HasNoZeroDateMode()) {
return types.NewDatum(zeroV), true, innerErr
}
if tmIsInvalid || mode.HasNoZeroDateMode() {
sc.AppendWarning(innerErr)
}
return types.NewDatum(zeroV), true, nil
} else if tmIsInvalid && col.Tp == mysql.TypeTimestamp {
// Prevent from being stored! Invalid timestamp!
if mode.HasStrictMode() {
return types.NewDatum(zeroV), true, types.ErrWrongValue.GenWithStackByArgs(zeroT, str)
}
// no strict mode, truncate to 0000-00-00 00:00:00
sc.AppendWarning(types.ErrWrongValue.GenWithStackByArgs(zeroT, str))
return types.NewDatum(zeroV), true, nil
} else if tm.IsZero() || tm.InvalidZero() {
if tm.IsZero() {
// Don't care NoZeroDate mode if time val is invalid.
if !tmIsInvalid && !mode.HasNoZeroDateMode() {
return types.NewDatum(zeroV), true, nil
}
} else if tm.InvalidZero() {
if !mode.HasNoZeroInDateMode() {
return casted, true, nil
}
}
innerErr := types.ErrWrongValue.GenWithStackByArgs(zeroT, str)
if mode.HasStrictMode() && !ignoreErr {
return types.NewDatum(zeroV), true, innerErr
}
// TODO: as in MySQL 8.0's implement, warning message is `types.ErrWarnDataOutOfRange`,
// but this error message need a `rowIdx` argument, in this context, the `rowIdx` is missing.
// And refactor this function seems too complicated, so we set the warning message the same to error's.
sc.AppendWarning(innerErr)
return types.NewDatum(zeroV), true, nil
}
return casted, false, nil
}
// CastValue casts a value based on column type.
// If forceIgnoreTruncate is true, truncated errors will be ignored.
// If returnErr is true, directly return any conversion errors.
// It's safe now and it's the same as the behavior of select statement.
// Set it to true only in FillVirtualColumnValue and UnionScanExec.Next()
// If the handle of err is changed latter, the behavior of forceIgnoreTruncate also need to change.
// TODO: change the third arg to TypeField. Not pass ColumnInfo.
func CastValue(ctx sessionctx.Context, val types.Datum, col *model.ColumnInfo, returnErr, forceIgnoreTruncate bool) (casted types.Datum, err error) {
sc := ctx.GetSessionVars().StmtCtx
casted, err = val.ConvertTo(sc, &col.FieldType)
// TODO: make sure all truncate errors are handled by ConvertTo.
if returnErr && err != nil {
return casted, err
}
if err != nil && types.ErrTruncated.Equal(err) && col.Tp != mysql.TypeSet && col.Tp != mysql.TypeEnum {
str, err1 := val.ToString()
if err1 != nil {
logutil.BgLogger().Warn("Datum ToString failed", zap.Stringer("Datum", val), zap.Error(err1))
}
err = types.ErrTruncatedWrongVal.GenWithStackByArgs(col.FieldType.CompactStr(), str)
} else if (sc.InInsertStmt || sc.InUpdateStmt) && !casted.IsNull() &&
(val.Kind() != types.KindMysqlTime || !val.GetMysqlTime().IsZero()) &&
(col.Tp == mysql.TypeDate || col.Tp == mysql.TypeDatetime || col.Tp == mysql.TypeTimestamp) {
str, err1 := val.ToString()
if err1 != nil {
logutil.BgLogger().Warn("Datum ToString failed", zap.Stringer("Datum", val), zap.Error(err1))
str = val.GetString()
}
if innCasted, exit, innErr := handleZeroDatetime(ctx, col, casted, str, types.ErrWrongValue.Equal(err)); exit {
return innCasted, innErr
}
}
err = sc.HandleTruncate(err)
if forceIgnoreTruncate {
err = nil
} else if err != nil {
return casted, err
}
if col.Tp == mysql.TypeString && !types.IsBinaryStr(&col.FieldType) {
truncateTrailingSpaces(&casted)
}
err = validateStringDatum(ctx, &val, &casted, col)
if forceIgnoreTruncate {
err = nil
}
return casted, err
}
func validateStringDatum(ctx sessionctx.Context, origin, casted *types.Datum, col *model.ColumnInfo) error {
// Only strings are need to validate.
if !types.IsString(col.Tp) {
return nil
}
fromBinary := origin.Kind() == types.KindBinaryLiteral ||
(origin.Kind() == types.KindString && origin.Collation() == charset.CollationBin)
toBinary := types.IsTypeBlob(col.Tp) || col.Charset == charset.CharsetBin
if fromBinary && toBinary {
return nil
}
enc := charset.FindEncoding(col.Charset)
// Skip utf8 check if possible.
if enc.Tp() == charset.EncodingTpUTF8 && ctx.GetSessionVars().SkipUTF8Check {
return nil
}
// Skip ascii check if possible.
if enc.Tp() == charset.EncodingTpASCII && ctx.GetSessionVars().SkipASCIICheck {
return nil
}
if col.Charset == charset.CharsetUTF8 && config.GetGlobalConfig().CheckMb4ValueInUTF8 {
// Use a strict mode implementation. 4 bytes characters are invalid.
enc = charset.EncodingUTF8MB3StrictImpl
}
if fromBinary {
src := casted.GetBytes()
encBytes, err := enc.Transform(nil, src, charset.OpDecode)
if err != nil {
casted.SetBytesAsString(encBytes, charset.CollationUTF8MB4, 0)
nSrc := charset.CountValidBytesDecode(enc, src)
return handleWrongCharsetValue(ctx, col, src, nSrc)
}
casted.SetBytesAsString(encBytes, charset.CollationUTF8MB4, 0)
return nil
}
// Check if the string is valid in the given column charset.
str := casted.GetBytes()
if !charset.IsValid(enc, str) {
replace, _ := enc.Transform(nil, str, charset.OpReplace)
casted.SetBytesAsString(replace, charset.CollationUTF8MB4, 0)
nSrc := charset.CountValidBytes(enc, str)
return handleWrongCharsetValue(ctx, col, str, nSrc)
}
return nil
}
// ColDesc describes column information like MySQL desc and show columns do.
type ColDesc struct {
Field string
Type string
// Charset is nil if the column doesn't have a charset, or a string indicating the charset name.
Charset interface{}
// Collation is nil if the column doesn't have a collation, or a string indicating the collation name.
Collation interface{}
Null string
Key string
DefaultValue interface{}
Extra string
Privileges string
Comment string
}
const defaultPrivileges = "select,insert,update,references"
// NewColDesc returns a new ColDesc for a column.
func NewColDesc(col *Column) *ColDesc {
// TODO: if we have no primary key and a unique index which's columns are all not null
// we will set these columns' flag as PriKeyFlag
// see https://dev.mysql.com/doc/refman/5.7/en/show-columns.html
// create table
name := col.Name
nullFlag := "YES"
if mysql.HasNotNullFlag(col.Flag) {
nullFlag = "NO"
}
keyFlag := ""
if mysql.HasPriKeyFlag(col.Flag) {
keyFlag = "PRI"
} else if mysql.HasUniKeyFlag(col.Flag) {
keyFlag = "UNI"
} else if mysql.HasMultipleKeyFlag(col.Flag) {
keyFlag = "MUL"
}
var defaultValue interface{}
if !mysql.HasNoDefaultValueFlag(col.Flag) {
defaultValue = col.GetDefaultValue()
if defaultValStr, ok := defaultValue.(string); ok {
if (col.Tp == mysql.TypeTimestamp || col.Tp == mysql.TypeDatetime) &&
strings.EqualFold(defaultValStr, ast.CurrentTimestamp) &&
col.Decimal > 0 {
defaultValue = fmt.Sprintf("%s(%d)", defaultValStr, col.Decimal)
}
}
}
extra := ""
if mysql.HasAutoIncrementFlag(col.Flag) {
extra = "auto_increment"
} else if mysql.HasOnUpdateNowFlag(col.Flag) {
// in order to match the rules of mysql 8.0.16 version
// see https://github.com/pingcap/tidb/issues/10337
extra = "DEFAULT_GENERATED on update CURRENT_TIMESTAMP" + OptionalFsp(&col.FieldType)
} else if col.IsGenerated() {
if col.GeneratedStored {
extra = "STORED GENERATED"
} else {
extra = "VIRTUAL GENERATED"
}
}
desc := &ColDesc{
Field: name.O,
Type: col.GetTypeDesc(),
Charset: col.Charset,
Collation: col.Collate,
Null: nullFlag,
Key: keyFlag,
DefaultValue: defaultValue,
Extra: extra,
Privileges: defaultPrivileges,
Comment: col.Comment,
}
if !field_types.HasCharset(&col.ColumnInfo.FieldType) {
desc.Charset = nil
desc.Collation = nil
}
return desc
}
// ColDescFieldNames returns the fields name in result set for desc and show columns.
func ColDescFieldNames(full bool) []string {
if full {
return []string{"Field", "Type", "Collation", "Null", "Key", "Default", "Extra", "Privileges", "Comment"}
}
return []string{"Field", "Type", "Null", "Key", "Default", "Extra"}
}
// CheckOnce checks if there are duplicated column names in cols.
func CheckOnce(cols []*Column) error {
m := map[string]struct{}{}
for _, col := range cols {
name := col.Name
_, ok := m[name.L]
if ok {
return errDuplicateColumn.GenWithStackByArgs(name)
}
m[name.L] = struct{}{}
}
return nil
}
// CheckNotNull checks if nil value set to a column with NotNull flag is set.
func (c *Column) CheckNotNull(data *types.Datum) error {
if (mysql.HasNotNullFlag(c.Flag) || mysql.HasPreventNullInsertFlag(c.Flag)) && data.IsNull() {
return ErrColumnCantNull.GenWithStackByArgs(c.Name)
}
return nil
}
// HandleBadNull handles the bad null error.
// If BadNullAsWarning is true, it will append the error as a warning, else return the error.
func (c *Column) HandleBadNull(d *types.Datum, sc *stmtctx.StatementContext) error {
if err := c.CheckNotNull(d); err != nil {
if sc.BadNullAsWarning {
sc.AppendWarning(err)
*d = GetZeroValue(c.ToInfo())
return nil
}
return err
}
return nil
}
// IsPKHandleColumn checks if the column is primary key handle column.
func (c *Column) IsPKHandleColumn(tbInfo *model.TableInfo) bool {
return mysql.HasPriKeyFlag(c.Flag) && tbInfo.PKIsHandle
}
// IsCommonHandleColumn checks if the column is common handle column.
func (c *Column) IsCommonHandleColumn(tbInfo *model.TableInfo) bool {
return mysql.HasPriKeyFlag(c.Flag) && tbInfo.IsCommonHandle
}
// CheckNotNull checks if row has nil value set to a column with NotNull flag set.
func CheckNotNull(cols []*Column, row []types.Datum) error {
for _, c := range cols {
if err := c.CheckNotNull(&row[c.Offset]); err != nil {
return err
}
}
return nil
}
// GetColOriginDefaultValue gets default value of the column from original default value.
func GetColOriginDefaultValue(ctx sessionctx.Context, col *model.ColumnInfo) (types.Datum, error) {
return getColDefaultValue(ctx, col, col.GetOriginDefaultValue())
}
// GetColDefaultValue gets default value of the column.
func GetColDefaultValue(ctx sessionctx.Context, col *model.ColumnInfo) (types.Datum, error) {
defaultValue := col.GetDefaultValue()
if !col.DefaultIsExpr {
return getColDefaultValue(ctx, col, defaultValue)
}
return getColDefaultExprValue(ctx, col, defaultValue.(string))
}
// EvalColDefaultExpr eval default expr node to explicit default value.
func EvalColDefaultExpr(ctx sessionctx.Context, col *model.ColumnInfo, defaultExpr ast.ExprNode) (types.Datum, error) {
d, err := expression.EvalAstExpr(ctx, defaultExpr)
if err != nil {
return types.Datum{}, err
}
// Check the evaluated data type by cast.
value, err := CastValue(ctx, d, col, false, false)
if err != nil {
return types.Datum{}, err
}
return value, nil
}
func getColDefaultExprValue(ctx sessionctx.Context, col *model.ColumnInfo, defaultValue string) (types.Datum, error) {
var defaultExpr ast.ExprNode
expr := fmt.Sprintf("select %s", defaultValue)
stmts, _, err := parser.New().ParseSQL(expr)
if err == nil {
defaultExpr = stmts[0].(*ast.SelectStmt).Fields.Fields[0].Expr
}
d, err := expression.EvalAstExpr(ctx, defaultExpr)
if err != nil {
return types.Datum{}, err
}
// Check the evaluated data type by cast.
value, err := CastValue(ctx, d, col, false, false)
if err != nil {
return types.Datum{}, err
}
return value, nil
}
func getColDefaultValue(ctx sessionctx.Context, col *model.ColumnInfo, defaultVal interface{}) (types.Datum, error) {
if defaultVal == nil {
return getColDefaultValueFromNil(ctx, col)
}
if col.Tp != mysql.TypeTimestamp && col.Tp != mysql.TypeDatetime {
value, err := CastValue(ctx, types.NewDatum(defaultVal), col, false, false)
if err != nil {
return types.Datum{}, err
}
return value, nil
}
// Check and get timestamp/datetime default value.
sc := ctx.GetSessionVars().StmtCtx
var needChangeTimeZone bool
// If the column's default value is not ZeroDatetimeStr nor CurrentTimestamp, should use the time zone of the default value itself.
if col.Tp == mysql.TypeTimestamp {
if vv, ok := defaultVal.(string); ok && vv != types.ZeroDatetimeStr && !strings.EqualFold(vv, ast.CurrentTimestamp) {
needChangeTimeZone = true
originalTZ := sc.TimeZone
// For col.Version = 0, the timezone information of default value is already lost, so use the system timezone as the default value timezone.
sc.TimeZone = timeutil.SystemLocation()
if col.Version >= model.ColumnInfoVersion1 {
sc.TimeZone = time.UTC
}
defer func() { sc.TimeZone = originalTZ }()
}
}
value, err := expression.GetTimeValue(ctx, defaultVal, col.Tp, int8(col.Decimal))
if err != nil {
return types.Datum{}, errGetDefaultFailed.GenWithStackByArgs(col.Name)
}
// If the column's default value is not ZeroDatetimeStr or CurrentTimestamp, convert the default value to the current session time zone.
if needChangeTimeZone {
t := value.GetMysqlTime()
err = t.ConvertTimeZone(sc.TimeZone, ctx.GetSessionVars().Location())
if err != nil {
return value, err
}
value.SetMysqlTime(t)
}
return value, nil
}
func getColDefaultValueFromNil(ctx sessionctx.Context, col *model.ColumnInfo) (types.Datum, error) {
if !mysql.HasNotNullFlag(col.Flag) {
return types.Datum{}, nil
}
if col.Tp == mysql.TypeEnum {
// For enum type, if no default value and not null is set,
// the default value is the first element of the enum list
defEnum, err := types.ParseEnumValue(col.FieldType.Elems, 1)
if err != nil {
return types.Datum{}, err
}
return types.NewCollateMysqlEnumDatum(defEnum, col.Collate), nil
}
if mysql.HasAutoIncrementFlag(col.Flag) {
// Auto increment column doesn't has default value and we should not return error.
return GetZeroValue(col), nil
}
vars := ctx.GetSessionVars()
sc := vars.StmtCtx
if !vars.StrictSQLMode {
sc.AppendWarning(ErrNoDefaultValue.FastGenByArgs(col.Name))
return GetZeroValue(col), nil
}
if sc.BadNullAsWarning {
sc.AppendWarning(ErrColumnCantNull.FastGenByArgs(col.Name))
return GetZeroValue(col), nil
}
return types.Datum{}, ErrNoDefaultValue.FastGenByArgs(col.Name)
}
// GetZeroValue gets zero value for given column type.
func GetZeroValue(col *model.ColumnInfo) types.Datum {
var d types.Datum
switch col.Tp {
case mysql.TypeTiny, mysql.TypeInt24, mysql.TypeShort, mysql.TypeLong, mysql.TypeLonglong:
if mysql.HasUnsignedFlag(col.Flag) {
d.SetUint64(0)
} else {
d.SetInt64(0)
}
case mysql.TypeYear:
d.SetInt64(0)
case mysql.TypeFloat:
d.SetFloat32(0)
case mysql.TypeDouble:
d.SetFloat64(0)
case mysql.TypeNewDecimal:
d.SetLength(col.Flen)
d.SetFrac(col.Decimal)
d.SetMysqlDecimal(new(types.MyDecimal))
case mysql.TypeString:
if col.Flen > 0 && col.Charset == charset.CharsetBin {
d.SetBytes(make([]byte, col.Flen))
} else {
d.SetString("", col.Collate)
}
case mysql.TypeVarString, mysql.TypeVarchar, mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
d.SetString("", col.Collate)
case mysql.TypeDuration:
d.SetMysqlDuration(types.ZeroDuration)
case mysql.TypeDate:
d.SetMysqlTime(types.ZeroDate)
case mysql.TypeTimestamp:
d.SetMysqlTime(types.ZeroTimestamp)
case mysql.TypeDatetime:
d.SetMysqlTime(types.ZeroDatetime)
case mysql.TypeBit:
d.SetMysqlBit(types.ZeroBinaryLiteral)
case mysql.TypeSet:
d.SetMysqlSet(types.Set{}, col.Collate)
case mysql.TypeEnum:
d.SetMysqlEnum(types.Enum{}, col.Collate)
case mysql.TypeJSON:
d.SetMysqlJSON(json.CreateBinary(nil))
}
return d
}
// OptionalFsp convert a FieldType.Decimal to string.
func OptionalFsp(fieldType *types.FieldType) string {
fsp := fieldType.Decimal
if fsp == 0 {
return ""
}
return "(" + strconv.Itoa(fsp) + ")"
}