This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
aggregation.go
268 lines (243 loc) · 6.09 KB
/
aggregation.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
/*
* Radon
*
* Copyright 2019 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package sqltypes
import (
"github.com/xelabs/go-mysqlstack/sqlparser/depends/common"
querypb "github.com/xelabs/go-mysqlstack/sqlparser/depends/query"
)
// 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"
)
// Aggregation operator.
type Aggregation struct {
distinct bool
index int
aggrTyp AggrType
fieldType querypb.Type
isPushDown bool
// prec controls the number of digits.
prec int
}
// AggEvaluateContext is used to store intermediate result when calculating aggregate functions.
type AggEvaluateContext struct {
count int64
val Value
hasErr bool
// buffer used to store the values when Aggregation.distinct is true.
buffer *common.HashTable
}
// NewAggregation new an Aggregetion.
func NewAggregation(index int, aggrTyp AggrType, distinct, isPushDown bool) *Aggregation {
return &Aggregation{
distinct: distinct,
index: index,
aggrTyp: aggrTyp,
isPushDown: isPushDown,
prec: -1,
}
}
// InitEvalCtx used to init the AggEvaluateContext.
func (aggr *Aggregation) InitEvalCtx(x []Value) *AggEvaluateContext {
var count int64
v := MakeTrusted(Null, nil)
if x != nil {
v = x[aggr.index]
}
buffer := common.NewHashTable()
if !aggr.isPushDown && v.Type() != Null {
count = 1
if aggr.distinct {
key := v.Raw()
buffer.Put(key, []byte{})
}
}
return &AggEvaluateContext{
count: count,
val: v,
buffer: buffer,
}
}
// FixField used to fix querypb.Field lenght and decimal.
func (aggr *Aggregation) FixField(field *querypb.Field) {
if !aggr.isPushDown || aggr.aggrTyp == AggrTypeAvg {
switch aggr.aggrTyp {
case AggrTypeMax, AggrTypeMin:
case AggrTypeCount:
field.Decimals = 0
field.ColumnLength = 21
field.Type = querypb.Type_INT64
case AggrTypeAvg:
field.ColumnLength = field.ColumnLength + 4
decimals := field.Decimals + 4
if IsIntegral(field.Type) || field.Type == Decimal {
if IsUnsigned(field.Type) {
field.ColumnLength++
}
if field.Decimals == 0 {
field.ColumnLength++
}
if decimals > 30 {
decimals = 30
}
field.Type = Decimal
} else if IsFloat(field.Type) {
if decimals > 31 {
decimals = 31
}
field.Type = querypb.Type_FLOAT64
} else if IsTemporal(field.Type) {
field.Type = Decimal
} else {
decimals = 31
field.Type = querypb.Type_FLOAT64
}
field.Decimals = decimals
case AggrTypeSum:
if IsIntegral(field.Type) || field.Type == Decimal {
field.ColumnLength = field.ColumnLength + DecimalLongLongDigits
if IsUnsigned(field.Type) {
field.ColumnLength++
}
field.Type = Decimal
} else if IsFloat(field.Type) {
if field.Decimals < 31 {
field.ColumnLength = field.ColumnLength + DoubleDigits + 2
} else {
field.ColumnLength = 23
}
field.Type = querypb.Type_FLOAT64
} else if IsTemporal(field.Type) {
field.Type = Decimal
} else {
field.Decimals = 31
field.ColumnLength = 23
field.Type = querypb.Type_FLOAT64
}
}
}
// FLOAT(M,D).
if field.Type == Decimal || IsFloat(field.Type) && field.Decimals < 31 {
aggr.prec = int(field.Decimals)
}
aggr.fieldType = field.Type
}
// Update during executing.
func (aggr *Aggregation) Update(x []Value, evalCtx *AggEvaluateContext) {
if evalCtx.hasErr {
return
}
v := x[aggr.index]
if v.Type() == Null {
return
}
if !aggr.isPushDown && aggr.distinct {
key := v.Raw()
if has, _ := evalCtx.buffer.Get(key); !has {
evalCtx.buffer.Put(key, []byte{})
} else {
return
}
}
var err error
switch aggr.aggrTyp {
case AggrTypeMin:
evalCtx.val = Min(evalCtx.val, v)
case AggrTypeMax:
evalCtx.val = Max(evalCtx.val, v)
case AggrTypeSum:
evalCtx.count++
evalCtx.val, err = NullsafeSum(evalCtx.val, v, aggr.fieldType, aggr.prec)
case AggrTypeCount:
if aggr.isPushDown {
evalCtx.val, err = NullsafeAdd(evalCtx.val, v, aggr.fieldType, aggr.prec)
} else {
evalCtx.count++
}
case AggrTypeAvg:
if !aggr.isPushDown {
evalCtx.count++
evalCtx.val, err = NullsafeSum(evalCtx.val, v, aggr.fieldType, aggr.prec)
}
}
if err != nil {
evalCtx.hasErr = true
}
}
// GetResult used to get Value finally.
func (aggr *Aggregation) GetResult(evalCtx *AggEvaluateContext) Value {
var val Value
var err error
if evalCtx.hasErr {
return MakeTrusted(aggr.fieldType, []byte("0"))
}
switch aggr.aggrTyp {
case AggrTypeAvg:
if !aggr.isPushDown {
val, err = NullsafeDiv(evalCtx.val, NewInt64(evalCtx.count), aggr.fieldType, aggr.prec)
}
case AggrTypeMax, AggrTypeMin:
val = evalCtx.val
case AggrTypeSum:
val, err = Cast(evalCtx.val, aggr.fieldType)
case AggrTypeCount:
if aggr.isPushDown {
val = evalCtx.val
} else {
val = NewInt64(evalCtx.count)
}
}
if err != nil {
val = MakeTrusted(aggr.fieldType, []byte("0"))
}
return val
}
// NewAggEvalCtxs new evalCtxs.
func NewAggEvalCtxs(aggrs []*Aggregation, x []Value) []*AggEvaluateContext {
var evalCtxs []*AggEvaluateContext
for _, aggr := range aggrs {
evalCtx := aggr.InitEvalCtx(x)
evalCtxs = append(evalCtxs, evalCtx)
}
return evalCtxs
}
// GetResults will be called when all data have been processed.
func GetResults(aggrs []*Aggregation, evalCtxs []*AggEvaluateContext, x []Value) ([]Value, []int) {
var deIdxs []int
i := 0
for i < len(aggrs) {
aggr := aggrs[i]
evalCtx := evalCtxs[i]
if aggr.isPushDown && aggr.aggrTyp == AggrTypeAvg {
var err error
if x[aggr.index], err = NullsafeDiv(evalCtxs[i+1].val, evalCtxs[i+2].val, aggr.fieldType, aggr.prec); err != nil {
x[aggr.index] = MakeTrusted(aggr.fieldType, []byte("0"))
}
deIdxs = append(deIdxs, aggr.index+1)
i = i + 2
} else {
x[aggr.index] = aggr.GetResult(evalCtx)
}
i++
}
return x, deIdxs
}