github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/agg/count.go (about) 1 // Copyright 2021 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 agg 16 17 import ( 18 "github.com/matrixorigin/matrixone/pkg/container/types" 19 ) 20 21 type Decimal128AndString interface { 22 types.Decimal | []byte | bool | types.Uuid 23 } 24 25 type Count[T1 types.OrderedT | Decimal128AndString] struct { 26 // IsStar is true: count(*) 27 IsStar bool 28 } 29 30 func CountReturnType(_ []types.Type) types.Type { 31 return types.New(types.T_int64, 0, 0, 0) 32 } 33 34 func NewCount[T1 types.OrderedT | Decimal128AndString](isStar bool) *Count[T1] { 35 return &Count[T1]{IsStar: isStar} 36 } 37 38 func (c *Count[T1]) Grows(_ int) { 39 } 40 41 func (c *Count[T1]) Eval(vs []int64) []int64 { 42 return vs 43 } 44 45 func (c *Count[T1]) Merge(_, _ int64, x, y int64, _ bool, _ bool, _ any) (int64, bool) { 46 return x + y, false 47 } 48 49 func (c *Count[T1]) Fill(_ int64, _ T1, v int64, z int64, _ bool, hasNull bool) (int64, bool) { 50 if hasNull { 51 if !c.IsStar { 52 return v, false 53 } else { 54 return v + z, false 55 } 56 } 57 return v + z, false 58 } 59 60 func (c *Count[T1]) MarshalBinary() ([]byte, error) { 61 return types.EncodeBool(&c.IsStar), nil 62 } 63 64 func (c *Count[T1]) UnmarshalBinary(data []byte) error { 65 // avoid resulting errors caused by morpc overusing memory 66 copyData := make([]byte, len(data)) 67 copy(copyData, data) 68 c.IsStar = types.DecodeBool(copyData) 69 return nil 70 }