Skip to content

Commit d7e591c

Browse files
rui-moLakehouse Engine Bot
authored andcommitted
Fix Spark timestamp_seconds function
Alchemy-item: (ID = 582) Fix Spark timestamp_seconds function commit 1/1 - bd71f7e
1 parent 3b49272 commit d7e591c

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

velox/functions/sparksql/DateTimeFunctions.h

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -960,37 +960,40 @@ struct SecondsToTimestampFunction {
960960
VELOX_DEFINE_FUNCTION_TYPES(TExec);
961961

962962
template <typename T>
963-
FOLLY_ALWAYS_INLINE void call(out_type<Timestamp>& result, T seconds) {
963+
FOLLY_ALWAYS_INLINE bool call(out_type<Timestamp>& result, T seconds) {
964964
if constexpr (std::is_integral_v<T>) {
965965
result = Timestamp(static_cast<int64_t>(seconds), 0);
966+
return true;
966967
} else {
968+
if (std::isnan(seconds)) {
969+
return false;
970+
}
971+
967972
// Cast to double and check bounds to prevent ensuing overflow.
968973
const double secondsD = static_cast<double>(seconds);
969974

970975
if (secondsD >= kMaxSecondsD) {
971976
result = Timestamp(kMaxSeconds, kMaxNanoseconds);
972-
return;
973-
}
974-
if (secondsD <= kMinSecondsD) {
977+
} else if (secondsD <= kMinSecondsD) {
975978
result = Timestamp(kMinSeconds, kMinNanoseconds);
976-
return;
977-
}
978-
979-
// Scale to microseconds and truncate toward zero.
980-
const double microsD = secondsD * Timestamp::kMicrosecondsInSecond;
981-
const int64_t micros = static_cast<int64_t>(microsD);
982-
983-
// Split into whole seconds and remaining microseconds.
984-
int64_t wholeSeconds = micros / util::kMicrosPerSec;
985-
int64_t remainingMicros = micros % util::kMicrosPerSec;
986-
if (remainingMicros < 0) {
987-
wholeSeconds -= 1;
988-
remainingMicros += util::kMicrosPerSec;
979+
} else {
980+
// Scale to microseconds and truncate toward zero.
981+
const double microsD = secondsD * Timestamp::kMicrosecondsInSecond;
982+
const int64_t micros = static_cast<int64_t>(microsD);
983+
984+
// Split into whole seconds and remaining microseconds.
985+
int64_t wholeSeconds = micros / util::kMicrosPerSec;
986+
int64_t remainingMicros = micros % util::kMicrosPerSec;
987+
if (remainingMicros < 0) {
988+
wholeSeconds -= 1;
989+
remainingMicros += util::kMicrosPerSec;
990+
}
991+
992+
const int64_t nano =
993+
remainingMicros * Timestamp::kNanosecondsInMicrosecond;
994+
result = Timestamp(wholeSeconds, nano);
989995
}
990-
991-
const int64_t nano =
992-
remainingMicros * Timestamp::kNanosecondsInMicrosecond;
993-
result = Timestamp(wholeSeconds, nano);
996+
return true;
994997
}
995998
}
996999

0 commit comments

Comments
 (0)