raviston commented on PR #60293: URL: https://github.com/apache/spark/pull/60293#issuecomment-6764092848
A walkthrough for reviewers: how I got to this change, what each part of the diff does, and what I checked. ORC line references are to [apache/orc v2.3.1](https://github.com/apache/orc/tree/v2.3.1), the version on master. ## 1. Reproduction on master I wrote one file per value with Spark, then compared `count()` with `spark.sql.orc.filterPushdown` on and off (session time zone UTC): | File value | `t = v` on / off | `t <= v` on / off | `t >= v` on / off | |---|---|---|---| | `0001-01-02 00:00:00` | **0** / 1 | **0** / 1 | 1 / 1 | | `1500-01-01 00:00:00` | **0** / 1 | 1 / 1 | **0** / 1 | | `1582-10-15 00:00:00` | 1 / 1 | 1 / 1 | 1 / 1 | | `1582-10-16 00:00:00` | 1 / 1 | 1 / 1 | 1 / 1 | For every value, the pushed literal's `getTime()` equaled the file's `getStatistics()(1).getMinimum().getTime()` read through the ORC reader API. So Spark writes and pushes the same hybrid-calendar value. The difference only appears inside ORC's predicate evaluation. ## 2. Root cause in ORC Spark writes `TIMESTAMP` values in the hybrid Julian/Gregorian calendar (`OrcSerializer` → `DateTimeUtils.toJavaTimestamp`). `OrcFilters.castLiteralValue` builds the pushed literal the same way. ORC then uses that literal in two checks that disagree before 1582-10-15: - **Statistics: converted.** [`RecordReaderImpl.evaluatePredicateProto`](https://github.com/apache/orc/blob/v2.3.1/java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java#L683-L684) deserializes the row group statistics with `ColumnStatisticsImpl.deserialize(null, statsProto, writerUsedProlepticGregorian, true)`. The last argument is hard-coded to `true`, regardless of the reader option. The timestamp statistics constructor then calls [`DateUtils.convertTime(..., writerUsedProlepticGregorian, true, true)`](https://github.com/apache/orc/blob/v2.3.1/java/core/src/java/org/apache/orc/impl/ColumnStatisticsImpl.java#L1640-L1657). For a file written in the hybrid calendar, that reaches [`convertTimeToProleptic`](https://github.com/apache/orc/blob/v2.3.1/java/core/src/java/org/apache/orc/impl/DateUtils.java#L117-L119), which rewrites every value `< SWITCHOVER_MILLIS` (1582-10-15 00:00:00 UTC, [L76-L77](https://github.com/apache/orc/blob/v2.3.1/java/core/src/java/org/apache/orc/impl/ DateUtils.java#L76-L77)) by re-parsing its wall clock in the proleptic calendar. That moves it by up to 10 days, e.g. +2 days in year 1 and -9 days in 1500. The unconverted hybrid literal is then outside [min, max]. - **Bloom filters: not converted.** The writer adds the stored millis to the bloom filter as they are ([`TimestampTreeWriter` L111-L122](https://github.com/apache/orc/blob/v2.3.1/java/core/src/java/org/apache/orc/impl/writer/TimestampTreeWriter.java#L111-L122)). The reader tests the literal's `getTime()` without any calendar conversion ([`checkInBloomFilter` L955-L962](https://github.com/apache/orc/blob/v2.3.1/java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java#L955-L962)). `DATE` is not affected, because ORC compares dates as local dates. `TIMESTAMP_NTZ` isn't either, because Spark pushes it as a `LONG`. ## 3. Alternatives I considered 1. **Push a proleptic literal**, as the JIRA suggests. It fixes the statistics check, but the bloom filter holds the hybrid millis, so a file with a bloom filter on the column would be skipped for `=`, `<=>` and `IN`. This follows from the code in section 2. I did not build this variant. 2. **Push both literals**, e.g. `(t = hybrid) OR (t = proleptic)`, so each check finds the one it needs. That keeps more data than either alone, which is fine on its own, but it breaks once the predicate is negated. For `NOT (t = v)`, a file whose values are all `w` is skipped when `w`'s converted statistic equals `v`'s hybrid value, i.e. when `w` is 2 days before `v` in year 1. Spark's `NOT (t = v)` keeps those rows. Ranges have the same problem, so a pushed predicate here has to be exact, not just wide enough. 3. **Use `RebaseDateTime.lastSwitchGregorianTs` as the cutoff.** I checked its value: it is `1900-01-01T00:00:00Z`, because Spark's own rebase also corrects time zone offsets of old dates until then. Using it would stop pushing down every timestamp before 1900, even though ORC converts nothing after 1582-10-15. So the change doesn't push down a predicate that no literal can make correct, and keeps everything else. ## 4. The change, part by part (`OrcFilters.scala`) - **L29, L32: imports of `MILLIS_PER_DAY` and `lastSwitchGregorianDay`.** `lastSwitchGregorianDay` is `-141427`, i.e. 1582-10-15. That is the same day as ORC's `SWITCHOVER_DAYS = HYBRID_DATE_FORMAT.parse("1582-10-15").toEpochDay()`. - **L38: `ArrayImplicits._`**, for `toImmutableArraySeq` on the `In` values (L319). - **L187-L189: `GregorianSwitchMillis = lastSwitchGregorianDay * MILLIS_PER_DAY`** = `-12219292800000`, which is ORC's `SWITCHOVER_MILLIS` (`TimeUnit.DAYS.toMillis(SWITCHOVER_DAYS)`). - **L295-L309: `hasValueBeforeCalendarSwitch`.** It is true for a `TimestampType` attribute if any value, after `castLiteralValue`, has `getTime() < GregorianSwitchMillis`. It tests the value that is actually pushed, in the representation ORC compares, rather than the proleptic date. This handles both forms a filter can hold: an `Instant` (java8 API on), which `castLiteralValue` converts, and a hybrid `java.sql.Timestamp` (java8 API off), which it passes through. - **L311-L321: `attributeAndValues`.** This is the attribute and values of each leaf predicate that carries a value: `=`, `<=>`, `<`, `<=`, `>`, `>=` and `IN`. - **L326-L328: the new first case of the leaf match returns `None`.** It has to come before *all* the other cases. A value that failed only the `isTimestamp(name)` cases from SPARK-60449 would otherwise fall through to the generic `EqualTo`/`In`/... cases and still be pushed. Returning `None` reuses the existing handling of a non-convertible leaf in `convertibleFilters`: an `AND` keeps its other side, while an `OR` or a `NOT` around it is not pushed. `IsNull`/`IsNotNull` carry no value, so they are still pushed. **Why the cutoff is exact.** ORC converts only statistics `< SWITCHOVER_MILLIS`, and the conversion keeps them before it: they move by at most 10 days, and the shift is about -10 days next to the switch. So for a literal `L >= SWITCHOVER_MILLIS`, a statistic `S` below the switch is `< L` both before and after the conversion. A statistic at or after the switch isn't converted at all. Either way, the comparison ORC makes is the comparison Spark means, and the bloom filter values are unchanged. The check is on the hybrid value, not the calendar date, and that makes a difference near the boundary. With the JVM in America/Los_Angeles, the instant `1582-10-14T23:00:00Z` is local 1582-10-14 15:07, and its hybrid value is on or after the switch, so it is pushed, correctly. An early version of my unit test expected otherwise and failed. The end-to-end test, which compares results, passed. ## 5. Tests (`OrcFilterSuite.scala`; `OrcV1FilterSuite` runs them again with DSv1) - **`SPARK-60152: filter pushdown - timestamp before 1582-10-15` (L414):** - Data: one file each for 0001-01-02, 1500-01-01, 1582-10-15, 1582-10-16 and 2020-05-25, so each file's statistics hold only its value. - Conditions: 40 (`=`, `<=>`, `<`, `<=`, `>`, `>=`, `!=` and `IN` with each value). The expected counts come from the same rows in memory. - Combinations: bloom filters on the column on and off × java8 API on and off × both native readers. - It also checks with `stripSparkFilter` that `t = TIMESTAMP'2020-05-25 10:11:12'` and `t > TIMESTAMP'1582-10-16 00:00:00'` still skip the other files. That shows pushdown isn't disabled for later values. - **`SPARK-60152: timestamp predicates with a value before 1582-10-15 are not pushed down` (L457):** it calls `OrcFilters.createFilter` for each leaf type, plus `NOT (t = v)` and an `IN` that mixes a later and an earlier value. Each value is given both as an `Instant` and as a hybrid `java.sql.Timestamp`. Values before the switch (0001-01-02, 1500-01-01, 1582-10-04) are not pushed, and values after it (1582-10-16, 1900-01-01) are. It also checks that `AND` still pushes its other side and that `IsNull`/`IsNotNull` are pushed. - **Existing `filter pushdown - timestamp` (L303):** it used the 1000-01-01 and 1582-10-01 values to check that `=`, `<=>`, `<`, `<=`, `>` and `>=` are pushed, i.e. it asserted the behavior that drops rows. It now uses its 1900-01-01 and 2020-05-25 values for the same operator checks. - **Without the change**, both new tests fail: `0 did not equal 1 t = TIMESTAMP'0001-01-02 00:00:00'`. With it, `OrcFilterSuite` and `OrcV1FilterSuite` pass (54 tests), and so do all 13 suites in `org.apache.spark.sql.execution.datasources.orc` (841 tests). ## 6. Not in this PR `TimestampLTZNanosType`/`TimestampNTZNanosType` (`TimestampNanosOrcOps`) write `new OrcTimestamp(instant.toEpochMilli)` without the hybrid conversion, and build their literals with `Timestamp.valueOf(...)`. Whether their old values hit a similar mismatch needs its own investigation, so I left them out. I'm happy to look at them in a follow-up. -- 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]
