github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/field_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 multi 16 17 import ( 18 "testing" 19 20 "github.com/matrixorigin/matrixone/pkg/container/types" 21 "github.com/matrixorigin/matrixone/pkg/container/vector" 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 arg struct { 28 info string 29 proc *process.Process 30 vs []*vector.Vector 31 match bool // if false, the case shouldn't meet the function requirement 32 err bool 33 expect *vector.Vector 34 } 35 36 func TestFieldNumber(t *testing.T) { 37 testCases := []arg{ 38 { 39 info: "field(null, 1)", proc: testutil.NewProc(), 40 vs: []*vector.Vector{ 41 testutil.MakeScalarNull(types.T_any, 1), 42 testutil.MakeScalarInt64(1, 1), 43 }, 44 match: true, 45 err: false, 46 expect: testutil.MakeScalarUint64(0, 1), 47 }, 48 49 { 50 info: "field(null, 1, 1)", proc: testutil.NewProc(), 51 vs: []*vector.Vector{ 52 testutil.MakeScalarNull(types.T_any, 1), 53 testutil.MakeScalarInt64(1, 1), 54 testutil.MakeScalarInt64(1, 1), 55 }, 56 match: true, 57 err: false, 58 expect: testutil.MakeScalarUint64(0, 1), 59 }, 60 61 { 62 info: "field(1, 1, 2)", proc: testutil.NewProc(), 63 vs: []*vector.Vector{ 64 testutil.MakeScalarInt64(1, 1), 65 testutil.MakeScalarInt64(1, 1), 66 testutil.MakeScalarInt64(2, 1), 67 }, 68 match: true, 69 err: false, 70 expect: testutil.MakeScalarUint64(1, 1), 71 }, 72 73 { 74 info: "field(1, 2, 1)", proc: testutil.NewProc(), 75 vs: []*vector.Vector{ 76 testutil.MakeScalarInt64(1, 1), 77 testutil.MakeScalarInt64(2, 1), 78 testutil.MakeScalarInt64(1, 1), 79 }, 80 match: true, 81 err: false, 82 expect: testutil.MakeScalarUint64(2, 1), 83 }, 84 85 { 86 info: "field(1, 2, 3, 4)", proc: testutil.NewProc(), 87 vs: []*vector.Vector{ 88 testutil.MakeScalarInt64(1, 1), 89 testutil.MakeScalarInt64(2, 1), 90 testutil.MakeScalarInt64(3, 1), 91 testutil.MakeScalarInt64(4, 1), 92 }, 93 match: true, 94 err: false, 95 expect: testutil.MakeScalarUint64(0, 1), 96 }, 97 98 { 99 info: "field(1, null, 1)", proc: testutil.NewProc(), 100 vs: []*vector.Vector{ 101 testutil.MakeScalarInt64(1, 1), 102 testutil.MakeScalarNull(types.T_any, 1), 103 testutil.MakeScalarInt64(1, 1), 104 }, 105 match: true, 106 err: false, 107 expect: testutil.MakeScalarUint64(2, 1), 108 }, 109 } 110 111 for _, tc := range testCases { 112 t.Run(tc.info, func(t *testing.T) { 113 gotV, err := FieldNumber[int64](tc.vs, tc.proc) 114 if err != nil { 115 t.Fatal(err) 116 } 117 require.Equal(t, tc.expect.Col, gotV.Col) 118 }) 119 } 120 } 121 122 func TestFieldString(t *testing.T) { 123 testCases := []arg{ 124 { 125 info: "field(null, 'a')", proc: testutil.NewProc(), 126 vs: []*vector.Vector{ 127 testutil.MakeScalarNull(types.T_any, 1), 128 testutil.MakeScalarVarchar("a", 1), 129 }, 130 match: true, 131 err: false, 132 expect: testutil.MakeScalarUint64(0, 1), 133 }, 134 135 { 136 info: "field(null, 'a', 'a')", proc: testutil.NewProc(), 137 vs: []*vector.Vector{ 138 testutil.MakeScalarNull(types.T_any, 1), 139 testutil.MakeScalarVarchar("a", 1), 140 testutil.MakeScalarVarchar("a", 1), 141 }, 142 match: true, 143 err: false, 144 expect: testutil.MakeScalarUint64(0, 1), 145 }, 146 147 { 148 info: "field('Bb', 'Bb', 'Aa')", proc: testutil.NewProc(), 149 vs: []*vector.Vector{ 150 testutil.MakeScalarVarchar("Bb", 1), 151 testutil.MakeScalarVarchar("Bb", 1), 152 testutil.MakeScalarVarchar("Aa", 1), 153 }, 154 match: true, 155 err: false, 156 expect: testutil.MakeScalarUint64(1, 1), 157 }, 158 159 { 160 info: "field('Bb', 'Aa', 'Bb')", proc: testutil.NewProc(), 161 vs: []*vector.Vector{ 162 testutil.MakeScalarVarchar("Bb", 1), 163 testutil.MakeScalarVarchar("Aa", 1), 164 testutil.MakeScalarVarchar("Bb", 1), 165 }, 166 match: true, 167 err: false, 168 expect: testutil.MakeScalarUint64(2, 1), 169 }, 170 171 { 172 info: "field('Gg', 'Aa', 'Bb', 'Cc', 'Dd', 'Ff')", proc: testutil.NewProc(), 173 vs: []*vector.Vector{ 174 testutil.MakeScalarVarchar("Gg", 1), 175 testutil.MakeScalarVarchar("Aa", 1), 176 testutil.MakeScalarVarchar("Bb", 1), 177 testutil.MakeScalarVarchar("Cc", 1), 178 testutil.MakeScalarVarchar("Dd", 1), 179 testutil.MakeScalarVarchar("Ff", 1), 180 }, 181 match: true, 182 err: false, 183 expect: testutil.MakeScalarUint64(0, 1), 184 }, 185 186 { 187 info: "field('Bb', null, 'Bb')", proc: testutil.NewProc(), 188 vs: []*vector.Vector{ 189 testutil.MakeScalarVarchar("Bb", 1), 190 testutil.MakeScalarNull(types.T_any, 1), 191 testutil.MakeScalarVarchar("Bb", 1), 192 }, 193 match: true, 194 err: false, 195 expect: testutil.MakeScalarUint64(2, 1), 196 }, 197 } 198 199 for _, tc := range testCases { 200 t.Run(tc.info, func(t *testing.T) { 201 gotV, err := FieldString(tc.vs, tc.proc) 202 if err != nil { 203 t.Fatal(err) 204 } 205 require.Equal(t, tc.expect.Col, gotV.Col) 206 }) 207 } 208 }