github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/agg/aggUt/count_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  func TestCount(t *testing.T) {
    26  	int8TestTyp := types.New(types.T_int8, 0, 0, 0)
    27  	boolTestTyp := types.New(types.T_bool, 0, 0, 0)
    28  	varcharTestTyp := types.New(types.T_varchar, types.MaxVarcharLen, 0, 0)
    29  	decimalTestTyp := types.New(types.T_decimal128, 0, 0, 0)
    30  	uuidTestTyp := types.New(types.T_uuid, 0, 0, 0)
    31  
    32  	testCases := []testCase{
    33  		// int8 count test
    34  		{
    35  			op:         agg.AggregateCount,
    36  			isDistinct: false,
    37  			inputTyp:   int8TestTyp,
    38  
    39  			input:    []int8{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
    40  			inputNsp: nil,
    41  			expected: []int64{10},
    42  
    43  			mergeInput:  []int8{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
    44  			mergeNsp:    nil,
    45  			mergeExpect: []int64{20},
    46  
    47  			testMarshal: true,
    48  		},
    49  		{
    50  			op:         agg.AggregateCount,
    51  			isDistinct: true,
    52  			inputTyp:   int8TestTyp,
    53  
    54  			input:    []int8{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
    55  			inputNsp: nil,
    56  			expected: []int64{10},
    57  
    58  			mergeInput:  []int8{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
    59  			mergeNsp:    nil,
    60  			mergeExpect: []int64{10},
    61  
    62  			testMarshal: false,
    63  		},
    64  		{
    65  			op:         agg.AggregateCount,
    66  			isDistinct: true,
    67  			inputTyp:   int8TestTyp,
    68  
    69  			input:    []int8{1, 1, 2, 2, 3, 3, 4, 4, 5, 5},
    70  			inputNsp: nil,
    71  			expected: []int64{5},
    72  
    73  			mergeInput:  []int8{6, 6, 7, 7, 8, 8, 9, 9, 10, 10},
    74  			mergeNsp:    nil,
    75  			mergeExpect: []int64{10},
    76  
    77  			testMarshal: false,
    78  		},
    79  		// bool count test
    80  		{
    81  			op:         agg.AggregateCount,
    82  			isDistinct: false,
    83  			inputTyp:   boolTestTyp,
    84  
    85  			input:    []bool{true, true, false, true, false, true, false, true, false, true},
    86  			inputNsp: nil,
    87  			expected: []int64{10},
    88  
    89  			mergeInput:  []bool{false, false, false, false, false, false, false, false, false, false},
    90  			mergeNsp:    nil,
    91  			mergeExpect: []int64{20},
    92  
    93  			testMarshal: true,
    94  		},
    95  		{
    96  			op:         agg.AggregateCount,
    97  			isDistinct: false,
    98  			inputTyp:   varcharTestTyp,
    99  
   100  			input:    []string{"aa", "bb", "cc"},
   101  			inputNsp: nil,
   102  			expected: []int64{3},
   103  
   104  			mergeInput:  []string{"aa", "bb", "cc"},
   105  			mergeNsp:    nil,
   106  			mergeExpect: []int64{6},
   107  
   108  			testMarshal: true,
   109  		},
   110  		// varchar count test
   111  		{
   112  			op:         agg.AggregateCount,
   113  			isDistinct: true,
   114  			inputTyp:   varcharTestTyp,
   115  
   116  			input:    []string{"aa", "bb", "cc"},
   117  			inputNsp: nil,
   118  			expected: []int64{3},
   119  
   120  			mergeInput:  []string{"aa", "bb", "cc"},
   121  			mergeNsp:    nil,
   122  			mergeExpect: []int64{3},
   123  
   124  			testMarshal: false,
   125  		},
   126  		// decimal128 count test
   127  		{
   128  			op:         agg.AggregateCount,
   129  			isDistinct: false,
   130  			inputTyp:   decimalTestTyp,
   131  
   132  			input:    []int64{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
   133  			inputNsp: nil,
   134  			expected: []int64{10},
   135  
   136  			mergeInput:  []int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
   137  			mergeNsp:    nil,
   138  			mergeExpect: []int64{20},
   139  
   140  			testMarshal: true,
   141  		},
   142  		{
   143  			op:         agg.AggregateCount,
   144  			isDistinct: true,
   145  			inputTyp:   decimalTestTyp,
   146  
   147  			input:    []int64{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
   148  			inputNsp: nil,
   149  			expected: []int64{10},
   150  
   151  			mergeInput:  []int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
   152  			mergeNsp:    nil,
   153  			mergeExpect: []int64{10},
   154  
   155  			testMarshal: false,
   156  		},
   157  		{
   158  			op:         agg.AggregateCount,
   159  			isDistinct: false,
   160  			inputTyp:   uuidTestTyp,
   161  			input: []string{
   162  				"f6355110-2d0c-11ed-940f-000c29847904",
   163  				"1ef96142-2d0d-11ed-940f-000c29847904",
   164  				"117a0bd5-2d0d-11ed-940f-000c29847904",
   165  				"18b21c70-2d0d-11ed-940f-000c29847904",
   166  				"1b50c129-2dba-11ed-940f-000c29847904",
   167  			},
   168  			inputNsp: nil,
   169  			expected: []int64{5},
   170  			mergeInput: []string{
   171  				"f6355110-2d0c-11ed-940f-000c29847904",
   172  				"1ef96142-2d0d-11ed-940f-000c29847904",
   173  				"117a0bd5-2d0d-11ed-940f-000c29847904",
   174  				"18b21c70-2d0d-11ed-940f-000c29847904",
   175  				"1b50c129-2dba-11ed-940f-000c29847904",
   176  			},
   177  			mergeNsp:    nil,
   178  			mergeExpect: []int64{10},
   179  			testMarshal: true,
   180  		},
   181  		{
   182  			op:         agg.AggregateCount,
   183  			isDistinct: true,
   184  			inputTyp:   uuidTestTyp,
   185  			input: []string{
   186  				"f6355110-2d0c-11ed-940f-000c29847904",
   187  				"1ef96142-2d0d-11ed-940f-000c29847904",
   188  				"117a0bd5-2d0d-11ed-940f-000c29847904",
   189  				"18b21c70-2d0d-11ed-940f-000c29847904",
   190  				"1b50c129-2dba-11ed-940f-000c29847904",
   191  			},
   192  			inputNsp: nil,
   193  			expected: []int64{5},
   194  			mergeInput: []string{
   195  				"f6355110-2d0c-11ed-940f-000c29847904",
   196  				"1ef96142-2d0d-11ed-940f-000c29847904",
   197  				"117a0bd5-2d0d-11ed-940f-000c29847904",
   198  				"18b21c70-2d0d-11ed-940f-000c29847904",
   199  				"1b50c129-2dba-11ed-940f-000c29847904",
   200  			},
   201  			mergeNsp:    nil,
   202  			mergeExpect: []int64{5},
   203  			testMarshal: false,
   204  		},
   205  	}
   206  
   207  	RunTest(t, testCases)
   208  }