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