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