/
insert_plan.go
232 lines (200 loc) · 5.34 KB
/
insert_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
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package planner
import (
"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 {
newNode := *(p.node)
// 1. Currently insert/replace not support subquery.
rows, ok := newNode.Rows.(sqlparser.Values)
if !ok {
return errors.Errorf("unsupported: rows.can.not.be.subquery[%T]", newNode.Rows)
}
// 2. Currently insert/replace not support partitions.
if len(newNode.Partitions) != 0 {
return errors.Errorf("unsupported: radon.now.not.support.insert.with.partition.")
}
// We`ll rewrite ast on newNode and the table`s format should be like "db.t1", so the "Qualifier" in ast should be not empty.
if newNode.Table.Qualifier.IsEmpty() {
newNode.Table.Qualifier = sqlparser.NewTableIdent(p.database)
}
database := newNode.Table.Qualifier.String()
table := newNode.Table.Name.String()
methodType, err := p.router.PartitionType(database, table)
if err != nil {
return err
}
switch methodType {
case router.MethodTypeGlobal, router.MethodTypeSingle:
segments, err := p.router.Lookup(database, table, nil, nil)
if err != nil {
return err
}
for _, segment := range segments {
tuple := xcontext.QueryTuple{
Query: sqlparser.String(&newNode),
Backend: segment.Backend,
Range: segment.Range.String(),
}
p.Querys = append(p.Querys, tuple)
}
return nil
case router.MethodTypeHash, router.MethodTypeList:
// Get the shard key.
shardKey, err := p.router.ShardKey(database, table)
if err != nil {
return err
}
// Check the OnDup.
if len(newNode.OnDup) > 0 {
// analyze whether update shardkey.
if isUpdateShardKey(sqlparser.UpdateExprs(newNode.OnDup), shardKey) {
return errors.New("unsupported: cannot.update.shard.key")
}
}
// Find the shard key index.
idx := -1
for i, column := range newNode.Columns {
if column.EqualString(shardKey) {
idx = i
break
}
}
if idx == -1 {
return errors.Errorf("unsupported: shardkey.column[%v].missing", shardKey)
}
// Rebuild distributed querys.
type rowsTuple struct {
backend string
table string
rangi string
rows sqlparser.Values
}
// key: partition table, value: rowsTuple.
rTuples := make(map[string]*rowsTuple)
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
}
partTable := segments[0].Table
backend := segments[0].Backend
rangi := segments[0].Range.String()
rTuple, ok := rTuples[partTable]
if !ok {
rTuple = &rowsTuple{
backend: backend,
table: partTable,
rangi: rangi,
rows: make(sqlparser.Values, 0, 16),
}
rTuples[partTable] = rTuple
}
rTuple.rows = append(rTuple.rows, row)
}
// sorts SQL by partitionTable in increasing order to avoid deadlock #605.
partTables := []string{}
for partTable, _ := range rTuples {
partTables = append(partTables, partTable)
}
sort.Strings(partTables)
// Rebuild querys with router info.
for _, partTable := range partTables {
v := rTuples[partTable]
newNode.Table.Name = sqlparser.NewTableIdent(partTable)
newNode.Rows = v.rows
tuple := xcontext.QueryTuple{
Query: sqlparser.String(&newNode),
Backend: v.backend,
Range: v.rangi,
}
p.Querys = append(p.Querys, tuple)
}
return nil
default:
return errors.Errorf("unsupported: radon.not.support.method.type[%s].", methodType)
}
}
// 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
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 *InsertPlan) Size() int {
size := len(p.RawQuery)
for _, q := range p.Querys {
size += len(q.Query)
}
return size
}