Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,7 @@ object DateTimeUtils {
* is expressed in microseconds since the epoch.
*/
def getMilliseconds(timestamp: SQLTimestamp, timeZone: TimeZone): Decimal = {
val micros = Decimal(getMicroseconds(timestamp, timeZone))
(micros / Decimal(MICROS_PER_MILLIS)).toPrecision(8, 3)
Decimal(getMicroseconds(timestamp, timeZone), 8, 3)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@MaxGekk, is it safe? Seems previously it could return null but now we cannot return null in some conditions. For instance:

scala> Decimal(9223372036854775L)
res27: org.apache.spark.sql.types.Decimal = 9223372036854775

scala> Decimal(9223372036854775L, 8, 3)
java.lang.ArithmeticException: Unscaled value too large for precision
  at org.apache.spark.sql.types.Decimal.set(Decimal.scala:79)
  at org.apache.spark.sql.types.Decimal$.apply(Decimal.scala:564)
  ... 49 elided

whereas toPrecision seems able to return null for overflow.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think so, getMicroseconds returns an int in the range [0, 60000000) for which Decimal(..., 8, 3) is always valid, for example:

scala> Decimal(60000000, 8, 3)
res1: org.apache.spark.sql.types.Decimal = 60000.000

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, yes. I checked other cases too and seems fine.

}

/**
Expand Down
Loading