theirix commented on code in PR #18032:
URL: https://github.com/apache/datafusion/pull/18032#discussion_r2495386898
##########
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>> {
+ let [arg1, arg2] = take_function_args(self.name(), arg_types)?;
+
+ fn coerced_type_base(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(d.clone()),
+ other => {
+ exec_err!("Unsupported data type {other:?} for {}
function", name)
+ }
+ }
+ }
+
+ 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)
+ }
+ }
+ }
+
+ Ok(vec![
+ coerced_type_base(self.name(), arg1)?,
+ coerced_type_exp(self.name(), arg2)?,
+ ])
+ }
+
fn invoke_with_args(&self, args: ScalarFunctionArgs) ->
Result<ColumnarValue> {
- let args = ColumnarValue::values_to_arrays(&args.args)?;
+ let base = &args.args[0].to_array(args.number_rows)?;
+ let exponent = &args.args[1];
- let arr: ArrayRef = match args[0].data_type() {
+ let arr: ArrayRef = match base.data_type() {
+ DataType::Float32 => {
+ calculate_binary_math::<Float32Type, Float32Type, Float32Type,
_>(
+ base,
+ exponent,
+ |b, e| Ok(f32::powf(b, e)),
+ )?
+ }
DataType::Float64 => {
- let bases = args[0].as_primitive::<Float64Type>();
- let exponents = args[1].as_primitive::<Float64Type>();
- let result = arrow::compute::binary::<_, _, _, Float64Type>(
- bases,
- exponents,
- f64::powf,
- )?;
- Arc::new(result) as _
+ calculate_binary_math::<Float64Type, Float64Type, Float64Type,
_>(
+ &base,
+ exponent,
+ |b, e| Ok(f64::powf(b, e)),
+ )?
+ }
+ DataType::Int32 => {
+ calculate_binary_math::<Int32Type, Int32Type, Int32Type, _>(
+ &base,
+ exponent,
+ |b, e| match e.try_into() {
+ Ok(exp_u32) => b.pow_checked(exp_u32),
+ Err(_) => Err(ArrowError::ArithmeticOverflow(format!(
+ "Exponent {e} in integer computation is out of
bounds."
+ ))),
+ },
+ )?
}
DataType::Int64 => {
- let bases = downcast_named_arg!(&args[0], "base", Int64Array);
- let exponents = downcast_named_arg!(&args[1], "exponent",
Int64Array);
- bases
- .iter()
- .zip(exponents.iter())
- .map(|(base, exp)| match (base, exp) {
- (Some(base), Some(exp)) => Ok(Some(base.pow_checked(
- exp.try_into().map_err(|_| {
- exec_datafusion_err!(
- "Can't use negative exponents: {exp} in
integer computation, please use Float."
- )
- })?,
- ).map_err(|e| arrow_datafusion_err!(e))?)),
- _ => Ok(None),
- })
- .collect::<Result<Int64Array>>()
- .map(Arc::new)? as _
+ calculate_binary_math::<Int64Type, Int64Type, Int64Type, _>(
+ &base,
+ exponent,
+ |b, e| match e.try_into() {
+ Ok(exp_u32) => b.pow_checked(exp_u32),
+ Err(_) => Err(ArrowError::ArithmeticOverflow(format!(
+ "Exponent {e} in integer computation is out of
bounds."
+ ))),
+ },
+ )?
}
+ DataType::Decimal32(precision, scale) => match
exponent.data_type() {
+ DataType::Int64 => rescale_decimal(
+ calculate_binary_math::<Decimal32Type, Int64Type,
Decimal32Type, _>(
+ &base,
+ exponent,
+ |b, e| pow_decimal32_int(b, *scale, e),
+ )?,
+ *precision,
+ *scale,
+ )?,
+ DataType::Float64 => rescale_decimal(
+ calculate_binary_math::<Decimal32Type, Float64Type,
Decimal32Type, _>(
+ &base,
+ exponent,
+ |b, e| pow_decimal32_float(b, *scale, e),
+ )?,
+ *precision,
+ *scale,
+ )?,
+ other => {
+ return exec_err!("Unsupported data type {other:?} for
exponent")
+ }
+ },
+ DataType::Decimal64(precision, scale) => match
exponent.data_type() {
+ DataType::Int64 => rescale_decimal(
+ calculate_binary_math::<Decimal64Type, Int64Type,
Decimal64Type, _>(
+ &base,
+ exponent,
+ |b, e| pow_decimal64_int(b, *scale, e),
+ )?,
+ *precision,
+ *scale,
+ )?,
+ DataType::Float64 => rescale_decimal(
+ calculate_binary_math::<Decimal64Type, Float64Type,
Decimal64Type, _>(
+ &base,
+ exponent,
+ |b, e| pow_decimal64_float(b, *scale, e),
+ )?,
+ *precision,
+ *scale,
+ )?,
+ other => {
+ return exec_err!("Unsupported data type {other:?} for
exponent")
+ }
+ },
+ DataType::Decimal128(precision, scale) => match
exponent.data_type() {
+ DataType::Int64 => rescale_decimal(
+ calculate_binary_math::<Decimal128Type, Int64Type,
Decimal128Type, _>(
+ &base,
+ exponent,
+ |b, e| pow_decimal128_int(b, *scale, e),
+ )?,
+ *precision,
+ *scale,
+ )?,
+ DataType::Float64 => rescale_decimal(
+ calculate_binary_math::<
+ Decimal128Type,
+ Float64Type,
+ Decimal128Type,
+ _,
+ >(&base, exponent, |b, e| {
+ pow_decimal128_float(b, *scale, e)
+ })?,
+ *precision,
+ *scale,
+ )?,
+ other => {
+ return exec_err!("Unsupported data type {other:?} for
exponent")
+ }
+ },
+ DataType::Decimal256(precision, scale) => match
exponent.data_type() {
+ DataType::Int64 => rescale_decimal(
+ calculate_binary_math::<Decimal256Type, Int64Type,
Decimal256Type, _>(
+ &base,
+ exponent,
+ |b, e| pow_decimal256_int(b, *scale, e),
+ )?,
+ *precision,
+ *scale,
+ )?,
+ DataType::Float64 => rescale_decimal(
+ calculate_binary_math::<
Review Comment:
Exactly. `calculate_binary_math` don't lose precision and scale, it just
doesn't know about it. I tried to integrate rescaling to
`calculate_binary_math` itself, but it became difficult to have rescaling
enabled for decimal type and disabled for non-decimal types with the lack of
specialisation in Rust.
Probably, it's worth too introduce a thin wrapper around it.
--
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]