github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/colexec/execgen/cmd/execgen/agg_gen_util.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 package main 12 13 import ( 14 "fmt" 15 "io" 16 "strings" 17 18 "github.com/cockroachdb/cockroachdb-parser/pkg/sql/types" 19 ) 20 21 const ( 22 // aggKindTmplVar specifies the template "variable" that describes the kind 23 // of aggregator using an aggregate function. It is replaced with "Hash", 24 // "Ordered", or "Window" before executing the template. 25 aggKindTmplVar = "_AGGKIND" 26 hashAggKind = "Hash" 27 orderedAggKind = "Ordered" 28 windowAggKind = "Window" 29 ) 30 31 func registerAggGenerator(aggGen generator, filenameSuffix, dep string, genWindowVariant bool) { 32 aggGeneratorAdapter := func(aggKind string) generator { 33 return func(inputFileContents string, wr io.Writer) error { 34 inputFileContents = strings.ReplaceAll(inputFileContents, aggKindTmplVar, aggKind) 35 return aggGen(inputFileContents, wr) 36 } 37 } 38 aggKinds := []string{hashAggKind, orderedAggKind} 39 if genWindowVariant { 40 aggKinds = append(aggKinds, windowAggKind) 41 } 42 for _, aggKind := range aggKinds { 43 registerGenerator( 44 aggGeneratorAdapter(aggKind), 45 fmt.Sprintf("%s_%s", strings.ToLower(aggKind), filenameSuffix), 46 dep, 47 ) 48 } 49 } 50 51 // aggTmplInfoBase is a helper struct used in generating the code of many 52 // aggregates serving as a base for calling methods on (whenever 53 // lastArgWidthOverload isn't available). 54 type aggTmplInfoBase struct { 55 // canonicalTypeFamily is the canonical type family of the current aggregate 56 // object used by the aggregate function. 57 canonicalTypeFamily types.Family 58 } 59 60 // CopyVal is a function that should only be used in templates. 61 func (b *aggTmplInfoBase) CopyVal(dest, src string) string { 62 return copyVal(b.canonicalTypeFamily, dest, src) 63 } 64 65 // SetVariableSize is a function that should only be used in templates. See the 66 // comment on setVariableSize for more details. 67 func (b aggTmplInfoBase) SetVariableSize(target, value string) string { 68 return setVariableSize(b.canonicalTypeFamily, target, value) 69 } 70 71 // Remove unused warning. 72 var ( 73 a aggTmplInfoBase 74 _ = a.SetVariableSize 75 _ = a.CopyVal 76 )