Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support no hyphen included date format in castVARCHAR_date32_int64 and fix issue in unit test #56

Merged
merged 4 commits into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions cpp/src/gandiva/precompiled/time.cc
Original file line number Diff line number Diff line change
Expand Up @@ -685,9 +685,17 @@ const char* castVARCHAR_date32_int64(gdv_int64 context, gdv_date32 in_day,
const int char_buffer_length = kDateStringLen + 1; // snprintf adds \0
char char_buffer[char_buffer_length];

// yyyy-MM-dd hh:mm:ss.sss
int res = snprintf(char_buffer, char_buffer_length,
int res = -1;
if (length == 10) {
// yyyy-MM-dd
res = snprintf(char_buffer, char_buffer_length,
"%04" PRId64 "-%02" PRId64 "-%02" PRId64, year, month, day);
} else if (length == 8) {
// yyyyMMdd
res = snprintf(char_buffer, char_buffer_length,
"%04" PRId64 "%02" PRId64 "%02" PRId64, year, month, day);
}

if (res < 0) {
gdv_fn_context_set_error_msg(context, "Could not format the date");
return "";
Expand Down
15 changes: 13 additions & 2 deletions cpp/src/gandiva/precompiled/time_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,19 @@ TEST(TestTime, castVarcharDate) {
ExecutionContext context;
int64_t context_ptr = reinterpret_cast<int64_t>(&context);
gdv_int32 out_len;
gdv_date32 date = castDATE_utf8(context_ptr, "1967-12-1", 9);
const char* out = castVARCHAR_date32_int64(context_ptr, date, 10L, &out_len);
gdv_date64 date64 = castDATE_utf8(context_ptr, "2020-12-1", 9);
gdv_date32 date32 = castDATE32_date64(date64);
// Test yyyy-MM-dd format
const char* out = castVARCHAR_date32_int64(context_ptr, date32, 10L, &out_len);
EXPECT_EQ(std::string(out, out_len), "2020-12-01");
// Test yyyyMMdd format
out = castVARCHAR_date32_int64(context_ptr, date32, 8L, &out_len);
EXPECT_EQ(std::string(out, out_len), "20201201");

// Test date before unix epoch
date64 = castDATE_utf8(context_ptr, "1967-12-1", 9);
date32 = castDATE32_date64(date64);
out = castVARCHAR_date32_int64(context_ptr, date32, 10L, &out_len);
EXPECT_EQ(std::string(out, out_len), "1967-12-01");
}

Expand Down