Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE committed Apr 24, 2024
1 parent ba8253b commit f3aac93
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
7 changes: 2 additions & 5 deletions velox/expression/CastExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "velox/expression/ScopedVarSetter.h"
#include "velox/external/date/tz.h"
#include "velox/functions/lib/RowsTranslationUtil.h"
#include "velox/functions/lib/TimeUtils.h"
#include "velox/type/Type.h"
#include "velox/vector/ComplexVector.h"
#include "velox/vector/FunctionVector.h"
Expand Down Expand Up @@ -181,11 +182,7 @@ VectorPtr CastExpr::castFromDate(
case TypeKind::TIMESTAMP: {
static const int64_t kMillisPerDay{86'400'000};
const auto& queryConfig = context.execCtx()->queryCtx()->queryConfig();
const auto sessionTzName = queryConfig.sessionTimezone();
const auto* timeZone =
(queryConfig.adjustTimestampToTimezone() && !sessionTzName.empty())
? date::locate_zone(sessionTzName)
: nullptr;
const auto* timeZone = getTimeZoneFromConfig(queryConfig);
auto* resultFlatVector = castResult->as<FlatVector<Timestamp>>();
applyToSelectedNoThrowLocal(context, rows, castResult, [&](int row) {
auto timestamp = Timestamp::fromMillis(
Expand Down
24 changes: 22 additions & 2 deletions velox/expression/tests/CastExprTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,26 @@ TEST_F(CastExprTest, stringToTimestamp) {
std::nullopt,
};
testCast<std::string, Timestamp>("timestamp", input, expected);

setTimezone("GMT-8");
testCast<std::string, Timestamp>(
"timestamp",
{
"1970-01-01",
"1970-01-01 08:00:00",
"1970-01-01 00:00:00",
"1970-01-01 08:00:11",
"1970-01-01 09:00:00",
std::nullopt,
},
{
Timestamp(-28800, 0),
Timestamp(0, 0),
Timestamp(-28800, 0),
Timestamp(11, 0),
Timestamp(3600, 0),
std::nullopt,
});
}

TEST_F(CastExprTest, timestampToString) {
Expand Down Expand Up @@ -2385,8 +2405,8 @@ class TestingDictionaryToFewerRowsFunction : public exec::VectorFunction {
exec::EvalCtx& context,
VectorPtr& result) const override {
const auto size = rows.size();
auto indices =
makeIndices(size, [](auto /*row*/) { return 0; }, context.pool());
auto indices = makeIndices(
size, [](auto /*row*/) { return 0; }, context.pool());

result = BaseVector::wrapInDictionary(nullptr, indices, size, args[0]);
}
Expand Down
18 changes: 17 additions & 1 deletion velox/functions/lib/TimeUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,28 @@ inline constexpr int64_t kSecondsInDay = 86'400;
inline constexpr int64_t kDaysInWeek = 7;
extern const folly::F14FastMap<std::string, int8_t> kDayOfWeekNames;

// Format timezone to make it compatible with date::locate_zone.
// For example, converts "GMT+8" to "Etc/GMT-8".
// Here is the list of IANA timezone names:
// https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
FOLLY_ALWAYS_INLINE std::string formatTimezone(const std::string& timezone) {
if (timezone.find("GMT") == 0 && timezone.size() > 4) {
std::string prefix;
if (timezone[3] != '+' && timezone[3] != '-') {
return timezone;
}
std::string prefix = timezone[3] == '+' ? "Etc/GMT-" : "Etc/GMT+";
return prefix + timezone.substr(4);
}
return timezone;
}

FOLLY_ALWAYS_INLINE const date::time_zone* getTimeZoneFromConfig(
const core::QueryConfig& config) {
if (config.adjustTimestampToTimezone()) {
auto sessionTzName = config.sessionTimezone();
if (!sessionTzName.empty()) {
return date::locate_zone(sessionTzName);
return date::locate_zone(formatTimezone(sessionTzName));
}
}
return nullptr;
Expand Down

0 comments on commit f3aac93

Please sign in to comment.