Skip to content

Commit 82a7d4a

Browse files
raushanprabhakar1Raushan Prabhakar
andauthored
fixing negative power to zero (apache#22277)
## Which issue does this PR close? - Closes apache#22272 ## Rationale for this change PostgreSQL rejects `power(0::float8, negative::float8)` because zero raised to a negative power is undefined. DataFusion used `f64::powf`, which returns infinity for that case. This aligns float (and related decimal float paths) with PostgreSQL’s domain semantics. ## What changes are included in this PR? - Added `float64_power_checked` to error when `base == 0.0` and `exp < 0.0` with message `zero raised to a negative power is undefined`. - Wired it into `Float64`/`Float64` `power`, decimal array-exponent float fallback, and `compute_pow_f64_result`. - Updated `math.slt` expectations for `pow(0, -0.5)` and added coverage for `power(0.0::float8, -1.0::float8)`. - Added unit tests in `power.rs`. ## Are these changes tested? Yes. Unit tests in `datafusion/functions/src/math/power.rs`, and SQL logic tests in `datafusion/sqllogictest/test_files/math.slt` (including the regression for apache#22272). ## Are there any user-facing changes? Yes. Queries that previously returned positive infinity for `power` / `pow` with a zero base and negative exponent now return a runtime error (compute error) with that message, matching PostgreSQL for this case. No public Rust API signature changes; treat as SQL/runtime behavior only unless your process labels SQL semantics as API. --------- Co-authored-by: Raushan Prabhakar <[email protected]>
1 parent f962686 commit 82a7d4a

2 files changed

Lines changed: 56 additions & 7 deletions

File tree

‎datafusion/functions/src/math/power.rs‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ use datafusion_expr::{
3838
use datafusion_macros::user_doc;
3939
use num_traits::{NumCast, ToPrimitive};
4040

41+
/// Matches PostgreSQL: `power(0::float8, negative)` is undefined (IEEE 754 would yield infinity).
42+
#[inline]
43+
fn float64_power_checked(base: f64, exp: f64) -> Result<f64, ArrowError> {
44+
if base == 0.0 && exp < 0.0 {
45+
return Err(ArrowError::ComputeError(
46+
"zero raised to a negative power is undefined".to_string(),
47+
));
48+
}
49+
Ok(base.powf(exp))
50+
}
51+
4152
#[user_doc(
4253
doc_section(label = "Math Functions"),
4354
description = "Returns a base expression raised to the power of an exponent.",
@@ -203,7 +214,7 @@ fn compute_pow_f64_result(
203214
scale: i8,
204215
exp: f64,
205216
) -> Result<i128, ArrowError> {
206-
let result_f64 = base_f64.powf(exp);
217+
let result_f64 = float64_power_checked(base_f64, exp)?;
207218

208219
if !result_f64.is_finite() {
209220
return Err(ArrowError::ArithmeticOverflow(format!(
@@ -382,7 +393,7 @@ fn pow_decimal_with_float_fallback(
382393
let result_f64 = calculate_binary_math::<Float64Type, Float64Type, Float64Type, _>(
383394
&base_f64,
384395
&ColumnarValue::Array(exp_f64),
385-
|b, e| Ok(f64::powf(b, e)),
396+
float64_power_checked,
386397
)?;
387398

388399
let result = cast(result_f64.as_ref(), &original_type)?;
@@ -436,7 +447,7 @@ impl ScalarUDFImpl for PowerFunc {
436447
calculate_binary_math::<Float64Type, Float64Type, Float64Type, _>(
437448
&base,
438449
exponent,
439-
|b, e| Ok(f64::powf(b, e)),
450+
float64_power_checked,
440451
)?
441452
}
442453
(DataType::Decimal32(precision, scale), DataType::Int64) => {
@@ -657,5 +668,25 @@ mod tests {
657668
// Test non-finite exponent returns error
658669
assert!(pow_decimal_float(100i128, 2, f64::NAN).is_err());
659670
assert!(pow_decimal_float(100i128, 2, f64::INFINITY).is_err());
671+
672+
// PostgreSQL: zero to a negative power is undefined
673+
assert!(pow_decimal_float(0i128, 2, -1.0).is_err());
674+
}
675+
676+
#[test]
677+
fn test_pow_decimal256_zero_to_negative_exp_errors() {
678+
assert!(pow_decimal256_float(i256::ZERO, 2, -1.0).is_err());
679+
// Negative integer exponent uses pow_decimal256_float via pow_decimal256_int
680+
assert!(pow_decimal256_int(i256::ZERO, 2, -1).is_err());
681+
}
682+
683+
#[test]
684+
fn test_float64_power_checked_zero_negative_exp() {
685+
assert_eq!(float64_power_checked(0.0, 1.0).unwrap(), 0.0);
686+
assert_eq!(float64_power_checked(2.0, -1.0).unwrap(), 0.5);
687+
for base in [0.0f64, -0.0] {
688+
assert!(float64_power_checked(base, -1.0).is_err());
689+
assert!(float64_power_checked(base, -0.5).is_err());
690+
}
660691
}
661692
}

‎datafusion/sqllogictest/test_files/math.slt‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -742,11 +742,29 @@ SELECT pow(-2, -0.5)
742742
----
743743
NaN
744744

745-
# pow() with zero base and negative exponent (returns Infinity)
746-
query R
745+
# pow() with zero base and negative exponent (PostgreSQL: domain error)
746+
query error DataFusion error: Arrow error: Compute error: zero raised to a negative power is undefined
747747
SELECT pow(0, -0.5)
748-
----
749-
Infinity
748+
749+
# power(0::float8, negative::float8) - issue #22272
750+
query error DataFusion error: Arrow error: Compute error: zero raised to a negative power is undefined
751+
SELECT power(0.0::float8, -1.0::float8)
752+
753+
# zero base, negative exponent: exact Decimal+Int64 path (pow_decimal_int → pow_decimal_float)
754+
query error DataFusion error: Arrow error: Compute error: zero raised to a negative power is undefined
755+
SELECT pow(0::decimal(38, 0), -2::bigint)
756+
757+
# bigint operands (typically coerced to float); still must reject 0^(negative)
758+
query error DataFusion error: Arrow error: Compute error: zero raised to a negative power is undefined
759+
SELECT pow(0::bigint, -2::bigint)
760+
761+
# decimal base + float scalar exponent (compute_pow_f64_result / pow_decimal_float fallback)
762+
query error DataFusion error: Arrow error: Compute error: zero raised to a negative power is undefined
763+
SELECT pow(0.0::decimal(10,2), -1.0)
764+
765+
# decimal base + array exponent (pow_decimal_with_float_fallback)
766+
query error DataFusion error: Arrow error: Compute error: zero raised to a negative power is undefined
767+
SELECT power(base, exp) FROM (VALUES (0.0::decimal(10,2), -1.0::float8)) AS t(base, exp)
750768

751769
# pow() with integer base of 1 and negative exponent
752770
query R

0 commit comments

Comments
 (0)