akoshchiy commented on code in PR #26989:
URL: https://github.com/apache/datafusion/pull/26989#discussion_r4672323500
##########
datafusion/expr-common/src/casts.rs:
##########
@@ -391,6 +406,129 @@ fn exact_preimage_int_to_str_eq_like(
}
}
+/// Computes the source-domain preimage of a `Date32` to timezone-naive
timestamp
+/// cast. Each source day maps to midnight, so aligned literals have an exact
+/// preimage. Non-midnight literals have no equality preimage; ordered
predicates
+/// compare against the next day using an empty interval `[ceil, ceil)`.
+///
+/// Retained policy: at extreme source values where the cast itself overflows,
+/// `CAST` can error and `TRY_CAST` can return `NULL` while the rewritten
+/// source-unit comparison returns a Boolean.
+fn date32_to_timestamp_preimage(
+ source_type: &DataType,
+ source: &DataType,
+ target: &DataType,
+ op: Operator,
+ lit_value: &ScalarValue,
+) -> Option<CastPredicatePreimage> {
+ let (DataType::Date32, DataType::Timestamp(_, None)) = (source, target)
else {
+ return None;
+ };
+
+ let lit_value = match lit_value {
+ ScalarValue::Dictionary(_, inner) => inner.as_ref(),
+ literal => literal,
+ };
+ if lit_value.data_type() != *target {
+ return None;
+ }
+ let (ScalarValue::TimestampSecond(value, _)
+ | ScalarValue::TimestampMillisecond(value, _)
+ | ScalarValue::TimestampMicrosecond(value, _)
+ | ScalarValue::TimestampNanosecond(value, _)) = lit_value
+ else {
+ return None;
+ };
+
+ let bound = |value| wrap_dictionary_scalar(source_type,
ScalarValue::Date32(value));
+ let Some(value) = *value else {
+ return Some(CastPredicatePreimage::Exact(bound(None)));
+ };
+
+ let ticks_per_day = date_to_timestamp_multiplier(source, target)?;
+ let floor = value.div_euclid(ticks_per_day);
+ if value.rem_euclid(ticks_per_day) == 0 {
+ return Some(CastPredicatePreimage::Exact(bound(Some(
+ i32::try_from(floor).ok()?,
+ ))));
+ }
+
+ if !matches!(
+ op,
+ Operator::Lt | Operator::LtEq | Operator::Gt | Operator::GtEq
+ ) {
+ return None;
+ }
+ let ceil = i32::try_from(floor.checked_add(1)?).ok()?;
+ let bound = bound(Some(ceil));
+ Interval::try_new(bound.clone(), bound)
+ .ok()
+ .map(CastPredicatePreimage::Range)
+}
+
+/// Computes the source-domain preimage of a `Date64` to timezone-naive
timestamp
+/// cast. Arrow preserves sub-day milliseconds in `Date64`, so seconds require
a
+/// truncation bucket, milliseconds are exact, and finer units require checking
+/// millisecond alignment rather than midnight alignment.
+///
+/// Retained policy: at extreme source values where the cast itself overflows,
Review Comment:
I've added check, so there is no rewrite for Date to microsecond or
nanosecond timestamps. `Case 1` from sql should be fine now. Don't know is
there any better workaround for it.
Other cases are failing in `main` too, should it be fixed in the PR too or
better to move it to another issue?
--
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]