-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathaggregate_executor.go
More file actions
99 lines (87 loc) · 2.44 KB
/
aggregate_executor.go
File metadata and controls
99 lines (87 loc) · 2.44 KB
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
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package executor
import (
"expression"
"planner"
"xcontext"
"github.com/xelabs/go-mysqlstack/sqlparser/depends/common"
"github.com/xelabs/go-mysqlstack/sqlparser/depends/sqltypes"
"github.com/xelabs/go-mysqlstack/xlog"
)
var (
_ Executor = &AggregateExecutor{}
)
// AggregateExecutor represents aggregate executor.
// Including: COUNT/MAX/MIN/SUM/AVG/GROUPBY.
type AggregateExecutor struct {
log *xlog.Log
plan planner.Plan
}
// NewAggregateExecutor creates new AggregateExecutor.
func NewAggregateExecutor(log *xlog.Log, plan planner.Plan) *AggregateExecutor {
return &AggregateExecutor{
log: log,
plan: plan,
}
}
// Execute used to execute the executor.
func (executor *AggregateExecutor) Execute(ctx *xcontext.ResultContext) error {
rs := ctx.Results
executor.aggregate(rs)
return nil
}
// Aggregate used to do rows-aggregator(COUNT/SUM/MIN/MAX/AVG) and grouped them into group-by fields.
func (executor *AggregateExecutor) aggregate(result *sqltypes.Result) {
var deIdxs []int
plan := executor.plan.(*planner.AggregatePlan)
if plan.Empty() {
return
}
aggs := plan.NormalAggregators()
aggrLen := len(aggs)
groupAggrs := plan.GroupAggregators()
type group struct {
row []sqltypes.Value
evalCtxs []*expression.AggEvaluateContext
}
aggrs := expression.NewAggregations(aggs, plan.IsPushDown, result.Fields)
groups := make(map[string]group)
for _, row1 := range result.Rows {
keySlice := []byte{0x01}
for _, v := range groupAggrs {
keySlice = append(keySlice, row1[v.Index].Raw()...)
keySlice = append(keySlice, 0x02)
}
key := common.BytesToString(keySlice)
if g, ok := groups[key]; !ok {
evalCtxs := expression.NewAggEvalCtxs(aggrs, row1)
groups[key] = group{row1, evalCtxs}
} else {
if aggrLen > 0 {
for i, aggr := range aggrs {
aggr.Update(row1, g.evalCtxs[i])
}
}
}
}
// Handle the avg operator and rebuild the results.
i := 0
result.Rows = make([][]sqltypes.Value, len(groups))
for _, g := range groups {
result.Rows[i], deIdxs = expression.GetResults(aggrs, g.evalCtxs, g.row)
i++
}
if len(groups) == 0 && aggrLen > 0 {
result.Rows = make([][]sqltypes.Value, 1)
evalCtxs := expression.NewAggEvalCtxs(aggrs, nil)
result.Rows[0], deIdxs = expression.GetResults(aggrs, evalCtxs, make([]sqltypes.Value, len(aggrs)))
}
// Remove avg decompose columns.
result.RemoveColumns(deIdxs...)
}