github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/agg/aggUt/avg_test.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 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
    26  func TestAvg(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 avg test
    33  		{
    34  			op:         agg.AggregateAvg,
    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{4.5},
    41  
    42  			mergeInput:  []int8{10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
    43  			mergeNsp:    nil,
    44  			mergeExpect: []float64{9.5},
    45  
    46  			testMarshal: true,
    47  		},
    48  		// int8 distinct avg test
    49  		{
    50  			op:         agg.AggregateAvg,
    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{3},
    57  
    58  			mergeInput:  []int8{6, 6, 7, 7, 8, 8, 9, 9, 10, 10},
    59  			mergeNsp:    nil,
    60  			mergeExpect: []float64{5.5},
    61  
    62  			testMarshal: false,
    63  		},
    64  		// decimal64 avg test
    65  		{
    66  			op:         agg.AggregateAvg,
    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{4.5},
    73  
    74  			mergeInput:  []int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
    75  			mergeNsp:    nil,
    76  			mergeExpect: []float64{4.5},
    77  
    78  			testMarshal: true,
    79  		},
    80  		// decimal128 avg test
    81  		{
    82  			op:         agg.AggregateAvg,
    83  			isDistinct: false,
    84  			inputTyp:   decimal128Typ,
    85  
    86  			input:    []int64{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
    87  			inputNsp: nil,
    88  			expected: []float64{4.5},
    89  
    90  			mergeInput:  []int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
    91  			mergeNsp:    nil,
    92  			mergeExpect: []float64{4.5},
    93  
    94  			testMarshal: true,
    95  		},
    96  		// decimal128 distinct avg test
    97  		{
    98  			op:         agg.AggregateAvg,
    99  			isDistinct: true,
   100  			inputTyp:   decimal128Typ,
   101  
   102  			input:    []int64{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
   103  			inputNsp: nil,
   104  			expected: []float64{4.5},
   105  
   106  			mergeInput:  []int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
   107  			mergeNsp:    nil,
   108  			mergeExpect: []float64{4.5},
   109  
   110  			testMarshal: false,
   111  		},
   112  	}
   113  
   114  	RunTest(t, testCases)
   115  }