forked from vitessio/vitess
/
from.go
174 lines (164 loc) · 5.5 KB
/
from.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
// Copyright 2016, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package planbuilder
import (
"errors"
"fmt"
"github.com/youtube/vitess/go/cistring"
"github.com/youtube/vitess/go/vt/sqlparser"
"github.com/youtube/vitess/go/vt/vtgate/engine"
"github.com/youtube/vitess/go/vt/vtgate/vindexes"
)
// This file has functions to analyze the FROM clause.
// processTableExprs analyzes the FROM clause. It produces a builder
// with all the routes identified.
func processTableExprs(tableExprs sqlparser.TableExprs, vschema VSchema) (builder, error) {
if len(tableExprs) != 1 {
return nil, errors.New("unsupported: ',' join operator")
}
return processTableExpr(tableExprs[0], vschema)
}
// processTableExpr produces a builder subtree for the given TableExpr.
func processTableExpr(tableExpr sqlparser.TableExpr, vschema VSchema) (builder, error) {
switch tableExpr := tableExpr.(type) {
case *sqlparser.AliasedTableExpr:
return processAliasedTable(tableExpr, vschema)
case *sqlparser.ParenTableExpr:
bldr, err := processTableExprs(tableExpr.Exprs, vschema)
// We want to point to the higher level parenthesis because
// more routes can be merged with this one. If so, the order
// should be maintained as dictated by the parenthesis.
if rb, ok := bldr.(*route); ok {
rb.Select.From = sqlparser.TableExprs{tableExpr}
}
return bldr, err
case *sqlparser.JoinTableExpr:
return processJoin(tableExpr, vschema)
}
panic("unreachable")
}
// processAliasedTable produces a builder subtree for the given AliasedTableExpr.
// If the expression is a subquery, then the the route built for it will contain
// the entire subquery tree in the from clause, as if it was a table.
// The symtab entry for the query will be a tabsym where the columns
// will be built from the select expressions of the subquery.
// Since the table aliases only contain vindex columns, we'll follow
// the same rule: only columns from the subquery that are identified as
// vindex columns will be added to the tabsym.
// A symtab symbol can only point to a route. This means that we canoot
// support complex joins in subqueries yet.
func processAliasedTable(tableExpr *sqlparser.AliasedTableExpr, vschema VSchema) (builder, error) {
switch expr := tableExpr.Expr.(type) {
case *sqlparser.TableName:
eroute, table, err := getTablePlan(expr, vschema)
if err != nil {
return nil, err
}
alias := sqlparser.TableIdent(sqlparser.String(expr))
astName := expr.Name
if tableExpr.As != "" {
alias = tableExpr.As
astName = alias
}
return newRoute(
sqlparser.TableExprs([]sqlparser.TableExpr{tableExpr}),
eroute,
table,
vschema,
alias,
astName,
), nil
case *sqlparser.Subquery:
sel, ok := expr.Select.(*sqlparser.Select)
if !ok {
return nil, errors.New("unsupported: union operator in subqueries")
}
subplan, err := processSelect(sel, vschema, nil)
if err != nil {
return nil, err
}
subroute, ok := subplan.(*route)
if !ok {
return nil, errors.New("unsupported: complex join in subqueries")
}
table := &vindexes.Table{
Keyspace: subroute.ERoute.Keyspace,
}
for _, colsyms := range subroute.Colsyms {
if colsyms.Vindex == nil {
continue
}
table.ColumnVindexes = append(table.ColumnVindexes, &vindexes.ColumnVindex{
Column: cistring.CIString(colsyms.Alias),
Vindex: colsyms.Vindex,
})
}
rtb := newRoute(
sqlparser.TableExprs([]sqlparser.TableExpr{tableExpr}),
subroute.ERoute,
table,
vschema,
tableExpr.As,
tableExpr.As,
)
subroute.Redirect = rtb
return rtb, nil
}
panic("unreachable")
}
// getTablePlan produces the initial engine.Route for the specified TableName.
// It also returns the associated vschema info (*Table) so that
// it can be used to create the symbol table entry.
func getTablePlan(tableName *sqlparser.TableName, vschema VSchema) (*engine.Route, *vindexes.Table, error) {
table, err := vschema.Find(string(tableName.Qualifier), string(tableName.Name))
if err != nil {
return nil, nil, err
}
if table.Keyspace.Sharded {
return &engine.Route{
Opcode: engine.SelectScatter,
Keyspace: table.Keyspace,
JoinVars: make(map[string]struct{}),
}, table, nil
}
return &engine.Route{
Opcode: engine.SelectUnsharded,
Keyspace: table.Keyspace,
JoinVars: make(map[string]struct{}),
}, table, nil
}
// processJoin produces a builder subtree for the given Join.
// If the left and right nodes can be part of the same route,
// then it's a route. Otherwise, it's a join.
func processJoin(ajoin *sqlparser.JoinTableExpr, vschema VSchema) (builder, error) {
switch ajoin.Join {
case sqlparser.JoinStr, sqlparser.StraightJoinStr, sqlparser.LeftJoinStr:
case sqlparser.RightJoinStr:
convertToLeftJoin(ajoin)
default:
return nil, fmt.Errorf("unsupported: %s", ajoin.Join)
}
lplan, err := processTableExpr(ajoin.LeftExpr, vschema)
if err != nil {
return nil, err
}
rplan, err := processTableExpr(ajoin.RightExpr, vschema)
if err != nil {
return nil, err
}
return lplan.Join(rplan, ajoin)
}
// convertToLeftJoin converts a right join into a left join.
func convertToLeftJoin(ajoin *sqlparser.JoinTableExpr) {
newRHS := ajoin.LeftExpr
// If the LHS is a join, we have to parenthesize it.
// Otherwise, it can be used as is.
if _, ok := newRHS.(*sqlparser.JoinTableExpr); ok {
newRHS = &sqlparser.ParenTableExpr{
Exprs: sqlparser.TableExprs{newRHS},
}
}
ajoin.LeftExpr, ajoin.RightExpr = ajoin.RightExpr, newRHS
ajoin.Join = sqlparser.LeftJoinStr
}