theirix commented on code in PR #18032:
URL: https://github.com/apache/datafusion/pull/18032#discussion_r2495374021
##########
datafusion/functions/src/math/power.rs:
##########
@@ -91,58 +194,209 @@ impl ScalarUDFImpl for PowerFunc {
}
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
- match arg_types[0] {
- DataType::Int64 => Ok(DataType::Int64),
- _ => Ok(DataType::Float64),
+ // Ok(arg_types[0].clone())
+ let [data_type, _] = take_function_args(self.name(), arg_types)?;
+ match data_type {
+ d if d.is_floating() => Ok(DataType::Float64),
+ d if d.is_integer() => Ok(DataType::Int64),
+ d if is_decimal(data_type) => Ok(d.clone()),
+ // DataType::Decimal32(p, s) => Ok(DataType::Decimal32(*p, *s)),
+ // DataType::Decimal64(p, s) => Ok(DataType::Decimal64(*p, 0)),
+ // DataType::Decimal128(p, s) => Ok(DataType::Decimal128(*p, 0)),
+ // DataType::Decimal256(p, s) => Ok(DataType::Decimal256(*p, 0)),
+ other => exec_err!(
+ "Unsupported data type {other:?} for {} function",
+ self.name()
+ ),
}
}
fn aliases(&self) -> &[String] {
&self.aliases
}
+ fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
Review Comment:
It's a good question. In the original code it was prevented at a signature
level, so the [evaluation
operated](https://github.com/apache/datafusion/blob/32d26187a358aaa0803867a7963cd149ec1fcd1e/datafusion/functions/src/math/power.rs#L118)
on allowed types only. However, when I tried to ban this int-to-float with new
coercion rules, the `scalar.slt` test failed, assuming we can do it (second
expression):
```sql
SELECT power(i32, exp_i) as power_i32,
power(i64, exp_f) as power_i64,
pow(f32, exp_i) as power_f32,
power(f64, exp_f) as power_f64,
pow(2, 3) as power_int_scalar,
power(2.5, 3.0) as power_float_scalar
FROM (select test.*, 3 as exp_i, 3.0 as exp_f from test) a
```
I am unsure if it worked before then.
With decimals, we allow float power only if it is convertible to int in
runtime (a focus of all these `pow_decimal_float` checks). Maybe we can apply
the same logic to the int-to-float.
--
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]