-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathaggregate_plan.go
More file actions
223 lines (190 loc) · 5.09 KB
/
aggregate_plan.go
File metadata and controls
223 lines (190 loc) · 5.09 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
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
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package planner
import (
"encoding/json"
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/xelabs/go-mysqlstack/sqlparser"
"github.com/xelabs/go-mysqlstack/xlog"
)
var (
_ Plan = &AggregatePlan{}
)
// AggrType type.
type AggrType string
const (
// AggrTypeNull enum.
AggrTypeNull AggrType = ""
// AggrTypeCount enum.
AggrTypeCount AggrType = "COUNT"
// AggrTypeSum enum.
AggrTypeSum AggrType = "SUM"
// AggrTypeMin enum.
AggrTypeMin AggrType = "MIN"
// AggrTypeMax enum.
AggrTypeMax AggrType = "MAX"
// AggrTypeAvg enum.
AggrTypeAvg AggrType = "AVG"
// AggrTypeGroupBy enum.
AggrTypeGroupBy AggrType = "GROUP BY"
)
// Aggregator tuple.
type Aggregator struct {
Field string
Index int
Type AggrType
Distinct bool
}
// AggregatePlan represents order-by plan.
type AggregatePlan struct {
log *xlog.Log
tuples []selectTuple
groups []selectTuple
rewritten sqlparser.SelectExprs
normalAggrs []Aggregator
groupAggrs []Aggregator
// type
typ PlanType
// IsPushDown whether aggfunc can be pushed down.
IsPushDown bool
}
// NewAggregatePlan used to create AggregatePlan.
func NewAggregatePlan(log *xlog.Log, exprs []sqlparser.SelectExpr, tuples, groups []selectTuple, isPushDown bool) *AggregatePlan {
return &AggregatePlan{
log: log,
tuples: tuples,
groups: groups,
rewritten: exprs,
typ: PlanTypeAggregate,
IsPushDown: isPushDown,
}
}
// analyze used to check the aggregator is at the support level.
// Supports:
// SUM/COUNT/MIN/MAX/AVG/GROUPBY
// Notes:
// group by fields must be in the select list, for example:
// select count(a), a from t group by a --[OK]
// select count(a) from t group by a --[ER]
func (p *AggregatePlan) analyze() error {
var nullAggrs []Aggregator
tuples := p.tuples
// aggregators.
k := 0
for _, tuple := range tuples {
aggrFuc := strings.ToLower(tuple.aggrFuc)
if aggrFuc == "" {
if tuple.field == "*" {
return errors.Errorf("unsupported: exists.aggregate.and.'*'.select.exprs")
}
nullAggrs = append(nullAggrs, Aggregator{Field: tuple.field, Index: k, Type: AggrTypeNull})
k++
continue
}
var aggType AggrType
switch aggrFuc {
case "sum":
aggType = AggrTypeSum
case "count":
aggType = AggrTypeCount
case "min":
aggType = AggrTypeMin
case "max":
aggType = AggrTypeMax
case "avg":
aggType = AggrTypeAvg
default:
return errors.Errorf("unsupported: function:%+v", tuple.aggrFuc)
}
p.normalAggrs = append(p.normalAggrs, Aggregator{Field: tuple.field, Index: k, Type: aggType, Distinct: tuple.distinct})
if p.IsPushDown {
if aggType == AggrTypeAvg {
p.normalAggrs = append(p.normalAggrs, Aggregator{Field: fmt.Sprintf("sum(%s)", tuple.aggrField), Index: k, Type: AggrTypeSum})
p.normalAggrs = append(p.normalAggrs, Aggregator{Field: fmt.Sprintf("count(%s)", tuple.aggrField), Index: k + 1, Type: AggrTypeCount})
avgs := decomposeAvg(&tuple)
p.rewritten = append(p.rewritten, &sqlparser.AliasedExpr{})
copy(p.rewritten[(k+2):], p.rewritten[k+1:])
p.rewritten[k] = avgs[0]
p.rewritten[(k + 1)] = avgs[1]
k++
}
} else {
p.rewritten[k] = decomposeAgg(&tuple)
p.tuples[k].expr = p.rewritten[k]
}
k++
}
// Groupbys.
for _, by := range p.groups {
// check: groupby field in select list
idx := -1
for _, null := range nullAggrs {
if null.Field == by.field {
idx = null.Index
break
}
}
if idx == -1 {
return errors.Errorf("unsupported: group.by.field[%s].should.be.in.noaggregate.select.list", by.field)
}
p.groupAggrs = append(p.groupAggrs, Aggregator{Field: by.field, Index: idx, Type: AggrTypeGroupBy})
}
return nil
}
// Build used to build distributed querys.
func (p *AggregatePlan) Build() error {
return p.analyze()
}
// Type returns the type of the plan.
func (p *AggregatePlan) Type() PlanType {
return p.typ
}
// JSON returns the plan info.
func (p *AggregatePlan) JSON() string {
type aggrs struct {
Aggrs []Aggregator
ReWritten string
}
a := &aggrs{}
a.Aggrs = append(a.Aggrs, p.normalAggrs...)
a.Aggrs = append(a.Aggrs, p.groupAggrs...)
buf := sqlparser.NewTrackedBuffer(nil)
buf.Myprintf("%v", p.rewritten)
a.ReWritten = buf.String()
bout, err := json.MarshalIndent(a, "", "\t")
if err != nil {
return err.Error()
}
return string(bout)
}
// Children returns the children of the plan.
func (p *AggregatePlan) Children() *PlanTree {
return nil
}
// NormalAggregators returns the aggregators.
func (p *AggregatePlan) NormalAggregators() []Aggregator {
return p.normalAggrs
}
// GroupAggregators returns the group aggregators.
func (p *AggregatePlan) GroupAggregators() []Aggregator {
return p.groupAggrs
}
// ReWritten used to re-write the SelectExprs clause.
func (p *AggregatePlan) ReWritten() sqlparser.SelectExprs {
return p.rewritten
}
// Empty returns the aggregator number more than zero.
func (p *AggregatePlan) Empty() bool {
return (len(p.normalAggrs) == 0 && len(p.groupAggrs) == 0)
}
// Size returns the memory size.
func (p *AggregatePlan) Size() int {
return 0
}