github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/func_unary_test.go (about) 1 // Copyright 2021 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 "context" 19 "fmt" 20 "math" 21 "testing" 22 "time" 23 24 "github.com/matrixorigin/matrixone/pkg/sql/plan/function/functionUtil" 25 26 "github.com/stretchr/testify/assert" 27 "github.com/stretchr/testify/require" 28 29 "github.com/matrixorigin/matrixone/pkg/common/moerr" 30 "github.com/matrixorigin/matrixone/pkg/container/types" 31 "github.com/matrixorigin/matrixone/pkg/fileservice" 32 "github.com/matrixorigin/matrixone/pkg/testutil" 33 ) 34 35 type tcTemp struct { 36 typ types.T 37 info string 38 inputs []testutil.FunctionTestInput 39 expect testutil.FunctionTestResult 40 } 41 42 func initAbsTestCase() []tcTemp { 43 return []tcTemp{ 44 { 45 info: "test abs int64", 46 inputs: []testutil.FunctionTestInput{ 47 testutil.NewFunctionTestInput(types.T_int64.ToType(), 48 []int64{-23, 9999999, -11, -99999, 9999999, -11, -99999, 9999999, -11, -99999, 9999999, -11, -99999}, 49 []bool{false, false, false, false, false, false, false, false, false, false, false, false, false}), 50 }, 51 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, 52 []int64{23, 9999999, 11, 99999, 9999999, 11, 99999, 9999999, 11, 99999, 9999999, 11, 99999}, 53 []bool{false, false, false, false, false, false, false, false, false, false, false, false, false}), 54 }, 55 } 56 } 57 58 func TestAbs(t *testing.T) { 59 testCases := initAbsTestCase() 60 61 // do the test work. 62 proc := testutil.NewProcess() 63 for _, tc := range testCases { 64 fcTC := testutil.NewFunctionTestCase(proc, 65 tc.inputs, tc.expect, AbsInt64) 66 s, info := fcTC.Run() 67 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 68 } 69 } 70 71 func BenchmarkAbsInt64(b *testing.B) { 72 testCases := initAbsTestCase() 73 proc := testutil.NewProcess() 74 75 b.StartTimer() 76 for _, tc := range testCases { 77 fcTC := testutil.NewFunctionTestCase(proc, 78 tc.inputs, tc.expect, AbsInt64) 79 _ = fcTC.BenchMarkRun() 80 } 81 b.StopTimer() 82 } 83 84 func initAbsArrayTestCase() []tcTemp { 85 return []tcTemp{ 86 { 87 info: "test abs float32 array", 88 typ: types.T_array_float32, 89 inputs: []testutil.FunctionTestInput{ 90 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), 91 [][]float32{{-4, 9999999, -99999}, {0, -25, 49}}, 92 []bool{false, false}), 93 }, 94 expect: testutil.NewFunctionTestResult(types.T_array_float32.ToType(), false, 95 [][]float32{{4, 9999999, 99999}, {0, 25, 49}}, 96 []bool{false, false}), 97 }, 98 { 99 info: "test abs float64 array", 100 typ: types.T_array_float64, 101 inputs: []testutil.FunctionTestInput{ 102 testutil.NewFunctionTestInput(types.T_array_float64.ToType(), 103 [][]float64{{-4, 9999999, -99999}, {0, -25, 49}}, 104 []bool{false, false}), 105 }, 106 expect: testutil.NewFunctionTestResult(types.T_array_float64.ToType(), false, 107 [][]float64{{4, 9999999, 99999}, {0, 25, 49}}, 108 []bool{false, false}), 109 }, 110 } 111 } 112 113 func TestAbsArray(t *testing.T) { 114 testCases := initAbsArrayTestCase() 115 116 proc := testutil.NewProcess() 117 for _, tc := range testCases { 118 var fcTC testutil.FunctionTestCase 119 switch tc.typ { 120 case types.T_array_float32: 121 fcTC = testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, AbsArray[float32]) 122 case types.T_array_float64: 123 fcTC = testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, AbsArray[float64]) 124 } 125 s, info := fcTC.Run() 126 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 127 } 128 } 129 130 func initNormalizeL2ArrayTestCase() []tcTemp { 131 return []tcTemp{ 132 { 133 info: "test normalize_l2 float32 array", 134 typ: types.T_array_float32, 135 inputs: []testutil.FunctionTestInput{ 136 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), 137 [][]float32{ 138 {}, 139 {1, 2, 3, 4}, 140 {-1, 2, 3, 4}, 141 {10, 3.333333333333333, 4, 5}, 142 {1, 2, 3.6666666666666665, 4.666666666666666}}, 143 []bool{true, false, false, false, false, false}), 144 }, 145 expect: testutil.NewFunctionTestResult(types.T_array_float32.ToType(), false, 146 [][]float32{ 147 {}, 148 {0.18257418, 0.36514837, 0.5477226, 0.73029673}, 149 {-0.18257418, 0.36514837, 0.5477226, 0.73029673}, 150 {0.8108108, 0.27027026, 0.32432434, 0.4054054}, 151 {0.1576765, 0.315353, 0.5781472, 0.73582363}, 152 }, 153 []bool{true, false, false, false, false, false}), 154 }, 155 { 156 info: "test normalize_l2 float64 array", 157 typ: types.T_array_float64, 158 inputs: []testutil.FunctionTestInput{ 159 testutil.NewFunctionTestInput(types.T_array_float64.ToType(), 160 [][]float64{ 161 {}, 162 {1, 2, 3, 4}, 163 {-1, 2, 3, 4}, 164 {10, 3.333333333333333, 4, 5}, 165 {1, 2, 3.6666666666666665, 4.666666666666666}, 166 }, 167 []bool{true, false, false, false, false, false}), 168 }, 169 expect: testutil.NewFunctionTestResult(types.T_array_float64.ToType(), false, 170 [][]float64{ 171 {}, 172 {0.18257418583505536, 0.3651483716701107, 0.5477225575051661, 0.7302967433402214}, 173 {-0.18257418583505536, 0.3651483716701107, 0.5477225575051661, 0.7302967433402214}, 174 {0.8108108108108107, 0.27027027027027023, 0.3243243243243243, 0.4054054054054054}, 175 {0.15767649936829103, 0.31535299873658207, 0.5781471643504004, 0.7358236637186913}, 176 }, 177 []bool{true, false, false, false, false, false}), 178 }, 179 } 180 } 181 182 func TestNormalizeL2Array(t *testing.T) { 183 testCases := initNormalizeL2ArrayTestCase() 184 185 proc := testutil.NewProcess() 186 for _, tc := range testCases { 187 var fcTC testutil.FunctionTestCase 188 switch tc.typ { 189 case types.T_array_float32: 190 fcTC = testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, NormalizeL2Array[float32]) 191 case types.T_array_float64: 192 fcTC = testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, NormalizeL2Array[float64]) 193 } 194 s, info := fcTC.Run() 195 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 196 } 197 } 198 199 func initSummationArrayTestCase() []tcTemp { 200 return []tcTemp{ 201 { 202 info: "test summation float32 array", 203 typ: types.T_array_float32, 204 inputs: []testutil.FunctionTestInput{ 205 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), 206 [][]float32{{1, 2, 3}, {4, 5, 6}}, 207 []bool{false, false}), 208 }, 209 expect: testutil.NewFunctionTestResult(types.T_float64.ToType(), false, 210 []float64{6, 15}, 211 []bool{false, false}), 212 }, 213 { 214 info: "test summation float64 array", 215 typ: types.T_array_float64, 216 inputs: []testutil.FunctionTestInput{ 217 testutil.NewFunctionTestInput(types.T_array_float64.ToType(), 218 [][]float64{{1, 2, 3}, {4, 5, 6}}, 219 []bool{false, false}), 220 }, 221 expect: testutil.NewFunctionTestResult(types.T_float64.ToType(), false, 222 []float64{6, 15}, 223 []bool{false, false}), 224 }, 225 } 226 } 227 228 func TestSummationArray(t *testing.T) { 229 testCases := initSummationArrayTestCase() 230 231 proc := testutil.NewProcess() 232 for _, tc := range testCases { 233 var fcTC testutil.FunctionTestCase 234 switch tc.typ { 235 case types.T_array_float32: 236 fcTC = testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, SummationArray[float32]) 237 case types.T_array_float64: 238 fcTC = testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, SummationArray[float64]) 239 } 240 s, info := fcTC.Run() 241 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 242 } 243 } 244 245 func initL1NormArrayTestCase() []tcTemp { 246 return []tcTemp{ 247 { 248 info: "test L1Norm float32 array", 249 typ: types.T_array_float32, 250 inputs: []testutil.FunctionTestInput{ 251 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), 252 [][]float32{{1, 2, 3}, {4, 5, 6}}, 253 []bool{false, false}), 254 }, 255 expect: testutil.NewFunctionTestResult(types.T_float64.ToType(), false, 256 []float64{6, 15}, 257 []bool{false, false}), 258 }, 259 { 260 info: "test L1Norm float64 array", 261 typ: types.T_array_float64, 262 inputs: []testutil.FunctionTestInput{ 263 testutil.NewFunctionTestInput(types.T_array_float64.ToType(), 264 [][]float64{{1, 2, 3}, {4, 5, 6}}, 265 []bool{false, false}), 266 }, 267 expect: testutil.NewFunctionTestResult(types.T_float64.ToType(), false, 268 []float64{6, 15}, 269 []bool{false, false}), 270 }, 271 } 272 } 273 274 func TestL1NormArray(t *testing.T) { 275 testCases := initL1NormArrayTestCase() 276 277 proc := testutil.NewProcess() 278 for _, tc := range testCases { 279 var fcTC testutil.FunctionTestCase 280 switch tc.typ { 281 case types.T_array_float32: 282 fcTC = testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, L1NormArray[float32]) 283 case types.T_array_float64: 284 fcTC = testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, L1NormArray[float64]) 285 } 286 s, info := fcTC.Run() 287 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 288 } 289 } 290 291 func initL2NormArrayTestCase() []tcTemp { 292 return []tcTemp{ 293 { 294 info: "test L2Norm float32 array", 295 typ: types.T_array_float32, 296 inputs: []testutil.FunctionTestInput{ 297 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), 298 [][]float32{{1, 2, 3}, {4, 5, 6}}, 299 []bool{false, false}), 300 }, 301 expect: testutil.NewFunctionTestResult(types.T_float64.ToType(), false, 302 []float64{3.741657386773941, 8.774964387392123}, 303 []bool{false, false}), 304 }, 305 { 306 info: "test L2Norm float64 array", 307 typ: types.T_array_float64, 308 inputs: []testutil.FunctionTestInput{ 309 testutil.NewFunctionTestInput(types.T_array_float64.ToType(), 310 [][]float64{{1, 2, 3}, {4, 5, 6}}, 311 []bool{false, false}), 312 }, 313 expect: testutil.NewFunctionTestResult(types.T_float64.ToType(), false, 314 []float64{3.741657386773941, 8.774964387392124}, 315 []bool{false, false}), 316 }, 317 } 318 } 319 320 func TestL2NormArray(t *testing.T) { 321 testCases := initL2NormArrayTestCase() 322 323 proc := testutil.NewProcess() 324 for _, tc := range testCases { 325 var fcTC testutil.FunctionTestCase 326 switch tc.typ { 327 case types.T_array_float32: 328 fcTC = testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, L2NormArray[float32]) 329 case types.T_array_float64: 330 fcTC = testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, L2NormArray[float64]) 331 } 332 s, info := fcTC.Run() 333 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 334 } 335 } 336 337 func initSubVectorTestCase() []tcTemp { 338 return []tcTemp{ 339 { 340 info: "2", 341 typ: types.T_array_float32, 342 inputs: []testutil.FunctionTestInput{ 343 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), [][]float32{{1, 2, 3}}, []bool{false}), 344 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{1}, []bool{false}), 345 }, 346 expect: testutil.NewFunctionTestResult(types.T_array_float32.ToType(), false, 347 [][]float32{{1, 2, 3}}, 348 []bool{false}), 349 }, 350 { 351 info: "2", 352 typ: types.T_array_float32, 353 inputs: []testutil.FunctionTestInput{ 354 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), [][]float32{{1, 2, 3}}, []bool{false}), 355 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{2}, []bool{false}), 356 }, 357 expect: testutil.NewFunctionTestResult(types.T_array_float32.ToType(), false, 358 [][]float32{{2, 3}}, 359 []bool{false}), 360 }, 361 { 362 info: "2", 363 typ: types.T_array_float32, 364 inputs: []testutil.FunctionTestInput{ 365 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), [][]float32{{1, 2, 3}}, []bool{false}), 366 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{3}, []bool{false}), 367 }, 368 expect: testutil.NewFunctionTestResult(types.T_array_float32.ToType(), false, 369 [][]float32{{3}}, 370 []bool{false}), 371 }, 372 { 373 info: "2", 374 typ: types.T_array_float32, 375 inputs: []testutil.FunctionTestInput{ 376 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), [][]float32{{1, 2, 3}}, []bool{false}), 377 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{-1}, []bool{false}), 378 }, 379 expect: testutil.NewFunctionTestResult(types.T_array_float32.ToType(), false, 380 [][]float32{{3}}, 381 []bool{false}), 382 }, 383 { 384 info: "2", 385 typ: types.T_array_float32, 386 inputs: []testutil.FunctionTestInput{ 387 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), [][]float32{{1, 2, 3}}, []bool{false}), 388 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{-2}, []bool{false}), 389 }, 390 expect: testutil.NewFunctionTestResult(types.T_array_float32.ToType(), false, 391 [][]float32{{2, 3}}, 392 []bool{false}), 393 }, 394 { 395 info: "2", 396 typ: types.T_array_float32, 397 inputs: []testutil.FunctionTestInput{ 398 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), [][]float32{{1, 2, 3}}, []bool{false}), 399 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{-3}, []bool{false}), 400 }, 401 expect: testutil.NewFunctionTestResult(types.T_array_float32.ToType(), false, 402 [][]float32{{1, 2, 3}}, 403 []bool{false}), 404 }, 405 { 406 info: "2", 407 typ: types.T_array_float32, 408 inputs: []testutil.FunctionTestInput{ 409 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), [][]float32{{1, 2, 3}}, []bool{false}), 410 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{0}, []bool{false}), 411 }, 412 expect: testutil.NewFunctionTestResult(types.T_array_float32.ToType(), false, 413 [][]float32{{}}, 414 []bool{false}), 415 }, 416 { 417 info: "2", 418 typ: types.T_array_float64, 419 inputs: []testutil.FunctionTestInput{ 420 testutil.NewFunctionTestInput(types.T_array_float64.ToType(), [][]float64{{1, 2, 3}}, []bool{false}), 421 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{1}, []bool{false}), 422 }, 423 expect: testutil.NewFunctionTestResult(types.T_array_float64.ToType(), false, 424 [][]float64{{1, 2, 3}}, 425 []bool{false}), 426 }, 427 { 428 info: "3", 429 typ: types.T_array_float32, 430 inputs: []testutil.FunctionTestInput{ 431 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), [][]float32{{1, 2, 3}}, []bool{false}), 432 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{1}, []bool{false}), 433 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{1}, []bool{false}), 434 }, 435 expect: testutil.NewFunctionTestResult(types.T_array_float32.ToType(), false, 436 [][]float32{{1}}, 437 []bool{false}), 438 }, 439 { 440 info: "3", 441 typ: types.T_array_float32, 442 inputs: []testutil.FunctionTestInput{ 443 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), [][]float32{{1, 2, 3}}, []bool{false}), 444 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{1}, []bool{false}), 445 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{2}, []bool{false}), 446 }, 447 expect: testutil.NewFunctionTestResult(types.T_array_float32.ToType(), false, 448 [][]float32{{1, 2}}, 449 []bool{false}), 450 }, 451 { 452 info: "3", 453 typ: types.T_array_float32, 454 inputs: []testutil.FunctionTestInput{ 455 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), [][]float32{{1, 2, 3}}, []bool{false}), 456 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{1}, []bool{false}), 457 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{3}, []bool{false}), 458 }, 459 expect: testutil.NewFunctionTestResult(types.T_array_float32.ToType(), false, 460 [][]float32{{1, 2, 3}}, 461 []bool{false}), 462 }, 463 { 464 info: "3", 465 typ: types.T_array_float32, 466 inputs: []testutil.FunctionTestInput{ 467 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), [][]float32{{1, 2, 3}}, []bool{false}), 468 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{1}, []bool{false}), 469 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{4}, []bool{false}), 470 }, 471 expect: testutil.NewFunctionTestResult(types.T_array_float32.ToType(), false, 472 [][]float32{{1, 2, 3}}, 473 []bool{false}), 474 }, 475 { 476 info: "3", 477 typ: types.T_array_float32, 478 inputs: []testutil.FunctionTestInput{ 479 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), [][]float32{{1, 2, 3}}, []bool{false}), 480 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{-2}, []bool{false}), 481 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{2}, []bool{false}), 482 }, 483 expect: testutil.NewFunctionTestResult(types.T_array_float32.ToType(), false, 484 [][]float32{{2, 3}}, 485 []bool{false}), 486 }, 487 { 488 info: "3", 489 typ: types.T_array_float32, 490 inputs: []testutil.FunctionTestInput{ 491 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), [][]float32{{1, 2, 3}}, []bool{false}), 492 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{-3}, []bool{false}), 493 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{2}, []bool{false}), 494 }, 495 expect: testutil.NewFunctionTestResult(types.T_array_float32.ToType(), false, 496 [][]float32{{1, 2}}, 497 []bool{false}), 498 }, 499 } 500 } 501 502 func TestSubVector(t *testing.T) { 503 testCases := initSubVectorTestCase() 504 505 proc := testutil.NewProcess() 506 for _, tc := range testCases { 507 var fcTC testutil.FunctionTestCase 508 switch tc.typ { 509 case types.T_array_float32: 510 switch tc.info { 511 case "2": 512 fcTC = testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, SubVectorWith2Args[float32]) 513 case "3": 514 fcTC = testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, SubVectorWith3Args[float32]) 515 } 516 case types.T_array_float64: 517 switch tc.info { 518 case "2": 519 fcTC = testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, SubVectorWith2Args[float64]) 520 case "3": 521 fcTC = testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, SubVectorWith3Args[float64]) 522 } 523 } 524 525 s, info := fcTC.Run() 526 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 527 } 528 } 529 530 func initAsciiStringTestCase() []tcTemp { 531 return []tcTemp{ 532 { 533 info: "test Ascii string", 534 inputs: []testutil.FunctionTestInput{ 535 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 536 []string{"-23", "9999999", "-11"}, 537 []bool{false, false, false}), 538 }, 539 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 540 []uint8{45, 57, 45}, 541 []bool{false, false, false}), 542 }, 543 } 544 } 545 546 func TestAsciiString(t *testing.T) { 547 testCases := initAsciiStringTestCase() 548 549 // do the test work. 550 proc := testutil.NewProcess() 551 for _, tc := range testCases { 552 fcTC := testutil.NewFunctionTestCase(proc, 553 tc.inputs, tc.expect, AsciiString) 554 s, info := fcTC.Run() 555 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 556 } 557 } 558 559 func initAsciiIntTestCase() []tcTemp { 560 return []tcTemp{ 561 { 562 info: "test Ascii Int", 563 inputs: []testutil.FunctionTestInput{ 564 testutil.NewFunctionTestInput(types.T_int64.ToType(), 565 []int64{11}, 566 []bool{false}), 567 }, 568 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 569 []uint8{49}, 570 []bool{false}), 571 }, 572 } 573 } 574 575 func TestAsciiInt(t *testing.T) { 576 testCases := initAsciiIntTestCase() 577 578 // do the test work. 579 proc := testutil.NewProcess() 580 for _, tc := range testCases { 581 fcTC := testutil.NewFunctionTestCase(proc, 582 tc.inputs, tc.expect, AsciiInt[int64]) 583 s, info := fcTC.Run() 584 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 585 } 586 } 587 588 func initAsciiUintTestCase() []tcTemp { 589 return []tcTemp{ 590 { 591 info: "test Ascii Int", 592 inputs: []testutil.FunctionTestInput{ 593 testutil.NewFunctionTestInput(types.T_uint64.ToType(), 594 []uint64{11}, 595 []bool{false}), 596 }, 597 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 598 []uint8{49}, 599 []bool{false}), 600 }, 601 } 602 } 603 604 func TestAsciiUint(t *testing.T) { 605 testCases := initAsciiUintTestCase() 606 607 // do the test work. 608 proc := testutil.NewProcess() 609 for _, tc := range testCases { 610 fcTC := testutil.NewFunctionTestCase(proc, 611 tc.inputs, tc.expect, AsciiUint[uint64]) 612 s, info := fcTC.Run() 613 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 614 } 615 } 616 617 func initBinTestCase() []tcTemp { 618 return []tcTemp{ 619 { 620 info: "test Bin Int", 621 inputs: []testutil.FunctionTestInput{ 622 testutil.NewFunctionTestInput(types.T_uint8.ToType(), 623 []uint8{2, 4, 6, 8, 16, 32, 64, 128}, 624 []bool{false, false, false, false, false, false, false, false}), 625 }, 626 expect: testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, 627 []string{"10", "100", "110", "1000", "10000", "100000", "1000000", "10000000"}, 628 []bool{false, false, false, false, false, false, false, false}), 629 }, 630 } 631 } 632 633 func TestBin(t *testing.T) { 634 testCases := initBinTestCase() 635 636 // do the test work. 637 proc := testutil.NewProcess() 638 for _, tc := range testCases { 639 fcTC := testutil.NewFunctionTestCase(proc, 640 tc.inputs, tc.expect, Bin[uint8]) 641 s, info := fcTC.Run() 642 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 643 } 644 } 645 646 func initBinFloatTestCase() []tcTemp { 647 return []tcTemp{ 648 { 649 info: "test Bin Int", 650 inputs: []testutil.FunctionTestInput{ 651 testutil.NewFunctionTestInput(types.T_float32.ToType(), 652 []float32{2.1111, 4.4261264, 6.1151275, 8.48484, 16.266, 32.3338787, 64.0000000, 128.26454}, 653 []bool{false, false, false, false, false, false, false, false}), 654 }, 655 expect: testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, 656 []string{"10", "100", "110", "1000", "10000", "100000", "1000000", "10000000"}, 657 []bool{false, false, false, false, false, false, false, false}), 658 }, 659 } 660 } 661 662 func TestBinFloat(t *testing.T) { 663 testCases := initBinFloatTestCase() 664 665 // do the test work. 666 proc := testutil.NewProcess() 667 for _, tc := range testCases { 668 fcTC := testutil.NewFunctionTestCase(proc, 669 tc.inputs, tc.expect, BinFloat[float32]) 670 s, info := fcTC.Run() 671 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 672 } 673 } 674 675 func initBitLengthFuncTestCase() []tcTemp { 676 return []tcTemp{ 677 { 678 info: "test BitLengthFunc", 679 inputs: []testutil.FunctionTestInput{ 680 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 681 []string{""}, 682 []bool{false}), 683 }, 684 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, 685 []int64{0}, 686 []bool{false}), 687 }, 688 { 689 info: "test BitLengthFunc", 690 inputs: []testutil.FunctionTestInput{ 691 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 692 []string{""}, 693 []bool{true}), 694 }, 695 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, 696 []int64{}, 697 []bool{true}), 698 }, 699 { 700 info: "test BitLengthFunc", 701 inputs: []testutil.FunctionTestInput{ 702 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 703 []string{"matrix", "origin", "=", "mo", " ", "\t", ""}, 704 []bool{false, false, false, false, false, false, false}), 705 }, 706 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, 707 []int64{48, 48, 8, 16, 8, 8, 0}, 708 []bool{false, false, false, false, false, false, false}), 709 }, 710 } 711 } 712 713 func TestBitLengthFunc(t *testing.T) { 714 testCases := initBitLengthFuncTestCase() 715 716 // do the test work. 717 proc := testutil.NewProcess() 718 for _, tc := range testCases { 719 fcTC := testutil.NewFunctionTestCase(proc, 720 tc.inputs, tc.expect, BitLengthFunc) 721 s, info := fcTC.Run() 722 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 723 } 724 } 725 726 func initCurrentDateTestCase() []tcTemp { 727 return []tcTemp{ 728 { 729 info: "test current date", 730 inputs: []testutil.FunctionTestInput{ 731 testutil.NewFunctionTestInput(types.T_date.ToType(), 732 []types.Date{types.Date(111)}, 733 []bool{false}), 734 }, 735 expect: testutil.NewFunctionTestResult(types.T_date.ToType(), false, 736 []types.Date{types.Date(111)}, 737 []bool{false}), 738 }, 739 } 740 } 741 742 func TestCurrentDate(t *testing.T) { 743 testCases := initCurrentDateTestCase() 744 745 // do the test work. 746 proc := testutil.NewProcess() 747 for _, tc := range testCases { 748 fcTC := testutil.NewFunctionTestCase(proc, 749 tc.inputs, tc.expect, CurrentDate) 750 s, _ := fcTC.Run() 751 require.Equal(t, s, false) 752 } 753 } 754 755 func initDateToDateTestCase() []tcTemp { 756 return []tcTemp{ 757 { 758 info: "test date to date", 759 inputs: []testutil.FunctionTestInput{ 760 testutil.NewFunctionTestInput(types.T_date.ToType(), 761 []types.Date{types.Date(20040403), types.Date(20211003), types.Date(20200823)}, 762 []bool{false, false, false}), 763 }, 764 expect: testutil.NewFunctionTestResult(types.T_date.ToType(), false, 765 []types.Date{types.Date(20040403), types.Date(20211003), types.Date(20200823)}, 766 []bool{false, false, false}), 767 }, 768 } 769 } 770 771 func TestDateToDate(t *testing.T) { 772 testCases := initDateToDateTestCase() 773 774 // do the test work. 775 proc := testutil.NewProcess() 776 for _, tc := range testCases { 777 fcTC := testutil.NewFunctionTestCase(proc, 778 tc.inputs, tc.expect, DateToDate) 779 s, info := fcTC.Run() 780 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 781 } 782 } 783 784 func initDatetimeToDateTestCase() []tcTemp { 785 t, _ := types.ParseDatetime("2020-10-10 11:11:11", 6) 786 return []tcTemp{ 787 { 788 info: "test datetime to date", 789 inputs: []testutil.FunctionTestInput{ 790 testutil.NewFunctionTestInput(types.T_datetime.ToType(), 791 []types.Datetime{t}, 792 []bool{false}), 793 }, 794 expect: testutil.NewFunctionTestResult(types.T_date.ToType(), false, 795 []types.Date{t.ToDate()}, 796 []bool{false}), 797 }, 798 } 799 } 800 801 func TestDatetimeToDate(t *testing.T) { 802 testCases := initDatetimeToDateTestCase() 803 804 // do the test work. 805 proc := testutil.NewProcess() 806 for _, tc := range testCases { 807 fcTC := testutil.NewFunctionTestCase(proc, 808 tc.inputs, tc.expect, DatetimeToDate) 809 s, info := fcTC.Run() 810 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 811 } 812 } 813 814 func initTimeToDateTestCase() []tcTemp { 815 t, _ := types.ParseTime("2020-10-10 11:11:11", 6) 816 return []tcTemp{ 817 { 818 info: "test datetime to date", 819 inputs: []testutil.FunctionTestInput{ 820 testutil.NewFunctionTestInput(types.T_time.ToType(), 821 []types.Time{t}, 822 []bool{false}), 823 }, 824 expect: testutil.NewFunctionTestResult(types.T_date.ToType(), false, 825 []types.Date{t.ToDate()}, 826 []bool{false}), 827 }, 828 } 829 } 830 831 func TestTimeToDate(t *testing.T) { 832 testCases := initTimeToDateTestCase() 833 834 // do the test work. 835 proc := testutil.NewProcess() 836 for _, tc := range testCases { 837 fcTC := testutil.NewFunctionTestCase(proc, 838 tc.inputs, tc.expect, TimeToDate) 839 s, info := fcTC.Run() 840 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 841 } 842 } 843 844 func initDateStringToDateTestCase() []tcTemp { 845 t, _ := types.ParseDatetime("2020-10-10 11:11:11", 6) 846 return []tcTemp{ 847 { 848 info: "test date string to date", 849 inputs: []testutil.FunctionTestInput{ 850 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 851 []string{"2020-10-10"}, 852 []bool{false}), 853 }, 854 expect: testutil.NewFunctionTestResult(types.T_date.ToType(), false, 855 []types.Date{t.ToDate()}, 856 []bool{false}), 857 }, 858 } 859 } 860 861 func TestDateStringToDate(t *testing.T) { 862 testCases := initDateStringToDateTestCase() 863 864 // do the test work. 865 proc := testutil.NewProcess() 866 for _, tc := range testCases { 867 fcTC := testutil.NewFunctionTestCase(proc, 868 tc.inputs, tc.expect, DateStringToDate) 869 s, info := fcTC.Run() 870 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 871 } 872 } 873 874 func initDateToDayTestCase() []tcTemp { 875 return []tcTemp{ 876 { 877 info: "test date to date", 878 inputs: []testutil.FunctionTestInput{ 879 testutil.NewFunctionTestInput(types.T_date.ToType(), 880 []types.Date{types.Date(20040403), types.Date(20211003), types.Date(20200823)}, 881 []bool{false, false, false}), 882 }, 883 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 884 []uint8{5, 6, 23}, 885 []bool{false, false, false}), 886 }, 887 } 888 } 889 890 func TestDateToDay(t *testing.T) { 891 testCases := initDateToDayTestCase() 892 893 // do the test work. 894 proc := testutil.NewProcess() 895 for _, tc := range testCases { 896 fcTC := testutil.NewFunctionTestCase(proc, 897 tc.inputs, tc.expect, DateToDay) 898 s, info := fcTC.Run() 899 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 900 } 901 } 902 903 func initDatetimeToDayTestCase() []tcTemp { 904 return []tcTemp{ 905 { 906 info: "test date to date", 907 inputs: []testutil.FunctionTestInput{ 908 testutil.NewFunctionTestInput(types.T_datetime.ToType(), 909 []types.Datetime{types.Datetime(20040403), types.Datetime(20211003), types.Datetime(20200823)}, 910 []bool{false, false, false}), 911 }, 912 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 913 []uint8{1, 1, 1}, 914 []bool{false, false, false}), 915 }, 916 } 917 } 918 919 func TestDatetimeToDay(t *testing.T) { 920 testCases := initDatetimeToDayTestCase() 921 922 // do the test work. 923 proc := testutil.NewProcess() 924 for _, tc := range testCases { 925 fcTC := testutil.NewFunctionTestCase(proc, 926 tc.inputs, tc.expect, DatetimeToDay) 927 s, info := fcTC.Run() 928 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 929 } 930 } 931 932 func initDayOfYearTestCase() []tcTemp { 933 return []tcTemp{ 934 { 935 info: "test date of Year", 936 inputs: []testutil.FunctionTestInput{ 937 testutil.NewFunctionTestInput(types.T_date.ToType(), 938 []types.Date{types.Date(20040403), types.Date(20211003), types.Date(20200823)}, 939 []bool{false, false, false}), 940 }, 941 expect: testutil.NewFunctionTestResult(types.T_uint16.ToType(), false, 942 []uint16{278, 311, 358}, 943 []bool{false, false, false}), 944 }, 945 } 946 } 947 948 func TestDayOfYear(t *testing.T) { 949 testCases := initDayOfYearTestCase() 950 951 // do the test work. 952 proc := testutil.NewProcess() 953 for _, tc := range testCases { 954 fcTC := testutil.NewFunctionTestCase(proc, 955 tc.inputs, tc.expect, DayOfYear) 956 s, info := fcTC.Run() 957 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 958 } 959 } 960 961 func initEmptyTestCase() []tcTemp { 962 return []tcTemp{ 963 { 964 info: "test date of Year", 965 inputs: []testutil.FunctionTestInput{ 966 testutil.NewFunctionTestInput(types.T_char.ToType(), 967 []string{"", "sdfsdf", ""}, 968 []bool{false, false, true}), 969 }, 970 expect: testutil.NewFunctionTestResult(types.T_bool.ToType(), false, 971 []bool{true, false, false}, 972 []bool{false, false, true}), 973 }, 974 } 975 } 976 977 func TestEmpty(t *testing.T) { 978 testCases := initEmptyTestCase() 979 980 // do the test work. 981 proc := testutil.NewProcess() 982 for _, tc := range testCases { 983 fcTC := testutil.NewFunctionTestCase(proc, 984 tc.inputs, tc.expect, Empty) 985 s, info := fcTC.Run() 986 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 987 } 988 } 989 990 func initJsonQuoteTestCase() []tcTemp { 991 return []tcTemp{ 992 { 993 info: "test json quote", 994 inputs: []testutil.FunctionTestInput{ 995 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 996 []string{"key:v", "sdfsdf", ""}, 997 []bool{false, false, true}), 998 }, 999 expect: testutil.NewFunctionTestResult(types.T_json.ToType(), false, 1000 []string{"\f\u0005key:v", "\f\u0006sdfsdf", ""}, 1001 []bool{false, false, true}), 1002 }, 1003 } 1004 } 1005 1006 func TestJsonQuote(t *testing.T) { 1007 testCases := initJsonQuoteTestCase() 1008 1009 // do the test work. 1010 proc := testutil.NewProcess() 1011 for _, tc := range testCases { 1012 fcTC := testutil.NewFunctionTestCase(proc, 1013 tc.inputs, tc.expect, JsonQuote) 1014 s, info := fcTC.Run() 1015 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1016 } 1017 } 1018 1019 func initJsonUnquoteTestCase() []tcTemp { 1020 return []tcTemp{ 1021 { 1022 info: "test json unquote", 1023 inputs: []testutil.FunctionTestInput{ 1024 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 1025 []string{`"hello"`, `"world"`, `""`}, 1026 []bool{false, false, true}), 1027 }, 1028 expect: testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, 1029 []string{"hello", "world", ""}, 1030 []bool{false, false, true}), 1031 }, 1032 } 1033 } 1034 1035 func TestJsonUnquote(t *testing.T) { 1036 testCases := initJsonUnquoteTestCase() 1037 1038 // do the test work. 1039 proc := testutil.NewProcess() 1040 for _, tc := range testCases { 1041 fcTC := testutil.NewFunctionTestCase(proc, 1042 tc.inputs, tc.expect, JsonUnquote) 1043 s, info := fcTC.Run() 1044 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1045 } 1046 } 1047 1048 func TestLoadFile(t *testing.T) { 1049 dir := t.TempDir() 1050 proc := testutil.NewProc() 1051 ctx := context.Background() 1052 filepath := dir + "test" 1053 fs, readPath, err := fileservice.GetForETL(ctx, proc.FileService, filepath) 1054 assert.Nil(t, err) 1055 err = fs.Write(ctx, fileservice.IOVector{ 1056 FilePath: readPath, 1057 Entries: []fileservice.IOEntry{ 1058 { 1059 Offset: 0, 1060 Size: 4, 1061 Data: []byte("1234"), 1062 }, 1063 { 1064 Offset: 4, 1065 Size: 4, 1066 Data: []byte("5678"), 1067 }, 1068 }, 1069 }) 1070 assert.Nil(t, err) 1071 1072 testCases := []tcTemp{ 1073 { 1074 info: "test load file", 1075 inputs: []testutil.FunctionTestInput{ 1076 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 1077 []string{filepath}, 1078 []bool{false}), 1079 }, 1080 expect: testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, 1081 []string{"12345678"}, 1082 []bool{false}), 1083 }, 1084 } 1085 1086 // do the test work. 1087 for _, tc := range testCases { 1088 fcTC := testutil.NewFunctionTestCase(proc, 1089 tc.inputs, tc.expect, LoadFile) 1090 s, info := fcTC.Run() 1091 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1092 } 1093 } 1094 1095 func initMoMemoryUsageTestCase() []tcTemp { 1096 return []tcTemp{ 1097 { 1098 info: "test mo memory usage", 1099 inputs: []testutil.FunctionTestInput{ 1100 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 1101 []string{""}, 1102 []bool{true}), 1103 }, 1104 expect: testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, 1105 []string{""}, 1106 []bool{true}), 1107 }, 1108 } 1109 } 1110 1111 func TestMoMemoryUsage(t *testing.T) { 1112 testCases := initMoMemoryUsageTestCase() 1113 1114 // do the test work. 1115 proc := testutil.NewProcess() 1116 for _, tc := range testCases { 1117 fcTC := testutil.NewFunctionTestCase(proc, 1118 tc.inputs, tc.expect, MoMemUsage) 1119 _, _ = fcTC.Run() 1120 require.Error(t, moerr.NewInvalidInput(proc.Ctx, "mo mem usage can only take scalar input")) 1121 } 1122 } 1123 1124 func TestMoEnableMemoryUsage(t *testing.T) { 1125 testCases := initMoMemoryUsageTestCase() 1126 1127 // do the test work. 1128 proc := testutil.NewProcess() 1129 for _, tc := range testCases { 1130 fcTC := testutil.NewFunctionTestCase(proc, 1131 tc.inputs, tc.expect, MoEnableMemUsageDetail) 1132 _, _ = fcTC.Run() 1133 require.Error(t, moerr.NewInvalidInput(proc.Ctx, "mo mem usage can only take scalar input")) 1134 } 1135 } 1136 1137 func TestMoDisableMemoryUsage(t *testing.T) { 1138 testCases := initMoMemoryUsageTestCase() 1139 1140 // do the test work. 1141 proc := testutil.NewProcess() 1142 for _, tc := range testCases { 1143 fcTC := testutil.NewFunctionTestCase(proc, 1144 tc.inputs, tc.expect, MoDisableMemUsageDetail) 1145 _, _ = fcTC.Run() 1146 require.Error(t, moerr.NewInvalidInput(proc.Ctx, "mo mem usage can only take scalar input")) 1147 } 1148 } 1149 1150 func initSpaceTestCase() []tcTemp { 1151 return []tcTemp{ 1152 { 1153 info: "test space", 1154 inputs: []testutil.FunctionTestInput{ 1155 testutil.NewFunctionTestInput(types.T_uint64.ToType(), 1156 []uint64{1, 2, 3, 0}, 1157 []bool{false, false, false}), 1158 }, 1159 expect: testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, 1160 []string{" ", " ", " ", ""}, 1161 []bool{false, false, false}), 1162 }, 1163 } 1164 } 1165 1166 func TestSpace(t *testing.T) { 1167 testCases := initSpaceTestCase() 1168 1169 // do the test work. 1170 proc := testutil.NewProcess() 1171 for _, tc := range testCases { 1172 fcTC := testutil.NewFunctionTestCase(proc, 1173 tc.inputs, tc.expect, SpaceNumber[uint64]) 1174 s, info := fcTC.Run() 1175 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1176 } 1177 } 1178 1179 func initToTimeCase() []tcTemp { 1180 d1, _ := types.ParseDatetime("2022-01-01", 6) 1181 d2, _ := types.ParseDatetime("2022-01-01 16:22:44", 6) 1182 //d3, scale, _ := types.Parse128("20221212112233.4444") 1183 //d3, _ = d3.Scale(3 - scale) 1184 return []tcTemp{ 1185 { 1186 info: "test to time", 1187 typ: types.T_date, 1188 inputs: []testutil.FunctionTestInput{ 1189 testutil.NewFunctionTestInput(types.T_date.ToType(), 1190 []types.Date{d1.ToDate()}, 1191 []bool{false}), 1192 }, 1193 expect: testutil.NewFunctionTestResult(types.T_time.ToType(), false, 1194 []types.Time{types.TimeFromClock(false, 0, 0, 0, 0)}, 1195 []bool{false}), 1196 }, 1197 { 1198 info: "test to time", 1199 typ: types.T_datetime, 1200 inputs: []testutil.FunctionTestInput{ 1201 testutil.NewFunctionTestInput(types.T_datetime.ToType(), 1202 []types.Datetime{d2}, 1203 []bool{false}), 1204 }, 1205 expect: testutil.NewFunctionTestResult(types.T_time.ToType(), false, 1206 []types.Time{types.TimeFromClock(false, 16, 22, 44, 0)}, 1207 []bool{false}), 1208 }, 1209 { 1210 info: "test to time", 1211 typ: types.T_int64, 1212 inputs: []testutil.FunctionTestInput{ 1213 testutil.NewFunctionTestInput(types.T_int64.ToType(), 1214 []int64{20221212112233}, 1215 []bool{false}), 1216 }, 1217 expect: testutil.NewFunctionTestResult(types.T_time.ToType(), false, 1218 []types.Time{types.TimeFromClock(false, 2022121211, 22, 33, 0)}, 1219 []bool{false}), 1220 }, 1221 { 1222 info: "test to time", 1223 typ: types.T_varchar, 1224 inputs: []testutil.FunctionTestInput{ 1225 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 1226 []string{"2022-01-01 16:22:44.1235"}, 1227 []bool{false}), 1228 }, 1229 expect: testutil.NewFunctionTestResult(types.T_time.ToType(), false, 1230 []types.Time{types.TimeFromClock(false, 16, 22, 44, 123500)}, 1231 []bool{false}), 1232 }, 1233 //{ 1234 // info: "test to time", 1235 // typ: types.T_decimal128, 1236 // inputs: []testutil.FunctionTestInput{ 1237 // testutil.NewFunctionTestInput(types.T_decimal128.ToType(), 1238 // []types.Decimal128{d3}, 1239 // []bool{false}), 1240 // }, 1241 // expect: testutil.NewFunctionTestResult(types.T_time.ToType(), false, 1242 // []types.Time{types.TimeFromClock(false, 2022121211, 22, 33, 444000)}, 1243 // []bool{false}), 1244 //}, 1245 } 1246 } 1247 1248 func TestToTime(t *testing.T) { 1249 testCases := initToTimeCase() 1250 1251 // do the test work. 1252 proc := testutil.NewProcess() 1253 for _, tc := range testCases { 1254 var fcTC testutil.FunctionTestCase 1255 switch tc.typ { 1256 case types.T_date: 1257 fcTC = testutil.NewFunctionTestCase(proc, 1258 tc.inputs, tc.expect, DateToTime) 1259 case types.T_datetime: 1260 fcTC = testutil.NewFunctionTestCase(proc, 1261 tc.inputs, tc.expect, DatetimeToTime) 1262 case types.T_int64: 1263 fcTC = testutil.NewFunctionTestCase(proc, 1264 tc.inputs, tc.expect, Int64ToTime) 1265 case types.T_varchar: 1266 fcTC = testutil.NewFunctionTestCase(proc, 1267 tc.inputs, tc.expect, DateStringToTime) 1268 //case types.T_decimal128: 1269 // fcTC = testutil.NewFunctionTestCase(proc, 1270 // tc.inputs, tc.expect, Decimal128ToTime) 1271 } 1272 s, info := fcTC.Run() 1273 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1274 } 1275 } 1276 1277 func initToTimestampCase() []tcTemp { 1278 d1, _ := types.ParseDatetime("2022-01-01", 6) 1279 d2, _ := types.ParseDatetime("2022-01-01 00:00:00", 6) 1280 1281 return []tcTemp{ 1282 { 1283 info: "test to timestamp", 1284 typ: types.T_date, 1285 inputs: []testutil.FunctionTestInput{ 1286 testutil.NewFunctionTestInput(types.T_date.ToType(), 1287 []types.Date{d1.ToDate()}, 1288 []bool{false}), 1289 }, 1290 expect: testutil.NewFunctionTestResult(types.T_timestamp.ToType(), false, 1291 []types.Timestamp{types.FromClockZone(time.Local, 2022, 1, 1, 0, 0, 0, 0)}, 1292 []bool{false}), 1293 }, 1294 { 1295 info: "test to timestamp", 1296 typ: types.T_datetime, 1297 inputs: []testutil.FunctionTestInput{ 1298 testutil.NewFunctionTestInput(types.T_datetime.ToType(), 1299 []types.Datetime{d2}, 1300 []bool{false}), 1301 }, 1302 expect: testutil.NewFunctionTestResult(types.T_timestamp.ToType(), false, 1303 []types.Timestamp{types.FromClockZone(time.Local, 2022, 1, 1, 0, 0, 0, 0)}, 1304 []bool{false}), 1305 }, 1306 { 1307 info: "test to timestamp", 1308 typ: types.T_varchar, 1309 inputs: []testutil.FunctionTestInput{ 1310 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 1311 []string{"2022-01-01"}, 1312 []bool{false}), 1313 }, 1314 expect: testutil.NewFunctionTestResult(types.T_timestamp.ToType(), false, 1315 []types.Timestamp{types.FromClockZone(time.Local, 2022, 1, 1, 0, 0, 0, 0)}, 1316 []bool{false}), 1317 }, 1318 } 1319 } 1320 1321 func TestToTimeStamp(t *testing.T) { 1322 testCases := initToTimestampCase() 1323 1324 // do the test work. 1325 proc := testutil.NewProcess() 1326 for _, tc := range testCases { 1327 var fcTC testutil.FunctionTestCase 1328 switch tc.typ { 1329 case types.T_date: 1330 fcTC = testutil.NewFunctionTestCase(proc, 1331 tc.inputs, tc.expect, DateToTimestamp) 1332 case types.T_datetime: 1333 fcTC = testutil.NewFunctionTestCase(proc, 1334 tc.inputs, tc.expect, DatetimeToTimestamp) 1335 case types.T_varchar: 1336 fcTC = testutil.NewFunctionTestCase(proc, 1337 tc.inputs, tc.expect, DateStringToTimestamp) 1338 } 1339 s, info := fcTC.Run() 1340 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1341 } 1342 } 1343 1344 func TestValues(t *testing.T) { 1345 testCases := []tcTemp{ 1346 { 1347 info: "values(col_int8)", 1348 inputs: []testutil.FunctionTestInput{ 1349 testutil.NewFunctionTestInput(types.T_int8.ToType(), 1350 []int8{-23}, []bool{false}), 1351 }, 1352 expect: testutil.NewFunctionTestResult(types.T_int8.ToType(), false, 1353 []int8{-23}, []bool{false}), 1354 }, 1355 { 1356 info: "values(col_uint8)", 1357 inputs: []testutil.FunctionTestInput{ 1358 testutil.NewFunctionTestInput(types.T_uint8.ToType(), 1359 []uint8{23, 24, 25}, []bool{false}), 1360 }, 1361 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 1362 []uint8{23, 24, 25}, []bool{false}), 1363 }, 1364 } 1365 1366 proc := testutil.NewProcess() 1367 for _, tc := range testCases { 1368 fcTC := testutil.NewFunctionTestCase(proc, 1369 tc.inputs, tc.expect, Values) 1370 s, info := fcTC.Run() 1371 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1372 } 1373 } 1374 1375 func initHourTestCase() []tcTemp { 1376 d1, _ := types.ParseDatetime("2004-04-03 10:20:00", 6) 1377 d2, _ := types.ParseTimestamp(time.Local, "2004-08-03 01:01:37", 6) 1378 1379 return []tcTemp{ 1380 { 1381 info: "test hour", 1382 typ: types.T_datetime, 1383 inputs: []testutil.FunctionTestInput{ 1384 testutil.NewFunctionTestInput(types.T_datetime.ToType(), 1385 []types.Datetime{d1}, 1386 []bool{false}), 1387 }, 1388 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 1389 []uint8{10}, 1390 []bool{false}), 1391 }, 1392 { 1393 info: "test hour", 1394 typ: types.T_timestamp, 1395 inputs: []testutil.FunctionTestInput{ 1396 testutil.NewFunctionTestInput(types.T_timestamp.ToType(), 1397 []types.Timestamp{d2}, 1398 []bool{false}), 1399 }, 1400 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 1401 []uint8{1}, 1402 []bool{false}), 1403 }, 1404 } 1405 } 1406 1407 func TestHour(t *testing.T) { 1408 testCases := initHourTestCase() 1409 1410 // do the test work. 1411 proc := testutil.NewProcess() 1412 for _, tc := range testCases { 1413 var fcTC testutil.FunctionTestCase 1414 switch tc.typ { 1415 case types.T_datetime: 1416 fcTC = testutil.NewFunctionTestCase(proc, 1417 tc.inputs, tc.expect, DatetimeToHour) 1418 case types.T_timestamp: 1419 fcTC = testutil.NewFunctionTestCase(proc, 1420 tc.inputs, tc.expect, TimestampToHour) 1421 } 1422 s, info := fcTC.Run() 1423 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1424 } 1425 } 1426 1427 func initMinuteTestCase() []tcTemp { 1428 d1, _ := types.ParseDatetime("2004-04-03 10:20:00", 6) 1429 d2, _ := types.ParseTimestamp(time.Local, "2004-08-03 01:01:37", 6) 1430 1431 return []tcTemp{ 1432 { 1433 info: "test hour", 1434 typ: types.T_datetime, 1435 inputs: []testutil.FunctionTestInput{ 1436 testutil.NewFunctionTestInput(types.T_datetime.ToType(), 1437 []types.Datetime{d1}, 1438 []bool{false}), 1439 }, 1440 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 1441 []uint8{20}, 1442 []bool{false}), 1443 }, 1444 { 1445 info: "test hour", 1446 typ: types.T_timestamp, 1447 inputs: []testutil.FunctionTestInput{ 1448 testutil.NewFunctionTestInput(types.T_timestamp.ToType(), 1449 []types.Timestamp{d2}, 1450 []bool{false}), 1451 }, 1452 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 1453 []uint8{1}, 1454 []bool{false}), 1455 }, 1456 } 1457 } 1458 1459 func TestMinute(t *testing.T) { 1460 testCases := initMinuteTestCase() 1461 1462 // do the test work. 1463 proc := testutil.NewProcess() 1464 for _, tc := range testCases { 1465 var fcTC testutil.FunctionTestCase 1466 switch tc.typ { 1467 case types.T_datetime: 1468 fcTC = testutil.NewFunctionTestCase(proc, 1469 tc.inputs, tc.expect, DatetimeToMinute) 1470 case types.T_timestamp: 1471 fcTC = testutil.NewFunctionTestCase(proc, 1472 tc.inputs, tc.expect, TimestampToMinute) 1473 } 1474 s, info := fcTC.Run() 1475 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1476 } 1477 } 1478 1479 func initSecondTestCase() []tcTemp { 1480 d1, _ := types.ParseDatetime("2004-04-03 10:20:00", 6) 1481 d2, _ := types.ParseTimestamp(time.Local, "2004-01-03 23:15:08", 6) 1482 1483 return []tcTemp{ 1484 { 1485 info: "test hour", 1486 typ: types.T_datetime, 1487 inputs: []testutil.FunctionTestInput{ 1488 testutil.NewFunctionTestInput(types.T_datetime.ToType(), 1489 []types.Datetime{d1}, 1490 []bool{false}), 1491 }, 1492 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 1493 []uint8{0}, 1494 []bool{false}), 1495 }, 1496 { 1497 info: "test hour", 1498 typ: types.T_timestamp, 1499 inputs: []testutil.FunctionTestInput{ 1500 testutil.NewFunctionTestInput(types.T_timestamp.ToType(), 1501 []types.Timestamp{d2}, 1502 []bool{false}), 1503 }, 1504 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 1505 []uint8{8}, 1506 []bool{false}), 1507 }, 1508 } 1509 } 1510 1511 func TestSecond(t *testing.T) { 1512 testCases := initSecondTestCase() 1513 1514 // do the test work. 1515 proc := testutil.NewProcess() 1516 for _, tc := range testCases { 1517 var fcTC testutil.FunctionTestCase 1518 switch tc.typ { 1519 case types.T_datetime: 1520 fcTC = testutil.NewFunctionTestCase(proc, 1521 tc.inputs, tc.expect, DatetimeToSecond) 1522 case types.T_timestamp: 1523 fcTC = testutil.NewFunctionTestCase(proc, 1524 tc.inputs, tc.expect, TimestampToSecond) 1525 } 1526 s, info := fcTC.Run() 1527 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1528 } 1529 } 1530 1531 func initBinaryTestCase() []tcTemp { 1532 return []tcTemp{ 1533 { 1534 info: "test hour", 1535 typ: types.T_datetime, 1536 inputs: []testutil.FunctionTestInput{ 1537 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 1538 []string{"hello"}, 1539 []bool{false}), 1540 }, 1541 expect: testutil.NewFunctionTestResult(types.T_binary.ToType(), false, 1542 []string{"hello"}, 1543 []bool{false}), 1544 }, 1545 } 1546 } 1547 1548 func TestBinary(t *testing.T) { 1549 testCases := initBinaryTestCase() 1550 1551 // do the test work. 1552 proc := testutil.NewProcess() 1553 for _, tc := range testCases { 1554 fcTC := testutil.NewFunctionTestCase(proc, 1555 tc.inputs, tc.expect, Binary) 1556 s, info := fcTC.Run() 1557 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1558 } 1559 } 1560 1561 func initHexStringTestCase() []tcTemp { 1562 return []tcTemp{ 1563 { 1564 info: "test hex string", 1565 inputs: []testutil.FunctionTestInput{ 1566 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 1567 []string{"a", "", "255", ""}, 1568 []bool{false, false, false, true}), 1569 }, 1570 expect: testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, 1571 []string{"61", "", "323535", ""}, 1572 []bool{false, false, false, true}), 1573 }, 1574 { 1575 //TODO: Verify the multi-row case: original code:https://github.com/m-schen/matrixone/blob/d2f81f4b9d843ecb749fa0277332b4150e1fd87f/pkg/sql/plan/function/builtin/unary/hex_test.go#L58 1576 info: "test hex string - multirow", 1577 inputs: []testutil.FunctionTestInput{testutil.NewFunctionTestInput(types.T_varchar.ToType(), 1578 []string{"Hello", "Gopher!"}, 1579 []bool{false, false}), 1580 }, 1581 expect: testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, 1582 []string{"48656c6c6f", "476f7068657221"}, 1583 []bool{false, false}), 1584 }, 1585 { 1586 info: "test encode - string to hex", 1587 inputs: []testutil.FunctionTestInput{testutil.NewFunctionTestInput(types.T_varchar.ToType(), 1588 []string{"", "abc", "a\nb", `a\nb`, "a\"b"}, 1589 []bool{false, false, false, false, false}), 1590 }, 1591 expect: testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, 1592 []string{"", "616263", "610a62", "615c6e62", "612262"}, 1593 []bool{false, false, false, false, false}), 1594 }, 1595 } 1596 } 1597 1598 func TestHexString(t *testing.T) { 1599 testCases := initHexStringTestCase() 1600 1601 proc := testutil.NewProcess() 1602 for _, tc := range testCases { 1603 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, HexString) 1604 s, info := fcTC.Run() 1605 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1606 } 1607 } 1608 1609 func initHexInt64TestCase() []tcTemp { 1610 return []tcTemp{ 1611 { 1612 info: "test hex int64", 1613 inputs: []testutil.FunctionTestInput{ 1614 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{255, 231323423423421, 0}, []bool{false, false, true}), 1615 }, 1616 expect: testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, 1617 []string{"FF", "D2632E7B3BBD", ""}, 1618 []bool{false, false, true}), 1619 }, 1620 { 1621 //TODO: Verify the multi-row case. Original code: https://github.com/m-schen/matrixone/blob/d2f81f4b9d843ecb749fa0277332b4150e1fd87f/pkg/sql/plan/function/builtin/unary/hex_test.go#L116 1622 info: "test hex int64 - multirow", 1623 inputs: []testutil.FunctionTestInput{ 1624 testutil.NewFunctionTestInput(types.T_int64.ToType(), []int64{123, 234, 345}, []bool{false, false, false}), 1625 }, 1626 expect: testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, 1627 []string{"7B", "EA", "159"}, 1628 []bool{false, false, false}), 1629 }, 1630 } 1631 } 1632 1633 func TestHexInt64(t *testing.T) { 1634 testCases := initHexInt64TestCase() 1635 1636 proc := testutil.NewProcess() 1637 for _, tc := range testCases { 1638 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, HexInt64) 1639 s, info := fcTC.Run() 1640 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1641 } 1642 } 1643 1644 // HexArray 1645 func initHexArrayTestCase() []tcTemp { 1646 1647 arrayF32Cases := []struct { 1648 info string 1649 data [][]float32 1650 wants []string 1651 }{ 1652 { 1653 info: "test encode - array_float32 to hex", 1654 data: [][]float32{ 1655 {0.34881967306137085, 0.0028086076490581036, 0.5752133727073669}, 1656 {0.95072953, 0.54392913, 0.30788785}, 1657 {0.98972348, 0.61145728, 0.27879944}, 1658 {0.37520402, 0.13316834, 0.94819581}, 1659 }, 1660 wants: []string{ 1661 "7e98b23e9e10383b2f41133f", 1662 "0363733ff13e0b3f7aa39d3e", 1663 "855e7d3f77881c3fcdbe8e3e", 1664 "be1ac03e485d083ef6bc723f", 1665 }, 1666 }, 1667 } 1668 1669 arrayF64Cases := []struct { 1670 info string 1671 data [][]float64 1672 wants []string 1673 }{ 1674 { 1675 info: "test encode - array_float64 to hex", 1676 data: [][]float64{ 1677 {0.34881967306137085, 0.0028086076490581036, 0.5752133727073669}, 1678 }, 1679 wants: []string{ 1680 "000000c00f53d63f000000c01302673f000000e02568e23f", 1681 }, 1682 }, 1683 } 1684 1685 var testInputs = make([]tcTemp, 0, len(arrayF32Cases)+len(arrayF64Cases)) 1686 1687 for _, c := range arrayF32Cases { 1688 1689 testInputs = append(testInputs, tcTemp{ 1690 info: c.info, 1691 inputs: []testutil.FunctionTestInput{ 1692 testutil.NewFunctionTestInput(types.T_array_float32.ToType(), c.data, []bool{}), 1693 }, 1694 expect: testutil.NewFunctionTestResult(types.T_text.ToType(), false, c.wants, []bool{}), 1695 }) 1696 } 1697 1698 for _, c := range arrayF64Cases { 1699 1700 testInputs = append(testInputs, tcTemp{ 1701 info: c.info, 1702 inputs: []testutil.FunctionTestInput{ 1703 testutil.NewFunctionTestInput(types.T_array_float64.ToType(), c.data, []bool{}), 1704 }, 1705 expect: testutil.NewFunctionTestResult(types.T_text.ToType(), false, c.wants, []bool{}), 1706 }) 1707 } 1708 1709 return testInputs 1710 1711 } 1712 1713 func TestHexArray(t *testing.T) { 1714 testCases := initHexArrayTestCase() 1715 1716 proc := testutil.NewProcess() 1717 for _, tc := range testCases { 1718 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, HexArray) 1719 s, info := fcTC.Run() 1720 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1721 } 1722 } 1723 1724 func initMd5TestCase() []tcTemp { 1725 return []tcTemp{ 1726 { 1727 info: "test encode - string to md5", 1728 inputs: []testutil.FunctionTestInput{ 1729 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 1730 []string{"", "abc", "abcd", "abc\b", "abc\"d", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, 1731 []bool{false, false, false, false, false, false}), 1732 }, 1733 expect: testutil.NewFunctionTestResult(types.T_blob.ToType(), false, 1734 []string{ 1735 "d41d8cd98f00b204e9800998ecf8427e", 1736 "900150983cd24fb0d6963f7d28e17f72", 1737 "e2fc714c4727ee9395f324cd2e7f331f", 1738 "c7fa18a56de1b25123523e8475ceb311", 1739 "0671c72bd761b6ab47f5385798998780", 1740 "5eca9bd3eb07c006cd43ae48dfde7fd3", 1741 }, 1742 []bool{false, false, false, false, false, false}), 1743 }, 1744 } 1745 } 1746 1747 func TestMd5(t *testing.T) { 1748 testCases := initMd5TestCase() 1749 1750 proc := testutil.NewProcess() 1751 for _, tc := range testCases { 1752 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Md5) 1753 s, info := fcTC.Run() 1754 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1755 } 1756 } 1757 1758 func initUnhexTestCase() []tcTemp { 1759 return []tcTemp{ 1760 { 1761 info: "test unhex", 1762 inputs: []testutil.FunctionTestInput{ 1763 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 1764 []string{"616263", "610a62", "615c6e62", "612262", "e4bda0e5a5bd", "invalid", "", ""}, 1765 []bool{false, false, false, false, false, false, false, true}), 1766 }, 1767 expect: testutil.NewFunctionTestResult(types.T_blob.ToType(), false, 1768 []string{"abc", "a\nb", `a\nb`, "a\"b", "你好", "", "", ""}, 1769 []bool{false, false, false, false, false, true, false, true}), 1770 }, 1771 { 1772 info: "test encode - hex to blob", 1773 inputs: []testutil.FunctionTestInput{ 1774 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 1775 []string{"", "616263", "610a62", "615c6e62", "612262"}, 1776 []bool{false, false, false, false, false}), 1777 }, 1778 expect: testutil.NewFunctionTestResult(types.T_blob.ToType(), false, 1779 []string{"", "abc", "a\nb", `a\nb`, "a\"b"}, 1780 []bool{false, false, false, false, false}), 1781 }, 1782 { 1783 info: "test encode - hex to blob(array_float32)", 1784 inputs: []testutil.FunctionTestInput{ 1785 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 1786 []string{ 1787 "7e98b23e9e10383b2f41133f", 1788 "0363733ff13e0b3f7aa39d3e", 1789 "855e7d3f77881c3fcdbe8e3e", 1790 "be1ac03e485d083ef6bc723f", 1791 }, 1792 []bool{false, false, false, false}), 1793 }, 1794 expect: testutil.NewFunctionTestResult(types.T_blob.ToType(), false, []string{ 1795 functionUtil.QuickBytesToStr(types.ArrayToBytes([]float32{0.34881967306137085, 0.0028086076490581036, 0.5752133727073669})), 1796 functionUtil.QuickBytesToStr(types.ArrayToBytes([]float32{0.95072953, 0.54392913, 0.30788785})), 1797 functionUtil.QuickBytesToStr(types.ArrayToBytes([]float32{0.98972348, 0.61145728, 0.27879944})), 1798 functionUtil.QuickBytesToStr(types.ArrayToBytes([]float32{0.37520402, 0.13316834, 0.94819581})), 1799 }, []bool{false, false, false, false}), 1800 }, 1801 { 1802 info: "test encode - hex to blob(array_float64)", 1803 inputs: []testutil.FunctionTestInput{ 1804 testutil.NewFunctionTestInput(types.T_varchar.ToType(), []string{ 1805 "000000c00f53d63f000000c01302673f000000e02568e23f", 1806 }, []bool{false}), 1807 }, 1808 expect: testutil.NewFunctionTestResult(types.T_blob.ToType(), false, []string{ 1809 functionUtil.QuickBytesToStr(types.ArrayToBytes([]float64{0.34881967306137085, 0.0028086076490581036, 0.5752133727073669})), 1810 }, []bool{false}), 1811 }, 1812 } 1813 } 1814 1815 func TestUnhex(t *testing.T) { 1816 testCases := initUnhexTestCase() 1817 1818 proc := testutil.NewProcess() 1819 for _, tc := range testCases { 1820 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Unhex) 1821 s, info := fcTC.Run() 1822 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1823 } 1824 } 1825 1826 // ToBase64 1827 func initToBase64TestCase() []tcTemp { 1828 regularCases := []struct { 1829 info string 1830 data []string 1831 wants []string 1832 }{ 1833 { 1834 info: "test encode - string to base64", 1835 data: []string{"", "abc", "a\nb", `a\nb`, "a\"b"}, 1836 wants: []string{"", "YWJj", "YQpi", "YVxuYg==", "YSJi"}, 1837 }, 1838 } 1839 1840 var testInputs = make([]tcTemp, 0, len(regularCases)) 1841 for _, c := range regularCases { 1842 1843 testInputs = append(testInputs, tcTemp{ 1844 info: c.info, 1845 inputs: []testutil.FunctionTestInput{ 1846 testutil.NewFunctionTestInput(types.T_varchar.ToType(), c.data, []bool{}), 1847 }, 1848 expect: testutil.NewFunctionTestResult(types.T_text.ToType(), false, c.wants, []bool{}), 1849 }) 1850 } 1851 1852 return testInputs 1853 1854 } 1855 1856 func TestToBase64(t *testing.T) { 1857 testCases := initToBase64TestCase() 1858 1859 proc := testutil.NewProcess() 1860 for _, tc := range testCases { 1861 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, ToBase64) 1862 s, info := fcTC.Run() 1863 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1864 } 1865 } 1866 1867 // FromBase64 1868 func initFromBase64TestCase() []tcTemp { 1869 regularCases := []struct { 1870 info string 1871 data []string 1872 wants []string 1873 }{ 1874 1875 { 1876 info: "test encode - base64 to blob", 1877 data: []string{"", "YWJj", "YQpi", "YSJi"}, 1878 wants: []string{"", "abc", "a\nb", "a\"b"}, 1879 }, 1880 } 1881 1882 var testInputs = make([]tcTemp, 0, len(regularCases)) 1883 for _, c := range regularCases { 1884 1885 testInputs = append(testInputs, tcTemp{ 1886 info: c.info, 1887 inputs: []testutil.FunctionTestInput{ 1888 testutil.NewFunctionTestInput(types.T_varchar.ToType(), c.data, []bool{}), 1889 }, 1890 expect: testutil.NewFunctionTestResult(types.T_blob.ToType(), false, c.wants, []bool{}), 1891 }) 1892 } 1893 1894 return testInputs 1895 1896 } 1897 1898 func TestFromBase64(t *testing.T) { 1899 testCases := initFromBase64TestCase() 1900 1901 proc := testutil.NewProcess() 1902 for _, tc := range testCases { 1903 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, FromBase64) 1904 s, info := fcTC.Run() 1905 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1906 } 1907 } 1908 1909 func initBlobLengthTestCase() []tcTemp { 1910 return []tcTemp{ 1911 { 1912 info: "test length blob", 1913 inputs: []testutil.FunctionTestInput{ 1914 testutil.NewFunctionTestInput(types.T_blob.ToType(), 1915 //TODO: verify: Passing String instead of []byte. Original Code: https://github.com/m-schen/matrixone/blob/d2f81f4b9d843ecb749fa0277332b4150e1fd87f/pkg/sql/plan/function/builtin/unary/length_test.go#L117 1916 []string{"12345678", ""}, 1917 []bool{false, false}), 1918 }, 1919 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, 1920 []int64{8, 0}, 1921 []bool{false, false}), 1922 }, 1923 } 1924 } 1925 1926 func TestBlobLength(t *testing.T) { 1927 testCases := initBlobLengthTestCase() 1928 1929 proc := testutil.NewProcess() 1930 for _, tc := range testCases { 1931 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Length) 1932 s, info := fcTC.Run() 1933 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1934 } 1935 } 1936 1937 func initLengthTestCase() []tcTemp { 1938 return []tcTemp{ 1939 { 1940 //TODO: verify if makevector can be represented like this. Original Code: https://github.com/m-schen/matrixone/blob/d2f81f4b9d843ecb749fa0277332b4150e1fd87f/pkg/sql/plan/function/builtin/unary/length_test.go#L51 1941 info: "test length varchar", 1942 inputs: []testutil.FunctionTestInput{testutil.NewFunctionTestInput(types.T_varchar.ToType(), []string{"abcdefghijklm"}, []bool{false})}, 1943 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, []int64{13}, []bool{false}), 1944 }, 1945 { 1946 //TODO: verify if makevector can be represented like this. Original Code: https://github.com/m-schen/matrixone/blob/d2f81f4b9d843ecb749fa0277332b4150e1fd87f/pkg/sql/plan/function/builtin/unary/length_test.go#L58 1947 info: "test length char", 1948 inputs: []testutil.FunctionTestInput{testutil.NewFunctionTestInput(types.T_char.ToType(), []string{"abcdefghijklm"}, []bool{false})}, 1949 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, []int64{13}, []bool{false}), 1950 }, 1951 { 1952 //TODO: Previously T_Text was not added. Original code: https://github.com/m-schen/matrixone/blob/d2f81f4b9d843ecb749fa0277332b4150e1fd87f/pkg/sql/plan/function/builtin/unary/length_test.go#L71 1953 info: "test length text", 1954 inputs: []testutil.FunctionTestInput{testutil.NewFunctionTestInput(types.T_text.ToType(), []string{"abcdefghijklm"}, []bool{false})}, 1955 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, []int64{13}, []bool{false}), 1956 }, 1957 } 1958 } 1959 1960 func TestLength(t *testing.T) { 1961 testCases := initLengthTestCase() 1962 1963 proc := testutil.NewProcess() 1964 for _, tc := range testCases { 1965 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Length) 1966 s, info := fcTC.Run() 1967 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 1968 } 1969 } 1970 1971 // LengthUTF8 1972 1973 func initLengthUTF8TestCase() []tcTemp { 1974 return []tcTemp{ 1975 { 1976 info: "test lengthutf8", 1977 inputs: []testutil.FunctionTestInput{testutil.NewFunctionTestInput(types.T_varchar.ToType(), 1978 []string{ 1979 "abc", 1980 "", 1981 " ", 1982 "中国123", 1983 "abc😄", 1984 "中国中国中国中国中国中国中国中国中国中国1234", 1985 "中国中国中国中国中国中国中国中国中国中国1234😄ggg!", 1986 "你好", 1987 "français", 1988 "にほんご", 1989 "Español", 1990 "123456", 1991 "андрей", 1992 "\\", 1993 string(rune(0x0c)), 1994 string('"'), 1995 string('\a'), 1996 string('\b'), 1997 string('\t'), 1998 string('\n'), 1999 string('\r'), 2000 string(rune(0x10)), 2001 "你好", 2002 "再见", 2003 "今天", 2004 "日期时间", 2005 "明天", 2006 "\n\t\r\b" + string(rune(0)) + "\\_\\%\\", 2007 }, 2008 []bool{ 2009 false, 2010 false, 2011 false, 2012 false, 2013 false, 2014 false, 2015 false, 2016 false, 2017 false, 2018 false, 2019 false, 2020 false, 2021 false, 2022 false, 2023 false, 2024 false, 2025 false, 2026 false, 2027 false, 2028 false, 2029 false, 2030 false, 2031 false, 2032 false, 2033 false, 2034 false, 2035 false, 2036 false, 2037 })}, 2038 expect: testutil.NewFunctionTestResult(types.T_uint64.ToType(), false, 2039 []uint64{ 2040 3, 2041 0, 2042 3, 2043 5, 2044 4, 2045 24, 2046 29, 2047 2, 2048 8, 2049 4, 2050 7, 2051 6, 2052 6, 2053 1, 2054 1, 2055 1, 2056 1, 2057 1, 2058 1, 2059 1, 2060 1, 2061 1, 2062 2, 2063 2, 2064 2, 2065 4, 2066 2, 2067 10, 2068 }, 2069 []bool{ 2070 false, 2071 false, 2072 false, 2073 false, 2074 false, 2075 false, 2076 false, 2077 false, 2078 false, 2079 false, 2080 false, 2081 false, 2082 false, 2083 false, 2084 false, 2085 false, 2086 false, 2087 false, 2088 false, 2089 false, 2090 false, 2091 false, 2092 false, 2093 false, 2094 false, 2095 false, 2096 false, 2097 false, 2098 }), 2099 }, 2100 } 2101 } 2102 2103 func TestLengthUTF8(t *testing.T) { 2104 testCases := initLengthUTF8TestCase() 2105 2106 proc := testutil.NewProcess() 2107 for _, tc := range testCases { 2108 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, LengthUTF8) 2109 s, info := fcTC.Run() 2110 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2111 } 2112 } 2113 2114 // Ltrim 2115 2116 func initLtrimTestCase() []tcTemp { 2117 return []tcTemp{ 2118 { 2119 info: "test ltrim", 2120 inputs: []testutil.FunctionTestInput{testutil.NewFunctionTestInput(types.T_varchar.ToType(), 2121 []string{" 123", " 123", "123 ", " 8 ", " 8 a ", ""}, 2122 []bool{false, false, false, false, false, true}, 2123 )}, 2124 expect: testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, 2125 []string{"123", "123", "123 ", "8 ", "8 a ", ""}, 2126 []bool{false, false, false, false, false, true}, 2127 ), 2128 }, 2129 } 2130 } 2131 2132 func TestLtrim(t *testing.T) { 2133 testCases := initLtrimTestCase() 2134 2135 proc := testutil.NewProcess() 2136 for _, tc := range testCases { 2137 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Ltrim) 2138 2139 s, info := fcTC.Run() 2140 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2141 } 2142 } 2143 2144 // Rtrim 2145 2146 func initRtrimTestCase() []tcTemp { 2147 return []tcTemp{ 2148 { 2149 info: "test rtrim", 2150 inputs: []testutil.FunctionTestInput{testutil.NewFunctionTestInput(types.T_varchar.ToType(), 2151 []string{ 2152 "barbar ", 2153 "MySQL", 2154 "a", 2155 " 20.06 ", 2156 " right ", 2157 "你好 ", 2158 "2017-06-15 ", 2159 "2017-06-15 ", 2160 2161 "アイウエオ", 2162 "アイウエオ ", 2163 "アイウエオ ", 2164 "アイウエオ ", 2165 "アイウエオ ", 2166 "アイウエオ ", 2167 "アイウエオ ", 2168 "あいうえお", 2169 "あいうえお ", 2170 "あいうえお ", 2171 "あいうえお ", 2172 "あいうえお", 2173 "あいうえお ", 2174 "あいうえお ", 2175 "龔龖龗龞龡", 2176 "龔龖龗龞龡 ", 2177 "龔龖龗龞龡 ", 2178 "龔龖龗龞龡 ", 2179 "龔龖龗龞龡", 2180 "龔龖龗龞龡 ", 2181 "龔龖龗龞龡 ", 2182 "アイウエオ", 2183 "アイウエオ ", 2184 "アイウエオ ", 2185 "アイウエオ ", 2186 "アイウエオ", 2187 "アイウエオ ", 2188 "アイウエオ ", 2189 "あいうえお", 2190 "あいうえお ", 2191 "あいうえお ", 2192 "あいうえお ", 2193 "あいうえお", 2194 "あいうえお ", 2195 "あいうえお ", 2196 "龔龖龗龞龡", 2197 "龔龖龗龞龡 ", 2198 "龔龖龗龞龡 ", 2199 "龔龖龗龞龡 ", 2200 "龔龖龗龞龡", 2201 "龔龖龗龞龡 ", 2202 "龔龖龗龞龡 ", 2203 "アイウエオ", 2204 "アイウエオ ", 2205 "アイウエオ ", 2206 "アイウエオ ", 2207 "アイウエオ", 2208 "アイウエオ ", 2209 "アイウエオ ", 2210 "あいうえお", 2211 "あいうえお ", 2212 "あいうえお ", 2213 "あいうえお ", 2214 "あいうえお", 2215 "あいうえお ", 2216 "あいうえお ", 2217 "龔龖龗龞龡", 2218 "龔龖龗龞龡 ", 2219 "龔龖龗龞龡 ", 2220 "龔龖龗龞龡 ", 2221 "龔龖龗龞龡", 2222 "龔龖龗龞龡 ", 2223 "龔龖龗龞龡 ", 2224 "2017-06-15 ", 2225 "2019-06-25 ", 2226 " 2019-06-25 ", 2227 " 2019-06-25 ", 2228 " 2012-10-12 ", 2229 " 2004-04-24. ", 2230 " 2008-12-04. ", 2231 " 2012-03-23. ", 2232 " 2013-04-30 ", 2233 " 1994-10-04 ", 2234 " 2018-06-04 ", 2235 " 2012-10-12 ", 2236 "1241241^&@%#^*^!@#&*(!& ", 2237 " 123 ", 2238 }, 2239 []bool{ 2240 false, 2241 false, 2242 false, 2243 false, 2244 false, 2245 false, 2246 false, 2247 false, 2248 false, 2249 false, 2250 false, 2251 false, 2252 false, 2253 false, 2254 false, 2255 false, 2256 false, 2257 false, 2258 false, 2259 false, 2260 false, 2261 false, 2262 false, 2263 false, 2264 false, 2265 false, 2266 false, 2267 false, 2268 false, 2269 false, 2270 false, 2271 false, 2272 false, 2273 false, 2274 false, 2275 false, 2276 false, 2277 false, 2278 false, 2279 false, 2280 false, 2281 false, 2282 false, 2283 false, 2284 false, 2285 false, 2286 false, 2287 false, 2288 false, 2289 false, 2290 false, 2291 false, 2292 false, 2293 false, 2294 false, 2295 false, 2296 false, 2297 false, 2298 false, 2299 false, 2300 false, 2301 false, 2302 false, 2303 false, 2304 false, 2305 false, 2306 false, 2307 false, 2308 false, 2309 false, 2310 false, 2311 false, 2312 false, 2313 false, 2314 false, 2315 false, 2316 false, 2317 false, 2318 false, 2319 false, 2320 false, 2321 false, 2322 false, 2323 false, 2324 false, 2325 })}, 2326 expect: testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, 2327 []string{ 2328 "barbar", 2329 "MySQL", 2330 "a", 2331 " 20.06", 2332 " right", 2333 "你好", 2334 "2017-06-15", 2335 "2017-06-15", 2336 2337 "アイウエオ", 2338 "アイウエオ", 2339 "アイウエオ", 2340 "アイウエオ", 2341 "アイウエオ", 2342 "アイウエオ", 2343 "アイウエオ", 2344 "あいうえお", 2345 "あいうえお", 2346 "あいうえお", 2347 "あいうえお", 2348 "あいうえお", 2349 "あいうえお", 2350 "あいうえお", 2351 "龔龖龗龞龡", 2352 "龔龖龗龞龡", 2353 "龔龖龗龞龡", 2354 "龔龖龗龞龡", 2355 "龔龖龗龞龡", 2356 "龔龖龗龞龡", 2357 "龔龖龗龞龡", 2358 "アイウエオ", 2359 "アイウエオ", 2360 "アイウエオ", 2361 "アイウエオ", 2362 "アイウエオ", 2363 "アイウエオ", 2364 "アイウエオ", 2365 "あいうえお", 2366 "あいうえお", 2367 "あいうえお", 2368 "あいうえお", 2369 "あいうえお", 2370 "あいうえお", 2371 "あいうえお", 2372 "龔龖龗龞龡", 2373 "龔龖龗龞龡", 2374 "龔龖龗龞龡", 2375 "龔龖龗龞龡", 2376 "龔龖龗龞龡", 2377 "龔龖龗龞龡", 2378 "龔龖龗龞龡", 2379 "アイウエオ", 2380 "アイウエオ", 2381 "アイウエオ", 2382 "アイウエオ", 2383 "アイウエオ", 2384 "アイウエオ", 2385 "アイウエオ", 2386 "あいうえお", 2387 "あいうえお", 2388 "あいうえお", 2389 "あいうえお", 2390 "あいうえお", 2391 "あいうえお", 2392 "あいうえお", 2393 "龔龖龗龞龡", 2394 "龔龖龗龞龡", 2395 "龔龖龗龞龡", 2396 "龔龖龗龞龡", 2397 "龔龖龗龞龡", 2398 "龔龖龗龞龡", 2399 "龔龖龗龞龡", 2400 "2017-06-15", 2401 "2019-06-25", 2402 " 2019-06-25", 2403 " 2019-06-25", 2404 " 2012-10-12", 2405 " 2004-04-24.", 2406 " 2008-12-04.", 2407 " 2012-03-23.", 2408 " 2013-04-30", 2409 " 1994-10-04", 2410 " 2018-06-04", 2411 " 2012-10-12", 2412 "1241241^&@%#^*^!@#&*(!&", 2413 " 123", 2414 }, 2415 []bool{ 2416 false, 2417 false, 2418 false, 2419 false, 2420 false, 2421 false, 2422 false, 2423 false, 2424 false, 2425 false, 2426 false, 2427 false, 2428 false, 2429 false, 2430 false, 2431 false, 2432 false, 2433 false, 2434 false, 2435 false, 2436 false, 2437 false, 2438 false, 2439 false, 2440 false, 2441 false, 2442 false, 2443 false, 2444 false, 2445 false, 2446 false, 2447 false, 2448 false, 2449 false, 2450 false, 2451 false, 2452 false, 2453 false, 2454 false, 2455 false, 2456 false, 2457 false, 2458 false, 2459 false, 2460 false, 2461 false, 2462 false, 2463 false, 2464 false, 2465 false, 2466 false, 2467 false, 2468 false, 2469 false, 2470 false, 2471 false, 2472 false, 2473 false, 2474 false, 2475 false, 2476 false, 2477 false, 2478 false, 2479 false, 2480 false, 2481 false, 2482 false, 2483 false, 2484 false, 2485 false, 2486 false, 2487 false, 2488 false, 2489 false, 2490 false, 2491 false, 2492 false, 2493 false, 2494 false, 2495 false, 2496 false, 2497 false, 2498 false, 2499 false, 2500 false, 2501 }), 2502 }, 2503 } 2504 } 2505 2506 func TestRtrim(t *testing.T) { 2507 testCases := initRtrimTestCase() 2508 2509 proc := testutil.NewProcess() 2510 for _, tc := range testCases { 2511 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Rtrim) 2512 s, info := fcTC.Run() 2513 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2514 } 2515 } 2516 2517 // Reverse 2518 2519 func initReverseTestCase() []tcTemp { 2520 return []tcTemp{ 2521 { 2522 //TODO: How to handle ScalarNulls. Original code: https://github.com/m-schen/matrixone/blob/6715c45c2f6e2b15808b10a21fafc17d03a8ae0b/pkg/sql/plan/function/builtin/unary/reverse_test.go#L75 2523 info: "test reverse", 2524 inputs: []testutil.FunctionTestInput{testutil.NewFunctionTestInput(types.T_varchar.ToType(), 2525 []string{ 2526 "abc", 2527 "abcd", 2528 "hello", 2529 "アイウエオ", 2530 "あいうえお", 2531 "龔龖龗龞龡", 2532 "你好", 2533 "再 见", 2534 "bcd", 2535 "def", 2536 "xyz", 2537 "1a1", 2538 "2012", 2539 "@($)@($#)_@(#", 2540 "2023-04-24", 2541 "10:03:23.021412", 2542 "sdfad ", 2543 }, 2544 []bool{ 2545 false, 2546 false, 2547 false, 2548 false, 2549 false, 2550 false, 2551 false, 2552 false, 2553 false, 2554 false, 2555 false, 2556 false, 2557 false, 2558 false, 2559 false, 2560 false, 2561 false, 2562 })}, 2563 expect: testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, 2564 []string{ 2565 "cba", 2566 "dcba", 2567 "olleh", 2568 "オエウイア", 2569 "おえういあ", 2570 "龡龞龗龖龔", 2571 "好你", 2572 "见 再", 2573 "dcb", 2574 "fed", 2575 "zyx", 2576 "1a1", 2577 "2102", 2578 "#(@_)#$(@)$(@", 2579 "42-40-3202", 2580 "214120.32:30:01", 2581 " dafds", 2582 }, 2583 []bool{ 2584 false, 2585 false, 2586 false, 2587 false, 2588 false, 2589 false, 2590 false, 2591 false, 2592 false, 2593 false, 2594 false, 2595 false, 2596 false, 2597 false, 2598 false, 2599 false, 2600 false, 2601 }), 2602 }, 2603 } 2604 } 2605 2606 func TestReverse(t *testing.T) { 2607 testCases := initReverseTestCase() 2608 2609 proc := testutil.NewProcess() 2610 for _, tc := range testCases { 2611 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Reverse) 2612 s, info := fcTC.Run() 2613 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2614 } 2615 } 2616 2617 // Oct 2618 2619 func initOctUint8TestCase() []tcTemp { 2620 e1, _, _ := types.Parse128("14") 2621 e2, _, _ := types.Parse128("143") 2622 e3, _, _ := types.Parse128("144") 2623 e4, _, _ := types.Parse128("377") 2624 2625 return []tcTemp{ 2626 { 2627 info: "test oct uint8", 2628 inputs: []testutil.FunctionTestInput{ 2629 testutil.NewFunctionTestInput(types.T_uint8.ToType(), 2630 []uint8{12, 99, 100, 255}, 2631 []bool{false, false, false, false}), 2632 }, 2633 expect: testutil.NewFunctionTestResult(types.T_decimal128.ToType(), false, 2634 []types.Decimal128{e1, e2, e3, e4}, 2635 []bool{false, false, false, false}), 2636 }, 2637 } 2638 } 2639 2640 func TestOctUint8(t *testing.T) { 2641 testCases := initOctUint8TestCase() 2642 2643 proc := testutil.NewProcess() 2644 for _, tc := range testCases { 2645 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Oct[uint8]) 2646 s, info := fcTC.Run() 2647 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2648 } 2649 } 2650 2651 func initOctUint16TestCase() []tcTemp { 2652 e1, _, _ := types.Parse128("14") 2653 e2, _, _ := types.Parse128("143") 2654 e3, _, _ := types.Parse128("144") 2655 e4, _, _ := types.Parse128("377") 2656 e5, _, _ := types.Parse128("2000") 2657 e6, _, _ := types.Parse128("23420") 2658 e7, _, _ := types.Parse128("177777") 2659 2660 return []tcTemp{ 2661 { 2662 info: "test oct uint16", 2663 inputs: []testutil.FunctionTestInput{ 2664 testutil.NewFunctionTestInput(types.T_uint16.ToType(), 2665 []uint16{12, 99, 100, 255, 1024, 10000, 65535}, 2666 []bool{false, false, false, false, false, false, false}), 2667 }, 2668 expect: testutil.NewFunctionTestResult(types.T_decimal128.ToType(), false, 2669 []types.Decimal128{e1, e2, e3, e4, e5, e6, e7}, 2670 []bool{false, false, false, false, false, false, false}), 2671 }, 2672 } 2673 } 2674 2675 func TestOctUint16(t *testing.T) { 2676 testCases := initOctUint16TestCase() 2677 2678 proc := testutil.NewProcess() 2679 for _, tc := range testCases { 2680 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Oct[uint16]) 2681 s, info := fcTC.Run() 2682 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2683 } 2684 } 2685 2686 func initOctUint32TestCase() []tcTemp { 2687 e1, _, _ := types.Parse128("14") 2688 e2, _, _ := types.Parse128("143") 2689 e3, _, _ := types.Parse128("144") 2690 e4, _, _ := types.Parse128("377") 2691 e5, _, _ := types.Parse128("2000") 2692 e6, _, _ := types.Parse128("23420") 2693 e7, _, _ := types.Parse128("177777") 2694 e8, _, _ := types.Parse128("37777777777") 2695 2696 return []tcTemp{ 2697 { 2698 info: "test oct uint32", 2699 inputs: []testutil.FunctionTestInput{ 2700 testutil.NewFunctionTestInput(types.T_uint32.ToType(), 2701 []uint32{12, 99, 100, 255, 1024, 10000, 65535, 4294967295}, 2702 []bool{false, false, false, false, false, false, false, false}), 2703 }, 2704 expect: testutil.NewFunctionTestResult(types.T_decimal128.ToType(), false, 2705 []types.Decimal128{e1, e2, e3, e4, e5, e6, e7, e8}, 2706 []bool{false, false, false, false, false, false, false, false}), 2707 }, 2708 } 2709 } 2710 2711 func TestOctUint32(t *testing.T) { 2712 testCases := initOctUint32TestCase() 2713 2714 proc := testutil.NewProcess() 2715 for _, tc := range testCases { 2716 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Oct[uint32]) 2717 s, info := fcTC.Run() 2718 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2719 } 2720 } 2721 2722 func initOctUint64TestCase() []tcTemp { 2723 e1, _, _ := types.Parse128("14") 2724 e2, _, _ := types.Parse128("143") 2725 e3, _, _ := types.Parse128("144") 2726 e4, _, _ := types.Parse128("377") 2727 e5, _, _ := types.Parse128("2000") 2728 e6, _, _ := types.Parse128("23420") 2729 e7, _, _ := types.Parse128("177777") 2730 e8, _, _ := types.Parse128("37777777777") 2731 e9, _, _ := types.Parse128("1777777777777777777777") 2732 2733 return []tcTemp{ 2734 { 2735 info: "test oct uint64", 2736 inputs: []testutil.FunctionTestInput{ 2737 testutil.NewFunctionTestInput(types.T_uint64.ToType(), 2738 []uint64{12, 99, 100, 255, 1024, 10000, 65535, 4294967295, 18446744073709551615}, 2739 []bool{false, false, false, false, false, false, false, false, false}), 2740 }, 2741 expect: testutil.NewFunctionTestResult(types.T_decimal128.ToType(), false, 2742 []types.Decimal128{e1, e2, e3, e4, e5, e6, e7, e8, e9}, 2743 []bool{false, false, false, false, false, false, false, false, false}), 2744 }, 2745 } 2746 } 2747 2748 func TestOctUint64(t *testing.T) { 2749 testCases := initOctUint64TestCase() 2750 2751 proc := testutil.NewProcess() 2752 for _, tc := range testCases { 2753 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Oct[uint64]) 2754 s, info := fcTC.Run() 2755 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2756 } 2757 } 2758 2759 func initOctInt8TestCase() []tcTemp { 2760 e1, _, _ := types.Parse128("1777777777777777777600") 2761 e2, _, _ := types.Parse128("1777777777777777777777") 2762 e3, _, _ := types.Parse128("177") 2763 2764 return []tcTemp{ 2765 { 2766 info: "test oct int8", 2767 inputs: []testutil.FunctionTestInput{ 2768 testutil.NewFunctionTestInput(types.T_int8.ToType(), 2769 []int8{-128, -1, 127}, 2770 []bool{false, false, false}), 2771 }, 2772 expect: testutil.NewFunctionTestResult(types.T_decimal128.ToType(), false, 2773 []types.Decimal128{e1, e2, e3}, 2774 []bool{false, false, false}), 2775 }, 2776 } 2777 } 2778 2779 func TestOctInt8(t *testing.T) { 2780 testCases := initOctInt8TestCase() 2781 2782 proc := testutil.NewProcess() 2783 for _, tc := range testCases { 2784 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Oct[int8]) 2785 s, info := fcTC.Run() 2786 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2787 } 2788 } 2789 2790 func initOctInt16TestCase() []tcTemp { 2791 e1, _, _ := types.Parse128("1777777777777777700000") 2792 2793 return []tcTemp{ 2794 { 2795 info: "test oct int16", 2796 inputs: []testutil.FunctionTestInput{ 2797 testutil.NewFunctionTestInput(types.T_int16.ToType(), 2798 []int16{-32768}, 2799 []bool{false}), 2800 }, 2801 expect: testutil.NewFunctionTestResult(types.T_decimal128.ToType(), false, 2802 []types.Decimal128{e1}, 2803 []bool{false}), 2804 }, 2805 } 2806 } 2807 2808 func TestOctInt16(t *testing.T) { 2809 testCases := initOctInt16TestCase() 2810 2811 proc := testutil.NewProcess() 2812 for _, tc := range testCases { 2813 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Oct[int16]) 2814 s, info := fcTC.Run() 2815 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2816 } 2817 } 2818 2819 func initOctInt32TestCase() []tcTemp { 2820 e1, _, _ := types.Parse128("1777777777760000000000") 2821 2822 return []tcTemp{ 2823 { 2824 info: "test oct int32", 2825 inputs: []testutil.FunctionTestInput{ 2826 testutil.NewFunctionTestInput(types.T_int32.ToType(), 2827 []int32{-2147483648}, 2828 []bool{false}), 2829 }, 2830 expect: testutil.NewFunctionTestResult(types.T_decimal128.ToType(), false, 2831 []types.Decimal128{e1}, 2832 []bool{false}), 2833 }, 2834 } 2835 } 2836 2837 func TestOctInt32(t *testing.T) { 2838 testCases := initOctInt32TestCase() 2839 2840 proc := testutil.NewProcess() 2841 for _, tc := range testCases { 2842 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Oct[int32]) 2843 s, info := fcTC.Run() 2844 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2845 } 2846 } 2847 2848 func initOctInt64TestCase() []tcTemp { 2849 e1, _, _ := types.Parse128("1000000000000000000000") 2850 2851 return []tcTemp{ 2852 { 2853 info: "test oct int64", 2854 inputs: []testutil.FunctionTestInput{ 2855 testutil.NewFunctionTestInput(types.T_int64.ToType(), 2856 []int64{-9223372036854775808}, 2857 []bool{false}), 2858 }, 2859 expect: testutil.NewFunctionTestResult(types.T_decimal128.ToType(), false, 2860 []types.Decimal128{e1}, 2861 []bool{false}), 2862 }, 2863 } 2864 } 2865 2866 func TestOctInt64(t *testing.T) { 2867 testCases := initOctInt64TestCase() 2868 2869 proc := testutil.NewProcess() 2870 for _, tc := range testCases { 2871 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Oct[int64]) 2872 s, info := fcTC.Run() 2873 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2874 } 2875 //TODO: I am excluding scalar testcase, as per our last discussion on WeCom: https://github.com/m-schen/matrixone/blob/0a48ec5488caff6fd918ad558ebe054eba745be8/pkg/sql/plan/function/builtin/unary/oct_test.go#L176 2876 //TODO: Previous OctFloat didn't have testcase. Should we add new testcases? 2877 } 2878 2879 // Month 2880 2881 func initDateToMonthTestCase() []tcTemp { 2882 d1, _ := types.ParseDateCast("2004-04-03") 2883 d2, _ := types.ParseDateCast("2004-08-03") 2884 d3, _ := types.ParseDateCast("2004-01-03") 2885 return []tcTemp{ 2886 { 2887 info: "test date to month", 2888 inputs: []testutil.FunctionTestInput{ 2889 testutil.NewFunctionTestInput(types.T_date.ToType(), 2890 []types.Date{d1, d2, d3}, 2891 []bool{false, false, false}), 2892 }, 2893 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 2894 []uint8{4, 8, 1}, 2895 []bool{false, false, false}), 2896 }, 2897 } 2898 } 2899 2900 func TestDateToMonth(t *testing.T) { 2901 testCases := initDateToMonthTestCase() 2902 2903 proc := testutil.NewProcess() 2904 for _, tc := range testCases { 2905 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, DateToMonth) 2906 s, info := fcTC.Run() 2907 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2908 } 2909 } 2910 2911 func initDateTimeToMonthTestCase() []tcTemp { 2912 d1, _ := types.ParseDatetime("2004-04-03 13:11:10", 6) 2913 d2, _ := types.ParseDatetime("1999-08-05 11:01:02", 6) 2914 d3, _ := types.ParseDatetime("2004-01-03 23:15:08", 6) 2915 return []tcTemp{ 2916 { 2917 info: "test datetime to month", 2918 inputs: []testutil.FunctionTestInput{ 2919 testutil.NewFunctionTestInput(types.T_datetime.ToType(), 2920 []types.Datetime{d1, d2, d3}, 2921 []bool{false, false, false}), 2922 }, 2923 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 2924 []uint8{4, 8, 1}, 2925 []bool{false, false, false}), 2926 }, 2927 } 2928 } 2929 2930 func TestDateTimeToMonth(t *testing.T) { 2931 testCases := initDateTimeToMonthTestCase() 2932 2933 proc := testutil.NewProcess() 2934 for _, tc := range testCases { 2935 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, DatetimeToMonth) 2936 s, info := fcTC.Run() 2937 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2938 } 2939 } 2940 2941 func initDateStringToMonthTestCase() []tcTemp { 2942 return []tcTemp{ 2943 { 2944 info: "test datestring to month", 2945 inputs: []testutil.FunctionTestInput{ 2946 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 2947 []string{"2014-04-03", "2009-11-03", "2012-07-03", "2012-02-03 18:23:15"}, 2948 []bool{false, false, false, false}), 2949 }, 2950 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 2951 []uint8{4, 11, 7, 2}, 2952 []bool{false, false, false, false}), 2953 }, 2954 } 2955 } 2956 2957 func TestDateStringToMonth(t *testing.T) { 2958 testCases := initDateStringToMonthTestCase() 2959 2960 proc := testutil.NewProcess() 2961 for _, tc := range testCases { 2962 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, DateStringToMonth) 2963 s, info := fcTC.Run() 2964 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2965 } 2966 //TODO: Ignoring Scalar Nulls: Original code: https://github.com/m-schen/matrixone/blob/823b5524f1c6eb189ee9652013bdf86b99e5571e/pkg/sql/plan/function/builtin/unary/month_test.go#L150 2967 } 2968 2969 // Year 2970 2971 func initDateToYearTestCase() []tcTemp { 2972 d1, _ := types.ParseDateCast("2004-04-03") 2973 d2, _ := types.ParseDateCast("2014-08-03") 2974 d3, _ := types.ParseDateCast("2008-01-03") 2975 return []tcTemp{ 2976 { 2977 info: "test date to year", 2978 inputs: []testutil.FunctionTestInput{ 2979 testutil.NewFunctionTestInput(types.T_date.ToType(), 2980 []types.Date{d1, d2, d3}, 2981 []bool{false, false, false}), 2982 }, 2983 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, 2984 []int64{2004, 2014, 2008}, 2985 []bool{false, false, false}), 2986 }, 2987 } 2988 } 2989 2990 func TestDateToYear(t *testing.T) { 2991 testCases := initDateToYearTestCase() 2992 2993 proc := testutil.NewProcess() 2994 for _, tc := range testCases { 2995 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, DateToYear) 2996 s, info := fcTC.Run() 2997 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 2998 } 2999 } 3000 3001 func initDateTimeToYearTestCase() []tcTemp { 3002 d1, _ := types.ParseDatetime("2004-04-03 13:11:10", 6) 3003 d2, _ := types.ParseDatetime("1999-08-05 11:01:02", 6) 3004 d3, _ := types.ParseDatetime("2004-01-03 23:15:08", 6) 3005 return []tcTemp{ 3006 { 3007 info: "test datetime to year", 3008 inputs: []testutil.FunctionTestInput{ 3009 testutil.NewFunctionTestInput(types.T_datetime.ToType(), 3010 []types.Datetime{d1, d2, d3}, 3011 []bool{false, false, false}), 3012 }, 3013 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, 3014 []int64{2004, 1999, 2004}, 3015 []bool{false, false, false}), 3016 }, 3017 } 3018 } 3019 3020 func TestDateTimeToYear(t *testing.T) { 3021 testCases := initDateTimeToYearTestCase() 3022 3023 proc := testutil.NewProcess() 3024 for _, tc := range testCases { 3025 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, DatetimeToYear) 3026 s, info := fcTC.Run() 3027 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 3028 } 3029 } 3030 3031 func initDateStringToYearTestCase() []tcTemp { 3032 return []tcTemp{ 3033 { 3034 info: "test datestring to year", 3035 inputs: []testutil.FunctionTestInput{ 3036 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 3037 []string{"2014-04-03", "2009-11-03", "2012-07-03", "2012-02-03 18:23:15"}, 3038 []bool{false, false, false, false}), 3039 }, 3040 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, 3041 []int64{2014, 2009, 2012, 2012}, 3042 []bool{false, false, false, false}), 3043 }, 3044 } 3045 } 3046 3047 func TestDateStringToYear(t *testing.T) { 3048 testCases := initDateStringToYearTestCase() 3049 3050 proc := testutil.NewProcess() 3051 for _, tc := range testCases { 3052 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, DateStringToYear) 3053 s, info := fcTC.Run() 3054 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 3055 } 3056 //TODO: Ignoring Scalar Nulls: Original code:https://github.com/m-schen/matrixone/blob/e8259e975b2c256e529bf26c0ac278fe0df8e97c/pkg/sql/plan/function/builtin/unary/year_test.go#L150 3057 } 3058 3059 // Week 3060 3061 func initDateToWeekTestCase() []tcTemp { 3062 d11, _ := types.ParseDateCast("2003-12-30") 3063 d12, _ := types.ParseDateCast("2004-01-02") 3064 d13, _ := types.ParseDateCast("2004-12-31") 3065 d14, _ := types.ParseDateCast("2005-01-01") 3066 3067 d21, _ := types.ParseDateCast("2001-02-16") 3068 d22, _ := types.ParseDateCast("2012-06-18") 3069 d23, _ := types.ParseDateCast("2015-09-25") 3070 d24, _ := types.ParseDateCast("2022-12-05") 3071 return []tcTemp{ 3072 { 3073 info: "test date to week - first and last week", 3074 inputs: []testutil.FunctionTestInput{ 3075 testutil.NewFunctionTestInput(types.T_date.ToType(), 3076 []types.Date{d11, d12, d13, d14}, 3077 []bool{false, false, false, false}), 3078 }, 3079 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 3080 []uint8{1, 1, 53, 53}, 3081 []bool{false, false, false, false}), 3082 }, 3083 { 3084 info: "test date to week - normal", 3085 inputs: []testutil.FunctionTestInput{ 3086 testutil.NewFunctionTestInput(types.T_date.ToType(), 3087 []types.Date{d21, d22, d23, d24}, 3088 []bool{false, false, false, false}), 3089 }, 3090 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 3091 []uint8{7, 25, 39, 49}, 3092 []bool{false, false, false, false}), 3093 }, 3094 { 3095 info: "test date to week - null", 3096 inputs: []testutil.FunctionTestInput{ 3097 testutil.NewFunctionTestInput(types.T_date.ToType(), 3098 []types.Date{d11}, 3099 []bool{true}), 3100 }, 3101 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 3102 []uint8{0}, 3103 []bool{true}), 3104 }, 3105 //TODO: Ignoring Scalar Nulls: Original code:https://github.com/m-schen/matrixone/blob/749eb739130decdbbf3dcc3dd5b21f656620edd9/pkg/sql/plan/function/builtin/unary/week_test.go#L52 3106 } 3107 } 3108 3109 func TestDateToWeek(t *testing.T) { 3110 testCases := initDateToWeekTestCase() 3111 3112 proc := testutil.NewProcess() 3113 for _, tc := range testCases { 3114 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, DateToWeek) 3115 s, info := fcTC.Run() 3116 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 3117 } 3118 } 3119 3120 func initDateTimeToWeekTestCase() []tcTemp { 3121 d11, _ := types.ParseDatetime("2003-12-30 13:11:10", 6) 3122 d12, _ := types.ParseDatetime("2004-01-02 19:22:10", 6) 3123 d13, _ := types.ParseDatetime("2004-12-31 00:00:00", 6) 3124 d14, _ := types.ParseDatetime("2005-01-01 04:05:06", 6) 3125 3126 d21, _ := types.ParseDatetime("2001-02-16 13:11:10", 6) 3127 d22, _ := types.ParseDatetime("2012-06-18 19:22:10", 6) 3128 d23, _ := types.ParseDatetime("2015-09-25 00:00:00", 6) 3129 d24, _ := types.ParseDatetime("2022-12-05 04:05:06", 6) 3130 return []tcTemp{ 3131 { 3132 info: "test datetime to week - first and last week", 3133 inputs: []testutil.FunctionTestInput{ 3134 testutil.NewFunctionTestInput(types.T_datetime.ToType(), 3135 []types.Datetime{d11, d12, d13, d14}, 3136 []bool{false, false, false, false}), 3137 }, 3138 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 3139 []uint8{1, 1, 53, 53}, 3140 []bool{false, false, false, false}), 3141 }, 3142 { 3143 info: "test datetime to week - normal", 3144 inputs: []testutil.FunctionTestInput{ 3145 testutil.NewFunctionTestInput(types.T_datetime.ToType(), 3146 []types.Datetime{d21, d22, d23, d24}, 3147 []bool{false, false, false, false}), 3148 }, 3149 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 3150 []uint8{7, 25, 39, 49}, 3151 []bool{false, false, false, false}), 3152 }, 3153 { 3154 info: "test datetime to week - null", 3155 inputs: []testutil.FunctionTestInput{ 3156 testutil.NewFunctionTestInput(types.T_datetime.ToType(), 3157 []types.Datetime{d11}, 3158 []bool{true}), 3159 }, 3160 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, 3161 []uint8{1}, 3162 []bool{true}), 3163 }, 3164 } 3165 } 3166 3167 func TestDateTimeToWeek(t *testing.T) { 3168 testCases := initDateTimeToWeekTestCase() 3169 3170 proc := testutil.NewProcess() 3171 for _, tc := range testCases { 3172 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, DatetimeToWeek) 3173 s, info := fcTC.Run() 3174 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 3175 } 3176 //TODO: Ignoring Scalar Nulls: Original code:https://github.com/m-schen/matrixone/blob/749eb739130decdbbf3dcc3dd5b21f656620edd9/pkg/sql/plan/function/builtin/unary/week_test.go#L114 3177 } 3178 3179 // Week day 3180 3181 func initDateToWeekdayTestCase() []tcTemp { 3182 d11, _ := types.ParseDateCast("2004-04-03") 3183 d12, _ := types.ParseDateCast("2021-10-03") 3184 d13, _ := types.ParseDateCast("2020-08-23") 3185 return []tcTemp{ 3186 { 3187 info: "test date to weekday - first and last weekday", 3188 inputs: []testutil.FunctionTestInput{ 3189 testutil.NewFunctionTestInput(types.T_date.ToType(), 3190 []types.Date{d11, d12, d13}, 3191 []bool{false, false, false}), 3192 }, 3193 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, 3194 []int64{5, 6, 6}, 3195 []bool{false, false, false}), 3196 }, 3197 } 3198 } 3199 3200 func TestDateToWeekday(t *testing.T) { 3201 testCases := initDateToWeekdayTestCase() 3202 3203 proc := testutil.NewProcess() 3204 for _, tc := range testCases { 3205 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, DateToWeekday) 3206 s, info := fcTC.Run() 3207 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 3208 } 3209 } 3210 3211 func initDateTimeToWeekdayTestCase() []tcTemp { 3212 d11, _ := types.ParseDatetime("2004-04-03 13:11:10", 6) 3213 d12, _ := types.ParseDatetime("2021-10-03 15:24:18", 6) 3214 d13, _ := types.ParseDatetime("2020-08-23 21:53:09", 6) 3215 3216 return []tcTemp{ 3217 { 3218 info: "test datetime to weekday - first and last weekday", 3219 inputs: []testutil.FunctionTestInput{ 3220 testutil.NewFunctionTestInput(types.T_datetime.ToType(), 3221 []types.Datetime{d11, d12, d13}, 3222 []bool{false, false, false}), 3223 }, 3224 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, 3225 []int64{5, 6, 6}, 3226 []bool{false, false, false}), 3227 }, 3228 } 3229 } 3230 3231 func TestDateTimeToWeekday(t *testing.T) { 3232 testCases := initDateTimeToWeekdayTestCase() 3233 3234 proc := testutil.NewProcess() 3235 for _, tc := range testCases { 3236 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, DatetimeToWeekday) 3237 s, info := fcTC.Run() 3238 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 3239 } 3240 } 3241 3242 func initPiTestCase() []tcTemp { 3243 return []tcTemp{ 3244 { 3245 info: "test pi", 3246 //TODO: Validate if T_int8 is ok 3247 inputs: []testutil.FunctionTestInput{testutil.NewFunctionTestInput(types.T_int8.ToType(), []int8{0}, []bool{false})}, 3248 expect: testutil.NewFunctionTestResult(types.T_float64.ToType(), false, []float64{math.Pi}, []bool{false}), 3249 }, 3250 } 3251 } 3252 3253 func TestPi(t *testing.T) { 3254 testCases := initPiTestCase() 3255 3256 proc := testutil.NewProcess() 3257 for _, tc := range testCases { 3258 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, Pi) 3259 s, info := fcTC.Run() 3260 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 3261 } 3262 } 3263 3264 func initUTCTimestampTestCase() []tcTemp { 3265 return []tcTemp{ 3266 { 3267 info: "test UTCTimestamp", 3268 //TODO: Validate: Original Code: https://github.com/m-schen/matrixone/blob/9a29d4656c2c6be66885270a2a50664d3ba2a203/pkg/sql/plan/function/builtin/multi/utctimestamp_test.go#L24 3269 inputs: []testutil.FunctionTestInput{testutil.NewFunctionTestInput(types.T_int8.ToType(), []int8{}, []bool{})}, 3270 expect: testutil.NewFunctionTestResult(types.T_datetime.ToType(), false, []types.Datetime{}, []bool{}), 3271 }, 3272 } 3273 } 3274 3275 func TestUTCTimestamp(t *testing.T) { 3276 testCases := initUTCTimestampTestCase() 3277 3278 proc := testutil.NewProcess() 3279 for _, tc := range testCases { 3280 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, UTCTimestamp) 3281 s, info := fcTC.Run() 3282 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 3283 } 3284 } 3285 3286 func TestSleep(t *testing.T) { 3287 testCases := []tcTemp{ 3288 { 3289 info: "sleep uint64", 3290 inputs: []testutil.FunctionTestInput{ 3291 testutil.NewFunctionTestInput(types.T_uint64.ToType(), []uint64{1}, []bool{false}), 3292 }, 3293 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, []uint8{0}, []bool{false}), 3294 }, 3295 { 3296 info: "sleep uint64 with null", 3297 inputs: []testutil.FunctionTestInput{ 3298 testutil.NewFunctionTestInput(types.T_uint64.ToType(), []uint64{1, 0, 1}, []bool{false, true, true}), 3299 }, 3300 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), true, []uint8{0, 0, 0}, []bool{false, true, true}), 3301 }, 3302 } 3303 3304 // do the test work. 3305 proc := testutil.NewProcess() 3306 for _, tc := range testCases { 3307 fcTC := testutil.NewFunctionTestCase(proc, 3308 tc.inputs, tc.expect, Sleep[uint64]) 3309 s, info := fcTC.Run() 3310 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 3311 } 3312 3313 testCases2 := []tcTemp{ 3314 { 3315 info: "sleep float64", 3316 inputs: []testutil.FunctionTestInput{ 3317 testutil.NewFunctionTestInput(types.T_float64.ToType(), []float64{0.1, 0.2}, []bool{false, false}), 3318 }, 3319 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), false, []uint8{0, 0}, []bool{false, false}), 3320 }, 3321 { 3322 info: "sleep float64 with null", 3323 inputs: []testutil.FunctionTestInput{ 3324 testutil.NewFunctionTestInput(types.T_float64.ToType(), []float64{0.1, 0, 0.1}, []bool{false, true, true}), 3325 }, 3326 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), true, []uint8{0, 0, 0}, []bool{false, true, true}), 3327 }, 3328 { 3329 info: "sleep float64 with null", 3330 inputs: []testutil.FunctionTestInput{ 3331 testutil.NewFunctionTestInput(types.T_float64.ToType(), []float64{0.1, -1.0, 0.1}, []bool{false, false, true}), 3332 }, 3333 expect: testutil.NewFunctionTestResult(types.T_uint8.ToType(), true, []uint8{0, 0, 0}, []bool{false, true, true}), 3334 }, 3335 } 3336 3337 for _, tc := range testCases2 { 3338 fcTC := testutil.NewFunctionTestCase(proc, 3339 tc.inputs, tc.expect, Sleep[float64]) 3340 s, info := fcTC.Run() 3341 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 3342 } 3343 } 3344 3345 func initBitCastTestCase() []tcTemp { 3346 return []tcTemp{ 3347 { 3348 info: "test Decode Int8", 3349 inputs: []testutil.FunctionTestInput{ 3350 testutil.NewFunctionTestInput(types.New(types.T_varbinary, 10, 0), 3351 []string{"a", "s", ""}, 3352 []bool{false, false, true}), 3353 testutil.NewFunctionTestInput(types.T_int8.ToType(), 3354 []int8{0}, 3355 []bool{false}), 3356 }, 3357 expect: testutil.NewFunctionTestResult(types.T_int8.ToType(), false, 3358 []int8{97, 115, 0}, 3359 []bool{false, false, true}), 3360 }, 3361 { 3362 info: "test Decode Int16", 3363 inputs: []testutil.FunctionTestInput{ 3364 testutil.NewFunctionTestInput(types.New(types.T_varbinary, 10, 0), 3365 []string{"as", "df", ""}, 3366 []bool{false, false, true}), 3367 testutil.NewFunctionTestInput(types.T_int16.ToType(), 3368 []int16{0}, 3369 []bool{false}), 3370 }, 3371 expect: testutil.NewFunctionTestResult(types.T_int16.ToType(), false, 3372 []int16{29537, 26212, 0}, 3373 []bool{false, false, true}), 3374 }, 3375 { 3376 info: "test Decode Int32", 3377 inputs: []testutil.FunctionTestInput{ 3378 testutil.NewFunctionTestInput(types.New(types.T_varbinary, 10, 0), 3379 []string{"asdf", "jkl;", ""}, 3380 []bool{false, false, true}), 3381 testutil.NewFunctionTestInput(types.T_int32.ToType(), 3382 []int32{0}, 3383 []bool{false}), 3384 }, 3385 expect: testutil.NewFunctionTestResult(types.T_int32.ToType(), false, 3386 []int32{1717859169, 996961130, 0}, 3387 []bool{false, false, true}), 3388 }, 3389 { 3390 info: "test Decode Int64", 3391 inputs: []testutil.FunctionTestInput{ 3392 testutil.NewFunctionTestInput(types.New(types.T_varbinary, 10, 0), 3393 []string{"asdfjkl;", ""}, 3394 []bool{false, true}), 3395 testutil.NewFunctionTestInput(types.T_int64.ToType(), 3396 []int64{0}, 3397 []bool{false}), 3398 }, 3399 expect: testutil.NewFunctionTestResult(types.T_int64.ToType(), false, 3400 []int64{4281915450451063649, 0}, 3401 []bool{false, true}), 3402 }, 3403 } 3404 } 3405 3406 func TestBitCast(t *testing.T) { 3407 testCases := initBitCastTestCase() 3408 3409 // do the test work. 3410 proc := testutil.NewProcess() 3411 for _, tc := range testCases { 3412 fcTC := testutil.NewFunctionTestCase(proc, 3413 tc.inputs, tc.expect, BitCast) 3414 s, info := fcTC.Run() 3415 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 3416 } 3417 } 3418 3419 func initSHA1TestCase() []tcTemp { 3420 return []tcTemp{ 3421 { 3422 info: "test sha1", 3423 inputs: []testutil.FunctionTestInput{ 3424 testutil.NewFunctionTestInput(types.T_varchar.ToType(), 3425 []string{"abc", "", ""}, 3426 []bool{false, false, true}), 3427 }, 3428 expect: testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, 3429 []string{"a9993e364706816aba3e25717850c26c9cd0d89d", "da39a3ee5e6b4b0d3255bfef95601890afd80709", ""}, 3430 []bool{false, false, true}), 3431 }, 3432 } 3433 } 3434 3435 func TestSHA1(t *testing.T) { 3436 testCases := initSHA1TestCase() 3437 3438 proc := testutil.NewProcess() 3439 for _, tc := range testCases { 3440 fcTC := testutil.NewFunctionTestCase(proc, tc.inputs, tc.expect, SHA1Func) 3441 s, info := fcTC.Run() 3442 require.True(t, s, fmt.Sprintf("case is '%s', err info is '%s'", tc.info, info)) 3443 } 3444 }