github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/agg/avg.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/container/nulls"
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/matrixorigin/matrixone/pkg/vectorize/sum"
    21  )
    22  
    23  type Numeric interface {
    24  	types.Ints | types.UInts | types.Floats
    25  }
    26  
    27  type Avg[T Numeric] struct {
    28  	Cnts []int64
    29  }
    30  
    31  type Decimal64Avg struct {
    32  	Cnts []int64
    33  }
    34  
    35  type Decimal128Avg struct {
    36  	Cnts []int64
    37  }
    38  
    39  func AvgReturnType(typs []types.Type) types.Type {
    40  	switch typs[0].Oid {
    41  	case types.T_decimal64:
    42  		return types.New(types.T_decimal128, 0, typs[0].Scale, typs[0].Precision)
    43  	case types.T_decimal128:
    44  		return types.New(types.T_decimal128, 0, typs[0].Scale, typs[0].Precision)
    45  	case types.T_float32, types.T_float64:
    46  		return types.New(types.T_float64, 0, 0, 0)
    47  	case types.T_int8, types.T_int16, types.T_int32, types.T_int64:
    48  		return types.New(types.T_float64, 0, 0, 0)
    49  	case types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64:
    50  		return types.New(types.T_float64, 0, 0, 0)
    51  	default:
    52  		return types.Type{}
    53  	}
    54  }
    55  
    56  func NewAvg[T Numeric]() *Avg[T] {
    57  	return &Avg[T]{}
    58  }
    59  
    60  func (a *Avg[T]) Grows(cnt int) {
    61  	for i := 0; i < cnt; i++ {
    62  		a.Cnts = append(a.Cnts, 0)
    63  	}
    64  }
    65  
    66  func (a *Avg[T]) Eval(vs []float64) []float64 {
    67  	for i := range vs {
    68  		if a.Cnts[i] == 0 {
    69  			continue
    70  		}
    71  		vs[i] = vs[i] / float64(a.Cnts[i])
    72  	}
    73  	return vs
    74  }
    75  
    76  func (a *Avg[T]) Fill(i int64, value T, ov float64, z int64, isEmpty bool, isNull bool) (float64, bool) {
    77  	if !isNull {
    78  		a.Cnts[i] += z
    79  		return ov + float64(value)*float64(z), false
    80  	}
    81  	return ov, isEmpty
    82  }
    83  
    84  func (a *Avg[T]) Merge(xIndex int64, yIndex int64, x float64, y float64, xEmpty bool, yEmpty bool, yAvg any) (float64, bool) {
    85  	if !yEmpty {
    86  		ya := yAvg.(*Avg[T])
    87  		a.Cnts[xIndex] += ya.Cnts[yIndex]
    88  		if !xEmpty {
    89  			return x + y, false
    90  		}
    91  		return y, false
    92  	}
    93  
    94  	return x, xEmpty
    95  }
    96  
    97  func (a *Avg[T]) MarshalBinary() ([]byte, error) {
    98  	return types.EncodeSlice(a.Cnts), nil
    99  }
   100  
   101  func (a *Avg[T]) UnmarshalBinary(data []byte) error {
   102  	// avoid resulting errors caused by morpc overusing memory
   103  	copyData := make([]byte, len(data))
   104  	copy(copyData, data)
   105  	a.Cnts = types.DecodeSlice[int64](copyData)
   106  	return nil
   107  }
   108  
   109  func NewD64Avg() *Decimal64Avg {
   110  	return &Decimal64Avg{}
   111  }
   112  
   113  func (a *Decimal64Avg) Grows(cnt int) {
   114  	for i := 0; i < cnt; i++ {
   115  		a.Cnts = append(a.Cnts, 0)
   116  	}
   117  }
   118  
   119  func (a *Decimal64Avg) Eval(vs []types.Decimal128) []types.Decimal128 {
   120  	for i := range vs {
   121  		if a.Cnts[i] == 0 {
   122  			continue
   123  		}
   124  		vs[i] = vs[i].DivInt64(a.Cnts[i])
   125  	}
   126  	return vs
   127  }
   128  
   129  func (a *Decimal64Avg) Fill(i int64, value types.Decimal64, ov types.Decimal128, z int64, isEmpty bool, isNull bool) (types.Decimal128, bool) {
   130  	if !isNull {
   131  		a.Cnts[i] += z
   132  		tmp64 := value.MulInt64(z)
   133  		return ov.Add(types.Decimal128_FromDecimal64(tmp64)), false
   134  	}
   135  	return ov, isEmpty
   136  }
   137  
   138  func (a *Decimal64Avg) Merge(xIndex int64, yIndex int64, x types.Decimal128, y types.Decimal128, xEmpty bool, yEmpty bool, yAvg any) (types.Decimal128, bool) {
   139  	if !yEmpty {
   140  		ya := yAvg.(*Decimal64Avg)
   141  		a.Cnts[xIndex] += ya.Cnts[yIndex]
   142  		if !xEmpty {
   143  			return x.Add(y), false
   144  		}
   145  		return y, false
   146  	}
   147  
   148  	return x, xEmpty
   149  }
   150  
   151  func (a *Decimal64Avg) BatchFill(rs, vs any, start, count int64, vps []uint64, zs []int64, nsp *nulls.Nulls) error {
   152  	if err := sum.Decimal64Sum128(rs.([]types.Decimal128), vs.([]types.Decimal64), start, count, vps, zs, nsp); err != nil {
   153  		return err
   154  	}
   155  	for i := int64(0); i < count; i++ {
   156  		if nsp.Contains(uint64(i + start)) {
   157  			continue
   158  		}
   159  		if vps[i] == 0 {
   160  			continue
   161  		}
   162  		j := vps[i] - 1
   163  		a.Cnts[j] += zs[i+start]
   164  	}
   165  	return nil
   166  }
   167  
   168  func (a *Decimal64Avg) MarshalBinary() ([]byte, error) {
   169  	return types.EncodeSlice(a.Cnts), nil
   170  }
   171  
   172  func (a *Decimal64Avg) UnmarshalBinary(data []byte) error {
   173  	// avoid resulting errors caused by morpc overusing memory
   174  	copyData := make([]byte, len(data))
   175  	copy(copyData, data)
   176  	a.Cnts = types.DecodeSlice[int64](copyData)
   177  	return nil
   178  }
   179  
   180  func NewD128Avg() *Decimal128Avg {
   181  	return &Decimal128Avg{}
   182  }
   183  
   184  func (a *Decimal128Avg) Grows(cnt int) {
   185  	for i := 0; i < cnt; i++ {
   186  		a.Cnts = append(a.Cnts, 0)
   187  	}
   188  }
   189  
   190  func (a *Decimal128Avg) Eval(vs []types.Decimal128) []types.Decimal128 {
   191  	for i := range vs {
   192  		if a.Cnts[i] == 0 {
   193  			continue
   194  		}
   195  		vs[i] = vs[i].DivInt64(a.Cnts[i])
   196  	}
   197  	return vs
   198  }
   199  
   200  func (a *Decimal128Avg) Fill(i int64, value types.Decimal128, ov types.Decimal128, z int64, isEmpty bool, isNull bool) (types.Decimal128, bool) {
   201  	if !isNull {
   202  		a.Cnts[i] += z
   203  		return ov.Add(value.MulInt64(z)), false
   204  	}
   205  	return ov, isEmpty
   206  }
   207  
   208  func (a *Decimal128Avg) Merge(xIndex int64, yIndex int64, x types.Decimal128, y types.Decimal128, xEmpty bool, yEmpty bool, yAvg any) (types.Decimal128, bool) {
   209  	if !yEmpty {
   210  		ya := yAvg.(*Decimal128Avg)
   211  		a.Cnts[xIndex] += ya.Cnts[yIndex]
   212  		if !xEmpty {
   213  			return x.Add(y), false
   214  		}
   215  		return y, false
   216  	}
   217  
   218  	return x, xEmpty
   219  }
   220  
   221  func (a *Decimal128Avg) BatchFill(rs, vs any, start, count int64, vps []uint64, zs []int64, nsp *nulls.Nulls) error {
   222  	if err := sum.Decimal128Sum(rs.([]types.Decimal128), vs.([]types.Decimal128), start, count, vps, zs, nsp); err != nil {
   223  		return err
   224  	}
   225  	for i := int64(0); i < count; i++ {
   226  		if nsp.Contains(uint64(i + start)) {
   227  			continue
   228  		}
   229  		if vps[i] == 0 {
   230  			continue
   231  		}
   232  		j := vps[i] - 1
   233  		a.Cnts[j] += zs[i+start]
   234  	}
   235  	return nil
   236  }
   237  
   238  func (a *Decimal128Avg) MarshalBinary() ([]byte, error) {
   239  	return types.EncodeSlice(a.Cnts), nil
   240  }
   241  
   242  func (a *Decimal128Avg) UnmarshalBinary(data []byte) error {
   243  	// avoid resulting errors caused by morpc overusing memory
   244  	copyData := make([]byte, len(data))
   245  	copy(copyData, data)
   246  	a.Cnts = types.DecodeSlice[int64](copyData)
   247  	return nil
   248  }