raviston commented on code in PR #60293:
URL: https://github.com/apache/spark/pull/60293#discussion_r4541486112
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/orc/OrcFilters.scala:
##########
@@ -285,10 +293,47 @@ private[sql] object OrcFilters extends OrcFiltersBase {
import org.apache.spark.sql.sources._
+ // Spark writes TimestampType values to ORC in the hybrid Julian/Gregorian
calendar, and
+ // `castLiteralValue` converts a pushed down value the same way. But ORC
compares the value
+ // with the statistics after converting the ones whose wall clock is
before 1582-10-15 to the
+ // proleptic Gregorian calendar (see `DateUtils.convertTimeToProleptic`),
which moves them by
+ // up to 10 days. So a predicate with a value whose wall clock is before
1582-10-15 is not
+ // pushed down. A later value compares the same with a converted statistic
as with the stored
+ // one, as both are earlier than it. ORC compares wall clocks in the JVM
default time zone,
+ // and its wall clock is computed here as in
`SerializationUtils.convertToUtc`.
+ def hasValueBeforeCalendarSwitch(attribute: String, values: Seq[Any]):
Boolean =
+ dataTypeMap.get(attribute).exists(_.fieldType == TimestampType) && {
+ val timeZone = TimeZone.getDefault
+ values.exists { v =>
+ castLiteralValue(v, TimestampType) match {
+ case ts: Timestamp =>
+ val millis = ts.getTime
+ millis + timeZone.getOffset(millis) < GregorianSwitchMillis
Review Comment:
Thanks, you're right. I reproduced it with this PR: with files at 1582-10-15
00:00, 06:00, 12:00, 18:00 and 23:30 (UTC), `t >= TIMESTAMP'1582-10-10
18:00:00'` returns 2 with pushdown on and 5 with it off, `t > ...` 1 and 5, and
`t != ...` / `t NOT IN (...)` 4 and 5, with the Java 8 API on and off.
A cutoff of 1582-10-16 00:00 isn't quite enough in some JVM time zones,
though. `toJavaTimestamp` rebases with the tables in
`gregorian-julian-rebase-micros.json`, which were built with older time zone
rules. Where the standard offset has changed since, the moved value is an hour
off from the exact rebase (`rebaseGregorianToJulianMicros(TimeZone, Long)`).
With the JVM in America/Asuncion (JDK 21.0.7, tzdata 2025a),
`TIMESTAMP'1582-10-05 23:00:00'` is pushed as `1582-10-16 00:00:00`, and `t >
TIMESTAMP'1582-10-05 23:00:00'` returns 1 with pushdown on and 2 with it off,
on files at 1582-10-15 12:00 and 1582-10-16 06:00, with the cutoff at
1582-10-16. America/Chihuahua, America/Ojinaga, America/Nuuk, Asia/Amman and
Asia/Damascus are the same. Over all 603 JVM time zones, the latest wall clock
a value from 1582-10-05 to 1582-10-14 moves to is 1582-10-16 00:59.
So 4b8f3cb9dd0 moves the cutoff to a wall clock of 1582-10-17 00:00, which
only gives up pushdown for values on 1582-10-15 and 1582-10-16:
- `filter pushdown - timestamp before 1582-10-15` uses `1582-10-05 23:30:00`
and `1582-10-10 18:00:00` as condition values only, adds a `1582-10-15
18:00:00` file, and also runs in America/Asuncion.
- In `timestamp predicates with a value before 1582-10-15 are not pushed
down`, `1582-10-15T01:00:00` is now `false`, and `1582-10-10T18:00:00`,
`1582-10-15T23:59:59.999999`, `1582-10-16T23:59:59.999999` (`false`) and
`1582-10-17T00:00:00` (`true`) are added.
To check it beyond the tests, I built row group statistics the way ORC's
`TimestampTreeWriter` does, for values from 1582-10-04 to 1582-10-17, and
evaluated the predicates built by `OrcFilters.createFilter` with ORC's
`RecordReaderImpl.evaluatePredicateProto`, in all 603 JVM time zones. No
predicate with one of these values skips a file that has matching rows anymore.
With a 1582-10-16 cutoff, the six zones above still lose rows.
--
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]