-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
vtable.go
163 lines (140 loc) · 4.41 KB
/
vtable.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
/*
Copyright 2021 The Vitess Authors.
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 semantics
import (
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)
// vTableInfo is used to represent projected results, not real tables. It is used for
// ORDER BY, GROUP BY and HAVING that need to access result columns
type vTableInfo struct {
tableName string
columnNames []string
cols []sqlparser.Expr
tables TableSet
}
var _ TableInfo = (*vTableInfo)(nil)
// Dependencies implements the TableInfo interface
func (v *vTableInfo) dependencies(colName string, org originable) (dependencies, error) {
var deps dependencies = ¬hing{}
var err error
for i, name := range v.columnNames {
if name != colName {
continue
}
recursiveDeps, qt := org.depsForExpr(v.cols[i])
var directDeps TableSet
/*
If we find a match, it means the query looks something like:
SELECT 1 as x FROM t1 ORDER BY/GROUP BY x - d/r: 0/0
SELECT t1.x as x FROM t1 ORDER BY/GROUP BY x - d/r: 0/1
SELECT x FROM t1 ORDER BY/GROUP BY x - d/r: 1/1
Now, after figuring out the recursive deps
*/
if recursiveDeps.NumberOfTables() > 0 {
directDeps = recursiveDeps
}
newDeps := createCertain(directDeps, recursiveDeps, qt)
deps, err = deps.merge(newDeps)
if err != nil {
return nil, err
}
}
if deps.empty() && v.hasStar() {
return createUncertain(v.tables, v.tables), nil
}
return deps, nil
}
// IsInfSchema implements the TableInfo interface
func (v *vTableInfo) IsInfSchema() bool {
return false
}
func (v *vTableInfo) matches(name sqlparser.TableName) bool {
return v.tableName == name.Name.String() && name.Qualifier.IsEmpty()
}
func (v *vTableInfo) authoritative() bool {
return true
}
func (v *vTableInfo) Name() (sqlparser.TableName, error) {
return sqlparser.TableName{}, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "oh noes")
}
func (v *vTableInfo) getExpr() *sqlparser.AliasedTableExpr {
return nil
}
// GetVindexTable implements the TableInfo interface
func (v *vTableInfo) GetVindexTable() *vindexes.Table {
return nil
}
func (v *vTableInfo) getColumns() []ColumnInfo {
cols := make([]ColumnInfo, 0, len(v.columnNames))
for _, col := range v.columnNames {
cols = append(cols, ColumnInfo{
Name: col,
})
}
return cols
}
func (v *vTableInfo) hasStar() bool {
return v.tables.NumberOfTables() > 0
}
// GetTables implements the TableInfo interface
func (v *vTableInfo) getTableSet(org originable) TableSet {
return v.tables
}
// GetExprFor implements the TableInfo interface
func (v *vTableInfo) getExprFor(s string) (sqlparser.Expr, error) {
for i, colName := range v.columnNames {
if colName == s {
return v.cols[i], nil
}
}
return nil, vterrors.NewErrorf(vtrpcpb.Code_NOT_FOUND, vterrors.BadFieldError, "Unknown column '%s' in 'field list'", s)
}
func createVTableInfoForExpressions(expressions sqlparser.SelectExprs, tables []TableInfo, org originable) *vTableInfo {
cols, colNames, ts := selectExprsToInfos(expressions, tables, org)
return &vTableInfo{
columnNames: colNames,
cols: cols,
tables: ts,
}
}
func selectExprsToInfos(
expressions sqlparser.SelectExprs,
tables []TableInfo,
org originable,
) (cols []sqlparser.Expr, colNames []string, ts TableSet) {
for _, selectExpr := range expressions {
switch expr := selectExpr.(type) {
case *sqlparser.AliasedExpr:
cols = append(cols, expr.Expr)
if expr.As.IsEmpty() {
switch expr := expr.Expr.(type) {
case *sqlparser.ColName:
// for projections, we strip out the qualifier and keep only the column name
colNames = append(colNames, expr.Name.String())
default:
colNames = append(colNames, sqlparser.String(expr))
}
} else {
colNames = append(colNames, expr.As.String())
}
case *sqlparser.StarExpr:
for _, table := range tables {
ts.MergeInPlace(table.getTableSet(org))
}
}
}
return
}