Trikooo commented on code in PR #20012:
URL: https://github.com/apache/datafusion/pull/20012#discussion_r2742971435


##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -598,13 +598,32 @@ impl ExprSchemable for Expr {
                         )
                     })?;
 
-                let arguments = args
+                let coerced_values: Vec<Option<ScalarValue>> = args
                     .iter()
-                    .map(|e| match e {
-                        Expr::Literal(sv, _) => Some(sv),
-                        _ => None,
+                    .zip(new_fields.iter())
+                    .map(|(expr, field)| {
+                        let mut current_expr = expr;
+
+                        // Loop to remove all casting layers
+                        loop {
+                            match current_expr {
+                                Expr::Cast(cast) => current_expr = &cast.expr,
+                                Expr::TryCast(cast) => current_expr = 
&cast.expr,
+                                _ => break,
+                            }
+                        }
+
+                        let literal = if let Expr::Literal(sv, _) = 
current_expr {
+                            Some(sv)
+                        } else {
+                            None
+                        };
+
+                        literal.map(|sv| 
sv.cast_to(field.data_type())).transpose()

Review Comment:
   I verified that `to_field` is called 3 times, the following methods are the 
triggers: 
   
   1. `SqlToRel::select_to_plan`.
   2. `SqlToRel::try_process_unnest`.
   3. `SessionState::optimize` Specifically during the `TypeCoercion` Analyzer 
pass. 
   
   The first two triggers happen during the initial `SqlToRel` phase. At that 
point, the Analyzer hasn't even seen the plan yet, so the literal is still raw 
(e.g., `Int64(1)`) therefore I think we cannot materialize the cast at that 
stage.



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