/
operator_to_query.go
290 lines (257 loc) · 7.88 KB
/
operator_to_query.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
/*
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 planbuilder
import (
"fmt"
"sort"
"strings"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vtgate/planbuilder/physical"
"vitess.io/vitess/go/vt/vtgate/semantics"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vtgate/planbuilder/abstract"
)
type queryBuilder struct {
ctx *plancontext.PlanningContext
sel sqlparser.SelectStatement
tableNames []string
}
func toSQL(ctx *plancontext.PlanningContext, op abstract.PhysicalOperator) sqlparser.SelectStatement {
q := &queryBuilder{ctx: ctx}
buildQuery(op, q)
q.sortTables()
return q.sel
}
func buildQuery(op abstract.PhysicalOperator, qb *queryBuilder) {
switch op := op.(type) {
case *physical.Table:
dbName := ""
if op.QTable.IsInfSchema {
dbName = op.QTable.Table.Qualifier.String()
}
qb.addTable(dbName, op.QTable.Table.Name.String(), op.QTable.Alias.As.String(), op.TableID(), op.QTable.Alias.Hints)
for _, pred := range op.QTable.Predicates {
qb.addPredicate(pred)
}
for _, name := range op.Columns {
qb.addProjection(&sqlparser.AliasedExpr{Expr: name})
}
case *physical.ApplyJoin:
buildQuery(op.LHS, qb)
// If we are going to add the predicate used in join here
// We should not add the predicate's copy of when it was split into
// two parts. To avoid this, we use the SkipPredicates map.
for _, expr := range qb.ctx.JoinPredicates[op.Predicate] {
qb.ctx.SkipPredicates[expr] = nil
}
qbR := &queryBuilder{ctx: qb.ctx}
buildQuery(op.RHS, qbR)
if op.LeftJoin {
qb.joinOuterWith(qbR, op.Predicate)
} else {
qb.joinInnerWith(qbR, op.Predicate)
}
case *physical.Filter:
buildQuery(op.Source, qb)
for _, pred := range op.Predicates {
qb.addPredicate(pred)
}
case *physical.Derived:
buildQuery(op.Source, qb)
sel := qb.sel.(*sqlparser.Select) // we can only handle SELECT in derived tables at the moment
qb.sel = nil
opQuery := sqlparser.RemoveKeyspace(op.Query).(*sqlparser.Select)
sel.Limit = opQuery.Limit
sel.OrderBy = opQuery.OrderBy
sel.GroupBy = opQuery.GroupBy
sel.Having = opQuery.Having
sel.SelectExprs = opQuery.SelectExprs
qb.addTableExpr(op.Alias, op.Alias, op.TableID(), &sqlparser.DerivedTable{
Select: sel,
}, nil, op.ColumnAliases)
for _, col := range op.Columns {
qb.addProjection(&sqlparser.AliasedExpr{Expr: col})
}
default:
panic(fmt.Sprintf("%T", op))
}
}
func (qb *queryBuilder) sortTables() {
_ = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
sel, isSel := node.(*sqlparser.Select)
if !isSel {
return true, nil
}
ts := &tableSorter{
sel: sel,
tbl: qb.ctx.SemTable,
}
sort.Sort(ts)
return true, nil
}, qb.sel)
}
func (qb *queryBuilder) addTable(db, tableName, alias string, tableID semantics.TableSet, hints sqlparser.IndexHints) {
tableExpr := sqlparser.TableName{
Name: sqlparser.NewIdentifierCS(tableName),
Qualifier: sqlparser.NewIdentifierCS(db),
}
qb.addTableExpr(tableName, alias, tableID, tableExpr, hints, nil)
}
func (qb *queryBuilder) addTableExpr(
tableName, alias string,
tableID semantics.TableSet,
tblExpr sqlparser.SimpleTableExpr,
hints sqlparser.IndexHints,
columnAliases sqlparser.Columns,
) {
if qb.sel == nil {
qb.sel = &sqlparser.Select{}
}
sel := qb.sel.(*sqlparser.Select)
elems := &sqlparser.AliasedTableExpr{
Expr: tblExpr,
Partitions: nil,
As: sqlparser.NewIdentifierCS(alias),
Hints: hints,
Columns: columnAliases,
}
err := qb.ctx.SemTable.ReplaceTableSetFor(tableID, elems)
if err != nil {
log.Warningf("error in replacing table expression in semtable: %v", err)
}
sel.From = append(sel.From, elems)
qb.sel = sel
qb.tableNames = append(qb.tableNames, tableName)
}
func (qb *queryBuilder) addPredicate(expr sqlparser.Expr) {
if _, toBeSkipped := qb.ctx.SkipPredicates[expr]; toBeSkipped {
// This is a predicate that was added to the RHS of an ApplyJoin.
// The original predicate will be added, so we don't have to add this here
return
}
sel := qb.sel.(*sqlparser.Select)
if sel.Where == nil {
sel.AddWhere(expr)
return
}
for _, exp := range sqlparser.SplitAndExpression(nil, expr) {
sel.AddWhere(exp)
}
}
func (qb *queryBuilder) addProjection(projection *sqlparser.AliasedExpr) {
sel := qb.sel.(*sqlparser.Select)
sel.SelectExprs = append(sel.SelectExprs, projection)
}
func (qb *queryBuilder) joinInnerWith(other *queryBuilder, onCondition sqlparser.Expr) {
sel := qb.sel.(*sqlparser.Select)
otherSel := other.sel.(*sqlparser.Select)
sel.From = append(sel.From, otherSel.From...)
sel.SelectExprs = append(sel.SelectExprs, otherSel.SelectExprs...)
var predicate sqlparser.Expr
if sel.Where != nil {
predicate = sel.Where.Expr
}
if otherSel.Where != nil {
predExprs := sqlparser.SplitAndExpression(nil, predicate)
otherExprs := sqlparser.SplitAndExpression(nil, otherSel.Where.Expr)
predicate = sqlparser.AndExpressions(append(predExprs, otherExprs...)...)
}
if predicate != nil {
sel.Where = &sqlparser.Where{Type: sqlparser.WhereClause, Expr: predicate}
}
qb.addPredicate(onCondition)
}
func (qb *queryBuilder) joinOuterWith(other *queryBuilder, onCondition sqlparser.Expr) {
sel := qb.sel.(*sqlparser.Select)
otherSel := other.sel.(*sqlparser.Select)
var lhs sqlparser.TableExpr
if len(sel.From) == 1 {
lhs = sel.From[0]
} else {
lhs = &sqlparser.ParenTableExpr{Exprs: sel.From}
}
var rhs sqlparser.TableExpr
if len(otherSel.From) == 1 {
rhs = otherSel.From[0]
} else {
rhs = &sqlparser.ParenTableExpr{Exprs: otherSel.From}
}
sel.From = []sqlparser.TableExpr{&sqlparser.JoinTableExpr{
LeftExpr: lhs,
RightExpr: rhs,
Join: sqlparser.LeftJoinType,
Condition: &sqlparser.JoinCondition{
On: onCondition,
},
}}
sel.SelectExprs = append(sel.SelectExprs, otherSel.SelectExprs...)
var predicate sqlparser.Expr
if sel.Where != nil {
predicate = sel.Where.Expr
}
if otherSel.Where != nil {
predicate = sqlparser.AndExpressions(predicate, otherSel.Where.Expr)
}
if predicate != nil {
sel.Where = &sqlparser.Where{Type: sqlparser.WhereClause, Expr: predicate}
}
}
func (qb *queryBuilder) rewriteExprForDerivedTable(expr sqlparser.Expr, dtName string) {
sqlparser.Rewrite(expr, func(cursor *sqlparser.Cursor) bool {
switch node := cursor.Node().(type) {
case *sqlparser.ColName:
hasTable := qb.hasTable(node.Qualifier.Name.String())
if hasTable {
node.Qualifier = sqlparser.TableName{
Name: sqlparser.NewIdentifierCS(dtName),
}
}
}
return true
}, nil)
}
func (qb *queryBuilder) hasTable(tableName string) bool {
for _, name := range qb.tableNames {
if strings.EqualFold(tableName, name) {
return true
}
}
return false
}
type tableSorter struct {
sel *sqlparser.Select
tbl *semantics.SemTable
}
// Len implements the Sort interface
func (ts *tableSorter) Len() int {
return len(ts.sel.From)
}
// Less implements the Sort interface
func (ts *tableSorter) Less(i, j int) bool {
lhs := ts.sel.From[i]
rhs := ts.sel.From[j]
left, ok := lhs.(*sqlparser.AliasedTableExpr)
if !ok {
return i < j
}
right, ok := rhs.(*sqlparser.AliasedTableExpr)
if !ok {
return i < j
}
return ts.tbl.TableSetFor(left).TableOffset() < ts.tbl.TableSetFor(right).TableOffset()
}
// Swap implements the Sort interface
func (ts *tableSorter) Swap(i, j int) {
ts.sel.From[i], ts.sel.From[j] = ts.sel.From[j], ts.sel.From[i]
}