/
horizon_planning.go
1200 lines (1094 loc) · 35.9 KB
/
horizon_planning.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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2019 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 planbuilder
import (
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/vtgate/planbuilder/abstract"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vtgate/semantics"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vtgate/engine"
)
type horizonPlanning struct {
sel *sqlparser.Select
qp *abstract.QueryProjection
}
func (hp *horizonPlanning) planHorizon(ctx *plancontext.PlanningContext, plan logicalPlan, truncateColumns bool) (logicalPlan, error) {
rb, isRoute := plan.(*routeGen4)
if !isRoute && ctx.SemTable.NotSingleRouteErr != nil {
// If we got here, we don't have a single shard plan
return nil, ctx.SemTable.NotSingleRouteErr
}
if isRoute && rb.isSingleShard() {
err := planSingleShardRoutePlan(hp.sel, rb)
if err != nil {
return nil, err
}
return plan, nil
}
// If the current plan is a simpleProjection, we want to rewrite derived expression.
// In transformDerivedPlan (operator_transformers.go), derived tables that are not
// a simple route are put behind a simpleProjection. In this simple projection,
// every Route will represent the original derived table. Thus, pushing new expressions
// to those Routes require us to rewrite them.
// On the other hand, when a derived table is a simple Route, we do not put it under
// a simpleProjection. We create a new Route that contains the derived table in the
// FROM clause. Meaning that, when we push expressions to the select list of this
// new Route, we do not want them to rewrite them.
if _, isSimpleProj := plan.(*simpleProjection); isSimpleProj {
oldRewriteDerivedExpr := ctx.RewriteDerivedExpr
defer func() {
ctx.RewriteDerivedExpr = oldRewriteDerivedExpr
}()
ctx.RewriteDerivedExpr = true
}
var err error
hp.qp, err = abstract.CreateQPFromSelect(hp.sel)
if err != nil {
return nil, err
}
needsOrdering := len(hp.qp.OrderExprs) > 0
canShortcut := isRoute && hp.sel.Having == nil && !needsOrdering
// If we still have a HAVING clause, it's because it could not be pushed to the WHERE,
// so it probably has aggregations
switch {
case hp.qp.NeedsAggregation() || hp.sel.Having != nil:
plan, err = hp.planAggregations(ctx, plan)
if err != nil {
return nil, err
}
// if we already did sorting, we don't need to do it again
needsOrdering = needsOrdering && !hp.qp.CanPushDownSorting
case canShortcut:
err = planSingleShardRoutePlan(hp.sel, rb)
if err != nil {
return nil, err
}
default:
err = pushProjections(ctx, plan, hp.qp.SelectExprs)
if err != nil {
return nil, err
}
}
// If we didn't already take care of ORDER BY during aggregation planning, we need to handle it now
if needsOrdering {
plan, err = hp.planOrderBy(ctx, hp.qp.OrderExprs, plan)
if err != nil {
return nil, err
}
}
plan, err = hp.planDistinct(ctx, plan)
if err != nil {
return nil, err
}
if !truncateColumns {
return plan, nil
}
plan, err = hp.truncateColumnsIfNeeded(ctx, plan)
if err != nil {
return nil, err
}
return plan, nil
}
func pushProjections(ctx *plancontext.PlanningContext, plan logicalPlan, selectExprs []abstract.SelectExpr) error {
for _, e := range selectExprs {
aliasExpr, err := e.GetAliasedExpr()
if err != nil {
return err
}
if _, _, err := pushProjection(ctx, aliasExpr, plan, true, false, false); err != nil {
return err
}
}
return nil
}
func (hp *horizonPlanning) truncateColumnsIfNeeded(ctx *plancontext.PlanningContext, plan logicalPlan) (logicalPlan, error) {
if len(plan.OutputColumns()) == hp.qp.GetColumnCount() {
return plan, nil
}
switch p := plan.(type) {
case *routeGen4:
p.eroute.SetTruncateColumnCount(hp.qp.GetColumnCount())
case *joinGen4, *semiJoin, *hashJoin:
// since this is a join, we can safely add extra columns and not need to truncate them
case *orderedAggregate:
p.truncateColumnCount = hp.qp.GetColumnCount()
case *memorySort:
p.truncater.SetTruncateColumnCount(hp.qp.GetColumnCount())
case *pulloutSubquery:
newUnderlyingPlan, err := hp.truncateColumnsIfNeeded(ctx, p.underlying)
if err != nil {
return nil, err
}
p.underlying = newUnderlyingPlan
default:
plan = &simpleProjection{
logicalPlanCommon: newBuilderCommon(plan),
eSimpleProj: &engine.SimpleProjection{},
}
exprs := hp.qp.SelectExprs[0:hp.qp.GetColumnCount()]
err := pushProjections(ctx, plan, exprs)
if err != nil {
return nil, err
}
}
return plan, nil
}
func checkIfAlreadyExists(expr *sqlparser.AliasedExpr, node sqlparser.SelectStatement, semTable *semantics.SemTable) int {
exprDep := semTable.RecursiveDeps(expr.Expr)
// Here to find if the expr already exists in the SelectStatement, we have 3 cases
// input is a Select -> In this case we want to search in the select
// input is a Union -> In this case we want to search in the First Select of the Union
// input is a Parenthesised Select -> In this case we want to search in the select
// all these three cases are handled by the call to GetFirstSelect.
sel := sqlparser.GetFirstSelect(node)
exprCol, isExprCol := expr.Expr.(*sqlparser.ColName)
// first pass - search for aliased expressions
for i, selectExpr := range sel.SelectExprs {
if !isExprCol {
break
}
selectExpr, ok := selectExpr.(*sqlparser.AliasedExpr)
if ok && selectExpr.As.Equal(exprCol.Name) {
return i
}
}
// next pass - we are searching the actual expressions and not the aliases
for i, selectExpr := range sel.SelectExprs {
selectExpr, ok := selectExpr.(*sqlparser.AliasedExpr)
if !ok {
continue
}
selectExprCol, isSelectExprCol := selectExpr.Expr.(*sqlparser.ColName)
selectExprDep := semTable.RecursiveDeps(selectExpr.Expr)
// Check that the two expressions have the same dependencies
if selectExprDep != exprDep {
continue
}
if isSelectExprCol && isExprCol && exprCol.Name.Equal(selectExprCol.Name) {
// the expressions are ColName, we compare their name
return i
}
if sqlparser.EqualsExpr(selectExpr.Expr, expr.Expr) {
// the expressions are not ColName, so we just compare the expressions
return i
}
}
return -1
}
func (hp *horizonPlanning) planAggregations(ctx *plancontext.PlanningContext, plan logicalPlan) (logicalPlan, error) {
isPushable := !isJoin(plan)
grouping := hp.qp.GetGrouping()
vindexOverlapWithGrouping := hasUniqueVindex(ctx.SemTable, grouping)
if isPushable && vindexOverlapWithGrouping {
// If we have a plan that we can push the group by and aggregation through, we don't need to do aggregation
// at the vtgate level at all
err := hp.planAggregationWithoutOA(ctx, plan)
if err != nil {
return nil, err
}
resultPlan, err := hp.planOrderBy(ctx, hp.qp.OrderExprs, plan)
if err != nil {
return nil, err
}
newPlan, err := hp.planHaving(ctx, resultPlan)
if err != nil {
return nil, err
}
return newPlan, nil
}
return hp.planAggrUsingOA(ctx, plan, grouping)
}
func (hp *horizonPlanning) planAggrUsingOA(
ctx *plancontext.PlanningContext,
plan logicalPlan,
grouping []abstract.GroupBy,
) (logicalPlan, error) {
oa := &orderedAggregate{
groupByKeys: make([]*engine.GroupByParams, 0, len(grouping)),
}
var order []abstract.OrderBy
if hp.qp.CanPushDownSorting {
hp.qp.AlignGroupByAndOrderBy()
// the grouping order might have changed, so we reload the grouping expressions
grouping = hp.qp.GetGrouping()
order = hp.qp.OrderExprs
} else {
for _, expr := range grouping {
order = append(order, expr.AsOrderBy())
}
}
// here we are building up the grouping keys for the OA,
// but they are lacking the input offsets because we have yet to push the columns down
for _, expr := range grouping {
oa.groupByKeys = append(oa.groupByKeys, &engine.GroupByParams{
Expr: expr.Inner,
FromGroupBy: true,
CollationID: ctx.SemTable.CollationForExpr(expr.Inner),
})
}
if hp.sel.Having != nil {
rewriter := hp.qp.AggrRewriter()
sqlparser.Rewrite(hp.sel.Having.Expr, rewriter.Rewrite(), nil)
if rewriter.Err != nil {
return nil, rewriter.Err
}
}
aggregationExprs, err := hp.qp.AggregationExpressions()
if err != nil {
return nil, err
}
// If we have a distinct aggregating expression,
// we handle it by pushing it down to the underlying input as a grouping column
distinctGroupBy, distinctOffsets, aggrs, err := hp.handleDistinctAggr(ctx, aggregationExprs)
if err != nil {
return nil, err
}
if len(distinctGroupBy) > 0 {
grouping = append(grouping, distinctGroupBy...)
// all the distinct grouping aggregates use the same expression, so it should be OK to just add it once
order = append(order, distinctGroupBy[0].AsOrderBy())
oa.preProcess = true
}
newPlan, groupingOffsets, aggrParamOffsets, pushed, err := hp.pushAggregation(ctx, plan, grouping, aggrs, false)
if err != nil {
return nil, err
}
if !pushed {
oa.preProcess = true
oa.aggrOnEngine = true
}
plan = newPlan
_, isRoute := plan.(*routeGen4)
needsProj := !isRoute
var aggPlan = plan
var proj *projection
if needsProj {
length := getLengthOfProjection(groupingOffsets, aggrs)
proj = &projection{
source: plan,
columns: make([]sqlparser.Expr, length),
columnNames: make([]string, length),
}
aggPlan = proj
}
aggrParams, err := generateAggregateParams(aggrs, aggrParamOffsets, proj, pushed)
if err != nil {
return nil, err
}
if proj != nil {
groupingOffsets, err = passGroupingColumns(proj, groupingOffsets, grouping)
if err != nil {
return nil, err
}
}
// Next we add the aggregation expressions and grouping offsets to the OA
addColumnsToOA(ctx, oa, distinctGroupBy, aggrParams, distinctOffsets, groupingOffsets, aggregationExprs)
aggPlan, err = hp.planOrderBy(ctx, order, aggPlan)
if err != nil {
return nil, err
}
oa.resultsBuilder = resultsBuilder{
logicalPlanCommon: newBuilderCommon(aggPlan),
weightStrings: make(map[*resultColumn]int),
}
return hp.planHaving(ctx, oa)
}
func passGroupingColumns(proj *projection, groupings []offsets, grouping []abstract.GroupBy) (projGrpOffsets []offsets, err error) {
for idx, grp := range groupings {
origGrp := grouping[idx]
var offs offsets
expr := origGrp.AsAliasedExpr()
offs.col, err = proj.addColumn(origGrp.InnerIndex, sqlparser.NewOffset(grp.col, expr.Expr), expr.ColumnName())
if err != nil {
return nil, err
}
if grp.wsCol != -1 {
offs.wsCol, err = proj.addColumn(nil, sqlparser.NewOffset(grp.wsCol, weightStringFor(expr.Expr)), "")
if err != nil {
return nil, err
}
}
projGrpOffsets = append(projGrpOffsets, offs)
}
return projGrpOffsets, nil
}
func generateAggregateParams(aggrs []abstract.Aggr, aggrParamOffsets [][]offsets, proj *projection, pushed bool) ([]*engine.AggregateParams, error) {
aggrParams := make([]*engine.AggregateParams, len(aggrs))
for idx, paramOffset := range aggrParamOffsets {
aggr := aggrs[idx]
incomingOffset := paramOffset[0].col
var offset int
if proj != nil {
var aggrExpr sqlparser.Expr
for _, ofs := range paramOffset {
curr := &sqlparser.Offset{V: ofs.col}
if aggrExpr == nil {
aggrExpr = curr
} else {
aggrExpr = &sqlparser.BinaryExpr{
Operator: sqlparser.MultOp,
Left: aggrExpr,
Right: curr,
}
}
}
pos, err := proj.addColumn(aggr.Index, aggrExpr, aggr.Alias)
if err != nil {
return nil, err
}
offset = pos
} else {
offset = incomingOffset
}
opcode := engine.AggregateSum
switch aggr.OpCode {
case engine.AggregateMin, engine.AggregateMax, engine.AggregateRandom:
opcode = aggr.OpCode
case engine.AggregateCount, engine.AggregateCountStar, engine.AggregateCountDistinct, engine.AggregateSumDistinct:
if !pushed {
opcode = aggr.OpCode
}
}
aggrParams[idx] = &engine.AggregateParams{
Opcode: opcode,
Col: offset,
Alias: aggr.Alias,
Expr: aggr.Original.Expr,
Original: aggr.Original,
OrigOpcode: aggr.OpCode,
}
}
return aggrParams, nil
}
func addColumnsToOA(
ctx *plancontext.PlanningContext,
oa *orderedAggregate,
// these are the group by expressions that where added because we have unique aggregations
distinctGroupBy []abstract.GroupBy,
// these are the aggregate params we already have for non-distinct aggregations
aggrParams []*engine.AggregateParams,
// distinctOffsets mark out where we need to use the distinctGroupBy offsets
// to create *engine.AggregateParams for the distinct aggregations
distinctOffsets []int,
// these are the offsets for the group by params
groupings []offsets,
// aggregationExprs are all the original aggregation expressions the query requested
aggregationExprs []abstract.Aggr,
) {
if len(distinctGroupBy) == 0 {
// no distinct aggregations
oa.aggregates = aggrParams
} else {
count := len(groupings) - len(distinctOffsets)
addDistinctAggr := func(offset int) {
// the last grouping we pushed is the one we added for the distinct aggregation
o := groupings[count]
count++
a := aggregationExprs[offset]
collID := ctx.SemTable.CollationForExpr(a.Func.GetArg())
oa.aggregates = append(oa.aggregates, &engine.AggregateParams{
Opcode: a.OpCode,
Col: o.col,
KeyCol: o.col,
WAssigned: o.wsCol >= 0,
WCol: o.wsCol,
Alias: a.Alias,
Original: a.Original,
CollationID: collID,
})
}
lastOffset := distinctOffsets[len(distinctOffsets)-1]
distinctIdx := 0
for i := 0; i <= lastOffset || i <= len(aggrParams); i++ {
for distinctIdx < len(distinctOffsets) && i == distinctOffsets[distinctIdx] {
// we loop here since we could be dealing with multiple distinct aggregations after each other
addDistinctAggr(i)
distinctIdx++
}
if i < len(aggrParams) {
oa.aggregates = append(oa.aggregates, aggrParams[i])
}
}
// we have to remove the tail of the grouping offsets, so we only have the offsets for the GROUP BY in the query
groupings = groupings[:len(groupings)-len(distinctOffsets)]
}
for i, grouping := range groupings {
oa.groupByKeys[i].KeyCol = grouping.col
oa.groupByKeys[i].WeightStringCol = grouping.wsCol
}
}
// handleDistinctAggr takes in a slice of aggregations and returns GroupBy elements that replace
// the distinct aggregations in the input, along with a slice of offsets and the non-distinct aggregations left,
// so we can later reify the original aggregations
func (hp *horizonPlanning) handleDistinctAggr(ctx *plancontext.PlanningContext, exprs []abstract.Aggr) (
distincts []abstract.GroupBy, offsets []int, aggrs []abstract.Aggr, err error) {
var distinctExpr sqlparser.Expr
for i, expr := range exprs {
if !expr.Distinct {
aggrs = append(aggrs, expr)
continue
}
inner, innerWS, err := hp.qp.GetSimplifiedExpr(expr.Func.GetArg())
if err != nil {
return nil, nil, nil, err
}
if exprHasVindex(ctx.SemTable, innerWS, false) {
aggrs = append(aggrs, expr)
continue
}
if distinctExpr == nil {
distinctExpr = innerWS
} else {
if !sqlparser.EqualsExpr(distinctExpr, innerWS) {
err = vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, "unsupported: only one distinct aggregation allowed in a select: %s", sqlparser.String(expr.Original))
return nil, nil, nil, err
}
}
distincts = append(distincts, abstract.GroupBy{
Inner: inner,
WeightStrExpr: innerWS,
InnerIndex: expr.Index,
})
offsets = append(offsets, i)
}
return
}
func (hp *horizonPlanning) planAggregationWithoutOA(ctx *plancontext.PlanningContext, plan logicalPlan) error {
for _, expr := range hp.qp.SelectExprs {
aliasedExpr, err := expr.GetAliasedExpr()
if err != nil {
return err
}
_, _, err = pushProjection(ctx, aliasedExpr, plan, true, false, false)
if err != nil {
return err
}
}
for _, expr := range hp.qp.GetGrouping() {
// since all the grouping will be done at the mysql level,
// we know that we won't need any weight_string() calls
err := planGroupByGen4(ctx, expr, plan /*weighString*/, false)
if err != nil {
return err
}
}
return nil
}
type offsets struct {
col, wsCol int
}
func newOffset(col int) offsets {
return offsets{col: col, wsCol: -1}
}
func (hp *horizonPlanning) createGroupingsForColumns(columns []*sqlparser.ColName) ([]abstract.GroupBy, error) {
var lhsGrouping []abstract.GroupBy
for _, lhsColumn := range columns {
expr, wsExpr, err := hp.qp.GetSimplifiedExpr(lhsColumn)
if err != nil {
return nil, err
}
lhsGrouping = append(lhsGrouping, abstract.GroupBy{
Inner: expr,
WeightStrExpr: wsExpr,
})
}
return lhsGrouping, nil
}
func hasUniqueVindex(semTable *semantics.SemTable, groupByExprs []abstract.GroupBy) bool {
for _, groupByExpr := range groupByExprs {
if exprHasUniqueVindex(semTable, groupByExpr.WeightStrExpr) {
return true
}
}
return false
}
func (hp *horizonPlanning) planOrderBy(ctx *plancontext.PlanningContext, orderExprs []abstract.OrderBy, plan logicalPlan) (logicalPlan, error) {
switch plan := plan.(type) {
case *routeGen4:
newPlan, err := planOrderByForRoute(ctx, orderExprs, plan, hp.qp.HasStar)
if err != nil {
return nil, err
}
return newPlan, nil
case *joinGen4:
newPlan, err := hp.planOrderByForJoin(ctx, orderExprs, plan)
if err != nil {
return nil, err
}
return newPlan, nil
case *hashJoin:
newPlan, err := hp.planOrderByForHashJoin(ctx, orderExprs, plan)
if err != nil {
return nil, err
}
return newPlan, nil
case *orderedAggregate:
// remove ORDER BY NULL from the list of order by expressions since we will be doing the ordering on vtgate level so NULL is not useful
var orderExprsWithoutNils []abstract.OrderBy
for _, expr := range orderExprs {
if sqlparser.IsNull(expr.Inner.Expr) {
continue
}
orderExprsWithoutNils = append(orderExprsWithoutNils, expr)
}
orderExprs = orderExprsWithoutNils
for _, order := range orderExprs {
if sqlparser.ContainsAggregation(order.WeightStrExpr) {
ms, err := createMemorySortPlanOnAggregation(ctx, plan, orderExprs)
if err != nil {
return nil, err
}
return ms, nil
}
}
newInput, err := hp.planOrderBy(ctx, orderExprs, plan.input)
if err != nil {
return nil, err
}
plan.input = newInput
return plan, nil
case *memorySort:
return plan, nil
case *simpleProjection:
return hp.createMemorySortPlan(ctx, plan, orderExprs, true)
case *vindexFunc:
// This is evaluated at VTGate only, so weight_string function cannot be used.
return hp.createMemorySortPlan(ctx, plan, orderExprs /* useWeightStr */, false)
case *limit, *semiJoin, *filter, *pulloutSubquery, *projection:
inputs := plan.Inputs()
if len(inputs) == 0 {
break
}
newFirstInput, err := hp.planOrderBy(ctx, orderExprs, inputs[0])
if err != nil {
return nil, err
}
inputs[0] = newFirstInput
err = plan.Rewrite(inputs...)
if err != nil {
return nil, err
}
return plan, nil
}
return nil, vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, "ordering on complex query %T", plan)
}
func isSpecialOrderBy(o abstract.OrderBy) bool {
if sqlparser.IsNull(o.Inner.Expr) {
return true
}
f, isFunction := o.Inner.Expr.(*sqlparser.FuncExpr)
return isFunction && f.Name.Lowered() == "rand"
}
func planOrderByForRoute(ctx *plancontext.PlanningContext, orderExprs []abstract.OrderBy, plan *routeGen4, hasStar bool) (logicalPlan, error) {
for _, order := range orderExprs {
err := checkOrderExprCanBePlannedInScatter(plan, order, hasStar)
if err != nil {
return nil, err
}
plan.Select.AddOrder(order.Inner)
if isSpecialOrderBy(order) {
continue
}
var wsExpr sqlparser.Expr
if ctx.SemTable.NeedsWeightString(order.Inner.Expr) {
wsExpr = order.WeightStrExpr
}
offset, weightStringOffset, err := wrapAndPushExpr(ctx, order.Inner.Expr, wsExpr, plan)
if err != nil {
return nil, err
}
plan.eroute.OrderBy = append(plan.eroute.OrderBy, engine.OrderByParams{
Col: offset,
WeightStringCol: weightStringOffset,
Desc: order.Inner.Direction == sqlparser.DescOrder,
CollationID: ctx.SemTable.CollationForExpr(order.Inner.Expr),
})
}
return plan, nil
}
// checkOrderExprCanBePlannedInScatter verifies that the given order by expression can be planned.
// It checks if the expression exists in the plan's select list when the query is a scatter.
func checkOrderExprCanBePlannedInScatter(plan *routeGen4, order abstract.OrderBy, hasStar bool) error {
if !hasStar {
return nil
}
sel := sqlparser.GetFirstSelect(plan.Select)
found := false
for _, expr := range sel.SelectExprs {
aliasedExpr, isAliasedExpr := expr.(*sqlparser.AliasedExpr)
if isAliasedExpr && sqlparser.EqualsExpr(aliasedExpr.Expr, order.Inner.Expr) {
found = true
break
}
}
if !found {
return vterrors.New(vtrpcpb.Code_UNIMPLEMENTED, "unsupported: in scatter query: order by must reference a column in the select list: "+sqlparser.String(order.Inner))
}
return nil
}
// wrapAndPushExpr pushes the expression and weighted_string function to the plan using semantics.SemTable
// It returns (expr offset, weight_string offset, error)
func wrapAndPushExpr(ctx *plancontext.PlanningContext, expr sqlparser.Expr, weightStrExpr sqlparser.Expr, plan logicalPlan) (int, int, error) {
offset, _, err := pushProjection(ctx, &sqlparser.AliasedExpr{Expr: expr}, plan, true, true, false)
if err != nil {
return 0, 0, err
}
if weightStrExpr == nil {
return offset, -1, nil
}
if !sqlparser.IsColName(expr) {
switch unary := expr.(type) {
case *sqlparser.CastExpr:
expr = unary.Expr
case *sqlparser.ConvertExpr:
expr = unary.Expr
}
if !sqlparser.IsColName(expr) {
return 0, 0, vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, "unsupported: in scatter query: complex order by expression: %s", sqlparser.String(expr))
}
}
qt := ctx.SemTable.TypeFor(expr)
wsNeeded := true
if qt != nil && sqltypes.IsNumber(*qt) {
wsNeeded = false
}
weightStringOffset := -1
if wsNeeded {
aliasedExpr := &sqlparser.AliasedExpr{Expr: weightStringFor(weightStrExpr)}
weightStringOffset, _, err = pushProjection(ctx, aliasedExpr, plan, true, true, false)
if err != nil {
return 0, 0, err
}
}
return offset, weightStringOffset, nil
}
func weightStringFor(expr sqlparser.Expr) sqlparser.Expr {
return &sqlparser.WeightStringFuncExpr{Expr: expr}
}
func (hp *horizonPlanning) planOrderByForHashJoin(ctx *plancontext.PlanningContext, orderExprs []abstract.OrderBy, plan *hashJoin) (logicalPlan, error) {
if len(orderExprs) == 1 && isSpecialOrderBy(orderExprs[0]) {
rhs, err := hp.planOrderBy(ctx, orderExprs, plan.Right)
if err != nil {
return nil, err
}
plan.Right = rhs
return plan, nil
}
if orderExprsDependsOnTableSet(orderExprs, ctx.SemTable, plan.Right.ContainsTables()) {
newRight, err := hp.planOrderBy(ctx, orderExprs, plan.Right)
if err != nil {
return nil, err
}
plan.Right = newRight
return plan, nil
}
sortPlan, err := hp.createMemorySortPlan(ctx, plan, orderExprs, true)
if err != nil {
return nil, err
}
return sortPlan, nil
}
func (hp *horizonPlanning) planOrderByForJoin(ctx *plancontext.PlanningContext, orderExprs []abstract.OrderBy, plan *joinGen4) (logicalPlan, error) {
if len(orderExprs) == 1 && isSpecialOrderBy(orderExprs[0]) {
lhs, err := hp.planOrderBy(ctx, orderExprs, plan.Left)
if err != nil {
return nil, err
}
rhs, err := hp.planOrderBy(ctx, orderExprs, plan.Right)
if err != nil {
return nil, err
}
plan.Left = lhs
plan.Right = rhs
return plan, nil
}
// We can only push down sorting on the LHS of the join.
// If the order is on the RHS, we need to do the sorting on the vtgate
if orderExprsDependsOnTableSet(orderExprs, ctx.SemTable, plan.Left.ContainsTables()) {
newLeft, err := hp.planOrderBy(ctx, orderExprs, plan.Left)
if err != nil {
return nil, err
}
plan.Left = newLeft
return plan, nil
}
sortPlan, err := hp.createMemorySortPlan(ctx, plan, orderExprs, true)
if err != nil {
return nil, err
}
return sortPlan, nil
}
func createMemorySortPlanOnAggregation(ctx *plancontext.PlanningContext, plan *orderedAggregate, orderExprs []abstract.OrderBy) (logicalPlan, error) {
primitive := &engine.MemorySort{}
ms := &memorySort{
resultsBuilder: resultsBuilder{
logicalPlanCommon: newBuilderCommon(plan),
weightStrings: make(map[*resultColumn]int),
truncater: primitive,
},
eMemorySort: primitive,
}
for _, order := range orderExprs {
offset, woffset, found := findExprInOrderedAggr(plan, order)
if !found {
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "expected to find the order by expression (%s) in orderedAggregate", sqlparser.String(order.Inner))
}
collationID := ctx.SemTable.CollationForExpr(order.WeightStrExpr)
ms.eMemorySort.OrderBy = append(ms.eMemorySort.OrderBy, engine.OrderByParams{
Col: offset,
WeightStringCol: woffset,
Desc: order.Inner.Direction == sqlparser.DescOrder,
StarColFixedIndex: offset,
CollationID: collationID,
})
}
return ms, nil
}
func findExprInOrderedAggr(plan *orderedAggregate, order abstract.OrderBy) (keyCol int, weightStringCol int, found bool) {
for _, key := range plan.groupByKeys {
if sqlparser.EqualsExpr(order.WeightStrExpr, key.Expr) ||
sqlparser.EqualsExpr(order.Inner.Expr, key.Expr) {
return key.KeyCol, key.WeightStringCol, true
}
}
for _, aggregate := range plan.aggregates {
if sqlparser.EqualsExpr(order.WeightStrExpr, aggregate.Original.Expr) ||
sqlparser.EqualsExpr(order.Inner.Expr, aggregate.Original.Expr) {
return aggregate.Col, -1, true
}
}
return 0, 0, false
}
func (hp *horizonPlanning) createMemorySortPlan(ctx *plancontext.PlanningContext, plan logicalPlan, orderExprs []abstract.OrderBy, useWeightStr bool) (logicalPlan, error) {
primitive := &engine.MemorySort{}
ms := &memorySort{
resultsBuilder: resultsBuilder{
logicalPlanCommon: newBuilderCommon(plan),
weightStrings: make(map[*resultColumn]int),
truncater: primitive,
},
eMemorySort: primitive,
}
for _, order := range orderExprs {
wsExpr := order.WeightStrExpr
if !useWeightStr {
wsExpr = nil
}
offset, weightStringOffset, err := wrapAndPushExpr(ctx, order.Inner.Expr, wsExpr, plan)
if err != nil {
return nil, err
}
ms.eMemorySort.OrderBy = append(ms.eMemorySort.OrderBy, engine.OrderByParams{
Col: offset,
WeightStringCol: weightStringOffset,
Desc: order.Inner.Direction == sqlparser.DescOrder,
StarColFixedIndex: offset,
CollationID: ctx.SemTable.CollationForExpr(order.Inner.Expr),
})
}
return ms, nil
}
func orderExprsDependsOnTableSet(orderExprs []abstract.OrderBy, semTable *semantics.SemTable, ts semantics.TableSet) bool {
for _, expr := range orderExprs {
exprDependencies := semTable.RecursiveDeps(expr.Inner.Expr)
if !exprDependencies.IsSolvedBy(ts) {
return false
}
}
return true
}
func (hp *horizonPlanning) planDistinct(ctx *plancontext.PlanningContext, plan logicalPlan) (logicalPlan, error) {
if !hp.qp.NeedsDistinct() {
return plan, nil
}
switch p := plan.(type) {
case *routeGen4:
// we always make the underlying query distinct,
// and then we might also add a distinct operator on top if it is needed
p.Select.MakeDistinct()
if p.isSingleShard() || selectHasUniqueVindex(ctx.SemTable, hp.qp.SelectExprs) {
return plan, nil
}
return hp.addDistinct(ctx, plan)
case *joinGen4, *pulloutSubquery:
return hp.addDistinct(ctx, plan)
case *orderedAggregate:
return hp.planDistinctOA(ctx.SemTable, p)
default:
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "unknown plan type for DISTINCT %T", plan)
}
}
func (hp *horizonPlanning) planDistinctOA(semTable *semantics.SemTable, currPlan *orderedAggregate) (logicalPlan, error) {
oa := &orderedAggregate{
resultsBuilder: resultsBuilder{
logicalPlanCommon: newBuilderCommon(currPlan),
weightStrings: make(map[*resultColumn]int),
},
}
for _, sExpr := range hp.qp.SelectExprs {
expr, err := sExpr.GetExpr()
if err != nil {
return nil, err
}
found := false
for _, grpParam := range currPlan.groupByKeys {
if sqlparser.EqualsExpr(expr, grpParam.Expr) {
found = true
oa.groupByKeys = append(oa.groupByKeys, grpParam)
break
}
}
if found {
continue
}
for _, aggrParam := range currPlan.aggregates {
if sqlparser.EqualsExpr(expr, aggrParam.Expr) {
found = true
oa.groupByKeys = append(oa.groupByKeys, &engine.GroupByParams{KeyCol: aggrParam.Col, WeightStringCol: -1, CollationID: semTable.CollationForExpr(expr)})
break
}
}
if !found {
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "[BUG] unable to plan distinct query as the column is not projected: %s", sqlparser.String(sExpr.Col))
}
}
return oa, nil
}
func (hp *horizonPlanning) addDistinct(ctx *plancontext.PlanningContext, plan logicalPlan) (logicalPlan, error) {
var orderExprs []abstract.OrderBy
var groupByKeys []*engine.GroupByParams
for index, sExpr := range hp.qp.SelectExprs {
aliasExpr, err := sExpr.GetAliasedExpr()
if err != nil {
return nil, err
}
if isAmbiguousOrderBy(index, aliasExpr.As, hp.qp.SelectExprs) {
return nil, vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, "generating order by clause: ambiguous symbol reference: %s", sqlparser.String(aliasExpr.As))
}
var inner sqlparser.Expr
if aliasExpr.As.IsEmpty() {
inner = aliasExpr.Expr
} else {
// If we have an alias, we need to use the alias and not the original expression
// to make sure dependencies work correctly,
// we simply copy the dependencies of the original expression here
inner = sqlparser.NewColName(aliasExpr.As.String())
ctx.SemTable.CopyDependencies(aliasExpr.Expr, inner)
}
grpParam := &engine.GroupByParams{KeyCol: index, WeightStringCol: -1, CollationID: ctx.SemTable.CollationForExpr(inner), Expr: inner}
_, wOffset, err := wrapAndPushExpr(ctx, aliasExpr.Expr, aliasExpr.Expr, plan)
if err != nil {
return nil, err
}
grpParam.WeightStringCol = wOffset
groupByKeys = append(groupByKeys, grpParam)
orderExprs = append(orderExprs, abstract.OrderBy{
Inner: &sqlparser.Order{Expr: inner},
WeightStrExpr: aliasExpr.Expr},
)
}
innerPlan, err := hp.planOrderBy(ctx, orderExprs, plan)
if err != nil {
return nil, err
}
oa := &orderedAggregate{