-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathexpr.go
More file actions
217 lines (201 loc) · 6.15 KB
/
expr.go
File metadata and controls
217 lines (201 loc) · 6.15 KB
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
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package planner
import (
"router"
"github.com/pkg/errors"
"github.com/xelabs/go-mysqlstack/sqlparser"
)
// getDMLRouting used to get the routing from the where clause.
func getDMLRouting(database, table, shardkey string, where *sqlparser.Where, router *router.Router) ([]router.Segment, error) {
if shardkey != "" && where != nil {
filters := splitAndExpression(nil, where.Expr)
for _, filter := range filters {
comparison, ok := filter.(*sqlparser.ComparisonExpr)
if !ok {
continue
}
// Only deal with Equal statement.
switch comparison.Operator {
case sqlparser.EqualStr:
if nameMatch(comparison.Left, table, shardkey) {
sqlval, ok := comparison.Right.(*sqlparser.SQLVal)
if ok {
return router.Lookup(database, table, sqlval, sqlval)
}
}
}
}
}
return router.Lookup(database, table, nil, nil)
}
func hasSubquery(node sqlparser.SQLNode) bool {
has := false
_ = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
if _, ok := node.(*sqlparser.Subquery); ok {
has = true
return false, errors.New("dummy")
}
return true, nil
}, node)
return has
}
func nameMatch(node sqlparser.Expr, table, shardkey string) bool {
colname, ok := node.(*sqlparser.ColName)
return ok && (colname.Qualifier.Name.String() == "" || colname.Qualifier.Name.String() == table) && (colname.Name.String() == shardkey)
}
// isShardKeyChanging returns true if any of the update
// expressions modify a shardkey column.
func isShardKeyChanging(exprs sqlparser.UpdateExprs, shardkey string) bool {
for _, assignment := range exprs {
if shardkey == assignment.Name.Name.String() {
return true
}
}
return false
}
// splitAndExpression breaks up the Expr into AND-separated conditions
// and appends them to filters, which can be shuffled and recombined
// as needed.
func splitAndExpression(filters []sqlparser.Expr, node sqlparser.Expr) []sqlparser.Expr {
if node == nil {
return filters
}
if node, ok := node.(*sqlparser.AndExpr); ok {
filters = splitAndExpression(filters, node.Left)
return splitAndExpression(filters, node.Right)
}
return append(filters, node)
}
// checkComparison checks the WHERE or JOIN-ON clause contains non-sqlval comparison(t1.id=t2.id).
func checkComparison(expr sqlparser.Expr) error {
filters := splitAndExpression(nil, expr)
for _, filter := range filters {
comparison, ok := filter.(*sqlparser.ComparisonExpr)
if !ok {
continue
}
if _, ok := comparison.Right.(*sqlparser.SQLVal); !ok {
buf := sqlparser.NewTrackedBuffer(nil)
comparison.Format(buf)
return errors.Errorf("unsupported: [%s].must.be.value.compare", buf.String())
}
}
return nil
}
type selectTuple struct {
field string
column string
fn string
distinct bool
}
// parserSelectExpr parses the AliasedExpr to {as, column, func} tuple.
// field: the filed name of mysql returns
// column: column name
// func: function name
// For example: select count(*), count(*) as cstar, max(a), max(b) as mb, a as a1, x.b from t,x group by a1,b
// {field:count(*) column:* fn:count}
// {field:cstar column:* fn:count}
// {field:max(a) column:a fn:max}
// {field:mb column:b fn:max}
// {field:a1 column:a fn:}
// {field:b column:x.b fn:}
func parserSelectExpr(expr *sqlparser.AliasedExpr) (*selectTuple, error) {
field := ""
colName := ""
colName1 := ""
funcName := ""
distinct := false
field = expr.As.String()
err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
switch node := node.(type) {
case *sqlparser.ColName:
if colName != "" {
return false, errors.Errorf("unsupported: more.than.one.column.in.a.select.expr")
}
colName = node.Name.String()
colName1 = colName
if !node.Qualifier.IsEmpty() {
colName = node.Qualifier.Name.String() + "." + colName
}
case *sqlparser.FuncExpr:
distinct = node.Distinct
if node.IsAggregate() {
if node != expr.Expr {
return false, errors.Errorf("unsupported: expression.in.select.exprs")
}
funcName = node.Name.String()
if len(node.Exprs) != 1 {
return false, errors.Errorf("unsupported: invalid.use.of.group.function[%s]", funcName)
}
}
return true, nil
case *sqlparser.GroupConcatExpr:
return false, errors.Errorf("unsupported: group_concat.in.select.exprs")
case *sqlparser.Subquery:
return false, errors.Errorf("unsupported: subqueries.in.select.exprs")
}
return true, nil
}, expr.Expr)
if err != nil {
return nil, err
}
if field == "" {
_, isCol := expr.Expr.(*sqlparser.ColName)
if isCol {
field = colName1
} else {
buf := sqlparser.NewTrackedBuffer(nil)
expr.Format(buf)
field = buf.String()
}
}
return &selectTuple{field, colName, funcName, distinct}, nil
}
func parserSelectExprs(exprs sqlparser.SelectExprs) ([]selectTuple, error) {
var tuples []selectTuple
for _, expr := range exprs {
switch expr.(type) {
case *sqlparser.AliasedExpr:
exp := expr.(*sqlparser.AliasedExpr)
tuple, err := parserSelectExpr(exp)
if err != nil {
return nil, err
}
tuples = append(tuples, *tuple)
case *sqlparser.StarExpr:
tuple := selectTuple{field: "*", column: "*"}
tuples = append(tuples, tuple)
case sqlparser.Nextval:
return nil, errors.Errorf("unsupported: Nextval.in.select.exprs")
}
}
return tuples, nil
}
func checkInTuple(name string, tuples []selectTuple) bool {
for _, tuple := range tuples {
if (tuple.field == "*") || (tuple.field == name) {
return true
}
}
return false
}
// decomposeAvg decomposes avg(a) to sum(a) and count(a).
func decomposeAvg(tuple *selectTuple) []*sqlparser.AliasedExpr {
var ret []*sqlparser.AliasedExpr
sum := &sqlparser.AliasedExpr{Expr: &sqlparser.FuncExpr{
Name: sqlparser.NewColIdent("sum"),
Exprs: []sqlparser.SelectExpr{&sqlparser.AliasedExpr{Expr: sqlparser.NewValArg([]byte(tuple.column))}},
}}
count := &sqlparser.AliasedExpr{Expr: &sqlparser.FuncExpr{
Name: sqlparser.NewColIdent("count"),
Exprs: []sqlparser.SelectExpr{&sqlparser.AliasedExpr{Expr: sqlparser.NewValArg([]byte(tuple.column))}},
}}
ret = append(ret, sum, count)
return ret
}