vinodkc commented on code in PR #53370:
URL: https://github.com/apache/spark/pull/53370#discussion_r2612176591
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala:
##########
@@ -897,6 +897,117 @@ object DateTimeUtils extends SparkDateTimeUtils {
}
}
+ private def withTimeConversionErrorHandling(f: => Long): Long = {
+ try {
+ val nanos = f
+ if (nanos < 0 || nanos >= NANOS_PER_DAY) {
+ throw new DateTimeException(
+ s"Invalid TIME value: must be between 00:00:00 and
23:59:59.999999999, " +
+ s"but got $nanos nanoseconds")
+ }
+ nanos
+ } catch {
+ case e: DateTimeException =>
+ throw QueryExecutionErrors.ansiDateTimeArgumentOutOfRange(e)
+ case e: ArithmeticException =>
+ val wrapped = new DateTimeException(s"Overflow in TIME conversion:
${e.getMessage}", e)
+ throw QueryExecutionErrors.ansiDateTimeArgumentOutOfRange(wrapped)
+ }
+ }
+
+ /**
+ * Creates a TIME value from seconds since midnight (integral types).
+ * @param seconds Seconds (0 to 86399)
+ * @return Nanoseconds since midnight
+ */
+ def timeFromSeconds(seconds: Long): Long = withTimeConversionErrorHandling {
+ Math.multiplyExact(seconds, NANOS_PER_SECOND)
+ }
+
+ /**
+ * Creates a TIME value from seconds since midnight (decimal type).
+ * @param seconds Seconds (0 to 86399.999999)
+ * @return Nanoseconds since midnight
+ */
+ def timeFromSeconds(seconds: Decimal): Long =
withTimeConversionErrorHandling {
+ val operand = new java.math.BigDecimal(NANOS_PER_SECOND)
+ seconds.toJavaBigDecimal.multiply(operand).longValueExact()
+ }
+
+ /**
+ * Creates a TIME value from seconds since midnight (float type).
+ * @param seconds Seconds (0 to 86399.999999)
+ * @return Nanoseconds since midnight
+ */
+ def timeFromSeconds(seconds: Float): Long = withTimeConversionErrorHandling {
+ if (seconds.isNaN || seconds.isInfinite) {
+ throw new DateTimeException("Cannot convert NaN or Infinite value to
TIME")
+ }
+ (seconds.toDouble * NANOS_PER_SECOND).toLong
+ }
+
+ /**
+ * Creates a TIME value from seconds since midnight (double type).
+ * @param seconds Seconds (0 to 86399.999999)
+ * @return Nanoseconds since midnight
+ */
+ def timeFromSeconds(seconds: Double): Long = withTimeConversionErrorHandling
{
+ if (seconds.isNaN || seconds.isInfinite) {
+ throw new DateTimeException("Cannot convert NaN or Infinite value to
TIME")
+ }
+ (seconds * NANOS_PER_SECOND).toLong
+ }
+
+ /**
+ * Creates a TIME value from milliseconds since midnight.
+ * @param millis Milliseconds (0 to 86399999)
+ * @return Nanoseconds since midnight
+ */
+ def timeFromMillis(millis: Long): Long = withTimeConversionErrorHandling {
+ Math.multiplyExact(millis, NANOS_PER_MILLIS)
+ }
+
+ /**
+ * Creates a TIME value from microseconds since midnight.
+ * @param micros Microseconds (0 to 86399999999)
+ * @return Nanoseconds since midnight
+ */
+ def timeFromMicros(micros: Long): Long = withTimeConversionErrorHandling {
+ Math.multiplyExact(micros, NANOS_PER_MICROS)
+ }
+
+ /**
+ * Converts a TIME value to seconds.
+ * @param nanos Nanoseconds since midnight
+ * @return Seconds as Decimal(14, 6)
+ */
+ def timeToSeconds(nanos: Long): Decimal = {
Review Comment:
No, wrapping is not needed for timeToSeconds, timeToMillis, and timeToMicros
- Input `nanos` is guaranteed valid (AnyTimeType enforces 0-86399999999999
nanoseconds)
- No exceptions to handle
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]