github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/colexec/bool_and_or_agg_tmpl.go (about) 1 // Copyright 2020 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 // {{/* 12 // +build execgen_template 13 // 14 // This file is the execgen template for bool_and_or_agg.eg.go. It's formatted in a 15 // special way, so it's both valid Go and a valid text/template input. This 16 // permits editing this file with editor support. 17 // 18 // */}} 19 20 package colexec 21 22 import ( 23 "unsafe" 24 25 "github.com/cockroachdb/cockroach/pkg/col/coldata" 26 "github.com/cockroachdb/cockroach/pkg/sql/colexecbase/colexecerror" 27 "github.com/cockroachdb/cockroach/pkg/sql/colmem" 28 ) 29 30 // Remove unused warning. 31 var _ = colexecerror.InternalError 32 33 // {{/* 34 35 // _ASSIGN_BOOL_OP is the template boolean operation function for assigning the 36 // first input to the result of a boolean operation of the second and the third 37 // inputs. 38 func _ASSIGN_BOOL_OP(_, _, _ string) { 39 colexecerror.InternalError("") 40 } 41 42 // */}} 43 44 // {{range .}} 45 46 func newBool_OP_TYPEAggAlloc(allocator *colmem.Allocator, allocSize int64) aggregateFuncAlloc { 47 return &bool_OP_TYPEAggAlloc{allocator: allocator, allocSize: allocSize} 48 } 49 50 type bool_OP_TYPEAgg struct { 51 sawNonNull bool 52 53 groups []bool 54 vec []bool 55 56 nulls *coldata.Nulls 57 curIdx int 58 curAgg bool 59 } 60 61 var _ aggregateFunc = &bool_OP_TYPEAgg{} 62 63 const sizeOfBool_OP_TYPEAgg = int64(unsafe.Sizeof(bool_OP_TYPEAgg{})) 64 65 func (b *bool_OP_TYPEAgg) Init(groups []bool, vec coldata.Vec) { 66 b.groups = groups 67 b.vec = vec.Bool() 68 b.nulls = vec.Nulls() 69 b.Reset() 70 } 71 72 func (b *bool_OP_TYPEAgg) Reset() { 73 b.curIdx = -1 74 b.nulls.UnsetNulls() 75 // _DEFAULT_VAL indicates whether we are doing an AND aggregate or OR aggregate. 76 // For bool_and the _DEFAULT_VAL is true and for bool_or the _DEFAULT_VAL is false. 77 b.curAgg = _DEFAULT_VAL 78 } 79 80 func (b *bool_OP_TYPEAgg) CurrentOutputIndex() int { 81 return b.curIdx 82 } 83 84 func (b *bool_OP_TYPEAgg) SetOutputIndex(idx int) { 85 if b.curIdx != -1 { 86 b.curIdx = idx 87 b.nulls.UnsetNullsAfter(idx) 88 } 89 } 90 91 func (b *bool_OP_TYPEAgg) Compute(batch coldata.Batch, inputIdxs []uint32) { 92 inputLen := batch.Length() 93 vec, sel := batch.ColVec(int(inputIdxs[0])), batch.Selection() 94 col, nulls := vec.Bool(), vec.Nulls() 95 if sel != nil { 96 sel = sel[:inputLen] 97 for _, i := range sel { 98 _ACCUMULATE_BOOLEAN(b, nulls, i) 99 } 100 } else { 101 col = col[:inputLen] 102 for i := range col { 103 _ACCUMULATE_BOOLEAN(b, nulls, i) 104 } 105 } 106 } 107 108 func (b *bool_OP_TYPEAgg) Flush() { 109 if !b.sawNonNull { 110 b.nulls.SetNull(b.curIdx) 111 } else { 112 b.vec[b.curIdx] = b.curAgg 113 } 114 b.curIdx++ 115 } 116 117 func (b *bool_OP_TYPEAgg) HandleEmptyInputScalar() { 118 b.nulls.SetNull(0) 119 } 120 121 type bool_OP_TYPEAggAlloc struct { 122 allocator *colmem.Allocator 123 allocSize int64 124 aggFuncs []bool_OP_TYPEAgg 125 } 126 127 var _ aggregateFuncAlloc = &bool_OP_TYPEAggAlloc{} 128 129 func (a *bool_OP_TYPEAggAlloc) newAggFunc() aggregateFunc { 130 if len(a.aggFuncs) == 0 { 131 a.allocator.AdjustMemoryUsage(sizeOfBool_OP_TYPEAgg * a.allocSize) 132 a.aggFuncs = make([]bool_OP_TYPEAgg, a.allocSize) 133 } 134 f := &a.aggFuncs[0] 135 a.aggFuncs = a.aggFuncs[1:] 136 return f 137 } 138 139 // {{end}} 140 141 // {{/* 142 // _ACCUMULATE_BOOLEAN aggregates the boolean value at index i into the boolean aggregate. 143 func _ACCUMULATE_BOOLEAN(b *bool_OP_TYPEAgg, nulls *coldata.Nulls, i int) { // */}} 144 // {{define "accumulateBoolean" -}} 145 if b.groups[i] { 146 if b.curIdx >= 0 { 147 if !b.sawNonNull { 148 b.nulls.SetNull(b.curIdx) 149 } else { 150 b.vec[b.curIdx] = b.curAgg 151 } 152 } 153 b.curIdx++ 154 // {{with .Global}} 155 b.curAgg = _DEFAULT_VAL 156 // {{end}} 157 b.sawNonNull = false 158 } 159 isNull := nulls.NullAt(i) 160 if !isNull { 161 // {{with .Global}} 162 _ASSIGN_BOOL_OP(b.curAgg, b.curAgg, col[i]) 163 // {{end}} 164 b.sawNonNull = true 165 } 166 167 // {{end}} 168 169 // {{/* 170 } // */}}