Skip to content

[format] Do not push down ORC DATE predicates before 1582-10-15 - #11702

Open
m-karakus wants to merge 2 commits into
apache:masterfrom
m-karakus:fix/orc-date-filter-before-1582
Open

m-karakus wants to merge 2 commits into
apache:masterfrom
m-karakus:fix/orc-date-filter-before-1582

Conversation

@m-karakus

Copy link
Copy Markdown

Purpose

Linked issue: #11576

A pushed-down DATE filter on an ORC table drops rows that match it, but only for dates before
1582-10-15 and only for some operators.

Root cause

OrcPredicateFunctionVisitor builds the literal with Date.valueOf(LocalDate.ofEpochDay(...)).
The problem is on the statistics side, not in the literal: paimon writes DATE values as proleptic
Gregorian epoch days, but the ORC files carry no calendar marker, so ReaderImpl falls back to
the hybrid Julian/Gregorian calendar when it converts the DATE column statistics
(writerUsedProlepticGregorian() is false for these files).

Dumping the statistics of a file that holds 1200-06-15 shows the stored day is already correct
(-281071) but is read back as Hybrid AD 1200-06-08, so the file looks like it only covers
earlier dates and =, >= and IN prune it. <= keeps the file, which is why the query
result depended on the operator.

Setting WriterOptions#setProlepticGregorian(true) does not fix it here: paimon writes dates
through a plain LongColumnVector, so the calendar conversion that the writer applies with that
flag would move the stored day in the other direction.

Fix

Pushdown is only an optimization, so decline it for DATE literals before 1582-10-15 and leave the
predicate to the engine, the same way the existing signedZeroPushdown guard declines ±0.0.
Dates on or after the switchover still push down unchanged.

Tests

New OrcDatePredicateTest writes real ORC files through OrcFileFormat and reads them back the
way the engine does (push down what ORC accepts, then apply the predicate to the returned rows).
It covers =, >=, <=, < and IN on a pre-switchover date, and = on a modern date.

Without the fix all three tests fail (expected: 1 but was: 0); with it they pass.

mvn -pl paimon-format test    ->  Tests run: 865, Failures: 0, Errors: 0, Skipped: 6

paimon writes DATE values as proleptic Gregorian epoch days, but the ORC
files carry no calendar marker, so ORC reads the DATE statistics of a file
on the hybrid Julian/Gregorian calendar. Before the switchover the two
calendars differ by several days, so the statistics of a file holding
1200-06-15 look like 1200-06-08 and a pushed-down `=`, `>=` or `IN`
predicate prunes the file even though it matches. `<=` happens to keep it,
which is why the query result depends on the operator.

Pushdown is an optimization only, so skip it for DATE literals before
1582-10-15 and let the engine evaluate them against the rows.

Refs apache#11576

@Akash3121 Akash3121 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, added optional nitpicks

new PredicateBuilder(ROW_TYPE)
.in(1, Arrays.asList((int) ancient.toEpochDay(), 20000));
assertThat(readMatching(predicate, ancient)).isEqualTo(1);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PredicateBuilder#in(int, List) rewrites literal lists of size <= 20 into an OR chain of Equal predicates, so a 2-element list exercises visitEqual + visitOr rather than OrcPredicateFunctionVisitor#visitIn (the test would still pass even without the beforeGregorianSwitchover check in visitIn).

To exercise visitIn directly, could we either pass > 20 literals to PredicateBuilder#in (e.g., via IntStream.rangeClosed(20000, 20020)) or construct a LeafPredicate with In.INSTANCE?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, you are right: PredicateBuilder#in keeps only lists longer than 20 literals as an In leaf, so my two-element list was compiled into OR(Equal, Equal) and visitIn was never called.

The test now builds 20000..20020 (21 literals) and asserts the visitor output is an OrcFilters.In, which pins that the IN path is the one under test. A second predicate adds the ancient day to that list, asserts the whole pushdown is declined, and still expects both matching rows back from the reader.

Verified by mutation: with beforeGregorianSwitchover forced to false, this test now fails (it passed before this change).

assertThat(readMatching(equal(ancient), ancient)).isEqualTo(1);
assertThat(readMatching(equal(recent), ancient)).isZero();
assertThat(readMatching(equal(recent), recent)).isEqualTo(1);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since readMatching also applies predicate.test(row) in Java after reading, these assertions would still pass even if OrcPredicateFunctionVisitor declined pushdown for all dates. Could we also assert on the visitor output around the exact switchover boundary so the threshold is pinned?

assertThat(equal(LocalDate.of(1582, 10, 14)).visit(OrcPredicateFunctionVisitor.VISITOR))
        .isEmpty();
assertThat(equal(LocalDate.of(1582, 10, 15)).visit(OrcPredicateFunctionVisitor.VISITOR))
        .isPresent();

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, the row counts alone do not prove anything about pushdown since readMatching re-applies predicate.test(row) after the read.

Added testPushdownIsDeclinedOnlyBeforeTheSwitchover with exactly the boundary assertions you suggested: 1582-10-14 → Optional.empty(), 1582-10-15 → present. Both directions are mutation-checked now — forcing the predicate to false fails 4/4 tests, forcing it to true (declining every DATE literal, the case your first comment describes as passing silently) fails the new boundary test and the OrcFilters.In assertion.

… boundary

The previous in-list case passed a two-element list, but PredicateBuilder#in
rewrites lists of at most 20 literals into an OR chain of equals, so visitIn
was never reached. Building the list from 20000 to 20020 keeps it above that
limit, and asserting the visitor output is an OrcFilters.In pins that the IN
path is the one under test.

readMatching also applies predicate.test(row) after reading, so the row-count
assertions hold even when pushdown is declined. Assert on the visitor output
around the boundary as well: 1582-10-14 is declined, 1582-10-15 is pushed down.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants