Commit 8a5e75d
committed
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. The residual cast
defeats downstream pruning/pushdown that expects a bare-column comparison.
The 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
`DataType::is_temporal()` is true for both `Date32` and `Date64`. The cast
is misclassified 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, not "any date-to-date".
`Date32` counts days while `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>`.
Signed-off-by: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com>1 parent 67947b6 commit 8a5e75d
2 files changed
Lines changed: 76 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
101 | 110 | | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
102 | 114 | | |
103 | 115 | | |
104 | 116 | | |
| |||
813 | 825 | | |
814 | 826 | | |
815 | 827 | | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
816 | 879 | | |
817 | 880 | | |
818 | 881 | | |
| |||
Lines changed: 13 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
604 | 604 | | |
605 | 605 | | |
606 | 606 | | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
607 | 619 | | |
608 | 620 | | |
609 | 621 | | |
| |||
646 | 658 | | |
647 | 659 | | |
648 | 660 | | |
| 661 | + | |
649 | 662 | | |
650 | 663 | | |
651 | 664 | | |
| |||
0 commit comments