Skip to content

Commit

Permalink
Use presto function
Browse files Browse the repository at this point in the history
  • Loading branch information
acvictor committed Mar 25, 2024
1 parent 8f0adb1 commit 9d2169d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
2 changes: 2 additions & 0 deletions velox/functions/sparksql/Register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ void registerFunctions(const std::string& prefix) {
registerFunction<FromUtcTimestampFunction, Timestamp, Timestamp, Varchar>(
{prefix + "from_utc_timestamp"});

registerFunction<CurrentDateFunction, Date>({prefix + "current_date"});

registerFunction<UnixDateFunction, int32_t, Date>({prefix + "unix_date"});

registerFunction<UnixTimestampFunction, int64_t>({prefix + "unix_timestamp"});
Expand Down
64 changes: 54 additions & 10 deletions velox/functions/sparksql/tests/DateTimeFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,14 @@
*/

#include "velox/common/base/tests/GTestUtils.h"
#include "velox/external/date/tz.h"
#include "velox/functions/sparksql/tests/SparkFunctionBaseTest.h"
#include "velox/type/Timestamp.h"
#include "velox/type/tz/TimeZoneMap.h"

namespace facebook::velox::functions::sparksql::test {
namespace {

std::string timestampToString(Timestamp ts) {
TimestampToStringOptions options;
options.mode = TimestampToStringOptions::Mode::kFull;
std::string result;
result.resize(getMaxStringLength(options));
const auto view = Timestamp::tsToStringView(ts, options, result.data());
result.resize(view.size());
return result;
}

class DateTimeFunctionsTest : public SparkFunctionBaseTest {
public:
static constexpr int32_t kMin = std::numeric_limits<int32_t>::min();
Expand All @@ -41,6 +32,25 @@ class DateTimeFunctionsTest : public SparkFunctionBaseTest {
static constexpr int8_t kMinTinyint = std::numeric_limits<int8_t>::min();
static constexpr int8_t kMaxTinyint = std::numeric_limits<int8_t>::max();

std::string timestampToString(Timestamp ts) {
TimestampToStringOptions options;
options.mode = TimestampToStringOptions::Mode::kFull;
std::string result;
result.resize(getMaxStringLength(options));
const auto view = Timestamp::tsToStringView(ts, options, result.data());
result.resize(view.size());
return result;
}

int32_t getCurrentDate(const std::optional<std::string>& timeZone) {
return parseDate(date::format(
"%Y-%m-%d",
timeZone.has_value()
? date::make_zoned(
timeZone.value(), std::chrono::system_clock::now())
: std::chrono::system_clock::now()));
}

protected:
void setQueryTimeZone(const std::string& timeZone) {
queryCtx_->testingOverrideConfigUnsafe({
Expand Down Expand Up @@ -208,6 +218,40 @@ TEST_F(DateTimeFunctionsTest, weekOfYear) {
EXPECT_EQ(15, weekOfYear("2013-04-08"));
}

TEST_F(DateTimeFunctionsTest, currentDate) {
// Since the execution of the code is slightly delayed, it is difficult for us
// to get the correct value of current_date. If you compare directly based on
// the current time, you may get wrong result at the last second of the day,
// and current_date may be the next day of the comparison value. In order to
// avoid this situation, we compute a new comparison value after the execution
// of current_date, so that the result of current_date is either consistent
// with the first comparison value or the second comparison value, and the
// difference between the two comparison values is at most one day.
auto emptyRowVector = makeRowVector(ROW({}), 1);

// Do not set the timezone, so the timezone obtained from QueryConfig
// will be nullptr.
auto dateBefore = getCurrentDate(std::nullopt);
auto result = evaluateOnce<int32_t>("current_date()", emptyRowVector);
auto dateAfter = getCurrentDate(std::nullopt);

EXPECT_TRUE(result.has_value());
EXPECT_LE(dateBefore, result);
EXPECT_LE(result, dateAfter);
EXPECT_LE(dateAfter - dateBefore, 1);

auto tz = "America/Los_Angeles";
setQueryTimeZone(tz);
dateBefore = getCurrentDate(tz);
result = evaluateOnce<int32_t>("current_date()", emptyRowVector);
dateAfter = getCurrentDate(tz);

EXPECT_TRUE(result.has_value());
EXPECT_LE(dateBefore, result);
EXPECT_LE(result, dateAfter);
EXPECT_LE(dateAfter - dateBefore, 1);
}

TEST_F(DateTimeFunctionsTest, unixDate) {
const auto unixDate = [&](std::string_view date) {
return evaluateOnce<int32_t, int32_t>(
Expand Down

0 comments on commit 9d2169d

Please sign in to comment.