neilconway commented on code in PR #22482:
URL: https://github.com/apache/datafusion/pull/22482#discussion_r3319055553


##########
datafusion/functions/src/math/power.rs:
##########
@@ -582,22 +490,43 @@ impl ScalarUDFImpl for PowerFunc {
             )));
         }
 
+        // The simplified expression must keep `return_type`'s declared
+        // type. For example, `power(decimal, float)` returns `Float64`,
+        // so rewriting `power(decimal, 0)` to `1::decimal` or
+        // `power(decimal, 1)` to the decimal base would violate that
+        // contract and trip the optimizer's schema-compatibility check.
+        // Wrap simplified forms in a cast to `return_type` when needed.
+        let return_type =
+            self.return_type(&[base_type.clone(), exponent_type.clone()])?;
+        let needs_cast = |t: &DataType| *t != return_type;
         match exponent {
             Expr::Literal(value, _)
                 if value == ScalarValue::new_zero(&exponent_type)? =>
             {
                 Ok(ExprSimplifyResult::Simplified(lit(ScalarValue::new_one(
                     &base_type,

Review Comment:
   Makes sense, done.



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