github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/hash_test.go (about)

     1  // Copyright 2021 - 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 multi
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    21  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  	"github.com/matrixorigin/matrixone/pkg/testutil"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  const (
    29  	Rows = 10 // default test rows
    30  )
    31  
    32  type hashTestCase struct {
    33  	flgs  []bool // flgs[i] == true: nullable
    34  	types []types.Type
    35  	proc  *process.Process
    36  }
    37  
    38  var (
    39  	tcs []hashTestCase
    40  )
    41  
    42  func init() {
    43  	tcs = []hashTestCase{
    44  		newTestCase([]bool{false}, []types.Type{{Oid: types.T_int8}}),
    45  		newTestCase([]bool{false, true, false, true}, []types.Type{
    46  			{Oid: types.T_int8},
    47  			{Oid: types.T_int16},
    48  			{Oid: types.T_int32},
    49  			{Oid: types.T_int64},
    50  		}),
    51  		newTestCase([]bool{false, true, false, true}, []types.Type{
    52  			{Oid: types.T_int8},
    53  			{Oid: types.T_int16},
    54  			{Oid: types.T_varchar},
    55  			{Oid: types.T_varchar},
    56  		}),
    57  	}
    58  }
    59  
    60  func TestHash(t *testing.T) {
    61  	for _, tc := range tcs {
    62  		bat := newBatch(t, tc.flgs, tc.types, tc.proc, Rows)
    63  		vec, err := Hash(bat.Vecs, tc.proc)
    64  		require.NoError(t, err)
    65  		bat.Clean(tc.proc.Mp())
    66  		vec.Free(tc.proc.Mp())
    67  		require.Equal(t, int64(0), tc.proc.Mp().CurrNB())
    68  	}
    69  }
    70  
    71  func newTestCase(flgs []bool, ts []types.Type) hashTestCase {
    72  	return hashTestCase{
    73  		types: ts,
    74  		flgs:  flgs,
    75  		proc:  testutil.NewProcessWithMPool(mpool.MustNewZero()),
    76  	}
    77  }
    78  
    79  // create a new block based on the type information, flgs[i] == ture: has null
    80  func newBatch(t *testing.T, flgs []bool, ts []types.Type, proc *process.Process, rows int64) *batch.Batch {
    81  	return testutil.NewBatch(ts, false, int(rows), proc.Mp())
    82  }