alamb commented on code in PR #18968:
URL: https://github.com/apache/datafusion/pull/18968#discussion_r2620881073
##########
datafusion/functions/src/math/power.rs:
##########
@@ -162,57 +181,23 @@ impl ScalarUDFImpl for PowerFunc {
}
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
- Ok(arg_types[0].clone())
+ if arg_types[0].is_null() {
+ Ok(DataType::Float64)
+ } else {
+ Ok(arg_types[0].clone())
+ }
}
fn aliases(&self) -> &[String] {
&self.aliases
}
- fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
- let [arg1, arg2] = take_function_args(self.name(), arg_types)?;
-
- fn coerced_type_exp(name: &str, data_type: &DataType) ->
Result<DataType> {
- match data_type {
- DataType::Null => Ok(DataType::Int64),
- d if d.is_floating() => Ok(DataType::Float64),
- d if d.is_integer() => Ok(DataType::Int64),
- d if is_decimal(d) => Ok(DataType::Float64),
- other => {
- exec_err!("Unsupported data type {other:?} for {}
function", name)
- }
- }
- }
-
- // Determine the exponent type first, as it affects base coercion
- let exp_type = coerced_type_exp(self.name(), arg2)?;
-
- // For base coercion: always use Float64 for integer/null bases
- // This matches PostgreSQL behavior and handles negative exponents
correctly
- fn coerced_type_base(name: &str, data_type: &DataType) ->
Result<DataType> {
- match data_type {
- d if d.is_floating() => Ok(DataType::Float64),
- // Integer and Null bases always coerce to Float64
- // (integer power doesn't support negative exponents, and pow()
- // should return float like PostgreSQL does)
- DataType::Null => Ok(DataType::Float64),
- d if d.is_integer() => Ok(DataType::Float64),
- d if is_decimal(d) => Ok(d.clone()),
- other => {
- exec_err!("Unsupported data type {other:?} for {}
function", name)
- }
- }
- }
-
- Ok(vec![coerced_type_base(self.name(), arg1)?, exp_type])
- }
-
fn invoke_with_args(&self, args: ScalarFunctionArgs) ->
Result<ColumnarValue> {
- let base = &args.args[0].to_array(args.number_rows)?;
- let exponent = &args.args[1];
+ let [base, exponent] = take_function_args(self.name(), &args.args)?;
Review Comment:
👍
--
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]