github.com/matrixorigin/matrixone@v1.2.0/pkg/util/executor/result_test.go (about)

     1  // Copyright 2023 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 executor
    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/assert"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestReadRows(t *testing.T) {
    28  	mp := mpool.MustNewZero()
    29  	defer func() {
    30  		require.Equal(t, int64(0), mp.CurrNB())
    31  	}()
    32  
    33  	memRes := NewMemResult(
    34  		[]types.Type{types.New(types.T_int32, 0, 0), types.New(types.T_varchar, 2, 0)},
    35  		mp)
    36  	memRes.NewBatch()
    37  	require.NoError(t, AppendFixedRows(memRes, 0, []int32{1, 2, 3, 4}))
    38  	require.NoError(t, AppendStringRows(memRes, 1, []string{"s1", "s2", "s3", "s4"}))
    39  
    40  	memRes.NewBatch()
    41  	require.NoError(t, AppendFixedRows(memRes, 0, []int32{5, 6, 7, 8}))
    42  	require.NoError(t, AppendStringRows(memRes, 1, []string{"s5", "s6", "s7", "s8"}))
    43  
    44  	res := memRes.GetResult()
    45  	defer res.Close()
    46  
    47  	var col1 []int32
    48  	var col2 [][]byte
    49  	var cols2WithString []string
    50  	res.ReadRows(func(_ int, cols []*vector.Vector) bool {
    51  		col1 = append(col1, GetFixedRows[int32](cols[0])...)
    52  		col2 = append(col2, GetBytesRows(cols[1])...)
    53  		cols2WithString = append(cols2WithString, GetStringRows(cols[1])...)
    54  		return true
    55  	})
    56  	assert.Equal(t, []int32{1, 2, 3, 4, 5, 6, 7, 8}, col1)
    57  	assert.Equal(t, [][]byte{[]byte("s1"), []byte("s2"), []byte("s3"), []byte("s4"), []byte("s5"), []byte("s6"), []byte("s7"), []byte("s8")}, col2)
    58  	assert.Equal(t, []string{"s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8"}, cols2WithString)
    59  }