-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathddl_plan.go
308 lines (275 loc) · 8.19 KB
/
ddl_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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package planner
import (
"errors"
"fmt"
"strings"
"router"
"xcontext"
"github.com/xelabs/go-mysqlstack/sqlparser"
"github.com/xelabs/go-mysqlstack/sqlparser/depends/common"
"github.com/xelabs/go-mysqlstack/xlog"
)
var (
_ Plan = &DDLPlan{}
)
// DDLPlan represents a CREATE, ALTER, DROP or RENAME plan
type DDLPlan struct {
log *xlog.Log
// router
router *router.Router
// ddl ast
node *sqlparser.DDL
// database
database string
// raw query
RawQuery string
// type
typ PlanType
// mode
ReqMode xcontext.RequestMode
// query and backend tuple
Querys []xcontext.QueryTuple
}
// NewDDLPlan used to create DDLPlan
func NewDDLPlan(log *xlog.Log, database string, query string, node *sqlparser.DDL, router *router.Router) *DDLPlan {
return &DDLPlan{
log: log,
node: node,
router: router,
database: database,
RawQuery: query,
typ: PlanTypeDDL,
Querys: make([]xcontext.QueryTuple, 0, 16),
}
}
// checkUnsupportedOperations used to check whether we do unsupported operations when shardtype is HASH/LIST.
func (p *DDLPlan) checkUnsupportedOperations(database, table string) error {
node := p.node
// Get the shard key.
shardKey, err := p.router.ShardKey(database, table)
if err != nil {
return err
}
// Unsupported operations check when shardtype is HASH/LIST.
if shardKey != "" {
switch node.Action {
case sqlparser.AlterDropColumnStr:
if strings.EqualFold(shardKey, node.DropColumnName) {
return errors.New("unsupported: cannot.drop.the.column.on.shard.key")
}
case sqlparser.AlterModifyColumnStr:
if node.ModifyColumnDef.Name.EqualString(shardKey) {
return errors.New("unsupported: cannot.modify.the.column.on.shard.key")
}
// constraint check in column definition
if node.ModifyColumnDef.Type.PrimaryKeyOpt == sqlparser.ColKeyPrimary ||
node.ModifyColumnDef.Type.UniqueKeyOpt == sqlparser.ColKeyUniqueKey {
err := fmt.Sprintf("The unique/primary constraint should be only defined on the sharding key column[%s]", shardKey)
return errors.New(err)
}
case sqlparser.AlterAddColumnStr:
// constraint check in column definition
for _, col := range node.TableSpec.Columns {
if col.Type.PrimaryKeyOpt == sqlparser.ColKeyPrimary ||
col.Type.UniqueKeyOpt == sqlparser.ColKeyUniqueKey {
err := fmt.Sprintf("The unique/primary constraint should be only defined on the sharding key column[%s]", shardKey)
return errors.New(err)
}
}
// constraint check in index definition
for _, index := range node.TableSpec.Indexes {
if index.Unique || index.Primary {
err := fmt.Sprintf("The unique/primary constraint should be only defined on the sharding key column[%s]", shardKey)
return errors.New(err)
}
}
}
}
return nil
}
// commonImpl used to build distributed querys for create/alter.
func (p *DDLPlan) commonImpl() error {
oldNode := p.node
oldTable := oldNode.Table.Name.String()
database := p.database
if !oldNode.Table.Qualifier.IsEmpty() {
database = oldNode.Table.Qualifier.String()
}
if err := p.checkUnsupportedOperations(database, oldTable); err != nil {
return err
}
segments, err := p.router.Lookup(database, oldTable, nil, nil)
if err != nil {
return err
}
for _, segment := range segments {
// Rewrite ddl ast, replace oldTable to segment table(new table) and format a new query
newNode := *oldNode
newTable := segment.Table
buf := sqlparser.NewTrackedBuffer(nil)
newNode.Table = sqlparser.TableName{
Name: sqlparser.NewTableIdent(newTable),
Qualifier: sqlparser.NewTableIdent(database),
}
newNode.NewName = newNode.Table
newNode.Format(buf)
newQuery := buf.String()
tuple := xcontext.QueryTuple{
Query: newQuery,
Backend: segment.Backend,
Range: segment.Range.String(),
}
p.Querys = append(p.Querys, tuple)
}
return nil
}
// dropTblImpl used to build distributed querys for: drop table t1
func (p *DDLPlan) dropTblImpl() error {
oldNode := p.node
oldTable := oldNode.Table.Name.String()
database := p.database
if !oldNode.Table.Qualifier.IsEmpty() {
database = oldNode.Table.Qualifier.String()
}
segments, err := p.router.Lookup(database, oldTable, nil, nil)
if err != nil {
return err
}
for _, segment := range segments {
// Rewrite ddl ast, replace oldTable to segment table(new table) and format a new query
newNode := *oldNode
newTable := segment.Table
buf := sqlparser.NewTrackedBuffer(nil)
// Now we just drop a table once a time, here can be optimized with proxy/ddl.go in the future
newNode.Tables = sqlparser.TableNames{
sqlparser.TableName{
Name: sqlparser.NewTableIdent(newTable),
Qualifier: sqlparser.NewTableIdent(database),
},
}
newNode.Format(buf)
newQuery := buf.String()
tuple := xcontext.QueryTuple{
Query: newQuery,
Backend: segment.Backend,
Range: segment.Range.String(),
}
p.Querys = append(p.Querys, tuple)
}
return nil
}
// renameImpl used to build distributed querys for rename oldTbl to newTbl.
func (p *DDLPlan) renameImpl() error {
oldNode := p.node
// Check if fromDatabase and toDatabase is same or not.
fromDatabase := p.database
if !oldNode.Table.Qualifier.IsEmpty() {
fromDatabase = oldNode.Table.Qualifier.String()
}
if toDatabase := oldNode.NewName.Qualifier.String(); toDatabase != "" && toDatabase != fromDatabase {
// toDatabase must equal to fromDatabase if not empty
err := fmt.Sprintf("unsupported: Database is not equal[%s:%s]", fromDatabase, toDatabase)
return errors.New(err)
}
oldFromTable := oldNode.Table.Name.String()
oldToTable := oldNode.NewName.Name.String()
segments, err := p.router.Lookup(fromDatabase, oldFromTable, nil, nil)
if err != nil {
return err
}
for _, segment := range segments {
// Get newFromTable and newToTable
newFromTable := segment.Table
var newToTable string
shardKey, err := p.router.ShardKey(fromDatabase, oldFromTable)
if err != nil {
return err
}
if shardKey != "" {
// just to the shardtable, the suffix with "_0001" is valid
splits := strings.SplitN(segment.Table, "_", -1)
suffix := splits[len(splits)-1]
newToTable = oldToTable + "_" + suffix
} else {
newToTable = oldToTable
}
// Rewrite rename ast, replace oldFromTable to newFromTable and oldToTable to newToTable, then format to a new query
newNode := *oldNode
buf := sqlparser.NewTrackedBuffer(nil)
newNode.Table = sqlparser.TableName{
Name: sqlparser.NewTableIdent(newFromTable),
Qualifier: sqlparser.NewTableIdent(fromDatabase),
}
newNode.NewName = sqlparser.TableName{
Name: sqlparser.NewTableIdent(newToTable),
Qualifier: sqlparser.NewTableIdent(fromDatabase),
}
newNode.Format(buf)
newQuery := buf.String()
tuple := xcontext.QueryTuple{
Query: newQuery,
Backend: segment.Backend,
Range: segment.Range.String(),
}
p.Querys = append(p.Querys, tuple)
}
return nil
}
// Build used to build DDL distributed querys.
// sqlparser.DDL is a simple grammar ast, it just parses database and table name in the prefix.
// In our sql syntax in sql.y, alter will be changed to rename in case next:
// ALTER ignore_opt TABLE table_name RENAME to_opt table_name
// {
// Change this to a rename statement
// $$ = &DDL{Action: RenameStr, Table: $4, NewName: $7}
// }
func (p *DDLPlan) Build() error {
var err error
switch p.node.Action {
case sqlparser.DropTableStr:
err = p.dropTblImpl()
case sqlparser.RenameStr:
err = p.renameImpl()
default:
err = p.commonImpl()
}
return err
}
// Type returns the type of the plan.
func (p *DDLPlan) Type() PlanType {
return p.typ
}
// JSON returns the plan info.
func (p *DDLPlan) JSON() string {
type explain struct {
RawQuery string `json:",omitempty"`
Partitions []xcontext.QueryTuple `json:",omitempty"`
}
// Partitions.
var parts []xcontext.QueryTuple
parts = append(parts, p.Querys...)
exp := &explain{
RawQuery: p.RawQuery,
Partitions: parts,
}
out, err := common.ToJSONString(exp, false, "", "\t")
if err != nil {
return err.Error()
}
return out
}
// Size returns the memory size.
func (p *DDLPlan) Size() int {
size := len(p.RawQuery)
for _, q := range p.Querys {
size += len(q.Query)
}
return size
}