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


##########
datafusion/optimizer/src/analyzer/type_coercion.rs:
##########
@@ -290,19 +290,72 @@ impl<'a> TypeCoercionRewriter<'a> {
         right: Expr,
         right_schema: &DFSchema,
     ) -> Result<(Expr, Expr)> {
+        if let Expr::Literal(ref lit_value) = left {
+            if let Some(casted) =
+                try_cast_literal_to_type(lit_value, op, 
&right.get_type(right_schema)?)
+            {
+                return Ok((casted, right));
+            };
+        }
+
+        if let Expr::Literal(ref lit_value) = right {
+            if let Some(casted) =
+                try_cast_literal_to_type(lit_value, op, 
&left.get_type(left_schema)?)
+            {
+                return Ok((left, casted));
+            };
+        }
+
         let (left_type, right_type) = BinaryTypeCoercer::new(
             &left.get_type(left_schema)?,
             &op,
             &right.get_type(right_schema)?,
         )
         .get_input_types()?;
+
         Ok((
             left.cast_to(&left_type, left_schema)?,
             right.cast_to(&right_type, right_schema)?,
         ))
     }
 }
 
+fn try_cast_literal_to_type(
+    lit_value: &ScalarValue,
+    op: Operator,
+    target_type: &DataType,
+) -> Option<Expr> {
+    match (op, lit_value) {
+        (
+            Operator::Eq | Operator::NotEq,
+            ScalarValue::Utf8(Some(_))
+            | ScalarValue::Utf8View(Some(_))
+            | ScalarValue::LargeUtf8(Some(_)),
+        ) => {
+            // 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)

Review Comment:
   IMO it should be enough to add logic in the other file.
   What coercions get applied is the SQL dialect-specific, query 
planner-dependent.
   What optimizations get applied -- is the common optimizer brain all 
datafusion users end up using.



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