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

Add Spark CAST(double/float as timestamp) #12041

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

boneanxs
Copy link
Contributor

@boneanxs boneanxs commented Jan 9, 2025

Add Spark CAST (double/float as timestamp). The input value is treated as the
number of seconds since the epoch (1970-01-01 00:00:00 UTC).

Spark's implementation: https://github.com/apache/spark/blob/fd86f85e181fc2dc0f50a096855acf83a6cc5d9c/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala#L675

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 9, 2025
Copy link

netlify bot commented Jan 9, 2025

Deploy Preview for meta-velox canceled.

Name Link
🔨 Latest commit 64a7230
🔍 Latest deploy log https://app.netlify.com/sites/meta-velox/deploys/67aea1225d094d00080b1662

@boneanxs
Copy link
Contributor Author

Hey @rui-mo @jinchengchenghh , can help review this? Thanks!

Copy link
Collaborator

@rui-mo rui-mo left a comment

Choose a reason for hiding this comment

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

ToKind == TypeKind::TIMESTAMP) {
const auto castResult =
hooks_->castDoubleToTimestamp(static_cast<double>(inputRowValue));
if (castResult.hasError()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we use 'setResultOrError'?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to set Nulls if the value is Nan or Infinity, now I use the error type to distinguish this:

        if (castResult.error().isUserError()) {
          setError(castResult.error().message());
        } else {
          result->setNull(row, true);
        }

setResultOrError doesn't support handle error separately.

Status::Invalid("Can not convert NaN or Infinity to timestamp"));
}
return Timestamp::fromMicrosNoError(
static_cast<int64_t>(value * Timestamp::kMicrosecondsInSecond));
Copy link
Collaborator

Choose a reason for hiding this comment

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

How is overflow handled? It looks different with the impl. of 'castIntToTimestamp'.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we need to handle it inside velox?

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you try a overflow case that value is max double?
Spark result is

spark-sql (default)> select cast(1.79769e+308 as timestamp);
+294247-01-10 04:00:54.775807

Not sure the result is same in Velox.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you're correct, the overflow handling is not same with the Spark, fix them by add separate check, also add tests to cover it.

@@ -291,6 +291,46 @@ TEST_F(SparkCastExprTest, intToTimestamp) {
testIntegralToTimestampCast<int32_t>();
}

TEST_F(SparkCastExprTest, doubleToTimestamp) {
testCast(
makeNullableFlatVector<double>({
Copy link
Contributor

Choose a reason for hiding this comment

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

makeNullableFlatVector -> makeFlatVector


TEST_F(SparkCastExprTest, floatToTimestamp) {
testCast(
makeNullableFlatVector<float>({
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

@@ -35,19 +35,37 @@ Expected<Timestamp> SparkCastHooks::castStringToTimestamp(
view.data(), view.size(), util::TimestampParseMode::kSparkCast);
}

Expected<Timestamp> SparkCastHooks::castIntToTimestamp(int64_t seconds) const {
template <typename T>
Expected<Timestamp> SparkCastHooks::castNumberToTimestamp(T value) const {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please still use the T seconds.

@@ -75,5 +79,8 @@ class SparkCastHooks : public exec::CastHooks {
.skipTrailingZeros = true,
.zeroPaddingYear = true,
.dateTimeSeparator = ' '};

template <typename T>
Expected<Timestamp> castNumberToTimestamp(T value) const;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move the function before the class fields.

Copy link
Contributor

@jinchengchenghh jinchengchenghh left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution.

Copy link
Collaborator

@rui-mo rui-mo left a comment

Choose a reason for hiding this comment

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

Thanks!

const auto castResult =
hooks_->castDoubleToTimestamp(static_cast<double>(inputRowValue));
if (castResult.hasError()) {
if (castResult.error().isUserError()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The error handling according to type seems to be Spark-specific. I'm wondering if it's possible to wrap it inside the cast hook.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we have to return null for Nan and Inf, Looks Expected can't support return null value, so here I update to use Expected<std::optional<Timestamp>> to wrap the logic inside Spark implementation.

velox/docs/functions/spark/conversion.rst Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants