Skip to content

Commit

Permalink
ARROW-5580: [C++][Gandiva] Support timestamp functions
Browse files Browse the repository at this point in the history
Match signatures of Gandiva timestamp functions with arrow

Author: Prudhvi Porandla <[email protected]>
Author: prudhvi <[email protected]>

Closes apache#4539 from pprudhvi/timestampGandivaFixes and squashes the following commits:

6d79279 <Prudhvi Porandla> remove unnecessary whitespaces in code
5062f70 <Prudhvi Porandla> add spaces
4d43ba2 <Prudhvi Porandla>   Timestamp arithmetic - Correct function definitions in Gandiva
c2795e7 <prudhvi> timestamp_arith: change method signatures in types.h, fix unit tests
59f5d00 <prudhvi> make gandiva timestamp arithmetic compatible with arrow/java
  • Loading branch information
pprudhvi authored and wesm committed Jun 17, 2019
1 parent a6f043a commit 73ceae4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 54 deletions.
16 changes: 8 additions & 8 deletions cpp/src/gandiva/function_registry_timestamp_arithmetic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
namespace gandiva {

#define TIMESTAMP_ADD_FNS(name) \
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, timestamp, int32, timestamp), \
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, date64, int32, date64), \
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, timestamp, int64, timestamp), \
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, date64, int64, date64)
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, int32, timestamp, timestamp), \
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, int32, date64, date64), \
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, int64, timestamp, timestamp), \
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, int64, date64, date64)

#define TIMESTAMP_DIFF_FN(name) \
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, timestamp, timestamp, int32)
Expand All @@ -40,10 +40,10 @@ namespace gandiva {
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, int64, timestamp, timestamp)

#define DATE_DIFF_FNS(name) \
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, date64, int32, date64), \
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, timestamp, int32, date64), \
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, date64, int64, date64), \
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, timestamp, int64, date64)
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, int32, date64, date64), \
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, int32, timestamp, date64), \
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, int64, date64, date64), \
BINARY_GENERIC_SAFE_NULL_IF_NULL(name, int64, timestamp, date64)

std::vector<NativeFunction> GetDateTimeArithmeticFunctionRegistry() {
static std::vector<NativeFunction> datetime_fn_registry_ = {
Expand Down
34 changes: 17 additions & 17 deletions cpp/src/gandiva/precompiled/time_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,50 +219,50 @@ TEST(TestTime, TimeStampTrunc) {

TEST(TestTime, TimeStampAdd) {
EXPECT_EQ(
timestampaddSecond_timestamp_int32(StringToTimestamp("2000-05-01 10:20:34"), 30),
timestampaddSecond_int32_timestamp(30, StringToTimestamp("2000-05-01 10:20:34")),
StringToTimestamp("2000-05-01 10:21:04"));

EXPECT_EQ(
timestampaddMinute_timestamp_int64(StringToTimestamp("2000-05-01 10:20:34"), -30),
timestampaddMinute_int64_timestamp(-30, StringToTimestamp("2000-05-01 10:20:34")),
StringToTimestamp("2000-05-01 09:50:34"));

EXPECT_EQ(
timestampaddHour_timestamp_int32(StringToTimestamp("2000-05-01 10:20:34"), 20),
timestampaddHour_int32_timestamp(20, StringToTimestamp("2000-05-01 10:20:34")),
StringToTimestamp("2000-05-02 06:20:34"));

EXPECT_EQ(
timestampaddDay_timestamp_int64(StringToTimestamp("2000-05-01 10:20:34"), -35),
timestampaddDay_int64_timestamp(-35, StringToTimestamp("2000-05-01 10:20:34")),
StringToTimestamp("2000-03-27 10:20:34"));

EXPECT_EQ(timestampaddWeek_timestamp_int32(StringToTimestamp("2000-05-01 10:20:34"), 4),
EXPECT_EQ(timestampaddWeek_int32_timestamp(4, StringToTimestamp("2000-05-01 10:20:34")),
StringToTimestamp("2000-05-29 10:20:34"));

EXPECT_EQ(
timestampaddMonth_timestamp_int64(StringToTimestamp("2000-05-01 10:20:34"), 10),
timestampaddMonth_int64_timestamp(10, StringToTimestamp("2000-05-01 10:20:34")),
StringToTimestamp("2001-03-01 10:20:34"));

EXPECT_EQ(
timestampaddQuarter_timestamp_int32(StringToTimestamp("2000-05-01 10:20:34"), -2),
timestampaddQuarter_int32_timestamp(-2, StringToTimestamp("2000-05-01 10:20:34")),
StringToTimestamp("1999-11-01 10:20:34"));

EXPECT_EQ(timestampaddYear_timestamp_int64(StringToTimestamp("2000-05-01 10:20:34"), 2),
EXPECT_EQ(timestampaddYear_int64_timestamp(2, StringToTimestamp("2000-05-01 10:20:34")),
StringToTimestamp("2002-05-01 10:20:34"));

EXPECT_EQ(
timestampaddQuarter_timestamp_int32(StringToTimestamp("2000-05-01 10:20:34"), -5),
timestampaddQuarter_int32_timestamp(-5, StringToTimestamp("2000-05-01 10:20:34")),
StringToTimestamp("1999-02-01 10:20:34"));
EXPECT_EQ(
timestampaddQuarter_timestamp_int32(StringToTimestamp("2000-05-01 10:20:34"), -6),
timestampaddQuarter_int32_timestamp(-6, StringToTimestamp("2000-05-01 10:20:34")),
StringToTimestamp("1998-11-01 10:20:34"));

// date_add
EXPECT_EQ(date_add_timestamp_int32(StringToTimestamp("2000-05-01 00:00:00"), 7),
EXPECT_EQ(date_add_int32_timestamp(7, StringToTimestamp("2000-05-01 00:00:00")),
StringToTimestamp("2000-05-08 00:00:00"));

EXPECT_EQ(add_int32_timestamp(4, StringToTimestamp("2000-05-01 00:00:00")),
StringToTimestamp("2000-05-05 00:00:00"));

EXPECT_EQ(add_timestamp_int64(StringToTimestamp("2000-05-01 00:00:00"), 7),
EXPECT_EQ(add_int64_timestamp(7, StringToTimestamp("2000-05-01 00:00:00")),
StringToTimestamp("2000-05-08 00:00:00"));

EXPECT_EQ(date_add_int64_timestamp(4, StringToTimestamp("2000-05-01 00:00:00")),
Expand All @@ -272,19 +272,19 @@ TEST(TestTime, TimeStampAdd) {
StringToTimestamp("2000-03-02 00:00:00"));

// date_sub
EXPECT_EQ(date_sub_timestamp_int32(StringToTimestamp("2000-05-01 00:00:00"), 7),
EXPECT_EQ(date_sub_int32_timestamp(7, StringToTimestamp("2000-05-01 00:00:00")),
StringToTimestamp("2000-04-24 00:00:00"));

EXPECT_EQ(subtract_timestamp_int32(StringToTimestamp("2000-05-01 00:00:00"), -7),
EXPECT_EQ(subtract_int32_timestamp(-7, StringToTimestamp("2000-05-01 00:00:00")),
StringToTimestamp("2000-05-08 00:00:00"));

EXPECT_EQ(date_diff_timestamp_int64(StringToTimestamp("2000-05-01 00:00:00"), 365),
EXPECT_EQ(date_diff_int64_timestamp(365, StringToTimestamp("2000-05-01 00:00:00")),
StringToTimestamp("1999-05-02 00:00:00"));

EXPECT_EQ(date_diff_timestamp_int64(StringToTimestamp("2000-03-01 00:00:00"), 1),
EXPECT_EQ(date_diff_int64_timestamp(1, StringToTimestamp("2000-03-01 00:00:00")),
StringToTimestamp("2000-02-29 00:00:00"));

EXPECT_EQ(date_diff_timestamp_int64(StringToTimestamp("2000-02-29 00:00:00"), 365),
EXPECT_EQ(date_diff_int64_timestamp(365, StringToTimestamp("2000-02-29 00:00:00")),
StringToTimestamp("1999-03-01 00:00:00"));
}

Expand Down
12 changes: 6 additions & 6 deletions cpp/src/gandiva/precompiled/timestamp_arithmetic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ TIMESTAMP_DIFF(timestamp)

#define ADD_INT32_TO_TIMESTAMP_FIXED_UNITS(TYPE, NAME, TO_MILLIS) \
FORCE_INLINE \
TYPE NAME##_##TYPE##_int32(TYPE millis, int32 count) { \
TYPE NAME##_int32_##TYPE(int32 count, TYPE millis) { \
return millis + TO_MILLIS * static_cast<TYPE>(count); \
}

Expand All @@ -110,21 +110,21 @@ TIMESTAMP_DIFF(timestamp)
// since the input millis are since epoch
#define ADD_INT32_TO_TIMESTAMP_MONTH_UNITS(TYPE, NAME, N_MONTHS) \
FORCE_INLINE \
TYPE NAME##_##TYPE##_int32(TYPE millis, int32 count) { \
TYPE NAME##_int32_##TYPE(int32 count, TYPE millis) { \
EpochTimePoint tp(millis); \
return tp.AddMonths(static_cast<int>(count * N_MONTHS)).MillisSinceEpoch(); \
}

// TODO: Handle overflow while converting int64 to millis
#define ADD_INT64_TO_TIMESTAMP_FIXED_UNITS(TYPE, NAME, TO_MILLIS) \
FORCE_INLINE \
TYPE NAME##_##TYPE##_int64(TYPE millis, int64 count) { \
TYPE NAME##_int64_##TYPE(int64 count, TYPE millis) { \
return millis + TO_MILLIS * static_cast<TYPE>(count); \
}

#define ADD_INT64_TO_TIMESTAMP_MONTH_UNITS(TYPE, NAME, N_MONTHS) \
FORCE_INLINE \
TYPE NAME##_##TYPE##_int64(TYPE millis, int64 count) { \
TYPE NAME##_int64_##TYPE(int64 count, TYPE millis) { \
EpochTimePoint tp(millis); \
return tp.AddMonths(static_cast<int>(count * N_MONTHS)).MillisSinceEpoch(); \
}
Expand Down Expand Up @@ -186,13 +186,13 @@ ADD_INT64_TO_TIMESTAMP_FIXED_UNITS(timestamp, date_diff, -1 * MILLIS_IN_DAY)

#define ADD_TIMESTAMP_TO_INT32_FIXED_UNITS(TYPE, NAME, TO_MILLIS) \
FORCE_INLINE \
TYPE NAME##_int32_##TYPE(int32 count, TYPE millis) { \
TYPE NAME##_##TYPE##_int32(TYPE millis, int32 count) { \
return millis + TO_MILLIS * (TYPE)count; \
}

#define ADD_TIMESTAMP_TO_INT64_FIXED_UNITS(TYPE, NAME, TO_MILLIS) \
FORCE_INLINE \
TYPE NAME##_int64_##TYPE(int64 count, TYPE millis) { \
TYPE NAME##_##TYPE##_int64(TYPE millis, int64 count) { \
return millis + TO_MILLIS * (TYPE)count; \
}

Expand Down
46 changes: 23 additions & 23 deletions cpp/src/gandiva/precompiled/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,32 @@ int32 hash32_buf(const uint8* buf, int len, int32 seed);
int64 hash64(double val, int64 seed);
int64 hash64_buf(const uint8* buf, int len, int64 seed);

int64 timestampaddSecond_timestamp_int32(timestamp, int32);
int64 timestampaddMinute_timestamp_int32(timestamp, int32);
int64 timestampaddHour_timestamp_int32(timestamp, int32);
int64 timestampaddDay_timestamp_int32(timestamp, int32);
int64 timestampaddWeek_timestamp_int32(timestamp, int32);
int64 timestampaddMonth_timestamp_int32(timestamp, int32);
int64 timestampaddQuarter_timestamp_int32(timestamp, int32);
int64 timestampaddYear_timestamp_int32(timestamp, int32);

int64 timestampaddSecond_timestamp_int64(timestamp, int64);
int64 timestampaddMinute_timestamp_int64(timestamp, int64);
int64 timestampaddHour_timestamp_int64(timestamp, int64);
int64 timestampaddDay_timestamp_int64(timestamp, int64);
int64 timestampaddWeek_timestamp_int64(timestamp, int64);
int64 timestampaddMonth_timestamp_int64(timestamp, int64);
int64 timestampaddQuarter_timestamp_int64(timestamp, int64);
int64 timestampaddYear_timestamp_int64(timestamp, int64);

int64 date_add_timestamp_int32(timestamp, int32);
int64 add_timestamp_int64(timestamp, int64);
int64 timestampaddSecond_int32_timestamp(int32, timestamp);
int64 timestampaddMinute_int32_timestamp(int32, timestamp);
int64 timestampaddHour_int32_timestamp(int32, timestamp);
int64 timestampaddDay_int32_timestamp(int32, timestamp);
int64 timestampaddWeek_int32_timestamp(int32, timestamp);
int64 timestampaddMonth_int32_timestamp(int32, timestamp);
int64 timestampaddQuarter_int32_timestamp(int32, timestamp);
int64 timestampaddYear_int32_timestamp(int32, timestamp);

int64 timestampaddSecond_int64_timestamp(int64, timestamp);
int64 timestampaddMinute_int64_timestamp(int64, timestamp);
int64 timestampaddHour_int64_timestamp(int64, timestamp);
int64 timestampaddDay_int64_timestamp(int64, timestamp);
int64 timestampaddWeek_int64_timestamp(int64, timestamp);
int64 timestampaddMonth_int64_timestamp(int64, timestamp);
int64 timestampaddQuarter_int64_timestamp(int64, timestamp);
int64 timestampaddYear_int64_timestamp(int64, timestamp);

int64 date_add_int32_timestamp(int32, timestamp);
int64 add_int64_timestamp(int64, timestamp);
int64 add_int32_timestamp(int32, timestamp);
int64 date_add_int64_timestamp(int64, timestamp);

int64 date_sub_timestamp_int32(timestamp, int32);
int64 subtract_timestamp_int32(timestamp, int32);
int64 date_diff_timestamp_int64(timestamp, int64);
int64 date_sub_int32_timestamp(int32, timestamp);
int64 subtract_int32_timestamp(int32, timestamp);
int64 date_diff_int64_timestamp(int64, timestamp);

bool is_distinct_from_timestamp_timestamp(int64, bool, int64, bool);
bool is_not_distinct_from_int32_int32(int32, bool, int32, bool);
Expand Down

0 comments on commit 73ceae4

Please sign in to comment.