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