github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/mergedelete/mergedelete_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 mergedelete
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/catalog"
    24  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    25  	"github.com/matrixorigin/matrixone/pkg/container/types"
    26  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    27  	"github.com/matrixorigin/matrixone/pkg/objectio"
    28  	"github.com/matrixorigin/matrixone/pkg/sql/colexec/deletion"
    29  	"github.com/matrixorigin/matrixone/pkg/sql/colexec/value_scan"
    30  	"github.com/matrixorigin/matrixone/pkg/testutil"
    31  	"github.com/matrixorigin/matrixone/pkg/vm"
    32  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    33  	"github.com/stretchr/testify/require"
    34  )
    35  
    36  type mockRelation struct {
    37  	engine.Relation
    38  	result *batch.Batch
    39  }
    40  
    41  func (e *mockRelation) Delete(ctx context.Context, b *batch.Batch, attrName string) error {
    42  	e.result = b
    43  	return nil
    44  }
    45  
    46  func TestMergeDelete(t *testing.T) {
    47  	proc := testutil.NewProc()
    48  	proc.Ctx = context.TODO()
    49  	metaLocBat0 := &batch.Batch{
    50  		Attrs: []string{
    51  			catalog.BlockMetaOffset,
    52  		},
    53  		Vecs: []*vector.Vector{
    54  			testutil.MakeInt64Vector([]int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, nil),
    55  		},
    56  	}
    57  	bytes, err := metaLocBat0.MarshalBinary()
    58  	require.Nil(t, err)
    59  
    60  	vcu32, _ := vector.NewConstFixed(types.T_uint32.ToType(), uint32(15), 1, proc.GetMPool())
    61  	batch1 := &batch.Batch{
    62  		Attrs: []string{
    63  			catalog.BlockMeta_Delete_ID,
    64  			catalog.BlockMeta_DeltaLoc,
    65  			catalog.BlockMeta_Type,
    66  			catalog.BlockMeta_Partition,
    67  			catalog.BlockMeta_Deletes_Length,
    68  		},
    69  		Vecs: []*vector.Vector{
    70  			testutil.MakeTextVector([]string{"mock_block_id0"}, nil),
    71  			testutil.MakeTextVector([]string{string(bytes)}, nil),
    72  			testutil.MakeInt8Vector([]int8{deletion.RawBatchOffset}, nil),
    73  			testutil.MakeInt32Vector([]int32{0}, nil),
    74  			vcu32,
    75  		},
    76  	}
    77  	batch1.SetRowCount(1)
    78  	uuid1 := objectio.NewSegmentid()
    79  	blkId1 := objectio.NewBlockid(uuid1, 0, 0)
    80  	metaLocBat1 := &batch.Batch{
    81  		Attrs: []string{
    82  			catalog.Row_ID,
    83  		},
    84  		Vecs: []*vector.Vector{
    85  			testutil.MakeRowIdVector([]types.Rowid{
    86  				*objectio.NewRowid(blkId1, 0),
    87  				*objectio.NewRowid(blkId1, 1),
    88  				*objectio.NewRowid(blkId1, 2),
    89  				*objectio.NewRowid(blkId1, 3),
    90  				*objectio.NewRowid(blkId1, 4),
    91  				*objectio.NewRowid(blkId1, 5),
    92  				*objectio.NewRowid(blkId1, 6),
    93  				*objectio.NewRowid(blkId1, 7),
    94  				*objectio.NewRowid(blkId1, 8),
    95  				*objectio.NewRowid(blkId1, 9),
    96  				*objectio.NewRowid(blkId1, 10),
    97  				*objectio.NewRowid(blkId1, 11),
    98  				*objectio.NewRowid(blkId1, 12),
    99  				*objectio.NewRowid(blkId1, 13),
   100  				*objectio.NewRowid(blkId1, 14),
   101  			}, nil),
   102  		},
   103  	}
   104  	bytes1, err := metaLocBat1.MarshalBinary()
   105  	require.Nil(t, err)
   106  
   107  	metaLocBat2 := &batch.Batch{
   108  		Attrs: []string{
   109  			catalog.BlockMetaOffset,
   110  		},
   111  		Vecs: []*vector.Vector{
   112  			testutil.MakeInt64Vector([]int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, nil),
   113  		},
   114  	}
   115  	bytes2, err := metaLocBat2.MarshalBinary()
   116  	require.Nil(t, err)
   117  
   118  	metaLocBat3 := &batch.Batch{
   119  		Attrs: []string{
   120  			catalog.BlockMeta_DeltaLoc,
   121  		},
   122  		Vecs: []*vector.Vector{
   123  			testutil.MakeTextVector([]string{"d:magic:15"}, nil),
   124  		},
   125  	}
   126  	bytes3, err := metaLocBat3.MarshalBinary()
   127  	require.Nil(t, err)
   128  
   129  	vcu32_2, _ := vector.NewConstFixed(types.T_uint32.ToType(), uint32(45), 3, proc.GetMPool())
   130  	batch2 := &batch.Batch{
   131  		Attrs: []string{
   132  			catalog.BlockMeta_Delete_ID,
   133  			catalog.BlockMeta_DeltaLoc,
   134  			catalog.BlockMeta_Type,
   135  			catalog.BlockMeta_Partition,
   136  			catalog.BlockMeta_Deletes_Length,
   137  		},
   138  		Vecs: []*vector.Vector{
   139  			testutil.MakeTextVector([]string{"mock_block_id1", "mock_block_id2", "mock_block_id3"}, nil),
   140  			testutil.MakeTextVector([]string{string(bytes1), string(bytes2), string(bytes3)}, nil),
   141  			testutil.MakeInt8Vector([]int8{deletion.RawRowIdBatch, deletion.CNBlockOffset, deletion.FlushDeltaLoc}, nil),
   142  			testutil.MakeInt32Vector([]int32{0, 0, 0}, nil),
   143  			vcu32_2,
   144  		},
   145  	}
   146  	batch2.SetRowCount(3)
   147  
   148  	argument1 := Argument{
   149  		DelSource:    &mockRelation{},
   150  		AffectedRows: 0,
   151  		OperatorBase: vm.OperatorBase{
   152  			OperatorInfo: vm.OperatorInfo{
   153  				Idx:     0,
   154  				IsFirst: false,
   155  				IsLast:  false,
   156  			},
   157  		},
   158  	}
   159  
   160  	require.NoError(t, argument1.Prepare(proc))
   161  	resetChildren(&argument1, batch1)
   162  	_, err = argument1.Call(proc)
   163  	require.NoError(t, err)
   164  	require.Equal(t, uint64(15), argument1.AffectedRows)
   165  
   166  	// Check DelSource
   167  	result0 := argument1.DelSource.(*mockRelation).result
   168  	// check attr names
   169  	require.True(t, reflect.DeepEqual(
   170  		[]string{
   171  			catalog.BlockMetaOffset,
   172  		},
   173  		result0.Attrs,
   174  	))
   175  	// check vector
   176  	require.Equal(t, 1, len(result0.Vecs))
   177  	for i, vec := range result0.Vecs {
   178  		require.Equal(t, 15, vec.Length(), fmt.Sprintf("column number: %d", i))
   179  	}
   180  
   181  	resetChildren(&argument1, batch2)
   182  	_, err = argument1.Call(proc)
   183  	require.NoError(t, err)
   184  	require.Equal(t, uint64(60), argument1.AffectedRows)
   185  
   186  	// Check DelSource
   187  	result1 := argument1.DelSource.(*mockRelation).result
   188  	// check attr names
   189  	require.True(t, reflect.DeepEqual(
   190  		[]string{
   191  			catalog.BlockMeta_DeltaLoc,
   192  		},
   193  		result1.Attrs,
   194  	))
   195  	// check vector
   196  	require.Equal(t, 1, len(result1.Vecs))
   197  	for i, vec := range result1.Vecs {
   198  		require.Equal(t, 1, vec.Length(), fmt.Sprintf("column number: %d", i))
   199  	}
   200  
   201  	// free resource
   202  	argument1.Free(proc, false, nil)
   203  	metaLocBat0.Clean(proc.GetMPool())
   204  	metaLocBat1.Clean(proc.GetMPool())
   205  	metaLocBat2.Clean(proc.GetMPool())
   206  	metaLocBat3.Clean(proc.GetMPool())
   207  	batch1.Clean(proc.GetMPool())
   208  	batch2.Clean(proc.GetMPool())
   209  	// constVector can't free
   210  	// 2 * 16 is 2 header of const vector.
   211  	require.Equal(t, int64(16+2*16), proc.GetMPool().CurrNB())
   212  }
   213  
   214  func resetChildren(arg *Argument, bat *batch.Batch) {
   215  	arg.SetChildren(
   216  		[]vm.Operator{
   217  			&value_scan.Argument{
   218  				Batchs: []*batch.Batch{bat},
   219  			},
   220  		})
   221  }