forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 2
/
group.go
441 lines (380 loc) · 9.35 KB
/
group.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
// Copyright 2015 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. See the AUTHORS file
// for names of contributors.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
package sql
import (
"fmt"
"strings"
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/cockroachdb/cockroach/util/log"
)
var aggregates = map[string]aggregateImpl{
"avg": &avgAggregate{},
"count": &countAggregate{},
"max": &maxAggregate{},
"min": &minAggregate{},
"sum": &sumAggregate{},
}
func (p *planner) groupBy(n *parser.Select, s *scanNode) (*groupNode, error) {
// TODO(pmattis): This only handles aggregate functions, not GROUP BY.
// Loop over the render expressions and extract any aggregate functions.
var funcs []*aggregateFunc
for i, r := range s.render {
r, f, err := extractAggregateFuncs(r)
if err != nil {
return nil, err
}
s.render[i] = r
funcs = append(funcs, f...)
}
if len(funcs) == 0 {
return nil, nil
}
// Aggregation is being performed. Loop over the render expressions again and
// verify that the only qvalues mentioned by GROUP BY expressions are allowed
// outside of the aggregate function arguments. For example, the following is
// illegal because k is used outside of the aggregate function and not part
// of a GROUP BY expression.
//
// SELECT COUNT(k), k FROM kv GROUP BY v
for _, r := range s.render {
if err := checkAggregateExpr(r); err != nil {
return nil, err
}
}
if log.V(2) {
strs := make([]string, 0, len(funcs))
for _, f := range funcs {
strs = append(strs, f.val.String())
}
log.Infof("Group: %s", strings.Join(strs, ", "))
}
group := &groupNode{
columns: s.columns,
render: s.render,
funcs: funcs,
}
// Replace the render expressions in the scanNode with expressions that
// compute only the arguments to the aggregate expressions.
s.columns = make([]string, 0, len(funcs))
s.render = make([]parser.Expr, 0, len(funcs))
for _, f := range funcs {
if len(f.val.Exprs) != 1 {
panic(fmt.Sprintf("%s has %d arguments (expected 1)", f.val.Name, len(f.val.Exprs)))
}
s.columns = append(s.columns, f.val.String())
s.render = append(s.render, f.val.Exprs[0])
}
return group, nil
}
type groupNode struct {
plan planNode
columns []string
row parser.DTuple
render []parser.Expr
funcs []*aggregateFunc
needGroup bool
err error
}
func (n *groupNode) Columns() []string {
return n.columns
}
func (n *groupNode) Ordering() []int {
return n.plan.Ordering()
}
func (n *groupNode) Values() parser.DTuple {
return n.row
}
func (n *groupNode) Next() bool {
if !n.needGroup {
return false
}
n.needGroup = false
// Loop over the rows passing the values into the corresponding aggregation
// functions.
for n.plan.Next() {
values := n.plan.Values()
for i, f := range n.funcs {
if n.err = f.impl.Add(values[i]); n.err != nil {
return false
}
}
}
n.err = n.plan.Err()
if n.err != nil {
return false
}
// Fill in the aggregate function result value.
for _, f := range n.funcs {
if f.val.datum, n.err = f.impl.Result(); n.err != nil {
return false
}
}
// Render the results.
n.row = make([]parser.Datum, len(n.render))
for i, r := range n.render {
n.row[i], n.err = parser.EvalExpr(r)
if n.err != nil {
return false
}
}
return n.err == nil
}
func (n *groupNode) Err() error {
return n.err
}
func (n *groupNode) ExplainPlan() (name, description string, children []planNode) {
name = "group"
strs := make([]string, 0, len(n.funcs))
for _, f := range n.funcs {
strs = append(strs, f.val.String())
}
description = strings.Join(strs, ", ")
return name, description, []planNode{n.plan}
}
// wrap the supplied planNode with the groupNode if grouping/aggregation is required.
func (n *groupNode) wrap(plan planNode) planNode {
if n == nil {
return plan
}
n.plan = plan
n.needGroup = true
return n
}
type extractAggregatesVisitor struct {
funcs []*aggregateFunc
err error
}
var _ parser.Visitor = &extractAggregatesVisitor{}
func (v *extractAggregatesVisitor) Visit(expr parser.Expr, pre bool) (parser.Visitor, parser.Expr) {
if !pre || v.err != nil {
return nil, expr
}
switch t := expr.(type) {
case *parser.FuncExpr:
if len(t.Name.Indirect) > 0 {
break
}
if impl, ok := aggregates[strings.ToLower(string(t.Name.Base))]; ok {
f := &aggregateFunc{
val: aggregateValue{
FuncExpr: t,
},
impl: impl.New(),
}
v.funcs = append(v.funcs, f)
return nil, &f.val
}
}
return v, expr
}
func extractAggregateFuncs(expr parser.Expr) (parser.Expr, []*aggregateFunc, error) {
v := extractAggregatesVisitor{}
expr = parser.WalkExpr(&v, expr)
return expr, v.funcs, v.err
}
type checkAggregateVisitor struct {
err error
}
var _ parser.Visitor = &checkAggregateVisitor{}
func (v *checkAggregateVisitor) Visit(expr parser.Expr, pre bool) (parser.Visitor, parser.Expr) {
if !pre || v.err != nil {
return nil, expr
}
switch t := expr.(type) {
case *qvalue:
// TODO(pmattis): Check that the qvalue is part of a GROUP BY expression.
v.err = fmt.Errorf("column \"%s\" must appear in the GROUP BY clause or be used in an aggregate function", t.col.Name)
return nil, expr
}
return v, expr
}
func checkAggregateExpr(expr parser.Expr) error {
v := checkAggregateVisitor{}
_ = parser.WalkExpr(&v, expr)
return v.err
}
type aggregateValue struct {
datum parser.Datum
// Tricky: we embed a parser.FuncExpr so that aggregateValue implements
// parser.expr()! Note that we can't just implement aggregateValue.expr() as
// that interface method is defined in the wrong package.
*parser.FuncExpr
}
var _ parser.DReference = &aggregateValue{}
func (v *aggregateValue) Datum() parser.Datum {
return v.datum
}
type aggregateFunc struct {
val aggregateValue
impl aggregateImpl
}
type aggregateImpl interface {
New() aggregateImpl
Add(parser.Datum) error
Result() (parser.Datum, error)
}
var _ aggregateImpl = &avgAggregate{}
var _ aggregateImpl = &countAggregate{}
var _ aggregateImpl = &maxAggregate{}
var _ aggregateImpl = &minAggregate{}
var _ aggregateImpl = &sumAggregate{}
type avgAggregate struct {
sumAggregate
count int
}
func (a *avgAggregate) New() aggregateImpl {
return &avgAggregate{}
}
func (a *avgAggregate) Add(datum parser.Datum) error {
if datum == parser.DNull {
return nil
}
if err := a.sumAggregate.Add(datum); err != nil {
return err
}
a.count++
return nil
}
func (a *avgAggregate) Result() (parser.Datum, error) {
sum, err := a.sumAggregate.Result()
if err != nil {
return parser.DNull, err
}
if sum == parser.DNull {
return sum, nil
}
switch t := sum.(type) {
case parser.DInt:
return parser.DFloat(t) / parser.DFloat(a.count), nil
case parser.DFloat:
return t / parser.DFloat(a.count), nil
default:
return parser.DNull, fmt.Errorf("unexpected SUM result type: %s", t.Type())
}
}
type countAggregate struct {
count int
}
func (a *countAggregate) New() aggregateImpl {
return &countAggregate{}
}
func (a *countAggregate) Add(datum parser.Datum) error {
if datum == parser.DNull {
return nil
}
switch t := datum.(type) {
case parser.DTuple:
for _, d := range t {
if d != parser.DNull {
a.count++
break
}
}
default:
a.count++
}
return nil
}
func (a *countAggregate) Result() (parser.Datum, error) {
return parser.DInt(a.count), nil
}
type maxAggregate struct {
max parser.Datum
}
func (a *maxAggregate) New() aggregateImpl {
return &maxAggregate{}
}
func (a *maxAggregate) Add(datum parser.Datum) error {
if datum == parser.DNull {
return nil
}
if a.max == nil {
a.max = datum
return nil
}
c := a.max.Compare(datum)
if c < 0 {
a.max = datum
}
return nil
}
func (a *maxAggregate) Result() (parser.Datum, error) {
if a.max == nil {
return parser.DNull, nil
}
return a.max, nil
}
type minAggregate struct {
min parser.Datum
}
func (a *minAggregate) New() aggregateImpl {
return &minAggregate{}
}
func (a *minAggregate) Add(datum parser.Datum) error {
if datum == parser.DNull {
return nil
}
if a.min == nil {
a.min = datum
return nil
}
c := a.min.Compare(datum)
if c > 0 {
a.min = datum
}
return nil
}
func (a *minAggregate) Result() (parser.Datum, error) {
if a.min == nil {
return parser.DNull, nil
}
return a.min, nil
}
type sumAggregate struct {
sum parser.Datum
}
func (a *sumAggregate) New() aggregateImpl {
return &sumAggregate{}
}
func (a *sumAggregate) Add(datum parser.Datum) error {
if datum == parser.DNull {
return nil
}
if a.sum == nil {
a.sum = datum
return nil
}
switch t := datum.(type) {
case parser.DInt:
if v, ok := a.sum.(parser.DInt); ok {
a.sum = v + t
return nil
}
case parser.DFloat:
if v, ok := a.sum.(parser.DFloat); ok {
a.sum = v + t
return nil
}
}
return fmt.Errorf("unexpected SUM argument type: %s", datum.Type())
}
func (a *sumAggregate) Result() (parser.Datum, error) {
if a.sum == nil {
return parser.DNull, nil
}
return a.sum, nil
}