-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathddl_plan.go
More file actions
210 lines (184 loc) · 5.18 KB
/
ddl_plan.go
File metadata and controls
210 lines (184 loc) · 5.18 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
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package planner
import (
"encoding/json"
"errors"
"fmt"
"regexp"
"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),
}
}
// Build used to build DDL distributed querys.
// sqlparser.DDL is a simple grammar ast, it just parses database and table name in the prefix.
func (p *DDLPlan) Build() error {
node := p.node
switch node.Action {
case sqlparser.CreateDBStr:
p.ReqMode = xcontext.ReqScatter
return nil
default:
table := node.Table.Name.String()
database := p.database
if !node.Table.Qualifier.IsEmpty() {
database = node.Table.Qualifier.String()
}
// Get the shard key.
shardKey, err := p.router.ShardKey(database, table)
if err != nil {
return err
}
// Unsupported operations check if shardtype is HASH.
if shardKey != "" {
switch node.Action {
case sqlparser.AlterDropColumnStr:
if shardKey == node.DropColumnName {
return errors.New("unsupported: cannot.drop.the.column.on.shard.key")
}
case sqlparser.AlterModifyColumnStr:
if shardKey == node.ModifyColumnDef.Name.String() {
return errors.New("unsupported: cannot.modify.the.column.on.shard.key")
}
// constraint check in column definition
switch node.ModifyColumnDef.Type.KeyOpt {
case sqlparser.ColKeyUnique, sqlparser.ColKeyUniqueKey, sqlparser.ColKeyPrimary, sqlparser.ColKey:
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 {
switch col.Type.KeyOpt {
case sqlparser.ColKeyUnique, sqlparser.ColKeyUniqueKey, sqlparser.ColKeyPrimary, sqlparser.ColKey:
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 {
info := index.Info
if info.Unique || info.Primary {
err := fmt.Sprintf("The unique/primary constraint should be only defined on the sharding key column[%s]", shardKey)
return errors.New(err)
}
}
}
}
segments, err := p.router.Lookup(database, table, nil, nil)
if err != nil {
return err
}
for _, segment := range segments {
var query string
segTable := segment.Table
var rawQuery string
var re *regexp.Regexp
if node.Table.Qualifier.IsEmpty() {
segTable = fmt.Sprintf("`%s`.`%s`", database, segTable)
rawQuery = strings.Replace(p.RawQuery, "`", "", 2)
// \b: https://www.regular-expressions.info/wordboundaries.html
re, _ = regexp.Compile(fmt.Sprintf(`\b(%s)\b`, table))
} else {
segTable = fmt.Sprintf("`%s`.`%s`", database, segTable)
newTable := fmt.Sprintf("%s.%s", database, table)
rawQuery = strings.Replace(p.RawQuery, "`", "", 4)
re, _ = regexp.Compile(fmt.Sprintf(`\b(%s)\b`, newTable))
}
// avoid the name of the column is the same as the table name, eg, issues/438
// just replace the first place.
var count = 0
var occurrence = 1
query = re.ReplaceAllStringFunc(rawQuery, func(m string) string {
count = count + 1;
if (count == occurrence) {
return segTable
}
return m
})
tuple := xcontext.QueryTuple{
Query: query,
Backend: segment.Backend,
Range: segment.Range.String(),
}
p.Querys = append(p.Querys, tuple)
}
}
return nil
}
// 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,
}
bout, err := json.MarshalIndent(exp, "", "\t")
if err != nil {
return err.Error()
}
return common.BytesToString(bout)
}
// Children returns the children of the plan.
func (p *DDLPlan) Children() *PlanTree {
return nil
}
// 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
}