-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathconcat_agg_tmpl.go
190 lines (171 loc) · 4.74 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
// 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/colmem"
)
func (a *concat_AGGKINDAgg) Init(groups []bool, vec coldata.Vec) {
// {{if eq "_AGGKIND" "Ordered"}}
a.groups = groups
// {{end}}
a.vec = vec
a.col = vec.Bytes()
a.nulls = vec.Nulls()
a.Reset()
}
func (a *concat_AGGKINDAgg) Reset() {
a.curIdx = 0
a.foundNonNullForCurrentGroup = false
a.curAgg = zeroBytesValue
a.nulls.UnsetNulls()
}
func (a *concat_AGGKINDAgg) CurrentOutputIndex() int {
return a.curIdx
}
func (a *concat_AGGKINDAgg) SetOutputIndex(idx int) {
a.curIdx = idx
}
func (a *concat_AGGKINDAgg) Compute(batch coldata.Batch, inputIdxs []uint32) {
inputLen := batch.Length()
vec, sel := batch.ColVec(int(inputIdxs[0])), batch.Selection()
col, nulls := vec.Bytes(), vec.Nulls()
a.allocator.PerformOperation(
[]coldata.Vec{a.vec},
func() {
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)
}
}
}
},
)
}
func (a *concat_AGGKINDAgg) Flush() {
a.allocator.PerformOperation(
[]coldata.Vec{a.vec}, func() {
if !a.foundNonNullForCurrentGroup {
a.nulls.SetNull(a.curIdx)
} else {
a.col.Set(a.curIdx, a.curAgg)
}
a.curIdx++
})
}
func (a *concat_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_AGGKINDAgg, a.allocSize)
for i := range a.aggFuncs {
a.aggFuncs[i].allocator = a.allocator
}
}
f := &a.aggFuncs[0]
a.aggFuncs = a.aggFuncs[1:]
return f
}
const sizeOfConcat_AGGKINDAgg = int64(unsafe.Sizeof(concat_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_AGGKINDAgg
}
type concat_AGGKINDAgg struct {
// alloc is the alloc used to create this aggregateFunc
// memory usage of output vector varies during aggregation, we
// need the allocator to monitor this change.
allocator *colmem.Allocator
// {{if eq "_AGGKIND" "Ordered"}}
groups []bool
// {{end}}
curIdx int
// curAgg holds the running total.
curAgg []byte
// col points to the output vector we are updating.
col *coldata.Bytes
// 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
}
// {{/*
func _ACCUMULATE_CONCAT(a *concat_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 {
a.col.Set(a.curIdx, a.curAgg)
}
a.curIdx++
a.curAgg = zeroBytesValue
// {{/*
// 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 {
a.curAgg = append(a.curAgg, col.Get(i)...)
a.foundNonNullForCurrentGroup = true
}
// {{end}}
// {{/*
} // */}}