/
window.go
249 lines (215 loc) · 5.63 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
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
// 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 (
"io"
"strings"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/expression"
)
type Window struct {
SelectExprs []sql.Expression
UnaryNode
}
var _ sql.Node = (*Window)(nil)
var _ sql.Expressioner = (*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(%s)", strings.Join(exprs, ", "))
_ = pr.WriteChildren(sql.DebugString(w.Child))
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] = expression.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
}
// Expressions implements sql.Expressioner
func (w *Window) Expressions() []sql.Expression {
return w.SelectExprs
}
// 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
}
return &windowIter{
selectExprs: w.SelectExprs,
childIter: childIter,
ctx: ctx,
}, nil
}
type windowIter struct {
ctx *sql.Context
selectExprs []sql.Expression
childIter sql.RowIter
rows []sql.Row
buffers []sql.Row
pos int
}
func (i *windowIter) Next() (sql.Row, error) {
if i.buffers == nil {
err := i.compute()
if err != nil {
return nil, err
}
}
if i.pos >= len(i.rows) {
return nil, io.EOF
}
row := i.rows[i.pos]
for j, expr := range i.selectExprs {
var err error
switch expr := expr.(type) {
case sql.WindowAggregation:
row[j], err = expr.EvalRow(i.pos, i.buffers[j])
if err != nil {
return nil, err
}
case sql.Aggregation:
row[j], err = i.buffers[j][0].(sql.AggregationBuffer).Eval(i.ctx)
if err != nil {
return nil, err
}
}
}
i.pos++
return row, nil
}
func (i *windowIter) compute() error {
i.buffers = make([]sql.Row, len(i.selectExprs))
// TOOD(aaron): i.buffers is a []sql.Row, and we tuck an
// AggregationBuffer into one when we have a sql.Aggregation. But
// i.buffers should get the same treatment it gets in groupByIter,
// where we can use a common interface that makes sense for our use
// case. This needs some work to look better.
var err error
for j, expr := range i.selectExprs {
i.buffers[j], err = newBuffer(expr)
if err != nil {
return err
}
}
for {
row, err := i.childIter.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
outRow := make(sql.Row, len(i.selectExprs))
for j, expr := range i.selectExprs {
switch expr := expr.(type) {
case sql.WindowAggregation:
err := expr.Add(i.ctx, i.buffers[j], row)
if err != nil {
return err
}
case sql.Aggregation:
err = i.buffers[j][0].(sql.AggregationBuffer).Update(i.ctx, row)
if err != nil {
return err
}
default:
outRow[j], err = expr.Eval(i.ctx, row)
if err != nil {
return err
}
}
}
i.rows = append(i.rows, outRow)
}
for j, expr := range i.selectExprs {
if wa, ok := expr.(sql.WindowAggregation); ok {
err := wa.Finish(i.ctx, i.buffers[j])
if err != nil {
return err
}
}
}
return nil
}
func newBuffer(expr sql.Expression) (sql.Row, error) {
switch n := expr.(type) {
case sql.Aggregation:
// For now, we tuck the sql.AggregationBuffer into the first
// element of the returned row.
b, err := n.NewBuffer()
if err != nil {
return nil, err
}
return sql.NewRow(b), nil
case sql.WindowAggregation:
return n.NewBuffer(), nil
default:
return nil, nil
}
}
func (i *windowIter) Close(ctx *sql.Context) error {
i.Dispose()
i.buffers = nil
return i.childIter.Close(ctx)
}
func (i *windowIter) Dispose() {
for j, expr := range i.selectExprs {
switch expr.(type) {
case sql.Aggregation:
i.buffers[j][0].(sql.AggregationBuffer).Dispose()
}
}
}