github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/containers/batch_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 containers
    16  
    17  import (
    18  	"bytes"
    19  	"math/rand"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/google/uuid"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    26  
    27  	"github.com/matrixorigin/matrixone/pkg/container/types"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestBatch1a(t *testing.T) {
    32  	defer testutils.AfterTest(t)()
    33  	vecTypes := types.MockColTypes()[2:4] // int32, int64
    34  	attrs := []string{"attr1", "attr2"}
    35  	opts := Options{}
    36  	opts.Capacity = 0
    37  	bat := BuildBatch(attrs, vecTypes, opts)
    38  	bat.Vecs[0].Append(int32(1), false)
    39  	bat.Vecs[0].Append(int32(2), false)
    40  	bat.Vecs[0].Append(int32(3), false)
    41  	bat.Vecs[1].Append(int64(11), false)
    42  	bat.Vecs[1].Append(int64(12), false)
    43  	bat.Vecs[1].Append(int64(13), false)
    44  
    45  	assert.Equal(t, 3, bat.Length())
    46  	assert.False(t, bat.HasDelete())
    47  	bat.Delete(1)
    48  	assert.Equal(t, 3, bat.Length())
    49  	assert.True(t, bat.HasDelete())
    50  	assert.True(t, bat.IsDeleted(1))
    51  
    52  	w := new(bytes.Buffer)
    53  	_, err := bat.WriteTo(w)
    54  	assert.NoError(t, err)
    55  
    56  	r := bytes.NewBuffer(w.Bytes())
    57  	bat2 := NewEmptyBatch()
    58  	_, err = bat2.ReadFrom(r)
    59  	assert.NoError(t, err)
    60  	assert.True(t, bat.Equals(bat2))
    61  
    62  	bat.Close()
    63  }
    64  
    65  func TestBatch1b(t *testing.T) {
    66  	defer testutils.AfterTest(t)()
    67  	vecTypes := types.MockColTypes()[12:14] // Varchar, Char
    68  	attrs := []string{"attr1", "attr2"}
    69  	opts := Options{}
    70  	opts.Capacity = 0
    71  	bat := BuildBatch(attrs, vecTypes, opts)
    72  	bat.Vecs[0].Append([]byte("a"), false)
    73  	bat.Vecs[0].Append([]byte("b"), false)
    74  	bat.Vecs[0].Append([]byte("c"), false)
    75  	bat.Vecs[1].Append([]byte("1"), false)
    76  	bat.Vecs[1].Append([]byte("2"), false)
    77  	bat.Vecs[1].Append([]byte("3"), false)
    78  
    79  	assert.Equal(t, 3, bat.Length())
    80  	assert.False(t, bat.HasDelete())
    81  	bat.Delete(1)
    82  	assert.Equal(t, 3, bat.Length())
    83  	assert.True(t, bat.HasDelete())
    84  	assert.True(t, bat.IsDeleted(1))
    85  
    86  	w := new(bytes.Buffer)
    87  	_, err := bat.WriteTo(w)
    88  	assert.NoError(t, err)
    89  
    90  	r := bytes.NewBuffer(w.Bytes())
    91  	bat2 := NewEmptyBatch()
    92  	_, err = bat2.ReadFrom(r)
    93  	assert.NoError(t, err)
    94  	assert.True(t, bat.Equals(bat2))
    95  
    96  	bat.Close()
    97  }
    98  func TestBatch2(t *testing.T) {
    99  	defer testutils.AfterTest(t)()
   100  	vecTypes := types.MockColTypes()
   101  	bat := MockBatch(vecTypes, 10, 3, nil)
   102  	assert.Equal(t, 10, bat.Length())
   103  
   104  	cloned := bat.CloneWindow(0, 5)
   105  	assert.Equal(t, 5, cloned.Length())
   106  	t.Log(cloned.Allocated())
   107  	cloned.Close()
   108  	cloned = bat.CloneWindow(0, bat.Length())
   109  	assert.True(t, bat.Equals(cloned))
   110  	cloned.Close()
   111  	bat.Close()
   112  }
   113  
   114  func TestBatch3(t *testing.T) {
   115  	defer testutils.AfterTest(t)()
   116  	vecTypes := types.MockColTypes()
   117  	bat := MockBatch(vecTypes, 101, 3, nil)
   118  	defer bat.Close()
   119  	bats := bat.Split(5)
   120  	assert.Equal(t, 5, len(bats))
   121  	row := 0
   122  	for _, b := range bats {
   123  		row += b.Length()
   124  	}
   125  	assert.Equal(t, bat.Length(), row)
   126  
   127  	bat2 := MockBatch(vecTypes, 20, 3, nil)
   128  	bats = bat2.Split(2)
   129  	t.Log(bats[0].Vecs[3].Length())
   130  	t.Log(bats[1].Vecs[3].Length())
   131  }
   132  
   133  func TestBatchWithPool(t *testing.T) {
   134  	defer testutils.AfterTest(t)()
   135  	colTypes := types.MockColTypes()
   136  	seedBat := MockBatch(colTypes, 10, 3, nil)
   137  	defer seedBat.Close()
   138  
   139  	uid := uuid.New()
   140  	pool := NewVectorPool(uid.String(), 200)
   141  
   142  	bat := BuildBatchWithPool(seedBat.Attrs, colTypes, 0, pool)
   143  
   144  	err := bat.Append(seedBat)
   145  	assert.NoError(t, err)
   146  	assert.True(t, seedBat.Equals(bat))
   147  
   148  	bat.Reset()
   149  
   150  	usedCnt, _ := pool.FixedSizeUsed(false)
   151  	assert.True(t, usedCnt > 0)
   152  
   153  	assert.Equal(t, 0, bat.Length())
   154  	err = bat.Append(seedBat)
   155  	assert.NoError(t, err)
   156  	assert.True(t, seedBat.Equals(bat))
   157  	bat.Close()
   158  
   159  	usedCnt, _ = pool.FixedSizeUsed(false)
   160  	assert.Equal(t, 0, usedCnt)
   161  	usedCnt, _ = pool.VarlenUsed(false)
   162  	assert.Equal(t, 0, usedCnt)
   163  
   164  	pool.Destory()
   165  }
   166  
   167  func TestBatchSpliter(t *testing.T) {
   168  	defer testutils.AfterTest(t)()
   169  	vecTypes := types.MockColTypes()
   170  	bat := MockBatch(vecTypes, 11, 3, nil)
   171  	defer bat.Close()
   172  	spliter := NewBatchSplitter(bat, 5)
   173  	expects_1 := []int{5, 5, 1}
   174  	var actuals_1 []int
   175  	for {
   176  		bat, err := spliter.Next()
   177  		if err != nil {
   178  			break
   179  		}
   180  		actuals_1 = append(actuals_1, bat.Length())
   181  	}
   182  	assert.Equal(t, expects_1, actuals_1)
   183  
   184  	bat2 := MockBatch(vecTypes, 10, 3, nil)
   185  	defer bat2.Close()
   186  	spliter = NewBatchSplitter(bat2, 5)
   187  	expects_2 := []int{5, 5}
   188  	var actuals_2 []int
   189  	for {
   190  		bat, err := spliter.Next()
   191  		if err != nil {
   192  			break
   193  		}
   194  		actuals_2 = append(actuals_2, bat.Length())
   195  	}
   196  	assert.Equal(t, expects_2, actuals_2)
   197  }
   198  
   199  func TestApproxSize(t *testing.T) {
   200  	defer testutils.AfterTest(t)()
   201  	vec := MockVector(types.T_uint8.ToType(), 1024, false, nil)
   202  	t.Log(vec.ApproxSize())
   203  	defer vec.Close()
   204  
   205  	svec := MakeVector(types.T_varchar.ToType(), common.DefaultAllocator)
   206  	defer svec.Close()
   207  	sizeCnt := 0
   208  	for i := 0; i < 1024; i++ {
   209  		l := rand.Intn(100)
   210  		sizeCnt += l
   211  		svec.Append([]byte(strings.Repeat("x", l)), false)
   212  	}
   213  	t.Log(svec.ApproxSize(), sizeCnt)
   214  }