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