github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/mergeblock/mergeblock_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 package mergeblock 15 16 import ( 17 "context" 18 "fmt" 19 "reflect" 20 "testing" 21 22 "github.com/matrixorigin/matrixone/pkg/container/types" 23 "github.com/matrixorigin/matrixone/pkg/vm/process" 24 25 "github.com/matrixorigin/matrixone/pkg/objectio" 26 "github.com/matrixorigin/matrixone/pkg/sql/colexec/value_scan" 27 "github.com/matrixorigin/matrixone/pkg/vm" 28 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/blockio" 29 30 "github.com/matrixorigin/matrixone/pkg/catalog" 31 "github.com/matrixorigin/matrixone/pkg/container/batch" 32 "github.com/matrixorigin/matrixone/pkg/container/vector" 33 "github.com/matrixorigin/matrixone/pkg/testutil" 34 "github.com/matrixorigin/matrixone/pkg/vm/engine" 35 "github.com/stretchr/testify/require" 36 ) 37 38 type mockRelation struct { 39 engine.Relation 40 result *batch.Batch 41 } 42 43 func (e *mockRelation) Write(_ context.Context, b *batch.Batch) error { 44 e.result = b 45 return nil 46 } 47 48 func TestMergeBlock(t *testing.T) { 49 proc := testutil.NewProc() 50 proc.Ctx = context.TODO() 51 segmentid := objectio.NewSegmentid() 52 name1 := objectio.BuildObjectName(segmentid, 0) 53 name2 := objectio.BuildObjectName(segmentid, 1) 54 name3 := objectio.BuildObjectName(segmentid, 2) 55 loc1 := blockio.EncodeLocation(name1, objectio.Extent{}, 15, 0) 56 loc2 := blockio.EncodeLocation(name2, objectio.Extent{}, 15, 0) 57 loc3 := blockio.EncodeLocation(name3, objectio.Extent{}, 15, 0) 58 59 sid1 := loc1.Name().SegmentId() 60 blkInfo1 := objectio.BlockInfo{ 61 BlockID: *objectio.NewBlockid( 62 &sid1, 63 loc1.Name().Num(), 64 loc1.ID()), 65 SegmentID: sid1, 66 //non-appendable block 67 EntryState: false, 68 } 69 blkInfo1.SetMetaLocation(loc1) 70 71 sid2 := loc2.Name().SegmentId() 72 blkInfo2 := objectio.BlockInfo{ 73 BlockID: *objectio.NewBlockid( 74 &sid2, 75 loc2.Name().Num(), 76 loc2.ID()), 77 SegmentID: sid2, 78 //non-appendable block 79 EntryState: false, 80 } 81 blkInfo2.SetMetaLocation(loc2) 82 83 sid3 := loc3.Name().SegmentId() 84 blkInfo3 := objectio.BlockInfo{ 85 BlockID: *objectio.NewBlockid( 86 &sid3, 87 loc3.Name().Num(), 88 loc3.ID()), 89 SegmentID: sid3, 90 //non-appendable block 91 EntryState: false, 92 } 93 blkInfo3.SetMetaLocation(loc3) 94 95 batch1 := &batch.Batch{ 96 Attrs: []string{catalog.BlockMeta_TableIdx_Insert, catalog.BlockMeta_BlockInfo, catalog.ObjectMeta_ObjectStats}, 97 Vecs: []*vector.Vector{ 98 testutil.MakeInt16Vector([]int16{0, 0, 0}, nil), 99 testutil.MakeTextVector([]string{ 100 string(objectio.EncodeBlockInfo(blkInfo1)), 101 string(objectio.EncodeBlockInfo(blkInfo2)), 102 string(objectio.EncodeBlockInfo(blkInfo3))}, 103 nil), 104 testutil.MakeTextVector([]string{ 105 string(objectio.ZeroObjectStats[:]), 106 string(objectio.ZeroObjectStats[:]), 107 string(objectio.ZeroObjectStats[:])}, 108 nil), 109 }, 110 Cnt: 1, 111 } 112 batch1.SetRowCount(3) 113 114 argument1 := Argument{ 115 Tbl: &mockRelation{}, 116 //Unique_tbls: []engine.Relation{&mockRelation{}, &mockRelation{}}, 117 affectedRows: 0, 118 OperatorBase: vm.OperatorBase{ 119 OperatorInfo: vm.OperatorInfo{ 120 Idx: 0, 121 IsFirst: false, 122 IsLast: false, 123 }, 124 }, 125 AddAffectedRows: true, 126 } 127 resetChildren(&argument1, batch1) 128 129 argument1.Prepare(proc) 130 _, err := argument1.Call(proc) 131 require.NoError(t, err) 132 require.Equal(t, uint64(15*3), argument1.affectedRows) 133 // Check Tbl 134 { 135 result := argument1.Tbl.(*mockRelation).result 136 // check attr names 137 require.True(t, reflect.DeepEqual( 138 []string{catalog.BlockMeta_BlockInfo, catalog.ObjectMeta_ObjectStats}, 139 result.Attrs, 140 )) 141 // check vector 142 require.Equal(t, 2, len(result.Vecs)) 143 //for i, vec := range result.Vecs { 144 require.Equal(t, 3, result.Vecs[0].Length(), fmt.Sprintf("column number: %d", 0)) 145 require.Equal(t, 3, result.Vecs[1].Length(), fmt.Sprintf("column number: %d", 1)) 146 //} 147 } 148 // Check UniqueTables 149 //for j := range argument1.Unique_tbls { 150 // tbl := argument1.Unique_tbls[j] 151 // result := tbl.(*mockRelation).result 152 // // check attr names 153 // require.True(t, reflect.DeepEqual( 154 // []string{catalog.BlockMeta_MetaLoc}, 155 // result.Attrs, 156 // )) 157 // // check vector 158 // require.Equal(t, 1, len(result.Vecs)) 159 // for i, vec := range result.Vecs { 160 // require.Equal(t, 1, vec.Length(), fmt.Sprintf("column number: %d", i)) 161 // } 162 //} 163 argument1.Free(proc, false, nil) 164 for k := range argument1.container.mp { 165 argument1.container.mp[k].Clean(proc.GetMPool()) 166 } 167 argument1.GetChildren(0).Free(proc, false, nil) 168 proc.FreeVectors() 169 require.Equal(t, int64(0), proc.GetMPool().CurrNB()) 170 } 171 172 func resetChildren(arg *Argument, bat *batch.Batch) { 173 arg.SetChildren( 174 []vm.Operator{ 175 &value_scan.Argument{ 176 Batchs: []*batch.Batch{bat}, 177 }, 178 }) 179 } 180 181 func mockBlockInfoBat(proc *process.Process, withStats bool) *batch.Batch { 182 var attrs []string 183 if withStats { 184 attrs = []string{catalog.BlockMeta_TableIdx_Insert, catalog.BlockMeta_BlockInfo, catalog.ObjectMeta_ObjectStats} 185 } else { 186 attrs = []string{catalog.BlockMeta_TableIdx_Insert, catalog.BlockMeta_BlockInfo} 187 } 188 189 blockInfoBat := batch.NewWithSize(len(attrs)) 190 blockInfoBat.Attrs = attrs 191 blockInfoBat.Vecs[0] = proc.GetVector(types.T_int16.ToType()) 192 blockInfoBat.Vecs[1] = proc.GetVector(types.T_text.ToType()) 193 194 if withStats { 195 blockInfoBat.Vecs[2], _ = vector.NewConstBytes(types.T_binary.ToType(), 196 objectio.ZeroObjectStats.Marshal(), 1, proc.GetMPool()) 197 } 198 199 return blockInfoBat 200 } 201 202 func TestArgument_GetMetaLocBat(t *testing.T) { 203 arg := Argument{ 204 Tbl: &mockRelation{}, 205 //Unique_tbls: []engine.Relation{&mockRelation{}, &mockRelation{}}, 206 affectedRows: 0, 207 OperatorBase: vm.OperatorBase{ 208 OperatorInfo: vm.OperatorInfo{ 209 Idx: 0, 210 IsFirst: false, 211 IsLast: false, 212 }, 213 }, 214 AddAffectedRows: true, 215 } 216 217 proc := testutil.NewProc() 218 proc.Ctx = context.TODO() 219 220 arg.Prepare(proc) 221 222 bat := mockBlockInfoBat(proc, true) 223 arg.GetMetaLocBat(bat, proc) 224 225 require.Equal(t, 2, len(arg.container.mp[0].Vecs)) 226 227 arg.container.mp[0].Clean(proc.GetMPool()) 228 bat.Clean(proc.GetMPool()) 229 230 bat = mockBlockInfoBat(proc, false) 231 arg.GetMetaLocBat(bat, proc) 232 233 require.Equal(t, 2, len(arg.container.mp[0].Vecs)) 234 235 arg.Free(proc, false, nil) 236 for k := range arg.container.mp { 237 arg.container.mp[k].Clean(proc.GetMPool()) 238 } 239 240 proc.FreeVectors() 241 bat.Clean(proc.GetMPool()) 242 require.Equal(t, int64(0), proc.GetMPool().CurrNB()) 243 }