-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
concatenate.go
365 lines (319 loc) · 10.3 KB
/
concatenate.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
Copyright 2020 The Vitess Authors.
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 (
"context"
"sync"
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)
// Concatenate Primitive is used to concatenate results from multiple sources.
var _ Primitive = (*Concatenate)(nil)
// Concatenate specified the parameter for concatenate primitive
type Concatenate struct {
Sources []Primitive
// These column offsets do not need to be typed checked - they usually contain weight_string()
// columns that are not going to be returned to the user
NoNeedToTypeCheck map[int]any
}
// NewConcatenate creates a Concatenate primitive. The ignoreCols slice contains the offsets that
// don't need to have the same type between sources -
// weight_string() sometimes returns VARBINARY and sometimes VARCHAR
func NewConcatenate(Sources []Primitive, ignoreCols []int) *Concatenate {
ignoreTypes := map[int]any{}
for _, i := range ignoreCols {
ignoreTypes[i] = nil
}
return &Concatenate{
Sources: Sources,
NoNeedToTypeCheck: ignoreTypes,
}
}
// RouteType returns a description of the query routing type used by the primitive
func (c *Concatenate) RouteType() string {
return "Concatenate"
}
// GetKeyspaceName specifies the Keyspace that this primitive routes to
func (c *Concatenate) GetKeyspaceName() string {
res := c.Sources[0].GetKeyspaceName()
for i := 1; i < len(c.Sources); i++ {
res = formatTwoOptionsNicely(res, c.Sources[i].GetKeyspaceName())
}
return res
}
// GetTableName specifies the table that this primitive routes to.
func (c *Concatenate) GetTableName() string {
res := c.Sources[0].GetTableName()
for i := 1; i < len(c.Sources); i++ {
res = formatTwoOptionsNicely(res, c.Sources[i].GetTableName())
}
return res
}
func formatTwoOptionsNicely(a, b string) string {
if a == b {
return a
}
return a + "_" + b
}
// ErrWrongNumberOfColumnsInSelect is an error
var ErrWrongNumberOfColumnsInSelect = vterrors.NewErrorf(vtrpcpb.Code_FAILED_PRECONDITION, vterrors.WrongNumberOfColumnsInSelect, "The used SELECT statements have a different number of columns")
// TryExecute performs a non-streaming exec.
func (c *Concatenate) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
res, err := c.execSources(ctx, vcursor, bindVars, wantfields)
if err != nil {
return nil, err
}
fields, err := c.getFields(res)
if err != nil {
return nil, err
}
var rowsAffected uint64
var rows [][]sqltypes.Value
for _, r := range res {
rowsAffected += r.RowsAffected
if len(rows) > 0 &&
len(r.Rows) > 0 &&
len(rows[0]) != len(r.Rows[0]) {
return nil, ErrWrongNumberOfColumnsInSelect
}
rows = append(rows, r.Rows...)
}
return &sqltypes.Result{
Fields: fields,
RowsAffected: rowsAffected,
Rows: rows,
}, nil
}
func (c *Concatenate) getFields(res []*sqltypes.Result) ([]*querypb.Field, error) {
if len(res) == 0 {
return nil, nil
}
var fields []*querypb.Field
for _, r := range res {
if r.Fields == nil {
continue
}
if fields == nil {
fields = r.Fields
continue
}
err := c.compareFields(fields, r.Fields)
if err != nil {
return nil, err
}
}
return fields, nil
}
func (c *Concatenate) execSources(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) ([]*sqltypes.Result, error) {
if vcursor.Session().InTransaction() {
// as we are in a transaction, we need to execute all queries inside a single transaction
// therefore it needs a sequential execution.
return c.sequentialExec(ctx, vcursor, bindVars, wantfields)
}
// not in transaction, so execute in parallel.
return c.parallelExec(ctx, vcursor, bindVars, wantfields)
}
func (c *Concatenate) parallelExec(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) ([]*sqltypes.Result, error) {
results := make([]*sqltypes.Result, len(c.Sources))
var outerErr error
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var wg sync.WaitGroup
for i, source := range c.Sources {
currIndex, currSource := i, source
vars := copyBindVars(bindVars)
wg.Add(1)
go func() {
defer wg.Done()
result, err := vcursor.ExecutePrimitive(ctx, currSource, vars, wantfields)
if err != nil {
outerErr = err
cancel()
}
results[currIndex] = result
}()
}
wg.Wait()
return results, outerErr
}
func (c *Concatenate) sequentialExec(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) ([]*sqltypes.Result, error) {
results := make([]*sqltypes.Result, len(c.Sources))
for i, source := range c.Sources {
currIndex, currSource := i, source
vars := copyBindVars(bindVars)
result, err := vcursor.ExecutePrimitive(ctx, currSource, vars, wantfields)
if err != nil {
return nil, err
}
results[currIndex] = result
}
return results, nil
}
// TryStreamExecute performs a streaming exec.
func (c *Concatenate) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
if vcursor.Session().InTransaction() {
// as we are in a transaction, we need to execute all queries inside a single transaction
// therefore it needs a sequential execution.
return c.sequentialStreamExec(ctx, vcursor, bindVars, wantfields, callback)
}
// not in transaction, so execute in parallel.
return c.parallelStreamExec(ctx, vcursor, bindVars, wantfields, callback)
}
func (c *Concatenate) parallelStreamExec(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
var seenFields []*querypb.Field
var outerErr error
var fieldsSent bool
var cbMu, fieldsMu sync.Mutex
var wg, fieldSendWg sync.WaitGroup
fieldSendWg.Add(1)
for i, source := range c.Sources {
wg.Add(1)
currIndex, currSource := i, source
go func() {
defer wg.Done()
err := vcursor.StreamExecutePrimitive(ctx, currSource, bindVars, wantfields, func(resultChunk *sqltypes.Result) error {
// if we have fields to compare, make sure all the fields are all the same
if currIndex == 0 {
fieldsMu.Lock()
if !fieldsSent {
defer fieldSendWg.Done()
defer fieldsMu.Unlock()
seenFields = resultChunk.Fields
fieldsSent = true
// No other call can happen before this call.
return callback(resultChunk)
}
fieldsMu.Unlock()
}
fieldSendWg.Wait()
if resultChunk.Fields != nil {
err := c.compareFields(seenFields, resultChunk.Fields)
if err != nil {
return err
}
}
// This to ensure only one send happens back to the client.
cbMu.Lock()
defer cbMu.Unlock()
select {
case <-ctx.Done():
return nil
default:
return callback(resultChunk)
}
})
// This is to ensure other streams complete if the first stream failed to unlock the wait.
if currIndex == 0 {
fieldsMu.Lock()
if !fieldsSent {
fieldsSent = true
fieldSendWg.Done()
}
fieldsMu.Unlock()
}
if err != nil {
outerErr = err
ctx.Done()
}
}()
}
wg.Wait()
return outerErr
}
func (c *Concatenate) sequentialStreamExec(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
// all the below fields ensure that the fields are sent only once.
var seenFields []*querypb.Field
var fieldsMu sync.Mutex
var fieldsSent bool
for idx, source := range c.Sources {
err := vcursor.StreamExecutePrimitive(ctx, source, bindVars, wantfields, func(resultChunk *sqltypes.Result) error {
// if we have fields to compare, make sure all the fields are all the same
if idx == 0 {
fieldsMu.Lock()
defer fieldsMu.Unlock()
if !fieldsSent {
fieldsSent = true
seenFields = resultChunk.Fields
return callback(resultChunk)
}
}
if resultChunk.Fields != nil {
err := c.compareFields(seenFields, resultChunk.Fields)
if err != nil {
return err
}
}
// check if context has expired.
if ctx.Err() != nil {
return ctx.Err()
}
return callback(resultChunk)
})
if err != nil {
return err
}
}
return nil
}
// GetFields fetches the field info.
func (c *Concatenate) GetFields(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
// TODO: type coercions
res, err := c.Sources[0].GetFields(ctx, vcursor, bindVars)
if err != nil {
return nil, err
}
for i := 1; i < len(c.Sources); i++ {
result, err := c.Sources[i].GetFields(ctx, vcursor, bindVars)
if err != nil {
return nil, err
}
err = c.compareFields(res.Fields, result.Fields)
if err != nil {
return nil, err
}
}
return res, nil
}
// NeedsTransaction returns whether a transaction is needed for this primitive
func (c *Concatenate) NeedsTransaction() bool {
for _, source := range c.Sources {
if source.NeedsTransaction() {
return true
}
}
return false
}
// Inputs returns the input primitives for this
func (c *Concatenate) Inputs() []Primitive {
return c.Sources
}
func (c *Concatenate) description() PrimitiveDescription {
return PrimitiveDescription{OperatorType: c.RouteType()}
}
func (c *Concatenate) compareFields(fields1 []*querypb.Field, fields2 []*querypb.Field) error {
if len(fields1) != len(fields2) {
return ErrWrongNumberOfColumnsInSelect
}
for i, field1 := range fields1 {
if _, found := c.NoNeedToTypeCheck[i]; found {
continue
}
field2 := fields2[i]
if field1.Type != field2.Type {
return vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, "merging field of different types is not supported, name: (%v, %v) types: (%v, %v)", field1.Name, field2.Name, field1.Type, field2.Type)
}
}
return nil
}