alamb commented on code in PR #26989:
URL: https://github.com/apache/datafusion/pull/26989#discussion_r4648312531
##########
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:
What does "retained" mean here? Does it mean like after this PR the behavior
is the same? that will probably get stale as soon as we merge the PR
an example I think would help understand the implication of this
I am pretty sure this is a wrong results problem (even if the agent accepted
a documentation as "expected result")
##########
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
Review Comment:
I think an example for these cases would help a lot (e.g. requring a
truncation bucket, etc)
##########
datafusion/expr-common/src/casts.rs:
##########
@@ -1766,6 +1899,406 @@ mod tests {
);
}
+ #[test]
Review Comment:
these are very verbose rust tests -- do we really need to use rust unit
tests ? Don't the SLT tests cover most of this anyways (or could be extended to
cover them)?
##########
datafusion/expr-common/src/casts.rs:
##########
@@ -391,6 +476,121 @@ fn exact_preimage_int_to_str_eq_like(
}
}
+/// Computes the source-domain preimage of a `Date32` to timezone-naive
timestamp
Review Comment:
> Rewritten comparisons can return Booleans where CAST errors or TRY_CAST
returns NULL.
This sounds like a wrong results bug to me -- i don't think documenting is
an adequate remedy
--
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]