github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/projection/projection_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 projection
    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  )
    35  
    36  // add unit tests for cases
    37  type projectionTestCase struct {
    38  	arg   *Argument
    39  	types []types.Type
    40  	proc  *process.Process
    41  }
    42  
    43  var (
    44  	tcs []projectionTestCase
    45  )
    46  
    47  func init() {
    48  	tcs = []projectionTestCase{
    49  		{
    50  			proc: testutil.NewProcessWithMPool(mpool.MustNewZero()),
    51  			types: []types.Type{
    52  				types.T_int8.ToType(),
    53  			},
    54  			arg: &Argument{
    55  				Es: []*plan.Expr{
    56  					{
    57  						Expr: &plan.Expr_Col{Col: &plan.ColRef{ColPos: 0}},
    58  						Typ: plan.Type{
    59  							Id: int32(types.T_int8),
    60  						},
    61  					},
    62  				},
    63  				OperatorBase: vm.OperatorBase{
    64  					OperatorInfo: vm.OperatorInfo{
    65  						Idx:     0,
    66  						IsFirst: false,
    67  						IsLast:  false,
    68  					},
    69  				},
    70  			},
    71  		},
    72  	}
    73  }
    74  
    75  func TestString(t *testing.T) {
    76  	buf := new(bytes.Buffer)
    77  	for _, tc := range tcs {
    78  		tc.arg.String(buf)
    79  	}
    80  }
    81  
    82  func TestPrepare(t *testing.T) {
    83  	for _, tc := range tcs {
    84  		err := tc.arg.Prepare(tc.proc)
    85  		require.NoError(t, err)
    86  	}
    87  }
    88  
    89  func TestProjection(t *testing.T) {
    90  	for _, tc := range tcs {
    91  		err := tc.arg.Prepare(tc.proc)
    92  		require.NoError(t, err)
    93  
    94  		bats := []*batch.Batch{
    95  			newBatch(tc.types, tc.proc, Rows),
    96  			newBatch(tc.types, tc.proc, Rows),
    97  			batch.EmptyBatch,
    98  		}
    99  		resetChildren(tc.arg, bats)
   100  		_, _ = tc.arg.Call(tc.proc)
   101  
   102  		tc.proc.FreeVectors()
   103  		tc.arg.Free(tc.proc, false, nil)
   104  		tc.arg.GetChildren(0).Free(tc.proc, false, nil)
   105  		require.Equal(t, int64(0), tc.proc.Mp().CurrNB())
   106  	}
   107  }
   108  
   109  // create a new block based on the type information
   110  func newBatch(ts []types.Type, proc *process.Process, rows int64) *batch.Batch {
   111  	return testutil.NewBatch(ts, false, int(rows), proc.Mp())
   112  }
   113  
   114  func resetChildren(arg *Argument, bats []*batch.Batch) {
   115  	arg.SetChildren(
   116  		[]vm.Operator{
   117  			&value_scan.Argument{
   118  				Batchs: bats,
   119  			},
   120  		})
   121  }