forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution.go
143 lines (121 loc) · 3.64 KB
/
execution.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
// 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 (
"errors"
"fmt"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/vt/schema"
"github.com/youtube/vitess/go/vt/sqlparser"
)
var (
TooComplex = errors.New("Complex")
execLimit = &sqlparser.Limit{Rowcount: sqlparser.ValArg(":_vtMaxResultSize")}
)
// ExecPlan is built for selects and DMLs.
// PK Values values within ExecPlan can be:
// sqltypes.Value: sourced form the query, or
// string: bind variable name starting with ':', or
// nil if no value was specified
type ExecPlan struct {
PlanId PlanType
Reason ReasonType
TableName string
// FieldQuery is used to fetch field info
FieldQuery *sqlparser.ParsedQuery
// FullQuery will be set for all plans.
FullQuery *sqlparser.ParsedQuery
// For PK plans, only OuterQuery is set.
// For SUBQUERY plans, Subquery is also set.
// IndexUsed is set only for PLAN_SELECT_SUBQUERY
OuterQuery *sqlparser.ParsedQuery
Subquery *sqlparser.ParsedQuery
IndexUsed string
// For selects, columns to be returned
// For PLAN_INSERT_SUBQUERY, columns to be inserted
ColumnNumbers []int
// PLAN_PK_EQUAL, PLAN_DML_PK: where clause values
// PLAN_PK_IN: IN clause values
// PLAN_INSERT_PK: values clause
PKValues []interface{}
// For update: set clause if pk is changing
SecondaryPKValues []interface{}
// For PLAN_INSERT_SUBQUERY: pk columns in the subquery result
SubqueryPKColumns []int
// PLAN_SET
SetKey string
SetValue interface{}
}
func (node *ExecPlan) setTableInfo(tableName string, getTable TableGetter) (*schema.Table, error) {
tableInfo, ok := getTable(tableName)
if !ok {
return nil, fmt.Errorf("table %s not found in schema", tableName)
}
node.TableName = tableInfo.Name
return tableInfo, nil
}
type TableGetter func(tableName string) (*schema.Table, bool)
func GetExecPlan(sql string, getTable TableGetter) (plan *ExecPlan, err error) {
statement, err := sqlparser.Parse(sql)
if err != nil {
return nil, err
}
plan, err = analyzeSQL(statement, getTable)
if err != nil {
return nil, err
}
if plan.PlanId == PLAN_PASS_DML {
log.Warningf("PASS_DML: %s", sql)
}
return plan, nil
}
func GetStreamExecPlan(sql string, getTable TableGetter) (plan *ExecPlan, err error) {
statement, err := sqlparser.Parse(sql)
if err != nil {
return nil, err
}
plan = &ExecPlan{
PlanId: PLAN_PASS_SELECT,
FullQuery: GenerateFullQuery(statement),
}
switch stmt := statement.(type) {
case *sqlparser.Select:
if stmt.Lock != "" {
return nil, errors.New("select with lock disallowed with streaming")
}
tableName, _ := analyzeFrom(stmt.From)
if tableName != "" {
plan.setTableInfo(tableName, getTable)
}
case *sqlparser.Union:
// pass
default:
return nil, fmt.Errorf("'%v' not allowed for streaming", sqlparser.String(stmt))
}
return plan, nil
}
func analyzeSQL(statement sqlparser.Statement, getTable TableGetter) (plan *ExecPlan, err error) {
switch stmt := statement.(type) {
case *sqlparser.Union:
return &ExecPlan{
PlanId: PLAN_PASS_SELECT,
FieldQuery: GenerateFieldQuery(stmt),
FullQuery: GenerateFullQuery(stmt),
Reason: REASON_SELECT,
}, nil
case *sqlparser.Select:
return analyzeSelect(stmt, getTable)
case *sqlparser.Insert:
return analyzeInsert(stmt, getTable)
case *sqlparser.Update:
return analyzeUpdate(stmt, getTable)
case *sqlparser.Delete:
return analyzeDelete(stmt, getTable)
case *sqlparser.Set:
return analyzeSet(stmt), nil
case *sqlparser.DDL:
return analyzeDDL(stmt, getTable), nil
}
return nil, errors.New("invalid SQL")
}