Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE committed Sep 26, 2023
1 parent 1910a17 commit feb53da
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
42 changes: 42 additions & 0 deletions velox/functions/sparksql/DateTimeFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,46 @@ struct DateDiffFunction {
}
};

template <typename T>
struct DateFormatFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);

const date::time_zone* sessionTimeZone_ = nullptr;
std::shared_ptr<DateTimeFormatter> formatter_;
bool isConstFormat_ = false;

FOLLY_ALWAYS_INLINE void setFormatter(const arg_type<Varchar>* formatString) {
if (formatString != nullptr) {
formatter_ = buildJodaDateTimeFormatter(
std::string_view(formatString->data(), formatString->size()));
isConstFormat_ = true;
}
}

FOLLY_ALWAYS_INLINE void initialize(
const core::QueryConfig& config,
const arg_type<Timestamp>* /*timestamp*/,
const arg_type<Varchar>* formatString) {
sessionTimeZone_ = getTimeZoneFromConfig(config);
setFormatter(formatString);
}

FOLLY_ALWAYS_INLINE void call(
out_type<Varchar>& result,
const arg_type<Timestamp>& timestamp,
const arg_type<Varchar>& formatString) {
if (!isConstFormat_) {
formatter_ = buildJodaDateTimeFormatter(
std::string_view(formatString.data(), formatString.size()));
}

auto formattedResult = formatter_->format(timestamp, sessionTimeZone_);
auto resultSize = formattedResult.size();
result.resize(resultSize);
if (resultSize != 0) {
std::memcpy(result.data(), formattedResult.data(), resultSize);
}
}
};

} // namespace facebook::velox::functions::sparksql
2 changes: 2 additions & 0 deletions velox/functions/sparksql/Register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ void registerFunctions(const std::string& prefix) {
registerFunction<DateAddFunction, Date, Date, int16_t>({"date_add"});
registerFunction<DateAddFunction, Date, Date, int8_t>({"date_add"});
registerFunction<DateDiffFunction, int32_t, Date, Date>({"date_diff"});
registerFunction<DateFormatFunction, Varchar, Timestamp, Varchar>(
{"date_format"});
registerFunction<Atan2FunctionIgnoreZeroSign, double, double, double>(
{prefix + "atan2"});
registerFunction<Log2FunctionNaNAsNull, double, double>({prefix + "log2"});
Expand Down
12 changes: 12 additions & 0 deletions velox/functions/sparksql/tests/DateTimeFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <stdint.h>
#include "velox/common/base/tests/GTestUtils.h"
#include "velox/functions/sparksql/tests/SparkFunctionBaseTest.h"
#include "velox/type/TimestampConversion.h"
#include "velox/type/tz/TimeZoneMap.h"

namespace facebook::velox::functions::sparksql::test {
Expand Down Expand Up @@ -271,5 +272,16 @@ TEST_F(DateTimeFunctionsTest, dateDiff) {
EXPECT_EQ(-366, dateDiff(parseDate("2020-02-29"), parseDate("2019-02-28")));
}

TEST_F(DateTimeFunctionsTest, dateFormat) {
const auto dateFormat = [&](std::optional<Timestamp> timestamp,
const std::string& formatString) {
return evaluateOnce<std::string>(
fmt::format("date_format(c0, '{}')", formatString), timestamp);
};
using util::fromTimestampString;

EXPECT_EQ("1970", dateFormat(fromTimestampString("1970-01-01"), "y"));
}

} // namespace
} // namespace facebook::velox::functions::sparksql::test

0 comments on commit feb53da

Please sign in to comment.