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

##########
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(_)),

Review Comment:
   tho I was wondering If we should care about the operator, since both 
postgres and duckdb cast the literal regardless of the operator (they even emit 
an error when the literal cannot be casted)
   
   we do not perform the round-trip casting verification here is also a concern



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