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

feat: Add support for Timestamp to Integral for Spark #12369

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: 8 additions & 0 deletions velox/expression/CastExpr-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ void CastExpr::applyCastKernel(
return;
}

if constexpr (
(FromKind == TypeKind::TIMESTAMP) && ToKind == TypeKind::BIGINT) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if to kind is int?

const auto castResult =
hooks_->castTimestampToInt(inputRowValue);
setResultOrError(castResult, row);
return;
}

// Optimize empty input strings casting by avoiding throwing exceptions.
if constexpr (
FromKind == TypeKind::VARCHAR || FromKind == TypeKind::VARBINARY) {
Expand Down
3 changes: 3 additions & 0 deletions velox/expression/CastHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,8 @@ class CastHooks {
virtual bool truncate() const = 0;

virtual PolicyType getPolicy() const = 0;

// Cast timestamp to integer.
virtual Expected<int64_t> castTimestampToInt(Timestamp timestamp) const = 0;
};
} // namespace facebook::velox::exec
5 changes: 5 additions & 0 deletions velox/expression/PrestoCastHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ Expected<int32_t> PrestoCastHooks::castStringToDate(
return util::fromDateString(dateString, util::ParseMode::kPrestoCast);
}

Expected<int64_t> PrestoCastHooks::castTimestampToInt(Timestamp timestamp) const {
return folly::makeUnexpected(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we verified in Presto that this conversion is indeed unsupported?

Status::UserError("Conversion to Timestamp is not supported"));
}

namespace {

using double_conversion::StringToDoubleConverter;
Expand Down
3 changes: 3 additions & 0 deletions velox/expression/PrestoCastHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class PrestoCastHooks : public CastHooks {

Expected<Timestamp> castIntToTimestamp(int64_t seconds) const override;

// Cast timestamp to integer.
Expected<int64_t> castTimestampToInt(Timestamp timestamp) const override;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that we are converting timestamp as big int not integer. Perhaps use consistent function name, and avoid repeating the name in comment.


// Uses standard cast mode to cast from string to date.
Expected<int32_t> castStringToDate(
const StringView& dateString) const override;
Expand Down
15 changes: 15 additions & 0 deletions velox/functions/sparksql/specialforms/SparkCastHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ Expected<Timestamp> SparkCastHooks::castIntToTimestamp(int64_t seconds) const {
return Timestamp(seconds, 0);
}

Expected<int64_t> SparkCastHooks::castTimestampToInt(Timestamp timestamp) const {
static constexpr int64_t MICROS_PER_SECOND = 1'000'000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add kMicroSecondsInSeconds like kNanosInSecond

int64_t micros = timestamp.toMicros();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto micros


// Replicate Spark conversion via floor division.
int64_t quotient = micros / MICROS_PER_SECOND;
int64_t remainder = micros % MICROS_PER_SECOND;

if (remainder != 0 && micros < 0) {
quotient -= 1;
}

return quotient;
}

Expected<int32_t> SparkCastHooks::castStringToDate(
const StringView& dateString) const {
// Allows all patterns supported by Spark:
Expand Down
3 changes: 3 additions & 0 deletions velox/functions/sparksql/specialforms/SparkCastHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class SparkCastHooks : public exec::CastHooks {
/// whitespaces before cast.
StringView removeWhiteSpaces(const StringView& view) const override;

// Cast timestamp to integral.
Expected<int64_t> castTimestampToInt(Timestamp timestamp) const override;

const TimestampToStringOptions& timestampToStringOptions() const override {
return timestampToStringOptions_;
}
Expand Down
36 changes: 36 additions & 0 deletions velox/functions/sparksql/tests/SparkCastExprTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,5 +750,41 @@ TEST_F(SparkCastExprTest, bigintToBinary) {
std::string("\x80\x00\x00\x00\x00\x00\x00\x00", 8)});
}

TEST_F(SparkCastExprTest, timestampToInt) {
std::vector<std::optional<Timestamp>> input = {
Timestamp(0, 0),
Timestamp(1, 0),
Timestamp(10, 0),
Timestamp(-1, 0),
Timestamp(-10, 0),
Timestamp(-1, 500000),
Timestamp(-2, 999999),
Timestamp(-10, 999999),
Timestamp(1, 999999),
Timestamp(-1, 1),
Timestamp(1234567, 500000),
Timestamp(-9876543, 1234),
std::nullopt,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps remove the null test as it tests the framework not the function itself.

};

std::vector<std::optional<int64_t>> expected = {
0,
1,
10,
-1,
-10,
-1,
-2,
-10,
1,
-1,
1234567,
-9876543,
std::nullopt,
};

testCast<Timestamp, int64_t>("bigint", input, expected);
}

} // namespace
} // namespace facebook::velox::test