github.com/matrixorigin/matrixone@v0.7.0/pkg/container/index/low_cardinality_index_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 index
    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/stretchr/testify/require"
    24  )
    25  
    26  func TestInsertWithNulls(t *testing.T) {
    27  	idx, err := newTestIndex(types.T_varchar.ToType())
    28  	require.NoError(t, err)
    29  
    30  	// test data = ["a", "b", NULL, "a", "c", NULL, "c", "b", "a", NULL]
    31  	v := vector.New(types.T_varchar.ToType())
    32  	require.NoError(t, v.Append([]byte("a"), false, idx.m))
    33  	require.NoError(t, v.Append([]byte("b"), false, idx.m))
    34  	require.NoError(t, v.Append([]byte(""), true, idx.m))
    35  	require.NoError(t, v.Append([]byte("a"), false, idx.m))
    36  	require.NoError(t, v.Append([]byte("c"), false, idx.m))
    37  	require.NoError(t, v.Append([]byte(""), true, idx.m))
    38  	require.NoError(t, v.Append([]byte("c"), false, idx.m))
    39  	require.NoError(t, v.Append([]byte("b"), false, idx.m))
    40  	require.NoError(t, v.Append([]byte("a"), false, idx.m))
    41  	require.NoError(t, v.Append([]byte(""), true, idx.m))
    42  
    43  	// dict = ["a"->1, "b"->2, "c"->3]
    44  	require.NoError(t, idx.InsertBatch(v))
    45  
    46  	require.Equal(t, []string{"a", "b", "c"}, vector.GetStrVectorValues(idx.dict.GetUnique()))
    47  	require.Equal(t, []uint16{1, 2, 0, 1, 3, 0, 3, 2, 1, 0}, vector.MustTCols[uint16](idx.poses))
    48  }
    49  
    50  func TestEncode(t *testing.T) {
    51  	idx, err := newTestIndex(types.T_varchar.ToType())
    52  	require.NoError(t, err)
    53  
    54  	v0 := vector.New(types.T_varchar.ToType())
    55  	require.NoError(t, vector.AppendBytes(v0, [][]byte{
    56  		[]byte("hello"),
    57  		[]byte("My"),
    58  		[]byte("name"),
    59  		[]byte("is"),
    60  		[]byte("Tom"),
    61  	}, idx.m))
    62  
    63  	err = idx.InsertBatch(v0)
    64  	require.NoError(t, err)
    65  
    66  	v1 := vector.New(types.T_varchar.ToType())
    67  	require.NoError(t, vector.AppendBytes(v1, [][]byte{
    68  		[]byte("Jack"),
    69  		[]byte("is"),
    70  		[]byte("My"),
    71  		[]byte("friend"),
    72  		[]byte("name"),
    73  		[]byte("Tom"),
    74  	}, idx.m))
    75  
    76  	enc := vector.New(types.T_uint16.ToType())
    77  	err = idx.Encode(enc, v1)
    78  	require.NoError(t, err)
    79  	col := vector.MustTCols[uint16](enc)
    80  	require.Equal(t, uint16(0), col[0])
    81  	require.Equal(t, uint16(4), col[1])
    82  	require.Equal(t, uint16(2), col[2])
    83  	require.Equal(t, uint16(0), col[3])
    84  	require.Equal(t, uint16(3), col[4])
    85  	require.Equal(t, uint16(5), col[5])
    86  }
    87  
    88  func newTestIndex(typ types.Type) (*LowCardinalityIndex, error) {
    89  	return New(typ, mpool.MustNewZero())
    90  }