kosiew opened a new issue, #27733: URL: https://github.com/apache/datafusion/issues/27733
## Related PR #26989 [comment](https://github.com/apache/datafusion/pull/26989#pullrequestreview-6030001555) ## Problem [#26989](https://github.com/apache/datafusion/pull/26989) adds source-date bounds for supported date-to-timestamp comparisons but deliberately leaves `Date32` and `Date64` casts to `TIMESTAMP(9)` in place. Ordinary timezone-naive SQL `TIMESTAMP` literals use nanoseconds, so a common case from [#26289](https://github.com/apache/datafusion/issues/26289) still cannot use a direct date predicate for pushdown or pruning. ## Why the cast cannot simply be removed A date can fit in either source type but overflow the nanosecond timestamp's `i64` representation. For example, on a row with `d = DATE '2500-01-01'`, `CAST(d AS TIMESTAMP(9)) > TIMESTAMP '1970-01-01 00:00:00'` errors, and the `TRY_CAST` form returns NULL. Replacing either with `d > DATE '1970-01-01'` returns TRUE and would keep the row in `WHERE`. The literal fitting in `TIMESTAMP(9)` says nothing about whether the column values fit. ## Desired behavior Rewrite supported comparisons to source-date bounds when safe. Preserve results on representable dates, NULL behavior, and `CAST` errors for overflowing rows the original expression would evaluate. An optimization must not turn an error or an unknown result into a Boolean or suppress an error by pruning the offending row before it is checked. ## Proposed direction Start with an overflow-free input interval proved at the rewrite site. Compute the representable source bounds for `Date32` and `Date64` in `datafusion/expr-common/src/casts.rs`; apply the rewrite in the logical optimizer only if reliable facts prove every non-NULL input reaching the cast is in range. Do not use the rewritten predicate's own bounds as proof. Keep the cast when proof is absent. The review also suggests an expression that signals overflow. Consider that separately if range proofs miss important cases: a check on surviving output rows alone is insufficient if a pushed-down date predicate can discard an overflowing row first. Any guarded rewrite must preserve the original expression's conditional evaluation behavior. ## Scope - In: binary comparisons against timezone-naive nanosecond timestamp literals, including casts inserted by SQL coercion. Keep unsupported shapes unchanged. - Out: microsecond and timezone-aware targets, `IN` list optimization, and unrelated cast families. [#27689](https://github.com/apache/datafusion/issues/27689) covers existing wrong results for timestamp-to-timestamp and integer-to-decimal `TRY_CAST` rewrites, not this missing date optimization. ## Acceptance criteria and verification - [ ] Add SQLLogicTests in `datafusion/sqllogictest/test_files/simplify_expr.slt`: for a proven in-range input, `EXPLAIN` shows a source-date predicate without a per-row timestamp cast, and results match the casted comparison. Cover aligned and non-aligned literals and an implicit-coercion case. - [ ] Cover both date types at the lowest and highest representable source values and just outside each bound. Include NULL inputs and check values in `SELECT` as well as row selection in `WHERE`. - [ ] With no range proof, preserve the `CAST` overflow error and `TRY_CAST` NULL result. Include an overflowing row that fails the proposed date predicate so the test catches errors hidden by pushdown or pruning. -- 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]
