Skip to content

Commit 37d3fdb

Browse files
U0001F3A2claude
andcommitted
test: cover more log() coercion paths and document the type change
Add integer, decimal, dictionary, and NULL cases to the coercion test, a Float32 typed NULL to the simplify test, and Float16/Float32 and NULL/NULL SQL cases. Add an upgrade guide entry for the log() result type change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f99f50d commit 37d3fdb

3 files changed

Lines changed: 55 additions & 4 deletions

File tree

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -776,15 +776,25 @@ mod tests {
776776
fn test_log_simplify_typed_null() {
777777
// Coercion can turn a NULL argument into a typed NULL literal
778778
let context = SimplifyContext::default();
779-
for args in [
780-
vec![lit(ScalarValue::Float64(None)), lit(2.0)],
781-
vec![lit(2.0), lit(ScalarValue::Float64(None))],
779+
for (args, expected) in [
780+
(
781+
vec![lit(ScalarValue::Float64(None)), lit(2.0)],
782+
ScalarValue::Float64(None),
783+
),
784+
(
785+
vec![lit(2.0), lit(ScalarValue::Float64(None))],
786+
ScalarValue::Float64(None),
787+
),
788+
(
789+
vec![lit(ScalarValue::Float32(None)), lit(2.0f32)],
790+
ScalarValue::Float32(None),
791+
),
782792
] {
783793
let result = LogFunc::new().simplify(args, &context).unwrap();
784794
let ExprSimplifyResult::Simplified(expr) = result else {
785795
panic!("Expected ExprSimplifyResult::Simplified")
786796
};
787-
assert_eq!(expr, lit(ScalarValue::Float64(None)));
797+
assert_eq!(expr, lit(expected));
788798
}
789799
}
790800

@@ -1271,6 +1281,12 @@ mod tests {
12711281
assert_eq!(coerce(&[Float32, Float16]), [Float32, Float32]);
12721282
assert_eq!(coerce(&[Float16, Float64]), [Float64, Float64]);
12731283
assert_eq!(coerce(&[Int64, Float32]), [Float64, Float64]);
1284+
assert_eq!(coerce(&[Float32, Int64]), [Float64, Float64]);
1285+
assert_eq!(coerce(&[Decimal128(10, 2), Float32]), [Float64, Float64]);
1286+
assert_eq!(
1287+
coerce(&[Float64, Dictionary(Box::new(Int32), Box::new(Float32))]),
1288+
[Float64, Float64]
1289+
);
12741290
// Homogeneous inputs keep their type
12751291
assert_eq!(coerce(&[Float16, Float16]), [Float16, Float16]);
12761292
assert_eq!(coerce(&[Float32, Float32]), [Float32, Float32]);
@@ -1282,5 +1298,10 @@ mod tests {
12821298
[Float32, Decimal128(10, 2)]
12831299
);
12841300
assert_eq!(coerce(&[Decimal128(10, 2)]), [Decimal128(10, 2)]);
1301+
// A NULL base is typed by the value; a NULL value matches the decimal
1302+
// value signature and stays untyped
1303+
assert_eq!(coerce(&[Null, Float32]), [Float32, Float32]);
1304+
assert_eq!(coerce(&[Float32, Null]), [Float32, Null]);
1305+
assert_eq!(coerce(&[Null, Null]), [Null, Null]);
12851306
}
12861307
}

‎datafusion/sqllogictest/test_files/math.slt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,12 @@ SELECT log(2.5::float, 10.9::float), arrow_typeof(log(2.5::float, 10.9::float));
13391339
----
13401340
2.606992 Float32
13411341

1342+
# A Float16 base is widened to a Float32 value's type
1343+
query RT
1344+
SELECT log(arrow_cast(2.5, 'Float16'), 10.9::float), arrow_typeof(log(arrow_cast(2.5, 'Float16'), 10.9::float));
1345+
----
1346+
2.606992 Float32
1347+
13421348
# #22581: a Float64 base must keep full precision against a Float32 value.
13431349
# 16777217 is not representable in Float32, so an f32 computation would round it.
13441350
query RT
@@ -1357,6 +1363,11 @@ SELECT log(NULL, 2.0::float), arrow_typeof(log(NULL, 2.0::float));
13571363
----
13581364
NULL Float32
13591365

1366+
query RT
1367+
SELECT log(NULL, NULL), arrow_typeof(log(NULL, NULL));
1368+
----
1369+
NULL Float64
1370+
13601371
# A Float16 base is widened to a wider value's type
13611372
query RT
13621373
SELECT log(arrow_cast(2.5, 'Float16'), 1.0::double), arrow_typeof(log(arrow_cast(2.5, 'Float16'), 1.0::double));

‎docs/source/library-user-guide/upgrading/56.0.0.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,25 @@ value of the stride instead. See [issue #25856] for details.
684684
[postgresql]: https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-BIN
685685
[issue #25856]: https://github.com/apache/datafusion/issues/25856
686686

687+
### `log(base, value)` widens float arguments to the wider type
688+
689+
`log(base, value)` previously computed in the float type of `value`, so a
690+
wider `base` was narrowed and lost precision. Both arguments are now widened to
691+
the wider float type, and the result has that type. An integer or decimal
692+
`base` counts as `Float64`.
693+
694+
```sql
695+
-- Before: 0.041666668 (Float32)
696+
-- After: 0.041666666517376175 (Float64)
697+
SELECT log(arrow_cast(16777217, 'Float64'), arrow_cast(2.0, 'Float32'));
698+
```
699+
700+
Calls whose arguments have the same float type, and calls with a decimal
701+
`value`, are unchanged. Wrap the call in `CAST` to keep the previous result
702+
type. See [issue #22581] for details.
703+
704+
[issue #22581]: https://github.com/apache/datafusion/issues/22581
705+
687706
### `DataFrame::from_columns` accepts `IntoIterator`
688707

689708
`DataFrame::from_columns` now accepts any `IntoIterator<Item = (&str, ArrayRef)>`

0 commit comments

Comments
 (0)