github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/limit/limit_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 limit
    16  
    17  import (
    18  	"bytes"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    22  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  	"github.com/matrixorigin/matrixone/pkg/sql/colexec/value_scan"
    25  	"github.com/matrixorigin/matrixone/pkg/testutil"
    26  	"github.com/matrixorigin/matrixone/pkg/vm"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  const (
    32  	Rows          = 10      // default rows
    33  	BenchmarkRows = 1000000 // default rows for benchmark
    34  )
    35  
    36  // add unit tests for cases
    37  type limitTestCase struct {
    38  	arg   *Argument
    39  	types []types.Type
    40  	proc  *process.Process
    41  }
    42  
    43  var (
    44  	tcs []limitTestCase
    45  )
    46  
    47  func init() {
    48  	tcs = []limitTestCase{
    49  		{
    50  			proc: testutil.NewProcessWithMPool(mpool.MustNewZero()),
    51  			types: []types.Type{
    52  				types.T_int8.ToType(),
    53  			},
    54  			arg: &Argument{
    55  				Seen:  0,
    56  				Limit: 8,
    57  				OperatorBase: vm.OperatorBase{
    58  					OperatorInfo: vm.OperatorInfo{
    59  						Idx:     0,
    60  						IsFirst: false,
    61  						IsLast:  false,
    62  					},
    63  				},
    64  			},
    65  		},
    66  		{
    67  			proc: testutil.NewProcessWithMPool(mpool.MustNewZero()),
    68  			types: []types.Type{
    69  				types.T_int8.ToType(),
    70  			},
    71  			arg: &Argument{
    72  				Seen:  0,
    73  				Limit: 10,
    74  				OperatorBase: vm.OperatorBase{
    75  					OperatorInfo: vm.OperatorInfo{
    76  						Idx:     0,
    77  						IsFirst: false,
    78  						IsLast:  false,
    79  					},
    80  				},
    81  			},
    82  		},
    83  		{
    84  			proc: testutil.NewProcessWithMPool(mpool.MustNewZero()),
    85  			types: []types.Type{
    86  				types.T_int8.ToType(),
    87  			},
    88  			arg: &Argument{
    89  				Seen:  0,
    90  				Limit: 12,
    91  				OperatorBase: vm.OperatorBase{
    92  					OperatorInfo: vm.OperatorInfo{
    93  						Idx:     0,
    94  						IsFirst: false,
    95  						IsLast:  false,
    96  					},
    97  				},
    98  			},
    99  		},
   100  	}
   101  }
   102  
   103  func TestString(t *testing.T) {
   104  	buf := new(bytes.Buffer)
   105  	for _, tc := range tcs {
   106  		tc.arg.String(buf)
   107  	}
   108  }
   109  
   110  func TestPrepare(t *testing.T) {
   111  	for _, tc := range tcs {
   112  		err := tc.arg.Prepare(tc.proc)
   113  		require.NoError(t, err)
   114  	}
   115  }
   116  
   117  func TestLimit(t *testing.T) {
   118  	for _, tc := range tcs {
   119  		err := tc.arg.Prepare(tc.proc)
   120  		require.NoError(t, err)
   121  
   122  		bats := []*batch.Batch{
   123  			newBatch(tc.types, tc.proc, Rows),
   124  			newBatch(tc.types, tc.proc, Rows),
   125  			batch.EmptyBatch,
   126  		}
   127  		resetChildren(tc.arg, bats)
   128  		_, _ = tc.arg.Call(tc.proc)
   129  		tc.arg.Free(tc.proc, false, nil)
   130  		tc.arg.GetChildren(0).Free(tc.proc, false, nil)
   131  		tc.proc.FreeVectors()
   132  		require.Equal(t, int64(0), tc.proc.Mp().CurrNB())
   133  	}
   134  }
   135  
   136  func BenchmarkLimit(b *testing.B) {
   137  	for i := 0; i < b.N; i++ {
   138  		tcs = []limitTestCase{
   139  			{
   140  				proc: testutil.NewProcessWithMPool(mpool.MustNewZero()),
   141  				types: []types.Type{
   142  					types.T_int8.ToType(),
   143  				},
   144  				arg: &Argument{
   145  					Seen:  0,
   146  					Limit: 8,
   147  				},
   148  			},
   149  		}
   150  
   151  		t := new(testing.T)
   152  		for _, tc := range tcs {
   153  			err := tc.arg.Prepare(tc.proc)
   154  			require.NoError(t, err)
   155  
   156  			bats := []*batch.Batch{
   157  				newBatch(tc.types, tc.proc, BenchmarkRows),
   158  				batch.EmptyBatch,
   159  			}
   160  			resetChildren(tc.arg, bats)
   161  			_, _ = tc.arg.Call(tc.proc)
   162  			tc.arg.Free(tc.proc, false, nil)
   163  		}
   164  	}
   165  }
   166  
   167  // create a new block based on the type information
   168  func newBatch(ts []types.Type, proc *process.Process, rows int64) *batch.Batch {
   169  	return testutil.NewBatch(ts, false, int(rows), proc.Mp())
   170  }
   171  
   172  func resetChildren(arg *Argument, bats []*batch.Batch) {
   173  	arg.SetChildren(
   174  		[]vm.Operator{
   175  			&value_scan.Argument{
   176  				Batchs: bats,
   177  			},
   178  		})
   179  }