Skip to content

Commit b8956fb

Browse files
DenisTarasyukxxlaykxx
authored andcommitted
apacheGH-37608: [C++][Gandiva] TO_DATE function supports YYYY-MM and YYYY (apache#37609) (#48)
Added fix for case when pattern does not contain day part ### Rationale for this change TO_DATE Gandiva function returns wrong result if used with pattern 'YYYY-MM' or 'YYYY'. ### What changes are included in this PR? Add a fix for case when tm_mday is zero to set it to 1 ### Are these changes tested? Added tests that cover described cases ### Are there any user-facing changes? No * Closes: apache#37608 Authored-by: DenisTarasyuk <131180287+DenisTarasyuk@users.noreply.github.com> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
1 parent c6d605c commit b8956fb

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

cpp/src/arrow/util/value_parsing.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ static inline bool ParseTimestampStrptime(const char* buf, size_t length,
801801
// ignore the time part
802802
arrow_vendored::date::sys_seconds secs =
803803
arrow_vendored::date::sys_days(arrow_vendored::date::year(result.tm_year + 1900) /
804-
(result.tm_mon + 1) / result.tm_mday);
804+
(result.tm_mon + 1) / std::max(result.tm_mday, 1));
805805
if (!ignore_time_in_day) {
806806
secs += (std::chrono::hours(result.tm_hour) + std::chrono::minutes(result.tm_min) +
807807
std::chrono::seconds(result.tm_sec));

cpp/src/gandiva/to_date_holder_test.cc

+29
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,33 @@ TEST_F(TestToDateHolder, TestSimpleDateTimeMakeError) {
149149
EXPECT_EQ(status.IsInvalid(), true) << status.message();
150150
}
151151

152+
TEST_F(TestToDateHolder, TestSimpleDateYearMonth) {
153+
std::shared_ptr<ToDateHolder> to_date_holder;
154+
ASSERT_OK(ToDateHolder::Make("YYYY-MM", 1, &to_date_holder));
155+
156+
auto& to_date = *to_date_holder;
157+
bool out_valid;
158+
std::string s("2012-12");
159+
int64_t millis_since_epoch =
160+
to_date(&execution_context_, s.data(), (int)s.length(), true, &out_valid);
161+
EXPECT_EQ(millis_since_epoch, 1354320000000);
162+
163+
s = std::string("2012-01");
164+
millis_since_epoch =
165+
to_date(&execution_context_, s.data(), (int)s.length(), true, &out_valid);
166+
EXPECT_EQ(millis_since_epoch, 1325376000000);
167+
}
168+
169+
TEST_F(TestToDateHolder, TestSimpleDateYear) {
170+
std::shared_ptr<ToDateHolder> to_date_holder;
171+
ASSERT_OK(ToDateHolder::Make("YYYY", 1, &to_date_holder));
172+
173+
auto& to_date = *to_date_holder;
174+
bool out_valid;
175+
std::string s("1999");
176+
int64_t millis_since_epoch =
177+
to_date(&execution_context_, s.data(), (int)s.length(), true, &out_valid);
178+
EXPECT_EQ(millis_since_epoch, 915148800000);
179+
}
180+
152181
} // namespace gandiva

0 commit comments

Comments
 (0)