github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/aggfuncs/func_count_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package aggfuncs_test
    15  
    16  import (
    17  	"encoding/binary"
    18  	"testing"
    19  
    20  	"github.com/dgryski/go-farm"
    21  	. "github.com/whtcorpsinc/check"
    22  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    23  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    24  	"github.com/whtcorpsinc/milevadb/interlock/aggfuncs"
    25  )
    26  
    27  func genApproxDistinctMergePartialResult(begin, end uint64) string {
    28  	o := aggfuncs.NewPartialResult4ApproxCountDistinct()
    29  	encodedBytes := make([]byte, 8)
    30  	for i := begin; i < end; i++ {
    31  		binary.LittleEndian.PutUint64(encodedBytes, i)
    32  		x := farm.Hash64(encodedBytes)
    33  		o.InsertHash64(x)
    34  	}
    35  	return string(o.Serialize())
    36  }
    37  
    38  func (s *testSuite) TestMergePartialResult4Count(c *C) {
    39  	tester := builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeLonglong, 5, 5, 3, 8)
    40  	s.testMergePartialResult(c, tester)
    41  
    42  	tester = builPosetDaggTester(ast.AggFuncApproxCountDistinct, allegrosql.TypeLonglong, 5, genApproxDistinctMergePartialResult(0, 5), genApproxDistinctMergePartialResult(2, 5), 5)
    43  	s.testMergePartialResult(c, tester)
    44  }
    45  
    46  func (s *testSuite) TestCount(c *C) {
    47  	tests := []aggTest{
    48  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeLonglong, 5, 0, 5),
    49  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeFloat, 5, 0, 5),
    50  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeDouble, 5, 0, 5),
    51  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeNewDecimal, 5, 0, 5),
    52  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeString, 5, 0, 5),
    53  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeDate, 5, 0, 5),
    54  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeDuration, 5, 0, 5),
    55  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeJSON, 5, 0, 5),
    56  	}
    57  	for _, test := range tests {
    58  		s.testAggFunc(c, test)
    59  	}
    60  	tests2 := []multiArgsAggTest{
    61  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeLonglong, allegrosql.TypeLonglong}, allegrosql.TypeLonglong, 5, 0, 5),
    62  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeFloat, allegrosql.TypeFloat}, allegrosql.TypeLonglong, 5, 0, 5),
    63  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeDouble, allegrosql.TypeDouble}, allegrosql.TypeLonglong, 5, 0, 5),
    64  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeNewDecimal, allegrosql.TypeNewDecimal}, allegrosql.TypeLonglong, 5, 0, 5),
    65  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeString, allegrosql.TypeString}, allegrosql.TypeLonglong, 5, 0, 5),
    66  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeDate, allegrosql.TypeDate}, allegrosql.TypeLonglong, 5, 0, 5),
    67  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeDuration, allegrosql.TypeDuration}, allegrosql.TypeLonglong, 5, 0, 5),
    68  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeJSON, allegrosql.TypeJSON}, allegrosql.TypeLonglong, 5, 0, 5),
    69  	}
    70  	for _, test := range tests2 {
    71  		s.testMultiArgsAggFunc(c, test)
    72  	}
    73  
    74  	tests3 := []aggTest{
    75  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeLonglong, 5, 0, 5),
    76  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeFloat, 5, 0, 5),
    77  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeDouble, 5, 0, 5),
    78  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeNewDecimal, 5, 0, 5),
    79  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeString, 5, 0, 5),
    80  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeDate, 5, 0, 5),
    81  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeDuration, 5, 0, 5),
    82  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeJSON, 5, 0, 5),
    83  	}
    84  	for _, test := range tests3 {
    85  		s.testAggFunc(c, test)
    86  	}
    87  
    88  	tests4 := []multiArgsAggTest{
    89  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeLonglong, allegrosql.TypeLonglong}, allegrosql.TypeLonglong, 5, 0, 5),
    90  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeFloat, allegrosql.TypeFloat}, allegrosql.TypeLonglong, 5, 0, 5),
    91  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeDouble, allegrosql.TypeDouble}, allegrosql.TypeLonglong, 5, 0, 5),
    92  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeNewDecimal, allegrosql.TypeNewDecimal}, allegrosql.TypeLonglong, 5, 0, 5),
    93  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeString, allegrosql.TypeString}, allegrosql.TypeLonglong, 5, 0, 5),
    94  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeDate, allegrosql.TypeDate}, allegrosql.TypeLonglong, 5, 0, 5),
    95  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeDuration, allegrosql.TypeDuration}, allegrosql.TypeLonglong, 5, 0, 5),
    96  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeJSON, allegrosql.TypeJSON}, allegrosql.TypeLonglong, 5, 0, 5),
    97  	}
    98  
    99  	for _, test := range tests4 {
   100  		s.testMultiArgsAggFunc(c, test)
   101  	}
   102  }
   103  
   104  func (s *testSuite) TestMemCount(c *C) {
   105  	tests := []aggMemTest{
   106  		builPosetDaggMemTester(ast.AggFuncCount, allegrosql.TypeLonglong, 5,
   107  			aggfuncs.DefPartialResult4CountSize, defaultUFIDelateMemDeltaGens, false),
   108  		builPosetDaggMemTester(ast.AggFuncCount, allegrosql.TypeFloat, 5,
   109  			aggfuncs.DefPartialResult4CountSize, defaultUFIDelateMemDeltaGens, false),
   110  		builPosetDaggMemTester(ast.AggFuncCount, allegrosql.TypeDouble, 5,
   111  			aggfuncs.DefPartialResult4CountSize, defaultUFIDelateMemDeltaGens, false),
   112  		builPosetDaggMemTester(ast.AggFuncCount, allegrosql.TypeNewDecimal, 5,
   113  			aggfuncs.DefPartialResult4CountSize, defaultUFIDelateMemDeltaGens, false),
   114  		builPosetDaggMemTester(ast.AggFuncCount, allegrosql.TypeString, 5,
   115  			aggfuncs.DefPartialResult4CountSize, defaultUFIDelateMemDeltaGens, false),
   116  		builPosetDaggMemTester(ast.AggFuncCount, allegrosql.TypeDate, 5,
   117  			aggfuncs.DefPartialResult4CountSize, defaultUFIDelateMemDeltaGens, false),
   118  		builPosetDaggMemTester(ast.AggFuncCount, allegrosql.TypeDuration, 5,
   119  			aggfuncs.DefPartialResult4CountSize, defaultUFIDelateMemDeltaGens, false),
   120  		builPosetDaggMemTester(ast.AggFuncCount, allegrosql.TypeLonglong, 5,
   121  			aggfuncs.DefPartialResult4CountDistinctIntSize, distinctUFIDelateMemDeltaGens, true),
   122  		builPosetDaggMemTester(ast.AggFuncCount, allegrosql.TypeFloat, 5,
   123  			aggfuncs.DefPartialResult4CountDistinctRealSize, distinctUFIDelateMemDeltaGens, true),
   124  		builPosetDaggMemTester(ast.AggFuncCount, allegrosql.TypeDouble, 5,
   125  			aggfuncs.DefPartialResult4CountDistinctRealSize, distinctUFIDelateMemDeltaGens, true),
   126  		builPosetDaggMemTester(ast.AggFuncCount, allegrosql.TypeNewDecimal, 5,
   127  			aggfuncs.DefPartialResult4CountDistinctDecimalSize, distinctUFIDelateMemDeltaGens, true),
   128  		builPosetDaggMemTester(ast.AggFuncCount, allegrosql.TypeString, 5,
   129  			aggfuncs.DefPartialResult4CountDistinctStringSize, distinctUFIDelateMemDeltaGens, true),
   130  		builPosetDaggMemTester(ast.AggFuncCount, allegrosql.TypeDate, 5,
   131  			aggfuncs.DefPartialResult4CountWithDistinctSize, distinctUFIDelateMemDeltaGens, true),
   132  		builPosetDaggMemTester(ast.AggFuncCount, allegrosql.TypeDuration, 5,
   133  			aggfuncs.DefPartialResult4CountDistinctDurationSize, distinctUFIDelateMemDeltaGens, true),
   134  		builPosetDaggMemTester(ast.AggFuncCount, allegrosql.TypeJSON, 5,
   135  			aggfuncs.DefPartialResult4CountWithDistinctSize, distinctUFIDelateMemDeltaGens, true),
   136  		builPosetDaggMemTester(ast.AggFuncApproxCountDistinct, allegrosql.TypeLonglong, 5,
   137  			aggfuncs.DefPartialResult4ApproxCountDistinctSize, approxCountDistinctUFIDelateMemDeltaGens, true),
   138  		builPosetDaggMemTester(ast.AggFuncApproxCountDistinct, allegrosql.TypeString, 5,
   139  			aggfuncs.DefPartialResult4ApproxCountDistinctSize, approxCountDistinctUFIDelateMemDeltaGens, true),
   140  	}
   141  	for _, test := range tests {
   142  		s.testAggMemFunc(c, test)
   143  	}
   144  }
   145  
   146  func BenchmarkCount(b *testing.B) {
   147  	s := testSuite{}
   148  	s.SetUpSuite(nil)
   149  
   150  	rowNum := 50000
   151  	tests := []aggTest{
   152  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   153  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeFloat, rowNum, 0, rowNum),
   154  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeDouble, rowNum, 0, rowNum),
   155  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeNewDecimal, rowNum, 0, rowNum),
   156  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeString, rowNum, 0, rowNum),
   157  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeDate, rowNum, 0, rowNum),
   158  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeDuration, rowNum, 0, rowNum),
   159  		builPosetDaggTester(ast.AggFuncCount, allegrosql.TypeJSON, rowNum, 0, rowNum),
   160  	}
   161  	for _, test := range tests {
   162  		s.benchmarkAggFunc(b, test)
   163  	}
   164  
   165  	tests2 := []multiArgsAggTest{
   166  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeLonglong, allegrosql.TypeLonglong}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   167  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeFloat, allegrosql.TypeFloat}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   168  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeDouble, allegrosql.TypeDouble}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   169  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeNewDecimal, allegrosql.TypeNewDecimal}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   170  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeString, allegrosql.TypeString}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   171  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeDate, allegrosql.TypeDate}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   172  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeDuration, allegrosql.TypeDuration}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   173  		buildMultiArgsAggTester(ast.AggFuncCount, []byte{allegrosql.TypeJSON, allegrosql.TypeJSON}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   174  	}
   175  	for _, test := range tests2 {
   176  		s.benchmarkMultiArgsAggFunc(b, test)
   177  	}
   178  
   179  	tests3 := []multiArgsAggTest{
   180  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeLonglong, allegrosql.TypeLonglong}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   181  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeFloat, allegrosql.TypeFloat}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   182  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeDouble, allegrosql.TypeDouble}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   183  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeNewDecimal, allegrosql.TypeNewDecimal}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   184  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeString, allegrosql.TypeString}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   185  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeDate, allegrosql.TypeDate}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   186  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeDuration, allegrosql.TypeDuration}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   187  		buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{allegrosql.TypeJSON, allegrosql.TypeJSON}, allegrosql.TypeLonglong, rowNum, 0, rowNum),
   188  	}
   189  	for _, test := range tests3 {
   190  		s.benchmarkMultiArgsAggFunc(b, test)
   191  	}
   192  }