forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
1026 lines (985 loc) · 31.8 KB
/
resolver.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 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package plan
import (
"fmt"
"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/util/types"
)
const (
unknownClause = ""
fieldList = "field list"
havingClause = "having clause"
onClause = "on clause"
orderByClause = "order clause"
whereClause = "where clause"
groupByStatement = "group statement"
showStatement = "show statement"
)
// ResolveName resolves table name and column name.
// It generates ResultFields for ResultSetNode and resolves ColumnNameExpr to a ResultField.
func ResolveName(node ast.Node, info infoschema.InfoSchema, ctx context.Context) error {
defaultSchema := ctx.GetSessionVars().CurrentDB
resolver := nameResolver{Info: info, Ctx: ctx, DefaultSchema: model.NewCIStr(defaultSchema)}
node.Accept(&resolver)
return errors.Trace(resolver.Err)
}
// MockResolveName only serves for test.
func MockResolveName(node ast.Node, info infoschema.InfoSchema, defaultSchema string, ctx context.Context) error {
resolver := nameResolver{Info: info, Ctx: ctx, DefaultSchema: model.NewCIStr(defaultSchema)}
node.Accept(&resolver)
return resolver.Err
}
// nameResolver is the visitor to resolve table name and column name.
// In general, a reference can only refer to information that are available for it.
// So children elements are visited in the order that previous elements make information
// available for following elements.
//
// During visiting, information are collected and stored in resolverContext.
// When we enter a subquery, a new resolverContext is pushed to the contextStack, so subquery
// information can overwrite outer query information. When we look up for a column reference,
// we look up from top to bottom in the contextStack.
type nameResolver struct {
Info infoschema.InfoSchema
Ctx context.Context
DefaultSchema model.CIStr
Err error
useOuterContext bool
contextStack []*resolverContext
}
// resolverContext stores information in a single level of select statement
// that table name and column name can be resolved.
type resolverContext struct {
/* For Select Statement. */
// table map to lookup and check table name conflict.
tableMap map[string]int
// table map to lookup and check derived-table(subselect) name conflict.
derivedTableMap map[string]int
// tableSources collected in from clause.
tables []*ast.TableSource
// result fields collected in select field list.
fieldList []*ast.ResultField
// result fields collected in group by clause.
groupBy []*ast.ResultField
// The join node stack is used by on condition to find out
// available tables to reference. On condition can only
// refer to tables involved in current join.
joinNodeStack []*ast.Join
// When visiting TableRefs, tables in this context are not available
// because it is being collected.
inTableRefs bool
// When visiting on condition only tables in current join node are available.
inOnCondition bool
// When visiting field list, fieldList in this context are not available.
inFieldList bool
// When visiting group by, groupBy fields are not available.
inGroupBy bool
// When visiting having, only fieldList and groupBy fields are available.
inHaving bool
// When visiting having, checks if the expr is an aggregate function expr.
inHavingAgg bool
// OrderBy clause has different resolving rule than group by.
inOrderBy bool
// When visiting column name in ByItem, we should know if the column name is in an expression.
inByItemExpression bool
// If subquery use outer context.
useOuterContext bool
// When visiting multi-table delete stmt table list.
inDeleteTableList bool
// When visiting create/drop table statement.
inCreateOrDropTable bool
// When visiting show statement.
inShow bool
// When visiting create/alter table statement.
inColumnOption bool
}
// currentContext gets the current resolverContext.
func (nr *nameResolver) currentContext() *resolverContext {
stackLen := len(nr.contextStack)
if stackLen == 0 {
return nil
}
return nr.contextStack[stackLen-1]
}
// pushContext is called when we enter a statement.
func (nr *nameResolver) pushContext() {
nr.contextStack = append(nr.contextStack, &resolverContext{
tableMap: map[string]int{},
derivedTableMap: map[string]int{},
})
}
// popContext is called when we leave a statement.
func (nr *nameResolver) popContext() {
nr.contextStack = nr.contextStack[:len(nr.contextStack)-1]
}
// pushJoin is called when we enter a join node.
func (nr *nameResolver) pushJoin(j *ast.Join) {
ctx := nr.currentContext()
ctx.joinNodeStack = append(ctx.joinNodeStack, j)
}
// popJoin is called when we leave a join node.
func (nr *nameResolver) popJoin() {
ctx := nr.currentContext()
ctx.joinNodeStack = ctx.joinNodeStack[:len(ctx.joinNodeStack)-1]
}
// Enter implements ast.Visitor interface.
func (nr *nameResolver) Enter(inNode ast.Node) (outNode ast.Node, skipChildren bool) {
switch v := inNode.(type) {
case *ast.AdminStmt:
nr.pushContext()
case *ast.AggregateFuncExpr:
ctx := nr.currentContext()
if ctx.inHaving {
ctx.inHavingAgg = true
}
case *ast.AlterTableStmt:
nr.pushContext()
for _, spec := range v.Specs {
if spec.Tp == ast.AlterTableRenameTable {
nr.currentContext().inCreateOrDropTable = true
break
}
}
case *ast.AnalyzeTableStmt:
nr.pushContext()
case *ast.DropStatsStmt:
nr.pushContext()
case *ast.ByItem:
if _, ok := v.Expr.(*ast.ColumnNameExpr); !ok {
// If ByItem is not a single column name expression,
// the resolving rule is different from order by clause.
nr.currentContext().inByItemExpression = true
}
if nr.currentContext().inGroupBy {
// make sure item is not aggregate function
if ast.HasAggFlag(v.Expr) {
nr.Err = ErrInvalidGroupFuncUse
return inNode, true
}
}
case *ast.CreateIndexStmt:
nr.pushContext()
case *ast.CreateTableStmt:
nr.pushContext()
nr.currentContext().inCreateOrDropTable = true
case *ast.ColumnOption:
nr.currentContext().inColumnOption = true
case *ast.DeleteStmt:
nr.pushContext()
case *ast.DeleteTableList:
nr.currentContext().inDeleteTableList = true
case *ast.DoStmt:
nr.pushContext()
case *ast.DropTableStmt:
nr.pushContext()
nr.currentContext().inCreateOrDropTable = true
case *ast.DropIndexStmt:
nr.pushContext()
case *ast.FieldList:
nr.currentContext().inFieldList = true
case *ast.GroupByClause:
nr.currentContext().inGroupBy = true
case *ast.HavingClause:
nr.currentContext().inHaving = true
case *ast.InsertStmt:
nr.pushContext()
case *ast.LoadDataStmt:
nr.pushContext()
case *ast.Join:
nr.pushJoin(v)
case *ast.OnCondition:
nr.currentContext().inOnCondition = true
case *ast.OrderByClause:
nr.currentContext().inOrderBy = true
case *ast.RenameTableStmt:
nr.pushContext()
nr.currentContext().inCreateOrDropTable = true
case *ast.SelectStmt:
nr.pushContext()
case *ast.SetStmt:
for _, assign := range v.Variables {
if cn, ok := assign.Value.(*ast.ColumnNameExpr); ok && cn.Name.Table.L == "" {
// Convert column name expression to string value expression.
assign.Value = ast.NewValueExpr(cn.Name.Name.O)
}
}
nr.pushContext()
case *ast.ShowStmt:
nr.pushContext()
nr.currentContext().inShow = true
nr.fillShowFields(v)
case *ast.TableRefsClause:
nr.currentContext().inTableRefs = true
case *ast.TruncateTableStmt:
nr.pushContext()
case *ast.UnionStmt:
nr.pushContext()
case *ast.UpdateStmt:
nr.pushContext()
}
return inNode, false
}
// Leave implements ast.Visitor interface.
func (nr *nameResolver) Leave(inNode ast.Node) (node ast.Node, ok bool) {
switch v := inNode.(type) {
case *ast.AdminStmt:
nr.popContext()
case *ast.AggregateFuncExpr:
ctx := nr.currentContext()
if ctx.inHaving {
ctx.inHavingAgg = false
}
case *ast.AlterTableStmt:
nr.popContext()
case *ast.AnalyzeTableStmt:
nr.popContext()
case *ast.DropStatsStmt:
nr.popContext()
case *ast.TableName:
nr.handleTableName(v)
case *ast.ColumnNameExpr:
nr.handleColumnName(v)
case *ast.CreateIndexStmt:
nr.popContext()
case *ast.CreateTableStmt:
nr.popContext()
case *ast.ColumnOption:
nr.currentContext().inColumnOption = false
case *ast.DeleteTableList:
nr.currentContext().inDeleteTableList = false
case *ast.DoStmt:
nr.popContext()
case *ast.DropIndexStmt:
nr.popContext()
case *ast.DropTableStmt:
nr.popContext()
case *ast.TableSource:
nr.handleTableSource(v)
case *ast.OnCondition:
nr.currentContext().inOnCondition = false
case *ast.Join:
nr.handleJoin(v)
nr.popJoin()
case *ast.TableRefsClause:
nr.currentContext().inTableRefs = false
case *ast.FieldList:
nr.handleFieldList(v)
nr.currentContext().inFieldList = false
case *ast.GroupByClause:
ctx := nr.currentContext()
ctx.inGroupBy = false
for _, item := range v.Items {
switch x := item.Expr.(type) {
case *ast.ColumnNameExpr:
ctx.groupBy = append(ctx.groupBy, x.Refer)
}
}
case *ast.HavingClause:
nr.currentContext().inHaving = false
case *ast.OrderByClause:
nr.currentContext().inOrderBy = false
case *ast.ByItem:
nr.currentContext().inByItemExpression = false
case *ast.PositionExpr:
nr.handlePosition(v)
case *ast.RenameTableStmt:
nr.popContext()
case *ast.SelectStmt:
ctx := nr.currentContext()
v.SetResultFields(ctx.fieldList)
if ctx.useOuterContext {
nr.useOuterContext = true
}
nr.popContext()
case *ast.SetStmt:
nr.popContext()
case *ast.ShowStmt:
nr.popContext()
case *ast.SubqueryExpr:
if nr.useOuterContext {
// TODO: check this
// If there is a deep nest of subquery, there may be something wrong.
v.Correlated = true
nr.useOuterContext = false
}
case *ast.TruncateTableStmt:
nr.popContext()
case *ast.UnionStmt:
ctx := nr.currentContext()
v.SetResultFields(ctx.fieldList)
if ctx.useOuterContext {
nr.useOuterContext = true
}
nr.popContext()
case *ast.UnionSelectList:
nr.handleUnionSelectList(v)
case *ast.InsertStmt:
nr.popContext()
case *ast.LoadDataStmt:
nr.popContext()
case *ast.DeleteStmt:
nr.popContext()
case *ast.UpdateStmt:
nr.popContext()
}
return inNode, nr.Err == nil
}
// handleTableName looks up and sets the schema information and result fields for table name.
func (nr *nameResolver) handleTableName(tn *ast.TableName) {
if tn.Schema.L == "" {
sessionVars := nr.Ctx.GetSessionVars()
if sessionVars.CurrentDB == "" {
nr.Err = errors.Trace(ErrNoDB)
return
}
tn.Schema = nr.DefaultSchema
}
ctx := nr.currentContext()
if ctx.inCreateOrDropTable {
// The table may not exist in create table or drop table statement.
// Skip resolving the table to avoid error.
return
}
if ctx.inDeleteTableList {
idx, ok := ctx.tableMap[nr.tableUniqueName(tn.Schema, tn.Name)]
if !ok {
nr.Err = errors.Errorf("Unknown table %s", tn.Name.O)
return
}
ts := ctx.tables[idx]
tableName := ts.Source.(*ast.TableName)
tn.DBInfo = tableName.DBInfo
tn.TableInfo = tableName.TableInfo
tn.SetResultFields(tableName.GetResultFields())
return
}
table, err := nr.Info.TableByName(tn.Schema, tn.Name)
if err != nil {
nr.Err = errors.Trace(err)
return
}
tn.TableInfo = table.Meta()
dbInfo, _ := nr.Info.SchemaByName(tn.Schema)
tn.DBInfo = dbInfo
rfs := make([]*ast.ResultField, 0, len(tn.TableInfo.Columns))
tmp := make([]struct {
ast.ValueExpr
ast.ResultField
}, len(tn.TableInfo.Columns))
status := nr.Ctx.GetSessionVars().StmtCtx
for i, v := range tn.TableInfo.Columns {
if status.InUpdateOrDeleteStmt {
switch v.State {
case model.StatePublic, model.StateWriteOnly, model.StateWriteReorganization:
default:
continue
}
} else {
if v.State != model.StatePublic {
continue
}
}
expr := &tmp[i].ValueExpr
rf := &tmp[i].ResultField
expr.SetType(&v.FieldType)
*rf = ast.ResultField{
Column: v,
Table: tn.TableInfo,
DBName: tn.Schema,
Expr: expr,
TableName: tn,
}
rfs = append(rfs, rf)
}
tn.SetResultFields(rfs)
}
// handleTableSources checks name duplication
// and puts the table source in current resolverContext.
// Note:
// "select * from t as a join (select 1) as a;" is not duplicate.
// "select * from t as a join t as a;" is duplicate.
// "select * from (select 1) as a join (select 1) as a;" is duplicate.
func (nr *nameResolver) handleTableSource(ts *ast.TableSource) {
for _, v := range ts.GetResultFields() {
v.TableAsName = ts.AsName
}
ctx := nr.currentContext()
switch ts.Source.(type) {
case *ast.TableName:
var name string
if ts.AsName.L != "" {
name = ts.AsName.L
} else {
tableName := ts.Source.(*ast.TableName)
name = nr.tableUniqueName(tableName.Schema, tableName.Name)
}
if _, ok := ctx.tableMap[name]; ok {
nr.Err = errors.Errorf("duplicated table/alias name %s", name)
return
}
ctx.tableMap[name] = len(ctx.tables)
case *ast.SelectStmt:
name := ts.AsName.L
if _, ok := ctx.derivedTableMap[name]; ok {
nr.Err = errors.Errorf("duplicated table/alias name %s", name)
return
}
ctx.derivedTableMap[name] = len(ctx.tables)
}
dupNames := make(map[string]struct{}, len(ts.GetResultFields()))
for _, f := range ts.GetResultFields() {
// duplicate column name in one table is not allowed.
// "select * from (select 1, 1) as a;" is duplicate.
name := f.ColumnAsName.L
if name == "" {
name = f.Column.Name.L
}
if _, ok := dupNames[name]; ok {
nr.Err = errors.Errorf("Duplicate column name '%s'", name)
return
}
dupNames[name] = struct{}{}
}
ctx.tables = append(ctx.tables, ts)
}
// handleJoin sets result fields for join.
func (nr *nameResolver) handleJoin(j *ast.Join) {
if j.Right == nil {
j.SetResultFields(j.Left.GetResultFields())
return
}
leftLen := len(j.Left.GetResultFields())
rightLen := len(j.Right.GetResultFields())
rfs := make([]*ast.ResultField, leftLen+rightLen)
copy(rfs, j.Left.GetResultFields())
copy(rfs[leftLen:], j.Right.GetResultFields())
j.SetResultFields(rfs)
}
// handleColumnName looks up and sets ResultField for
// the column name.
func (nr *nameResolver) handleColumnName(cn *ast.ColumnNameExpr) {
ctx := nr.currentContext()
if ctx.inOnCondition {
// In on condition, only tables within current join is available.
nr.resolveColumnNameInOnCondition(cn)
return
}
if ctx.inColumnOption {
// In column option, only columns in current create table statement
// is available. But we check it in ddl/ddl_api.go.
return
}
// Try to resolve the column name from top to bottom in the context stack.
var where string
var ok bool
for i := len(nr.contextStack) - 1; i >= 0; i-- {
where, ok = nr.resolveColumnNameInContext(nr.contextStack[i], cn)
if ok {
// Column is already resolved or encountered an error.
if i < len(nr.contextStack)-1 {
// If in subselect, the query use outer query.
nr.currentContext().useOuterContext = true
}
return
}
}
fieldName := cn.Name.Name.String()
if len(cn.Name.Table.String()) != 0 {
fieldName = fmt.Sprintf("%s.%s", cn.Name.Table.String(), fieldName)
}
nr.Err = ErrUnknownColumn.GenByArgs(fieldName, where)
}
// resolveColumnNameInContext looks up and sets ResultField for a column with the ctx.
func (nr *nameResolver) resolveColumnNameInContext(ctx *resolverContext, cn *ast.ColumnNameExpr) (string, bool) {
if ctx.inTableRefs {
// In TableRefsClause, column reference only in join on condition which is handled before.
return unknownClause, false
}
if ctx.inFieldList {
// only resolve column using tables.
return fieldList, nr.resolveColumnInTableSources(cn, ctx.tables)
}
if ctx.inGroupBy {
// From tables first, then field list.
// If ctx.InByItemExpression is true, the item is not an identifier.
// Otherwise it is an identifier.
if ctx.inByItemExpression {
// From table first, then field list.
if nr.resolveColumnInTableSources(cn, ctx.tables) {
return groupByStatement, true
}
found := nr.resolveColumnInResultFields(ctx, cn, ctx.fieldList)
if nr.Err == nil && found {
// Check if resolved refer is an aggregate function expr.
if _, ok := cn.Refer.Expr.(*ast.AggregateFuncExpr); ok {
nr.Err = ErrIllegalReference.Gen("Reference '%s' not supported (reference to group function)", cn.Name.Name.O)
}
}
return groupByStatement, found
}
// Resolve from table first, then from select list.
found := nr.resolveColumnInTableSources(cn, ctx.tables)
if nr.Err != nil {
return groupByStatement, found
}
// We should copy the refer here.
// Because if the ByItem is an identifier, we should check if it
// is ambiguous even it is already resolved from table source.
// If the ByItem is not an identifier, we do not need the second check.
r := cn.Refer
if nr.resolveColumnInResultFields(ctx, cn, ctx.fieldList) {
if nr.Err != nil {
return groupByStatement, true
}
if r != nil {
// It is not ambiguous and already resolved from table source.
// We should restore its Refer.
cn.Refer = r
} else if _, ok := cn.Refer.Expr.(*ast.AggregateFuncExpr); ok {
nr.Err = ErrIllegalReference.Gen("Reference '%s' not supported (reference to group function)", cn.Name.Name.O)
}
return groupByStatement, true
}
return groupByStatement, found
}
if ctx.inHaving {
// First group by, then field list.
if nr.resolveColumnInResultFields(ctx, cn, ctx.groupBy) {
return havingClause, true
}
if ctx.inHavingAgg {
// If cn is in an aggregate function in having clause, check tablesource first.
if nr.resolveColumnInTableSources(cn, ctx.tables) {
return havingClause, true
}
}
return havingClause, nr.resolveColumnInResultFields(ctx, cn, ctx.fieldList)
}
if ctx.inOrderBy {
if nr.resolveColumnInResultFields(ctx, cn, ctx.groupBy) {
return orderByClause, true
}
if ctx.inByItemExpression {
// From table first, then field list.
if nr.resolveColumnInTableSources(cn, ctx.tables) {
return orderByClause, true
}
return orderByClause, nr.resolveColumnInResultFields(ctx, cn, ctx.fieldList)
}
// Field list first, then from table.
if nr.resolveColumnInResultFields(ctx, cn, ctx.fieldList) {
return orderByClause, true
}
return orderByClause, nr.resolveColumnInTableSources(cn, ctx.tables)
}
if ctx.inShow {
return showStatement, nr.resolveColumnInResultFields(ctx, cn, ctx.fieldList)
}
// In where clause.
return whereClause, nr.resolveColumnInTableSources(cn, ctx.tables)
}
// resolveColumnNameInOnCondition resolves the column name in current join.
func (nr *nameResolver) resolveColumnNameInOnCondition(cn *ast.ColumnNameExpr) {
ctx := nr.currentContext()
join := ctx.joinNodeStack[len(ctx.joinNodeStack)-1]
tableSources := appendTableSources(nil, join)
if !nr.resolveColumnInTableSources(cn, tableSources) {
fieldName := cn.Name.Name.String()
if len(cn.Name.Table.String()) != 0 {
fieldName = fmt.Sprintf("%s.%s", cn.Name.Table.String(), fieldName)
}
nr.Err = ErrUnknownColumn.GenByArgs(fieldName, onClause)
}
}
func (nr *nameResolver) resolveColumnInTableSources(cn *ast.ColumnNameExpr, tableSources []*ast.TableSource) (done bool) {
var matchedResultField *ast.ResultField
tableNameL := cn.Name.Table.L
columnNameL := cn.Name.Name.L
if tableNameL != "" {
var matchedTable ast.ResultSetNode
for _, ts := range tableSources {
if tableNameL == ts.AsName.L {
// different table name.
matchedTable = ts
break
} else if ts.AsName.L != "" {
// Table as name shadows table real name.
continue
}
if tn, ok := ts.Source.(*ast.TableName); ok {
if cn.Name.Schema.L != "" && cn.Name.Schema.L != tn.Schema.L {
continue
}
if tableNameL == tn.Name.L {
matchedTable = ts
}
}
}
if matchedTable != nil {
resultFields := matchedTable.GetResultFields()
for _, rf := range resultFields {
if rf.ColumnAsName.L == columnNameL || rf.Column.Name.L == columnNameL {
// resolve column.
matchedResultField = rf
break
}
}
}
} else {
for _, ts := range tableSources {
rfs := ts.GetResultFields()
for _, rf := range rfs {
matchAsName := rf.ColumnAsName.L != "" && rf.ColumnAsName.L == columnNameL
matchColumnName := rf.ColumnAsName.L == "" && rf.Column.Name.L == columnNameL
if matchAsName || matchColumnName {
matchedResultField = rf
}
}
}
}
if matchedResultField != nil {
// Bind column.
cn.Refer = matchedResultField
matchedResultField.Referenced = true
return true
}
return false
}
func (nr *nameResolver) resolveColumnInResultFields(ctx *resolverContext, cn *ast.ColumnNameExpr, rfs []*ast.ResultField) bool {
var matched *ast.ResultField
for _, rf := range rfs {
if cn.Name.Table.L != "" {
// Check table name
if rf.TableAsName.L != "" {
if cn.Name.Table.L != rf.TableAsName.L {
continue
}
} else if cn.Name.Table.L != rf.Table.Name.L {
continue
}
}
matchAsName := cn.Name.Name.L == rf.ColumnAsName.L
var matchColumnName bool
if ctx.inHaving {
matchColumnName = cn.Name.Name.L == rf.Column.Name.L
} else {
matchColumnName = rf.ColumnAsName.L == "" && cn.Name.Name.L == rf.Column.Name.L
}
if matchAsName || matchColumnName {
if rf.Column.Name.L == "" {
// This is not a real table column, resolve it directly.
cn.Refer = rf
rf.Referenced = true
return true
}
if matched == nil {
matched = rf
}
}
}
if matched != nil {
// If in GroupBy, we clone the ResultField
if ctx.inGroupBy || ctx.inHaving || ctx.inOrderBy {
nf := *matched
expr := matched.Expr
if cexpr, ok := expr.(*ast.ColumnNameExpr); ok {
expr = cexpr.Refer.Expr
}
nf.Expr = expr
matched = &nf
}
// Bind column.
cn.Refer = matched
matched.Referenced = true
return true
}
return false
}
// handleFieldList expands wild card field and sets fieldList in current context.
func (nr *nameResolver) handleFieldList(fieldList *ast.FieldList) {
var resultFields []*ast.ResultField
for _, v := range fieldList.Fields {
resultFields = append(resultFields, nr.createResultFields(v)...)
}
nr.currentContext().fieldList = resultFields
}
func getInnerFromParentheses(expr ast.ExprNode) ast.ExprNode {
if pexpr, ok := expr.(*ast.ParenthesesExpr); ok {
return getInnerFromParentheses(pexpr.Expr)
}
return expr
}
// createResultFields creates result field list for a single select field.
func (nr *nameResolver) createResultFields(field *ast.SelectField) (rfs []*ast.ResultField) {
ctx := nr.currentContext()
if field.WildCard != nil {
if len(ctx.tables) == 0 {
nr.Err = errors.New("no table used")
return
}
tableRfs := []*ast.ResultField{}
if field.WildCard.Table.L == "" {
for _, v := range ctx.tables {
tableRfs = append(tableRfs, v.GetResultFields()...)
}
} else {
name := nr.tableUniqueName(field.WildCard.Schema, field.WildCard.Table)
tableIdx, ok1 := ctx.tableMap[name]
derivedTableIdx, ok2 := ctx.derivedTableMap[name]
if !ok1 && !ok2 {
nr.Err = ErrUnknownTable.GenByArgs(field.WildCard.Table.String())
}
if ok1 {
tableRfs = ctx.tables[tableIdx].GetResultFields()
}
if ok2 {
tableRfs = append(tableRfs, ctx.tables[derivedTableIdx].GetResultFields()...)
}
}
for _, trf := range tableRfs {
trf.Referenced = true
// Convert it to ColumnNameExpr
cn := &ast.ColumnName{
Schema: trf.DBName,
Table: trf.Table.Name,
Name: trf.ColumnAsName,
}
cnExpr := &ast.ColumnNameExpr{
Name: cn,
Refer: trf,
}
ast.SetFlag(cnExpr)
cnExpr.SetType(trf.Expr.GetType())
rf := *trf
rf.Expr = cnExpr
rfs = append(rfs, &rf)
}
return
}
// The column is visited before so it must has been resolved already.
rf := &ast.ResultField{ColumnAsName: field.AsName}
innerExpr := getInnerFromParentheses(field.Expr)
switch v := innerExpr.(type) {
case *ast.ColumnNameExpr:
rf.Column = v.Refer.Column
rf.Table = v.Refer.Table
rf.DBName = v.Refer.DBName
rf.TableName = v.Refer.TableName
rf.Expr = v
default:
rf.Column = &model.ColumnInfo{} // Empty column info.
rf.Table = &model.TableInfo{} // Empty table info.
rf.Expr = v
}
if field.AsName.L == "" {
switch x := innerExpr.(type) {
case *ast.ColumnNameExpr:
rf.ColumnAsName = model.NewCIStr(x.Name.Name.O)
case *ast.ValueExpr:
if innerExpr.Text() != "" {
rf.ColumnAsName = model.NewCIStr(innerExpr.Text())
} else {
rf.ColumnAsName = model.NewCIStr(field.Text())
}
default:
rf.ColumnAsName = model.NewCIStr(field.Text())
}
}
rfs = append(rfs, rf)
return
}
func appendTableSources(in []*ast.TableSource, resultSetNode ast.ResultSetNode) (out []*ast.TableSource) {
switch v := resultSetNode.(type) {
case *ast.TableSource:
out = append(in, v)
case *ast.Join:
out = appendTableSources(in, v.Left)
if v.Right != nil {
out = appendTableSources(out, v.Right)
}
}
return
}
func (nr *nameResolver) tableUniqueName(schema, table model.CIStr) string {
if schema.L != "" && schema.L != nr.DefaultSchema.L {
return schema.L + "." + table.L
}
return table.L
}
func (nr *nameResolver) handlePosition(pos *ast.PositionExpr) {
ctx := nr.currentContext()
if pos.N < 1 || pos.N > len(ctx.fieldList) {
nr.Err = errors.Errorf("Unknown column '%d'", pos.N)
return
}
matched := ctx.fieldList[pos.N-1]
nf := *matched
expr := matched.Expr
if cexpr, ok := expr.(*ast.ColumnNameExpr); ok {
expr = cexpr.Refer.Expr
}
nf.Expr = expr
pos.Refer = &nf
pos.Refer.Referenced = true
if nr.currentContext().inGroupBy {
// make sure item is not aggregate function
if ast.HasAggFlag(pos.Refer.Expr) {
nr.Err = errors.New("group by cannot contain aggregate function")
}
}
}
func (nr *nameResolver) handleUnionSelectList(u *ast.UnionSelectList) {
firstSelFields := u.Selects[0].GetResultFields()
unionFields := make([]*ast.ResultField, len(firstSelFields))
// Copy first result fields, because we may change the result field type.
for i, v := range firstSelFields {
rf := *v
col := *v.Column
rf.Column = &col
if rf.Column.Flen == 0 {
rf.Column.Flen = types.UnspecifiedLength
}
rf.Expr = &ast.ValueExpr{}
unionFields[i] = &rf
}
nr.currentContext().fieldList = unionFields
}
func (nr *nameResolver) fillShowFields(s *ast.ShowStmt) {
if s.DBName == "" {
if s.Table != nil && s.Table.Schema.L != "" {
s.DBName = s.Table.Schema.O
} else {
s.DBName = nr.DefaultSchema.O
}
} else if s.Table != nil && s.Table.Schema.L == "" {
s.Table.Schema = model.NewCIStr(s.DBName)
}
var fields []*ast.ResultField
var (
names []string
ftypes []byte
)
switch s.Tp {
case ast.ShowEngines:
names = []string{"Engine", "Support", "Comment", "Transactions", "XA", "Savepoints"}
case ast.ShowDatabases:
names = []string{"Database"}
case ast.ShowTables:
if s.DBName == "" {
nr.Err = errors.Trace(ErrNoDB)
return
}
names = []string{fmt.Sprintf("Tables_in_%s", s.DBName)}
if s.Full {
names = append(names, "Table_type")
}
case ast.ShowTableStatus:
if s.DBName == "" {
nr.Err = errors.Trace(ErrNoDB)
return
}
names = []string{"Name", "Engine", "Version", "Row_format", "Rows", "Avg_row_length",
"Data_length", "Max_data_length", "Index_length", "Data_free", "Auto_increment",
"Create_time", "Update_time", "Check_time", "Collation", "Checksum",
"Create_options", "Comment"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeLonglong, mysql.TypeVarchar, mysql.TypeLonglong, mysql.TypeLonglong,
mysql.TypeLonglong, mysql.TypeLonglong, mysql.TypeLonglong, mysql.TypeLonglong, mysql.TypeLonglong,
mysql.TypeDatetime, mysql.TypeDatetime, mysql.TypeDatetime, mysql.TypeVarchar, mysql.TypeVarchar,
mysql.TypeVarchar, mysql.TypeVarchar}
case ast.ShowColumns:
names = table.ColDescFieldNames(s.Full)
case ast.ShowWarnings:
names = []string{"Level", "Code", "Message"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeLong, mysql.TypeVarchar}
case ast.ShowCharset:
names = []string{"Charset", "Description", "Default collation", "Maxlen"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeLonglong}
case ast.ShowVariables:
names = []string{"Variable_name", "Value"}
case ast.ShowStatus:
names = []string{"Variable_name", "Value"}
case ast.ShowCollation:
names = []string{"Collation", "Charset", "Id", "Default", "Compiled", "Sortlen"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeLonglong,
mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeLonglong}
case ast.ShowCreateTable:
names = []string{"Table", "Create Table"}
case ast.ShowCreateDatabase:
names = []string{"Database", "Create Database"}
case ast.ShowGrants:
names = []string{fmt.Sprintf("Grants for %s", s.User)}
case ast.ShowTriggers:
names = []string{"Trigger", "Event", "Table", "Statement", "Timing", "Created",
"sql_mode", "Definer", "character_set_client", "collation_connection", "Database Collation"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar,
mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar}
case ast.ShowProcedureStatus, ast.ShowEvents:
names = []string{}
ftypes = []byte{}
case ast.ShowIndex:
names = []string{"Table", "Non_unique", "Key_name", "Seq_in_index",
"Column_name", "Collation", "Cardinality", "Sub_part", "Packed",
"Null", "Index_type", "Comment", "Index_comment"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeLonglong, mysql.TypeVarchar, mysql.TypeLonglong,
mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeLonglong, mysql.TypeLonglong,
mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar}
case ast.ShowProcessList:
names = []string{"Id", "User", "Host", "db", "Command", "Time", "State", "Info"}
ftypes = []byte{mysql.TypeLonglong, mysql.TypeVarchar, mysql.TypeVarchar,
mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeLong, mysql.TypeVarchar, mysql.TypeString}
case ast.ShowStatsMeta:
names = []string{"Db_name", "Table_name", "Update_time", "Modify_count", "Row_count"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeDatetime, mysql.TypeLonglong, mysql.TypeLonglong}
case ast.ShowStatsHistograms:
names = []string{"Db_name", "Table_name", "Column_name", "Is_index", "Update_time", "Distinct_count", "Null_count"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeTiny, mysql.TypeDatetime,
mysql.TypeLonglong, mysql.TypeLonglong}
case ast.ShowStatsBuckets:
names = []string{"Db_name", "Table_name", "Column_name", "Is_index", "Bucket_id", "Count",
"Repeats", "Lower_Bound", "Upper_Bound"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeTiny, mysql.TypeLonglong,
mysql.TypeLonglong, mysql.TypeLonglong, mysql.TypeVarchar, mysql.TypeVarchar}
}
for i, name := range names {
f := &ast.ResultField{