github.com/matrixorigin/matrixone@v1.2.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/sql/colexec/value_scan"
    26  	"github.com/matrixorigin/matrixone/pkg/testutil"
    27  	"github.com/matrixorigin/matrixone/pkg/vm"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  const (
    33  	Rows          = 10     // default rows
    34  	BenchmarkRows = 100000 // default rows for benchmark
    35  )
    36  
    37  // add unit tests for cases
    38  type orderTestCase struct {
    39  	arg   *Argument
    40  	types []types.Type
    41  	proc  *process.Process
    42  }
    43  
    44  var (
    45  	tcs []orderTestCase
    46  )
    47  
    48  func init() {
    49  	tcs = []orderTestCase{
    50  		newTestCase([]types.Type{types.T_int8.ToType()}, []*plan.OrderBySpec{{Expr: newExpression(0), Flag: 0}}),
    51  		newTestCase([]types.Type{types.T_int8.ToType()}, []*plan.OrderBySpec{{Expr: newExpression(0), Flag: 2}}),
    52  		newTestCase([]types.Type{types.T_int8.ToType(), types.T_int64.ToType()}, []*plan.OrderBySpec{{Expr: newExpression(0), Flag: 0}, {Expr: newExpression(1), Flag: 0}}),
    53  		newTestCase([]types.Type{types.T_int8.ToType(), types.T_int64.ToType()}, []*plan.OrderBySpec{{Expr: newExpression(0), Flag: 2}, {Expr: newExpression(1), Flag: 2}}),
    54  	}
    55  }
    56  
    57  func TestString(t *testing.T) {
    58  	buf := new(bytes.Buffer)
    59  	for _, tc := range tcs {
    60  		tc.arg.String(buf)
    61  	}
    62  }
    63  
    64  func TestPrepare(t *testing.T) {
    65  	for _, tc := range tcs {
    66  		err := tc.arg.Prepare(tc.proc)
    67  		require.NoError(t, err)
    68  	}
    69  }
    70  
    71  func TestOrder(t *testing.T) {
    72  	for _, tc := range tcs {
    73  		err := tc.arg.Prepare(tc.proc)
    74  		require.NoError(t, err)
    75  
    76  		bats := []*batch.Batch{
    77  			newBatch(tc.types, tc.proc, Rows),
    78  			newBatch(tc.types, tc.proc, Rows),
    79  			batch.EmptyBatch,
    80  		}
    81  		resetChildren(tc.arg, bats)
    82  		_, _ = tc.arg.Call(tc.proc)
    83  
    84  		tc.arg.Free(tc.proc, false, nil)
    85  		tc.arg.GetChildren(0).Free(tc.proc, false, nil)
    86  		tc.proc.FreeVectors()
    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{types.T_int8.ToType()}, []*plan.OrderBySpec{{Expr: newExpression(0), Flag: 0}}),
    95  			newTestCase([]types.Type{types.T_int8.ToType()}, []*plan.OrderBySpec{{Expr: newExpression(0), Flag: 2}}),
    96  		}
    97  		t := new(testing.T)
    98  		for _, tc := range tcs {
    99  			err := tc.arg.Prepare(tc.proc)
   100  			require.NoError(t, err)
   101  
   102  			bats := []*batch.Batch{
   103  				newBatch(tc.types, tc.proc, BenchmarkRows),
   104  				newBatch(tc.types, tc.proc, BenchmarkRows),
   105  				batch.EmptyBatch,
   106  			}
   107  			resetChildren(tc.arg, bats)
   108  			_, _ = tc.arg.Call(tc.proc)
   109  			tc.arg.Free(tc.proc, false, nil)
   110  			tc.arg.GetChildren(0).Free(tc.proc, false, nil)
   111  		}
   112  	}
   113  }
   114  
   115  func newTestCase(ts []types.Type, fs []*plan.OrderBySpec) orderTestCase {
   116  	return orderTestCase{
   117  		types: ts,
   118  		proc:  testutil.NewProcessWithMPool(mpool.MustNewZero()),
   119  		arg: &Argument{
   120  			OrderBySpec: fs,
   121  			OperatorBase: vm.OperatorBase{
   122  				OperatorInfo: vm.OperatorInfo{
   123  					Idx:     0,
   124  					IsFirst: false,
   125  					IsLast:  false,
   126  				},
   127  			},
   128  		},
   129  	}
   130  }
   131  
   132  func newExpression(pos int32) *plan.Expr {
   133  	return &plan.Expr{
   134  		Expr: &plan.Expr_Col{
   135  			Col: &plan.ColRef{
   136  				ColPos: pos,
   137  			},
   138  		},
   139  		Typ: plan.Type{
   140  			Id: int32(types.T_int64),
   141  		},
   142  	}
   143  }
   144  
   145  // create a new block based on the type information
   146  func newBatch(ts []types.Type, proc *process.Process, rows int64) *batch.Batch {
   147  	return testutil.NewBatch(ts, false, int(rows), proc.Mp())
   148  }
   149  
   150  func resetChildren(arg *Argument, bats []*batch.Batch) {
   151  	arg.SetChildren(
   152  		[]vm.Operator{
   153  			&value_scan.Argument{
   154  				Batchs: bats,
   155  			},
   156  		})
   157  	arg.ctr.state = vm.Build
   158  }