Skip to content

Commit 7ed9a0b

Browse files
authored
fix: Remove power(decimal, int) code path (apache#22651)
## Which issue does this PR close? - Closes apache#22480 - Closes apache#22510 ## Rationale for this change `power(decimal, int)` attempted to compute `power` without loss of precision. For negative exponents, the code did the computation in `Float64` and then cast the result back to `decimal`. Unfortunately, the previous implementation got this wrong, because `decimal` might not have enough precision to accurately represent the result. It seems simpler to return `Float64` for the negative exponent case. We could potentially try to return `decimal` only for non-negative exponents and `Float64` for negative exponents, but that is complicated, and also means that the code would produce different results for literal arguments vs. columnar arguments, which I think should be avoided. On reflection, it seems simplest to just remove the `power(decimal, int)` code path entirely, and have `power` always return `Float64`. This also fixes another issue in the decimal code path (apache#22480) ## What changes are included in this PR? * Remove `power(decimal, ...)` support; both args will be coerced to Float64 if necessary, and the function will always return Float64 * Update SLT * Add new test cases for apache#22480 and apache#22510 ## Are these changes tested? Yes. ## Are there any user-facing changes? Yes, `power(decimal, ...)` will now return `Float64`.
1 parent d54ce65 commit 7ed9a0b

5 files changed

Lines changed: 89 additions & 430 deletions

File tree

‎datafusion/core/src/execution/session_state.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ use crate::datasource::provider_as_source;
3030
use crate::execution::SessionStateDefaults;
3131
use crate::execution::context::{EmptySerializerRegistry, FunctionFactory, QueryPlanner};
3232
use crate::physical_planner::{DefaultPhysicalPlanner, PhysicalPlanner};
33-
use arrow_schema::{DataType, FieldRef};
33+
#[cfg(feature = "sql")]
34+
use arrow_schema::DataType;
35+
use arrow_schema::FieldRef;
3436
use datafusion_catalog::MemoryCatalogProviderList;
3537
use datafusion_catalog::information_schema::{
3638
INFORMATION_SCHEMA, InformationSchemaProvider,

‎datafusion/core/tests/expr_api/simplification.rs‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -639,28 +639,29 @@ fn test_simplify_power() {
639639
// Power(c3, 0) ===> 1
640640
{
641641
let expr = power(col("c3_non_null"), lit(0));
642-
let expected = lit(1i64);
642+
let expected = lit(1.0f64);
643643
test_simplify(expr, expected)
644644
}
645-
// Power(c3, 1) ===> c3
645+
// Power(c3, 1) ===> cast(c3 AS Float64)
646646
{
647647
let expr = power(col("c3_non_null"), lit(1));
648-
let expected = col("c3_non_null");
648+
let expected =
649+
Expr::Cast(Cast::new(Box::new(col("c3_non_null")), DataType::Float64));
649650
test_simplify(expr, expected)
650651
}
651-
// Power(c3, Log(c3, c4)) ===> cast(c4 AS Int64)
652+
// Power(c3, Log(c3, c4)) ===> cast(c4 AS Float64)
652653
// The simplifier rewrites `power(b, log(b, x))` to `x`, but the
653654
// rewritten expression must keep the same type as the original
654-
// `power` call. `power`'s declared return type follows its base
655-
// argument (c3 = Int64), so the UInt32 c4 has to be cast to Int64
656-
// to preserve the output schema the optimizer already committed to.
655+
// `power` call. `power` returns Float64, so the UInt32 c4 has to be cast
656+
// to Float64 to preserve the output schema the optimizer already
657+
// committed to.
657658
{
658659
let expr = power(
659660
col("c3_non_null"),
660661
log(col("c3_non_null"), col("c4_non_null")),
661662
);
662663
let expected =
663-
Expr::Cast(Cast::new(Box::new(col("c4_non_null")), DataType::Int64));
664+
Expr::Cast(Cast::new(Box::new(col("c4_non_null")), DataType::Float64));
664665
test_simplify(expr, expected)
665666
}
666667
// Power(c3, c4) ===> Power(c3, c4)

0 commit comments

Comments
 (0)