forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve_columns.go
1219 lines (1090 loc) · 41.8 KB
/
resolve_columns.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 (
"fmt"
"sort"
"strings"
"gopkg.in/src-d/go-errors.v1"
"github.com/gabereiser/go-mysql-server/internal/similartext"
"github.com/gabereiser/go-mysql-server/sql"
"github.com/gabereiser/go-mysql-server/sql/expression"
"github.com/gabereiser/go-mysql-server/sql/plan"
"github.com/gabereiser/go-mysql-server/sql/transform"
)
func validateUniqueTableNames(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope, sel RuleSelector) (sql.Node, transform.TreeIdentity, error) {
// getTableAliases will error if any table name / alias is repeated
_, err := getTableAliases(n, scope)
if err != nil {
return nil, transform.SameTree, err
}
return n, transform.SameTree, err
}
// deferredColumn is a wrapper on UnresolvedColumn used to defer the resolution of the column because it may require
// some work done by other analyzer phases.
type deferredColumn struct {
*expression.UnresolvedColumn
}
func (dc *deferredColumn) DebugString() string {
return fmt.Sprintf("deferred(%s)", dc.UnresolvedColumn.String())
}
// IsNullable implements the Expression interface.
func (*deferredColumn) IsNullable() bool {
return true
}
// Children implements the Expression interface.
func (*deferredColumn) Children() []sql.Expression { return nil }
// WithChildren implements the Expression interface.
func (dc *deferredColumn) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 0 {
return nil, sql.ErrInvalidChildrenNumber.New(dc, len(children), 0)
}
return dc, nil
}
type tableCol struct {
table string
col string
}
func newTableCol(table, col string) tableCol {
return tableCol{
table: strings.ToLower(table),
col: strings.ToLower(col),
}
}
var _ sql.Tableable = tableCol{}
var _ sql.Nameable = tableCol{}
func (tc tableCol) Table() string {
return tc.table
}
func (tc tableCol) Name() string {
return tc.col
}
func (tc tableCol) String() string {
if tc.table != "" {
return fmt.Sprintf("%s.%s", tc.table, tc.col)
} else {
return tc.col
}
}
type indexedCol struct {
*sql.Column
index int
}
// column is the common interface that groups UnresolvedColumn and deferredColumn.
type column interface {
sql.Nameable
sql.Tableable
sql.Expression
}
// scopeLevelSymbols tracks available table and column name symbols at a specific scope level for a query. Each nested
// subquery in a statement represents an additional scope level.
type scopeLevelSymbols struct {
availableColumns map[string][]string
availableAliases map[string][]*expression.Alias
availableTables map[string]string
availableTableCols map[tableCol]struct{}
}
func newScopeLevelSymbols() *scopeLevelSymbols {
return &scopeLevelSymbols{
availableColumns: make(map[string][]string),
availableAliases: make(map[string][]*expression.Alias),
availableTables: make(map[string]string),
availableTableCols: make(map[tableCol]struct{}),
}
}
// availableNames tracks available table and column name symbols at each scope level for a query, where level 0
// is the top-level, outermost scope and each additional level is one layer of query scope inward.
type availableNames map[int]*scopeLevelSymbols
// indexColumn adds a column with the given table and column name at the given scope level
func (a availableNames) indexColumn(table, col string, scopeLevel int) {
col = strings.ToLower(col)
_, ok := a[scopeLevel]
if !ok {
a[scopeLevel] = newScopeLevelSymbols()
}
tableLower := strings.ToLower(table)
if !stringContains(a[scopeLevel].availableColumns[col], tableLower) {
a[scopeLevel].availableColumns[col] = append(a[scopeLevel].availableColumns[col], tableLower)
a[scopeLevel].availableTableCols[tableCol{table: tableLower, col: col}] = struct{}{}
}
}
// levels returns a sorted list of the scope levels for these available name symbols, starting with the most specific,
// or innermost, level and ending with the outermost level (i.e. the top level query).
func (a availableNames) levels() []int {
levels := make([]int, len(a))
i := 0
for l := range a {
levels[i] = l
i++
}
sort.Sort(sort.Reverse(sort.IntSlice(levels)))
return levels
}
// indexAlias adds an alias name to track at the specified scope level
func (a availableNames) indexAlias(e *expression.Alias, scopeLevel int) {
name := strings.ToLower(e.Name())
_, ok := a[scopeLevel]
if !ok {
a[scopeLevel] = newScopeLevelSymbols()
}
_, ok = a[scopeLevel].availableAliases[name]
if !ok {
a[scopeLevel].availableAliases[name] = make([]*expression.Alias, 0)
}
a[scopeLevel].availableAliases[name] = append(a[scopeLevel].availableAliases[name], e)
}
// indexTable adds a table with the given name at the specified scope level
func (a availableNames) indexTable(alias, name string, scopeLevel int) {
alias = strings.ToLower(alias)
_, ok := a[scopeLevel]
if !ok {
a[scopeLevel] = newScopeLevelSymbols()
}
a[scopeLevel].availableTables[alias] = strings.ToLower(name)
}
func (a availableNames) tablesAtLevel(scopeLevel int) map[string]string {
return a[scopeLevel].availableTables
}
func (a availableNames) allTables() []string {
var allTables []string
for _, symbols := range a {
for name, table := range symbols.availableTables {
allTables = append(allTables, name, table)
}
}
return dedupStrings(allTables)
}
// aliasesAndTablesForColumnAtLevel returns a slice of strings indicating how many distinct alias definitions are available
// for the specified column name, as well as a slice of strings indicating which distinct tables are available with that
// column name.
func (a availableNames) aliasesAndTablesForColumnAtLevel(column string, scopeLevel int) ([]string, []string) {
tableNames := a[scopeLevel].availableColumns[column]
aliasesFound := make([]string, 0, len(tableNames))
tablesFound := make([]string, 0, len(tableNames))
for _, tableName := range tableNames {
if tableName == "" {
// Regardless of the number of aliases defined with a specific alias name, availableColumns
// currently tracks a single empty string to represent them, so check in another datastructure
// to see how many alias definitions actually used this name.
for range a[scopeLevel].availableAliases[column] {
aliasesFound = append(aliasesFound, tableName)
}
} else {
tablesFound = append(tablesFound, tableName)
}
}
return aliasesFound, tablesFound
}
func (a availableNames) hasTableCol(tc tableCol) bool {
for scopeLevel := range a {
_, ok := a[scopeLevel].availableTableCols[tc]
if ok {
return true
}
}
return false
}
func dedupStrings(in []string) []string {
var seen = make(map[string]struct{})
var result []string
for _, s := range in {
if _, ok := seen[s]; !ok {
seen[s] = struct{}{}
result = append(result, s)
}
}
return result
}
// findOnDupUpdateLeftExprs gathers all the left expressions for statements in InsertInto.OnDupExprs
// the
func findOnDupUpdateLeftExprs(onDupExprs []sql.Expression) map[sql.Expression]bool {
onDupUpdateLeftExprs := map[sql.Expression]bool{}
for _, e := range onDupExprs {
if sf, ok := e.(*expression.SetField); ok {
onDupUpdateLeftExprs[sf.Left] = true
}
}
return onDupUpdateLeftExprs
}
// qualifyColumns assigns a table to any column expressions that don't have one already
func qualifyColumns(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope, sel RuleSelector) (sql.Node, transform.TreeIdentity, error) {
originalNode := n
// Calculate the available symbols BEFORE we get into a transform function, since symbols need to be calculated
// on the full scope; otherwise transform looks at sub-nodes and calculates a partial view of what is available.
symbols := getAvailableNamesByScope(n, scope)
var onDupUpdateSymbols availableNames
var onDupUpdateLeftExprs map[sql.Expression]bool
if in, ok := n.(*plan.InsertInto); ok && len(in.OnDupExprs) > 0 {
inNoSrc := plan.NewInsertInto(
in.Database(),
in.Destination,
nil,
in.IsReplace,
in.ColumnNames,
in.OnDupExprs,
in.Ignore,
)
onDupUpdateSymbols = getAvailableNamesByScope(inNoSrc, scope)
onDupUpdateLeftExprs = findOnDupUpdateLeftExprs(in.OnDupExprs)
}
return transform.Node(n, func(n sql.Node) (sql.Node, transform.TreeIdentity, error) {
if _, ok := n.(sql.Expressioner); !ok || n.Resolved() {
return n, transform.SameTree, nil
}
// Don't qualify unresolved JSON tables, wait for joins
if jt, ok := n.(*plan.JSONTable); ok {
if !jt.Resolved() {
return n, transform.SameTree, nil
}
}
// Updates need to have check constraints qualified since multiple tables could be involved
sameCheckConstraints := true
if nn, ok := n.(*plan.Update); ok {
newChecks, err := qualifyCheckConstraints(nn)
if err != nil {
return n, transform.SameTree, err
}
nn.Checks = newChecks
n = nn
sameCheckConstraints = false
}
// Before we can qualify references in a GroupBy node, we need to see if any aliases
// were defined and then used in grouping expressions
sameGroupBy := true
if groupBy, ok := n.(*plan.GroupBy); ok {
newGroupBy, identity, err := identifyGroupingAliasReferences(groupBy)
if err != nil {
return originalNode, transform.SameTree, err
}
if identity == transform.NewTree {
n = newGroupBy
sameGroupBy = false
}
}
newNode, sameNode, err := transform.OneNodeExprsWithNode(n, func(n sql.Node, e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
evalSymbols := symbols
if in, ok := n.(*plan.InsertInto); ok && len(in.OnDupExprs) > 0 && onDupUpdateLeftExprs[e] {
evalSymbols = onDupUpdateSymbols
}
return qualifyExpression(e, n, evalSymbols)
})
if err != nil {
return originalNode, transform.SameTree, err
}
if sameCheckConstraints && sameGroupBy && sameNode == transform.SameTree {
return newNode, transform.SameTree, nil
}
return newNode, transform.NewTree, nil
})
}
// identifyGroupingAliasReferences finds any aliases defined in the projected expressions of
// the specified GroupBy node, looks for references to those aliases in the grouping expressions
// of the same GroupBy node, and transforms them to an AliasReference expression. This is
// necessary because when qualifying columns, we can't currently distinguish between projection
// expressions and grouping expressions because GroupBy combines both in its Expresions() func,
// so we special case GroupBy here to identify aliases used in the grouping expressions.
func identifyGroupingAliasReferences(groupBy *plan.GroupBy) (*plan.GroupBy, transform.TreeIdentity, error) {
projectedAliases := aliasesDefinedInNode(groupBy)
// Temporarily remove projection expressions so we can transform only the grouping expressions
groupByWithOnlyGroupingExprs := plan.NewGroupBy(nil, groupBy.GroupByExprs, groupBy.Child)
newNode, identity, err := transform.OneNodeExpressions(groupByWithOnlyGroupingExprs, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
uc, ok := e.(*expression.UnresolvedColumn)
if !ok || uc.Table() != "" {
return e, transform.SameTree, nil
}
if stringContains(projectedAliases, strings.ToLower(uc.Name())) {
return expression.NewAliasReference(uc.Name()), transform.NewTree, nil
}
return e, transform.SameTree, nil
})
if identity == transform.NewTree && err == nil {
groupBy = plan.NewGroupBy(groupBy.SelectedExprs, newNode.(*plan.GroupBy).GroupByExprs, groupBy.Child)
}
return groupBy, identity, err
}
// qualifyCheckConstraints returns a new set of CheckConstraints created by taking the specified Update node's checks
// and qualifying them to the table involved in the update, including honoring any table aliases specified.
func qualifyCheckConstraints(update *plan.Update) (sql.CheckConstraints, error) {
checks := update.Checks
table, alias := getResolvedTableAndAlias(update.Child)
newExprs := make([]sql.Expression, len(checks))
for i, checkExpr := range checks.ToExpressions() {
newExpr, _, err := transform.Expr(checkExpr, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
switch e := e.(type) {
case *expression.UnresolvedColumn:
if e.Table() == "" {
tableName := table.Name()
if alias != "" {
tableName = alias
}
return expression.NewUnresolvedQualifiedColumn(tableName, e.Name()), transform.NewTree, nil
}
default:
// nothing else needed for other types
}
return e, transform.SameTree, nil
})
if err != nil {
return nil, err
}
newExprs[i] = newExpr
}
return checks.FromExpressions(newExprs)
}
// getAvailableNamesByScope searches the node |n|, the current query being analyzed, as well as any nodes from outer
// scope levels contained in |scope| in order to calculate the available columns, tables, and aliases available to
// the current scope.
func getAvailableNamesByScope(n sql.Node, scope *Scope) availableNames {
symbols := make(availableNames)
scopeNodes := make([]sql.Node, 0, 1+len(scope.InnerToOuter()))
scopeNodes = append(scopeNodes, n)
scopeNodes = append(scopeNodes, scope.InnerToOuter()...)
currentScopeLevel := len(scopeNodes)
// Examine all columns, from the innermost scope (this node) outward.
children := n.Children()
// find all ResolvedTables in InsertInto.Source visible when resolving columns iff there are on duplicate key updates
if in, ok := n.(*plan.InsertInto); ok && in.Source != nil && len(in.OnDupExprs) > 0 {
aliasedTables := make(map[sql.Node]bool)
transform.Inspect(in.Source, func(n sql.Node) bool {
if tblAlias, ok := n.(*plan.TableAlias); ok && tblAlias.Resolved() {
children = append(children, tblAlias)
aliasedTables[tblAlias.Child] = true
}
return true
})
transform.Inspect(in.Source, func(n sql.Node) bool {
if resTbl, ok := n.(*plan.ResolvedTable); ok && !aliasedTables[resTbl] {
children = append(children, resTbl)
}
return true
})
transform.Inspect(in.Source, func(n sql.Node) bool {
if subAlias, ok := n.(*plan.SubqueryAlias); ok && !aliasedTables[subAlias] {
children = append(children, subAlias)
}
return true
})
}
getColumnsInNodes(children, symbols, currentScopeLevel-1)
for _, currentScopeNode := range scopeNodes {
getColumnsInNodes([]sql.Node{currentScopeNode}, symbols, currentScopeLevel-1)
currentScopeLevel--
}
// Get table names in all outer scopes and nodes. Inner scoped names will overwrite those from the outer scope.
// note: we terminate the symbols for this level after finding the first column source
for scopeLevel, n := range scopeNodes {
transform.Inspect(n, func(n sql.Node) bool {
switch n := n.(type) {
case *plan.SubqueryAlias, *plan.ResolvedTable, *plan.ValueDerivedTable, *plan.RecursiveTable, *plan.RecursiveCte, *plan.IndexedTableAccess, *plan.JSONTable:
name := strings.ToLower(n.(sql.Nameable).Name())
symbols.indexTable(name, name, scopeLevel)
return false
case *plan.TableAlias:
switch t := n.Child.(type) {
case *plan.ResolvedTable, *plan.UnresolvedTable, *plan.SubqueryAlias,
*plan.RecursiveTable, *plan.IndexedTableAccess:
name := strings.ToLower(t.(sql.Nameable).Name())
alias := strings.ToLower(n.Name())
symbols.indexTable(alias, name, scopeLevel)
}
return false
case sql.Projector:
// projected aliases overwrite lower namespaces, but importantly,
// we do not terminate symbol generation.
for _, e := range n.ProjectedExprs() {
if a, ok := e.(*expression.Alias); ok {
symbols.indexAlias(a, scopeLevel)
}
}
}
return true
})
}
return symbols
}
// qualifyExpression examines the specified expression |e|, coming from the specified node |node|, and uses the |availableNames|
// symbol map to identify the table or expression alias an unqualified column reference should map to. The updated,
// qualified expression is returned along with the transform identity, or any error encountered.
func qualifyExpression(e sql.Expression, node sql.Node, symbols availableNames) (sql.Expression, transform.TreeIdentity, error) {
switch col := e.(type) {
case column:
if col.Resolved() {
return col, transform.SameTree, nil
}
// Skip qualification if an expression has already been identified as an alias reference
if _, ok := col.(*expression.AliasReference); ok {
return col, transform.SameTree, nil
}
// Skip this step for variables
if strings.HasPrefix(col.Name(), "@") || strings.HasPrefix(col.Table(), "@") {
return col, transform.SameTree, nil
}
// if there are no tables or columns anywhere in the query, just give up and let another part of the analyzer throw
// an analysis error. (for some queries, like SHOW statements, this is expected and not an error)
if len(symbols) == 0 {
return col, transform.SameTree, nil
}
canAccessAliasAtCurrentScope := true
if _, ok := node.(*plan.Filter); ok {
// Expression aliases from the same scope are NOT allowed in where/filter clauses
canAccessAliasAtCurrentScope = false
}
// If this column is already qualified, make sure the table name is known
if col.Table() != "" {
if validateQualifiedColumn(col, symbols) {
return col, transform.SameTree, nil
} else {
similar := similartext.Find(symbols.allTables(), col.Table())
return nil, transform.SameTree, sql.ErrTableNotFound.New(col.Table() + similar)
}
}
// Look in all the scopes (from inner to outer), to identify the column. Stop as soon as we have a scope with
// exactly 1 match for the column name. If there is ambiguity in available column names, that's an error.
name := strings.ToLower(col.Name())
for _, scopeLevel := range symbols.levels() {
aliasesFound, tablesFound := symbols.aliasesAndTablesForColumnAtLevel(name, scopeLevel)
switch len(aliasesFound) + len(tablesFound) {
case 0:
// This column could be in an outer scope, keep going
continue
case 1:
if len(aliasesFound) > 0 {
// This indicates we found a match with an alias definition
if canAccessAliasAtCurrentScope || scopeLevel != symbols.levels()[0] {
return expression.NewAliasReference(col.Name()), transform.NewTree, nil
}
} else {
return expression.NewUnresolvedQualifiedColumn(
tablesFound[0],
col.Name(),
), transform.NewTree, nil
}
default:
switch node.(type) {
case *plan.Sort, *plan.Having:
// For order by and having clauses... prefer an alias over a column when there is ambiguity
if len(aliasesFound) == 0 {
if len(tablesFound) == 1 {
return expression.NewUnresolvedQualifiedColumn(tablesFound[0], col.Name()), transform.NewTree, nil
} else if len(tablesFound) > 1 {
return col, transform.SameTree, sql.ErrAmbiguousColumnOrAliasName.New(col.Name())
}
} else if len(aliasesFound) == 1 {
return expression.NewAliasReference(col.Name()), transform.NewTree, nil
} else if len(aliasesFound) > 1 {
return col, transform.SameTree, sql.ErrAmbiguousColumnOrAliasName.New(col.Name())
}
default:
// otherwise, prefer the table column...
if len(tablesFound) == 1 {
return expression.NewUnresolvedQualifiedColumn(tablesFound[0], col.Name()), transform.NewTree, nil
} else if len(aliasesFound) > 0 {
// MySQL allows ambiguity with multiple alias names in some situations, so identify this as an
// alias reference and resolve the exact alias definition later
return expression.NewAliasReference(col.Name()), transform.NewTree, nil
}
}
return nil, transform.SameTree, sql.ErrAmbiguousColumnName.New(col.Name(), strings.Join(tablesFound, ", "))
}
}
if !canAccessAliasAtCurrentScope {
// return a deferredColumn if we still can't find a column and know this couldn't be an alias reference
return &deferredColumn{expression.NewUnresolvedQualifiedColumn(col.Table(), col.Name())}, transform.NewTree, nil
}
// If there are no tables that have any column with the column name let's just return it as it is. This may be an
// alias, so we'll wait for the reorder of the projection to resolve it.
return col, transform.SameTree, nil
case *expression.Star:
// Make sure that any qualified stars reference known tables
if col.Table != "" {
tableFound := false
for level := range symbols {
tables := symbols.tablesAtLevel(level)
if _, ok := tables[strings.ToLower(col.Table)]; ok {
tableFound = true
break
}
}
if !tableFound {
return nil, transform.SameTree, sql.ErrTableNotFound.New(col.Table)
}
}
return col, transform.SameTree, nil
default:
// If any other kind of expression has a star, just replace it
// with an unqualified star because it cannot be expanded.
return transform.Expr(e, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
if _, ok := e.(*expression.Star); ok {
return expression.NewStar(), transform.NewTree, nil
}
return e, transform.SameTree, nil
})
}
}
// validateQualifiedColumn returns true if the table name of the specified column is a valid table name symbol, meaning
// it is available in some scope of the current statement. If a valid table name symbol can't be found, false is returned.
func validateQualifiedColumn(col column, symbols availableNames) bool {
for scopeLevel := range symbols {
tables := symbols.tablesAtLevel(scopeLevel)
if _, ok := tables[strings.ToLower(col.Table())]; ok {
return true
}
}
if symbols.hasTableCol(tableCol{table: strings.ToLower(col.Table()), col: strings.ToLower(col.Name())}) {
return true
}
return false
}
func getColumnsInNodes(nodes []sql.Node, names availableNames, scopeLevel int) {
indexExpressions := func(exprs []sql.Expression) {
for _, e := range exprs {
switch e := e.(type) {
case *expression.Alias:
// Mark this as an available column; we'll record the Alias information later
names.indexColumn("", e.Name(), scopeLevel)
case *expression.GetField:
names.indexColumn(e.Table(), e.Name(), scopeLevel)
}
}
}
for _, node := range nodes {
switch n := node.(type) {
case *plan.TableAlias, *plan.ResolvedTable, *plan.SubqueryAlias, *plan.ValueDerivedTable, *plan.RecursiveTable, *plan.JSONTable:
for _, col := range n.Schema() {
names.indexColumn(col.Source, col.Name, scopeLevel)
}
case sql.Projector:
indexExpressions(n.ProjectedExprs())
getColumnsInNodes(node.Children(), names, scopeLevel)
default:
getColumnsInNodes(n.Children(), names, scopeLevel)
}
}
}
var errGlobalVariablesNotSupported = errors.NewKind("can't resolve global variable, %s was requested")
// resolveJSONTables is a helper function that resolves JSONTables in join as they have special visibility into the left side of the join
// This function should return a *plan.JSONTable when there's no error
func resolveJSONTables(ctx *sql.Context, a *Analyzer, scope *Scope, left sql.Node, jt *plan.JSONTable) (sql.Node, transform.TreeIdentity, error) {
if jt.Resolved() {
return jt, transform.SameTree, nil
}
// wrap left in project node to get scope.Schema to work correctly
proj := plan.NewProject([]sql.Expression{}, left)
rightScope := scope.newScope(proj)
// json table has visibility into columns on left of joins
columns, err := indexColumns(ctx, a, jt, rightScope)
if err != nil {
return nil, transform.SameTree, err
}
newJt, same, err := transform.OneNodeExprsWithNode(jt, func(n sql.Node, e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
uc, ok := e.(column)
if !ok || e.Resolved() {
return e, transform.SameTree, nil
}
return resolveColumnExpression(a, n, uc, columns)
})
if err != nil {
return nil, transform.SameTree, err
}
if same {
return jt, transform.SameTree, nil
}
return newJt, transform.NewTree, nil
}
func resolveJSONTablesInJoin(ctx *sql.Context, a *Analyzer, node sql.Node, scope *Scope, sel RuleSelector) (sql.Node, transform.TreeIdentity, error) {
return transform.Node(node, func(n sql.Node) (sql.Node, transform.TreeIdentity, error) {
if n.Resolved() {
return n, transform.SameTree, nil
}
var jtNew sql.Node
var jtSame transform.TreeIdentity
var jtErr error
switch j := n.(type) {
case *plan.JoinNode:
switch {
case j.Op.IsNatural() || j.Op.IsInner():
if jt, ok := j.Right().(*plan.JSONTable); ok {
jtNew, jtSame, jtErr = resolveJSONTables(ctx, a, scope, j.Left(), jt)
}
default:
return n, transform.SameTree, nil
}
default:
return n, transform.SameTree, nil
}
if jtErr != nil {
return nil, transform.SameTree, jtErr
}
if jtNew == nil || jtSame {
return n, transform.SameTree, nil
}
newN, err := n.WithChildren(n.Children()[0], jtNew)
if err != nil {
return nil, transform.SameTree, err
}
if _, ok := newN.(sql.Expressioner); !ok {
return newN, transform.NewTree, nil
}
columns, err := indexColumns(ctx, a, newN, scope)
if err != nil {
return nil, transform.SameTree, err
}
ret, _, err := transform.OneNodeExprsWithNode(newN, func(n sql.Node, e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
uc, ok := e.(column)
if !ok || e.Resolved() {
return e, transform.SameTree, nil
}
return resolveColumnExpression(a, newN, uc, columns)
})
if err != nil {
return nil, transform.SameTree, nil
}
return ret, transform.NewTree, nil
})
}
// resolveColumns replaces UnresolvedColumn expressions with GetField expressions for the appropriate numbered field in
// the expression's child node.
func resolveColumns(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope, sel RuleSelector) (sql.Node, transform.TreeIdentity, error) {
span, ctx := ctx.Span("resolve_columns")
defer span.End()
n, same1, err := transform.Node(n, func(n sql.Node) (sql.Node, transform.TreeIdentity, error) {
if n.Resolved() {
return n, transform.SameTree, nil
}
if _, ok := n.(sql.Expressioner); !ok {
return n, transform.SameTree, nil
}
// We need to use the schema, so all children must be resolved.
// TODO: also enforce the equivalent constraint for outer scopes. More complicated, because the outer scope can't
// be Resolved() owing to a child expression (the one being evaluated) not being resolved yet.
for _, c := range n.Children() {
if !c.Resolved() {
return n, transform.SameTree, nil
}
}
columns, err := indexColumns(ctx, a, n, scope)
if err != nil {
return nil, transform.SameTree, err
}
return transform.OneNodeExprsWithNode(n, func(n sql.Node, e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
uc, ok := e.(column)
if !ok || e.Resolved() {
return e, transform.SameTree, nil
}
return resolveColumnExpression(a, n, uc, columns)
})
})
if err != nil {
return nil, transform.SameTree, err
}
n, same2, err := resolveJSONTablesInJoin(ctx, a, n, scope, sel)
if err != nil {
return nil, transform.SameTree, err
}
return n, same1 && same2, nil
}
// indexColumns returns a map of column identifiers to their index in the node's schema. Columns from outer scopes are
// included as well, with lower indexes (prepended to node schema) but lower precedence (overwritten by inner nodes in
// map)
func indexColumns(_ *sql.Context, _ *Analyzer, n sql.Node, scope *Scope) (map[tableCol]indexedCol, error) {
var columns = make(map[tableCol]indexedCol)
var idx int
indexColumn := func(col *sql.Column) {
columns[tableCol{
table: strings.ToLower(col.Source),
col: strings.ToLower(col.Name),
}] = indexedCol{col, idx}
idx++
}
indexSchema := func(n sql.Schema) {
for _, col := range n {
indexColumn(col)
}
}
var indexColumnExpr func(e sql.Expression)
indexColumnExpr = func(e sql.Expression) {
switch e := e.(type) {
case *expression.Alias:
// Aliases get indexed twice with the same index number: once with the aliased name and once with the
// underlying name
indexColumn(transform.ExpressionToColumn(e))
idx--
indexColumnExpr(e.Child)
default:
indexColumn(transform.ExpressionToColumn(e))
}
}
indexChildNode := func(n sql.Node) {
switch n := n.(type) {
case sql.Projector:
for _, e := range n.ProjectedExprs() {
indexColumnExpr(e)
}
case *plan.Values:
// values nodes don't have a schema to index like other nodes that provide columns
default:
indexSchema(n.Schema())
}
}
if scope.OuterRelUnresolved() {
// the columns in this relation will be mis-indexed, skip
// until outer rel is resolved
return nil, nil
}
// Index the columns in the outer scope, outer to inner. This means inner scope columns will overwrite the outer
// ones of the same name. This matches the MySQL scope precedence rules.
indexSchema(scope.Schema())
// For the innermost scope (the node being evaluated), look at the schemas of the children instead of this node
// itself. Skip this for DDL nodes that handle indexing separately.
shouldIndexChildNode := true
switch n.(type) {
case *plan.AddColumn, *plan.ModifyColumn:
shouldIndexChildNode = false
case *plan.RecursiveCte, *plan.Union:
shouldIndexChildNode = false
}
if shouldIndexChildNode {
for _, child := range n.Children() {
indexChildNode(child)
}
}
// For certain DDL nodes, we have to do more work
indexSchemaForDefaults := func(column *sql.Column, order *sql.ColumnOrder, sch sql.Schema) {
tblSch := make(sql.Schema, len(sch))
copy(tblSch, sch)
if order == nil {
tblSch = append(tblSch, column)
} else if order.First {
tblSch = append(sql.Schema{column}, tblSch...)
} else { // must be After
index := 1
afterColumn := strings.ToLower(order.AfterColumn)
for _, col := range tblSch {
if strings.ToLower(col.Name) == afterColumn {
break
}
index++
}
if index <= len(tblSch) {
tblSch = append(tblSch, nil)
copy(tblSch[index+1:], tblSch[index:])
tblSch[index] = column
}
}
for _, col := range tblSch {
columns[tableCol{
table: "",
col: strings.ToLower(col.Name),
}] = indexedCol{col, idx}
columns[tableCol{
table: strings.ToLower(col.Source),
col: strings.ToLower(col.Name),
}] = indexedCol{col, idx}
idx++
}
}
switch node := n.(type) {
case *plan.CreateTable: // For this node in particular, the columns will only come into existence after the analyzer step, so we forge them here.
for _, col := range node.CreateSchema.Schema {
columns[tableCol{
table: "",
col: strings.ToLower(col.Name),
}] = indexedCol{col, idx}
columns[tableCol{
table: strings.ToLower(col.Source),
col: strings.ToLower(col.Name),
}] = indexedCol{col, idx}
idx++
}
case *plan.AddColumn: // Add/Modify need to have the full column set in order to resolve a default expression.
tbl := node.Table
indexSchemaForDefaults(node.Column(), node.Order(), tbl.Schema())
case *plan.ModifyColumn:
tbl := node.Table
indexSchemaForDefaults(node.NewColumn(), node.Order(), tbl.Schema())
case *plan.RecursiveCte, *plan.Union:
// opaque nodes have derived schemas
// TODO also subquery aliases?
indexChildNode(node.(sql.BinaryNode).Left())
case *plan.InsertInto:
// should index columns in InsertInto.Source
aliasedTables := make(map[sql.Node]bool)
transform.Inspect(node.Source, func(n sql.Node) bool {
// need to reset idx for each table found, as this function assumes only 1 table
if tblAlias, ok := n.(*plan.TableAlias); ok && tblAlias.Resolved() {
idx = 0
indexSchema(tblAlias.Schema())
aliasedTables[tblAlias.Child] = true
}
return true
})
transform.Inspect(node.Source, func(n sql.Node) bool {
if resTbl, ok := n.(*plan.ResolvedTable); ok && !aliasedTables[resTbl] {
idx = 0
indexSchema(resTbl.Schema())
}
return true
})
transform.Inspect(node.Source, func(n sql.Node) bool {
if resTbl, ok := n.(*plan.SubqueryAlias); ok && resTbl.Resolved() && !aliasedTables[resTbl] {
idx = 0
indexSchema(resTbl.Schema())
}
return true
})
}
return columns, nil
}
func resolveColumnExpression(a *Analyzer, n sql.Node, e column, columns map[tableCol]indexedCol) (sql.Expression, transform.TreeIdentity, error) {
name := strings.ToLower(e.Name())
table := strings.ToLower(e.Table())
col, ok := columns[tableCol{table, name}]
if !ok {
switch uc := e.(type) {
case *expression.UnresolvedColumn:
// Defer the resolution of the column to give the analyzer more
// time to resolve other parts so this can be resolved.
a.Log("deferring resolution of column %s", e)
return &deferredColumn{uc}, transform.NewTree, nil
default:
if table != "" {
return nil, transform.SameTree, sql.ErrTableColumnNotFound.New(e.Table(), e.Name())
}
// This means the expression is either a non-existent column or an alias defined in the same projection.
// Check for the latter first.
// NOTE: For GroupBy nodes, the projected expressions and grouping expressions are both returned from
// Expressions(), so at this point in the code, we can't tell if we are looking at a projected
// expression or a grouping expression here, and the alias resolution rules are different for each.
// We handle this with special casing for GroupBy that transforms identified aliases in grouping
// expressions into AliasReferences, so here we assume that this is a projection expression.
// Being able to differentiate between grouping and projection expressions here could help
// clean up this logic.
aliasesInNode := aliasesDefinedInNode(n)
if stringContains(aliasesInNode, name) {
return nil, transform.SameTree, sql.ErrMisusedAlias.New(name)
}
return nil, transform.SameTree, sql.ErrColumnNotFound.New(e.Name())
}
}
a.Log("column %s resolved to GetFieldWithTable: idx %d, typ %s, table %s, name %s, nullable %t",
e, col.index, col.Type, col.Source, col.Name, col.Nullable)
return expression.NewGetFieldWithTable(
col.index,
col.Type,
col.Source,
col.Name,
col.Nullable,
), transform.NewTree, nil
}
// pushdownGroupByAliases reorders the aggregation in a groupby so aliases defined in it can be resolved in the grouping
// of the groupby. To do so, all aliases are pushed down to a projection node under the group by.
func pushdownGroupByAliases(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope, sel RuleSelector) (sql.Node, transform.TreeIdentity, error) {