MaxGekk commented on a change in pull request #27807: [SPARK-31076][SQL]
Convert Catalyst's DATE/TIMESTAMP to Java Date/Timestamp via local date-time
URL: https://github.com/apache/spark/pull/27807#discussion_r389511120
##########
File path:
sql/core/src/test/resources/sql-tests/results/postgreSQL/date.sql.out
##########
@@ -848,7 +848,7 @@ SELECT DATE_TRUNC('CENTURY', TO_DATE('0055-08-10 BC',
'yyyy-MM-dd G'))
-- !query schema
struct<date_trunc(CENTURY, CAST(to_date('0055-08-10 BC', 'yyyy-MM-dd G') AS
TIMESTAMP)):timestamp>
-- !query output
--0099-01-01 00:00:00
+0100-01-01 00:00:00
Review comment:
I think this can be fixed by manually set the era in local date-time. Here
is the example which works as expected:
```scala
def fromJavaTimestamp(t: Timestamp): SQLTimestamp = {
if (t.getTime < GREGORIAN_CUTOVER_MILLIS) {
val era = if (t.after(Timestamp.valueOf("0001-01-01 00:00:00"))) 1
else 0
val ldt = t.toLocalDateTime.`with`(ChronoField.ERA, era)
val instant = ZonedDateTime.of(ldt, ZoneId.systemDefault()).toInstant
instantToMicros(instant)
} else {
instantToMicros(t.toInstant)
}
}
```
in the test:
```scala
val formatter = TimestampFormatter.getFractionFormatter(ZoneOffset.UTC)
val originLdt = LocalDateTime.of(-99, 1, 1, 0, 0, 0)
val i =
DateTimeUtils.instantToMicros(originLdt.atOffset(ZoneOffset.UTC).toInstant)
val jts = DateTimeUtils.toJavaTimestamp(i)
println(new SimpleDateFormat("yyyy G").format(jts))
println(jts.toLocalDateTime)
val back = DateTimeUtils.fromJavaTimestamp(jts)
println(formatter.format(back))
```
```
0100 BC
0100-01-01T02:30:17
-0099-01-01 00:00:00
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]