github.com/matrixorigin/matrixone@v1.2.0/pkg/common/hashmap/strhashmap_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 hashmap
    16  
    17  import (
    18  	"math/rand"
    19  	"strconv"
    20  	"testing"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    23  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    24  	"github.com/matrixorigin/matrixone/pkg/container/types"
    25  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  const (
    30  	Rows = 10
    31  )
    32  
    33  func TestInsert(t *testing.T) {
    34  	m := mpool.MustNewZero()
    35  	mp, err := NewStrMap(false, 0, 0, m)
    36  	require.NoError(t, err)
    37  	ts := []types.Type{
    38  		types.New(types.T_int8, 0, 0),
    39  		types.New(types.T_int16, 0, 0),
    40  		types.New(types.T_int32, 0, 0),
    41  		types.New(types.T_int64, 0, 0),
    42  		types.New(types.T_decimal64, 0, 0),
    43  		types.New(types.T_char, 0, 0),
    44  	}
    45  	vecs := newVectors(ts, false, Rows, m)
    46  	for i := 0; i < Rows; i++ {
    47  		ok, err := mp.Insert(vecs, i)
    48  		require.NoError(t, err)
    49  		require.Equal(t, true, ok)
    50  	}
    51  	for _, vec := range vecs {
    52  		vec.Free(m)
    53  	}
    54  	mp.Free()
    55  	require.Equal(t, int64(0), m.Stats().NumCurrBytes.Load())
    56  }
    57  
    58  func TestInsertValue(t *testing.T) {
    59  	m := mpool.MustNewZero()
    60  	mp, err := NewStrMap(false, 0, 0, m)
    61  	require.NoError(t, err)
    62  	ok, err := mp.InsertValue(int8(0))
    63  	require.NoError(t, err)
    64  	require.Equal(t, true, ok)
    65  	ok, err = mp.InsertValue(int16(0))
    66  	require.NoError(t, err)
    67  	require.Equal(t, false, ok)
    68  	ok, err = mp.InsertValue(int32(0))
    69  	require.NoError(t, err)
    70  	require.Equal(t, false, ok)
    71  	ok, err = mp.InsertValue(int64(0))
    72  	require.NoError(t, err)
    73  	require.Equal(t, false, ok)
    74  	ok, err = mp.InsertValue(uint8(0))
    75  	require.NoError(t, err)
    76  	require.Equal(t, false, ok)
    77  	ok, err = mp.InsertValue(uint16(0))
    78  	require.NoError(t, err)
    79  	require.Equal(t, false, ok)
    80  	ok, err = mp.InsertValue(uint32(0))
    81  	require.NoError(t, err)
    82  	require.Equal(t, false, ok)
    83  	ok, err = mp.InsertValue(uint64(0))
    84  	require.NoError(t, err)
    85  	require.Equal(t, false, ok)
    86  	ok, err = mp.InsertValue([]byte{})
    87  	require.NoError(t, err)
    88  	require.Equal(t, false, ok)
    89  	ok, err = mp.InsertValue(types.Date(0))
    90  	require.NoError(t, err)
    91  	require.Equal(t, false, ok)
    92  	ok, err = mp.InsertValue(types.Datetime(0))
    93  	require.NoError(t, err)
    94  	require.Equal(t, false, ok)
    95  	ok, err = mp.InsertValue(types.Timestamp(0))
    96  	require.NoError(t, err)
    97  	require.Equal(t, false, ok)
    98  	ok, err = mp.InsertValue(types.Decimal64(0))
    99  	require.NoError(t, err)
   100  	require.Equal(t, false, ok)
   101  	ok, err = mp.InsertValue(types.Decimal128{})
   102  	require.NoError(t, err)
   103  	require.Equal(t, false, ok)
   104  	mp.Free()
   105  	require.Equal(t, int64(0), m.Stats().NumCurrBytes.Load())
   106  }
   107  
   108  func TestIterator(t *testing.T) {
   109  	{
   110  		m := mpool.MustNewZero()
   111  		mp, err := NewStrMap(false, 0, 0, m)
   112  		require.NoError(t, err)
   113  		ts := []types.Type{
   114  			types.New(types.T_int8, 0, 0),
   115  			types.New(types.T_int16, 0, 0),
   116  			types.New(types.T_int32, 0, 0),
   117  			types.New(types.T_int64, 0, 0),
   118  			types.New(types.T_decimal64, 0, 0),
   119  			types.New(types.T_char, 0, 0),
   120  		}
   121  		vecs := newVectors(ts, false, Rows, m)
   122  		itr := mp.NewIterator()
   123  		vs, _, err := itr.Insert(0, Rows, vecs)
   124  		require.NoError(t, err)
   125  		require.Equal(t, []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, vs[:Rows])
   126  		vs, _ = itr.Find(0, Rows, vecs, nil)
   127  		require.Equal(t, []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, vs[:Rows])
   128  		for _, vec := range vecs {
   129  			vec.Free(m)
   130  		}
   131  		mp.Free()
   132  		require.Equal(t, int64(0), m.Stats().NumCurrBytes.Load())
   133  	}
   134  	{
   135  		m := mpool.MustNewZero()
   136  		mp, err := NewStrMap(true, 0, 0, m)
   137  		require.NoError(t, err)
   138  		ts := []types.Type{
   139  			types.New(types.T_int8, 0, 0),
   140  			types.New(types.T_int16, 0, 0),
   141  			types.New(types.T_int32, 0, 0),
   142  			types.New(types.T_int64, 0, 0),
   143  			types.New(types.T_decimal64, 0, 0),
   144  			types.New(types.T_char, 0, 0),
   145  		}
   146  		vecs := newVectors(ts, false, Rows, m)
   147  		itr := mp.NewIterator()
   148  		vs, _, err := itr.Insert(0, Rows, vecs)
   149  		require.NoError(t, err)
   150  		require.Equal(t, []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, vs[:Rows])
   151  		vs, _ = itr.Find(0, Rows, vecs, nil)
   152  		require.Equal(t, []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, vs[:Rows])
   153  		for _, vec := range vecs {
   154  			vec.Free(m)
   155  		}
   156  		mp.Free()
   157  		require.Equal(t, int64(0), m.Stats().NumCurrBytes.Load())
   158  	}
   159  	{
   160  		m := mpool.MustNewZero()
   161  		mp, err := NewStrMap(true, 0, 0, m)
   162  		require.NoError(t, err)
   163  		ts := []types.Type{
   164  			types.New(types.T_int8, 0, 0),
   165  			types.New(types.T_int16, 0, 0),
   166  			types.New(types.T_int32, 0, 0),
   167  			types.New(types.T_int64, 0, 0),
   168  			types.New(types.T_decimal64, 0, 0),
   169  			types.New(types.T_char, 0, 0),
   170  		}
   171  		vecs := newVectorsWithNull(ts, false, Rows, m)
   172  		itr := mp.NewIterator()
   173  		vs, _, err := itr.Insert(0, Rows, vecs)
   174  		require.NoError(t, err)
   175  		require.Equal(t, []uint64{1, 2, 1, 3, 1, 4, 1, 5, 1, 6}, vs[:Rows])
   176  		vs, _ = itr.Find(0, Rows, vecs, nil)
   177  		require.Equal(t, []uint64{1, 2, 1, 3, 1, 4, 1, 5, 1, 6}, vs[:Rows])
   178  		for _, vec := range vecs {
   179  			vec.Free(m)
   180  		}
   181  		mp.Free()
   182  		require.Equal(t, int64(0), m.Stats().NumCurrBytes.Load())
   183  	}
   184  }
   185  
   186  func newVectors(ts []types.Type, random bool, n int, m *mpool.MPool) []*vector.Vector {
   187  	vecs := make([]*vector.Vector, len(ts))
   188  	for i := range vecs {
   189  		vecs[i] = newVector(n, ts[i], m, random, nil)
   190  		vecs[i].GetNulls().InitWithSize(n)
   191  	}
   192  	return vecs
   193  }
   194  
   195  func newVectorsWithNull(ts []types.Type, random bool, n int, m *mpool.MPool) []*vector.Vector {
   196  	vecs := make([]*vector.Vector, len(ts))
   197  	for i := range vecs {
   198  		vecs[i] = newVector(n, ts[i], m, random, nil)
   199  		nsp := vecs[i].GetNulls()
   200  		nsp.InitWithSize(n)
   201  		for j := 0; j < n; j++ {
   202  			if j%2 == 0 {
   203  				nsp.Set(uint64(j))
   204  			}
   205  		}
   206  	}
   207  	return vecs
   208  }
   209  
   210  func newVector(n int, typ types.Type, m *mpool.MPool, random bool, Values interface{}) *vector.Vector {
   211  	switch typ.Oid {
   212  	case types.T_int8:
   213  		if vs, ok := Values.([]int8); ok {
   214  			return newInt8Vector(n, typ, m, random, vs)
   215  		}
   216  		return newInt8Vector(n, typ, m, random, nil)
   217  	case types.T_int16:
   218  		if vs, ok := Values.([]int16); ok {
   219  			return newInt16Vector(n, typ, m, random, vs)
   220  		}
   221  		return newInt16Vector(n, typ, m, random, nil)
   222  	case types.T_int32:
   223  		if vs, ok := Values.([]int32); ok {
   224  			return newInt32Vector(n, typ, m, random, vs)
   225  		}
   226  		return newInt32Vector(n, typ, m, random, nil)
   227  	case types.T_int64:
   228  		if vs, ok := Values.([]int64); ok {
   229  			return newInt64Vector(n, typ, m, random, vs)
   230  		}
   231  		return newInt64Vector(n, typ, m, random, nil)
   232  	case types.T_uint32:
   233  		if vs, ok := Values.([]uint32); ok {
   234  			return newUInt32Vector(n, typ, m, random, vs)
   235  		}
   236  		return newUInt32Vector(n, typ, m, random, nil)
   237  	case types.T_decimal64:
   238  		if vs, ok := Values.([]types.Decimal64); ok {
   239  			return newDecimal64Vector(n, typ, m, random, vs)
   240  		}
   241  		return newDecimal64Vector(n, typ, m, random, nil)
   242  	case types.T_decimal128:
   243  		if vs, ok := Values.([]types.Decimal128); ok {
   244  			return newDecimal128Vector(n, typ, m, random, vs)
   245  		}
   246  		return newDecimal128Vector(n, typ, m, random, nil)
   247  	case types.T_char, types.T_varchar:
   248  		if vs, ok := Values.([]string); ok {
   249  			return newStringVector(n, typ, m, random, vs)
   250  		}
   251  		return newStringVector(n, typ, m, random, nil)
   252  	default:
   253  		panic(moerr.NewInternalErrorNoCtx("unsupport vector's type '%v", typ))
   254  	}
   255  }
   256  
   257  func newInt8Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []int8) *vector.Vector {
   258  	vec := vector.NewVec(typ)
   259  	if vs != nil {
   260  		for i := range vs {
   261  			if err := vector.AppendFixed(vec, vs[i], false, m); err != nil {
   262  				vec.Free(m)
   263  				return nil
   264  			}
   265  		}
   266  		return vec
   267  	}
   268  	for i := 0; i < n; i++ {
   269  		v := i
   270  		if random {
   271  			v = rand.Int()
   272  		}
   273  		if err := vector.AppendFixed(vec, int8(v), false, m); err != nil {
   274  			vec.Free(m)
   275  			return nil
   276  		}
   277  	}
   278  	return vec
   279  }
   280  
   281  func newInt16Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []int16) *vector.Vector {
   282  	vec := vector.NewVec(typ)
   283  	if vs != nil {
   284  		for i := range vs {
   285  			if err := vector.AppendFixed(vec, vs[i], false, m); err != nil {
   286  				vec.Free(m)
   287  				return nil
   288  			}
   289  		}
   290  		return vec
   291  	}
   292  	for i := 0; i < n; i++ {
   293  		v := i
   294  		if random {
   295  			v = rand.Int()
   296  		}
   297  		if err := vector.AppendFixed(vec, int16(v), false, m); err != nil {
   298  			vec.Free(m)
   299  			return nil
   300  		}
   301  	}
   302  	return vec
   303  }
   304  
   305  func newInt32Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []int32) *vector.Vector {
   306  	vec := vector.NewVec(typ)
   307  	if vs != nil {
   308  		for i := range vs {
   309  			if err := vector.AppendFixed(vec, vs[i], false, m); err != nil {
   310  				vec.Free(m)
   311  				return nil
   312  			}
   313  		}
   314  		return vec
   315  	}
   316  	for i := 0; i < n; i++ {
   317  		v := i
   318  		if random {
   319  			v = rand.Int()
   320  		}
   321  		if err := vector.AppendFixed(vec, int32(v), false, m); err != nil {
   322  			vec.Free(m)
   323  			return nil
   324  		}
   325  	}
   326  	return vec
   327  }
   328  
   329  func newInt64Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []int64) *vector.Vector {
   330  	vec := vector.NewVec(typ)
   331  	if vs != nil {
   332  		for i := range vs {
   333  			if err := vector.AppendFixed(vec, vs[i], false, m); err != nil {
   334  				vec.Free(m)
   335  				return nil
   336  			}
   337  		}
   338  		return vec
   339  	}
   340  	for i := 0; i < n; i++ {
   341  		v := i
   342  		if random {
   343  			v = rand.Int()
   344  		}
   345  		if err := vector.AppendFixed(vec, int64(v), false, m); err != nil {
   346  			vec.Free(m)
   347  			return nil
   348  		}
   349  	}
   350  	return vec
   351  }
   352  
   353  func newUInt32Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []uint32) *vector.Vector {
   354  	vec := vector.NewVec(typ)
   355  	if vs != nil {
   356  		for i := range vs {
   357  			if err := vector.AppendFixed(vec, vs[i], false, m); err != nil {
   358  				vec.Free(m)
   359  				return nil
   360  			}
   361  		}
   362  		return vec
   363  	}
   364  	for i := 0; i < n; i++ {
   365  		v := i
   366  		if random {
   367  			v = rand.Int()
   368  		}
   369  		if err := vector.AppendFixed(vec, uint32(v), false, m); err != nil {
   370  			vec.Free(m)
   371  			return nil
   372  		}
   373  	}
   374  	return vec
   375  }
   376  
   377  func newDecimal64Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []types.Decimal64) *vector.Vector {
   378  	vec := vector.NewVec(typ)
   379  	if vs != nil {
   380  		for i := range vs {
   381  			if err := vector.AppendFixed(vec, vs[i], false, m); err != nil {
   382  				vec.Free(m)
   383  				return nil
   384  			}
   385  		}
   386  		return vec
   387  	}
   388  	for i := 0; i < n; i++ {
   389  		v := i
   390  		if random {
   391  			v = rand.Int()
   392  		}
   393  		d := types.Decimal64(v)
   394  		if err := vector.AppendFixed(vec, d, false, m); err != nil {
   395  
   396  			vec.Free(m)
   397  			return nil
   398  		}
   399  	}
   400  	return vec
   401  }
   402  
   403  func newDecimal128Vector(n int, typ types.Type, m *mpool.MPool, random bool, vs []types.Decimal128) *vector.Vector {
   404  	vec := vector.NewVec(typ)
   405  	if vs != nil {
   406  		for i := range vs {
   407  			if err := vector.AppendFixed(vec, vs[i], false, m); err != nil {
   408  				vec.Free(m)
   409  				return nil
   410  			}
   411  		}
   412  		return vec
   413  	}
   414  	for i := 0; i < n; i++ {
   415  		v := i
   416  		if random {
   417  			v = rand.Int()
   418  		}
   419  		d := types.Decimal128{B0_63: uint64(v), B64_127: 0}
   420  		if err := vector.AppendFixed(vec, d, false, m); err != nil {
   421  			vec.Free(m)
   422  			return nil
   423  		}
   424  	}
   425  	return vec
   426  }
   427  
   428  func newStringVector(n int, typ types.Type, m *mpool.MPool, random bool, vs []string) *vector.Vector {
   429  	vec := vector.NewVec(typ)
   430  	if vs != nil {
   431  		for i := range vs {
   432  			if err := vector.AppendBytes(vec, []byte(vs[i]), false, m); err != nil {
   433  				vec.Free(m)
   434  				return nil
   435  			}
   436  		}
   437  		return vec
   438  	}
   439  	for i := 0; i < n; i++ {
   440  		v := i
   441  		if random {
   442  			v = rand.Int()
   443  		}
   444  		if err := vector.AppendBytes(vec, []byte(strconv.Itoa(v)), false, m); err != nil {
   445  			vec.Free(m)
   446  			return nil
   447  		}
   448  	}
   449  	return vec
   450  }