Raviston6296 opened a new pull request, #60293:
URL: https://github.com/apache/spark/pull/60293

   ### What changes were proposed in this pull request?
   
   `OrcFilters` no longer pushes down a predicate on a `TIMESTAMP` column whose 
value is before 1582-10-15 in the hybrid Julian/Gregorian calendar. This covers 
`=`, `<=>`, `<`, `<=`, `>`, `>=` and `IN`, including under `NOT`. Spark still 
evaluates the predicate. The other parts of a conjunction, `IS NULL` / `IS NOT 
NULL`, and predicates with later values are pushed down as before.
   
   ### Why are the changes needed?
   
   They are a correctness bug. With the default 
`spark.sql.orc.filterPushdown=true`, a query can skip an ORC file whose rows 
match:
   
   ```sql
   CREATE TABLE o USING orc AS SELECT TIMESTAMP'0001-01-02 00:00:00' AS t;
   SELECT count(*) FROM o WHERE t = TIMESTAMP'0001-01-02 00:00:00';   -- 0, 
expected 1
   SELECT count(*) FROM o WHERE t <= TIMESTAMP'0001-01-02 00:00:00';  -- 0, 
expected 1
   ```
   
   With `spark.sql.orc.filterPushdown=false` both return 1. A file of 
`1500-01-01 00:00:00` returns 0 for `=` and `>=` in the same way. This was 
reported on 3.5.8, 4.0.0, 4.1.1, 4.2.0 and master.
   
   Spark writes `TIMESTAMP` values to ORC in the hybrid calendar 
(`OrcSerializer` uses `DateTimeUtils.toJavaTimestamp`), and 
`OrcFilters.castLiteralValue` converts a pushed down value the same way. ORC 
then uses the value in two ways that disagree before 1582-10-15:
   
   - **Statistics:** `RecordReaderImpl.evaluatePredicateProto` reads the 
min/max with `ColumnStatisticsImpl.deserialize(..., 
writerUsedProlepticGregorian, true)`. For a file written in the hybrid 
calendar, this converts every value before `DateUtils.SWITCHOVER_MILLIS` 
(1582-10-15 00:00:00 UTC) to the proleptic Gregorian calendar, which moves it 
by up to 10 days. The hybrid literal then falls outside [min, max].
   - **Bloom filters:** `checkInBloomFilter` tests the literal's millis against 
the values as they were stored, in the hybrid calendar, without any conversion.
   
   So a proleptic literal, which is what the statistics need, would make the 
bloom filter check skip the files instead. No single value is right for both, 
and the safe option is to not push such a predicate down. These are rare in 
queries.
   
   The cutoff is the one ORC uses. ORC converts only statistics before 
1582-10-15 00:00:00 UTC, and the conversion keeps them before it, so a value at 
or after 1582-10-15 compares the same with a statistic before and after the 
conversion. Spark's `RebaseDateTime.lastSwitchGregorianTs` is 1900-01-01, 
because Spark's own rebase also corrects time zone offsets until then, so it 
would disable pushdown for far more values than needed. This PR uses 
`RebaseDateTime.lastSwitchGregorianDay` (1582-10-15, the same day as ORC's 
`SWITCHOVER_DAYS`).
   
   `DATE` is not affected, because ORC compares dates as local dates. 
`TIMESTAMP_NTZ` is not affected either, because it is pushed down as a long.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. Queries on ORC tables that compare a `TIMESTAMP` column with a value 
before 1582-10-15 now return the matching rows, where they could return fewer 
rows before. Such predicates are no longer used to skip ORC stripes and row 
groups.
   
   ### How was this patch tested?
   
   - New `SPARK-60152: filter pushdown - timestamp before 1582-10-15` in 
`OrcFilterSuite`. It writes one file each for 0001-01-02, 1500-01-01, 
1582-10-15, 1582-10-16 and 2020-05-25, with and without bloom filters on the 
column. It then checks 40 predicates (`=`, `<=>`, `<`, `<=`, `>`, `>=`, `!=` 
and `IN` with each value) against the same rows in memory, with 
`spark.sql.datetime.java8API.enabled` on and off and with both native ORC 
readers. It also checks that predicates with later values still skip the other 
files.
   - New `SPARK-60152: timestamp predicates with a value before 1582-10-15 are 
not pushed down` in `OrcFilterSuite`. It calls `OrcFilters.createFilter` for 
each predicate, including `NOT` and an `IN` that mixes a later and an earlier 
value, with the value as an `Instant` and as a `java.sql.Timestamp`. It checks 
that `AND` still pushes the other side.
   - The existing `filter pushdown - timestamp` test used its 1000-01-01 and 
1582-10-01 values to check that `=`, `<=>`, `<`, `<=`, `>` and `>=` are pushed 
down. It now uses its 1900-01-01 and 2020-05-25 values for those checks.
   - Both new tests fail without the change (`0 did not equal 1 t = 
TIMESTAMP'0001-01-02 00:00:00'`).
   - `build/sbt 'sql/testOnly *.orc.OrcFilterSuite *.orc.OrcV1FilterSuite'` 
passes (54 tests; `OrcV1FilterSuite` runs the same tests with DSv1). `build/sbt 
'sql/testOnly org.apache.spark.sql.execution.datasources.orc.*'` and 
`sql/scalastyle` / `sql/Test/scalastyle` pass.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


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