github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/intersect/intersect_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 intersect
    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 intersectTestCase struct {
    30  	proc   *process.Process
    31  	arg    *Argument
    32  	cancel context.CancelFunc
    33  }
    34  
    35  func TestIntersect(t *testing.T) {
    36  	proc := testutil.NewProcess()
    37  	// [2 rows + 2 row, 3 columns] intersect [1 row + 1 rows, 3 columns]
    38  	/*
    39  		{1, 2, 3}	    {1, 2, 3}
    40  		{1, 2, 3} intersect {4, 5, 6} ==> {1, 2, 3}
    41  		{3, 4, 5}
    42  		{3, 4, 5}
    43  	*/
    44  	c := newIntersectTestCase(
    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}),
    64  					testutil.NewVector(1, types.T_int64.ToType(), proc.Mp(), false, []int64{2}),
    65  					testutil.NewVector(1, types.T_int64.ToType(), proc.Mp(), false, []int64{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  
    76  	err := Prepare(c.proc, c.arg)
    77  	require.NoError(t, err)
    78  	cnt := 0
    79  	end := false
    80  	for {
    81  		end, err = Call(0, c.proc, c.arg, false, false)
    82  		if end {
    83  			break
    84  		}
    85  		require.NoError(t, err)
    86  		result := c.proc.InputBatch()
    87  		if result != nil && len(result.Zs) != 0 {
    88  			cnt += result.Length()
    89  			require.Equal(t, 3, len(result.Vecs)) // 3 column
    90  			c.proc.InputBatch().Clean(c.proc.Mp())
    91  		}
    92  	}
    93  	require.Equal(t, 1, cnt) // 1 row
    94  	c.arg.Free(c.proc, false)
    95  	require.Equal(t, int64(0), c.proc.Mp().CurrNB())
    96  }
    97  
    98  func newIntersectTestCase(proc *process.Process, leftBatches, rightBatches []*batch.Batch) intersectTestCase {
    99  	ctx, cancel := context.WithCancel(context.Background())
   100  	proc.Reg.MergeReceivers = make([]*process.WaitRegister, 2)
   101  	{
   102  		c := make(chan *batch.Batch, len(leftBatches)+10)
   103  		for i := range leftBatches {
   104  			c <- leftBatches[i]
   105  		}
   106  		c <- nil
   107  		proc.Reg.MergeReceivers[0] = &process.WaitRegister{
   108  			Ctx: ctx,
   109  			Ch:  c,
   110  		}
   111  	}
   112  	{
   113  		c := make(chan *batch.Batch, len(rightBatches)+10)
   114  		for i := range rightBatches {
   115  			c <- rightBatches[i]
   116  		}
   117  		c <- nil
   118  		proc.Reg.MergeReceivers[1] = &process.WaitRegister{
   119  			Ctx: ctx,
   120  			Ch:  c,
   121  		}
   122  	}
   123  	arg := new(Argument)
   124  	return intersectTestCase{
   125  		proc:   proc,
   126  		arg:    arg,
   127  		cancel: cancel,
   128  	}
   129  }