You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/10/16 20:51:07 UTC
(datafusion) 01/01: fix: preserve negative zero in trunc (#25732)
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-25732-2ff1f76f3d8c223fd5ff75539621245461f072ea
in repository https://gitbox.apache.org/repos/asf/datafusion.git
commit 1b08fea3a26c2c43a4decfa2510098cbe4d6266e
Author: Stefan Wang <1f...@gmail.com>
AuthorDate: Fri Oct 16 20:50:44 2026 +0000
fix: preserve negative zero in trunc (#25732)
## Which issue does this PR close?
- Closes https://github.com/apache/datafusion/issues/25702.
## Rationale for this change
@jonasdedden reported that trunc(column) changes -0.0 to 0.0 on the
array path, while scalar trunc(-0.0) and trunc(column, 0) keep the sign.
That makes the one-argument column form behave differently from the
scalar form and from the explicit precision form.
After this change, the one-argument Float32 and Float64 array paths use
the native trunc() result directly, so the SQL path keeps the sign of
negative zero.
## What changes are included in this PR?
The Float32 and Float64 one-argument array implementation now matches
Rust's trunc() behavior for zero values, so trunc(column) keeps the sign
like the scalar and explicit precision forms.
## What is the testing strategy for this PR?
The reported behavior is shown below for Float32 and Float64 columns, on
upstream main and on this branch.
## Testing Done
A unit test in trunc.rs checks the Float32 and Float64 results directly,
and math.slt checks the same values through SQL. The reported behavior
is also reproducible with a small program.
Put the source below in src/main.rs of a new crate, add these
dependencies to its Cargo.toml with the path pointing at a checkout, and
pass the two queries as arguments to cargo run.
```toml
[dependencies]
datafusion = { path = "../datafusion/datafusion/core", default-features = false, features = ["sql", "math_expressions"] }
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
```
```bash
cargo run -q -- \
"SELECT 1.0 / trunc(column1), 1.0 / trunc(column1, 0) FROM (VALUES (CAST(-0.0 AS REAL)))" \
"SELECT 1.0 / trunc(column1), 1.0 / trunc(column1, 0) FROM (VALUES (CAST(-0.0 AS DOUBLE)))"
```
Before, on upstream main
(https://github.com/apache/datafusion/commit/91cd7ffb927b5a4f9fee1907db50a9610759de5b).
The one-argument form returns inf, the explicit precision form returns
-inf:
```text
SELECT 1.0 / trunc(column1), 1.0 / trunc(column1, 0) FROM (VALUES (CAST(-0.0 AS REAL)))
+-----------------------------+--------------------------------------+
| Float64(1) / trunc(column1) | Float64(1) / trunc(column1,Int64(0)) |
+-----------------------------+--------------------------------------+
| inf | -inf |
+-----------------------------+--------------------------------------+
SELECT 1.0 / trunc(column1), 1.0 / trunc(column1, 0) FROM (VALUES (CAST(-0.0 AS DOUBLE)))
+-----------------------------+--------------------------------------+
| Float64(1) / trunc(column1) | Float64(1) / trunc(column1,Int64(0)) |
+-----------------------------+--------------------------------------+
| inf | -inf |
+-----------------------------+--------------------------------------+
```
After, on this branch. Both forms return -inf:
```text
SELECT 1.0 / trunc(column1), 1.0 / trunc(column1, 0) FROM (VALUES (CAST(-0.0 AS REAL)))
+-----------------------------+--------------------------------------+
| Float64(1) / trunc(column1) | Float64(1) / trunc(column1,Int64(0)) |
+-----------------------------+--------------------------------------+
| -inf | -inf |
+-----------------------------+--------------------------------------+
SELECT 1.0 / trunc(column1), 1.0 / trunc(column1, 0) FROM (VALUES (CAST(-0.0 AS DOUBLE)))
+-----------------------------+--------------------------------------+
| Float64(1) / trunc(column1) | Float64(1) / trunc(column1,Int64(0)) |
+-----------------------------+--------------------------------------+
| -inf | -inf |
+-----------------------------+--------------------------------------+
```
<details><summary>Reproducer source: main.rs</summary>
```rust
use datafusion::error::Result;
use datafusion::prelude::SessionContext;
#[tokio::main]
async fn main() -> Result<()> {
let ctx = SessionContext::new();
for sql in std::env::args().skip(1) {
println!("{sql}");
ctx.sql(&sql).await?.show().await?;
}
Ok(())
}
```
</details>
## Are there any user-facing changes?
Yes. trunc(column) now preserves the sign of -0.0 for Float32 and
Float64 inputs.
---------
Signed-off-by: 1fanwang <1f...@gmail.com>
Co-authored-by: kosiew <ko...@gmail.com>
---
datafusion/functions/src/math/trunc.rs | 51 ++++++++++++++++-------------
datafusion/sqllogictest/test_files/math.slt | 10 ++++++
2 files changed, 39 insertions(+), 22 deletions(-)
diff --git a/datafusion/functions/src/math/trunc.rs b/datafusion/functions/src/math/trunc.rs
index bb8bea8ae7..dc7af1cbbb 100644
--- a/datafusion/functions/src/math/trunc.rs
+++ b/datafusion/functions/src/math/trunc.rs
@@ -162,10 +162,6 @@ impl ScalarUDFImpl for TruncFunc {
}
};
- // Whether an explicit precision argument was supplied. The array fast
- // paths below must only apply to the two-argument form: single-argument
- // `trunc(x)` uses a different zero handling (mapping `-0.0` to `0.0`)
- // that must be preserved.
let has_precision_arg = args.args.len() == 2;
// Scalar fast path using tuple matching for (value, precision)
@@ -305,15 +301,11 @@ fn trunc(args: &[ArrayRef]) -> Result<ArrayRef> {
match num.data_type() {
Float64 => match precision {
- ColumnarValue::Scalar(Int64(Some(0))) => {
- Ok(Arc::new(
- args[0]
- .as_primitive::<Float64Type>()
- .unary::<_, Float64Type>(|x: f64| {
- if x == 0_f64 { 0_f64 } else { x.trunc() }
- }),
- ) as ArrayRef)
- }
+ ColumnarValue::Scalar(Int64(Some(0))) => Ok(Arc::new(
+ args[0]
+ .as_primitive::<Float64Type>()
+ .unary::<_, Float64Type>(|x: f64| x.trunc()),
+ ) as ArrayRef),
ColumnarValue::Array(precision) => {
let num_array = num.as_primitive::<Float64Type>();
let precision_array = precision.as_primitive::<Int64Type>();
@@ -327,15 +319,11 @@ fn trunc(args: &[ArrayRef]) -> Result<ArrayRef> {
_ => exec_err!("trunc function requires a scalar or array for precision"),
},
Float32 => match precision {
- ColumnarValue::Scalar(Int64(Some(0))) => {
- Ok(Arc::new(
- args[0]
- .as_primitive::<Float32Type>()
- .unary::<_, Float32Type>(|x: f32| {
- if x == 0_f32 { 0_f32 } else { x.trunc() }
- }),
- ) as ArrayRef)
- }
+ ColumnarValue::Scalar(Int64(Some(0))) => Ok(Arc::new(
+ args[0]
+ .as_primitive::<Float32Type>()
+ .unary::<_, Float32Type>(|x: f32| x.trunc()),
+ ) as ArrayRef),
ColumnarValue::Array(precision) => {
let num_array = num.as_primitive::<Float32Type>();
let precision_array = precision.as_primitive::<Int64Type>();
@@ -555,6 +543,25 @@ mod test {
assert_eq!(floats.value(4), -321.0);
}
+ #[test]
+ fn test_truncate_float_zero_sign_one_arg() {
+ let args: Vec<ArrayRef> = vec![Arc::new(Float64Array::from(vec![-0.0, 0.0]))];
+ let result = trunc(&args).expect("failed to initialize function truncate");
+ let floats =
+ as_float64_array(&result).expect("failed to initialize function truncate");
+
+ assert!(floats.value(0).is_sign_negative());
+ assert!(floats.value(1).is_sign_positive());
+
+ let args: Vec<ArrayRef> = vec![Arc::new(Float32Array::from(vec![-0.0, 0.0]))];
+ let result = trunc(&args).expect("failed to initialize function truncate");
+ let floats =
+ as_float32_array(&result).expect("failed to initialize function truncate");
+
+ assert!(floats.value(0).is_sign_negative());
+ assert!(floats.value(1).is_sign_positive());
+ }
+
#[test]
fn test_compute_truncate_decimal128() {
// number 12.3456 (scale 4) truncated to 3 places = 12.345
diff --git a/datafusion/sqllogictest/test_files/math.slt b/datafusion/sqllogictest/test_files/math.slt
index 9ef5043264..61c606c72b 100644
--- a/datafusion/sqllogictest/test_files/math.slt
+++ b/datafusion/sqllogictest/test_files/math.slt
@@ -88,6 +88,16 @@ SELECT round(125.2345, -3), round(125.2345, -2), round(125.2345, -1), round(125.
----
0 100 130 125 125 125.2 125.23 125.235
+query RR
+SELECT 1.0 / trunc(column1), 1.0 / trunc(column1, 0) FROM (VALUES (CAST(-0.0 AS DOUBLE)));
+----
+-Infinity -Infinity
+
+query RR
+SELECT 1.0 / trunc(column1), 1.0 / trunc(column1, 0) FROM (VALUES (CAST(-0.0 AS REAL)));
+----
+-Infinity -Infinity
+
# Round signed and unsigned integer scalar widths
query IIIIIIII
SELECT
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org