-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathinsert_plan.go
More file actions
219 lines (189 loc) · 4.8 KB
/
insert_plan.go
File metadata and controls
219 lines (189 loc) · 4.8 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
211
212
213
214
215
216
217
218
219
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package planner
import (
"encoding/json"
"sort"
"router"
"xcontext"
"github.com/pkg/errors"
"github.com/xelabs/go-mysqlstack/sqlparser"
"github.com/xelabs/go-mysqlstack/sqlparser/depends/common"
"github.com/xelabs/go-mysqlstack/xlog"
)
var (
_ Plan = &InsertPlan{}
)
// InsertPlan represents insertion plan
type InsertPlan struct {
log *xlog.Log
// router
router *router.Router
// insert ast
node *sqlparser.Insert
// database
database string
// raw query
RawQuery string
// type
Typ PlanType
// mode
ReqMode xcontext.RequestMode
// query and backend tuple
Querys []xcontext.QueryTuple
}
// NewInsertPlan used to create InsertPlan
func NewInsertPlan(log *xlog.Log, database string, query string, node *sqlparser.Insert, router *router.Router) *InsertPlan {
return &InsertPlan{
log: log,
node: node,
router: router,
database: database,
RawQuery: query,
Typ: PlanTypeInsert,
Querys: make([]xcontext.QueryTuple, 0, 16),
}
}
// Build used to build distributed querys.
func (p *InsertPlan) Build() error {
node := p.node
database := p.database
// Qualifier is database in the insert query, such as "db.t1".
if !node.Table.Qualifier.IsEmpty() {
database = node.Table.Qualifier.String()
}
table := node.Table.Name.String()
// Get the shard key.
shardKey, err := p.router.ShardKey(database, table)
if err != nil {
return err
}
rows, ok := node.Rows.(sqlparser.Values)
if !ok {
return errors.Errorf("unsupported: rows.can.not.be.subquery[%T]", node.Rows)
}
// Table is global or single table.
if shardKey == "" {
segments, err := p.router.Lookup(database, table, nil, nil)
if err != nil {
return err
}
for _, segment := range segments {
buf := sqlparser.NewTrackedBuffer(nil)
buf.Myprintf("%s %v%sinto %s.%s%v %v%v", node.Action, node.Comments, node.Ignore, database, segment.Table, node.Columns, node.Rows, node.OnDup)
tuple := xcontext.QueryTuple{
Query: buf.String(),
Backend: segment.Backend,
Range: segment.Range.String(),
}
p.Querys = append(p.Querys, tuple)
}
return nil
}
// Check the OnDup.
if len(node.OnDup) > 0 {
// analyze shardkey changing.
if isShardKeyChanging(sqlparser.UpdateExprs(node.OnDup), shardKey) {
return errors.New("unsupported: cannot.update.shard.key")
}
}
// Find the shard key index.
idx := -1
for i, column := range node.Columns {
if column.String() == shardKey {
idx = i
break
}
}
if idx == -1 {
return errors.Errorf("unsupported: shardkey.column[%v].missing", shardKey)
}
// Rebuild distributed querys.
type valTuple struct {
backend string
table string
rangi string
vals sqlparser.Values
}
vals := make(map[string]*valTuple)
for _, row := range rows {
if idx >= len(row) {
return errors.Errorf("unsupported: shardkey[%v].out.of.index:[%v]", shardKey, idx)
}
shardVal, ok := row[idx].(*sqlparser.SQLVal)
if !ok {
return errors.Errorf("unsupported: shardkey[%v].type.canot.be[%T]", shardKey, row[idx])
}
segments, err := p.router.Lookup(database, table, shardVal, shardVal)
if err != nil {
return err
}
rewrittenTable := segments[0].Table
backend := segments[0].Backend
rangi := segments[0].Range.String()
val, ok := vals[rewrittenTable]
if !ok {
val = &valTuple{
backend: backend,
table: rewrittenTable,
rangi: rangi,
vals: make(sqlparser.Values, 0, 16),
}
vals[rewrittenTable] = val
}
val.vals = append(val.vals, row)
}
// Rebuild querys with router info.
for rewritten, v := range vals {
buf := sqlparser.NewTrackedBuffer(nil)
buf.Myprintf("%s %v%sinto %s.%s%v %v%v", node.Action, node.Comments, node.Ignore, database, rewritten, node.Columns, v.vals, node.OnDup)
tuple := xcontext.QueryTuple{
Query: buf.String(),
Backend: v.backend,
Range: v.rangi,
}
p.Querys = append(p.Querys, tuple)
}
return nil
}
// Type returns the type of the plan.
func (p *InsertPlan) Type() PlanType {
return p.Typ
}
// JSON returns the plan info.
func (p *InsertPlan) JSON() string {
type explain struct {
RawQuery string `json:",omitempty"`
Partitions []xcontext.QueryTuple `json:",omitempty"`
}
var parts []xcontext.QueryTuple
// Sort.
sort.Sort(xcontext.QueryTuples(p.Querys))
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 *InsertPlan) Children() *PlanTree {
return nil
}
// Size returns the memory size.
func (p *InsertPlan) Size() int {
size := len(p.RawQuery)
for _, q := range p.Querys {
size += len(q.Query)
}
return size
}