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


##########
datafusion/functions/src/math/power.rs:
##########
@@ -369,34 +368,21 @@ fn pow_decimal256_float_fallback(
     Ok(i256::from_i128(result_i128))
 }
 
-/// Fallback implementation for decimal power when exponent is an array.
-/// Casts decimal to float64, computes power, and casts back to original 
decimal type.
-/// This is used for performance when exponent varies per-row.
-fn pow_decimal_with_float_fallback(
-    base: &ArrayRef,
+/// Compute `power(decimal_base, float_exponent)` by casting the base to
+/// `Float64` and running `pow` in float space; returns `Float64`.
+fn pow_decimal_via_float64(

Review Comment:
   we could remove this entire codepath by letting signature coercion handle 
the casting for us
   
   
https://github.com/apache/datafusion/blob/11a79a6217891cff67c8f963265f5673e6f63267/datafusion/functions/src/math/power.rs#L93-L100
   
   - remove line 96, then we won't accept decimal + float, theyll be cast to 
float + float and we can remove all the relevant code paths including this part



##########
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:
   instead of casting the result here we can replace `base_type` with 
`return_type`
   
   ```rust
                   Ok(ExprSimplifyResult::Simplified(lit(ScalarValue::new_one(
                       &return_type,
                   )?)))
   ```



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