github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/agg/sum.go (about)

     1  // Copyright 2022 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/common/moerr"
    19  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/vectorize/sum"
    22  )
    23  
    24  // if input type is int8/int16/int32/int64, the return type is int64
    25  // if input type is uint8/uint16/uint32/uint64, the return type is uint64
    26  // f input type is float32/float64, the return type is float64
    27  type ReturnTyp interface {
    28  	uint64 | int64 | float64
    29  }
    30  
    31  type Sum[T1 Numeric, T2 ReturnTyp] struct {
    32  }
    33  
    34  type Decimal64Sum struct {
    35  }
    36  
    37  type Decimal128Sum struct {
    38  }
    39  
    40  func SumReturnType(typs []types.Type) types.Type {
    41  	switch typs[0].Oid {
    42  	case types.T_float32, types.T_float64:
    43  		return types.New(types.T_float64, 0, 0, 0)
    44  	case types.T_int8, types.T_int16, types.T_int32, types.T_int64:
    45  		return types.New(types.T_int64, 0, 0, 0)
    46  	case types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64:
    47  		return types.New(types.T_uint64, 0, 0, 0)
    48  	case types.T_decimal64:
    49  		return typs[0]
    50  	case types.T_decimal128:
    51  		return typs[0]
    52  	}
    53  	panic(moerr.NewInternalErrorNoCtx("unsupport type '%v' for sum", typs[0]))
    54  }
    55  
    56  func NewSum[T1 Numeric, T2 ReturnTyp]() *Sum[T1, T2] {
    57  	return &Sum[T1, T2]{}
    58  }
    59  
    60  func (s *Sum[T1, T2]) Grows(_ int) {
    61  }
    62  
    63  func (s *Sum[T1, T2]) Eval(vs []T2) []T2 {
    64  	return vs
    65  }
    66  
    67  func (s *Sum[T1, T2]) Fill(_ int64, value T1, ov T2, z int64, isEmpty bool, isNull bool) (T2, bool) {
    68  	if !isNull {
    69  		return ov + T2(value)*T2(z), false
    70  	}
    71  	return ov, isEmpty
    72  }
    73  
    74  func (s *Sum[T1, T2]) Merge(_ int64, _ int64, x T2, y T2, xEmpty bool, yEmpty bool, _ any) (T2, bool) {
    75  	if !yEmpty {
    76  		if !xEmpty {
    77  			return x + y, false
    78  		}
    79  		return y, false
    80  	}
    81  	return x, xEmpty
    82  
    83  }
    84  
    85  func (s *Sum[T1, T2]) MarshalBinary() ([]byte, error) {
    86  	return nil, nil
    87  }
    88  
    89  func (s *Sum[T1, T2]) UnmarshalBinary(data []byte) error {
    90  	return nil
    91  }
    92  
    93  func NewD64Sum() *Decimal64Sum {
    94  	return &Decimal64Sum{}
    95  }
    96  
    97  func (s *Decimal64Sum) Grows(_ int) {
    98  }
    99  
   100  func (s *Decimal64Sum) Eval(vs []types.Decimal64) []types.Decimal64 {
   101  	return vs
   102  }
   103  
   104  func (s *Decimal64Sum) Fill(_ int64, value types.Decimal64, ov types.Decimal64, z int64, isEmpty bool, isNull bool) (types.Decimal64, bool) {
   105  	if !isNull {
   106  		return ov.Add(value.MulInt64(z)), false
   107  	}
   108  	return ov, isEmpty
   109  }
   110  
   111  func (s *Decimal64Sum) Merge(_ int64, _ int64, x types.Decimal64, y types.Decimal64, xEmpty bool, yEmpty bool, _ any) (types.Decimal64, bool) {
   112  	if !yEmpty {
   113  		if !xEmpty {
   114  			return x.Add(y), false
   115  		}
   116  		return y, false
   117  	}
   118  	return x, xEmpty
   119  }
   120  
   121  func (s *Decimal64Sum) BatchFill(rs, vs any, start, count int64, vps []uint64, zs []int64, nsp *nulls.Nulls) error {
   122  	return sum.Decimal64Sum(rs.([]types.Decimal64), vs.([]types.Decimal64), start, count, vps, zs, nsp)
   123  }
   124  
   125  func (s *Decimal64Sum) MarshalBinary() ([]byte, error) {
   126  	return nil, nil
   127  }
   128  
   129  func (s *Decimal64Sum) UnmarshalBinary(data []byte) error {
   130  	return nil
   131  }
   132  
   133  func NewD128Sum() *Decimal128Sum {
   134  	return &Decimal128Sum{}
   135  }
   136  
   137  func (s *Decimal128Sum) Grows(_ int) {
   138  }
   139  
   140  func (s *Decimal128Sum) Eval(vs []types.Decimal128) []types.Decimal128 {
   141  	return vs
   142  }
   143  
   144  func (s *Decimal128Sum) Fill(_ int64, value types.Decimal128, ov types.Decimal128, z int64, isEmpty bool, isNull bool) (types.Decimal128, bool) {
   145  	if !isNull {
   146  		return ov.Add(value.MulInt64(z)), false
   147  	}
   148  	return ov, isEmpty
   149  }
   150  
   151  func (s *Decimal128Sum) Merge(_ int64, _ int64, x types.Decimal128, y types.Decimal128, xEmpty bool, yEmpty bool, _ any) (types.Decimal128, bool) {
   152  	if !yEmpty {
   153  		if !xEmpty {
   154  			return x.Add(y), false
   155  		}
   156  		return y, false
   157  	}
   158  	return x, xEmpty
   159  }
   160  
   161  func (s *Decimal128Sum) BatchFill(rs, vs any, start, count int64, vps []uint64, zs []int64, nsp *nulls.Nulls) error {
   162  	return sum.Decimal128Sum(rs.([]types.Decimal128), vs.([]types.Decimal128), start, count, vps, zs, nsp)
   163  }
   164  
   165  func (s *Decimal128Sum) MarshalBinary() ([]byte, error) {
   166  	return nil, nil
   167  }
   168  
   169  func (s *Decimal128Sum) UnmarshalBinary(data []byte) error {
   170  	return nil
   171  }