forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve_column_defaults.go
1074 lines (990 loc) · 39.1 KB
/
resolve_column_defaults.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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 analyzer
import (
"github.com/dolthub/vitess/go/sqltypes"
"github.com/gabereiser/go-mysql-server/sql"
"github.com/gabereiser/go-mysql-server/sql/expression"
"github.com/gabereiser/go-mysql-server/sql/information_schema"
"github.com/gabereiser/go-mysql-server/sql/parse"
"github.com/gabereiser/go-mysql-server/sql/plan"
"github.com/gabereiser/go-mysql-server/sql/transform"
"github.com/gabereiser/go-mysql-server/sql/types"
)
// validColumnDefaultFuncs is the set of functions that are legal in a column default value
var validColumnDefaultFuncs = map[string]struct{}{
"abs": {},
"acos": {},
"adddate": {},
"addtime": {},
"aes_decrypt": {},
"aes_encrypt": {},
"any_value": {},
"ascii": {},
"asin": {},
"atan2": {},
"atan": {},
"avg": {},
"benchmark": {},
"bin": {},
"bin_to_uuid": {},
"bit_and": {},
"bit_length": {},
"bit_count": {},
"bit_or": {},
"bit_xor": {},
"can_access_column": {},
"can_access_database": {},
"can_access_table": {},
"can_access_view": {},
"cast": {},
"ceil": {},
"ceiling": {},
"char": {},
"char_length": {},
"character_length": {},
"charset": {},
"coalesce": {},
"coercibility": {},
"collation": {},
"compress": {},
"concat": {},
"concat_ws": {},
"connection_id": {},
"conv": {},
"convert": {},
"convert_tz": {},
"cos": {},
"cot": {},
"count": {},
"crc32": {},
"cume_dist": {},
"curdate": {},
"current_role": {},
"current_timestamp": {},
"curtime": {},
"database": {},
"date": {},
"date_add": {},
"date_format": {},
"date_sub": {},
"datediff": {},
"day": {},
"dayname": {},
"dayofmonth": {},
"dayofweek": {},
"dayofyear": {},
"default": {},
"degrees": {},
"dense_rank": {},
"elt": {},
"exp": {},
"export_set": {},
"extract": {},
"extractvalue": {},
"field": {},
"find_in_set": {},
"first_value": {},
"floor": {},
"format": {},
"format_bytes": {},
"format_pico_time": {},
"found_rows": {},
"from_base64": {},
"from_days": {},
"from_unixtime": {},
"geomcollection": {},
"geometrycollection": {},
"get_dd_column_privileges": {},
"get_dd_create_options": {},
"get_dd_index_sub_part_length": {},
"get_format": {},
"get_lock": {},
"greatest": {},
"group_concat": {},
"grouping": {},
"gtid_subset": {},
"gtid_subtract": {},
"hex": {},
"hour": {},
"icu_version": {},
"if": {},
"ifnull": {},
"in": {},
"inet_aton": {},
"inet_ntoa": {},
"inet6_aton": {},
"inet6_ntoa": {},
"insert": {},
"instr": {},
"internal_auto_increment": {},
"internal_avg_row_length": {},
"internal_check_time": {},
"internal_checksum": {},
"internal_data_free": {},
"internal_data_length": {},
"internal_dd_char_length": {},
"internal_get_comment_or_error": {},
"internal_get_enabled_role_json": {},
"internal_get_hostname": {},
"internal_get_username": {},
"internal_get_view_warning_or_error": {},
"internal_index_column_cardinality": {},
"internal_index_length": {},
"internal_is_enabled_role": {},
"internal_is_mandatory_role": {},
"internal_keys_disabled": {},
"internal_max_data_length": {},
"internal_table_rows": {},
"internal_update_time": {},
"interval": {},
"is_free_lock": {},
"is_ipv4": {},
"is_ipv4_compat": {},
"is_ipv4_mapped": {},
"is_ipv6": {},
"is_used_lock": {},
"is_uuid": {},
"isnull": {},
"json_array": {},
"json_array_append": {},
"json_array_insert": {},
"json_arrayagg": {},
"json_contains": {},
"json_contains_path": {},
"json_depth": {},
"json_extract": {},
"json_insert": {},
"json_keys": {},
"json_length": {},
"json_merge": {},
"json_merge_patch": {},
"json_merge_preserve": {},
"json_object": {},
"json_objectagg": {},
"json_overlaps": {},
"json_pretty": {},
"json_quote": {},
"json_remove": {},
"json_replace": {},
"json_schema_valid": {},
"json_schema_validation_report": {},
"json_search": {},
"json_set": {},
"json_storage_free": {},
"json_storage_size": {},
"json_table": {},
"json_type": {},
"json_unquote": {},
"json_valid": {},
"json_value": {},
"lag": {},
"last_insert_id": {},
"last_value": {},
"lcase": {},
"lead": {},
"least": {},
"left": {},
"length": {},
"linestring": {},
"ln": {},
"load_file": {},
"localtimestamp": {},
"locate": {},
"log": {},
"log10": {},
"log2": {},
"lower": {},
"lpad": {},
"ltrim": {},
"make_set": {},
"makedate": {},
"maketime": {},
"master_pos_wait": {},
"max": {},
"mbrcontains": {},
"mbrcoveredby": {},
"mbrcovers": {},
"mbrdisjoint": {},
"mbrequals": {},
"mbrintersects": {},
"mbroverlaps": {},
"mbrtouches": {},
"mbrwithin": {},
"md5": {},
"microsecond": {},
"mid": {},
"min": {},
"minute": {},
"mod": {},
"month": {},
"monthname": {},
"multilinestring": {},
"multipoint": {},
"multipolygon": {},
"name_const": {},
"now": {},
"nth_value": {},
"ntile": {},
"nullif": {},
"oct": {},
"octet_length": {},
"ord": {},
"percent_rank": {},
"period_add": {},
"period_diff": {},
"pi": {},
"point": {},
"polygon": {},
"position": {},
"pow": {},
"power": {},
"ps_current_thread_id": {},
"ps_thread_id": {},
"quarter": {},
"quote": {},
"radians": {},
"rand": {},
"random_bytes": {},
"rank": {},
"regexp_instr": {},
"regexp_like": {},
"regexp_replace": {},
"regexp_substr": {},
"release_all_locks": {},
"release_lock": {},
"repeat": {},
"replace": {},
"reverse": {},
"right": {},
"roles_graphml": {},
"round": {},
"row_count": {},
"row_number": {},
"rpad": {},
"rtrim": {},
"schema": {},
"sec_to_time": {},
"second": {},
"session_user": {},
"sha1": {},
"sha": {},
"sha2": {},
"sign": {},
"sin": {},
"sleep": {},
"soundex": {},
"space": {},
"sqrt": {},
"st_area": {},
"st_asbinary": {},
"st_aswkb": {},
"st_asgeojson": {},
"st_astext": {},
"st_aswkt": {},
"st_buffer": {},
"st_buffer_strategy": {},
"st_centroid": {},
"st_contains": {},
"st_convexhull": {},
"st_crosses": {},
"st_difference": {},
"st_dimension": {},
"st_disjoint": {},
"st_distance": {},
"st_distance_sphere": {},
"st_endpoint": {},
"st_envelope": {},
"st_equals": {},
"st_exteriorring": {},
"st_geohash": {},
"st_geomcollfromtext": {},
"st_geomcollfromtxt": {},
"st_geomcollfromwkb": {},
"st_geometrycollectionfromtext": {},
"st_geometrycollectionfromwkb": {},
"st_geometryn": {},
"st_geometrytype": {},
"st_geomfromgeojson": {},
"st_geomfromtext": {},
"st_geometryfromtext": {},
"st_geomfromwkb": {},
"st_geometryfromwkb": {},
"st_interiorringn": {},
"st_intersection": {},
"st_intersects": {},
"st_isclosed": {},
"st_isempty": {},
"st_issimple": {},
"st_isvalid": {},
"st_latfromgeohash": {},
"st_latitude": {},
"st_length": {},
"st_linefromtext": {},
"st_linestringfromtext": {},
"st_linefromwkb": {},
"st_linestringfromwkb": {},
"st_longfromgeohash": {},
"st_longitude": {},
"st_makeenvelope": {},
"st_mlinefromtext": {},
"st_multilinestringfromtext": {},
"st_mlinefromwkb": {},
"st_multilinestringfromwkb": {},
"st_mpointfromtext": {},
"st_multipointfromtext": {},
"st_mpointfromwkb": {},
"st_multipointfromwkb": {},
"st_mpolyfromtext": {},
"st_multipolygonfromtext": {},
"st_mpolyfromwkb": {},
"st_multipolygonfromwkb": {},
"st_numgeometries": {},
"st_numinteriorring": {},
"st_numinteriorrings": {},
"st_numpoints": {},
"st_overlaps": {},
"st_pointfromgeohash": {},
"st_pointfromtext": {},
"st_pointfromwkb": {},
"st_pointn": {},
"st_polyfromtext": {},
"st_polygonfromtext": {},
"st_polyfromwkb": {},
"st_polygonfromwkb": {},
"st_simplify": {},
"st_srid": {},
"st_startpoint": {},
"st_swapxy": {},
"st_symdifference": {},
"st_touches": {},
"st_transform": {},
"st_union": {},
"st_validate": {},
"st_within": {},
"st_x": {},
"st_y": {},
"statement_digest": {},
"statement_digest_text": {},
"std": {},
"stddev": {},
"stddev_pop": {},
"stddev_samp": {},
"str_to_date": {},
"strcmp": {},
"subdate": {},
"substr": {},
"substring": {},
"substring_index": {},
"subtime": {},
"sum": {},
"sysdate": {},
"system_user": {},
"tan": {},
"time": {},
"time_format": {},
"time_to_sec": {},
"timediff": {},
"timestamp": {},
"timestampadd": {},
"timestampdiff": {},
"to_base64": {},
"to_days": {},
"to_seconds": {},
"trim": {},
"truncate": {},
"ucase": {},
"uncompress": {},
"uncompressed_length": {},
"unhex": {},
"unix_timestamp": {},
"updatexml": {},
"upper": {},
"user": {},
"utc_date": {},
"utc_time": {},
"utc_timestamp": {},
"uuid": {},
"uuid_short": {},
"uuid_to_bin": {},
"validate_password_strength": {},
"values": {},
"var_pop": {},
"var_samp": {},
"variance": {},
"version": {},
"wait_for_executed_gtid_set": {},
"wait_until_sql_thread_after_gtids": {},
"week": {},
"weekday": {},
"weekofyear": {},
"weight_string": {},
"year": {},
"yearweek": {},
}
// Resolving column defaults is a multi-phase process, with different analyzer rules for each phase.
//
// * parseColumnDefaults: Some integrators (dolt but not GMS) store their column defaults as strings, which we need to
// parse into expressions before we can analyze them any further.
// * resolveColumnDefaults: Once we have an expression for a default value, it may contain expressions that need
// simplification before further phases of processing can take place.
//
// After this stage, expressions in column default values are handled by the normal analyzer machinery responsible for
// resolving expressions, including things like columns and functions. Every node that needs to do this for its default
// values implements `sql.Expressioner` to expose such expressions. There is custom logic in `resolveColumns` to help
// identify the correct indexes for column references, which can vary based on the node type.
//
// Finally there are cleanup phases:
// * validateColumnDefaults: ensures that newly created column defaults from a DDL statement are legal for the type of
// column, various other business logic checks to match MySQL's logic.
// * stripTableNamesFromDefault: column defaults headed for storage or serialization in a query result need the table
// names in any GetField expressions stripped out so that they serialize to strings without such table names. Table
// names in GetField expressions are expected in much of the rest of the analyzer, so we do this after the bulk of
// analyzer work.
//
// The `information_schema.columns` table also needs access to the default values of every column in the database, and
// because it's a table it can't implement `sql.Expressioner` like other node types. Instead it has special handling
// here, as well as in the `resolve_functions` rule.
func validateColumnDefaults(ctx *sql.Context, _ *Analyzer, n sql.Node, _ *Scope, _ RuleSelector) (sql.Node, transform.TreeIdentity, error) {
span, ctx := ctx.Span("validateColumnDefaults")
defer span.End()
return transform.Node(n, func(n sql.Node) (sql.Node, transform.TreeIdentity, error) {
switch node := n.(type) {
case *plan.AlterDefaultSet:
table := getResolvedTable(node)
sch := table.Schema()
index := sch.IndexOfColName(node.ColumnName)
col := sch[index]
eWrapper := expression.WrapExpression(node.Default)
err := validateColumnDefault(ctx, col, eWrapper)
if err != nil {
return node, transform.SameTree, err
}
return node, transform.SameTree, nil
case sql.SchemaTarget:
switch node.(type) {
case *plan.AlterPK, *plan.AddColumn, *plan.ModifyColumn, *plan.AlterDefaultDrop, *plan.CreateTable, *plan.DropColumn:
// DDL nodes must validate any new column defaults, continue to logic below
default:
// other node types are not altering the schema and therefore don't need validation of column defaults
return n, transform.SameTree, nil
}
// There may be multiple DDL nodes in the plan (ALTER TABLE statements can have many clauses), and for each of them
// we need to count the column indexes in the very hacky way outlined above.
colIndex := 0
return transform.NodeExprs(n, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
eWrapper, ok := e.(*expression.Wrapper)
if !ok {
return e, transform.SameTree, nil
}
col, err := lookupColumnForTargetSchema(ctx, node, colIndex)
if err != nil {
return nil, transform.SameTree, err
}
colIndex++
err = validateColumnDefault(ctx, col, eWrapper)
if err != nil {
return nil, transform.SameTree, err
}
return e, transform.SameTree, nil
})
default:
return node, transform.SameTree, nil
}
})
}
func resolveColumnDefaults(ctx *sql.Context, _ *Analyzer, n sql.Node, _ *Scope, _ RuleSelector) (sql.Node, transform.TreeIdentity, error) {
span, ctx := ctx.Span("resolveColumnDefaults")
defer span.End()
return transform.Node(n, func(n sql.Node) (sql.Node, transform.TreeIdentity, error) {
// There may be multiple DDL nodes in the plan (ALTER TABLE statements can have many clauses), and for each of them
// we need to count the column indexes as we transform expressions
colIndex := 0
switch node := n.(type) {
case *plan.InsertDestination:
return transform.NodeExprs(node, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
eWrapper, ok := e.(*expression.Wrapper)
if !ok {
return e, transform.SameTree, nil
}
table := getResolvedTable(node)
sch := table.Schema()
if colIndex >= len(sch) {
return e, transform.SameTree, nil
}
col := sch[colIndex]
colIndex++
return resolveColumnDefault(ctx, col, eWrapper)
})
case *plan.InsertInto:
// node.Source needs to be explicitly handled here because it's not a
// registered child of InsertInto
newSource, identity, err := transform.NodeExprs(node.Source, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
eWrapper, ok := e.(*expression.Wrapper)
if !ok {
return e, transform.SameTree, nil
}
// Instead of grabbing the schema from TargetSchema(), use the Destination node
sch := node.Destination.Schema()
// InsertInto.Source can contain multiple rows, so loop over the included columns
colIndex = colIndex % len(node.ColumnNames)
// Columns can be specified in any order, so use the order from the InsertInto statement
schemaIndex := sch.IndexOfColName(node.ColumnNames[colIndex])
if schemaIndex == -1 {
return nil, transform.SameTree, sql.ErrColumnNotFound.New(node.ColumnNames[colIndex])
}
col := sch[schemaIndex]
colIndex++
return resolveColumnDefault(ctx, col, eWrapper)
})
if identity == transform.NewTree {
node.Source = newSource
}
return node, identity, err
case *plan.AlterDefaultSet:
table := getResolvedTable(node)
sch := table.Schema()
index := sch.IndexOfColName(node.ColumnName)
col := sch[index]
eWrapper := expression.WrapExpression(node.Default)
newExpr, same, err := resolveColumnDefault(ctx, col, eWrapper)
if err != nil {
return node, transform.SameTree, err
}
if same {
return node, transform.SameTree, nil
}
newNode, err := node.WithDefault(newExpr)
if err != nil {
return node, transform.SameTree, err
}
return newNode, transform.NewTree, nil
case sql.SchemaTarget:
return transform.NodeExprs(n, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
eWrapper, ok := e.(*expression.Wrapper)
if !ok {
return e, transform.SameTree, nil
}
col, err := lookupColumnForTargetSchema(ctx, node, colIndex)
if err != nil {
return nil, transform.SameTree, err
}
colIndex++
return resolveColumnDefault(ctx, col, eWrapper)
})
case *plan.ResolvedTable:
ct, ok := node.Table.(*information_schema.ColumnsTable)
if !ok {
return node, transform.SameTree, nil
}
allColumns, err := ct.AllColumns(ctx)
if err != nil {
return nil, transform.SameTree, err
}
allDefaults, same, err := transform.Exprs(transform.WrappedColumnDefaults(allColumns), func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
eWrapper, ok := e.(*expression.Wrapper)
if !ok {
return e, transform.SameTree, nil
}
colIdx := colIndex
colIndex++
return resolveColumnDefault(ctx, allColumns[colIdx], eWrapper)
})
if err != nil {
return nil, transform.SameTree, err
}
if !same {
node.Table, err = ct.WithColumnDefaults(allDefaults)
if err != nil {
return nil, transform.SameTree, err
}
}
return node, transform.NewTree, err
default:
return node, transform.SameTree, nil
}
})
}
// stripTableNamesFromColumnDefaults removes the table name from any GetField expressions in column default expressions.
// Default values can only reference their host table, and since we serialize the GetField expression for storage, it's
// important that we remove the table name before passing it off for storage. Otherwise we end up with serialized
// defaults like `tableName.field + 1` instead of just `field + 1`.
func stripTableNamesFromColumnDefaults(ctx *sql.Context, _ *Analyzer, n sql.Node, _ *Scope, _ RuleSelector) (sql.Node, transform.TreeIdentity, error) {
span, ctx := ctx.Span("stripTableNamesFromColumnDefaults")
defer span.End()
return transform.Node(n, func(n sql.Node) (sql.Node, transform.TreeIdentity, error) {
switch node := n.(type) {
case *plan.AlterDefaultSet:
eWrapper := expression.WrapExpression(node.Default)
newExpr, same, err := stripTableNamesFromDefault(eWrapper)
if err != nil {
return node, transform.SameTree, err
}
if same {
return node, transform.SameTree, nil
}
newNode, err := node.WithDefault(newExpr)
if err != nil {
return node, transform.SameTree, err
}
return newNode, transform.NewTree, nil
case sql.SchemaTarget:
return transform.NodeExprs(n, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
eWrapper, ok := e.(*expression.Wrapper)
if !ok {
return e, transform.SameTree, nil
}
return stripTableNamesFromDefault(eWrapper)
})
case *plan.ResolvedTable:
ct, ok := node.Table.(*information_schema.ColumnsTable)
if !ok {
return node, transform.SameTree, nil
}
allColumns, err := ct.AllColumns(ctx)
if err != nil {
return nil, transform.SameTree, err
}
allDefaults, same, err := transform.Exprs(transform.WrappedColumnDefaults(allColumns), func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
eWrapper, ok := e.(*expression.Wrapper)
if !ok {
return e, transform.SameTree, nil
}
return stripTableNamesFromDefault(eWrapper)
})
if err != nil {
return nil, transform.SameTree, err
}
if !same {
node.Table, err = ct.WithColumnDefaults(allDefaults)
if err != nil {
return nil, transform.SameTree, err
}
return node, transform.NewTree, err
}
return node, transform.SameTree, err
default:
return node, transform.SameTree, nil
}
})
}
// lookupColumnForTargetSchema looks at the target schema for the specified SchemaTarget node and returns
// the column based on the specified index. For most node types, this is simply indexing into the target
// schema but a few types require special handling.
func lookupColumnForTargetSchema(_ *sql.Context, node sql.SchemaTarget, colIndex int) (*sql.Column, error) {
schema := node.TargetSchema()
switch n2 := node.(type) {
case *plan.ModifyColumn:
if colIndex < len(schema) {
return schema[colIndex], nil
} else {
return n2.NewColumn(), nil
}
case *plan.AddColumn:
if colIndex < len(schema) {
return schema[colIndex], nil
} else {
return n2.Column(), nil
}
case *plan.AlterDefaultSet:
index := schema.IndexOfColName(n2.ColumnName)
if index == -1 {
return nil, sql.ErrTableColumnNotFound.New(n2.Table, n2.ColumnName)
}
return n2.Schema()[index], nil
default:
if colIndex < len(schema) {
return schema[colIndex], nil
} else {
return nil, sql.ErrColumnNotFound.New(colIndex)
}
}
}
// parseColumnDefaults transforms UnresolvedColumnDefault expressions into ColumnDefaultValue expressions, which
// amounts to parsing the string representation into an actual expression. We only require an actual column default
// value for some node types, where the value will be used.
func parseColumnDefaults(ctx *sql.Context, _ *Analyzer, n sql.Node, _ *Scope, _ RuleSelector) (sql.Node, transform.TreeIdentity, error) {
span, ctx := ctx.Span("parse_column_defaults")
defer span.End()
return transform.Node(n, func(n sql.Node) (sql.Node, transform.TreeIdentity, error) {
switch nn := n.(type) {
case *plan.InsertInto:
if !nn.Destination.Resolved() {
return nn, transform.SameTree, nil
}
var err error
n, err = nn.WithChildren(plan.NewInsertDestination(nn.Destination.Schema(), nn.Destination))
nn = n.(*plan.InsertInto)
if err != nil {
return nil, transform.SameTree, err
}
err = fillInColumnDefaults(ctx, nn)
if err != nil {
return nil, transform.SameTree, err
}
// InsertInto.Source needs special handling, since it is not modeled as a Child
newNode, _, err := parseDefaultsForNode(ctx, nn.Source)
if err != nil {
return nil, transform.SameTree, err
}
nn.Source = newNode
return parseDefaultsForNode(ctx, nn)
case *plan.ResolvedTable:
ct, ok := nn.Table.(*information_schema.ColumnsTable)
if !ok {
return nn, transform.SameTree, nil
}
allColumns, err := ct.AllColumns(ctx)
if err != nil {
return nil, transform.SameTree, err
}
allDefaults, same, err := transform.Exprs(transform.WrappedColumnDefaults(allColumns), func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
eWrapper, ok := e.(*expression.Wrapper)
if !ok {
return e, transform.SameTree, nil
}
newWr, parseSame, err := parseColumnDefault(ctx, eWrapper)
if err != nil {
return nil, transform.SameTree, err
}
newWr, stripSame, err := stripTableNamesFromDefault(newWr.(*expression.Wrapper))
if err != nil {
return nil, false, err
}
return newWr, parseSame && stripSame, nil
})
if err != nil {
return nil, transform.SameTree, err
}
if !same {
nn.Table, err = ct.WithColumnDefaults(allDefaults)
if err != nil {
return nil, transform.SameTree, err
}
return nn, transform.NewTree, err
}
return nn, transform.SameTree, err
default:
return parseDefaultsForNode(ctx, nn)
}
})
}
// parseDefaultsForNode walks over the expressions contained in the specified node and transforms all
// UnresolvedColumnDefaultValues into ColumnDefaultValues.
func parseDefaultsForNode(ctx *sql.Context, input sql.Node) (sql.Node, transform.TreeIdentity, error) {
return transform.NodeExprsWithNode(input, func(node sql.Node, e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
eWrapper, ok := e.(*expression.Wrapper)
if !ok {
return e, transform.SameTree, nil
}
switch node.(type) {
case *plan.Values, *plan.InsertDestination, sql.SchemaTarget:
return parseColumnDefault(ctx, eWrapper)
default:
return e, transform.SameTree, nil
}
})
}
// fillInColumnDefaults fills in column default expressions for any source data that explicitly used
// the 'DEFAULT' keyword as input. This requires that the InsertInto Destination has been fully resolved
// before the column default values can be used.
func fillInColumnDefaults(_ *sql.Context, insertInto *plan.InsertInto) error {
schema := insertInto.Destination.Schema()
// If the source values are not specified, fill in column default placeholders
if values, ok := insertInto.Source.(*plan.Values); ok {
for i, tuple := range values.ExpressionTuples {
if len(tuple) == 0 {
tuple = make([]sql.Expression, len(insertInto.ColumnNames))
for j, _ := range tuple {
tuple[j] = expression.NewDefaultColumn(insertInto.ColumnNames[j])
}
values.ExpressionTuples[i] = tuple
}
}
}
// Pull the column default values out into the same order the columns were specified
columnDefaultValues := make([]*sql.ColumnDefaultValue, len(insertInto.ColumnNames))
for i, columnName := range insertInto.ColumnNames {
index := schema.IndexOfColName(columnName)
if index == -1 {
return plan.ErrInsertIntoNonexistentColumn.New(columnName)
}
columnDefaultValues[i] = schema[index].Default
}
// Walk through the expression tuples looking for any column defaults to fill in
if values, ok := insertInto.Source.(*plan.Values); ok {
for _, exprTuple := range values.ExpressionTuples {
for i, value := range exprTuple {
newExpression, _, err := transform.Expr(value, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
if _, ok := e.(*expression.DefaultColumn); ok {
return columnDefaultValues[i], transform.NewTree, nil
}
return e, transform.SameTree, nil
})
if err != nil {
return err
}
exprTuple[i] = expression.WrapExpression(newExpression)
}
}
}
return nil
}
// parseColumnDefault transforms an UnresolvedColumnDefault expression into a ColumnDefaultValue expression
func parseColumnDefault(ctx *sql.Context, e *expression.Wrapper) (sql.Expression, transform.TreeIdentity, error) {
newDefault, ok := e.Unwrap().(*sql.ColumnDefaultValue)
if !ok {
return e, transform.SameTree, nil
}
if newDefault.Resolved() {
return e, transform.SameTree, nil
}
if ucd, ok := newDefault.Expression.(sql.UnresolvedColumnDefault); ok {
var err error
newDefault, err = parse.StringToColumnDefaultValue(ctx, ucd.String())
if err != nil {
return nil, transform.SameTree, err
}
}
return expression.WrapExpression(newDefault), transform.NewTree, nil
}
func resolveColumnDefault(ctx *sql.Context, col *sql.Column, e *expression.Wrapper) (sql.Expression, transform.TreeIdentity, error) {
newDefault, ok := e.Unwrap().(*sql.ColumnDefaultValue)
if !ok {
return e, transform.SameTree, nil
}
if newDefault == nil {
return e, transform.SameTree, nil
}
// TODO: fix the vitess parser so that it parses negative numbers as numbers and not negation of an expression
isLiteral := newDefault.IsLiteral()
if unaryMinusExpr, ok := newDefault.Expression.(*expression.UnaryMinus); ok {
if literalExpr, ok := unaryMinusExpr.Child.(*expression.Literal); ok {
switch val := literalExpr.Value().(type) {
case float32:
newDefault.Expression = expression.NewLiteral(-val, types.Float32)
isLiteral = true
case float64:
newDefault.Expression = expression.NewLiteral(-val, types.Float64)
isLiteral = true
}
}
}
var err error
newDefault, err = sql.NewColumnDefaultValue(newDefault.Expression, col.Type, isLiteral, newDefault.IsParenthesized(), col.Nullable)
if err != nil {
return nil, transform.SameTree, err
}
return expression.WrapExpression(newDefault), transform.NewTree, nil
}
// validateColumnDefault validates that the column default expression is valid for the column type and returns an error
// if not
func validateColumnDefault(ctx *sql.Context, col *sql.Column, e *expression.Wrapper) error {
newDefault, ok := e.Unwrap().(*sql.ColumnDefaultValue)
if !ok {
return nil
}
if newDefault == nil {
return nil
}
// Some column types can only have a NULL for a literal default, must be an expression otherwise
isLiteralRestrictedType := types.IsTextBlob(col.Type) || types.IsJSON(col.Type) || types.IsGeometry(col.Type)
if isLiteralRestrictedType && newDefault.IsLiteral() {
lit, err := newDefault.Expression.Eval(ctx, nil)
if err != nil {
return err
}
if lit != nil {
return sql.ErrInvalidTextBlobColumnDefault.New()
}
}
var err error
sql.Inspect(newDefault.Expression, func(e sql.Expression) bool {
switch e.(type) {
case sql.FunctionExpression, *expression.UnresolvedFunction:
var funcName string
switch expr := e.(type) {
case sql.FunctionExpression:
funcName = expr.FunctionName()
case *expression.UnresolvedFunction:
funcName = expr.Name()
}
if _, isValid := validColumnDefaultFuncs[funcName]; !isValid {
err = sql.ErrInvalidColumnDefaultFunction.New(funcName, col.Name)