github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/product/product_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 product
    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/sql/colexec"
    26  	"github.com/matrixorigin/matrixone/pkg/sql/colexec/hashbuild"
    27  	"github.com/matrixorigin/matrixone/pkg/testutil"
    28  	"github.com/matrixorigin/matrixone/pkg/vm"
    29  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  const (
    34  	Rows          = 10     // default rows
    35  	BenchmarkRows = 100000 // default rows for benchmark
    36  )
    37  
    38  // add unit tests for cases
    39  type productTestCase struct {
    40  	arg    *Argument
    41  	flgs   []bool // flgs[i] == true: nullable
    42  	types  []types.Type
    43  	proc   *process.Process
    44  	cancel context.CancelFunc
    45  	barg   *hashbuild.Argument
    46  }
    47  
    48  var (
    49  	tcs []productTestCase
    50  )
    51  
    52  func init() {
    53  	tcs = []productTestCase{
    54  		newTestCase([]bool{false}, []types.Type{types.T_int8.ToType()}, []colexec.ResultPos{colexec.NewResultPos(0, 0), colexec.NewResultPos(1, 0)}),
    55  		newTestCase([]bool{true}, []types.Type{types.T_int8.ToType()}, []colexec.ResultPos{colexec.NewResultPos(0, 0), colexec.NewResultPos(1, 0)}),
    56  	}
    57  }
    58  
    59  func TestString(t *testing.T) {
    60  	buf := new(bytes.Buffer)
    61  	for _, tc := range tcs {
    62  		tc.arg.String(buf)
    63  	}
    64  }
    65  
    66  func TestPrepare(t *testing.T) {
    67  	for _, tc := range tcs {
    68  		err := tc.arg.Prepare(tc.proc)
    69  		require.NoError(t, err)
    70  	}
    71  }
    72  
    73  func TestProduct(t *testing.T) {
    74  	for _, tc := range tcs {
    75  		bats := hashBuild(t, tc)
    76  		err := tc.arg.Prepare(tc.proc)
    77  		require.NoError(t, err)
    78  		tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows)
    79  		tc.proc.Reg.MergeReceivers[0].Ch <- batch.EmptyBatch
    80  		tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows)
    81  		tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows)
    82  		tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows)
    83  		tc.proc.Reg.MergeReceivers[0].Ch <- nil
    84  		tc.proc.Reg.MergeReceivers[1].Ch <- bats[1]
    85  		tc.proc.Reg.MergeReceivers[0].Ch <- nil
    86  		tc.proc.Reg.MergeReceivers[1].Ch <- nil
    87  		for {
    88  			ok, err := tc.arg.Call(tc.proc)
    89  			if ok.Status == vm.ExecStop || err != nil {
    90  				break
    91  			}
    92  		}
    93  		tc.arg.Free(tc.proc, false, nil)
    94  		tc.proc.FreeVectors()
    95  		require.Equal(t, int64(0), tc.proc.Mp().CurrNB())
    96  	}
    97  }
    98  
    99  func BenchmarkProduct(b *testing.B) {
   100  	for i := 0; i < b.N; i++ {
   101  		tcs = []productTestCase{
   102  			newTestCase([]bool{false}, []types.Type{types.T_int8.ToType()}, []colexec.ResultPos{colexec.NewResultPos(0, 0), colexec.NewResultPos(1, 0)}),
   103  			newTestCase([]bool{true}, []types.Type{types.T_int8.ToType()}, []colexec.ResultPos{colexec.NewResultPos(0, 0), colexec.NewResultPos(1, 0)}),
   104  		}
   105  		t := new(testing.T)
   106  		for _, tc := range tcs {
   107  			bats := hashBuild(t, tc)
   108  			err := tc.arg.Prepare(tc.proc)
   109  			require.NoError(t, err)
   110  			tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows)
   111  			tc.proc.Reg.MergeReceivers[0].Ch <- batch.EmptyBatch
   112  			tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows)
   113  			tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows)
   114  			tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows)
   115  			tc.proc.Reg.MergeReceivers[0].Ch <- nil
   116  			tc.proc.Reg.MergeReceivers[1].Ch <- bats[1]
   117  			for {
   118  				ok, err := tc.arg.Call(tc.proc)
   119  				if ok.Status == vm.ExecStop || err != nil {
   120  					break
   121  				}
   122  			}
   123  		}
   124  	}
   125  }
   126  
   127  func newTestCase(flgs []bool, ts []types.Type, rp []colexec.ResultPos) productTestCase {
   128  	proc := testutil.NewProcessWithMPool(mpool.MustNewZero())
   129  	proc.Reg.MergeReceivers = make([]*process.WaitRegister, 2)
   130  	ctx, cancel := context.WithCancel(context.Background())
   131  	proc.Reg.MergeReceivers[0] = &process.WaitRegister{
   132  		Ctx: ctx,
   133  		Ch:  make(chan *batch.Batch, 10),
   134  	}
   135  	proc.Reg.MergeReceivers[1] = &process.WaitRegister{
   136  		Ctx: ctx,
   137  		Ch:  make(chan *batch.Batch, 10),
   138  	}
   139  	return productTestCase{
   140  		types:  ts,
   141  		flgs:   flgs,
   142  		proc:   proc,
   143  		cancel: cancel,
   144  		arg: &Argument{
   145  			Typs:   ts,
   146  			Result: rp,
   147  			OperatorBase: vm.OperatorBase{
   148  				OperatorInfo: vm.OperatorInfo{
   149  					Idx:     0,
   150  					IsFirst: false,
   151  					IsLast:  false,
   152  				},
   153  			},
   154  		},
   155  		barg: &hashbuild.Argument{
   156  			Typs:            ts,
   157  			NeedMergedBatch: true,
   158  			OperatorBase: vm.OperatorBase{
   159  				OperatorInfo: vm.OperatorInfo{
   160  					Idx:     0,
   161  					IsFirst: false,
   162  					IsLast:  false,
   163  				},
   164  			},
   165  		},
   166  	}
   167  }
   168  
   169  func hashBuild(t *testing.T, tc productTestCase) []*batch.Batch {
   170  	err := tc.barg.Prepare(tc.proc)
   171  	require.NoError(t, err)
   172  	tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows)
   173  	for _, r := range tc.proc.Reg.MergeReceivers {
   174  		r.Ch <- nil
   175  	}
   176  	ok1, err := tc.barg.Call(tc.proc)
   177  	require.NoError(t, err)
   178  	require.Equal(t, false, ok1.Status == vm.ExecStop)
   179  	ok2, err := tc.barg.Call(tc.proc)
   180  	require.NoError(t, err)
   181  	require.Equal(t, false, ok2.Status == vm.ExecStop)
   182  	return []*batch.Batch{ok1.Batch, ok2.Batch}
   183  }
   184  
   185  // create a new block based on the type information, flgs[i] == ture: has null
   186  func newBatch(ts []types.Type, proc *process.Process, rows int64) *batch.Batch {
   187  	return testutil.NewBatch(ts, false, int(rows), proc.Mp())
   188  }