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