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