github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/aggexec/aggContext.go (about) 1 // Copyright 2024 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package aggexec 16 17 import "github.com/matrixorigin/matrixone/pkg/container/types" 18 19 // todo: sca. 20 //type AggContext struct { 21 // hasCommonContext bool 22 // hasGroupContext bool 23 // commonContext AggCommonExecContext 24 // groupContext []AggGroupExecContext 25 //} 26 // 27 //func (a *AggContext) setCommonContext(c AggCommonExecContext) { 28 // if c == nil { 29 // return 30 // } 31 // a.hasCommonContext = true 32 // a.commonContext = c 33 //} 34 // 35 //func (a *AggContext) preExtend(n int) { 36 // if !a.hasGroupContext { 37 // return 38 // } 39 // if n <= cap(a.groupContext) { 40 // return 41 // } 42 // 43 // oldLen := len(a.groupContext) 44 // a.groupContext = append(a.groupContext, make([]AggGroupExecContext, n-cap(a.groupContext))...) 45 // a.groupContext = a.groupContext[:oldLen] 46 //} 47 // 48 //func (a *AggContext) growsGroupContext(initGroup func() AggGroupExecContext, n int) { 49 // if !a.hasGroupContext { 50 // return 51 // } 52 // oldLen, newLen := len(a.groupContext), len(a.groupContext)+n 53 // if newLen > cap(a.groupContext) { 54 // a.groupContext = append(a.groupContext, make([]AggGroupExecContext, n)...) 55 // for i := oldLen; i < newLen; i++ { 56 // a.groupContext[i] = initGroup() 57 // } 58 // 59 // } else { 60 // a.groupContext = a.groupContext[:newLen] 61 // for i := oldLen; i < newLen; i++ { 62 // a.groupContext[i] = initGroup() 63 // } 64 // } 65 //} 66 // 67 //func (a *AggContext) getCommonContext() AggCommonExecContext { 68 // return a.commonContext 69 //} 70 // 71 //func (a *AggContext) getGroupContext(i int) AggGroupExecContext { 72 // if a.hasGroupContext { 73 // return a.groupContext[i] 74 // } 75 // return nil 76 //} 77 78 // AggCommonExecContext stores the common context for all the groups. 79 // like the type scale, timezone and so on. 80 type AggCommonExecContext interface { 81 AggCanMarshal 82 } 83 84 // AggGroupExecContext store the content of each group individually. 85 // like the row-count has been filled, the sum of the values and so on. 86 type AggGroupExecContext interface { 87 AggCanMarshal 88 } 89 90 /* 91 prepared context structures for agg. 92 93 EmptyContextOfSingleAggRetFixed and EmptyContextOfSingleAggRetBytes are used for aggregation 94 which does not need to store any context. 95 96 ContextWithEmptyFlagOfSingleAggRetFixed and ContextWithEmptyFlagOfSingleAggRetBytes are used for aggregation 97 which only needs to store a flag to indicate whether it is empty. 98 */ 99 100 type EmptyContextOfSingleAggRetFixed[T types.FixedSizeTExceptStrType] struct{} 101 102 func (a EmptyContextOfSingleAggRetFixed[T]) Marshal() []byte { return nil } 103 func (a EmptyContextOfSingleAggRetFixed[T]) Unmarshal([]byte) {} 104 func GenerateEmptyContextFromFixedToFixed[from, to types.FixedSizeTExceptStrType]() SingleAggFromFixedRetFixed[from, to] { 105 return EmptyContextOfSingleAggRetFixed[to]{} 106 } 107 func GenerateEmptyContextFromVarToFixed[to types.FixedSizeTExceptStrType]() SingleAggFromVarRetFixed[to] { 108 return EmptyContextOfSingleAggRetFixed[to]{} 109 } 110 111 type EmptyContextOfSingleAggRetBytes struct{} 112 113 func (a EmptyContextOfSingleAggRetBytes) Marshal() []byte { return nil } 114 func (a EmptyContextOfSingleAggRetBytes) Unmarshal([]byte) {} 115 func GenerateEmptyContextFromFixedToVar[from types.FixedSizeTExceptStrType]() SingleAggFromFixedRetVar[from] { 116 return EmptyContextOfSingleAggRetBytes{} 117 } 118 func GenerateEmptyContextFromVarToVar() SingleAggFromVarRetVar { 119 return EmptyContextOfSingleAggRetBytes{} 120 } 121 122 type ContextWithEmptyFlagOfSingleAggRetFixed[T types.FixedSizeTExceptStrType] struct { 123 IsEmpty bool 124 } 125 126 func (a *ContextWithEmptyFlagOfSingleAggRetFixed[T]) Marshal() []byte { 127 return types.EncodeBool(&a.IsEmpty) 128 } 129 func (a *ContextWithEmptyFlagOfSingleAggRetFixed[T]) Unmarshal(data []byte) { 130 a.IsEmpty = types.DecodeBool(data) 131 } 132 func GenerateFlagContextFromFixedToFixed[from, to types.FixedSizeTExceptStrType]() SingleAggFromFixedRetFixed[from, to] { 133 return &ContextWithEmptyFlagOfSingleAggRetFixed[to]{} 134 } 135 func InitFlagContextFromFixedToFixed[from, to types.FixedSizeTExceptStrType](exec SingleAggFromFixedRetFixed[from, to], setter AggSetter[to], arg, ret types.Type) error { 136 a := exec.(*ContextWithEmptyFlagOfSingleAggRetFixed[to]) 137 a.IsEmpty = true 138 return nil 139 } 140 141 func GenerateFlagContextFromVarToFixed[to types.FixedSizeTExceptStrType]() SingleAggFromVarRetFixed[to] { 142 return &ContextWithEmptyFlagOfSingleAggRetFixed[to]{} 143 } 144 145 type ContextWithEmptyFlagOfSingleAggRetBytes struct { 146 IsEmpty bool 147 } 148 149 func (a *ContextWithEmptyFlagOfSingleAggRetBytes) Marshal() []byte { 150 return types.EncodeBool(&a.IsEmpty) 151 } 152 func (a *ContextWithEmptyFlagOfSingleAggRetBytes) Unmarshal(data []byte) { 153 a.IsEmpty = types.DecodeBool(data) 154 } 155 func GenerateFlagContextFromFixedToVar[from types.FixedSizeTExceptStrType]() SingleAggFromFixedRetVar[from] { 156 return &ContextWithEmptyFlagOfSingleAggRetBytes{} 157 } 158 func GenerateFlagContextFromVarToVar() SingleAggFromVarRetVar { 159 return &ContextWithEmptyFlagOfSingleAggRetBytes{} 160 } 161 func InitFlagContextFromVarToVar(exec SingleAggFromVarRetVar, setter AggBytesSetter, arg, ret types.Type) error { 162 a := exec.(*ContextWithEmptyFlagOfSingleAggRetBytes) 163 a.IsEmpty = true 164 return nil 165 }