github.com/matrixorigin/matrixone@v0.7.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/testutil"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  const (
    30  	Rows          = 10      // default rows
    31  	BenchmarkRows = 1000000 // default rows for benchmark
    32  )
    33  
    34  // add unit tests for cases
    35  type limitTestCase struct {
    36  	arg   *Argument
    37  	types []types.Type
    38  	proc  *process.Process
    39  }
    40  
    41  var (
    42  	tcs []limitTestCase
    43  )
    44  
    45  func init() {
    46  	tcs = []limitTestCase{
    47  		{
    48  			proc: testutil.NewProcessWithMPool(mpool.MustNewZero()),
    49  			types: []types.Type{
    50  				{Oid: types.T_int8},
    51  			},
    52  			arg: &Argument{
    53  				Seen:  0,
    54  				Limit: 8,
    55  			},
    56  		},
    57  		{
    58  			proc: testutil.NewProcessWithMPool(mpool.MustNewZero()),
    59  			types: []types.Type{
    60  				{Oid: types.T_int8},
    61  			},
    62  			arg: &Argument{
    63  				Seen:  0,
    64  				Limit: 10,
    65  			},
    66  		},
    67  		{
    68  			proc: testutil.NewProcessWithMPool(mpool.MustNewZero()),
    69  			types: []types.Type{
    70  				{Oid: types.T_int8},
    71  			},
    72  			arg: &Argument{
    73  				Seen:  0,
    74  				Limit: 12,
    75  			},
    76  		},
    77  	}
    78  }
    79  
    80  func TestString(t *testing.T) {
    81  	buf := new(bytes.Buffer)
    82  	for _, tc := range tcs {
    83  		String(tc.arg, buf)
    84  	}
    85  }
    86  
    87  func TestPrepare(t *testing.T) {
    88  	for _, tc := range tcs {
    89  		err := Prepare(tc.proc, tc.arg)
    90  		require.NoError(t, err)
    91  	}
    92  }
    93  
    94  func TestLimit(t *testing.T) {
    95  	for _, tc := range tcs {
    96  		err := Prepare(tc.proc, tc.arg)
    97  		require.NoError(t, err)
    98  		tc.proc.Reg.InputBatch = newBatch(t, tc.types, tc.proc, Rows)
    99  		_, _ = Call(0, tc.proc, tc.arg, false, false)
   100  		if tc.proc.Reg.InputBatch != nil {
   101  			tc.proc.Reg.InputBatch.Clean(tc.proc.Mp())
   102  		}
   103  		tc.proc.Reg.InputBatch = newBatch(t, tc.types, tc.proc, Rows)
   104  		_, _ = Call(0, tc.proc, tc.arg, false, false)
   105  		if tc.proc.Reg.InputBatch != nil {
   106  			tc.proc.Reg.InputBatch.Clean(tc.proc.Mp())
   107  		}
   108  		tc.proc.Reg.InputBatch = &batch.Batch{}
   109  		_, _ = Call(0, tc.proc, tc.arg, false, false)
   110  		tc.proc.Reg.InputBatch = nil
   111  		_, _ = Call(0, tc.proc, tc.arg, false, false)
   112  		tc.arg.Free(tc.proc, false)
   113  		require.Equal(t, int64(0), tc.proc.Mp().CurrNB())
   114  	}
   115  }
   116  
   117  func BenchmarkLimit(b *testing.B) {
   118  	for i := 0; i < b.N; i++ {
   119  		tcs = []limitTestCase{
   120  			{
   121  				proc: testutil.NewProcessWithMPool(mpool.MustNewZero()),
   122  				types: []types.Type{
   123  					{Oid: types.T_int8},
   124  				},
   125  				arg: &Argument{
   126  					Seen:  0,
   127  					Limit: 8,
   128  				},
   129  			},
   130  		}
   131  
   132  		t := new(testing.T)
   133  		for _, tc := range tcs {
   134  			err := Prepare(tc.proc, tc.arg)
   135  			require.NoError(t, err)
   136  			tc.proc.Reg.InputBatch = newBatch(t, tc.types, tc.proc, BenchmarkRows)
   137  			_, _ = Call(0, tc.proc, tc.arg, false, false)
   138  			if tc.proc.Reg.InputBatch != nil {
   139  				tc.proc.Reg.InputBatch.Clean(tc.proc.Mp())
   140  			}
   141  			tc.proc.Reg.InputBatch = &batch.Batch{}
   142  			_, _ = Call(0, tc.proc, tc.arg, false, false)
   143  			tc.proc.Reg.InputBatch = nil
   144  			_, _ = Call(0, tc.proc, tc.arg, false, false)
   145  		}
   146  	}
   147  }
   148  
   149  // create a new block based on the type information
   150  func newBatch(t *testing.T, ts []types.Type, proc *process.Process, rows int64) *batch.Batch {
   151  	return testutil.NewBatch(ts, false, int(rows), proc.Mp())
   152  }