github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/binary/findinset_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 binary 16 17 import ( 18 "testing" 19 20 "github.com/matrixorigin/matrixone/pkg/common/mpool" 21 "github.com/matrixorigin/matrixone/pkg/container/types" 22 "github.com/matrixorigin/matrixone/pkg/container/vector" 23 "github.com/matrixorigin/matrixone/pkg/testutil" 24 "github.com/matrixorigin/matrixone/pkg/vm/process" 25 "github.com/stretchr/testify/require" 26 ) 27 28 func TestFindInSetLength(t *testing.T) { 29 procs := testutil.NewProc() 30 cases := []struct { 31 name string 32 proc *process.Process 33 left []string 34 right []string 35 isScalarL bool 36 isScalarR bool 37 expected []uint64 38 }{ 39 { 40 name: "normal string test 01", 41 proc: procs, 42 left: []string{"abc"}, 43 right: []string{"abc,def"}, 44 expected: []uint64{1}, 45 isScalarL: false, 46 isScalarR: false, 47 }, 48 { 49 name: "normal string test 02", 50 proc: procs, 51 left: []string{"xyz"}, 52 right: []string{"dec,xyz,abc"}, 53 expected: []uint64{2}, 54 isScalarL: false, 55 isScalarR: false, 56 }, 57 { 58 name: "left scalar test", 59 proc: procs, 60 left: []string{"z"}, 61 right: []string{"a,e,c,z"}, 62 expected: []uint64{4}, 63 isScalarL: false, 64 isScalarR: false, 65 }, 66 { 67 name: "left scalar test", 68 proc: procs, 69 left: []string{"abc"}, 70 right: []string{"abc,def"}, 71 expected: []uint64{1}, 72 isScalarL: true, 73 isScalarR: false, 74 }, 75 { 76 name: "right scalar test", 77 proc: procs, 78 left: []string{"abc"}, 79 right: []string{"abc,def"}, 80 expected: []uint64{1}, 81 isScalarL: false, 82 isScalarR: true, 83 }, 84 { 85 name: "both scalar test", 86 proc: procs, 87 left: []string{"abc"}, 88 right: []string{"abc,def"}, 89 expected: []uint64{1}, 90 isScalarL: true, 91 isScalarR: true, 92 }, 93 { 94 name: "left null test", 95 proc: procs, 96 right: []string{"abc"}, 97 expected: []uint64{0}, 98 isScalarL: true, 99 isScalarR: true, 100 }, 101 { 102 name: "right null test", 103 proc: procs, 104 left: []string{"abc"}, 105 expected: []uint64{0}, 106 isScalarL: true, 107 isScalarR: true, 108 }, 109 } 110 111 for _, c := range cases { 112 t.Run(c.name, func(t *testing.T) { 113 vecs := makeFindInSetTestVectors(c.left, c.right, c.isScalarL, c.isScalarR) 114 result, err := FindInSet(vecs, c.proc) 115 if err != nil { 116 t.Fatal(err) 117 } 118 col := result.Col.([]uint64) 119 require.Equal(t, c.expected, col) 120 require.Equal(t, c.isScalarL && c.isScalarR, result.IsScalar()) 121 }) 122 } 123 } 124 125 func makeFindInSetTestVectors(left []string, right []string, isScalarL bool, isScalarR bool) []*vector.Vector { 126 mp := mpool.MustNewZero() 127 vec := make([]*vector.Vector, 2) 128 if left != nil { 129 if isScalarL { 130 vec[0] = vector.NewConstString(types.T_varchar.ToType(), 1, left[0], mp) 131 } else { 132 vec[0] = testutil.MakeVarcharVector(left, nil) 133 } 134 } else { 135 vec[0] = testutil.MakeScalarNull(types.T_varchar, types.MaxVarcharLen) 136 } 137 138 if right != nil { 139 if isScalarR { 140 vec[1] = vector.NewConstString(types.T_varchar.ToType(), 1, right[0], mp) 141 } else { 142 vec[1] = testutil.MakeVarcharVector(right, nil) 143 } 144 } else { 145 vec[1] = testutil.MakeScalarNull(types.T_varchar, types.MaxVarcharLen) 146 } 147 148 return vec 149 }