-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
table.go
141 lines (116 loc) · 3.66 KB
/
table.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
/*
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 operators
import (
"fmt"
"vitess.io/vitess/go/slice"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vtgate/semantics"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)
type (
Table struct {
QTable *QueryTable
VTable *vindexes.Table
Columns []*sqlparser.ColName
noInputs
}
ColNameColumns interface {
GetColNames() []*sqlparser.ColName
AddCol(*sqlparser.ColName)
}
)
// Clone implements the Operator interface
func (to *Table) Clone([]Operator) Operator {
var columns []*sqlparser.ColName
for _, name := range to.Columns {
columns = append(columns, sqlparser.CloneRefOfColName(name))
}
return &Table{
QTable: to.QTable,
VTable: to.VTable,
Columns: columns,
}
}
// Introduces implements the PhysicalOperator interface
func (to *Table) introducesTableID() semantics.TableSet {
return to.QTable.ID
}
// AddPredicate implements the PhysicalOperator interface
func (to *Table) AddPredicate(_ *plancontext.PlanningContext, expr sqlparser.Expr) Operator {
return newFilter(to, expr)
}
func (to *Table) AddColumn(*plancontext.PlanningContext, bool, bool, *sqlparser.AliasedExpr) int {
panic(vterrors.VT13001("did not expect this method to be called"))
}
func (to *Table) FindCol(ctx *plancontext.PlanningContext, expr sqlparser.Expr, underRoute bool) int {
colToFind, ok := expr.(*sqlparser.ColName)
if !ok {
return -1
}
for idx, colName := range to.Columns {
if colName.Name.Equal(colToFind.Name) {
return idx
}
}
return -1
}
func (to *Table) GetColumns(*plancontext.PlanningContext) []*sqlparser.AliasedExpr {
return slice.Map(to.Columns, colNameToExpr)
}
func (to *Table) GetSelectExprs(ctx *plancontext.PlanningContext) sqlparser.SelectExprs {
return transformColumnsToSelectExprs(ctx, to)
}
func (to *Table) GetOrdering(*plancontext.PlanningContext) []OrderBy {
return nil
}
func (to *Table) GetColNames() []*sqlparser.ColName {
return to.Columns
}
func (to *Table) AddCol(col *sqlparser.ColName) {
to.Columns = append(to.Columns, col)
}
func (to *Table) TablesUsed() []string {
if sqlparser.SystemSchema(to.QTable.Table.Qualifier.String()) {
return nil
}
return SingleQualifiedIdentifier(to.VTable.Keyspace, to.VTable.Name)
}
func addColumn(ctx *plancontext.PlanningContext, op ColNameColumns, e sqlparser.Expr) int {
col, ok := e.(*sqlparser.ColName)
if !ok {
panic(vterrors.VT09018(fmt.Sprintf("cannot add '%s' expression to a table/vindex", sqlparser.String(e))))
}
sqlparser.RemoveKeyspaceInCol(col)
cols := op.GetColNames()
colAsExpr := func(c *sqlparser.ColName) sqlparser.Expr { return c }
if offset, found := canReuseColumn(ctx, cols, e, colAsExpr); found {
return offset
}
offset := len(cols)
op.AddCol(col)
return offset
}
func (to *Table) ShortDescription() string {
tbl := to.VTable.String()
var alias, where string
if to.QTable.Alias.As.NotEmpty() {
alias = " AS " + to.QTable.Alias.As.String()
}
if len(to.QTable.Predicates) > 0 {
where = " WHERE " + sqlparser.String(sqlparser.AndExpressions(to.QTable.Predicates...))
}
return tbl + alias + where
}