github.com/matrixorigin/matrixone@v0.7.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/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  )
    33  
    34  // add unit tests for cases
    35  type projectionTestCase struct {
    36  	arg   *Argument
    37  	types []types.Type
    38  	proc  *process.Process
    39  }
    40  
    41  var (
    42  	tcs []projectionTestCase
    43  )
    44  
    45  func init() {
    46  	tcs = []projectionTestCase{
    47  		{
    48  			proc: testutil.NewProcessWithMPool(mpool.MustNewZero()),
    49  			types: []types.Type{
    50  				{Oid: types.T_int8},
    51  			},
    52  			arg: &Argument{
    53  				Es: []*plan.Expr{
    54  					{Expr: &plan.Expr_Col{Col: &plan.ColRef{ColPos: 0}}},
    55  				},
    56  			},
    57  		},
    58  	}
    59  }
    60  
    61  func TestString(t *testing.T) {
    62  	buf := new(bytes.Buffer)
    63  	for _, tc := range tcs {
    64  		String(tc.arg, buf)
    65  	}
    66  }
    67  
    68  func TestPrepare(t *testing.T) {
    69  	for _, tc := range tcs {
    70  		err := Prepare(tc.proc, tc.arg)
    71  		require.NoError(t, err)
    72  	}
    73  }
    74  
    75  func TestProjection(t *testing.T) {
    76  	for _, tc := range tcs {
    77  		err := Prepare(tc.proc, tc.arg)
    78  		require.NoError(t, err)
    79  		tc.proc.Reg.InputBatch = newBatch(t, tc.types, tc.proc, Rows)
    80  		_, _ = Call(0, tc.proc, tc.arg, false, false)
    81  		if tc.proc.Reg.InputBatch != nil {
    82  			tc.proc.Reg.InputBatch.Clean(tc.proc.Mp())
    83  		}
    84  		tc.proc.Reg.InputBatch = newBatch(t, tc.types, tc.proc, Rows)
    85  		_, _ = Call(0, tc.proc, tc.arg, false, false)
    86  		if tc.proc.Reg.InputBatch != nil {
    87  			tc.proc.Reg.InputBatch.Clean(tc.proc.Mp())
    88  		}
    89  		tc.proc.Reg.InputBatch = &batch.Batch{}
    90  		_, _ = Call(0, tc.proc, tc.arg, false, false)
    91  		tc.proc.Reg.InputBatch = nil
    92  		_, _ = Call(0, tc.proc, tc.arg, false, false)
    93  		require.Equal(t, int64(0), tc.proc.Mp().CurrNB())
    94  	}
    95  }
    96  
    97  // create a new block based on the type information
    98  func newBatch(t *testing.T, ts []types.Type, proc *process.Process, rows int64) *batch.Batch {
    99  	return testutil.NewBatch(ts, false, int(rows), proc.Mp())
   100  }