github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/agg/stddev_pop.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 agg
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/types"
    19  	"github.com/matrixorigin/matrixone/pkg/sql/colexec/aggexec"
    20  	"math"
    21  )
    22  
    23  func RegisterStdDevPop1(id int64) {
    24  	aggexec.RegisterSingleAggFromFixedToFixed(
    25  		aggexec.MakeSingleAgg1RegisteredInfo(
    26  			aggexec.MakeSingleColumnAggInformation(id, types.T_bit.ToType(), VarPopReturnType, false, true),
    27  			newAggVarPop[uint64],
    28  			InitAggVarPop1[uint64],
    29  			FillAggVarPop1[uint64], nil, FillsAggVarPop1[uint64],
    30  			MergeAggVarPop1[uint64],
    31  			FlushAggStdDevPop1[uint64],
    32  		))
    33  
    34  	aggexec.RegisterSingleAggFromFixedToFixed(
    35  		aggexec.MakeSingleAgg1RegisteredInfo(
    36  			aggexec.MakeSingleColumnAggInformation(id, types.T_int8.ToType(), VarPopReturnType, false, true),
    37  			newAggVarPop[int8],
    38  			InitAggVarPop1[int8],
    39  			FillAggVarPop1[int8], nil, FillsAggVarPop1[int8],
    40  			MergeAggVarPop1[int8],
    41  			FlushAggStdDevPop1[int8],
    42  		))
    43  
    44  	aggexec.RegisterSingleAggFromFixedToFixed(
    45  		aggexec.MakeSingleAgg1RegisteredInfo(
    46  			aggexec.MakeSingleColumnAggInformation(id, types.T_int16.ToType(), VarPopReturnType, false, true),
    47  			newAggVarPop[int16],
    48  			InitAggVarPop1[int16],
    49  			FillAggVarPop1[int16], nil, FillsAggVarPop1[int16],
    50  			MergeAggVarPop1[int16],
    51  			FlushAggStdDevPop1[int16],
    52  		))
    53  
    54  	aggexec.RegisterSingleAggFromFixedToFixed(
    55  		aggexec.MakeSingleAgg1RegisteredInfo(
    56  			aggexec.MakeSingleColumnAggInformation(id, types.T_int32.ToType(), VarPopReturnType, false, true),
    57  			newAggVarPop[int32],
    58  			InitAggVarPop1[int32],
    59  			FillAggVarPop1[int32], nil, FillsAggVarPop1[int32],
    60  			MergeAggVarPop1[int32],
    61  			FlushAggStdDevPop1[int32],
    62  		))
    63  
    64  	aggexec.RegisterSingleAggFromFixedToFixed(
    65  		aggexec.MakeSingleAgg1RegisteredInfo(
    66  			aggexec.MakeSingleColumnAggInformation(id, types.T_int64.ToType(), VarPopReturnType, false, true),
    67  			newAggVarPop[int64],
    68  			InitAggVarPop1[int64],
    69  			FillAggVarPop1[int64], nil, FillsAggVarPop1[int64],
    70  			MergeAggVarPop1[int64],
    71  			FlushAggStdDevPop1[int64],
    72  		))
    73  
    74  	aggexec.RegisterSingleAggFromFixedToFixed(
    75  		aggexec.MakeSingleAgg1RegisteredInfo(
    76  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint8.ToType(), VarPopReturnType, false, true),
    77  			newAggVarPop[uint8],
    78  			InitAggVarPop1[uint8],
    79  			FillAggVarPop1[uint8], nil, FillsAggVarPop1[uint8],
    80  			MergeAggVarPop1[uint8],
    81  			FlushAggStdDevPop1[uint8],
    82  		))
    83  
    84  	aggexec.RegisterSingleAggFromFixedToFixed(
    85  		aggexec.MakeSingleAgg1RegisteredInfo(
    86  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint16.ToType(), VarPopReturnType, false, true),
    87  			newAggVarPop[uint16],
    88  			InitAggVarPop1[uint16],
    89  			FillAggVarPop1[uint16], nil, FillsAggVarPop1[uint16],
    90  			MergeAggVarPop1[uint16],
    91  			FlushAggStdDevPop1[uint16],
    92  		))
    93  
    94  	aggexec.RegisterSingleAggFromFixedToFixed(
    95  		aggexec.MakeSingleAgg1RegisteredInfo(
    96  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint32.ToType(), VarPopReturnType, false, true),
    97  			newAggVarPop[uint32],
    98  			InitAggVarPop1[uint32],
    99  			FillAggVarPop1[uint32], nil, FillsAggVarPop1[uint32],
   100  			MergeAggVarPop1[uint32],
   101  			FlushAggStdDevPop1[uint32],
   102  		))
   103  
   104  	aggexec.RegisterSingleAggFromFixedToFixed(
   105  		aggexec.MakeSingleAgg1RegisteredInfo(
   106  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint64.ToType(), VarPopReturnType, false, true),
   107  			newAggVarPop[uint64],
   108  			InitAggVarPop1[uint64],
   109  			FillAggVarPop1[uint64], nil, FillsAggVarPop1[uint64],
   110  			MergeAggVarPop1[uint64],
   111  			FlushAggStdDevPop1[uint64],
   112  		))
   113  
   114  	aggexec.RegisterSingleAggFromFixedToFixed(
   115  		aggexec.MakeSingleAgg1RegisteredInfo(
   116  			aggexec.MakeSingleColumnAggInformation(id, types.T_float32.ToType(), VarPopReturnType, false, true),
   117  			newAggVarPop[float32],
   118  			InitAggVarPop1[float32],
   119  			FillAggVarPop1[float32], nil, FillsAggVarPop1[float32],
   120  			MergeAggVarPop1[float32],
   121  			FlushAggStdDevPop1[float32],
   122  		))
   123  
   124  	aggexec.RegisterSingleAggFromFixedToFixed(
   125  		aggexec.MakeSingleAgg1RegisteredInfo(
   126  			aggexec.MakeSingleColumnAggInformation(id, types.T_float64.ToType(), VarPopReturnType, false, true),
   127  			newAggVarPop[float64],
   128  			InitAggVarPop1[float64],
   129  			FillAggVarPop1[float64], nil, FillsAggVarPop1[float64],
   130  			MergeAggVarPop1[float64],
   131  			FlushAggStdDevPop1[float64],
   132  		))
   133  
   134  	aggexec.RegisterSingleAggFromFixedToFixed(
   135  		aggexec.MakeSingleAgg1RegisteredInfo(
   136  			aggexec.MakeSingleColumnAggInformation(id, types.T_decimal64.ToType(), VarPopReturnType, false, true),
   137  			newAggVarPopDecimal64,
   138  			InitAggVarPop1Decimal64,
   139  			FillAggVarPop1Decimal64, nil, FillsAggVarPop1Decimal64,
   140  			MergeAggVarPop1Decimal64,
   141  			FlushAggStdDevPopDecimal64,
   142  		))
   143  
   144  	aggexec.RegisterSingleAggFromFixedToFixed(
   145  		aggexec.MakeSingleAgg1RegisteredInfo(
   146  			aggexec.MakeSingleColumnAggInformation(id, types.T_decimal128.ToType(), VarPopReturnType, false, true),
   147  			newAggVarPopDecimal128,
   148  			InitAggVarPop1Decimal128,
   149  			FillAggVarPop1Decimal128, nil, FillsAggVarPop1Decimal128,
   150  			MergeAggVarPop1Decimal128,
   151  			FlushAggStdDevPopDecimal128,
   152  		))
   153  }
   154  
   155  func FlushAggStdDevPop1[from numeric](
   156  	exec aggexec.SingleAggFromFixedRetFixed[from, float64],
   157  	getter aggexec.AggGetter[float64], setter aggexec.AggSetter[float64]) error {
   158  	a := exec.(*aggVarPop[from])
   159  	if a.count <= 1 {
   160  		setter(0)
   161  		return nil
   162  	}
   163  	avg := a.sum / float64(a.count)
   164  	variance := getter()/float64(a.count) - math.Pow(avg, 2)
   165  	setter(math.Sqrt(variance))
   166  	return nil
   167  }
   168  
   169  func FlushAggStdDevPopDecimal128(
   170  	exec aggexec.SingleAggFromFixedRetFixed[types.Decimal128, types.Decimal128],
   171  	getter aggexec.AggGetter[types.Decimal128], setter aggexec.AggSetter[types.Decimal128]) error {
   172  	a := exec.(*aggVarPopDecimal128)
   173  	r, err := getVarianceFromSumPowCount(a.sum, getter(), a.count, a.argScale)
   174  	if err != nil {
   175  		return err
   176  	}
   177  	if r.B0_63 == 0 && r.B64_127 == 0 {
   178  		setter(r)
   179  		return nil
   180  	}
   181  	temp, err1 := types.Decimal128FromFloat64(
   182  		math.Sqrt(types.Decimal128ToFloat64(r, a.retScale)),
   183  		38, a.retScale)
   184  	if err1 != nil {
   185  		return err1
   186  	}
   187  	setter(temp)
   188  	return nil
   189  }
   190  
   191  func FlushAggStdDevPopDecimal64(
   192  	exec aggexec.SingleAggFromFixedRetFixed[types.Decimal64, types.Decimal128],
   193  	getter aggexec.AggGetter[types.Decimal128], setter aggexec.AggSetter[types.Decimal128]) error {
   194  	a := exec.(*aggVarPopDecimal128)
   195  	r, err := getVarianceFromSumPowCount(a.sum, getter(), a.count, a.argScale)
   196  	if err != nil {
   197  		return err
   198  	}
   199  	if r.B0_63 == 0 && r.B64_127 == 0 {
   200  		setter(r)
   201  		return nil
   202  	}
   203  	temp, err1 := types.Decimal128FromFloat64(
   204  		math.Sqrt(types.Decimal128ToFloat64(r, a.retScale)),
   205  		38, a.retScale)
   206  	if err1 != nil {
   207  		return err1
   208  	}
   209  	setter(temp)
   210  	return nil
   211  }