-
Notifications
You must be signed in to change notification settings - Fork 0
/
row_number.go
executable file
·174 lines (146 loc) · 4.26 KB
/
row_number.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
// 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 window
import (
"sort"
"strings"
"github.com/Rock-liyi/p2pdb-store/sql/expression"
"github.com/Rock-liyi/p2pdb-store/sql"
)
type RowNumber struct {
window *sql.Window
pos int
}
var _ sql.FunctionExpression = (*RowNumber)(nil)
var _ sql.WindowAggregation = (*RowNumber)(nil)
func NewRowNumber() sql.Expression {
return &RowNumber{}
}
// Description implements sql.FunctionExpression
func (r *RowNumber) Description() string {
return "returns the number of rows updated."
}
// Window implements sql.WindowExpression
func (r *RowNumber) Window() *sql.Window {
return r.window
}
// IsNullable implements sql.Expression
func (r *RowNumber) Resolved() bool {
return windowResolved(r.window)
}
func (r *RowNumber) NewBuffer() sql.Row {
return sql.NewRow(make([]sql.Row, 0))
}
func (r *RowNumber) String() string {
sb := strings.Builder{}
sb.WriteString("row_number()")
if r.window != nil {
sb.WriteString(" ")
sb.WriteString(r.window.String())
}
return sb.String()
}
func (r *RowNumber) DebugString() string {
sb := strings.Builder{}
sb.WriteString("row_number()")
if r.window != nil {
sb.WriteString(" ")
sb.WriteString(sql.DebugString(r.window))
}
return sb.String()
}
// FunctionName implements sql.FunctionExpression
func (r *RowNumber) FunctionName() string {
return "ROW_NUMBER"
}
// Type implements sql.Expression
func (r *RowNumber) Type() sql.Type {
return sql.Int64
}
// IsNullable implements sql.Expression
func (r *RowNumber) IsNullable() bool {
return false
}
// Eval implements sql.Expression
func (r *RowNumber) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
panic("eval called on window function")
}
// Children implements sql.Expression
func (r *RowNumber) Children() []sql.Expression {
return r.window.ToExpressions()
}
// WithChildren implements sql.Expression
func (r *RowNumber) WithChildren(children ...sql.Expression) (sql.Expression, error) {
window, err := r.window.FromExpressions(children)
if err != nil {
return nil, err
}
return r.WithWindow(window)
}
// WithWindow implements sql.WindowAggregation
func (r *RowNumber) WithWindow(window *sql.Window) (sql.WindowAggregation, error) {
nr := *r
nr.window = window
return &nr, nil
}
// Add implements sql.WindowAggregation
func (r *RowNumber) Add(ctx *sql.Context, buffer, row sql.Row) error {
rows := buffer[0].([]sql.Row)
buffer[0] = append(rows, append(row, nil, r.pos))
r.pos++
return nil
}
// Finish implements sql.WindowAggregation
func (r *RowNumber) Finish(ctx *sql.Context, buffer sql.Row) error {
rows := buffer[0].([]sql.Row)
if len(rows) > 0 && r.window != nil && r.window.OrderBy != nil {
sorter := &expression.Sorter{
SortFields: append(partitionsToSortFields(r.Window().PartitionBy), r.Window().OrderBy...),
Rows: rows,
Ctx: ctx,
}
sort.Stable(sorter)
if sorter.LastError != nil {
return sorter.LastError
}
// Now that we have the rows in sorted order, number them
rowNumIdx := len(rows[0]) - 2
originalOrderIdx := len(rows[0]) - 1
var last sql.Row
var rowNum int
for _, row := range rows {
// every time we encounter a new partition, start the count over
isNew, err := isNewPartition(ctx, r.window.PartitionBy, last, row)
if err != nil {
return err
}
if isNew {
rowNum = 1
}
row[rowNumIdx] = rowNum
rowNum++
last = row
}
// And finally sort again by the original order
sort.SliceStable(rows, func(i, j int) bool {
return rows[i][originalOrderIdx].(int) < rows[j][originalOrderIdx].(int)
})
}
return nil
}
// EvalRow implements sql.WindowAggregation
func (r *RowNumber) EvalRow(i int, buffer sql.Row) (interface{}, error) {
rows := buffer[0].([]sql.Row)
return rows[i][len(rows[i])-2], nil
}