-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathconcat_agg_tmpl.go
227 lines (202 loc) · 5.96 KB
/
concat_agg_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
// Copyright 2020 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 concat_agg.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 colexec
import (
"unsafe"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/sql/colexec/execgen"
"github.com/cockroachdb/cockroach/pkg/sql/colexecbase/colexecerror"
"github.com/cockroachdb/cockroach/pkg/sql/colmem"
)
// {{/*
// Declarations to make the template compile properly
// _ASSIGN_ADD is the template concat function for assigning the first input
// to concatenation of second input and third input
func _ASSIGN_CONCAT(_, _, _, _, _, _ string) {
colexecerror.InternalError(nonTemplatePanic)
}
// _AGG_VAL_MEMORY_USAGE is the template function for calculating memory
// usage of aggregate value
func _AGG_VAL_MEMORY_USAGE(_ string) int64 {
colexecerror.InternalError(nonTemplatePanic)
}
// */}}
// {{range .}}
func (a *concat_TYPE_AGGKINDAgg) Init(groups []bool, vec coldata.Vec) {
// {{if eq "_AGGKIND" "Ordered"}}
a.groups = groups
// {{end}}
a.vec = vec
a.col = vec._RET_TYPE()
a.nulls = vec.Nulls()
a.Reset()
}
func (a *concat_TYPE_AGGKINDAgg) Reset() {
a.curIdx = 0
a.foundNonNullForCurrentGroup = false
a.curAgg = zero_RET_TYPEValue
a.nulls.UnsetNulls()
}
func (a *concat_TYPE_AGGKINDAgg) CurrentOutputIndex() int {
return a.curIdx
}
func (a *concat_TYPE_AGGKINDAgg) SetOutputIndex(idx int) {
a.curIdx = idx
}
func (a *concat_TYPE_AGGKINDAgg) Compute(batch coldata.Batch, inputIdxs []uint32) {
inputLen := batch.Length()
vec, sel := batch.ColVec(int(inputIdxs[0])), batch.Selection()
col, nulls := vec._RET_TYPE(), vec.Nulls()
a.alloc.allocator.PerformOperation(
[]coldata.Vec{a.vec},
func() {
previousAggValMemoryUsage := _AGG_VAL_MEMORY_USAGE(a.curAgg)
if nulls.MaybeHasNulls() {
if sel != nil {
sel = sel[:inputLen]
for _, i := range sel {
_ACCUMULATE_CONCAT(a, nulls, i, true)
}
} else {
for i := 0; i < inputLen; i++ {
_ACCUMULATE_CONCAT(a, nulls, i, true)
}
}
} else {
if sel != nil {
sel = sel[:inputLen]
for _, i := range sel {
_ACCUMULATE_CONCAT(a, nulls, i, false)
}
} else {
for i := 0; i < inputLen; i++ {
_ACCUMULATE_CONCAT(a, nulls, i, false)
}
}
}
currentAggValMemoryUsage := _AGG_VAL_MEMORY_USAGE(a.curAgg)
a.alloc.allocator.AdjustMemoryUsage(currentAggValMemoryUsage - previousAggValMemoryUsage)
},
)
}
func (a *concat_TYPE_AGGKINDAgg) Flush() {
a.alloc.allocator.PerformOperation(
[]coldata.Vec{a.vec}, func() {
if !a.foundNonNullForCurrentGroup {
a.nulls.SetNull(a.curIdx)
} else {
execgen.SET(a.col, a.curIdx, a.curAgg)
}
a.alloc.allocator.AdjustMemoryUsage(-(_AGG_VAL_MEMORY_USAGE(a.curAgg)))
a.curAgg = zero_RET_TYPEValue
a.curIdx++
})
}
func (a *concat_TYPE_AGGKINDAgg) HandleEmptyInputScalar() {
a.nulls.SetNull(0)
}
func (a *concat_AGGKINDAggAlloc) newAggFunc() aggregateFunc {
if len(a.aggFuncs) == 0 {
a.allocator.AdjustMemoryUsage(sizeOfConcat_AGGKINDAgg * a.allocSize)
a.aggFuncs = make([]concat_TYPE_AGGKINDAgg, a.allocSize)
for i := range a.aggFuncs {
a.aggFuncs[i].alloc = a
}
}
f := &a.aggFuncs[0]
a.aggFuncs = a.aggFuncs[1:]
return f
}
const sizeOfConcat_AGGKINDAgg = int64(unsafe.Sizeof(concat_TYPE_AGGKINDAgg{}))
func newConcat_AGGKINDAggAlloc(allocator *colmem.Allocator, allocSize int64) aggregateFuncAlloc {
return &concat_AGGKINDAggAlloc{aggAllocBase: aggAllocBase{
allocator: allocator,
allocSize: allocSize,
}}
}
type concat_AGGKINDAggAlloc struct {
aggAllocBase
aggFuncs []concat_TYPE_AGGKINDAgg
}
type concat_TYPE_AGGKINDAgg struct {
// alloc is the alloc used to create this aggregateFunc
// memory usage of curAgg varies during aggregation, we
// need the allocator to monitor this change
alloc *concat_AGGKINDAggAlloc
// {{if eq "_AGGKIND" "Ordered"}}
groups []bool
// {{end}}
curIdx int
// curAgg holds the running total
curAgg _RET_GOTYPE
// col points to the output vector we are updating
col _RET_GOTYPESLICE
// vec is the same as col before conversion from coldata.Vec
vec coldata.Vec
// nulls points to the output null vector that we are updating
nulls *coldata.Nulls
// foundNonNullForCurrentGroup tracks if we have seen any non-null values
// for the group that is currently being aggregated.
foundNonNullForCurrentGroup bool
}
// {{end}}
// {{/*
func _ACCUMULATE_CONCAT(
a *sum_SUMKIND_TYPE_AGGKINDAgg, nulls *coldata.Nulls, i int, _HAS_NULLS bool,
) { // */}}
// {{define "accumulateConcat" -}}
// {{if eq "_AGGKIND" "Ordered"}}
if a.groups[i] {
// If we encounter a new group, and we haven't found any non-nulls for the
// current group, the output for this group should be null.
if !a.foundNonNullForCurrentGroup {
a.nulls.SetNull(a.curIdx)
} else {
// {{with .Global}}
execgen.SET(a.col, a.curIdx, a.curAgg)
// {{end}}
}
a.curIdx++
// {{with .Global}}
a.curAgg = zero_RET_TYPEValue
// {{end}}
// {{/*
// We only need to reset this flag if there are nulls. If there are no
// nulls, this will be updated unconditionally below.
// */}}
// {{if .HasNulls}}
a.foundNonNullForCurrentGroup = false
// {{end}}
}
// {{end}}
var isNull bool
// {{if .HasNulls}}
isNull = nulls.NullAt(i)
// {{else}}
isNull = false
// {{end}}
if !isNull {
// {{with .Global}}
valueToConcat := execgen.UNSAFEGET(col, i)
// {{end}}
_ASSIGN_CONCAT(a.curAgg, a.curAgg, valueToConcat, _, _, col)
a.foundNonNullForCurrentGroup = true
}
// {{end}}
// {{/*
} // */}}