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


##########
datafusion/optimizer/src/simplify_expressions/unwrap_cast.rs:
##########
@@ -177,6 +192,45 @@ pub(super) fn 
is_cast_expr_and_support_unwrap_cast_in_comparison_for_inlist<
     true
 }
 
+fn cast_literal_to_type_with_op(
+    lit_value: &ScalarValue,
+    target_type: &DataType,
+    op: Operator,
+) -> Option<ScalarValue> {
+    macro_rules! cast_or_else_return_none {

Review Comment:
   Since `ScalarValue` is already dynamic, I don't think we need a macro to run 
on each type
   
   I played around a little locally and this is what I came up with. What do 
you think?
   
   ```rust
   /// Try to move a cast from a literal to the other side of a `=` / `!=` 
operator
   ///
   /// Specifically, rewrites
   /// ```sql
   /// cast(col) <op> <litera>
   /// ```
   ///
   /// To
   ///
   /// ```sql
   /// col <op> cast(<literal>)
   /// col <op> <cast_literal>
   /// ```
   fn cast_literal_to_type_with_op(
       lit_value: &ScalarValue,
       target_type: &DataType,
       op: Operator,
   ) -> Option<ScalarValue> {
       let (
           Operator::Eq | Operator::NotEq,
           ScalarValue::Utf8(Some(_))
           | ScalarValue::Utf8View(Some(_))
           | ScalarValue::LargeUtf8(Some(_)),
       ) = (op, lit_value)
       else {
           return None;
       };
   
       // Only try for integer types (TODO can we do this for other types
       // like timestamps)?
       use DataType::*;
       if matches!(
           target_type,
           Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64
       ) {
           let opts = arrow::compute::CastOptions {
               safe: false,
               format_options: Default::default(),
           };
   
           let array = ScalarValue::to_array(lit_value).ok()?;
           let casted =
               arrow::compute::cast_with_options(&array, target_type, 
&opts).ok()?;
           ScalarValue::try_from_array(&casted, 0).ok()
       } else {
           None
       }
   }
   ```
   
   



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to