You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/05/02 07:50:43 UTC

[GitHub] [arrow] projjal commented on a change in pull request #9890: ARROW-12205: [C++][Gandiva] Implement TO_TIME([number] seconds) and TO_TIMESTAMP([number] seconds) function

projjal commented on a change in pull request #9890:
URL: https://github.com/apache/arrow/pull/9890#discussion_r624649920



##########
File path: cpp/src/gandiva/precompiled/time_test.cc
##########
@@ -743,4 +743,89 @@ TEST(TestTime, TestLastDay) {
   EXPECT_EQ(StringToTimestamp("2015-12-31 00:00:00"), out);
 }
 
+TEST(TestTime, TestToTimestamp) {
+  auto ts = StringToTimestamp("1970-01-01 00:00:00");
+  EXPECT_EQ(ts, to_timestamp_int32(0));
+  EXPECT_EQ(ts, to_timestamp_int64(0));
+  EXPECT_EQ(ts, to_timestamp_float32(0));
+  EXPECT_EQ(ts, to_timestamp_float64(0));
+
+  ts = StringToTimestamp("1970-01-01 00:00:01");
+  EXPECT_EQ(ts, to_timestamp_int32(1));
+  EXPECT_EQ(ts, to_timestamp_int64(1));
+  EXPECT_EQ(ts, to_timestamp_float32(1));
+  EXPECT_EQ(ts, to_timestamp_float64(1));
+
+  ts = StringToTimestamp("1970-01-01 00:01:00");
+  EXPECT_EQ(ts, to_timestamp_int32(60));
+  EXPECT_EQ(ts, to_timestamp_int64(60));
+  EXPECT_EQ(ts, to_timestamp_float32(60));
+  EXPECT_EQ(ts, to_timestamp_float64(60));
+
+  ts = StringToTimestamp("1970-01-01 01:00:00");
+  EXPECT_EQ(ts, to_timestamp_int32(3600));
+  EXPECT_EQ(ts, to_timestamp_int64(3600));
+  EXPECT_EQ(ts, to_timestamp_float32(3600));
+  EXPECT_EQ(ts, to_timestamp_float64(3600));
+
+  ts = StringToTimestamp("1970-01-02 00:00:00");
+  EXPECT_EQ(ts, to_timestamp_int32(86400));
+  EXPECT_EQ(ts, to_timestamp_int64(86400));
+  EXPECT_EQ(ts, to_timestamp_float32(86400));
+  EXPECT_EQ(ts, to_timestamp_float64(86400));
+
+  // tests with fractional part
+  ts = StringToTimestamp("1970-01-01 00:00:01") + 500;
+  EXPECT_EQ(ts, to_timestamp_float32(1.500f));
+  EXPECT_EQ(ts, to_timestamp_float64(1.500));
+
+  ts = StringToTimestamp("1970-01-01 00:01:01") + 600;
+  EXPECT_EQ(ts, to_timestamp_float32(61.600f));
+  EXPECT_EQ(ts, to_timestamp_float64(61.600));
+
+  ts = StringToTimestamp("1970-01-01 01:00:01") + 400;
+  EXPECT_EQ(ts, to_timestamp_float32(3601.400f));
+  EXPECT_EQ(ts, to_timestamp_float64(3601.400));
+}
+
+TEST(TestTime, TestToTimeNumeric) {
+
+  auto ts = StringToTimestamp("1970-01-02 00:00:00") / 1000;
+  EXPECT_EQ(0, to_time_int32(ts));
+  EXPECT_EQ(0, to_time_int64(ts));
+  EXPECT_EQ(0, to_time_float32(ts));
+  EXPECT_EQ(0, to_time_float64(ts));
+
+  ts = StringToTimestamp("1970-01-01 00:00:01") / 1000;
+  EXPECT_EQ(1000, to_time_int32(ts));
+  EXPECT_EQ(1000, to_time_int64(ts));
+  EXPECT_EQ(1000, to_time_float32(ts));
+  EXPECT_EQ(1000, to_time_float64(ts));
+
+  ts = StringToTimestamp("1970-01-01 01:00:00") / 1000;
+  EXPECT_EQ(3600000, to_time_int32(ts));
+  EXPECT_EQ(3600000, to_time_int64(ts));
+  EXPECT_EQ(3600000, to_time_float32(ts));
+  EXPECT_EQ(3600000, to_time_float64(ts));
+
+  ts = StringToTimestamp("1970-01-01 23:59:59") / 1000;
+  EXPECT_EQ(86399000, to_time_int32(ts));
+  EXPECT_EQ(86399000, to_time_int64(ts));
+  EXPECT_EQ(86399000, to_time_float32(ts));
+  EXPECT_EQ(86399000, to_time_float64(ts));
+
+  // tests with fractional part
+  ts = StringToTimestamp("1970-01-01 00:00:01") + 500;
+  EXPECT_EQ(ts, to_time_float32(ts/1000.0));
+  EXPECT_EQ(ts, to_time_float64(ts/1000.0));
+
+  ts = StringToTimestamp("1970-01-01 00:01:01") + 600;
+  EXPECT_EQ(ts, to_time_float32(ts/1000.0));
+  EXPECT_EQ(ts, to_time_float64(ts/1000.0));
+
+  ts = StringToTimestamp("1970-01-01 01:00:01") + 400;

Review comment:
       Can you add to_time tests with years greater than 1970. Getting the expected values for them should be easy.

##########
File path: cpp/src/gandiva/precompiled/time.cc
##########
@@ -828,4 +841,23 @@ gdv_int64 castBIGINT_daytimeinterval(gdv_day_time_interval in) {
          extractDay_daytimeinterval(in) * MILLIS_IN_DAY;
 }
 
+// Convert the seconds since epoch argument to timestamp
+#define TO_TIMESTAMP(TYPE)                                      \
+  FORCE_INLINE                                                  \
+  gdv_timestamp to_timestamp##_##TYPE(gdv_##TYPE seconds) {     \
+    return static_cast<gdv_timestamp>(seconds * MILLIS_IN_SEC); \
+  }
+
+NUMERIC_TYPES(TO_TIMESTAMP)
+
+// Convert the seconds since epoch argument to time
+#define TO_TIME(TYPE)                                                        \
+  FORCE_INLINE                                                               \
+  gdv_timestamp to_time##_##TYPE(gdv_##TYPE seconds) {                       \
+    EpochTimePoint tp(seconds * MILLIS_IN_SEC);                              \

Review comment:
       static_cast<int64_t>(seconds * MILLIS_IN_SEC)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org