Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -378,16 +378,24 @@ object DateTimeUtils {
throw QueryExecutionErrors.cannotCastUTF8StringToDataTypeError(s, TimestampType)
}
}
// See issue SPARK-35679
// min second cause overflow in instant to micro
private val MIN_SECONDS = Math.floorDiv(Long.MinValue, MICROS_PER_SECOND)

/**
* Gets the number of microseconds since the epoch of 1970-01-01 00:00:00Z from the given
* instance of `java.time.Instant`. The epoch microsecond count is a simple incrementing count of
* microseconds where microsecond 0 is 1970-01-01 00:00:00Z.
*/
def instantToMicros(instant: Instant): Long = {
val us = Math.multiplyExact(instant.getEpochSecond, MICROS_PER_SECOND)
val result = Math.addExact(us, NANOSECONDS.toMicros(instant.getNano))
result
val secs = instant.getEpochSecond
if (secs == MIN_SECONDS) {
val us = Math.multiplyExact(secs + 1, MICROS_PER_SECOND)
Math.addExact(us, NANOSECONDS.toMicros(instant.getNano) - MICROS_PER_SECOND)
} else {
val us = Math.multiplyExact(secs, MICROS_PER_SECOND)
Math.addExact(us, NANOSECONDS.toMicros(instant.getNano))
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,4 +820,9 @@ class DateTimeUtilsSuite extends SparkFunSuite with Matchers with SQLHelper {
}
}
}

test("SPARK-35679: instantToMicros should be able to return microseconds of Long.MinValue") {
assert(instantToMicros(microsToInstant(Long.MaxValue)) === Long.MaxValue)
assert(instantToMicros(microsToInstant(Long.MinValue)) === Long.MinValue)
}
}