github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/mark/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 mark 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/nulls" 25 "github.com/matrixorigin/matrixone/pkg/container/types" 26 "github.com/matrixorigin/matrixone/pkg/pb/plan" 27 "github.com/matrixorigin/matrixone/pkg/sql/colexec/hashbuild" 28 "github.com/matrixorigin/matrixone/pkg/sql/plan/function" 29 "github.com/matrixorigin/matrixone/pkg/testutil" 30 "github.com/matrixorigin/matrixone/pkg/vm" 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 markTestCase 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 []markTestCase 52 ) 53 54 func init() { 55 tcs = []markTestCase{ 56 newTestCase([]bool{false}, []types.Type{types.T_int8.ToType()}, []int32{0}, 57 [][]*plan.Expr{ 58 { 59 newExpr(0, types.T_int8.ToType()), 60 }, 61 { 62 newExpr(0, types.T_int8.ToType()), 63 }, 64 }), 65 newTestCase([]bool{true}, []types.Type{types.T_int8.ToType()}, []int32{0}, 66 [][]*plan.Expr{ 67 { 68 newExpr(0, types.T_int8.ToType()), 69 }, 70 { 71 newExpr(0, types.T_int8.ToType()), 72 }, 73 }), 74 } 75 } 76 77 func TestString(t *testing.T) { 78 buf := new(bytes.Buffer) 79 for _, tc := range tcs { 80 tc.arg.String(buf) 81 } 82 } 83 84 func TestMark(t *testing.T) { 85 /* XXX There are some problems with the mark join code for handling null. Modify later 86 for _, tc := range tcs { 87 bat := hashBuild(t, tc) 88 err := tc.arg.Prepare(tc.proc) 89 require.NoError(t, err) 90 batWithNull := newBatch( tc.types, tc.proc, Rows) 91 batWithNull.Vecs[0].GetNulls().Np.Add(0) 92 tc.proc.Reg.MergeReceivers[0].Ch <- batWithNull 93 tc.proc.Reg.MergeReceivers[0].Ch <- batch.EmptyBatch 94 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch( tc.types, tc.proc, Rows) 95 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch( tc.types, tc.proc, Rows) 96 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch( tc.types, tc.proc, Rows) 97 tc.proc.Reg.MergeReceivers[0].Ch <- nil 98 tc.proc.Reg.MergeReceivers[1].Ch <- bat 99 for { 100 ok, err := tc.arg.Call(tc.proc) 101 if ok.Status == vm.ExecStop || err != nil { 102 cleanResult(&ok, tc.proc) 103 break 104 } 105 cleanResult(&ok, tc.proc) 106 } 107 require.Equal(t, int64(0), tc.proc.Mp().CurrNB()) 108 } 109 */ 110 for _, tc := range tcs { 111 err := tc.arg.Prepare(tc.proc) 112 require.NoError(t, err) 113 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows) 114 tc.proc.Reg.MergeReceivers[0].Ch <- batch.EmptyBatch 115 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows) 116 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows) 117 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows) 118 tc.proc.Reg.MergeReceivers[0].Ch <- nil 119 tc.proc.Reg.MergeReceivers[1].Ch <- batch.EmptyBatch 120 tc.proc.Reg.MergeReceivers[1].Ch <- nil 121 for { 122 ok, err := tc.arg.Call(tc.proc) 123 if ok.Status == vm.ExecStop || err != nil { 124 break 125 } 126 } 127 tc.proc.FreeVectors() 128 tc.arg.Free(tc.proc, false, nil) 129 require.Equal(t, int64(0), tc.proc.Mp().CurrNB()) 130 } 131 } 132 133 func TestHandleResultType(t *testing.T) { 134 ctr := new(container) 135 ctr.markVals = make([]bool, 3) 136 ctr.markNulls = nulls.NewWithSize(3) 137 ctr.handleResultType(0, condTrue) 138 ctr.handleResultType(1, condFalse) 139 ctr.handleResultType(2, condUnkown) 140 } 141 142 func BenchmarkMark(b *testing.B) { 143 for i := 0; i < b.N; i++ { 144 tcs = []markTestCase{ 145 newTestCase([]bool{false}, []types.Type{types.T_int8.ToType()}, []int32{0}, 146 [][]*plan.Expr{ 147 { 148 newExpr(0, types.T_int8.ToType()), 149 }, 150 { 151 newExpr(0, types.T_int8.ToType()), 152 }, 153 }), 154 newTestCase([]bool{true}, []types.Type{types.T_int8.ToType()}, []int32{0}, 155 [][]*plan.Expr{ 156 { 157 newExpr(0, types.T_int8.ToType()), 158 }, 159 { 160 newExpr(0, types.T_int8.ToType()), 161 }, 162 }), 163 } 164 t := new(testing.T) 165 for _, tc := range tcs { 166 bats := hashBuild(t, tc) 167 err := tc.arg.Prepare(tc.proc) 168 require.NoError(t, err) 169 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows) 170 tc.proc.Reg.MergeReceivers[0].Ch <- batch.EmptyBatch 171 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows) 172 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows) 173 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows) 174 tc.proc.Reg.MergeReceivers[0].Ch <- nil 175 tc.proc.Reg.MergeReceivers[1].Ch <- bats[0] 176 tc.proc.Reg.MergeReceivers[1].Ch <- bats[1] 177 for { 178 ok, err := tc.arg.Call(tc.proc) 179 if ok.Status == vm.ExecStop || err != nil { 180 break 181 } 182 } 183 } 184 } 185 } 186 187 func newExpr(pos int32, typ types.Type) *plan.Expr { 188 return &plan.Expr{ 189 Typ: plan.Type{ 190 Scale: typ.Scale, 191 Width: typ.Width, 192 Id: int32(typ.Oid), 193 }, 194 Expr: &plan.Expr_Col{ 195 Col: &plan.ColRef{ 196 ColPos: pos, 197 }, 198 }, 199 } 200 } 201 202 func newTestCase(flgs []bool, ts []types.Type, rp []int32, cs [][]*plan.Expr) markTestCase { 203 proc := testutil.NewProcessWithMPool(mpool.MustNewZero()) 204 proc.Reg.MergeReceivers = make([]*process.WaitRegister, 2) 205 ctx, cancel := context.WithCancel(context.Background()) 206 proc.Reg.MergeReceivers[0] = &process.WaitRegister{ 207 Ctx: ctx, 208 Ch: make(chan *batch.Batch, 10), 209 } 210 proc.Reg.MergeReceivers[1] = &process.WaitRegister{ 211 Ctx: ctx, 212 Ch: make(chan *batch.Batch, 3), 213 } 214 fr, _ := function.GetFunctionByName(ctx, "=", ts) 215 fid := fr.GetEncodedOverloadID() 216 args := make([]*plan.Expr, 0, 2) 217 args = append(args, &plan.Expr{ 218 Typ: plan.Type{ 219 Id: int32(ts[0].Oid), 220 }, 221 Expr: &plan.Expr_Col{ 222 Col: &plan.ColRef{ 223 RelPos: 0, 224 ColPos: 0, 225 }, 226 }, 227 }) 228 args = append(args, &plan.Expr{ 229 Typ: plan.Type{ 230 Id: int32(ts[0].Oid), 231 }, 232 Expr: &plan.Expr_Col{ 233 Col: &plan.ColRef{ 234 RelPos: 1, 235 ColPos: 0, 236 }, 237 }, 238 }) 239 cond := &plan.Expr{ 240 Typ: plan.Type{ 241 Id: int32(types.T_bool), 242 }, 243 Expr: &plan.Expr_F{ 244 F: &plan.Function{ 245 Args: args, 246 Func: &plan.ObjectRef{Obj: fid, ObjName: "="}, 247 }, 248 }, 249 } 250 251 c := markTestCase{ 252 types: ts, 253 flgs: flgs, 254 proc: proc, 255 cancel: cancel, 256 arg: &Argument{ 257 Typs: ts, 258 Result: rp, 259 Conditions: cs, 260 Cond: cond, 261 OnList: []*plan.Expr{cond}, 262 OperatorBase: vm.OperatorBase{ 263 OperatorInfo: vm.OperatorInfo{ 264 Idx: 0, 265 IsFirst: false, 266 IsLast: false, 267 }, 268 }, 269 }, 270 barg: &hashbuild.Argument{ 271 Typs: ts, 272 NeedHashMap: true, 273 Conditions: cs[1], 274 OperatorBase: vm.OperatorBase{ 275 OperatorInfo: vm.OperatorInfo{ 276 Idx: 0, 277 IsFirst: false, 278 IsLast: false, 279 }, 280 }, 281 }, 282 } 283 return c 284 } 285 286 func hashBuild(t *testing.T, tc markTestCase) []*batch.Batch { 287 err := tc.barg.Prepare(tc.proc) 288 require.NoError(t, err) 289 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows) 290 for _, r := range tc.proc.Reg.MergeReceivers { 291 r.Ch <- nil 292 } 293 ok1, err := tc.barg.Call(tc.proc) 294 require.NoError(t, err) 295 require.Equal(t, false, ok1.Status == vm.ExecStop) 296 ok2, err := tc.barg.Call(tc.proc) 297 require.NoError(t, err) 298 require.Equal(t, false, ok2.Status == vm.ExecStop) 299 return []*batch.Batch{ok1.Batch, ok2.Batch} 300 } 301 302 // create a new block based on the type information, flgs[i] == ture: has null 303 func newBatch(ts []types.Type, proc *process.Process, rows int64) *batch.Batch { 304 return testutil.NewBatch(ts, true, int(rows), proc.Mp()) 305 }