-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathddl_plan.go
More file actions
172 lines (147 loc) · 3.71 KB
/
ddl_plan.go
File metadata and controls
172 lines (147 loc) · 3.71 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
/*
* 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/hack"
"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
// Unsupported rename operation.
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
}
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")
}
}
segments, err := p.router.Lookup(database, table, nil, nil)
if err != nil {
return err
}
for _, segment := range segments {
var query string
segTable := segment.Table
if node.Table.Qualifier.IsEmpty() {
segTable = fmt.Sprintf("`%s`.`%s`", database, segTable)
rawQuery := strings.Replace(p.RawQuery, "`", "", 2)
re, _ := regexp.Compile(fmt.Sprintf(`\b(%s)\b`, table))
query = re.ReplaceAllString(rawQuery, segTable)
} 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))
query = re.ReplaceAllString(rawQuery, segTable)
}
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 hack.String(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
}