github.com/matrixorigin/matrixone@v0.7.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  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/stl/containers"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/container/types"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestBatch1(t *testing.T) {
    29  	defer testutils.AfterTest(t)()
    30  	vecTypes := types.MockColTypes(4)[2:]
    31  	attrs := []string{"attr1", "attr2"}
    32  	nullable := []bool{false, true}
    33  	opts := containers.Options{}
    34  	opts.Capacity = 0
    35  	bat := BuildBatch(attrs, vecTypes, nullable, opts)
    36  	bat.Vecs[0].Append(int32(1))
    37  	bat.Vecs[0].Append(int32(2))
    38  	bat.Vecs[0].Append(int32(3))
    39  	bat.Vecs[1].Append(int64(11))
    40  	bat.Vecs[1].Append(int64(12))
    41  	bat.Vecs[1].Append(int64(13))
    42  
    43  	assert.Equal(t, 3, bat.Length())
    44  	assert.False(t, bat.HasDelete())
    45  	bat.Delete(1)
    46  	assert.Equal(t, 3, bat.Length())
    47  	assert.True(t, bat.HasDelete())
    48  	assert.True(t, bat.IsDeleted(1))
    49  
    50  	w := new(bytes.Buffer)
    51  	_, err := bat.WriteTo(w)
    52  	assert.NoError(t, err)
    53  
    54  	r := bytes.NewBuffer(w.Bytes())
    55  	bat2 := NewEmptyBatch()
    56  	_, err = bat2.ReadFrom(r)
    57  	assert.NoError(t, err)
    58  	assert.True(t, bat.Equals(bat2))
    59  
    60  	bat.Close()
    61  }
    62  
    63  func TestBatch2(t *testing.T) {
    64  	defer testutils.AfterTest(t)()
    65  	vecTypes := types.MockColTypes(17)
    66  	bat := MockBatch(vecTypes, 10, 3, nil)
    67  	assert.Equal(t, 10, bat.Length())
    68  
    69  	cloned := bat.CloneWindow(0, 5)
    70  	assert.Equal(t, 5, cloned.Length())
    71  	t.Log(cloned.Allocated())
    72  	cloned.Close()
    73  	cloned = bat.CloneWindow(0, bat.Length())
    74  	assert.True(t, bat.Equals(cloned))
    75  	cloned.Close()
    76  	bat.Close()
    77  }
    78  
    79  func TestBatch3(t *testing.T) {
    80  	defer testutils.AfterTest(t)()
    81  	vecTypes := types.MockColTypes(17)
    82  	bat := MockBatch(vecTypes, 101, 3, nil)
    83  	defer bat.Close()
    84  	bats := bat.Split(5)
    85  	assert.Equal(t, 5, len(bats))
    86  	row := 0
    87  	for _, b := range bats {
    88  		row += b.Length()
    89  	}
    90  	assert.Equal(t, bat.Length(), row)
    91  
    92  	bat2 := MockBatch(vecTypes, 20, 3, nil)
    93  	bats = bat2.Split(2)
    94  	t.Log(bats[0].Vecs[3].Length())
    95  	t.Log(bats[1].Vecs[3].Length())
    96  }