Skip to content

Commit

Permalink
Restore CAST(varchar AS timestamp) behavior for legacy cast (#10106)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #10106

Allow 'T' separator when legacyCast is true.

Reviewed By: Yuhta

Differential Revision: D58283486

fbshipit-source-id: 48c9723a2f5cc4b87374a60be6d899bc872339ca
  • Loading branch information
mbasmanova authored and facebook-github-bot committed Jun 7, 2024
1 parent 7741e07 commit c860058
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion velox/expression/PrestoCastHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ PrestoCastHooks::PrestoCastHooks(const core::QueryConfig& config)
Expected<Timestamp> PrestoCastHooks::castStringToTimestamp(
const StringView& view) const {
const auto conversionResult = util::fromTimestampWithTimezoneString(
view.data(), view.size(), util::TimestampParseMode::kPrestoCast);
view.data(),
view.size(),
legacyCast_ ? util::TimestampParseMode::kLegacyCast
: util::TimestampParseMode::kPrestoCast);
if (conversionResult.hasError()) {
return folly::makeUnexpected(conversionResult.error());
}
Expand Down
21 changes: 21 additions & 0 deletions velox/expression/tests/CastExprTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,27 @@ TEST_F(CastExprTest, stringToTimestamp) {
(evaluateOnce<Timestamp, std::string>(
"cast(c0 as timestamp)", "1970-01-01T00:00")),
"Cannot cast VARCHAR '1970-01-01T00:00' to TIMESTAMP. Unable to parse timestamp value");

setLegacyCast(true);
input = {
"1970-01-01",
"1970-01-01T00:00 America/Sao_Paulo",
"2000-01-01",
"1970-01-01T00:00:00",
"2000-01-01 12:21:56",
"1970-01-01T00:00:00-02:00",
std::nullopt,
};
expected = {
Timestamp(0, 0),
Timestamp(10800, 0),
Timestamp(946684800, 0),
Timestamp(0, 0),
Timestamp(946729316, 0),
Timestamp(7200, 0),
std::nullopt,
};
testCast<std::string, Timestamp>("timestamp", input, expected);
}

TEST_F(CastExprTest, timestampToString) {
Expand Down
1 change: 1 addition & 0 deletions velox/type/TimestampConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ void parseTimeSeparator(
pos++;
}
break;
case TimestampParseMode::kLegacyCast:
case TimestampParseMode::kSparkCast:
if (buf[pos] == ' ' || buf[pos] == 'T') {
pos++;
Expand Down
3 changes: 3 additions & 0 deletions velox/type/TimestampConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ enum class TimestampParseMode {
// Allows leading and trailing spaces.
kPrestoCast,

// Same as kPrestoCast, but allows 'T' separator between date and time.
kLegacyCast,

/// A Spark-compatible timestamp string. A mix of the above. Accepts T and
/// space as separator between date and time. Allows leading and trailing
/// spaces.
Expand Down

0 comments on commit c860058

Please sign in to comment.