github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/intersectall/intersectall_test.go (about)

     1  // Copyright 2022 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 intersectall
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    24  	"github.com/matrixorigin/matrixone/pkg/testutil"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  type intersectAllTestCase struct {
    30  	proc   *process.Process
    31  	arg    *Argument
    32  	cancel context.CancelFunc
    33  }
    34  
    35  func TestIntersectAll(t *testing.T) {
    36  	proc := testutil.NewProcess()
    37  	// [2 rows + 2 row, 3 columns] minus [1 row + 2 rows, 3 columns]
    38  	/*
    39  		{1, 2, 3}				{1, 2, 3}	  {1, 2, 3}
    40  		{1, 2, 3} intersect all {4, 5, 6} ==> {1, 2, 3}
    41  		{3, 4, 5}				{1, 2, 3}
    42  		{3, 4, 5}
    43  	*/
    44  	c := newIntersectAllTestCase(
    45  		proc,
    46  		[]*batch.Batch{
    47  			testutil.NewBatchWithVectors(
    48  				[]*vector.Vector{
    49  					testutil.NewVector(2, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 1}),
    50  					testutil.NewVector(2, types.T_int64.ToType(), proc.Mp(), false, []int64{2, 2}),
    51  					testutil.NewVector(2, types.T_int64.ToType(), proc.Mp(), false, []int64{3, 3}),
    52  				}, nil),
    53  			testutil.NewBatchWithVectors(
    54  				[]*vector.Vector{
    55  					testutil.NewVector(2, types.T_int64.ToType(), proc.Mp(), false, []int64{3, 3}),
    56  					testutil.NewVector(2, types.T_int64.ToType(), proc.Mp(), false, []int64{4, 4}),
    57  					testutil.NewVector(2, types.T_int64.ToType(), proc.Mp(), false, []int64{5, 5}),
    58  				}, nil),
    59  		},
    60  		[]*batch.Batch{
    61  			testutil.NewBatchWithVectors(
    62  				[]*vector.Vector{
    63  					testutil.NewVector(1, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 1}),
    64  					testutil.NewVector(1, types.T_int64.ToType(), proc.Mp(), false, []int64{2, 2}),
    65  					testutil.NewVector(1, types.T_int64.ToType(), proc.Mp(), false, []int64{3, 3}),
    66  				}, nil),
    67  			testutil.NewBatchWithVectors(
    68  				[]*vector.Vector{
    69  					testutil.NewVector(1, types.T_int64.ToType(), proc.Mp(), false, []int64{4}),
    70  					testutil.NewVector(1, types.T_int64.ToType(), proc.Mp(), false, []int64{5}),
    71  					testutil.NewVector(1, types.T_int64.ToType(), proc.Mp(), false, []int64{6}),
    72  				}, nil),
    73  		},
    74  	)
    75  	err := Prepare(c.proc, c.arg)
    76  	require.NoError(t, err)
    77  	cnt := 0
    78  	end := false
    79  	for {
    80  		end, err = Call(0, c.proc, c.arg, false, false)
    81  		if end {
    82  			break
    83  		}
    84  		require.NoError(t, err)
    85  		result := c.proc.InputBatch()
    86  		if result != nil && len(result.Zs) != 0 {
    87  			cnt += result.Length()
    88  			require.Equal(t, 3, len(result.Vecs))
    89  			c.proc.InputBatch().Clean(c.proc.Mp())
    90  		} /*else {
    91  			c.proc.InputBatch().Clean(c.proc.Mp())
    92  		}*/
    93  	}
    94  	require.Equal(t, 2, cnt) // 1 row
    95  	c.arg.Free(c.proc, false)
    96  	require.Equal(t, int64(0), c.proc.Mp().CurrNB())
    97  }
    98  
    99  func newIntersectAllTestCase(proc *process.Process, leftBatches, rightBatches []*batch.Batch) intersectAllTestCase {
   100  	ctx, cancel := context.WithCancel(context.Background())
   101  	proc.Reg.MergeReceivers = make([]*process.WaitRegister, 2)
   102  	{
   103  		c := make(chan *batch.Batch, len(leftBatches)+1)
   104  		for i := range leftBatches {
   105  			c <- leftBatches[i]
   106  		}
   107  		c <- nil
   108  		proc.Reg.MergeReceivers[0] = &process.WaitRegister{
   109  			Ctx: ctx,
   110  			Ch:  c,
   111  		}
   112  	}
   113  	{
   114  		c := make(chan *batch.Batch, len(rightBatches)+1)
   115  		for i := range rightBatches {
   116  			c <- rightBatches[i]
   117  		}
   118  		c <- nil
   119  		proc.Reg.MergeReceivers[1] = &process.WaitRegister{
   120  			Ctx: ctx,
   121  			Ch:  c,
   122  		}
   123  	}
   124  	arg := new(Argument)
   125  	return intersectAllTestCase{
   126  		proc:   proc,
   127  		arg:    arg,
   128  		cancel: cancel,
   129  	}
   130  }