Skip to content

Commit

Permalink
Add millisecond Presto function (#275)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: #275

Reviewed By: mbasmanova

Differential Revision: D31122342

fbshipit-source-id: a137f16043ba217364d94df0c4342495cc58792e
  • Loading branch information
Aniket Mokashi authored and facebook-github-bot committed Sep 23, 2021
1 parent 5b68ac5 commit 4e7bbec
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions velox/docs/functions/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ Date and Time Functions
.. function:: to_unixtime(timestamp) -> double

Returns ``timestamp`` as a UNIX timestamp.

.. function:: millisecond(timestamp) -> int64

Returns the millisecond of the second from ``timestamp``.
1 change: 1 addition & 0 deletions velox/functions/prestosql/CoreFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void registerFunctions() {
registerFunction<udf_to_unixtime, double, Timestamp>(
{"to_unixtime", "to_unix_timestamp"});
registerFunction<udf_from_unixtime, Timestamp, double>();
registerFunction<udf_millisecond, int64_t, Timestamp>();

registerArithmeticFunctions();
registerCheckedArithmeticFunctions();
Expand Down
12 changes: 11 additions & 1 deletion velox/functions/prestosql/DateTimeImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
namespace facebook::velox::functions {
namespace {
constexpr double kNanosecondsInSecond = 1'000'000'000;
}
constexpr int64_t kNanosecondsInMilliseconds = 1'000'000;
} // namespace

FOLLY_ALWAYS_INLINE double toUnixtime(const Timestamp& timestamp) {
double result = timestamp.getSeconds();
Expand All @@ -34,4 +35,13 @@ FOLLY_ALWAYS_INLINE Timestamp fromUnixtime(double unixtime) {
return Timestamp(seconds, nanos * kNanosecondsInSecond);
}

VELOX_UDF_BEGIN(millisecond)
FOLLY_ALWAYS_INLINE bool call(
int64_t& result,
const arg_type<Timestamp>& timestamp) {
result = timestamp.getNanos() / kNanosecondsInMilliseconds;
return true;
}
VELOX_UDF_END();

} // namespace facebook::velox::functions
12 changes: 12 additions & 0 deletions velox/functions/prestosql/tests/DateTimeFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "velox/functions/prestosql/TimestampWithTimeZoneType.h"
#include "velox/functions/prestosql/tests/FunctionBaseTest.h"
#include "velox/type/Timestamp.h"

using namespace facebook::velox;

Expand Down Expand Up @@ -102,3 +103,14 @@ TEST_F(DateTimeFunctionsTest, fromUnixtimeWithTimeZone) {
assertEqualVectors(expected, result);
}
}

TEST_F(DateTimeFunctionsTest, millisecond) {
const auto millisecond = [&](std::optional<Timestamp> timestamp) {
return evaluateOnce<int64_t>("millisecond(c0)", timestamp);
};
EXPECT_EQ(std::nullopt, millisecond(std::nullopt));
EXPECT_EQ(0, millisecond(Timestamp(0, 0)));
EXPECT_EQ(0, millisecond(Timestamp(4000000000, 0)));
EXPECT_EQ(123, millisecond(Timestamp(-1, 123000000)));
EXPECT_EQ(12300, millisecond(Timestamp(-1, 12300000000)));
}

0 comments on commit 4e7bbec

Please sign in to comment.