forked from dolthub/go-mysql-server
/
window.go
202 lines (176 loc) · 5.76 KB
/
window.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
// Copyright 2021 Dolthub, Inc.
//
// 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 plan
import (
"errors"
"strings"
"github.com/gabereiser/go-mysql-server/sql"
"github.com/gabereiser/go-mysql-server/sql/expression"
"github.com/gabereiser/go-mysql-server/sql/expression/function/aggregation"
"github.com/gabereiser/go-mysql-server/sql/transform"
)
var ErrAggregationMissingWindow = errors.New("aggregation missing window expression")
type Window struct {
SelectExprs []sql.Expression
UnaryNode
}
var _ sql.Expressioner = (*Window)(nil)
var _ sql.Node = (*Window)(nil)
var _ sql.Projector = (*Window)(nil)
func NewWindow(selectExprs []sql.Expression, node sql.Node) *Window {
return &Window{
SelectExprs: selectExprs,
UnaryNode: UnaryNode{node},
}
}
// Resolved implements sql.Node
func (w *Window) Resolved() bool {
return w.UnaryNode.Child.Resolved() &&
expression.ExpressionsResolved(w.SelectExprs...)
}
func (w *Window) String() string {
pr := sql.NewTreePrinter()
var exprs = make([]string, len(w.SelectExprs))
for i, expr := range w.SelectExprs {
exprs[i] = expr.String()
}
_ = pr.WriteNode("Window(%s)", strings.Join(exprs, ", "))
_ = pr.WriteChildren(w.Child.String())
return pr.String()
}
func (w *Window) DebugString() string {
pr := sql.NewTreePrinter()
var exprs = make([]string, len(w.SelectExprs))
for i, expr := range w.SelectExprs {
exprs[i] = sql.DebugString(expr)
}
_ = pr.WriteNode("Window")
exprs = append(exprs, sql.DebugString(w.Child))
_ = pr.WriteChildren(exprs...)
return pr.String()
}
// Schema implements sql.Node
func (w *Window) Schema() sql.Schema {
var s = make(sql.Schema, len(w.SelectExprs))
for i, e := range w.SelectExprs {
s[i] = transform.ExpressionToColumn(e)
}
return s
}
// WithChildren implements sql.Node
func (w *Window) WithChildren(children ...sql.Node) (sql.Node, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(w, len(children), 1)
}
return NewWindow(w.SelectExprs, children[0]), nil
}
// CheckPrivileges implements the interface sql.Node.
func (w *Window) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
return w.Child.CheckPrivileges(ctx, opChecker)
}
// Expressions implements sql.Expressioner
func (w *Window) Expressions() []sql.Expression {
return w.SelectExprs
}
// ProjectedExprs implements sql.Projector
func (w *Window) ProjectedExprs() []sql.Expression {
return w.SelectExprs
}
// WithProjectedExprs implements sql.Projector
func (w *Window) WithProjectedExprs(exprs ...sql.Expression) (sql.Projector, error) {
node, err := w.WithExpressions(exprs...)
return node.(sql.Projector), err
}
// WithExpressions implements sql.Expressioner
func (w *Window) WithExpressions(e ...sql.Expression) (sql.Node, error) {
if len(e) != len(w.SelectExprs) {
return nil, sql.ErrInvalidChildrenNumber.New(w, len(e), len(w.SelectExprs))
}
return NewWindow(e, w.Child), nil
}
// RowIter implements sql.Node
func (w *Window) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
childIter, err := w.Child.RowIter(ctx, row)
if err != nil {
return nil, err
}
blockIters, outputOrdinals, err := windowToIter(w)
if err != nil {
return nil, err
}
return aggregation.NewWindowIter(blockIters, outputOrdinals, childIter), nil
}
// windowToIter transforms a plan.Window into a series
// of aggregation.WindowPartitionIter and a list of output projection indexes
// for each window partition.
// TODO: make partition ordering deterministic
func windowToIter(w *Window) ([]*aggregation.WindowPartitionIter, [][]int, error) {
partIdToOutputIdxs := make(map[uint64][]int, 0)
partIdToBlock := make(map[uint64]*aggregation.WindowPartition, 0)
var window *sql.WindowDefinition
var agg *aggregation.Aggregation
var fn sql.WindowFunction
var err error
// collect functions in hash map keyed by partitioning scheme
for i, expr := range w.SelectExprs {
if alias, ok := expr.(*expression.Alias); ok {
expr = alias.Child
}
switch e := expr.(type) {
case sql.Aggregation:
window = e.Window()
fn, err = e.NewWindowFunction()
case sql.WindowAggregation:
window = e.Window()
fn, err = e.NewWindowFunction()
default:
// non window aggregates resolve to LastAgg with empty over clause
window = sql.NewWindowDefinition(nil, nil, nil, "", "")
fn, err = aggregation.NewLast(e).NewWindowFunction()
}
if err != nil {
return nil, nil, err
}
agg = aggregation.NewAggregation(fn, fn.DefaultFramer())
id, err := window.PartitionId()
if err != nil {
return nil, nil, err
}
if block, ok := partIdToBlock[id]; !ok {
if err != nil {
return nil, nil, err
}
partIdToBlock[id] = aggregation.NewWindowPartition(
window.PartitionBy,
window.OrderBy,
[]*aggregation.Aggregation{agg},
)
partIdToOutputIdxs[id] = []int{i}
} else {
block.AddAggregation(agg)
partIdToOutputIdxs[id] = append(partIdToOutputIdxs[id], i)
}
}
// convert partition hash map into list
blockIters := make([]*aggregation.WindowPartitionIter, len(partIdToBlock))
outputOrdinals := make([][]int, len(partIdToBlock))
i := 0
for id, block := range partIdToBlock {
outputIdx := partIdToOutputIdxs[id]
blockIters[i] = aggregation.NewWindowPartitionIter(block)
outputOrdinals[i] = outputIdx
i++
}
return blockIters, outputOrdinals, nil
}