Skip to content

Commit

Permalink
Suppress exception for from_unixtime function
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE authored Nov 11, 2023
1 parent b24cfeb commit e5960e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 10 additions & 2 deletions velox/functions/sparksql/DateTimeFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,16 @@ struct FromUnixtimeFunction {
mysqlDateTime_ = buildJodaDateTimeFormatter(
std::string_view(timeFormat.data(), timeFormat.size()));
}
Timestamp timestamp = Timestamp::fromMillis(1000 * second);
auto formattedResult = mysqlDateTime_->format(timestamp, sessionTimeZone_);
Timestamp timestamp = Timestamp::fromSecond(second);
std::string formattedResult;
try {
formattedResult = mysqlDateTime_->format(timestamp, sessionTimeZone_);
} catch (const VeloxUserError& e) {
// Suppress exception caused by integer overflow or
// out of [-32767-01-01, 32767-12-31] date range.
formattedResult =
mysqlDateTime_->format(Timestamp(0, 0), sessionTimeZone_);
}
auto resultSize = formattedResult.size();
result.resize(resultSize);
if (resultSize != 0) {
Expand Down
4 changes: 4 additions & 0 deletions velox/type/Timestamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ struct Timestamp {
std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>
toTimePoint() const;

static Timestamp fromSecond(int64_t second) {
return Timestamp(second, 0);
}

static Timestamp fromMillis(int64_t millis) {
if (millis >= 0 || millis % 1'000 == 0) {
return Timestamp(millis / 1'000, (millis % 1'000) * 1'000'000);
Expand Down

0 comments on commit e5960e2

Please sign in to comment.