Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sparksql): Consider session timezone when casting timestamp to varchar #11958

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions velox/functions/sparksql/specialforms/SparkCastExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exec::ExprPtr SparkCastCallToSpecialForm::constructSpecialForm(
const TypePtr& type,
std::vector<exec::ExprPtr>&& compiledChildren,
bool trackCpuUsage,
const core::QueryConfig& /*config*/) {
const core::QueryConfig& config) {
VELOX_CHECK_EQ(
compiledChildren.size(),
1,
Expand All @@ -33,14 +33,14 @@ exec::ExprPtr SparkCastCallToSpecialForm::constructSpecialForm(
std::move(compiledChildren[0]),
trackCpuUsage,
false,
std::make_shared<SparkCastHooks>());
std::make_shared<SparkCastHooks>(config));
}

exec::ExprPtr SparkTryCastCallToSpecialForm::constructSpecialForm(
const TypePtr& type,
std::vector<exec::ExprPtr>&& compiledChildren,
bool trackCpuUsage,
const core::QueryConfig& /*config*/) {
const core::QueryConfig& config) {
VELOX_CHECK_EQ(
compiledChildren.size(),
1,
Expand All @@ -51,6 +51,6 @@ exec::ExprPtr SparkTryCastCallToSpecialForm::constructSpecialForm(
std::move(compiledChildren[0]),
trackCpuUsage,
true,
std::make_shared<SparkCastHooks>());
std::make_shared<SparkCastHooks>(config));
}
} // namespace facebook::velox::functions::sparksql
18 changes: 10 additions & 8 deletions velox/functions/sparksql/specialforms/SparkCastHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@
#include "velox/functions/sparksql/specialforms/SparkCastHooks.h"
#include "velox/functions/lib/string/StringImpl.h"
#include "velox/type/TimestampConversion.h"
#include "velox/type/tz/TimeZoneMap.h"

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

SparkCastHooks::SparkCastHooks(const velox::core::QueryConfig& config)
: config_(config) {
const auto sessionTzName = config.sessionTimezone();
if (!sessionTzName.empty()) {
timestampToStringOptions_.timeZone = tz::locateZone(sessionTzName);
}
}

Expected<Timestamp> SparkCastHooks::castStringToTimestamp(
const StringView& view) const {
return util::fromTimestampString(
Expand Down Expand Up @@ -76,14 +85,7 @@ StringView SparkCastHooks::removeWhiteSpaces(const StringView& view) const {

const TimestampToStringOptions& SparkCastHooks::timestampToStringOptions()
const {
static constexpr TimestampToStringOptions options = {
.precision = TimestampToStringOptions::Precision::kMicroseconds,
.leadingPositiveSign = true,
.skipTrailingZeros = true,
.zeroPaddingYear = true,
.dateTimeSeparator = ' ',
};
return options;
return timestampToStringOptions_;
}

exec::PolicyType SparkCastHooks::getPolicy() const {
Expand Down
12 changes: 12 additions & 0 deletions velox/functions/sparksql/specialforms/SparkCastHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

#pragma once

#include "velox/core/QueryCtx.h"
#include "velox/expression/CastHooks.h"

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

// This class provides cast hooks following Spark semantics.
class SparkCastHooks : public exec::CastHooks {
public:
explicit SparkCastHooks(const velox::core::QueryConfig& config);

// TODO: Spark hook allows more string patterns than Presto.
Expected<Timestamp> castStringToTimestamp(
const StringView& view) const override;
Expand Down Expand Up @@ -59,5 +62,14 @@ class SparkCastHooks : public exec::CastHooks {
}

exec::PolicyType getPolicy() const override;

private:
const core::QueryConfig& config_;
TimestampToStringOptions timestampToStringOptions_ = {
.precision = TimestampToStringOptions::Precision::kMicroseconds,
.leadingPositiveSign = true,
.skipTrailingZeros = true,
.zeroPaddingYear = true,
.dateTimeSeparator = ' '};
};
} // namespace facebook::velox::functions::sparksql
27 changes: 27 additions & 0 deletions velox/functions/sparksql/tests/SparkCastExprTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,33 @@ TEST_F(SparkCastExprTest, timestampToString) {
"-0010-02-01 10:00:00",
std::nullopt,
});

setTimezone("America/Los_Angeles");
testCast<Timestamp, std::string>(
"string",
{
Timestamp(0, 0),
Timestamp(3600, 0),
Timestamp(61, 10),
},
{
"1969-12-31 16:00:00",
"1969-12-31 17:00:00",
"1969-12-31 16:01:01",
});
setTimezone("Asia/Shanghai");
testCast<Timestamp, std::string>(
"string",
{
Timestamp(0, 0),
Timestamp(3600, 0),
Timestamp(61, 10),
},
{
"1970-01-01 08:00:00",
"1970-01-01 09:00:00",
"1970-01-01 08:01:01",
});
}

TEST_F(SparkCastExprTest, fromString) {
Expand Down
Loading