-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
lead_lag_tmpl.go
297 lines (267 loc) · 7.86 KB
/
lead_lag_tmpl.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
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
// {{/*
// +build execgen_template
//
// This file is the execgen template for lag.eg.go and lead.eg.go. It's
// formatted in a special way, so it's both valid Go and a valid text/template
// input. This permits editing this file with editor support.
//
// */}}
package colexecwindow
import (
"context"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/col/typeconv"
"github.com/cockroachdb/cockroach/pkg/sql/colcontainer"
"github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecutils"
"github.com/cockroachdb/cockroach/pkg/sql/colexecop"
"github.com/cockroachdb/cockroach/pkg/sql/colmem"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/mon"
"github.com/cockroachdb/errors"
"github.com/marusama/semaphore"
)
// {{/*
// Declarations to make the template compile properly.
// _CANONICAL_TYPE_FAMILY is the template variable.
const _CANONICAL_TYPE_FAMILY = types.UnknownFamily
// _TYPE_WIDTH is the template variable.
const _TYPE_WIDTH = 0
// */}}
// New_UPPERCASE_NAMEOperator creates a new Operator that computes window
// function _OP_NAME. outputColIdx specifies in which coldata.Vec the operator
// should put its output (if there is no such column, a new column is appended).
func New_UPPERCASE_NAMEOperator(
unlimitedAllocator *colmem.Allocator,
bufferAllocator *colmem.Allocator,
memoryLimit int64,
diskQueueCfg colcontainer.DiskQueueCfg,
fdSemaphore semaphore.Semaphore,
diskAcc *mon.BoundAccount,
input colexecop.Operator,
inputTypes []*types.T,
outputColIdx int,
partitionColIdx int,
argIdx int,
offsetIdx int,
defaultIdx int,
) (colexecop.Operator, error) {
argType := inputTypes[argIdx]
// Allow the direct-access buffer 25% of the available memory. The rest will
// be given to the bufferedWindowOp queue.
bufferMemLimit := int64(float64(memoryLimit) * 0.05)
buffer := colexecutils.NewSpillingBuffer(
bufferAllocator, bufferMemLimit, diskQueueCfg, fdSemaphore, []*types.T{argType}, diskAcc, argIdx)
base := _OP_NAMEBase{
buffer: buffer,
outputColIdx: outputColIdx,
partitionColIdx: partitionColIdx,
argIdx: argIdx,
offsetIdx: offsetIdx,
defaultIdx: defaultIdx,
}
switch typeconv.TypeFamilyToCanonicalTypeFamily(argType.Family()) {
// {{range .}}
case _CANONICAL_TYPE_FAMILY:
switch argType.Width() {
// {{range .WidthOverloads}}
case _TYPE_WIDTH:
return newBufferedWindowOperator(
&_OP_NAME_TYPEWindow{_OP_NAMEBase: base}, unlimitedAllocator, memoryLimit-bufferMemLimit,
diskQueueCfg, fdSemaphore, diskAcc, input, inputTypes, argType, outputColIdx,
), nil
// {{end}}
}
// {{end}}
}
return nil, errors.Errorf("unsupported _OP_NAME window operator type %s", argType.Name())
}
// _OP_NAMEBase extracts common fields and methods of the _OP_NAME windower
// variations.
type _OP_NAMEBase struct {
colexecop.InitHelper
colexecop.CloserHelper
_OP_NAMEComputeFields
buffer *colexecutils.SpillingBuffer
outputColIdx int
partitionColIdx int
argIdx int
offsetIdx int
defaultIdx int
}
// _OP_NAMEComputeFields extracts the fields that are used to calculate _OP_NAME
// output values.
type _OP_NAMEComputeFields struct {
partitionSize int
idx int
}
// {{range .}}
// {{range .WidthOverloads}}
type _OP_NAME_TYPEWindow struct {
_OP_NAMEBase
}
var _ bufferedWindower = &_OP_NAME_TYPEWindow{}
func (w *_OP_NAME_TYPEWindow) seekNextPartition(
batch coldata.Batch, startIdx int, isPartitionStart bool,
) (nextPartitionIdx int) {
n := batch.Length()
if w.partitionColIdx == -1 {
// There is only one partition, so it includes the entirety of this batch.
w.partitionSize += n
nextPartitionIdx = n
} else {
i := startIdx
partitionCol := batch.ColVec(w.partitionColIdx).Bool()
_ = partitionCol[n-1]
_ = partitionCol[i]
// Find the location of the start of the next partition (and the end of the
// current one).
for ; i < n; i++ {
//gcassert:bce
if partitionCol[i] {
// Don't break for the start of the current partition.
if !isPartitionStart || i != startIdx {
break
}
}
}
w.partitionSize += i - startIdx
nextPartitionIdx = i
}
// Add all tuples from the argument column that fall within the current
// partition to the buffer so that they can be accessed later.
if startIdx < nextPartitionIdx {
w.buffer.AppendTuples(w.Ctx, batch, startIdx, nextPartitionIdx)
}
return nextPartitionIdx
}
func (w *_OP_NAME_TYPEWindow) processBatch(batch coldata.Batch, startIdx, endIdx int) {
if startIdx >= endIdx {
// No processing needs to be done for this portion of the current partition.
return
}
leadLagVec := batch.ColVec(w.outputColIdx)
leadLagCol := leadLagVec.TemplateType()
leadLagNulls := leadLagVec.Nulls()
// {{if .Sliceable}}
_ = leadLagCol.Get(startIdx)
_ = leadLagCol.Get(endIdx-1)
// {{end}}
offsetVec := batch.ColVec(w.offsetIdx)
offsetCol := offsetVec.Int64()
offsetNulls := offsetVec.Nulls()
_ = offsetCol[startIdx]
_ = offsetCol[endIdx-1]
defaultVec := batch.ColVec(w.defaultIdx)
defaultCol := defaultVec.TemplateType()
defaultNulls := defaultVec.Nulls()
// {{if .Sliceable}}
_ = defaultCol.Get(startIdx)
_ = defaultCol.Get(endIdx-1)
// {{end}}
if offsetNulls.MaybeHasNulls() {
if defaultNulls.MaybeHasNulls() {
_PROCESS_BATCH(true, true)
return
}
_PROCESS_BATCH(true, false)
return
}
if defaultNulls.MaybeHasNulls() {
_PROCESS_BATCH(false, true)
return
}
_PROCESS_BATCH(false, false)
}
// {{end}}
// {{end}}
func (b *_OP_NAMEBase) transitionToProcessing() {
}
func (b *_OP_NAMEBase) startNewPartition() {
b.idx = 0
b.partitionSize = 0
b.buffer.Reset(b.Ctx)
}
func (b *_OP_NAMEBase) Init(ctx context.Context) {
if !b.InitHelper.Init(ctx) {
return
}
}
func (b *_OP_NAMEBase) Close() error {
if !b.CloserHelper.Close() {
return nil
}
return b.buffer.Close(b.EnsureCtx())
}
// {{/*
// _PROCESS_BATCH is a code fragment that iterates over the given batch and
// sets the lag or lead output value.
func _PROCESS_BATCH(_OFFSET_HAS_NULLS bool, _DEFAULT_HAS_NULLS bool) { // */}}
// {{define "processBatchTmpl" -}}
for i := startIdx; i < endIdx; i++ {
// {{if .OffsetHasNulls}}
if offsetNulls.NullAt(i) {
// When the offset is null, the output value is also null.
leadLagNulls.SetNull(i)
w.idx++
continue
}
// {{end}}
// {{if eq "_OP_NAME" "lag"}}
requestedIdx := w.idx - int(offsetCol[i])
// {{else}}
requestedIdx := w.idx + int(offsetCol[i])
// {{end}}
w.idx++
if requestedIdx < 0 || requestedIdx >= w.partitionSize {
// The offset is out of range, so set the output value to the default.
// {{if .DefaultHasNulls}}
if defaultNulls.NullAt(i) {
leadLagNulls.SetNull(i)
continue
}
// {{end}}
// {{if .IsBytesLike}}
leadLagCol.CopySlice(defaultCol, i, i, i+1)
// {{else}}
// {{with .Global}}
val := defaultCol.Get(i)
execgen.SET(leadLagCol, i, val)
// {{end}}
// {{end}}
continue
}
b, idx := w.buffer.GetBatchWithTuple(w.Ctx, requestedIdx)
vec := b.ColVec(0)
if vec.Nulls().NullAt(idx) {
leadLagNulls.SetNull(i)
continue
}
// {{with .Global}}
col := vec.TemplateType()
// {{end}}
// {{if $.IsBytesLike}}
// We have to use CopySlice here because the column already has a length of
// n elements, and Set cannot set values before the last one.
leadLagCol.CopySlice(col, i, idx, idx+1)
// {{else}}
val := col.Get(idx)
// {{if .Sliceable}}
//gcassert:bce
// {{end}}
// {{with .Global}}
execgen.SET(leadLagCol, i, val)
// {{end}}
// {{end}}
}
// {{end}}
// {{/*
} // */}}