Skip to content

Commit

Permalink
Fix casting varchar to timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE committed Jan 13, 2025
1 parent 88b16ad commit 96e2623
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 13 additions & 1 deletion velox/functions/sparksql/specialforms/SparkCastHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,20 @@ SparkCastHooks::SparkCastHooks(const velox::core::QueryConfig& config)

Expected<Timestamp> SparkCastHooks::castStringToTimestamp(
const StringView& view) const {
return util::fromTimestampString(
auto conversionResult = util::fromTimestampString(
view.data(), view.size(), util::TimestampParseMode::kSparkCast);
if (conversionResult.hasError()) {
return folly::makeUnexpected(conversionResult.error());
}
auto result = conversionResult.value();
// If no timezone information is available in the input string, check if we
// should understand it as being at the session timezone, and if so, convert
// to GMT.
const auto sessionTzName = config_.sessionTimezone();
if (!sessionTzName.empty()) {
result.toGMT(*tz::locateZone(sessionTzName));
}
return result;
}

Expected<Timestamp> SparkCastHooks::castIntToTimestamp(int64_t seconds) const {
Expand Down
14 changes: 14 additions & 0 deletions velox/functions/sparksql/tests/SparkCastExprTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,20 @@ TEST_F(SparkCastExprTest, stringToTimestamp) {
Timestamp(1426680197, 456000000),
};
testCast<std::string, Timestamp>("timestamp", input, expected);

setTimezone("Asia/Shanghai");
testCast<std::string, Timestamp>(
"timestamp",
{
"1970-01-01 00:00:00",
"1970-01-01 08:00:00",
"1970-01-01 08:00:59",
},
{
Timestamp(-8 * 3600, 0),
Timestamp(0, 0),
Timestamp(59, 0),
});
}

TEST_F(SparkCastExprTest, intToTimestamp) {
Expand Down

0 comments on commit 96e2623

Please sign in to comment.