github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/operatorSet_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 function 16 17 import ( 18 "github.com/matrixorigin/matrixone/pkg/container/types" 19 "github.com/matrixorigin/matrixone/pkg/testutil" 20 "github.com/stretchr/testify/require" 21 "testing" 22 ) 23 24 func Test_Operator_Unary_Tilde(t *testing.T) { 25 proc := testutil.NewProcess() 26 tc := tcTemp{ 27 info: "select unary_tilde(num) with num = 5, -5, null", 28 inputs: []testutil.FunctionTestInput{ 29 testutil.NewFunctionTestInput(types.T_int64.ToType(), 30 []int64{5, -5, 0}, []bool{false, false, true}), 31 }, 32 expect: testutil.NewFunctionTestResult(types.T_uint64.ToType(), false, 33 []uint64{18446744073709551610, 4, 0}, []bool{false, false, true}), 34 } 35 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorUnaryTilde[int64]) 36 succeed, info := tcc.Run() 37 require.True(t, succeed, tc.info, info) 38 } 39 40 func Test_Operator_Unary_Minus(t *testing.T) { 41 proc := testutil.NewProcess() 42 { 43 tc := tcTemp{ 44 info: "select -(num) with num = 5, -5, null", 45 inputs: []testutil.FunctionTestInput{ 46 testutil.NewFunctionTestInput(types.T_int64.ToType(), 47 []int64{5, -5, 0}, []bool{false, false, true}), 48 }, 49 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, 50 []int64{-5, 5, 0}, []bool{false, false, true}), 51 } 52 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorUnaryMinus[int64]) 53 succeed, info := tcc.Run() 54 require.True(t, succeed, tc.info, info) 55 } 56 57 { 58 tc := tcTemp{ 59 info: "select -(decimal64) with num = 123, 234, 345, null", 60 inputs: []testutil.FunctionTestInput{ 61 testutil.NewFunctionTestInput(types.T_decimal64.ToType(), 62 []types.Decimal64{123, 234, 345, 0}, []bool{false, false, false, true}), 63 }, 64 expect: testutil.NewFunctionTestResult(types.T_decimal64.ToType(), false, 65 []types.Decimal64{types.Decimal64(123).Minus(), types.Decimal64(234).Minus(), types.Decimal64(345).Minus(), types.Decimal64(0)}, []bool{false, false, false, true}), 66 } 67 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorUnaryMinusDecimal64) 68 succeed, info := tcc.Run() 69 require.True(t, succeed, tc.info, info) 70 } 71 72 { 73 tc := tcTemp{ 74 info: "select -(decimal128) with num = 123, 234, 345, null", 75 inputs: []testutil.FunctionTestInput{ 76 testutil.NewFunctionTestInput(types.T_decimal128.ToType(), 77 []types.Decimal128{ 78 {B0_63: 123, B64_127: 0}, 79 {B0_63: 234, B64_127: 0}, 80 {B0_63: 345, B64_127: 0}, 81 {B0_63: 0, B64_127: 0}, 82 }, 83 []bool{false, false, false, true}), 84 }, 85 expect: testutil.NewFunctionTestResult(types.T_decimal128.ToType(), false, 86 []types.Decimal128{ 87 types.Decimal128{B0_63: 123, B64_127: 0}.Minus(), 88 types.Decimal128{B0_63: 234, B64_127: 0}.Minus(), 89 types.Decimal128{B0_63: 345, B64_127: 0}.Minus(), 90 {B0_63: 0, B64_127: 0}, 91 }, 92 []bool{false, false, false, true}), 93 } 94 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorUnaryMinusDecimal128) 95 succeed, info := tcc.Run() 96 require.True(t, succeed, tc.info, info) 97 } 98 } 99 100 func Test_Operator_Unary_Plus(t *testing.T) { 101 proc := testutil.NewProcess() 102 { 103 tc := tcTemp{ 104 info: "select +(num) with num = 5, -5, null", 105 inputs: []testutil.FunctionTestInput{ 106 testutil.NewFunctionTestInput(types.T_int64.ToType(), 107 []int64{5, -5, 0}, []bool{false, false, true}), 108 }, 109 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, 110 []int64{5, -5, 0}, []bool{false, false, true}), 111 } 112 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorUnaryPlus[int64]) 113 succeed, info := tcc.Run() 114 require.True(t, succeed, tc.info, info) 115 } 116 } 117 118 func Test_Operator_Is(t *testing.T) { 119 proc := testutil.NewProcess() 120 { 121 tc := tcTemp{ 122 inputs: []testutil.FunctionTestInput{ 123 testutil.NewFunctionTestInput(types.T_bool.ToType(), 124 []bool{true, false, false}, []bool{false, false, true}), 125 testutil.NewFunctionTestConstInput(types.T_bool.ToType(), 126 []bool{true}, nil), 127 }, 128 expect: testutil.NewFunctionTestResult(types.T_bool.ToType(), false, 129 []bool{true, false, false}, nil), 130 } 131 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorOpIs) 132 succeed, info := tcc.Run() 133 require.True(t, succeed, tc.info, info) 134 } 135 136 { 137 tc := tcTemp{ 138 inputs: []testutil.FunctionTestInput{ 139 testutil.NewFunctionTestInput(types.T_bool.ToType(), 140 []bool{true, false, false}, []bool{false, false, true}), 141 testutil.NewFunctionTestConstInput(types.T_bool.ToType(), 142 []bool{false}, nil), 143 }, 144 expect: testutil.NewFunctionTestResult(types.T_bool.ToType(), false, 145 []bool{false, true, false}, nil), 146 } 147 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorOpIs) 148 succeed, info := tcc.Run() 149 require.True(t, succeed, tc.info, info) 150 } 151 } 152 153 func Test_Operator_Is_Not(t *testing.T) { 154 proc := testutil.NewProcess() 155 { 156 tc := tcTemp{ 157 inputs: []testutil.FunctionTestInput{ 158 testutil.NewFunctionTestInput(types.T_bool.ToType(), 159 []bool{true, false, true, false, false, true}, nil), 160 testutil.NewFunctionTestConstInput(types.T_bool.ToType(), 161 []bool{true}, nil), 162 }, 163 expect: testutil.NewFunctionTestResult(types.T_bool.ToType(), false, 164 []bool{false, true, false, true, true, false}, nil), 165 } 166 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorOpIsNot) 167 succeed, info := tcc.Run() 168 require.True(t, succeed, tc.info, info) 169 } 170 171 { 172 tc := tcTemp{ 173 inputs: []testutil.FunctionTestInput{ 174 testutil.NewFunctionTestInput(types.T_bool.ToType(), 175 []bool{true, false, true, false, false, true}, nil), 176 testutil.NewFunctionTestConstInput(types.T_bool.ToType(), 177 []bool{false}, nil), 178 }, 179 expect: testutil.NewFunctionTestResult(types.T_bool.ToType(), false, 180 []bool{true, false, true, false, false, true}, nil), 181 } 182 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorOpIsNot) 183 succeed, info := tcc.Run() 184 require.True(t, succeed, tc.info, info) 185 } 186 187 { 188 tc := tcTemp{ 189 inputs: []testutil.FunctionTestInput{ 190 testutil.NewFunctionTestInput(types.T_bool.ToType(), 191 []bool{false}, []bool{true}), 192 testutil.NewFunctionTestConstInput(types.T_bool.ToType(), 193 []bool{false}, nil), 194 }, 195 expect: testutil.NewFunctionTestResult(types.T_bool.ToType(), false, 196 []bool{true}, nil), 197 } 198 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorOpIsNot) 199 succeed, info := tcc.Run() 200 require.True(t, succeed, tc.info, info) 201 } 202 } 203 204 func Test_Operator_Is_True(t *testing.T) { 205 proc := testutil.NewProcess() 206 { 207 tc := tcTemp{ 208 inputs: []testutil.FunctionTestInput{ 209 testutil.NewFunctionTestInput(types.T_bool.ToType(), 210 []bool{true, false, false}, []bool{false, false, true}), 211 }, 212 expect: testutil.NewFunctionTestResult(types.T_bool.ToType(), false, 213 []bool{true, false, false}, nil), 214 } 215 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorIsTrue) 216 succeed, info := tcc.Run() 217 require.True(t, succeed, tc.info, info) 218 } 219 } 220 221 func Test_Operator_Is_Not_True(t *testing.T) { 222 proc := testutil.NewProcess() 223 { 224 tc := tcTemp{ 225 inputs: []testutil.FunctionTestInput{ 226 testutil.NewFunctionTestInput(types.T_bool.ToType(), 227 []bool{true, false, false}, []bool{false, false, true}), 228 }, 229 expect: testutil.NewFunctionTestResult(types.T_bool.ToType(), false, 230 []bool{false, true, true}, nil), 231 } 232 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorIsNotTrue) 233 succeed, info := tcc.Run() 234 require.True(t, succeed, tc.info, info) 235 } 236 } 237 238 func Test_Operator_Is_False(t *testing.T) { 239 proc := testutil.NewProcess() 240 { 241 tc := tcTemp{ 242 inputs: []testutil.FunctionTestInput{ 243 testutil.NewFunctionTestInput(types.T_bool.ToType(), 244 []bool{true, false, false}, []bool{false, false, true}), 245 }, 246 expect: testutil.NewFunctionTestResult(types.T_bool.ToType(), false, 247 []bool{false, true, false}, nil), 248 } 249 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorIsFalse) 250 succeed, info := tcc.Run() 251 require.True(t, succeed, tc.info, info) 252 } 253 } 254 255 func Test_Operator_Is_Not_False(t *testing.T) { 256 proc := testutil.NewProcess() 257 { 258 tc := tcTemp{ 259 inputs: []testutil.FunctionTestInput{ 260 testutil.NewFunctionTestInput(types.T_bool.ToType(), 261 []bool{true, false, false}, []bool{false, false, true}), 262 }, 263 expect: testutil.NewFunctionTestResult(types.T_bool.ToType(), false, 264 []bool{true, false, true}, nil), 265 } 266 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorIsNotFalse) 267 succeed, info := tcc.Run() 268 require.True(t, succeed, tc.info, info) 269 } 270 } 271 272 func Test_Operator_Is_Null(t *testing.T) { 273 proc := testutil.NewProcess() 274 { 275 tc := tcTemp{ 276 inputs: []testutil.FunctionTestInput{ 277 testutil.NewFunctionTestInput(types.T_bool.ToType(), 278 []bool{true, false, false}, []bool{false, false, true}), 279 }, 280 expect: testutil.NewFunctionTestResult(types.T_bool.ToType(), false, 281 []bool{false, false, true}, nil), 282 } 283 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorOpIsNull) 284 succeed, info := tcc.Run() 285 require.True(t, succeed, tc.info, info) 286 } 287 } 288 289 func Test_Operator_Is_Not_Null(t *testing.T) { 290 proc := testutil.NewProcess() 291 { 292 tc := tcTemp{ 293 inputs: []testutil.FunctionTestInput{ 294 testutil.NewFunctionTestInput(types.T_bool.ToType(), 295 []bool{true, false, false}, []bool{false, false, true}), 296 }, 297 expect: testutil.NewFunctionTestResult(types.T_bool.ToType(), false, 298 []bool{true, true, false}, nil), 299 } 300 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, operatorOpIsNotNull) 301 succeed, info := tcc.Run() 302 require.True(t, succeed, tc.info, info) 303 } 304 } 305 306 func Test_Operator_And(t *testing.T) { 307 proc := testutil.NewProcess() 308 { 309 tc := tcTemp{ 310 inputs: []testutil.FunctionTestInput{ 311 // 3 true + 3 false + 3 null 312 testutil.NewFunctionTestInput(types.T_bool.ToType(), 313 []bool{true, true, true, false, false, false, false, false, false}, 314 []bool{false, false, false, false, false, false, true, true, true}, 315 ), 316 317 // 3 loop of `true, false, null` 318 testutil.NewFunctionTestInput(types.T_bool.ToType(), 319 []bool{true, false, false, true, false, false, true, false, false}, 320 []bool{false, false, true, false, false, true, false, false, true}, 321 ), 322 }, 323 expect: testutil.NewFunctionTestResult( 324 types.T_bool.ToType(), false, 325 []bool{true, false, false, false, false, false, false, false, false}, 326 []bool{false, false, true, false, false, false, true, false, true}), 327 } 328 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, andFn) 329 succeed, info := tcc.Run() 330 require.True(t, succeed, tc.info, info) 331 } 332 } 333 334 func Test_Operator_Or(t *testing.T) { 335 proc := testutil.NewProcess() 336 { 337 tc := tcTemp{ 338 inputs: []testutil.FunctionTestInput{ 339 // 3 true + 3 false + 3 null 340 testutil.NewFunctionTestInput(types.T_bool.ToType(), 341 []bool{true, true, true, false, false, false, false, false, false}, 342 []bool{false, false, false, false, false, false, true, true, true}, 343 ), 344 345 // 3 loop of `true, false, null` 346 testutil.NewFunctionTestInput(types.T_bool.ToType(), 347 []bool{true, false, false, true, false, false, true, false, false}, 348 []bool{false, false, true, false, false, true, false, false, true}, 349 ), 350 }, 351 expect: testutil.NewFunctionTestResult( 352 types.T_bool.ToType(), false, 353 []bool{true, true, true, true, false, false, true, false, false}, 354 []bool{false, false, false, false, false, true, false, true, true}), 355 } 356 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, orFn) 357 succeed, info := tcc.Run() 358 require.True(t, succeed, tc.info, info) 359 } 360 } 361 362 func Test_Operator_Xor(t *testing.T) { 363 proc := testutil.NewProcess() 364 { 365 tc := tcTemp{ 366 inputs: []testutil.FunctionTestInput{ 367 // 3 true + 3 false + 3 null 368 testutil.NewFunctionTestInput(types.T_bool.ToType(), 369 []bool{true, true, true, false, false, false, false, false, false}, 370 []bool{false, false, false, false, false, false, true, true, true}, 371 ), 372 373 // 3 loop of `true, false, null` 374 testutil.NewFunctionTestInput(types.T_bool.ToType(), 375 []bool{true, false, false, true, false, false, true, false, false}, 376 []bool{false, false, true, false, false, true, false, false, true}, 377 ), 378 }, 379 expect: testutil.NewFunctionTestResult( 380 types.T_bool.ToType(), false, 381 []bool{false, true, false, true, false, false, false, false, false}, 382 []bool{false, false, true, false, false, true, true, true, true}), 383 } 384 tcc := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, xorFn) 385 succeed, info := tcc.Run() 386 require.True(t, succeed, tc.info, info) 387 } 388 }