github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/multi_col/group_concat/group_concat_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  package group_concat
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    21  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    22  	"github.com/matrixorigin/matrixone/pkg/testutil"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  type GroupConcatTestCase struct {
    28  	proc   *process.Process
    29  	ityp   []types.Type
    30  	arg    *Argument
    31  	vecs   []*vector.Vector
    32  	expect string
    33  }
    34  
    35  func NewTestCases(proc *process.Process) []GroupConcatTestCase {
    36  	return []GroupConcatTestCase{
    37  		{
    38  			// int16 varchar int64
    39  			//   1      a     2
    40  			//   3      b     4		--> 1a2,3b4,1a2
    41  			//   1      a     2
    42  			// dist is false
    43  			proc: proc,
    44  			ityp: []types.Type{types.T_int16.ToType(), types.T_varchar.ToType(), types.T_int64.ToType()},
    45  			arg:  &Argument{Dist: false, Separator: ",", GroupExpr: []*plan.Expr{{}, {}, {}}, OrderByExpr: nil},
    46  			vecs: []*vector.Vector{
    47  				testutil.NewVector(3, types.T_int16.ToType(), proc.Mp(), false, []int16{1, 3, 1}),
    48  				testutil.NewVector(3, types.T_varchar.ToType(), proc.Mp(), false, []string{"a", "b", "a"}),
    49  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{2, 4, 2}),
    50  			},
    51  			expect: "1a2,3b4,1a2",
    52  		},
    53  		{
    54  			// int16 varchar int64
    55  			//   1      a     2
    56  			//   3      b     4		--> 1a2,3b4
    57  			//   1      a     2
    58  			// dist is true
    59  			proc: proc,
    60  			ityp: []types.Type{types.T_int16.ToType(), types.T_varchar.ToType(), types.T_int64.ToType()},
    61  			arg:  &Argument{Dist: true, Separator: ",", GroupExpr: []*plan.Expr{{}, {}, {}}, OrderByExpr: nil},
    62  			vecs: []*vector.Vector{
    63  				testutil.NewVector(3, types.T_int16.ToType(), proc.Mp(), false, []int16{1, 3, 1}),
    64  				testutil.NewVector(3, types.T_varchar.ToType(), proc.Mp(), false, []string{"a", "b", "a"}),
    65  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{2, 4, 2}),
    66  			},
    67  			expect: "1a2,3b4",
    68  		},
    69  		{
    70  			// int16 varchar varchar
    71  			//   1      ab    c
    72  			//   3      b     d		--> 1abc,3bd,1abc
    73  			//   1      a     bc
    74  			// dist is true
    75  			proc: proc,
    76  			ityp: []types.Type{types.T_int16.ToType(), types.T_varchar.ToType(), types.T_varchar.ToType()},
    77  			arg:  &Argument{Dist: true, Separator: ",", GroupExpr: []*plan.Expr{{}, {}, {}}, OrderByExpr: nil},
    78  			vecs: []*vector.Vector{
    79  				testutil.NewVector(3, types.T_int16.ToType(), proc.Mp(), false, []int16{1, 3, 1}),
    80  				testutil.NewVector(3, types.T_varchar.ToType(), proc.Mp(), false, []string{"ab", "b", "a"}),
    81  				testutil.NewVector(3, types.T_varchar.ToType(), proc.Mp(), false, []string{"c", "d", "bc"}),
    82  			},
    83  			expect: "1abc,3bd,1abc",
    84  		},
    85  	}
    86  }
    87  
    88  func TestGroupConcat(t *testing.T) {
    89  	proc := testutil.NewProcess()
    90  	cases := NewTestCases(proc)
    91  	RunTestCases(cases, t, proc)
    92  }
    93  
    94  func RunTestCases(cases []GroupConcatTestCase, t *testing.T, proc *process.Process) {
    95  	for _, case_ := range cases {
    96  		gc := NewGroupConcat(case_.arg, case_.ityp)
    97  		gc.Grows(1, case_.proc.Mp())
    98  		gc.Fill(0, 0, 1, case_.vecs)
    99  		gc.Fill(0, 1, 1, case_.vecs)
   100  		gc.Fill(0, 2, 1, case_.vecs)
   101  		res, _ := gc.Eval(case_.proc.Mp())
   102  		require.Equal(t, case_.expect, res.GetString(0))
   103  		gc.Free(case_.proc.Mp())
   104  		for i := 0; i < len(case_.vecs); i++ {
   105  			case_.vecs[i].Free(case_.proc.Mp())
   106  		}
   107  		res.Free(case_.proc.Mp())
   108  	}
   109  	require.Equal(t, int64(0), proc.Mp().CurrNB())
   110  }