github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/agg/stddevpop.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  	"math"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  )
    22  
    23  type Stddevpop[T1 types.Floats | types.Ints | types.UInts] struct {
    24  	Variance *Variance[T1]
    25  }
    26  
    27  type StdD64 struct {
    28  	Variance *VD64
    29  }
    30  
    31  type StdD128 struct {
    32  	Variance *VD128
    33  }
    34  
    35  func StdDevPopReturnType(typs []types.Type) types.Type {
    36  	switch typs[0].Oid {
    37  	case types.T_decimal64, types.T_decimal128:
    38  		return types.New(types.T_decimal128, 0, typs[0].Scale, typs[0].Precision)
    39  	default:
    40  		return types.New(types.T_float64, 0, 0, 0)
    41  	}
    42  }
    43  
    44  // NewStdDevPop is used to create a StdDevPop which supports float,int,uint
    45  func NewStdDevPop[T1 types.Floats | types.Ints | types.UInts]() *Stddevpop[T1] {
    46  	return &Stddevpop[T1]{Variance: NewVariance[T1]()}
    47  }
    48  
    49  func (sdp *Stddevpop[T1]) Grows(sizes int) {
    50  	sdp.Variance.Grows(sizes)
    51  }
    52  
    53  func (sdp *Stddevpop[T1]) Eval(vs []float64) []float64 {
    54  	sdp.Variance.Eval(vs)
    55  	for i, v := range vs {
    56  		vs[i] = math.Sqrt(v)
    57  	}
    58  	return vs
    59  }
    60  
    61  func (sdp *Stddevpop[T1]) Merge(groupIndex1, groupIndex2 int64, x, y float64, IsEmpty1 bool, IsEmpty2 bool, agg any) (float64, bool) {
    62  	Stddevpop := agg.(*Stddevpop[T1])
    63  	return sdp.Variance.Merge(groupIndex1, groupIndex2, x, y, IsEmpty1, IsEmpty2, Stddevpop.Variance)
    64  }
    65  
    66  func (sdp *Stddevpop[T1]) Fill(groupIndex int64, v1 T1, v2 float64, z int64, IsEmpty bool, hasNull bool) (float64, bool) {
    67  	return sdp.Variance.Fill(groupIndex, v1, v2, z, IsEmpty, hasNull)
    68  }
    69  
    70  func (sdp *Stddevpop[T1]) MarshalBinary() ([]byte, error) {
    71  	return types.Encode(sdp.Variance)
    72  }
    73  
    74  func (sdp *Stddevpop[T1]) UnmarshalBinary(data []byte) error {
    75  	// avoid resulting errors caused by morpc overusing memory
    76  	copyData := make([]byte, len(data))
    77  	copy(copyData, data)
    78  	types.Decode(copyData, sdp.Variance)
    79  	return nil
    80  }
    81  
    82  func NewStdD64() *StdD64 {
    83  	return &StdD64{Variance: NewVD64()}
    84  }
    85  
    86  func (s *StdD64) Grows(size int) {
    87  	s.Variance.Grows(size)
    88  }
    89  
    90  func (s *StdD64) Eval(vs []types.Decimal128) []types.Decimal128 {
    91  	s.Variance.Eval(vs)
    92  	for i, v := range vs {
    93  		tmp := math.Sqrt(v.ToFloat64())
    94  		d, _ := types.Decimal128_FromFloat64(tmp, 34, 10)
    95  		vs[i] = d
    96  	}
    97  	return vs
    98  }
    99  
   100  func (s *StdD64) Merge(groupIndex1, groupIndex2 int64, x, y types.Decimal128, IsEmpty1 bool, IsEmpty2 bool, agg any) (types.Decimal128, bool) {
   101  	ss := agg.(*StdD64)
   102  	return s.Variance.Merge(groupIndex1, groupIndex2, x, y, IsEmpty1, IsEmpty2, ss.Variance)
   103  }
   104  
   105  func (s *StdD64) Fill(groupIndex int64, v1 types.Decimal64, v2 types.Decimal128, z int64, IsEmpty bool, hasNull bool) (types.Decimal128, bool) {
   106  	return s.Variance.Fill(groupIndex, v1, v2, z, IsEmpty, hasNull)
   107  }
   108  
   109  func (s *StdD64) MarshalBinary() ([]byte, error) {
   110  	return types.Encode(s.Variance)
   111  }
   112  
   113  func (s *StdD64) UnmarshalBinary(data []byte) error {
   114  	// avoid resulting errors caused by morpc overusing memory
   115  	copyData := make([]byte, len(data))
   116  	copy(copyData, data)
   117  	types.Decode(copyData, s.Variance)
   118  	return nil
   119  }
   120  
   121  func NewStdD128() *StdD128 {
   122  	return &StdD128{Variance: NewVD128()}
   123  }
   124  
   125  func (s *StdD128) Grows(size int) {
   126  	s.Variance.Grows(size)
   127  }
   128  
   129  func (s *StdD128) Eval(vs []types.Decimal128) []types.Decimal128 {
   130  	s.Variance.Eval(vs)
   131  	for i, v := range vs {
   132  		tmp := math.Sqrt(v.ToFloat64())
   133  		d, _ := types.Decimal128_FromFloat64(tmp, 34, 10)
   134  		vs[i] = d
   135  	}
   136  	return vs
   137  }
   138  
   139  func (s *StdD128) Merge(groupIndex1, groupIndex2 int64, x, y types.Decimal128, IsEmpty1 bool, IsEmpty2 bool, agg any) (types.Decimal128, bool) {
   140  	ss := agg.(*StdD128)
   141  	return s.Variance.Merge(groupIndex1, groupIndex2, x, y, IsEmpty1, IsEmpty2, ss.Variance)
   142  }
   143  
   144  func (s *StdD128) Fill(groupIndex int64, v1 types.Decimal128, v2 types.Decimal128, z int64, IsEmpty bool, hasNull bool) (types.Decimal128, bool) {
   145  	return s.Variance.Fill(groupIndex, v1, v2, z, IsEmpty, hasNull)
   146  }
   147  
   148  func (s *StdD128) MarshalBinary() ([]byte, error) {
   149  	return types.Encode(s.Variance)
   150  }
   151  
   152  func (s *StdD128) UnmarshalBinary(data []byte) error {
   153  	// avoid resulting errors caused by morpc overusing memory
   154  	copyData := make([]byte, len(data))
   155  	copy(copyData, data)
   156  	types.Decode(copyData, s.Variance)
   157  	return nil
   158  }