This repository has been archived by the owner on May 15, 2019. It is now read-only.
forked from vitessio/vitess
/
select.go
122 lines (113 loc) · 3.3 KB
/
select.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
// Copyright 2014, 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 (
"fmt"
"github.com/youtube/vitess/go/vt/sqlparser"
)
func buildSelectPlan(sel *sqlparser.Select, schema *Schema) *Plan {
plan := &Plan{ID: NoPlan}
tablename, _ := analyzeFrom(sel.From)
plan.Table, plan.Reason = schema.FindTable(tablename)
if plan.Reason != "" {
return plan
}
if !plan.Table.Keyspace.Sharded {
plan.ID = SelectUnsharded
return plan
}
getWhereRouting(sel.Where, plan, false)
if plan.IsMulti() {
if hasPostProcessing(sel) {
plan.ID = NoPlan
plan.Reason = "multi-shard query has post-processing constructs"
return plan
}
}
// The where clause might have changed.
plan.Rewritten = generateQuery(sel)
return plan
}
// TODO(sougou): Copied from tabletserver. Reuse.
func analyzeFrom(tableExprs sqlparser.TableExprs) (tablename string, hasHints bool) {
if len(tableExprs) > 1 {
return "", false
}
node, ok := tableExprs[0].(*sqlparser.AliasedTableExpr)
if !ok {
return "", false
}
return sqlparser.GetTableName(node.Expr), node.Hints != nil
}
func hasAggregates(node sqlparser.SelectExprs) bool {
for _, node := range node {
switch node := node.(type) {
case *sqlparser.NonStarExpr:
if exprHasAggregates(node.Expr) {
return true
}
}
}
return false
}
func exprHasAggregates(node sqlparser.Expr) bool {
switch node := node.(type) {
case *sqlparser.AndExpr:
return exprHasAggregates(node.Left) || exprHasAggregates(node.Right)
case *sqlparser.OrExpr:
return exprHasAggregates(node.Left) || exprHasAggregates(node.Right)
case *sqlparser.NotExpr:
return exprHasAggregates(node.Expr)
case *sqlparser.ParenBoolExpr:
return exprHasAggregates(node.Expr)
case *sqlparser.ComparisonExpr:
return exprHasAggregates(node.Left) || exprHasAggregates(node.Right)
case *sqlparser.RangeCond:
return exprHasAggregates(node.Left) || exprHasAggregates(node.From) || exprHasAggregates(node.To)
case *sqlparser.IsExpr:
return exprHasAggregates(node.Expr)
case *sqlparser.ExistsExpr:
return false
case sqlparser.StrVal, sqlparser.NumVal, sqlparser.ValArg,
*sqlparser.NullVal, sqlparser.BoolVal, *sqlparser.ColName,
sqlparser.ValTuple, sqlparser.ListArg, *sqlparser.KeyrangeExpr:
return false
case *sqlparser.Subquery:
return false
case *sqlparser.BinaryExpr:
return exprHasAggregates(node.Left) || exprHasAggregates(node.Right)
case *sqlparser.UnaryExpr:
return exprHasAggregates(node.Expr)
case *sqlparser.FuncExpr:
if node.IsAggregate() {
return true
}
for _, expr := range node.Exprs {
switch expr := expr.(type) {
case *sqlparser.NonStarExpr:
if exprHasAggregates(expr.Expr) {
return true
}
}
}
return false
case *sqlparser.CaseExpr:
if exprHasAggregates(node.Expr) || exprHasAggregates(node.Else) {
return true
}
for _, expr := range node.Whens {
if exprHasAggregates(expr.Cond) || exprHasAggregates(expr.Val) {
return true
}
}
return false
case nil:
return false
default:
panic(fmt.Errorf("unexpected type: %T", node))
}
}
func hasPostProcessing(sel *sqlparser.Select) bool {
return hasAggregates(sel.SelectExprs) || sel.Distinct != "" || sel.GroupBy != nil || sel.Having != nil || sel.OrderBy != nil || sel.Limit != nil
}