Commit a224b00
fix: unwrap identity Date cast in comparison unwrapping
`unwrap_cast_in_comparison` refused to fold an identity `CAST(col AS DATE)`
on a `Date32` column, leaving a residual `Cast(col AS Date32)` in the
predicate instead of comparing against the bare column. SQL `cast(col AS
date)` plants a real `Expr::Cast` with no identity elision (unlike the
`arrow_cast` UDF, whose `simplify()` short-circuits identity), so this cast
reached `try_cast_literal_to_type` and was rejected.
Root cause is in `is_lossy_temporal_cast`:
(is_date_type(from) && to.is_temporal()) || (is_date_type(to) && from.is_temporal())
For an identity `Date32 -> Date32` cast this is `true && true`, because
arrow's `DataType::is_temporal()` is true for `Date32`/`Date64`. The cast is
wrongly classified as a lossy temporal cast, `try_cast_literal_to_type`
returns `None`, and the residual cast is left in place.
Fix: short-circuit an identity cast (`from_type == to_type`) as non-lossy at
the top of `is_lossy_temporal_cast`. An identity cast can never change
comparison semantics.
This is deliberately scoped to *identical* types only. `Date32` counts days
and `Date64` counts milliseconds, but `try_cast_numeric_literal` uses
`mul = 1` for both, so a `Date32 <-> Date64` cast would convert units
wrongly and must stay blocked. A regression test asserts that.
Tests: identity `Date32 -> Date32` / `Date64 -> Date64` now fold;
`Date32 <-> Date64` still does not; plus an end-to-end simplifier test that
`cast(date_col AS DATE) = DATE '...'` simplifies to `date_col = <lit>`.
apache/datafusion `main` carries the same `is_lossy_temporal_cast`, so this
is also a candidate to upstream.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 11d6e91 commit a224b00
2 files changed
Lines changed: 76 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
77 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
78 | 90 | | |
79 | 91 | | |
80 | 92 | | |
| |||
760 | 772 | | |
761 | 773 | | |
762 | 774 | | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
763 | 826 | | |
764 | 827 | | |
765 | 828 | | |
| |||
Lines changed: 13 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
586 | 586 | | |
587 | 587 | | |
588 | 588 | | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
589 | 601 | | |
590 | 602 | | |
591 | 603 | | |
| |||
608 | 620 | | |
609 | 621 | | |
610 | 622 | | |
| 623 | + | |
611 | 624 | | |
612 | 625 | | |
613 | 626 | | |
| |||
0 commit comments