Skip to content

Commit c383b07

Browse files
committed
fix: keep log domain errors through simplifications
1 parent 3e6d538 commit c383b07

3 files changed

Lines changed: 109 additions & 10 deletions

File tree

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

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,9 @@ impl ScalarUDFImpl for LogFunc {
345345
/// Simplify the `log` function by the relevant rules:
346346
/// 1. Log(a, 1) ===> 0
347347
/// 2. Log(a, Power(a, b)) ===> b
348+
/// (only when `Power(a, b)` is known to be a valid log argument)
348349
/// 3. Log(a, a) ===> 1
350+
/// (only when `a` is known to be a valid log argument)
349351
fn simplify(
350352
&self,
351353
mut args: Vec<Expr>,
@@ -406,13 +408,18 @@ impl ScalarUDFImpl for LogFunc {
406408
if is_pow(&func)
407409
&& args.len() == 2
408410
&& base == args[0]
409-
&& !base_nullable =>
411+
&& !base_nullable
412+
&& is_known_positive_power_result(&args[0], &args[1]) =>
410413
{
411414
let b = args.pop().unwrap(); // length checked above
412415
Ok(ExprSimplifyResult::Simplified(b))
413416
}
414417
number => {
415-
if number == base && !base_nullable {
418+
// `log(a, a) => 1` is only valid when `a` is known to be a
419+
// strictly positive finite value. Zero/negative literals and
420+
// non-constant expressions must not be rewritten so the
421+
// domain-error check still runs (e.g. `log(0, 0)`).
422+
if number == base && !base_nullable && is_known_valid_log_value(&number) {
416423
Ok(ExprSimplifyResult::Simplified(lit(ScalarValue::new_one(
417424
&number_datatype,
418425
)?)))
@@ -438,6 +445,37 @@ fn is_pow(func: &ScalarUDF) -> bool {
438445
func.inner().is::<PowerFunc>()
439446
}
440447

448+
/// Cast a literal expression to `f64`, if possible.
449+
fn literal_as_f64(expr: &Expr) -> Option<f64> {
450+
let Expr::Literal(value, _) = expr else {
451+
return None;
452+
};
453+
match value.cast_to(&DataType::Float64) {
454+
Ok(ScalarValue::Float64(Some(v))) => Some(v),
455+
_ => None,
456+
}
457+
}
458+
459+
/// True when `expr` is a literal known to be a valid log argument: finite
460+
/// and strictly greater than zero. Non-literals, zero, negatives, NaN, and
461+
/// infinities are not statically safe and must not be rewritten away.
462+
fn is_known_valid_log_value(expr: &Expr) -> bool {
463+
matches!(literal_as_f64(expr), Some(v) if v.is_finite() && v > 0.0)
464+
}
465+
466+
/// True when `power(base, exponent)` is known to evaluate to a strictly
467+
/// positive finite value, so `log(base, power(base, exponent))` will not
468+
/// hit the log-of-zero domain error (or produce 0 via underflow).
469+
fn is_known_positive_power_result(base: &Expr, exponent: &Expr) -> bool {
470+
match (literal_as_f64(base), literal_as_f64(exponent)) {
471+
(Some(b), Some(e)) if b.is_finite() && b > 0.0 && e.is_finite() => {
472+
let p = b.powf(e);
473+
p.is_finite() && p > 0.0
474+
}
475+
_ => false,
476+
}
477+
}
478+
441479
#[cfg(test)]
442480
mod tests {
443481
use std::sync::Arc;
@@ -1374,4 +1412,28 @@ mod tests {
13741412
)
13751413
.expect("zero base should not error in this change");
13761414
}
1415+
1416+
#[test]
1417+
fn test_log_zero_every_physical_type() {
1418+
let data_types = [
1419+
DataType::Float16,
1420+
DataType::Float32,
1421+
DataType::Float64,
1422+
DataType::Decimal32(9, 2),
1423+
DataType::Decimal64(18, 2),
1424+
DataType::Decimal128(38, 0),
1425+
DataType::Decimal256(DECIMAL256_MAX_PRECISION, 0),
1426+
];
1427+
1428+
for data_type in data_types {
1429+
let zero = ScalarValue::new_zero(&data_type)
1430+
.unwrap_or_else(|e| panic!("zero for {data_type}: {e}"));
1431+
let err =
1432+
invoke_log(vec![ColumnarValue::Scalar(zero)], vec![data_type.clone()])
1433+
.expect_err(&format!(
1434+
"log(0) as {data_type} should be a domain error"
1435+
));
1436+
assert_log_of_zero(err);
1437+
}
1438+
}
13771439
}

‎datafusion/sqllogictest/test_files/math.slt‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,18 @@ SELECT log(x) FROM (VALUES (0.0::float8)) AS t(x)
11821182
query error DataFusion error: Arrow error: Compute error: cannot take logarithm of zero
11831183
SELECT log(0::decimal(10, 2))
11841184

1185+
# log(0, 0) must not simplify to 1
1186+
query error DataFusion error: Arrow error: Compute error: cannot take logarithm of zero
1187+
SELECT log(0.0::float8, 0.0::float8)
1188+
1189+
# log(a, power(a, b)) must not rewrite to b when the power result is zero
1190+
query error DataFusion error: Arrow error: Compute error: cannot take logarithm of zero
1191+
SELECT log(0.0::float8, power(0.0::float8, 1.0::float8))
1192+
1193+
# underflow of a positive base still yields a zero power result
1194+
query error DataFusion error: Arrow error: Compute error: cannot take logarithm of zero
1195+
SELECT log(2.0::float8, power(2.0::float8, -2000.0::float8))
1196+
11851197
# factorial negative (PostgreSQL-compatible domain error)
11861198
query error DataFusion error: Execution error: factorial of a negative number is undefined
11871199
select factorial(-1);
@@ -1233,7 +1245,9 @@ physical_plan
12331245
01)ProjectionExec: expr=[log(column1@0, 1) as l1, log(column1@0, column1@0) as la, power(column1@0, 0) as p0, power(column1@0, log(column1@0, column2@1)) as pl]
12341246
02)--DataSourceExec: partitions=1, partition_sizes=[1]
12351247

1236-
# Non-nullable bases still use the existing simplifications
1248+
# Non-nullable bases still use simplifications that cannot skip domain checks.
1249+
# log(a, 1) and power(...) are unchanged. log(a, a) is not rewritten because
1250+
# a column is not a known-positive literal.
12371251
query TT
12381252
EXPLAIN SELECT
12391253
log(a, 1) AS l1,
@@ -1243,14 +1257,36 @@ EXPLAIN SELECT
12431257
FROM (VALUES (2.0::double, 3.0::double)) AS t(a, b);
12441258
----
12451259
logical_plan
1246-
01)Projection: Float64(0) AS l1, Float64(1) AS la, Float64(1) AS p0, t.b AS pl
1260+
01)Projection: Float64(0) AS l1, log(t.a, t.a) AS la, Float64(1) AS p0, t.b AS pl
12471261
02)--SubqueryAlias: t
1248-
03)----Projection: column2 AS b
1262+
03)----Projection: column1 AS a, column2 AS b
12491263
04)------Values: (Float64(2), Float64(3))
12501264
physical_plan
1251-
01)ProjectionExec: expr=[0 as l1, 1 as la, 1 as p0, column2@1 as pl]
1265+
01)ProjectionExec: expr=[0 as l1, log(column1@0, column1@0) as la, 1 as p0, column2@1 as pl]
12521266
02)--DataSourceExec: partitions=1, partition_sizes=[1]
12531267

1268+
# Known-positive literals still use log(a, a) => 1
1269+
query TT
1270+
EXPLAIN SELECT log(2.0::float8, 2.0::float8);
1271+
----
1272+
logical_plan
1273+
01)Projection: Float64(1) AS log(Float64(2),Float64(2))
1274+
02)--EmptyRelation: rows=1
1275+
physical_plan
1276+
01)ProjectionExec: expr=[1 as log(Float64(2),Float64(2))]
1277+
02)--PlaceholderRowExec
1278+
1279+
# Known-positive power result still uses log(a, power(a, b)) => b
1280+
query TT
1281+
EXPLAIN SELECT log(2.0::float8, power(2.0::float8, 3.0::float8));
1282+
----
1283+
logical_plan
1284+
01)Projection: Float64(3) AS log(Float64(2),power(Float64(2),Float64(3)))
1285+
02)--EmptyRelation: rows=1
1286+
physical_plan
1287+
01)ProjectionExec: expr=[3 as log(Float64(2),power(Float64(2),Float64(3)))]
1288+
02)--PlaceholderRowExec
1289+
12541290
# Float 16/32/64 for log
12551291
query RT
12561292
SELECT log(2.5, arrow_cast(10.9, 'Float16')), arrow_typeof(log(2.5, arrow_cast(10.9, 'Float16')));

‎docs/source/user-guide/expressions.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,11 @@ Most math functions in DataFusion follow Rust / IEEE 754 semantics for
155155
corner cases. For example `log(-1)` returns `NaN`. Some domain errors
156156
match PostgreSQL instead and fail the query:
157157

158-
* `log(0)` / `log(0.0::float8)` — cannot take logarithm of zero
159-
* `sqrt` of a negative number
160-
* `power(0, negative)`
161-
* `factorial` of a negative number
158+
- `log(0)` / `log(0.0::float8)` — cannot take logarithm of zero
159+
- `sqrt` of a negative number
160+
- `power(0, negative)`
161+
- `factorial` of a negative number
162+
162163
:::
163164

164165
## Conditional Expressions

0 commit comments

Comments
 (0)