raviston commented on code in PR #60523:
URL: https://github.com/apache/spark/pull/60523#discussion_r4582969145


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/DataSourceStrategy.scala:
##########
@@ -621,9 +623,38 @@ object DataSourceStrategy
     }
   }
 
+  // The days of 1582-10-05 to 1582-10-14, which the hybrid Julian/Gregorian 
calendar of
+  // java.sql.Date skips: such a date converts to another java.sql.Date.
+  private val calendarGapDays =
+    LocalDate.of(1582, 10, 5).toEpochDay.toInt to LocalDate.of(1582, 10, 
14).toEpochDay.toInt
+
+  // A TIMESTAMP converts to java.sql.Timestamp in the JVM time zone, so the 
gap is widened by a
+  // day on each side (UTC 1582-10-04 to 1582-10-16) to hold its local days in 
every time zone.
+  // Bounds, not a NumericRange: a range of microseconds this long has too 
many elements.
+  private val calendarGapMicrosStart = LocalDate.of(1582, 10, 4).toEpochDay * 
MICROS_PER_DAY
+  private val calendarGapMicrosEnd = LocalDate.of(1582, 10, 16).toEpochDay * 
MICROS_PER_DAY
+
+  private def isInCalendarGap(value: Any, dataType: DataType): Boolean = 
(value, dataType) match {

Review Comment:
   The fixed window misses other values with the same problem. A value goes 
wrong when it does not come back unchanged from `toJavaDate` / 
`toJavaTimestamp` followed by `fromJavaDate` / `fromJavaTimestamp`, which is 
what `ParquetFilters` applies. I checked that round trip in all 603 JVM time 
zones:
   - every day from 1400 to 2000;
   - every minute from 1582-09-20 to 1582-10-31;
   - every 13 minutes from 1583 to 1910.
   
   Results:
   - The gap days fail in every zone, and the window covers them all.
   - 5 DATE values outside the gap also fail: days that a zone skipped when it 
moved across the date line, e.g. 1994-12-31 in `Pacific/Kiritimati`, 
`Pacific/Kanton` and `Pacific/Enderbury`, and 1993-08-21 in `Pacific/Kwajalein`.
   - TIMESTAMP values fail around the switch from local mean time to standard 
time in many zones, e.g. 1883-11-18 in `America/Los_Angeles` and 1899-12-31 in 
several. They also fail for the whole of 1867-10-18 in the Alaska zones, which 
come back a day later.
   
   End to end, with one file per value and the JVM and session time zone set to 
the zone, this PR still gives different results with pushdown on and off:
   
   | JVM time zone | Filter | pushdown on | pushdown off |
   |---|---|---|---|
   | `Pacific/Kiritimati` | `d > DATE'1994-12-31'` (Parquet and ORC) | 1 | 2 |
   | `America/Anchorage` | `t = TIMESTAMP'1867-10-19 12:00:00'` (Parquet) | 0 | 
1 |
   | `America/Los_Angeles` | `t = TIMESTAMP'1883-11-18 12:05:00'` (Parquet) | 0 
| 1 |
   
   Checking the round trip itself instead of the window covers all of them:
   
   ```scala
     private def isInCalendarGap(value: Any, dataType: DataType): Boolean = 
(value, dataType) match {
       case (days: Int, DateType) =>
         DateTimeUtils.fromJavaDate(DateTimeUtils.toJavaDate(days)) != days
       case (micros: Long, TimestampType) =>
         DateTimeUtils.fromJavaTimestamp(DateTimeUtils.toJavaTimestamp(micros)) 
!= micros
       case _ => false
     }
   ```
   
   With this (and the same check in `OrcFilters`, see there), I tried DATE 
literals within 10 days of the data dates and all the gap days, with 6 
comparisons each, in Parquet and ORC, with the Java 8 API on and off, in 
`America/Los_Angeles` and `Pacific/Kiritimati`. I also tried the TIMESTAMP 
cases above. All of them give the same result with pushdown on and off. Both of 
your tests pass with it, and so do `FileBasedDataSourceSuite`, 
`DataSourceStrategySuite`, `ParquetV1FilterSuite`, `ParquetV2FilterSuite`, 
`OrcV1FilterSuite` and `OrcFilterSuite` (295 tests). It also no longer needs 
the one-day widening for TIMESTAMP, since the check runs in the JVM time zone 
itself. The name and the comments would then need updating, e.g. 
`changesAsJavaDatetime`.
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/orc/OrcFilters.scala:
##########
@@ -288,7 +288,23 @@ private[sql] object OrcFilters extends OrcFiltersBase {
     // NOTE: For all case branches dealing with leaf predicates below, the 
additional `startAnd()`
     // call is mandatory. ORC `SearchArgument` builder requires that all leaf 
predicates must be
     // wrapped by a "parent" predicate (`And`, `Or`, or `Not`).
+    // ORC takes a DATE as java.sql.Date, so a date that the hybrid 
Julian/Gregorian calendar
+    // skips (1582-10-05 to 1582-10-14) would be pushed down as 1582-10-15.
+    def isDateInCalendarGap(value: Any): Boolean = value match {

Review Comment:
   The same applies here. `castLiteralValue` converts a `LocalDate` with 
`toJavaDate(localDateToDays(...))`. With the Java 8 API on and the JVM time 
zone `Pacific/Kiritimati`, ORC `d > DATE'1994-12-31'` returns 1 row with 
pushdown and 2 without. This check fixes it:
   
   ```scala
         case d: LocalDate =>
           val days = localDateToDays(d)
           DateTimeUtils.fromJavaDate(toJavaDate(days)) != days
   ```
   



##########
sql/core/src/test/scala/org/apache/spark/sql/FileBasedDataSourceSuite.scala:
##########
@@ -1850,6 +1850,69 @@ class FileBasedDataSourceSuite extends SharedSparkSession
       }
     }
   }
+
+  test("SPARK-61739: filter pushdown with a DATE that the hybrid calendar 
skips") {
+    // 1582-10-05 to 1582-10-14 do not exist in the hybrid Julian/Gregorian 
calendar of
+    // java.sql.Date and were pushed down as 1582-10-15, which skipped the 
file of 1582-10-15.
+    Seq("parquet", "orc").foreach { format =>
+      Seq("false", "true").foreach { java8Api =>
+        withSQLConf(SQLConf.DATETIME_JAVA8API_ENABLED.key -> java8Api) {
+          withTempPath { dir =>
+            val path = dir.getCanonicalPath
+            // One file per date, so that the statistics of a file hold one 
value.
+            Seq("1582-10-04", "1582-10-15", "1582-10-16").foreach { d =>
+              sql(s"SELECT DATE'$d' AS 
d").write.mode("append").format(format).save(path)
+            }
+            val df = spark.read.format(format).load(path)
+            Seq(
+              "d > DATE'1582-10-10'" -> Seq("1582-10-15", "1582-10-16"),
+              "d >= DATE'1582-10-14'" -> Seq("1582-10-15", "1582-10-16"),
+              "d < DATE'1582-10-05'" -> Seq("1582-10-04"),
+              "d <> DATE'1582-10-14'" -> Seq("1582-10-04", "1582-10-15", 
"1582-10-16"),
+              "NOT (d <=> DATE'1582-10-05')" -> Seq("1582-10-04", 
"1582-10-15", "1582-10-16"),
+              "d IN (DATE'1582-10-10', DATE'1582-10-16')" -> Seq("1582-10-16"),
+              "d NOT IN (DATE'1582-10-10', DATE'1582-10-16')" -> 
Seq("1582-10-04", "1582-10-15")
+            ).foreach { case (condition, expected) =>
+              checkAnswer(
+                df.where(condition).selectExpr("CAST(d AS STRING)"), 
expected.map(Row(_)))
+            }
+          }
+        }
+      }
+    }
+  }
+
+  test("SPARK-61739: filter pushdown with a TIMESTAMP in a day that the hybrid 
calendar skips") {

Review Comment:
   Could the tests also cover a case outside the gap, with the JVM time zone 
set by `withDefaultTimeZone` (and the session time zone set to match)? For 
example:
   - `d > DATE'1994-12-31'` in `Pacific/Kiritimati`, for Parquet and ORC, over 
the dates 1994-12-30 and 1995-01-01;
   - `t = TIMESTAMP'1867-10-19 12:00:00'` in `America/Anchorage`, for Parquet.
   
   Both give different results with pushdown on and off on this head.
   



-- 
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]

Reply via email to