alamb commented on code in PR #22114:
URL: https://github.com/apache/datafusion/pull/22114#discussion_r4269770164


##########
datafusion/functions/src/datetime/date_trunc.rs:
##########
@@ -587,8 +687,313 @@ where
     1 + 3 * ((date.month() - 1) / 3)
 }
 
+/// Extract an `i64` timestamp value from a literal that matches the column's
+/// `(unit, tz)`. Returns `None` if the literal is null or not a matching

Review Comment:
   I think this only supports `None` timestamps, right?



##########
datafusion/functions/src/datetime/date_trunc.rs:
##########
@@ -436,16 +457,93 @@ impl ScalarUDFImpl for DateTruncFunc {
         &self.aliases
     }
 
+    /// Compute the preimage for `date_trunc(granularity, col) = literal`.
+    ///
+    /// `date_trunc` floors a timestamp to the start of the given period, so
+    /// for any aligned literal `L` the preimage is the half-open interval
+    /// `[L, L + 1 granularity)`.
+    fn preimage(
+        &self,
+        args: &[Expr],
+        lit_expr: &Expr,
+        info: &SimplifyContext,
+    ) -> Result<PreimageResult> {
+        let [precision, col_expr] = take_function_args(self.name(), args)?;
+
+        // Granularity literal (e.g. "month").
+        let Some(granularity) = precision
+            .as_literal()
+            .and_then(|sv| sv.try_as_str().flatten())
+            .and_then(|s| DateTruncGranularity::from_str(s).ok())
+        else {
+            return Ok(PreimageResult::None);
+        };
+
+        // If the column is a Date32/Date64 wrapped in the signature's implicit
+        // cast to Timestamp(ns), emit bounds in the Date type against the raw
+        // column. `unwrap_cast_in_comparison` doesn't currently support Date

Review Comment:
   What does this mean that `unwrap_cast_in_comparison` doesn't support Date 
targets?
   
   That sounds like a gap we should fix (as a separate PR)
   - https://github.com/apache/datafusion/issues/26289



##########
datafusion/functions/src/datetime/date_trunc.rs:
##########
@@ -587,8 +687,313 @@ where
     1 + 3 * ((date.month() - 1) / 3)
 }
 
+/// Extract an `i64` timestamp value from a literal that matches the column's
+/// `(unit, tz)`. Returns `None` if the literal is null or not a matching
+/// `Timestamp*` variant — the simplifier is responsible for coercing the
+/// literal first, so a mismatch here means the rewrite is not applicable.
+/// Peel the signature-inserted `Cast(date_col, Timestamp(ns))` wrapper and
+/// emit Date-type preimage bounds against the raw column. Returns `None` if
+/// the column isn't a Date wrapped in the expected cast, or if the literal
+/// isn't a midnight-UTC value aligned to the granularity boundary.
+fn date_column_preimage(
+    col_expr: &Expr,
+    lit_expr: &Expr,
+    granularity: DateTruncGranularity,
+    info: &SimplifyContext,
+) -> Option<PreimageResult> {
+    // The signature wraps Date columns as exactly Cast(_, 
Timestamp(Nanosecond, None)).

Review Comment:
   Update -- filed an issue
   - https://github.com/apache/datafusion/issues/26289



##########
datafusion/functions/src/datetime/date_trunc.rs:
##########
@@ -587,8 +687,313 @@ where
     1 + 3 * ((date.month() - 1) / 3)
 }
 
+/// Extract an `i64` timestamp value from a literal that matches the column's
+/// `(unit, tz)`. Returns `None` if the literal is null or not a matching
+/// `Timestamp*` variant — the simplifier is responsible for coercing the
+/// literal first, so a mismatch here means the rewrite is not applicable.
+/// Peel the signature-inserted `Cast(date_col, Timestamp(ns))` wrapper and
+/// emit Date-type preimage bounds against the raw column. Returns `None` if
+/// the column isn't a Date wrapped in the expected cast, or if the literal
+/// isn't a midnight-UTC value aligned to the granularity boundary.
+fn date_column_preimage(
+    col_expr: &Expr,
+    lit_expr: &Expr,
+    granularity: DateTruncGranularity,
+    info: &SimplifyContext,
+) -> Option<PreimageResult> {
+    // The signature wraps Date columns as exactly Cast(_, 
Timestamp(Nanosecond, None)).

Review Comment:
   this is weird to me -- it seems like this should have been simplified to me 
(like constants evaluated so the cast was already evaluated)



-- 
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]

Reply via email to