github.com/matrixorigin/matrixone@v1.2.0/pkg/partition/partition_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 partition
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    23  	"github.com/matrixorigin/matrixone/pkg/testutil"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    27  )
    28  
    29  func TestPartition(t *testing.T) {
    30  	mp := mpool.MustNewZero()
    31  	v0 := vector.NewVec(types.T_int8.ToType())
    32  	vector.AppendFixedList(v0, []int8{3, 4, 5, 6, 7, 8}, nil, mp)
    33  	partitions := make([]int64, 2)
    34  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v0)
    35  	require.Equal(t, []int64{0, 1}, partitions)
    36  	nulls.Add(v0.GetNulls(), 1)
    37  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v0)
    38  	require.Equal(t, []int64{0, 1}, partitions)
    39  
    40  	v1 := vector.NewVec(types.T_int16.ToType())
    41  	vector.AppendFixedList(v1, []int16{3, 4, 5, 6, 7, 8}, nil, mp)
    42  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v1)
    43  	require.Equal(t, []int64{0, 1}, partitions)
    44  	nulls.Add(v0.GetNulls(), 1)
    45  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v1)
    46  	require.Equal(t, []int64{0, 1}, partitions)
    47  
    48  	v2 := vector.NewVec(types.T_int32.ToType())
    49  	vector.AppendFixedList(v2, []int32{3, 4, 5, 6, 7, 8}, nil, mp)
    50  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v2)
    51  	require.Equal(t, []int64{0, 1}, partitions)
    52  	nulls.Add(v2.GetNulls(), 1)
    53  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v2)
    54  	require.Equal(t, []int64{0, 1}, partitions)
    55  
    56  	v3 := vector.NewVec(types.T_int64.ToType())
    57  	vector.AppendFixedList(v3, []int64{3, 4, 5, 6, 7, 8}, nil, mp)
    58  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v3)
    59  	require.Equal(t, []int64{0, 1}, partitions)
    60  	nulls.Add(v3.GetNulls(), 1)
    61  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v3)
    62  	require.Equal(t, []int64{0, 1}, partitions)
    63  
    64  	v4 := vector.NewVec(types.T_uint8.ToType())
    65  	vector.AppendFixedList(v4, []uint8{3, 4, 5, 6, 7, 8}, nil, mp)
    66  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v4)
    67  	require.Equal(t, []int64{0, 1}, partitions)
    68  	nulls.Add(v4.GetNulls(), 1)
    69  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v4)
    70  	require.Equal(t, []int64{0, 1}, partitions)
    71  
    72  	v5 := vector.NewVec(types.T_uint16.ToType())
    73  	vector.AppendFixedList(v5, []uint16{3, 4, 5, 6, 7, 8}, nil, mp)
    74  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v5)
    75  	require.Equal(t, []int64{0, 1}, partitions)
    76  	nulls.Add(v5.GetNulls(), 1)
    77  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v5)
    78  	require.Equal(t, []int64{0, 1}, partitions)
    79  
    80  	v6 := vector.NewVec(types.T_uint32.ToType())
    81  	vector.AppendFixedList(v6, []uint32{3, 4, 5, 6, 7, 8}, nil, mp)
    82  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v6)
    83  	require.Equal(t, []int64{0, 1}, partitions)
    84  	nulls.Add(v6.GetNulls(), 1)
    85  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v6)
    86  	require.Equal(t, []int64{0, 1}, partitions)
    87  
    88  	v7 := vector.NewVec(types.T_uint64.ToType())
    89  	vector.AppendFixedList(v7, []uint64{3, 4, 5, 6, 7, 8}, nil, mp)
    90  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v7)
    91  	require.Equal(t, []int64{0, 1}, partitions)
    92  	nulls.Add(v7.GetNulls(), 1)
    93  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v7)
    94  	require.Equal(t, []int64{0, 1}, partitions)
    95  
    96  	v8 := vector.NewVec(types.T_date.ToType())
    97  	vector.AppendFixedList(v8, []types.Date{3, 4, 5, 6, 7, 8}, nil, mp)
    98  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v8)
    99  	require.Equal(t, []int64{0, 1}, partitions)
   100  	nulls.Add(v8.GetNulls(), 1)
   101  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v8)
   102  	require.Equal(t, []int64{0, 1}, partitions)
   103  
   104  	v9 := vector.NewVec(types.T_float32.ToType())
   105  	vector.AppendFixedList(v9, []float32{3, 4, 5, 6, 7, 8}, nil, mp)
   106  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v9)
   107  	require.Equal(t, []int64{0, 1}, partitions)
   108  	nulls.Add(v9.GetNulls(), 1)
   109  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v9)
   110  	require.Equal(t, []int64{0, 1}, partitions)
   111  
   112  	v10 := vector.NewVec(types.T_float64.ToType())
   113  	vector.AppendFixedList(v10, []float64{3, 4, 5, 6, 7, 8}, nil, mp)
   114  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v10)
   115  	require.Equal(t, []int64{0, 1}, partitions)
   116  	nulls.Add(v10.GetNulls(), 1)
   117  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v10)
   118  	require.Equal(t, []int64{0, 1}, partitions)
   119  
   120  	v11 := vector.NewVec(types.T_char.ToType())
   121  	vector.AppendStringList(v11, []string{"hello", "Gut", "konichiwa", "nihao", "nihao", "nihao", "nihao"}, nil, mp)
   122  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v10)
   123  	require.Equal(t, []int64{0, 1}, partitions)
   124  	nulls.Add(v11.GetNulls(), 1)
   125  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v11)
   126  	require.Equal(t, []int64{0, 1}, partitions)
   127  
   128  	v12 := testutil.NewVector(5, types.T_bit.ToType(), mp, false, []uint64{3, 4, 5, 6, 7, 8})
   129  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v12)
   130  	require.Equal(t, []int64{0, 1}, partitions)
   131  	nulls.Add(v12.GetNulls(), 1)
   132  	Partition([]int64{1, 3, 5}, []bool{false, false, false}, partitions, v12)
   133  	require.Equal(t, []int64{0, 1}, partitions)
   134  
   135  }