-
Notifications
You must be signed in to change notification settings - Fork 2k
/
controller_plan.go
239 lines (214 loc) · 6.6 KB
/
controller_plan.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
/*
Copyright 2019 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 vreplication
import (
"fmt"
"vitess.io/vitess/go/vt/sqlparser"
)
// controllerPlan is the plan for vreplication control statements.
type controllerPlan struct {
query string
opcode int
// numInserts is set for insertQuery.
numInserts int
// selector and applier are set for updateQuery and deleteQuery.
selector string
applier *sqlparser.ParsedQuery
// delCopyState deletes related copy state.
delCopyState *sqlparser.ParsedQuery
// delPostCopyAction deletes related post copy actions.
delPostCopyAction *sqlparser.ParsedQuery
}
const (
insertQuery = iota
updateQuery
deleteQuery
selectQuery
reshardingJournalQuery
)
// buildControllerPlan parses the input query and returns an appropriate plan.
func buildControllerPlan(query string) (*controllerPlan, error) {
stmt, err := sqlparser.Parse(query)
if err != nil {
return nil, err
}
var plan *controllerPlan
switch stmt := stmt.(type) {
case *sqlparser.Insert:
plan, err = buildInsertPlan(stmt)
case *sqlparser.Update:
plan, err = buildUpdatePlan(stmt)
case *sqlparser.Delete:
plan, err = buildDeletePlan(stmt)
case *sqlparser.Select:
plan, err = buildSelectPlan(stmt)
default:
return nil, fmt.Errorf("unsupported construct: %s", sqlparser.String(stmt))
}
if err != nil {
return nil, err
}
plan.query = query
return plan, nil
}
func buildInsertPlan(ins *sqlparser.Insert) (*controllerPlan, error) {
switch sqlparser.String(ins.Table) {
case reshardingJournalTableName:
return &controllerPlan{
opcode: reshardingJournalQuery,
}, nil
case vreplicationTableName:
// no-op
default:
return nil, fmt.Errorf("invalid table name: %v", sqlparser.String(ins.Table))
}
if ins.Action != sqlparser.InsertAct {
return nil, fmt.Errorf("unsupported construct: %v", sqlparser.String(ins))
}
if ins.Ignore {
return nil, fmt.Errorf("unsupported construct: %v", sqlparser.String(ins))
}
if ins.Partitions != nil {
return nil, fmt.Errorf("unsupported construct: %v", sqlparser.String(ins))
}
if ins.OnDup != nil {
return nil, fmt.Errorf("unsupported construct: %v", sqlparser.String(ins))
}
rows, ok := ins.Rows.(sqlparser.Values)
if !ok {
return nil, fmt.Errorf("unsupported construct: %v", sqlparser.String(ins))
}
idPos := 0
if len(ins.Columns) != 0 {
idPos = -1
for i, col := range ins.Columns {
if col.EqualString("id") {
idPos = i
break
}
}
}
if idPos >= 0 {
for _, row := range rows {
if idPos >= len(row) {
return nil, fmt.Errorf("malformed statement: %v", sqlparser.String(ins))
}
if _, ok := row[idPos].(*sqlparser.NullVal); !ok {
return nil, fmt.Errorf("id should not have a value: %v", sqlparser.String(ins))
}
}
}
return &controllerPlan{
opcode: insertQuery,
numInserts: len(rows),
}, nil
}
func buildUpdatePlan(upd *sqlparser.Update) (*controllerPlan, error) {
switch sqlparser.String(upd.TableExprs) {
case reshardingJournalTableName:
return &controllerPlan{
opcode: reshardingJournalQuery,
}, nil
case vreplicationTableName:
// no-op
default:
return nil, fmt.Errorf("invalid table name: %v", sqlparser.String(upd.TableExprs))
}
if upd.OrderBy != nil || upd.Limit != nil {
return nil, fmt.Errorf("unsupported construct: %v", sqlparser.String(upd))
}
for _, expr := range upd.Exprs {
if expr.Name.Name.EqualString("id") {
return nil, fmt.Errorf("id cannot be changed: %v", sqlparser.String(expr))
}
}
buf1 := sqlparser.NewTrackedBuffer(nil)
buf1.Myprintf("select id from %s%v", vreplicationTableName, upd.Where)
upd.Where = &sqlparser.Where{
Type: sqlparser.WhereClause,
Expr: &sqlparser.ComparisonExpr{
Left: &sqlparser.ColName{Name: sqlparser.NewIdentifierCI("id")},
Operator: sqlparser.InOp,
Right: sqlparser.ListArg("ids"),
},
}
buf2 := sqlparser.NewTrackedBuffer(nil)
buf2.Myprintf("%v", upd)
return &controllerPlan{
opcode: updateQuery,
selector: buf1.String(),
applier: buf2.ParsedQuery(),
}, nil
}
func buildDeletePlan(del *sqlparser.Delete) (*controllerPlan, error) {
switch sqlparser.String(del.TableExprs) {
case reshardingJournalTableName:
return &controllerPlan{
opcode: reshardingJournalQuery,
}, nil
case vreplicationTableName:
// no-op
default:
return nil, fmt.Errorf("invalid table name: %v", sqlparser.String(del.TableExprs))
}
if del.Targets != nil {
return nil, fmt.Errorf("unsupported construct: %v", sqlparser.String(del))
}
if del.Partitions != nil {
return nil, fmt.Errorf("unsupported construct: %v", sqlparser.String(del))
}
if del.OrderBy != nil || del.Limit != nil {
return nil, fmt.Errorf("unsupported construct: %v", sqlparser.String(del))
}
buf1 := sqlparser.NewTrackedBuffer(nil)
buf1.Myprintf("select id from %s%v", vreplicationTableName, del.Where)
del.Where = &sqlparser.Where{
Type: sqlparser.WhereClause,
Expr: &sqlparser.ComparisonExpr{
Left: &sqlparser.ColName{Name: sqlparser.NewIdentifierCI("id")},
Operator: sqlparser.InOp,
Right: sqlparser.ListArg("ids"),
},
}
buf2 := sqlparser.NewTrackedBuffer(nil)
buf2.Myprintf("%v", del)
copyStateWhere := &sqlparser.Where{
Type: sqlparser.WhereClause,
Expr: &sqlparser.ComparisonExpr{
Left: &sqlparser.ColName{Name: sqlparser.NewIdentifierCI("vrepl_id")},
Operator: sqlparser.InOp,
Right: sqlparser.ListArg("ids"),
},
}
buf3 := sqlparser.NewTrackedBuffer(nil)
buf3.Myprintf("delete from %s%v", copyStateTableName, copyStateWhere)
buf4 := sqlparser.NewTrackedBuffer(nil)
buf4.Myprintf("delete from %s%v", postCopyActionTableName, copyStateWhere)
return &controllerPlan{
opcode: deleteQuery,
selector: buf1.String(),
applier: buf2.ParsedQuery(),
delCopyState: buf3.ParsedQuery(),
delPostCopyAction: buf4.ParsedQuery(),
}, nil
}
func buildSelectPlan(sel *sqlparser.Select) (*controllerPlan, error) {
switch sqlparser.ToString(sel.From) {
case vreplicationTableName, reshardingJournalTableName, copyStateTableName, vreplicationLogTableName:
return &controllerPlan{
opcode: selectQuery,
}, nil
default:
return nil, fmt.Errorf("invalid table name: %v", sqlparser.ToString(sel.From))
}
}