Skip to content

Commit

Permalink
ARROW-16051: [Gandiva][C++] Fix datediff regression build
Browse files Browse the repository at this point in the history
Modifying the order to subtract in Arrow implementation for DateDiff function.

As is:
Hive implementation: 1st parameter - 2st parameter
Arrow implementation:  2st parameter - 1st parameter.

To be:
Hive implementation: 1st parameter - 2st parameter
Arrow implementation:  1st parameter - 2st parameter.

Closes #12736 from Johnnathanalmeida/fixbug/DateDiff

Authored-by: Johnnathan <[email protected]>
Signed-off-by: Pindikura Ravindra <[email protected]>
  • Loading branch information
Johnnathanalmeida authored and Pindikura Ravindra committed Apr 19, 2022
1 parent 04e2c92 commit 6c10a38
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cpp/src/gandiva/function_registry_datetime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ std::vector<NativeFunction> GetDateTimeFunctionRegistry() {
NativeFunction::kNeedsContext | NativeFunction::kNeedsFunctionHolder |
NativeFunction::kCanReturnErrors),

NativeFunction("datediff", {}, DataTypeVector{timestamp(), timestamp()}, int32(),
kResultNullIfNull, "datediff_timestamp_timestamp"),

DATE_TYPES(LAST_DAY_SAFE_NULL_IF_NULL, last_day, {}),
BASE_NUMERIC_TYPES(TO_TIME_SAFE_NULL_IF_NULL, to_time, {}),
BASE_NUMERIC_TYPES(TO_TIMESTAMP_SAFE_NULL_IF_NULL, to_timestamp, {})};
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/gandiva/function_registry_timestamp_arithmetic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ std::vector<NativeFunction> GetDateTimeArithmeticFunctionRegistry() {
TIMESTAMP_DIFF_FN(timestampdiffSecond, {}),
TIMESTAMP_DIFF_FN(timestampdiffMinute, {}),
TIMESTAMP_DIFF_FN(timestampdiffHour, {}),
TIMESTAMP_DIFF_FN(timestampdiffDay, {"datediff"}),
TIMESTAMP_DIFF_FN(timestampdiffDay, {}),
TIMESTAMP_DIFF_FN(timestampdiffWeek, {}),
TIMESTAMP_DIFF_FN(timestampdiffMonth, {}),
TIMESTAMP_DIFF_FN(timestampdiffQuarter, {}),
Expand Down
7 changes: 7 additions & 0 deletions cpp/src/gandiva/precompiled/time.cc
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,13 @@ CAST_NULLABLE_INTERVAL_DAY(int64)
return value; \
}

FORCE_INLINE
gdv_int32 datediff_timestamp_timestamp(gdv_timestamp start_millis,
gdv_timestamp end_millis) {
return static_cast<int32_t>(
((start_millis - end_millis) / (24 * (60 * (60 * (1000))))));
}

CAST_NULLABLE_INTERVAL_YEAR(int32)
CAST_NULLABLE_INTERVAL_YEAR(int64)

Expand Down
34 changes: 34 additions & 0 deletions cpp/src/gandiva/precompiled/time_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,40 @@ TEST(TestTime, TestExtractTime) {
EXPECT_EQ(extractSecond_time32(time_as_millis_in_day), 33);
}

TEST(TestTime, TestDateDiff) {
gdv_timestamp ts1 = StringToTimestamp("2019-06-30 00:00:00");
gdv_timestamp ts2 = StringToTimestamp("2019-05-31 00:00:00");
EXPECT_EQ(datediff_timestamp_timestamp(ts1, ts2), 30);

ts1 = StringToTimestamp("2019-06-30 00:00:00");
ts2 = StringToTimestamp("2019-02-28 00:00:00");
EXPECT_EQ(datediff_timestamp_timestamp(ts1, ts2), 122);

ts1 = StringToTimestamp("2019-06-30 00:00:00");
ts2 = StringToTimestamp("2019-03-31 00:00:00");
EXPECT_EQ(datediff_timestamp_timestamp(ts1, ts2), 91);

ts1 = StringToTimestamp("2019-06-30 00:00:00");
ts2 = StringToTimestamp("2019-06-30 00:00:00");
EXPECT_EQ(datediff_timestamp_timestamp(ts1, ts2), 0);

ts1 = StringToTimestamp("2019-06-30 00:00:00");
ts2 = StringToTimestamp("2019-07-01 00:00:00");
EXPECT_EQ(datediff_timestamp_timestamp(ts1, ts2), -1);

ts1 = StringToTimestamp("2019-06-30 00:00:00");
ts2 = StringToTimestamp("2019-07-31 00:00:00");
EXPECT_EQ(datediff_timestamp_timestamp(ts1, ts2), -31);

ts1 = StringToTimestamp("2019-06-30 00:00:00");
ts2 = StringToTimestamp("2019-07-30 00:00:00");
EXPECT_EQ(datediff_timestamp_timestamp(ts1, ts2), -30);

ts1 = StringToTimestamp("2019-06-30 00:00:00");
ts2 = StringToTimestamp("2019-07-29 00:00:00");
EXPECT_EQ(datediff_timestamp_timestamp(ts1, ts2), -29);
}

TEST(TestTime, TestTimestampDiffMonth) {
gdv_timestamp ts1 = StringToTimestamp("2019-06-30 00:00:00");
gdv_timestamp ts2 = StringToTimestamp("2019-05-31 00:00:00");
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/gandiva/precompiled/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ gdv_int64 date_trunc_Year_date64(gdv_date64);
gdv_int64 date_trunc_Decade_date64(gdv_date64);
gdv_int64 date_trunc_Century_date64(gdv_date64);
gdv_int64 date_trunc_Millennium_date64(gdv_date64);
gdv_int32 datediff_timestamp_timestamp(gdv_timestamp start_millis,
gdv_timestamp end_millis);

gdv_int64 date_trunc_Week_timestamp(gdv_timestamp);
double months_between_timestamp_timestamp(gdv_uint64, gdv_uint64);
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/gandiva/tests/date_time_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ TEST_F(TestProjector, TestTimestampDiff) {
exp_output.push_back(MakeArrowArrayInt32({816601, -816601, 0, -23 * 60}, validity));
exp_output.push_back(MakeArrowArrayInt32({13610, -13610, 0, -23}, validity));
exp_output.push_back(MakeArrowArrayInt32({567, -567, 0, 0}, validity));
exp_output.push_back(MakeArrowArrayInt32({567, -567, 0, 0}, validity));
exp_output.push_back(MakeArrowArrayInt32({-567, 567, 0, 0}, validity));
exp_output.push_back(MakeArrowArrayInt32({81, -81, 0, 0}, validity));
exp_output.push_back(MakeArrowArrayInt32({18, -18, 0, 0}, validity));
exp_output.push_back(MakeArrowArrayInt32({6, -6, 0, 0}, validity));
Expand Down

0 comments on commit 6c10a38

Please sign in to comment.