-
Notifications
You must be signed in to change notification settings - Fork 4
/
delete.go
254 lines (222 loc) · 8.63 KB
/
delete.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
/*
Copyright 2018 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package engine
import (
"encoding/json"
"fmt"
"time"
"gopkg.in/src-d/go-vitess.v1/jsonutil"
"gopkg.in/src-d/go-vitess.v1/sqltypes"
"gopkg.in/src-d/go-vitess.v1/vt/key"
"gopkg.in/src-d/go-vitess.v1/vt/sqlannotation"
"gopkg.in/src-d/go-vitess.v1/vt/srvtopo"
"gopkg.in/src-d/go-vitess.v1/vt/vterrors"
"gopkg.in/src-d/go-vitess.v1/vt/vtgate/vindexes"
querypb "gopkg.in/src-d/go-vitess.v1/vt/proto/query"
vtrpcpb "gopkg.in/src-d/go-vitess.v1/vt/proto/vtrpc"
)
var _ Primitive = (*Delete)(nil)
// Delete represents the instructions to perform a delete.
type Delete struct {
// Opcode is the execution opcode.
Opcode DeleteOpcode
// Keyspace specifies the keyspace to send the query to.
Keyspace *vindexes.Keyspace
// TargetDestination specifies the destination to send the query to.
TargetDestination key.Destination
// Query specifies the query to be executed.
Query string
// Vindex specifies the vindex to be used.
Vindex vindexes.Vindex
// Values specifies the vindex values to use for routing.
// For now, only one value is specified.
Values []sqltypes.PlanValue
// Table specifies the table for the delete.
Table *vindexes.Table
// OwnedVindexQuery is used for deleting lookup vindex entries.
OwnedVindexQuery string
// Option to override the standard behavior and allow a multi-shard delete
// to use single round trip autocommit.
MultiShardAutocommit bool
// QueryTimeout contains the optional timeout (in milliseconds) to apply to this query
QueryTimeout int
}
// MarshalJSON serializes the Delete into a JSON representation.
// It's used for testing and diagnostics.
func (del *Delete) MarshalJSON() ([]byte, error) {
var tname, vindexName string
if del.Table != nil {
tname = del.Table.Name.String()
}
if del.Vindex != nil {
vindexName = del.Vindex.String()
}
marshalDelete := struct {
Opcode DeleteOpcode
Keyspace *vindexes.Keyspace `json:",omitempty"`
Query string `json:",omitempty"`
Vindex string `json:",omitempty"`
Values []sqltypes.PlanValue `json:",omitempty"`
Table string `json:",omitempty"`
OwnedVindexQuery string `json:",omitempty"`
MultiShardAutocommit bool `json:",omitempty"`
QueryTimeout int `json:",omitempty"`
}{
Opcode: del.Opcode,
Keyspace: del.Keyspace,
Query: del.Query,
Vindex: vindexName,
Values: del.Values,
Table: tname,
OwnedVindexQuery: del.OwnedVindexQuery,
MultiShardAutocommit: del.MultiShardAutocommit,
QueryTimeout: del.QueryTimeout,
}
return jsonutil.MarshalNoEscape(marshalDelete)
}
// DeleteOpcode is a number representing the opcode
// for the Delete primitve.
type DeleteOpcode int
// This is the list of DeleteOpcode values.
const (
// DeleteUnsharded is for routing a delete statement
// to an unsharded keyspace.
DeleteUnsharded = DeleteOpcode(iota)
// DeleteEqual is for routing a delete statement
// to a single shard. Requires: A Vindex, a single
// Value, and an OwnedVindexQuery, which will be used to
// determine if lookup rows need to be deleted.
DeleteEqual
// DeleteScatter is for routing a scattered
// delete statement.
DeleteScatter
// DeleteByDestination is to route explicitly to a given
// target destination. Is used when the query explicitly sets a target destination:
// in the from clause:
// e.g: DELETE FROM `keyspace[-]`.x1 LIMIT 100
DeleteByDestination
)
var delName = map[DeleteOpcode]string{
DeleteUnsharded: "DeleteUnsharded",
DeleteEqual: "DeleteEqual",
DeleteScatter: "DeleteScatter",
DeleteByDestination: "DeleteByDestination",
}
// MarshalJSON serializes the DeleteOpcode as a JSON string.
// It's used for testing and diagnostics.
func (code DeleteOpcode) MarshalJSON() ([]byte, error) {
return json.Marshal(delName[code])
}
// RouteType returns a description of the query routing type used by the primitive
func (del *Delete) RouteType() string {
return delName[del.Opcode]
}
// Execute performs a non-streaming exec.
func (del *Delete) Execute(vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
if del.QueryTimeout != 0 {
cancel := vcursor.SetContextTimeout(time.Duration(del.QueryTimeout) * time.Millisecond)
defer cancel()
}
switch del.Opcode {
case DeleteUnsharded:
return del.execDeleteUnsharded(vcursor, bindVars)
case DeleteEqual:
return del.execDeleteEqual(vcursor, bindVars)
case DeleteScatter:
return del.execDeleteByDestination(vcursor, bindVars, key.DestinationAllShards{})
case DeleteByDestination:
return del.execDeleteByDestination(vcursor, bindVars, del.TargetDestination)
default:
// Unreachable.
return nil, fmt.Errorf("unsupported opcode: %v", del)
}
}
// StreamExecute performs a streaming exec.
func (del *Delete) StreamExecute(vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
return fmt.Errorf("query %q cannot be used for streaming", del.Query)
}
// GetFields fetches the field info.
func (del *Delete) GetFields(vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
return nil, fmt.Errorf("BUG: unreachable code for %q", del.Query)
}
func (del *Delete) execDeleteUnsharded(vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
rss, _, err := vcursor.ResolveDestinations(del.Keyspace.Name, nil, []key.Destination{key.DestinationAllShards{}})
if err != nil {
return nil, vterrors.Wrap(err, "execDeleteUnsharded")
}
if len(rss) != 1 {
return nil, vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "Keyspace does not have exactly one shard: %v", rss)
}
return execShard(vcursor, del.Query, bindVars, rss[0], true, true /* canAutocommit */)
}
func (del *Delete) execDeleteEqual(vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
key, err := del.Values[0].ResolveValue(bindVars)
if err != nil {
return nil, vterrors.Wrap(err, "execDeleteEqual")
}
rs, ksid, err := resolveSingleShard(vcursor, del.Vindex, del.Keyspace, key)
if err != nil {
return nil, vterrors.Wrap(err, "execDeleteEqual")
}
if len(ksid) == 0 {
return &sqltypes.Result{}, nil
}
if del.OwnedVindexQuery != "" {
err = del.deleteVindexEntries(vcursor, bindVars, rs, ksid)
if err != nil {
return nil, vterrors.Wrap(err, "execDeleteEqual")
}
}
rewritten := sqlannotation.AddKeyspaceIDs(del.Query, [][]byte{ksid}, "")
return execShard(vcursor, rewritten, bindVars, rs, true /* isDML */, true /* canAutocommit */)
}
func (del *Delete) deleteVindexEntries(vcursor VCursor, bindVars map[string]*querypb.BindVariable, rs *srvtopo.ResolvedShard, ksid []byte) error {
result, err := execShard(vcursor, del.OwnedVindexQuery, bindVars, rs, false /* isDML */, false /* canAutocommit */)
if err != nil {
return err
}
if len(result.Rows) == 0 {
return nil
}
colnum := 0
for _, colVindex := range del.Table.Owned {
ids := make([][]sqltypes.Value, len(result.Rows))
for range colVindex.Columns {
for rowIdx, row := range result.Rows {
ids[rowIdx] = append(ids[rowIdx], row[colnum])
}
colnum++
}
if err = colVindex.Vindex.(vindexes.Lookup).Delete(vcursor, ids, ksid); err != nil {
return err
}
}
return nil
}
func (del *Delete) execDeleteByDestination(vcursor VCursor, bindVars map[string]*querypb.BindVariable, dest key.Destination) (*sqltypes.Result, error) {
rss, _, err := vcursor.ResolveDestinations(del.Keyspace.Name, nil, []key.Destination{dest})
if err != nil {
return nil, vterrors.Wrap(err, "execDeleteScatter")
}
queries := make([]*querypb.BoundQuery, len(rss))
sql := sqlannotation.AnnotateIfDML(del.Query, nil)
for i := range rss {
queries[i] = &querypb.BoundQuery{
Sql: sql,
BindVariables: bindVars,
}
}
autocommit := (len(rss) == 1 || del.MultiShardAutocommit) && vcursor.AutocommitApproval()
res, errs := vcursor.ExecuteMultiShard(rss, queries, true /* isDML */, autocommit)
return res, vterrors.Aggregate(errs)
}