github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/agg/aggUt/variance_test.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 aggut
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/sql/colexec/agg"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  )
    24  
    25  // TODO: add decimal128 distinct test
    26  func TestVariance(t *testing.T) {
    27  	int8Typ := types.New(types.T_int8, 0, 0, 0)
    28  	decimal64Typ := types.New(types.T_decimal64, 0, 0, 0)
    29  	decimal128Typ := types.New(types.T_decimal128, 0, 0, 0)
    30  
    31  	testCases := []testCase{
    32  		// int8 variance test
    33  		{
    34  			op:         agg.AggregateVariance,
    35  			isDistinct: false,
    36  			inputTyp:   int8Typ,
    37  
    38  			input:    []int8{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
    39  			inputNsp: nil,
    40  			expected: []float64{8.25},
    41  
    42  			mergeInput:  []int8{10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
    43  			mergeNsp:    nil,
    44  			mergeExpect: []float64{33.25},
    45  
    46  			testMarshal: true,
    47  		},
    48  		// int8 variance test
    49  		{
    50  			op:         agg.AggregateVariance,
    51  			isDistinct: true,
    52  			inputTyp:   int8Typ,
    53  
    54  			input:    []int8{1, 1, 2, 2, 3, 3, 4, 4, 5, 5},
    55  			inputNsp: nil,
    56  			expected: []float64{2},
    57  
    58  			mergeInput:  []int8{6, 6, 7, 7, 8, 8, 9, 9, 10, 10},
    59  			mergeNsp:    nil,
    60  			mergeExpect: []float64{8.25},
    61  
    62  			testMarshal: false,
    63  		},
    64  		// decimal64 variance test
    65  		{
    66  			op:         agg.AggregateVariance,
    67  			isDistinct: false,
    68  			inputTyp:   decimal64Typ,
    69  
    70  			input:    []int64{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
    71  			inputNsp: nil,
    72  			expected: []float64{8.25},
    73  
    74  			mergeInput:  []int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
    75  			mergeNsp:    nil,
    76  			mergeExpect: []float64{8.25},
    77  
    78  			testMarshal: true,
    79  		},
    80  		{
    81  			op:         agg.AggregateVariance,
    82  			isDistinct: true,
    83  			inputTyp:   decimal64Typ,
    84  
    85  			input:    []int64{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
    86  			inputNsp: nil,
    87  			expected: []float64{8.25},
    88  
    89  			mergeInput:  []int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
    90  			mergeNsp:    nil,
    91  			mergeExpect: []float64{8.25},
    92  
    93  			testMarshal: false,
    94  		},
    95  		// decimal128 variance test
    96  		{
    97  			op:         agg.AggregateVariance,
    98  			isDistinct: false,
    99  			inputTyp:   decimal128Typ,
   100  
   101  			input:    []int64{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
   102  			inputNsp: nil,
   103  			expected: []float64{8.25},
   104  
   105  			mergeInput:  []int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
   106  			mergeNsp:    nil,
   107  			mergeExpect: []float64{8.25},
   108  
   109  			testMarshal: true,
   110  		},
   111  		{
   112  			op:         agg.AggregateVariance,
   113  			isDistinct: true,
   114  			inputTyp:   decimal128Typ,
   115  
   116  			input:    []int64{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
   117  			inputNsp: nil,
   118  			expected: []float64{8.25},
   119  
   120  			mergeInput:  []int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
   121  			mergeNsp:    nil,
   122  			mergeExpect: []float64{8.25},
   123  
   124  			testMarshal: false,
   125  		},
   126  	}
   127  	RunTest(t, testCases)
   128  }