github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/loopsemi/join_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 15 package loopsemi 16 17 import ( 18 "bytes" 19 "context" 20 "testing" 21 22 "github.com/matrixorigin/matrixone/pkg/common/mpool" 23 "github.com/matrixorigin/matrixone/pkg/container/batch" 24 "github.com/matrixorigin/matrixone/pkg/container/types" 25 "github.com/matrixorigin/matrixone/pkg/pb/plan" 26 "github.com/matrixorigin/matrixone/pkg/sql/colexec/hashbuild" 27 "github.com/matrixorigin/matrixone/pkg/sql/plan/function" 28 "github.com/matrixorigin/matrixone/pkg/testutil" 29 "github.com/matrixorigin/matrixone/pkg/vm/process" 30 "github.com/stretchr/testify/require" 31 ) 32 33 const ( 34 Rows = 10 // default rows 35 BenchmarkRows = 100000 // default rows for benchmark 36 ) 37 38 // add unit tests for cases 39 type joinTestCase struct { 40 arg *Argument 41 flgs []bool // flgs[i] == true: nullable 42 types []types.Type 43 proc *process.Process 44 cancel context.CancelFunc 45 barg *hashbuild.Argument 46 } 47 48 var ( 49 tcs []joinTestCase 50 ) 51 52 func init() { 53 tcs = []joinTestCase{ 54 newTestCase([]bool{false}, []types.Type{{Oid: types.T_int8}}, []int32{0}), 55 newTestCase([]bool{true}, []types.Type{{Oid: types.T_int8}}, []int32{0}), 56 } 57 } 58 59 func TestString(t *testing.T) { 60 buf := new(bytes.Buffer) 61 for _, tc := range tcs { 62 String(tc.arg, buf) 63 } 64 } 65 66 func TestPrepare(t *testing.T) { 67 for _, tc := range tcs { 68 err := Prepare(tc.proc, tc.arg) 69 require.NoError(t, err) 70 } 71 } 72 73 func TestJoin(t *testing.T) { 74 for _, tc := range tcs { 75 bat := hashBuild(t, tc) 76 err := Prepare(tc.proc, tc.arg) 77 require.NoError(t, err) 78 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(t, tc.flgs, tc.types, tc.proc, Rows) 79 tc.proc.Reg.MergeReceivers[0].Ch <- &batch.Batch{} 80 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(t, tc.flgs, tc.types, tc.proc, Rows) 81 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(t, tc.flgs, tc.types, tc.proc, Rows) 82 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(t, tc.flgs, tc.types, tc.proc, Rows) 83 tc.proc.Reg.MergeReceivers[0].Ch <- nil 84 tc.proc.Reg.MergeReceivers[1].Ch <- bat 85 for { 86 if ok, err := Call(0, tc.proc, tc.arg, false, false); ok || err != nil { 87 break 88 } 89 tc.proc.Reg.InputBatch.Clean(tc.proc.Mp()) 90 } 91 require.Equal(t, int64(0), tc.proc.Mp().CurrNB()) 92 } 93 } 94 95 func BenchmarkJoin(b *testing.B) { 96 for i := 0; i < b.N; i++ { 97 tcs = []joinTestCase{ 98 newTestCase([]bool{false}, []types.Type{{Oid: types.T_int8}}, []int32{0}), 99 newTestCase([]bool{true}, []types.Type{{Oid: types.T_int8}}, []int32{0}), 100 } 101 t := new(testing.T) 102 for _, tc := range tcs { 103 bat := hashBuild(t, tc) 104 err := Prepare(tc.proc, tc.arg) 105 require.NoError(t, err) 106 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(t, tc.flgs, tc.types, tc.proc, Rows) 107 tc.proc.Reg.MergeReceivers[0].Ch <- &batch.Batch{} 108 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(t, tc.flgs, tc.types, tc.proc, Rows) 109 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(t, tc.flgs, tc.types, tc.proc, Rows) 110 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(t, tc.flgs, tc.types, tc.proc, Rows) 111 tc.proc.Reg.MergeReceivers[0].Ch <- nil 112 tc.proc.Reg.MergeReceivers[1].Ch <- bat 113 for { 114 if ok, err := Call(0, tc.proc, tc.arg, false, false); ok || err != nil { 115 break 116 } 117 tc.proc.Reg.InputBatch.Clean(tc.proc.Mp()) 118 } 119 } 120 } 121 } 122 123 func newTestCase(flgs []bool, ts []types.Type, rp []int32) joinTestCase { 124 proc := testutil.NewProcessWithMPool(mpool.MustNewZero()) 125 proc.Reg.MergeReceivers = make([]*process.WaitRegister, 2) 126 ctx, cancel := context.WithCancel(context.Background()) 127 proc.Reg.MergeReceivers[0] = &process.WaitRegister{ 128 Ctx: ctx, 129 Ch: make(chan *batch.Batch, 10), 130 } 131 proc.Reg.MergeReceivers[1] = &process.WaitRegister{ 132 Ctx: ctx, 133 Ch: make(chan *batch.Batch, 4), 134 } 135 fid := function.EncodeOverloadID(function.EQUAL, 4) 136 args := make([]*plan.Expr, 0, 2) 137 args = append(args, &plan.Expr{ 138 Typ: &plan.Type{ 139 Size: ts[0].Size, 140 Id: int32(ts[0].Oid), 141 }, 142 Expr: &plan.Expr_Col{ 143 Col: &plan.ColRef{ 144 RelPos: 0, 145 ColPos: 0, 146 }, 147 }, 148 }) 149 args = append(args, &plan.Expr{ 150 Typ: &plan.Type{ 151 Size: ts[0].Size, 152 Id: int32(ts[0].Oid), 153 }, 154 Expr: &plan.Expr_Col{ 155 Col: &plan.ColRef{ 156 RelPos: 1, 157 ColPos: 0, 158 }, 159 }, 160 }) 161 cond := &plan.Expr{ 162 Typ: &plan.Type{ 163 Size: 1, 164 Id: int32(types.T_bool), 165 }, 166 Expr: &plan.Expr_F{ 167 F: &plan.Function{ 168 Args: args, 169 Func: &plan.ObjectRef{Obj: fid, ObjName: "="}, 170 }, 171 }, 172 } 173 return joinTestCase{ 174 types: ts, 175 flgs: flgs, 176 proc: proc, 177 cancel: cancel, 178 arg: &Argument{ 179 Typs: ts, 180 Cond: cond, 181 Result: rp, 182 }, 183 barg: &hashbuild.Argument{ 184 Typs: ts, 185 }, 186 } 187 } 188 189 func hashBuild(t *testing.T, tc joinTestCase) *batch.Batch { 190 err := hashbuild.Prepare(tc.proc, tc.barg) 191 require.NoError(t, err) 192 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(t, tc.flgs, tc.types, tc.proc, Rows) 193 tc.proc.Reg.MergeReceivers[0].Ch <- nil 194 ok, err := hashbuild.Call(0, tc.proc, tc.barg, false, false) 195 require.NoError(t, err) 196 require.Equal(t, true, ok) 197 return tc.proc.Reg.InputBatch 198 } 199 200 // create a new block based on the type information, flgs[i] == ture: has null 201 func newBatch(t *testing.T, flgs []bool, ts []types.Type, proc *process.Process, rows int64) *batch.Batch { 202 return testutil.NewBatch(ts, false, int(rows), proc.Mp()) 203 }