-
Notifications
You must be signed in to change notification settings - Fork 2k
/
grouping.go
128 lines (114 loc) · 3.54 KB
/
grouping.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
/*
Copyright 2020 The Vitess 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 planbuilder
import (
"fmt"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
)
func planGroupBy(pb *primitiveBuilder, input logicalPlan, groupBy sqlparser.GroupBy) (logicalPlan, error) {
if len(groupBy) == 0 {
// if we have no grouping declared, we only want to visit orderedAggregate
_, isOrdered := input.(*orderedAggregate)
if !isOrdered {
return input, nil
}
}
switch node := input.(type) {
case *mergeSort, *pulloutSubquery, *distinct:
inputs := node.Inputs()
input := inputs[0]
newInput, err := planGroupBy(pb, input, groupBy)
if err != nil {
return nil, err
}
inputs[0] = newInput
err = node.Rewrite(inputs...)
if err != nil {
return nil, err
}
return node, nil
case *route:
node.Select.(*sqlparser.Select).GroupBy = groupBy
return node, nil
case *orderedAggregate:
for _, expr := range groupBy {
colNumber := -1
switch e := expr.(type) {
case *sqlparser.ColName:
c := e.Metadata.(*column)
if c.Origin() == node {
return nil, vterrors.VT03005(sqlparser.String(e))
}
for i, rc := range node.resultColumns {
if rc.column == c {
colNumber = i
break
}
}
if colNumber == -1 {
return nil, vterrors.VT12001("in scatter query: GROUP BY column must reference column in SELECT list")
}
case *sqlparser.Literal:
num, err := ResultFromNumber(node.resultColumns, e, "group statement")
if err != nil {
return nil, err
}
colNumber = num
default:
return nil, vterrors.VT12001("in scatter query: only simple references are allowed")
}
node.groupByKeys = append(node.groupByKeys, &engine.GroupByParams{KeyCol: colNumber, WeightStringCol: -1, FromGroupBy: true})
}
// Append the distinct aggregate if any.
if node.extraDistinct != nil {
groupBy = append(groupBy, node.extraDistinct)
}
newInput, err := planGroupBy(pb, node.input, groupBy)
if err != nil {
return nil, err
}
node.input = newInput
return node, nil
}
return nil, vterrors.VT13001(fmt.Sprintf("unreachable %T.groupBy: ", input))
}
// planDistinct makes the output distinct
func planDistinct(input logicalPlan) (logicalPlan, error) {
switch node := input.(type) {
case *route:
node.Select.MakeDistinct()
return node, nil
case *orderedAggregate:
for i, rc := range node.resultColumns {
// If the column origin is oa (and not the underlying route),
// it means that it's an aggregate function supplied by oa.
// So, the distinct 'operator' cannot be pushed down into the
// route.
if rc.column.Origin() == node {
return newDistinctV3(node), nil
}
node.groupByKeys = append(node.groupByKeys, &engine.GroupByParams{KeyCol: i, WeightStringCol: -1, FromGroupBy: false})
}
newInput, err := planDistinct(node.input)
if err != nil {
return nil, err
}
node.input = newInput
return node, nil
case *distinct:
return input, nil
}
return nil, vterrors.VT13001(fmt.Sprintf("unreachable %T.distinct", input))
}