-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathaggregator.go
932 lines (834 loc) · 26.2 KB
/
aggregator.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
// Copyright 2016 The Cockroach 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 distsqlrun
import (
"context"
"fmt"
"strings"
"unsafe"
"github.com/cockroachdb/cockroach/pkg/sql/distsqlpb"
"github.com/cockroachdb/cockroach/pkg/sql/sem/builtins"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sem/types"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/mon"
"github.com/cockroachdb/cockroach/pkg/util/stringarena"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
)
// GetAggregateInfo returns the aggregate constructor and the return type for
// the given aggregate function when applied on the given type.
func GetAggregateInfo(
fn distsqlpb.AggregatorSpec_Func, inputTypes ...sqlbase.ColumnType,
) (
aggregateConstructor func(*tree.EvalContext, tree.Datums) tree.AggregateFunc,
returnType sqlbase.ColumnType,
err error,
) {
if fn == distsqlpb.AggregatorSpec_ANY_NOT_NULL {
// The ANY_NOT_NULL builtin does not have a fixed return type;
// handle it separately.
if len(inputTypes) != 1 {
return nil, sqlbase.ColumnType{}, errors.Errorf("any_not_null aggregate needs 1 input")
}
return builtins.NewAnyNotNullAggregate, inputTypes[0], nil
}
datumTypes := make([]types.T, len(inputTypes))
for i := range inputTypes {
datumTypes[i] = inputTypes[i].ToDatumType()
}
props, builtins := builtins.GetBuiltinProperties(strings.ToLower(fn.String()))
for _, b := range builtins {
types := b.Types.Types()
if len(types) != len(inputTypes) {
continue
}
match := true
for i, t := range types {
if !datumTypes[i].Equivalent(t) {
if props.NullableArgs && datumTypes[i].IsAmbiguous() {
continue
}
match = false
break
}
}
if match {
// Found!
constructAgg := func(evalCtx *tree.EvalContext, arguments tree.Datums) tree.AggregateFunc {
return b.AggregateFunc(datumTypes, evalCtx, arguments)
}
colTyp, err := sqlbase.DatumTypeToColumnType(b.FixedReturnType())
if err != nil {
return nil, sqlbase.ColumnType{}, err
}
return constructAgg, colTyp, nil
}
}
return nil, sqlbase.ColumnType{}, errors.Errorf(
"no builtin aggregate for %s on %+v", fn, inputTypes,
)
}
type aggregateFuncs []tree.AggregateFunc
func (af aggregateFuncs) close(ctx context.Context) {
for _, f := range af {
f.Close(ctx)
}
}
// aggregatorBase is the foundation of the processor core type that does
// "aggregation" in the SQL sense. It groups rows and computes an aggregate for
// each group. The group is configured using the group key and the aggregator
// can be configured with one or more aggregation functions, as defined in the
// AggregatorSpec_Func enum.
//
// aggregatorBase's output schema is comprised of what is specified by the
// accompanying SELECT expressions.
type aggregatorBase struct {
ProcessorBase
// runningState represents the state of the aggregator. This is in addition to
// ProcessorBase.State - the runningState is only relevant when
// ProcessorBase.State == StateRunning.
runningState aggregatorState
input RowSource
inputDone bool
inputTypes []sqlbase.ColumnType
funcs []*aggregateFuncHolder
outputTypes []sqlbase.ColumnType
datumAlloc sqlbase.DatumAlloc
rowAlloc sqlbase.EncDatumRowAlloc
bucketsAcc mon.BoundAccount
// isScalar can only be set if there are no groupCols, and it means that we
// will generate a result row even if there are no input rows. Used for
// queries like SELECT MAX(n) FROM t.
isScalar bool
groupCols columns
orderedGroupCols columns
aggregations []distsqlpb.AggregatorSpec_Aggregation
lastOrdGroupCols sqlbase.EncDatumRow
arena stringarena.Arena
row sqlbase.EncDatumRow
scratch []byte
cancelChecker *sqlbase.CancelChecker
}
// init initializes the aggregatorBase.
//
// trailingMetaCallback is passed as part of ProcStateOpts; the inputs to drain
// are in aggregatorBase.
func (ag *aggregatorBase) init(
self RowSource,
flowCtx *FlowCtx,
processorID int32,
spec *distsqlpb.AggregatorSpec,
input RowSource,
post *distsqlpb.PostProcessSpec,
output RowReceiver,
trailingMetaCallback func(context.Context) []ProducerMetadata,
) error {
ctx := flowCtx.EvalCtx.Ctx()
memMonitor := NewMonitor(ctx, flowCtx.EvalCtx.Mon, "aggregator-mem")
if sp := opentracing.SpanFromContext(ctx); sp != nil && tracing.IsRecording(sp) {
input = NewInputStatCollector(input)
ag.finishTrace = ag.outputStatsToTrace
}
ag.input = input
switch spec.Type {
case distsqlpb.AggregatorSpec_SCALAR:
ag.isScalar = true
case distsqlpb.AggregatorSpec_NON_SCALAR:
ag.isScalar = false
default:
// This case exists for backward compatibility.
ag.isScalar = (len(spec.GroupCols) == 0)
}
ag.groupCols = spec.GroupCols
ag.orderedGroupCols = spec.OrderedGroupCols
ag.aggregations = spec.Aggregations
ag.funcs = make([]*aggregateFuncHolder, len(spec.Aggregations))
ag.outputTypes = make([]sqlbase.ColumnType, len(spec.Aggregations))
ag.row = make(sqlbase.EncDatumRow, len(spec.Aggregations))
ag.bucketsAcc = memMonitor.MakeBoundAccount()
ag.arena = stringarena.Make(&ag.bucketsAcc)
// Loop over the select expressions and extract any aggregate functions --
// non-aggregation functions are replaced with parser.NewIdentAggregate,
// (which just returns the last value added to them for a bucket) to provide
// grouped-by values for each bucket. ag.funcs is updated to contain all
// the functions which need to be fed values.
ag.inputTypes = input.OutputTypes()
for i, aggInfo := range spec.Aggregations {
if aggInfo.FilterColIdx != nil {
col := *aggInfo.FilterColIdx
if col >= uint32(len(ag.inputTypes)) {
return errors.Errorf("FilterColIdx out of range (%d)", col)
}
t := ag.inputTypes[col].SemanticType
if t != sqlbase.ColumnType_BOOL && t != sqlbase.ColumnType_NULL {
return errors.Errorf(
"filter column %d must be of boolean type, not %s", *aggInfo.FilterColIdx, t,
)
}
}
argTypes := make([]sqlbase.ColumnType, len(aggInfo.ColIdx)+len(aggInfo.Arguments))
for j, c := range aggInfo.ColIdx {
if c >= uint32(len(ag.inputTypes)) {
return errors.Errorf("ColIdx out of range (%d)", aggInfo.ColIdx)
}
argTypes[j] = ag.inputTypes[c]
}
arguments := make(tree.Datums, len(aggInfo.Arguments))
for j, argument := range aggInfo.Arguments {
h := exprHelper{}
// Pass nil types and row - there are no variables in these expressions.
if err := h.init(argument, nil /* types */, flowCtx.EvalCtx); err != nil {
return errors.Wrap(err, argument.String())
}
d, err := h.eval(nil /* row */)
if err != nil {
return errors.Wrap(err, argument.String())
}
argTypes[len(aggInfo.ColIdx)+j], err = sqlbase.DatumTypeToColumnType(d.ResolvedType())
if err != nil {
return errors.Wrap(err, argument.String())
}
arguments[j] = d
}
aggConstructor, retType, err := GetAggregateInfo(aggInfo.Func, argTypes...)
if err != nil {
return err
}
ag.funcs[i] = ag.newAggregateFuncHolder(aggConstructor, arguments)
if aggInfo.Distinct {
ag.funcs[i].seen = make(map[string]struct{})
}
ag.outputTypes[i] = retType
}
return ag.ProcessorBase.Init(
self, post, ag.outputTypes, flowCtx, processorID, output, memMonitor,
ProcStateOpts{
InputsToDrain: []RowSource{ag.input},
TrailingMetaCallback: trailingMetaCallback,
},
)
}
var _ distsqlpb.DistSQLSpanStats = &AggregatorStats{}
const aggregatorTagPrefix = "aggregator."
// Stats implements the SpanStats interface.
func (as *AggregatorStats) Stats() map[string]string {
inputStatsMap := as.InputStats.Stats(aggregatorTagPrefix)
inputStatsMap[aggregatorTagPrefix+maxMemoryTagSuffix] = humanizeutil.IBytes(as.MaxAllocatedMem)
return inputStatsMap
}
// StatsForQueryPlan implements the DistSQLSpanStats interface.
func (as *AggregatorStats) StatsForQueryPlan() []string {
return append(
as.InputStats.StatsForQueryPlan("" /* prefix */),
fmt.Sprintf("%s: %s", maxMemoryQueryPlanSuffix, humanizeutil.IBytes(as.MaxAllocatedMem)),
)
}
func (ag *aggregatorBase) outputStatsToTrace() {
is, ok := getInputStats(ag.flowCtx, ag.input)
if !ok {
return
}
if sp := opentracing.SpanFromContext(ag.Ctx); sp != nil {
tracing.SetSpanStats(
sp,
&AggregatorStats{
InputStats: is,
MaxAllocatedMem: ag.MemMonitor.MaximumBytes(),
},
)
}
}
// hashAggregator is a specialization of aggregatorBase that must keep track of
// multiple grouping buckets at a time.
type hashAggregator struct {
aggregatorBase
// buckets is used during the accumulation phase to track the bucket keys
// that have been seen. After accumulation, the keys are extracted into
// bucketsIter for iteration.
buckets map[string]aggregateFuncs
bucketsIter []string
}
// orderedAggregator is a specialization of aggregatorBase that only needs to
// keep track of a single grouping bucket at a time.
type orderedAggregator struct {
aggregatorBase
// bucket is used during the accumulation phase to aggregate results.
bucket aggregateFuncs
}
var _ Processor = &hashAggregator{}
var _ RowSource = &hashAggregator{}
const hashAggregatorProcName = "hash aggregator"
var _ Processor = &orderedAggregator{}
var _ RowSource = &orderedAggregator{}
const orderedAggregatorProcName = "ordered aggregator"
// aggregatorState represents the state of the processor.
type aggregatorState int
const (
aggStateUnknown aggregatorState = iota
// aggAccumulating means that rows are being read from the input and used to
// compute intermediary aggregation results.
aggAccumulating
// aggEmittingRows means that accumulation has finished and rows are being
// sent to the output.
aggEmittingRows
)
func newAggregator(
flowCtx *FlowCtx,
processorID int32,
spec *distsqlpb.AggregatorSpec,
input RowSource,
post *distsqlpb.PostProcessSpec,
output RowReceiver,
) (Processor, error) {
if len(spec.GroupCols) == 0 &&
len(spec.Aggregations) == 1 &&
spec.Aggregations[0].FilterColIdx == nil &&
spec.Aggregations[0].Func == distsqlpb.AggregatorSpec_COUNT_ROWS &&
!spec.Aggregations[0].Distinct {
return newCountAggregator(flowCtx, processorID, input, post, output)
}
if len(spec.OrderedGroupCols) == len(spec.GroupCols) {
return newOrderedAggregator(flowCtx, processorID, spec, input, post, output)
}
ag := &hashAggregator{buckets: make(map[string]aggregateFuncs)}
if err := ag.init(
ag,
flowCtx,
processorID,
spec,
input,
post,
output,
func(context.Context) []ProducerMetadata {
ag.close()
return nil
},
); err != nil {
return nil, err
}
return ag, nil
}
func newOrderedAggregator(
flowCtx *FlowCtx,
processorID int32,
spec *distsqlpb.AggregatorSpec,
input RowSource,
post *distsqlpb.PostProcessSpec,
output RowReceiver,
) (*orderedAggregator, error) {
ag := &orderedAggregator{}
if err := ag.init(
ag,
flowCtx,
processorID,
spec,
input,
post,
output,
func(context.Context) []ProducerMetadata {
ag.close()
return nil
},
); err != nil {
return nil, err
}
return ag, nil
}
// Start is part of the RowSource interface.
func (ag *hashAggregator) Start(ctx context.Context) context.Context {
return ag.start(ctx, hashAggregatorProcName)
}
// Start is part of the RowSource interface.
func (ag *orderedAggregator) Start(ctx context.Context) context.Context {
return ag.start(ctx, orderedAggregatorProcName)
}
func (ag *aggregatorBase) start(ctx context.Context, procName string) context.Context {
ag.input.Start(ctx)
ctx = ag.StartInternal(ctx, procName)
ag.cancelChecker = sqlbase.NewCancelChecker(ctx)
ag.runningState = aggAccumulating
return ctx
}
func (ag *hashAggregator) close() {
if ag.InternalClose() {
log.VEventf(ag.Ctx, 2, "exiting aggregator")
ag.bucketsAcc.Close(ag.Ctx)
// If we have started emitting rows, bucketsIter will represent which
// buckets are still open, since buckets are closed once their results are
// emitted.
if ag.bucketsIter == nil {
for _, bucket := range ag.buckets {
bucket.close(ag.Ctx)
}
} else {
for _, bucket := range ag.bucketsIter {
ag.buckets[bucket].close(ag.Ctx)
}
}
ag.MemMonitor.Stop(ag.Ctx)
}
}
func (ag *orderedAggregator) close() {
if ag.InternalClose() {
log.VEventf(ag.Ctx, 2, "exiting aggregator")
ag.bucketsAcc.Close(ag.Ctx)
if ag.bucket != nil {
ag.bucket.close(ag.Ctx)
}
ag.MemMonitor.Stop(ag.Ctx)
}
}
// matchLastOrdGroupCols takes a row and matches it with the row stored by
// lastOrdGroupCols. It returns true if the two rows are equal on the grouping
// columns, and false otherwise.
func (ag *aggregatorBase) matchLastOrdGroupCols(row sqlbase.EncDatumRow) (bool, error) {
for _, colIdx := range ag.orderedGroupCols {
res, err := ag.lastOrdGroupCols[colIdx].Compare(
&ag.inputTypes[colIdx], &ag.datumAlloc, ag.flowCtx.EvalCtx, &row[colIdx],
)
if res != 0 || err != nil {
return false, err
}
}
return true, nil
}
// accumulateRows continually reads rows from the input and accumulates them
// into intermediary aggregate results. If it encounters metadata, the metadata
// is immediately returned. Subsequent calls of this function will resume row
// accumulation.
func (ag *hashAggregator) accumulateRows() (
aggregatorState,
sqlbase.EncDatumRow,
*ProducerMetadata,
) {
for {
row, meta := ag.input.Next()
if meta != nil {
if meta.Err != nil {
ag.MoveToDraining(nil /* err */)
return aggStateUnknown, nil, meta
}
return aggAccumulating, nil, meta
}
if row == nil {
log.VEvent(ag.Ctx, 1, "accumulation complete")
ag.inputDone = true
break
}
if ag.lastOrdGroupCols == nil {
ag.lastOrdGroupCols = ag.rowAlloc.CopyRow(row)
} else {
matched, err := ag.matchLastOrdGroupCols(row)
if err != nil {
ag.MoveToDraining(err)
return aggStateUnknown, nil, nil
}
if !matched {
ag.lastOrdGroupCols = ag.rowAlloc.CopyRow(row)
break
}
}
if err := ag.accumulateRow(row); err != nil {
ag.MoveToDraining(err)
return aggStateUnknown, nil, nil
}
}
// Queries like `SELECT MAX(n) FROM t` expect a row of NULLs if nothing was
// aggregated.
if len(ag.buckets) < 1 && len(ag.groupCols) == 0 {
bucket, err := ag.createAggregateFuncs()
if err != nil {
ag.MoveToDraining(err)
return aggStateUnknown, nil, nil
}
ag.buckets[""] = bucket
}
ag.bucketsIter = make([]string, 0, len(ag.buckets))
for bucket := range ag.buckets {
ag.bucketsIter = append(ag.bucketsIter, bucket)
}
// Transition to aggEmittingRows, and let it generate the next row/meta.
return aggEmittingRows, nil, nil
}
// accumulateRows continually reads rows from the input and accumulates them
// into intermediary aggregate results. If it encounters metadata, the metadata
// is immediately returned. Subsequent calls of this function will resume row
// accumulation.
func (ag *orderedAggregator) accumulateRows() (
aggregatorState,
sqlbase.EncDatumRow,
*ProducerMetadata,
) {
for {
row, meta := ag.input.Next()
if meta != nil {
if meta.Err != nil {
ag.MoveToDraining(nil /* err */)
return aggStateUnknown, nil, meta
}
return aggAccumulating, nil, meta
}
if row == nil {
log.VEvent(ag.Ctx, 1, "accumulation complete")
ag.inputDone = true
break
}
if ag.lastOrdGroupCols == nil {
ag.lastOrdGroupCols = ag.rowAlloc.CopyRow(row)
} else {
matched, err := ag.matchLastOrdGroupCols(row)
if err != nil {
ag.MoveToDraining(err)
return aggStateUnknown, nil, nil
}
if !matched {
ag.lastOrdGroupCols = ag.rowAlloc.CopyRow(row)
break
}
}
if err := ag.accumulateRow(row); err != nil {
ag.MoveToDraining(err)
return aggStateUnknown, nil, nil
}
}
// Queries like `SELECT MAX(n) FROM t` expect a row of NULLs if nothing was
// aggregated.
if ag.bucket == nil && ag.isScalar {
var err error
ag.bucket, err = ag.createAggregateFuncs()
if err != nil {
ag.MoveToDraining(err)
return aggStateUnknown, nil, nil
}
}
// Transition to aggEmittingRows, and let it generate the next row/meta.
return aggEmittingRows, nil, nil
}
func (ag *aggregatorBase) getAggResults(
bucket aggregateFuncs,
) (aggregatorState, sqlbase.EncDatumRow, *ProducerMetadata) {
for i, b := range bucket {
result, err := b.Result()
if err != nil {
ag.MoveToDraining(err)
return aggStateUnknown, nil, nil
}
if result == nil {
// We can't encode nil into an EncDatum, so we represent it with DNull.
result = tree.DNull
}
ag.row[i] = sqlbase.DatumToEncDatum(ag.outputTypes[i], result)
}
bucket.close(ag.Ctx)
if outRow := ag.ProcessRowHelper(ag.row); outRow != nil {
return aggEmittingRows, outRow, nil
}
// We might have switched to draining, we might not have. In case we
// haven't, aggEmittingRows is accurate. If we have, it will be ignored by
// the caller.
return aggEmittingRows, nil, nil
}
// emitRow constructs an output row from an accumulated bucket and returns it.
//
// emitRow() might move to stateDraining. It might also not return a row if the
// ProcOutputHelper filtered the current row out.
func (ag *hashAggregator) emitRow() (aggregatorState, sqlbase.EncDatumRow, *ProducerMetadata) {
if len(ag.bucketsIter) == 0 {
// We've exhausted all of the aggregation buckets.
if ag.inputDone {
// The input has been fully consumed. Transition to draining so that we
// emit any metadata that we've produced.
ag.MoveToDraining(nil /* err */)
return aggStateUnknown, nil, nil
}
// We've only consumed part of the input where the rows are equal over
// the columns specified by ag.orderedGroupCols, so we need to continue
// accumulating the remaining rows.
if err := ag.arena.UnsafeReset(ag.Ctx); err != nil {
ag.MoveToDraining(err)
return aggStateUnknown, nil, nil
}
ag.bucketsIter = nil
ag.buckets = make(map[string]aggregateFuncs)
for _, f := range ag.funcs {
if f.seen != nil {
f.seen = make(map[string]struct{})
}
}
if err := ag.accumulateRow(ag.lastOrdGroupCols); err != nil {
ag.MoveToDraining(err)
return aggStateUnknown, nil, nil
}
return aggAccumulating, nil, nil
}
bucket := ag.bucketsIter[0]
ag.bucketsIter = ag.bucketsIter[1:]
return ag.getAggResults(ag.buckets[bucket])
}
// emitRow constructs an output row from an accumulated bucket and returns it.
//
// emitRow() might move to stateDraining. It might also not return a row if the
// ProcOutputHelper filtered a the current row out.
func (ag *orderedAggregator) emitRow() (aggregatorState, sqlbase.EncDatumRow, *ProducerMetadata) {
if ag.bucket == nil {
// We've exhausted all of the aggregation buckets.
if ag.inputDone {
// The input has been fully consumed. Transition to draining so that we
// emit any metadata that we've produced.
ag.MoveToDraining(nil /* err */)
return aggStateUnknown, nil, nil
}
// We've only consumed part of the input where the rows are equal over
// the columns specified by ag.orderedGroupCols, so we need to continue
// accumulating the remaining rows.
if err := ag.arena.UnsafeReset(ag.Ctx); err != nil {
ag.MoveToDraining(err)
return aggStateUnknown, nil, nil
}
for _, f := range ag.funcs {
if f.seen != nil {
f.seen = make(map[string]struct{})
}
}
if err := ag.accumulateRow(ag.lastOrdGroupCols); err != nil {
ag.MoveToDraining(err)
return aggStateUnknown, nil, nil
}
return aggAccumulating, nil, nil
}
bucket := ag.bucket
ag.bucket = nil
return ag.getAggResults(bucket)
}
// Next is part of the RowSource interface.
func (ag *hashAggregator) Next() (sqlbase.EncDatumRow, *ProducerMetadata) {
for ag.State == StateRunning {
var row sqlbase.EncDatumRow
var meta *ProducerMetadata
switch ag.runningState {
case aggAccumulating:
ag.runningState, row, meta = ag.accumulateRows()
case aggEmittingRows:
ag.runningState, row, meta = ag.emitRow()
default:
log.Fatalf(ag.Ctx, "unsupported state: %d", ag.runningState)
}
if row == nil && meta == nil {
continue
}
return row, meta
}
return nil, ag.DrainHelper()
}
// Next is part of the RowSource interface.
func (ag *orderedAggregator) Next() (sqlbase.EncDatumRow, *ProducerMetadata) {
for ag.State == StateRunning {
var row sqlbase.EncDatumRow
var meta *ProducerMetadata
switch ag.runningState {
case aggAccumulating:
ag.runningState, row, meta = ag.accumulateRows()
case aggEmittingRows:
ag.runningState, row, meta = ag.emitRow()
default:
log.Fatalf(ag.Ctx, "unsupported state: %d", ag.runningState)
}
if row == nil && meta == nil {
continue
}
return row, meta
}
return nil, ag.DrainHelper()
}
// ConsumerClosed is part of the RowSource interface.
func (ag *hashAggregator) ConsumerClosed() {
// The consumer is done, Next() will not be called again.
ag.close()
}
// ConsumerClosed is part of the RowSource interface.
func (ag *orderedAggregator) ConsumerClosed() {
// The consumer is done, Next() will not be called again.
ag.close()
}
func (ag *aggregatorBase) accumulateRowIntoBucket(
row sqlbase.EncDatumRow, groupKey []byte, bucket aggregateFuncs,
) error {
// Feed the func holders for this bucket the non-grouping datums.
for i, a := range ag.aggregations {
if a.FilterColIdx != nil {
col := *a.FilterColIdx
if err := row[col].EnsureDecoded(&ag.inputTypes[col], &ag.datumAlloc); err != nil {
return err
}
if row[*a.FilterColIdx].Datum != tree.DBoolTrue {
// This row doesn't contribute to this aggregation.
continue
}
}
// Extract the corresponding arguments from the row to feed into the
// aggregate function.
// Most functions require at most one argument thus we separate
// the first argument and allocation of (if applicable) a variadic
// collection of arguments thereafter.
var firstArg tree.Datum
var otherArgs tree.Datums
if len(a.ColIdx) > 1 {
otherArgs = make(tree.Datums, len(a.ColIdx)-1)
}
isFirstArg := true
for j, c := range a.ColIdx {
if err := row[c].EnsureDecoded(&ag.inputTypes[c], &ag.datumAlloc); err != nil {
return err
}
if isFirstArg {
firstArg = row[c].Datum
isFirstArg = false
continue
}
otherArgs[j-1] = row[c].Datum
}
canAdd, err := ag.funcs[i].canAdd(ag.Ctx, groupKey, firstArg, otherArgs)
if err != nil {
return err
}
if !canAdd {
continue
}
if err := bucket[i].Add(ag.Ctx, firstArg, otherArgs...); err != nil {
return err
}
}
return nil
}
// accumulateRow accumulates a single row, returning an error if accumulation
// failed for any reason.
func (ag *hashAggregator) accumulateRow(row sqlbase.EncDatumRow) error {
if err := ag.cancelChecker.Check(); err != nil {
return err
}
// The encoding computed here determines which bucket the non-grouping
// datums are accumulated to.
encoded, err := ag.encode(ag.scratch, row)
if err != nil {
return err
}
ag.scratch = encoded[:0]
bucket, ok := ag.buckets[string(encoded)]
if !ok {
s, err := ag.arena.AllocBytes(ag.Ctx, encoded)
if err != nil {
return err
}
bucket, err = ag.createAggregateFuncs()
if err != nil {
return err
}
ag.buckets[s] = bucket
}
return ag.accumulateRowIntoBucket(row, encoded, bucket)
}
// accumulateRow accumulates a single row, returning an error if accumulation
// failed for any reason.
func (ag *orderedAggregator) accumulateRow(row sqlbase.EncDatumRow) error {
if err := ag.cancelChecker.Check(); err != nil {
return err
}
if ag.bucket == nil {
var err error
ag.bucket, err = ag.createAggregateFuncs()
if err != nil {
return err
}
}
return ag.accumulateRowIntoBucket(row, nil /* groupKey */, ag.bucket)
}
type aggregateFuncHolder struct {
create func(*tree.EvalContext, tree.Datums) tree.AggregateFunc
// arguments is the list of constant (non-aggregated) arguments to the
// aggregate, for instance, the separator in string_agg.
arguments tree.Datums
group *aggregatorBase
seen map[string]struct{}
arena *stringarena.Arena
}
const sizeOfAggregateFunc = int64(unsafe.Sizeof(tree.AggregateFunc(nil)))
func (ag *aggregatorBase) newAggregateFuncHolder(
create func(*tree.EvalContext, tree.Datums) tree.AggregateFunc, arguments tree.Datums,
) *aggregateFuncHolder {
return &aggregateFuncHolder{
create: create,
group: ag,
arena: &ag.arena,
arguments: arguments,
}
}
func (a *aggregateFuncHolder) canAdd(
ctx context.Context, encodingPrefix []byte, firstArg tree.Datum, otherArgs tree.Datums,
) (bool, error) {
if a.seen != nil {
encoded, err := sqlbase.EncodeDatumKeyAscending(encodingPrefix, firstArg)
if err != nil {
return false, err
}
// Encode additional arguments if necessary.
if otherArgs != nil {
encoded, err = sqlbase.EncodeDatumsKeyAscending(encoded, otherArgs)
if err != nil {
return false, err
}
}
if _, ok := a.seen[string(encoded)]; ok {
// skip
return false, nil
}
s, err := a.arena.AllocBytes(ctx, encoded)
if err != nil {
return false, err
}
a.seen[s] = struct{}{}
}
return true, nil
}
// encode returns the encoding for the grouping columns, this is then used as
// our group key to determine which bucket to add to.
func (ag *aggregatorBase) encode(
appendTo []byte, row sqlbase.EncDatumRow,
) (encoding []byte, err error) {
for _, colIdx := range ag.groupCols {
appendTo, err = row[colIdx].Encode(
&ag.inputTypes[colIdx], &ag.datumAlloc, sqlbase.DatumEncoding_ASCENDING_KEY, appendTo)
if err != nil {
return appendTo, err
}
}
return appendTo, nil
}
func (ag *aggregatorBase) createAggregateFuncs() (aggregateFuncs, error) {
if err := ag.bucketsAcc.Grow(ag.Ctx, sizeOfAggregateFunc*int64(len(ag.funcs))); err != nil {
return nil, err
}
bucket := make(aggregateFuncs, len(ag.funcs))
for i, f := range ag.funcs {
agg := f.create(ag.flowCtx.EvalCtx, f.arguments)
if err := ag.bucketsAcc.Grow(ag.Ctx, agg.Size()); err != nil {
return nil, err
}
bucket[i] = agg
}
return bucket, nil
}