github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/offset/offset_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 offset
    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 offsetTestCase struct {
    36  	arg   *Argument
    37  	types []types.Type
    38  	proc  *process.Process
    39  }
    40  
    41  var (
    42  	tcs []offsetTestCase
    43  )
    44  
    45  func init() {
    46  	tcs = []offsetTestCase{
    47  		{
    48  			proc: testutil.NewProcessWithMPool(mpool.MustNewZero()),
    49  			types: []types.Type{
    50  				{Oid: types.T_int8},
    51  			},
    52  			arg: &Argument{
    53  				Seen:   0,
    54  				Offset: 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  				Offset: 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  				Offset: 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 TestOffset(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  		require.Equal(t, int64(0), tc.proc.Mp().CurrNB())
   113  	}
   114  }
   115  
   116  func BenchmarkOffset(b *testing.B) {
   117  	for i := 0; i < b.N; i++ {
   118  		tcs = []offsetTestCase{
   119  			{
   120  				proc: testutil.NewProcessWithMPool(mpool.MustNewZero()),
   121  				types: []types.Type{
   122  					{Oid: types.T_int8},
   123  				},
   124  				arg: &Argument{
   125  					Seen:   0,
   126  					Offset: 8,
   127  				},
   128  			},
   129  		}
   130  
   131  		t := new(testing.T)
   132  		for _, tc := range tcs {
   133  			err := Prepare(tc.proc, tc.arg)
   134  			require.NoError(t, err)
   135  			tc.proc.Reg.InputBatch = newBatch(t, tc.types, tc.proc, BenchmarkRows)
   136  			_, _ = Call(0, tc.proc, tc.arg, false, false)
   137  			if tc.proc.Reg.InputBatch != nil {
   138  				tc.proc.Reg.InputBatch.Clean(tc.proc.Mp())
   139  			}
   140  			tc.proc.Reg.InputBatch = &batch.Batch{}
   141  			_, _ = Call(0, tc.proc, tc.arg, false, false)
   142  			tc.proc.Reg.InputBatch = nil
   143  			_, _ = Call(0, tc.proc, tc.arg, false, false)
   144  		}
   145  	}
   146  }
   147  
   148  // create a new block based on the type information
   149  func newBatch(t *testing.T, ts []types.Type, proc *process.Process, rows int64) *batch.Batch {
   150  	return testutil.NewBatch(ts, false, int(rows), proc.Mp())
   151  }