|
21 | 21 | #include <gmock/gmock.h> |
22 | 22 | #include <gtest/gtest.h> |
23 | 23 |
|
| 24 | +#include <limits> |
| 25 | + |
24 | 26 | #include "arrow/util/logging.h" |
25 | 27 | #include "gandiva/execution_context.h" |
26 | 28 | #include "gandiva/encrypt_utils_ecb.h" |
@@ -353,6 +355,14 @@ TEST(TestGdvFnStubs, TestCastVARCHARFromInt64) { |
353 | 355 | out_str = gdv_fn_castVARCHAR_int64_int64(ctx_ptr, 12345, 3, &out_len); |
354 | 356 | EXPECT_EQ(std::string(out_str, out_len), "123"); |
355 | 357 | EXPECT_FALSE(ctx.has_error()); |
| 358 | + |
| 359 | + out_str = gdv_fn_castVARCHAR_int64_int64(ctx_ptr, 347, 0, &out_len); |
| 360 | + EXPECT_EQ(std::string(out_str, out_len), ""); |
| 361 | + EXPECT_FALSE(ctx.has_error()); |
| 362 | + |
| 363 | + out_str = gdv_fn_castVARCHAR_int64_int64(ctx_ptr, 347, -1, &out_len); |
| 364 | + EXPECT_THAT(ctx.get_error(), ::testing::HasSubstr("Buffer length cannot be negative")); |
| 365 | + ctx.Reset(); |
356 | 366 | } |
357 | 367 |
|
358 | 368 | TEST(TestGdvFnStubs, TestCastVARCHARFromMilliseconds) { |
@@ -384,6 +394,15 @@ TEST(TestGdvFnStubs, TestCastVARCHARFromMilliseconds) { |
384 | 394 | out_str = gdv_fn_castVARCHAR_date64_int64(ctx_ptr, ts, 4, &out_len); |
385 | 395 | EXPECT_EQ(std::string(out_str, out_len), "2008"); |
386 | 396 | EXPECT_FALSE(ctx.has_error()); |
| 397 | + |
| 398 | + ts = StringToTimestamp("2021-04-23 10:20:33"); |
| 399 | + out_str = gdv_fn_castVARCHAR_date64_int64(ctx_ptr, ts, 0, &out_len); |
| 400 | + EXPECT_EQ(std::string(out_str, out_len), ""); |
| 401 | + EXPECT_FALSE(ctx.has_error()); |
| 402 | + |
| 403 | + out_str = gdv_fn_castVARCHAR_date64_int64(ctx_ptr, ts, -1, &out_len); |
| 404 | + EXPECT_THAT(ctx.get_error(), ::testing::HasSubstr("Buffer length cannot be negative")); |
| 405 | + ctx.Reset(); |
387 | 406 | } |
388 | 407 |
|
389 | 408 | TEST(TestGdvFnStubs, TestCastVARCHARFromFloat) { |
@@ -419,6 +438,14 @@ TEST(TestGdvFnStubs, TestCastVARCHARFromFloat) { |
419 | 438 | out_str = gdv_fn_castVARCHAR_float32_int64(ctx_ptr, 1.2345f, 3, &out_len); |
420 | 439 | EXPECT_EQ(std::string(out_str, out_len), "1.2"); |
421 | 440 | EXPECT_FALSE(ctx.has_error()); |
| 441 | + |
| 442 | + out_str = gdv_fn_castVARCHAR_float32_int64(ctx_ptr, 1.2345f, 0, &out_len); |
| 443 | + EXPECT_EQ(std::string(out_str, out_len), ""); |
| 444 | + EXPECT_FALSE(ctx.has_error()); |
| 445 | + |
| 446 | + out_str = gdv_fn_castVARCHAR_float32_int64(ctx_ptr, 1.2345f, -1, &out_len); |
| 447 | + EXPECT_THAT(ctx.get_error(), ::testing::HasSubstr("Buffer length cannot be negative")); |
| 448 | + ctx.Reset(); |
422 | 449 | } |
423 | 450 |
|
424 | 451 | TEST(TestGdvFnStubs, TestCastVARCHARFromDouble) { |
@@ -454,6 +481,25 @@ TEST(TestGdvFnStubs, TestCastVARCHARFromDouble) { |
454 | 481 | out_str = gdv_fn_castVARCHAR_float64_int64(ctx_ptr, 1.2345, 3, &out_len); |
455 | 482 | EXPECT_EQ(std::string(out_str, out_len), "1.2"); |
456 | 483 | EXPECT_FALSE(ctx.has_error()); |
| 484 | + |
| 485 | + out_str = gdv_fn_castVARCHAR_float64_int64(ctx_ptr, 1.2345, 0, &out_len); |
| 486 | + EXPECT_EQ(std::string(out_str, out_len), ""); |
| 487 | + EXPECT_FALSE(ctx.has_error()); |
| 488 | + |
| 489 | + out_str = gdv_fn_castVARCHAR_float64_int64(ctx_ptr, 1.2345, -1, &out_len); |
| 490 | + EXPECT_THAT(ctx.get_error(), ::testing::HasSubstr("Buffer length cannot be negative")); |
| 491 | + ctx.Reset(); |
| 492 | + |
| 493 | + // test long repeating decimal (1/3) with large buffer |
| 494 | + out_str = gdv_fn_castVARCHAR_float64_int64(ctx_ptr, 1.0 / 3.0, 100, &out_len); |
| 495 | + EXPECT_EQ(std::string(out_str, out_len), "0.3333333333333333"); |
| 496 | + EXPECT_FALSE(ctx.has_error()); |
| 497 | + |
| 498 | + // test exponential notation with large negative exponent (24 chars) |
| 499 | + out_str = |
| 500 | + gdv_fn_castVARCHAR_float64_int64(ctx_ptr, -1.2345678901234567e-100, 100, &out_len); |
| 501 | + EXPECT_EQ(std::string(out_str, out_len), "-1.2345678901234567E-100"); |
| 502 | + EXPECT_FALSE(ctx.has_error()); |
457 | 503 | } |
458 | 504 |
|
459 | 505 | TEST(TestGdvFnStubs, TestSubstringIndex) { |
@@ -529,6 +575,21 @@ TEST(TestGdvFnStubs, TestSubstringIndex) { |
529 | 575 | out_str = gdv_fn_substring_index(ctx_ptr, "路学\\L", 8, "\\", 1, -1, &out_len); |
530 | 576 | EXPECT_EQ(std::string(out_str, out_len), "L"); |
531 | 577 | EXPECT_FALSE(ctx.has_error()); |
| 578 | + |
| 579 | + // Large counts return full string when delimiter not found enough times |
| 580 | + out_str = gdv_fn_substring_index(ctx_ptr, "a.b.c", 5, ".", 1, -1000, &out_len); |
| 581 | + EXPECT_EQ(std::string(out_str, out_len), "a.b.c"); |
| 582 | + EXPECT_FALSE(ctx.has_error()); |
| 583 | + |
| 584 | + out_str = gdv_fn_substring_index(ctx_ptr, "a.b.c", 5, ".", 1, |
| 585 | + std::numeric_limits<int32_t>::max(), &out_len); |
| 586 | + EXPECT_EQ(std::string(out_str, out_len), "a.b.c"); |
| 587 | + EXPECT_FALSE(ctx.has_error()); |
| 588 | + |
| 589 | + out_str = gdv_fn_substring_index(ctx_ptr, "a.b.c", 5, ".", 1, |
| 590 | + std::numeric_limits<int32_t>::min(), &out_len); |
| 591 | + EXPECT_EQ(std::string(out_str, out_len), "a.b.c"); |
| 592 | + EXPECT_FALSE(ctx.has_error()); |
532 | 593 | } |
533 | 594 |
|
534 | 595 | TEST(TestGdvFnStubs, TestUpper) { |
|
0 commit comments