forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 13
/
query_executor.go
642 lines (591 loc) · 20.8 KB
/
query_executor.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
// Copyright 2014, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tabletserver
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/youtube/vitess/go/cistring"
"github.com/youtube/vitess/go/hack"
"github.com/youtube/vitess/go/mysql"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/trace"
"github.com/youtube/vitess/go/vt/callerid"
"github.com/youtube/vitess/go/vt/callinfo"
querypb "github.com/youtube/vitess/go/vt/proto/query"
vtrpcpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
"github.com/youtube/vitess/go/vt/sqlparser"
"github.com/youtube/vitess/go/vt/tabletserver/planbuilder"
"golang.org/x/net/context"
)
// QueryExecutor is used for executing a query request.
type QueryExecutor struct {
query string
bindVars map[string]interface{}
transactionID int64
plan *ExecPlan
ctx context.Context
logStats *LogStats
qe *QueryEngine
}
// poolConn is the interface implemented by users of this specialized pool.
type poolConn interface {
Exec(ctx context.Context, query string, maxrows int, wantfields bool) (*sqltypes.Result, error)
}
func addUserTableQueryStats(queryServiceStats *QueryServiceStats, ctx context.Context, tableName string, queryType string, duration int64) {
username := callerid.GetPrincipal(callerid.EffectiveCallerIDFromContext(ctx))
if username == "" {
username = callerid.GetUsername(callerid.ImmediateCallerIDFromContext(ctx))
}
queryServiceStats.UserTableQueryCount.Add([]string{tableName, username, queryType}, 1)
queryServiceStats.UserTableQueryTimesNs.Add([]string{tableName, username, queryType}, int64(duration))
}
// Execute performs a non-streaming query execution.
func (qre *QueryExecutor) Execute() (reply *sqltypes.Result, err error) {
qre.logStats.OriginalSQL = qre.query
qre.logStats.BindVariables = qre.bindVars
qre.logStats.TransactionID = qre.transactionID
planName := qre.plan.PlanID.String()
qre.logStats.PlanType = planName
defer func(start time.Time) {
duration := time.Now().Sub(start)
qre.qe.queryServiceStats.QueryStats.Add(planName, duration)
addUserTableQueryStats(qre.qe.queryServiceStats, qre.ctx, qre.plan.TableName, "Execute", int64(duration))
if reply == nil {
qre.plan.AddStats(1, duration, 0, 1)
return
}
qre.plan.AddStats(1, duration, int64(reply.RowsAffected), 0)
qre.logStats.RowsAffected = int(reply.RowsAffected)
qre.logStats.Rows = reply.Rows
qre.qe.queryServiceStats.ResultStats.Add(int64(len(reply.Rows)))
}(time.Now())
if err := qre.checkPermissions(); err != nil {
return nil, err
}
switch qre.plan.PlanID {
case planbuilder.PlanDDL:
return qre.execDDL()
case planbuilder.PlanNextval:
return qre.execNextval()
}
if qre.transactionID != 0 {
// Need upfront connection for DMLs and transactions
conn := qre.qe.txPool.Get(qre.transactionID)
defer conn.Recycle()
conn.RecordQuery(qre.query)
switch qre.plan.PlanID {
case planbuilder.PlanPassDML:
if qre.qe.strictMode.Get() != 0 {
return nil, NewTabletError(vtrpcpb.ErrorCode_BAD_INPUT, "DML too complex")
}
reply, err = qre.directFetch(conn, qre.plan.FullQuery, qre.bindVars, nil)
case planbuilder.PlanInsertPK:
reply, err = qre.execInsertPK(conn)
case planbuilder.PlanInsertSubquery:
reply, err = qre.execInsertSubquery(conn)
case planbuilder.PlanDMLPK:
reply, err = qre.execDMLPK(conn)
case planbuilder.PlanDMLSubquery:
reply, err = qre.execDMLSubquery(conn)
case planbuilder.PlanOther:
reply, err = qre.execSQL(conn, qre.query, true)
case planbuilder.PlanUpsertPK:
reply, err = qre.execUpsertPK(conn)
default: // select or set in a transaction, just count as select
reply, err = qre.execDirect(conn)
}
} else {
switch qre.plan.PlanID {
case planbuilder.PlanPassSelect:
reply, err = qre.execSelect()
case planbuilder.PlanSelectLock:
return nil, NewTabletError(vtrpcpb.ErrorCode_BAD_INPUT, "Disallowed outside transaction")
case planbuilder.PlanSet:
reply, err = qre.execSet()
case planbuilder.PlanOther:
conn, connErr := qre.getConn(qre.qe.connPool)
if connErr != nil {
return nil, connErr
}
defer conn.Recycle()
reply, err = qre.execSQL(conn, qre.query, true)
default:
if qre.qe.autoCommit.Get() == 0 {
return nil, NewTabletError(vtrpcpb.ErrorCode_BAD_INPUT,
"unsupported query outside transaction: %s", qre.query)
}
reply, err = qre.execDmlAutoCommit()
}
}
return reply, err
}
// Stream performs a streaming query execution.
func (qre *QueryExecutor) Stream(sendReply func(*sqltypes.Result) error) error {
qre.logStats.OriginalSQL = qre.query
qre.logStats.PlanType = qre.plan.PlanID.String()
defer func(start time.Time) {
qre.qe.queryServiceStats.QueryStats.Record(qre.plan.PlanID.String(), start)
addUserTableQueryStats(qre.qe.queryServiceStats, qre.ctx, qre.plan.TableName, "Stream", int64(time.Now().Sub(start)))
}(time.Now())
if err := qre.checkPermissions(); err != nil {
return err
}
conn, err := qre.getConn(qre.qe.streamConnPool)
if err != nil {
return err
}
defer conn.Recycle()
qd := NewQueryDetail(qre.logStats.ctx, conn)
qre.qe.streamQList.Add(qd)
defer qre.qe.streamQList.Remove(qd)
return qre.fullStreamFetch(conn, qre.plan.FullQuery, qre.bindVars, nil, sendReply)
}
func (qre *QueryExecutor) execDmlAutoCommit() (reply *sqltypes.Result, err error) {
return qre.execAsTransaction(func(conn *TxConnection) (reply *sqltypes.Result, err error) {
conn.RecordQuery(qre.query)
switch qre.plan.PlanID {
case planbuilder.PlanPassDML:
if qre.qe.strictMode.Get() != 0 {
return nil, NewTabletError(vtrpcpb.ErrorCode_BAD_INPUT, "DML too complex")
}
reply, err = qre.directFetch(conn, qre.plan.FullQuery, qre.bindVars, nil)
case planbuilder.PlanInsertPK:
reply, err = qre.execInsertPK(conn)
case planbuilder.PlanInsertSubquery:
reply, err = qre.execInsertSubquery(conn)
case planbuilder.PlanDMLPK:
reply, err = qre.execDMLPK(conn)
case planbuilder.PlanDMLSubquery:
reply, err = qre.execDMLSubquery(conn)
case planbuilder.PlanUpsertPK:
reply, err = qre.execUpsertPK(conn)
default:
return nil, NewTabletError(vtrpcpb.ErrorCode_BAD_INPUT, "unsupported query: %s", qre.query)
}
return reply, err
})
}
func (qre *QueryExecutor) execAsTransaction(f func(conn *TxConnection) (*sqltypes.Result, error)) (reply *sqltypes.Result, err error) {
transactionID := qre.qe.txPool.Begin(qre.ctx)
qre.logStats.AddRewrittenSQL("begin", time.Now())
defer func() {
// TxPool.Get may panic
if panicErr := recover(); panicErr != nil {
err = fmt.Errorf("DML autocommit got panic: %v", panicErr)
}
if err != nil {
qre.qe.txPool.Rollback(qre.ctx, transactionID)
qre.logStats.AddRewrittenSQL("rollback", time.Now())
} else {
qre.qe.txPool.Commit(qre.ctx, transactionID)
qre.logStats.AddRewrittenSQL("commit", time.Now())
}
}()
conn := qre.qe.txPool.Get(transactionID)
defer conn.Recycle()
return f(conn)
}
// checkPermissions
func (qre *QueryExecutor) checkPermissions() error {
// Skip permissions check if we have a background context.
if qre.ctx == context.Background() {
return nil
}
// Blacklist
remoteAddr := ""
username := ""
ci, ok := callinfo.FromContext(qre.ctx)
if ok {
remoteAddr = ci.RemoteAddr()
username = ci.Username()
}
action, desc := qre.plan.Rules.getAction(remoteAddr, username, qre.bindVars)
switch action {
case QRFail:
return NewTabletError(vtrpcpb.ErrorCode_BAD_INPUT, "Query disallowed due to rule: %s", desc)
case QRFailRetry:
return NewTabletError(vtrpcpb.ErrorCode_QUERY_NOT_SERVED, "Query disallowed due to rule: %s", desc)
}
// Check for SuperUser calling directly to VTTablet (e.g. VTWorker)
if qre.qe.exemptACL != nil && qre.qe.exemptACL.IsMember(username) {
qre.qe.tableaclExemptCount.Add(1)
return nil
}
callerID := callerid.ImmediateCallerIDFromContext(qre.ctx)
if callerID == nil {
if qre.qe.strictTableAcl {
return NewTabletError(vtrpcpb.ErrorCode_UNAUTHENTICATED, "missing caller id")
}
return nil
}
// a superuser that exempts from table ACL checking.
if qre.qe.exemptACL != nil && qre.qe.exemptACL.IsMember(callerID.Username) {
qre.qe.tableaclExemptCount.Add(1)
return nil
}
// empty table name, do not need a table ACL check.
if qre.plan.TableName == "" {
return nil
}
if qre.plan.Authorized == nil {
return NewTabletError(vtrpcpb.ErrorCode_PERMISSION_DENIED, "table acl error: nil acl")
}
tableACLStatsKey := []string{
qre.plan.TableName,
qre.plan.Authorized.GroupName,
qre.plan.PlanID.String(),
callerID.Username,
}
// perform table ACL check if it is enabled.
if !qre.plan.Authorized.IsMember(callerID.Username) {
if qre.qe.enableTableAclDryRun {
qre.qe.tableaclPseudoDenied.Add(tableACLStatsKey, 1)
return nil
}
// raise error if in strictTableAcl mode, else just log an error.
if qre.qe.strictTableAcl {
errStr := fmt.Sprintf("table acl error: %q cannot run %v on table %q", callerID.Username, qre.plan.PlanID, qre.plan.TableName)
qre.qe.tableaclDenied.Add(tableACLStatsKey, 1)
qre.qe.accessCheckerLogger.Infof("%s", errStr)
return NewTabletError(vtrpcpb.ErrorCode_PERMISSION_DENIED, "%s", errStr)
}
return nil
}
qre.qe.tableaclAllowed.Add(tableACLStatsKey, 1)
return nil
}
func (qre *QueryExecutor) execDDL() (*sqltypes.Result, error) {
ddlPlan := planbuilder.DDLParse(qre.query)
if ddlPlan.Action == "" {
return nil, NewTabletError(vtrpcpb.ErrorCode_BAD_INPUT, "DDL is not understood")
}
txid := qre.qe.txPool.Begin(qre.ctx)
defer qre.qe.txPool.Commit(qre.ctx, txid)
// Stolen from Execute
conn := qre.qe.txPool.Get(txid)
defer conn.Recycle()
result, err := qre.execSQL(conn, qre.query, false)
if err != nil {
return nil, err
}
if ddlPlan.TableName != "" && ddlPlan.TableName != ddlPlan.NewName {
// It's a drop or rename.
qre.qe.schemaInfo.DropTable(ddlPlan.TableName)
}
if ddlPlan.NewName != "" {
qre.qe.schemaInfo.CreateOrUpdateTable(qre.ctx, ddlPlan.NewName)
}
return result, nil
}
func (qre *QueryExecutor) execNextval() (*sqltypes.Result, error) {
t := qre.plan.TableInfo
t.Seq.Lock()
defer t.Seq.Unlock()
if t.NextVal >= t.LastVal {
_, err := qre.execAsTransaction(func(conn *TxConnection) (*sqltypes.Result, error) {
query := fmt.Sprintf("select next_id, cache, increment from `%s` where id = 0 for update", qre.plan.TableName)
conn.RecordQuery(query)
qr, err := qre.execSQL(conn, query, false)
if err != nil {
return nil, err
}
if len(qr.Rows) != 1 {
return nil, fmt.Errorf("unexpected rows from reading sequence %s (possible mis-route): %d", qre.plan.TableName, len(qr.Rows))
}
nextID, err := qr.Rows[0][0].ParseInt64()
if err != nil {
return nil, fmt.Errorf("error loading sequence %s: %v", qre.plan.TableName, err)
}
cache, err := qr.Rows[0][1].ParseInt64()
if err != nil {
return nil, fmt.Errorf("error loading sequence %s: %v", qre.plan.TableName, err)
}
if cache < 1 {
return nil, fmt.Errorf("invalid cache value for sequence %s: %d", qre.plan.TableName, cache)
}
inc, err := qr.Rows[0][2].ParseInt64()
if err != nil {
return nil, fmt.Errorf("error loading sequence %s: %v", qre.plan.TableName, err)
}
if inc < 1 {
return nil, fmt.Errorf("invalid increment for sequence %s: %d", qre.plan.TableName, inc)
}
newLast := nextID + cache*inc
query = fmt.Sprintf("update `%s` set next_id = %d where id = 0", qre.plan.TableName, newLast)
conn.RecordQuery(query)
_, err = qre.execSQL(conn, query, false)
if err != nil {
return nil, err
}
t.NextVal = nextID
t.Increment = inc
t.LastVal = newLast
return nil, nil
})
if err != nil {
return nil, err
}
}
ret := t.NextVal
t.NextVal += t.Increment
return &sqltypes.Result{
Fields: []*querypb.Field{{
Name: "nextval",
Type: sqltypes.Int64,
}},
Rows: [][]sqltypes.Value{{
sqltypes.MakeTrusted(sqltypes.Int64, strconv.AppendInt(nil, ret, 10)),
}},
RowsAffected: 1,
}, nil
}
// execDirect always sends the query to mysql
func (qre *QueryExecutor) execDirect(conn poolConn) (*sqltypes.Result, error) {
if qre.plan.Fields != nil {
result, err := qre.directFetch(conn, qre.plan.FullQuery, qre.bindVars, nil)
if err != nil {
return nil, err
}
result.Fields = qre.plan.Fields
return result, nil
}
return qre.fullFetch(conn, qre.plan.FullQuery, qre.bindVars, nil)
}
// execSelect sends a query to mysql only if another identical query is not running. Otherwise, it waits and
// reuses the result. If the plan is missng field info, it sends the query to mysql requesting full info.
func (qre *QueryExecutor) execSelect() (*sqltypes.Result, error) {
if qre.plan.Fields != nil {
result, err := qre.qFetch(qre.logStats, qre.plan.FullQuery, qre.bindVars)
if err != nil {
return nil, err
}
// result is read-only. So, let's copy it before modifying.
newResult := *result
newResult.Fields = qre.plan.Fields
return &newResult, nil
}
conn, err := qre.getConn(qre.qe.connPool)
if err != nil {
return nil, err
}
defer conn.Recycle()
return qre.fullFetch(conn, qre.plan.FullQuery, qre.bindVars, nil)
}
func (qre *QueryExecutor) execInsertPK(conn poolConn) (*sqltypes.Result, error) {
pkRows, err := buildValueList(qre.plan.TableInfo, qre.plan.PKValues, qre.bindVars)
if err != nil {
return nil, err
}
return qre.execInsertPKRows(conn, pkRows)
}
func (qre *QueryExecutor) execInsertSubquery(conn poolConn) (*sqltypes.Result, error) {
innerResult, err := qre.directFetch(conn, qre.plan.Subquery, qre.bindVars, nil)
if err != nil {
return nil, err
}
innerRows := innerResult.Rows
if len(innerRows) == 0 {
return &sqltypes.Result{RowsAffected: 0}, nil
}
if len(qre.plan.ColumnNumbers) != len(innerRows[0]) {
return nil, NewTabletError(vtrpcpb.ErrorCode_BAD_INPUT, "Subquery length does not match column list")
}
pkRows := make([][]sqltypes.Value, len(innerRows))
for i, innerRow := range innerRows {
pkRows[i] = applyFilterWithPKDefaults(qre.plan.TableInfo, qre.plan.SubqueryPKColumns, innerRow)
}
// Validating first row is sufficient
if err := validateRow(qre.plan.TableInfo, qre.plan.TableInfo.PKColumns, pkRows[0]); err != nil {
return nil, err
}
qre.bindVars["#values"] = innerRows
return qre.execInsertPKRows(conn, pkRows)
}
func (qre *QueryExecutor) execInsertPKRows(conn poolConn, pkRows [][]sqltypes.Value) (*sqltypes.Result, error) {
bsc := buildStreamComment(qre.plan.TableInfo, pkRows, nil)
return qre.directFetch(conn, qre.plan.OuterQuery, qre.bindVars, bsc)
}
func (qre *QueryExecutor) execUpsertPK(conn poolConn) (*sqltypes.Result, error) {
pkRows, err := buildValueList(qre.plan.TableInfo, qre.plan.PKValues, qre.bindVars)
if err != nil {
return nil, err
}
bsc := buildStreamComment(qre.plan.TableInfo, pkRows, nil)
result, err := qre.directFetch(conn, qre.plan.OuterQuery, qre.bindVars, bsc)
if err == nil {
return result, nil
}
terr, ok := err.(*TabletError)
if !ok {
return result, err
}
if terr.SQLError != mysql.ErrDupEntry {
return nil, err
}
// If the error didn't match pk, just return the error without updating.
if !strings.Contains(terr.Message, "'PRIMARY'") {
return nil, err
}
// At this point, we know the insert failed due to a duplicate pk row.
// So, we just update the row.
result, err = qre.execDMLPKRows(conn, qre.plan.UpsertQuery, pkRows)
if err != nil {
return nil, err
}
// Follow MySQL convention. RowsAffected must be 2 if a row was updated.
if result.RowsAffected == 1 {
result.RowsAffected = 2
}
return result, err
}
func (qre *QueryExecutor) execDMLPK(conn poolConn) (*sqltypes.Result, error) {
pkRows, err := buildValueList(qre.plan.TableInfo, qre.plan.PKValues, qre.bindVars)
if err != nil {
return nil, err
}
return qre.execDMLPKRows(conn, qre.plan.OuterQuery, pkRows)
}
func (qre *QueryExecutor) execDMLSubquery(conn poolConn) (*sqltypes.Result, error) {
innerResult, err := qre.directFetch(conn, qre.plan.Subquery, qre.bindVars, nil)
if err != nil {
return nil, err
}
return qre.execDMLPKRows(conn, qre.plan.OuterQuery, innerResult.Rows)
}
func (qre *QueryExecutor) execDMLPKRows(conn poolConn, query *sqlparser.ParsedQuery, pkRows [][]sqltypes.Value) (*sqltypes.Result, error) {
if len(pkRows) == 0 {
return &sqltypes.Result{RowsAffected: 0}, nil
}
secondaryList, err := buildSecondaryList(qre.plan.TableInfo, pkRows, qre.plan.SecondaryPKValues, qre.bindVars)
if err != nil {
return nil, err
}
result := &sqltypes.Result{}
maxRows := int(qre.qe.maxDMLRows.Get())
for i := 0; i < len(pkRows); i += maxRows {
end := i + maxRows
if end >= len(pkRows) {
end = len(pkRows)
}
pkRows := pkRows[i:end]
secondaryList := secondaryList
if secondaryList != nil {
secondaryList = secondaryList[i:end]
}
bsc := buildStreamComment(qre.plan.TableInfo, pkRows, secondaryList)
qre.bindVars["#pk"] = sqlparser.TupleEqualityList{
Columns: cistring.ToStrings(qre.plan.TableInfo.Indexes[0].Columns),
Rows: pkRows,
}
r, err := qre.directFetch(conn, query, qre.bindVars, bsc)
if err != nil {
return nil, err
}
// DMLs should only return RowsAffected.
result.RowsAffected += r.RowsAffected
}
return result, nil
}
func (qre *QueryExecutor) execSet() (*sqltypes.Result, error) {
conn, err := qre.getConn(qre.qe.connPool)
if err != nil {
return nil, err
}
defer conn.Recycle()
return qre.directFetch(conn, qre.plan.FullQuery, qre.bindVars, nil)
}
func (qre *QueryExecutor) getConn(pool *ConnPool) (*DBConn, error) {
span := trace.NewSpanFromContext(qre.ctx)
span.StartLocal("QueryExecutor.getConn")
defer span.Finish()
start := time.Now()
conn, err := pool.Get(qre.ctx)
switch err {
case nil:
qre.logStats.WaitingForConnection += time.Now().Sub(start)
return conn, nil
case ErrConnPoolClosed:
return nil, err
}
return nil, NewTabletErrorSQL(vtrpcpb.ErrorCode_INTERNAL_ERROR, err)
}
func (qre *QueryExecutor) qFetch(logStats *LogStats, parsedQuery *sqlparser.ParsedQuery, bindVars map[string]interface{}) (*sqltypes.Result, error) {
sql, err := qre.generateFinalSQL(parsedQuery, bindVars, nil)
if err != nil {
return nil, err
}
q, ok := qre.qe.consolidator.Create(string(sql))
if ok {
defer q.Broadcast()
waitingForConnectionStart := time.Now()
conn, err := qre.qe.connPool.Get(qre.ctx)
logStats.WaitingForConnection += time.Now().Sub(waitingForConnectionStart)
if err != nil {
q.Err = NewTabletErrorSQL(vtrpcpb.ErrorCode_INTERNAL_ERROR, err)
} else {
defer conn.Recycle()
q.Result, q.Err = qre.execSQL(conn, sql, false)
}
} else {
logStats.QuerySources |= QuerySourceConsolidator
startTime := time.Now()
q.Wait()
qre.qe.queryServiceStats.WaitStats.Record("Consolidations", startTime)
}
if q.Err != nil {
return nil, q.Err
}
return q.Result.(*sqltypes.Result), nil
}
func (qre *QueryExecutor) directFetch(conn poolConn, parsedQuery *sqlparser.ParsedQuery, bindVars map[string]interface{}, buildStreamComment []byte) (*sqltypes.Result, error) {
sql, err := qre.generateFinalSQL(parsedQuery, bindVars, buildStreamComment)
if err != nil {
return nil, err
}
return qre.execSQL(conn, sql, false)
}
// fullFetch also fetches field info
func (qre *QueryExecutor) fullFetch(conn poolConn, parsedQuery *sqlparser.ParsedQuery, bindVars map[string]interface{}, buildStreamComment []byte) (*sqltypes.Result, error) {
sql, err := qre.generateFinalSQL(parsedQuery, bindVars, buildStreamComment)
if err != nil {
return nil, err
}
return qre.execSQL(conn, sql, true)
}
func (qre *QueryExecutor) fullStreamFetch(conn *DBConn, parsedQuery *sqlparser.ParsedQuery, bindVars map[string]interface{}, buildStreamComment []byte, callback func(*sqltypes.Result) error) error {
sql, err := qre.generateFinalSQL(parsedQuery, bindVars, buildStreamComment)
if err != nil {
return err
}
return qre.execStreamSQL(conn, sql, callback)
}
func (qre *QueryExecutor) generateFinalSQL(parsedQuery *sqlparser.ParsedQuery, bindVars map[string]interface{}, buildStreamComment []byte) (string, error) {
bindVars["#maxLimit"] = qre.qe.maxResultSize.Get() + 1
sql, err := parsedQuery.GenerateQuery(bindVars)
if err != nil {
return "", NewTabletError(vtrpcpb.ErrorCode_BAD_INPUT, "%s", err)
}
if buildStreamComment != nil {
sql = append(sql, buildStreamComment...)
}
// undo hack done by stripTrailing
sql = restoreTrailing(sql, bindVars)
return hack.String(sql), nil
}
func (qre *QueryExecutor) execSQL(conn poolConn, sql string, wantfields bool) (*sqltypes.Result, error) {
defer qre.logStats.AddRewrittenSQL(sql, time.Now())
return conn.Exec(qre.ctx, sql, int(qre.qe.maxResultSize.Get()), wantfields)
}
func (qre *QueryExecutor) execStreamSQL(conn *DBConn, sql string, callback func(*sqltypes.Result) error) error {
start := time.Now()
err := conn.Stream(qre.ctx, sql, callback, int(qre.qe.streamBufferSize.Get()))
qre.logStats.AddRewrittenSQL(sql, start)
if err != nil {
// MySQL error that isn't due to a connection issue
return NewTabletErrorSQL(vtrpcpb.ErrorCode_UNKNOWN_ERROR, err)
}
return nil
}