-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add kMicroSecondsInSeconds like |
||
int64_t micros = timestamp.toMicros(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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?