Skip to content

Commit

Permalink
Add hour(timestamp_with_time_zone) -> int64 Presto function (#1230)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #1230

From discussions in [github issue](prestodb/presto#7122 (comment)), it says "Extracting hour from 2016-01-01 12:00:00 <TZ> should return 12 no matter what <TZ> is put in template.".  This diff implement that behavior.

Reviewed By: kagamiori

Differential Revision: D34944701

fbshipit-source-id: 4f9bad2de01432dce0ae158f84462a994ff5ddcc
  • Loading branch information
Chao Chen authored and facebook-github-bot committed Mar 23, 2022
1 parent 02c800b commit a6ff2af
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions velox/functions/prestosql/DateTimeFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,15 @@ struct HourFunction : public InitSessionTimezone<T> {
result = getDateTime(date).tm_hour;
return true;
}

FOLLY_ALWAYS_INLINE void call(
int64_t& result,
const arg_type<TimestampWithTimezone>& timestampWithTimezone) {
const auto milliseconds = *timestampWithTimezone.template at<0>();
Timestamp timestamp{milliseconds / kMillisecondsInSecond, 0UL};
timestamp.toTimezone(*timestampWithTimezone.template at<1>());
result = getDateTime(timestamp, nullptr).tm_hour;
}
};

template <typename T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void registerSimpleFunctions() {
registerFunction<DayOfYearFunction, int64_t, Date>({"doy", "day_of_year"});
registerFunction<HourFunction, int64_t, Timestamp>({"hour"});
registerFunction<HourFunction, int64_t, Date>({"hour"});
registerFunction<HourFunction, int64_t, TimestampWithTimezone>({"hour"});
registerFunction<MinuteFunction, int64_t, Timestamp>({"minute"});
registerFunction<MinuteFunction, int64_t, Date>({"minute"});
registerFunction<SecondFunction, int64_t, Timestamp>({"second"});
Expand Down
17 changes: 17 additions & 0 deletions velox/functions/prestosql/tests/DateTimeFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,23 @@ TEST_F(DateTimeFunctionsTest, hour) {
// EXPECT_EQ(21, hour(Timestamp(4000000000, 123000000)));
EXPECT_EQ(23, hour(Timestamp(998474645, 321000000)));
EXPECT_EQ(8, hour(Timestamp(998423705, 321000000)));

const auto hourWTZ = [&](double timestamp, std::string timeZoneName) {
return evaluateOnce<int64_t>(
"hour(from_unixtime(c0, c1))",
makeRowVector({
makeNullableFlatVector<double>({timestamp}),
makeNullableFlatVector<std::string>({timeZoneName}),
}));
};
EXPECT_EQ(20, hourWTZ(998423705, "+01:00"));
EXPECT_EQ(12, hourWTZ(41028, "+01:00"));
EXPECT_EQ(13, hourWTZ(41028, "+02:00"));
EXPECT_EQ(14, hourWTZ(41028, "+03:00"));
EXPECT_EQ(8, hourWTZ(41028, "-03:00"));
EXPECT_EQ(1, hourWTZ(41028, "+14:00"));
EXPECT_EQ(9, hourWTZ(-100, "-14:00"));
EXPECT_EQ(2, hourWTZ(-41028, "+14:00"));
}

TEST_F(DateTimeFunctionsTest, hourDate) {
Expand Down

0 comments on commit a6ff2af

Please sign in to comment.