Skip to content

Commit 5d68b75

Browse files
authored
fix: trunc function with precision uses round instead of trunc semantics (#19794)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #19793. ## Rationale for this change The helper functions `compute_truncate32` and `compute_truncate64` incorrectly use `.round()` instead of `.trunc()`, causing incorrect results: - `trunc(3.76, 1)` returned `3.8` (wrong - rounded) - `trunc(3.76, 1)` should return `3.7` (correct - truncated) <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? - Change `.round()` to `.trunc()` in `compute_truncate32` and `compute_truncate64` - Update unit test expected values to reflect correct truncation behavior <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? Yes. Unit tests updated and pass. <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 7c9a76a commit 5d68b75

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

datafusion/functions/src/math/trunc.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ fn trunc(args: &[ArrayRef]) -> Result<ArrayRef> {
202202

203203
fn compute_truncate32(x: f32, y: i64) -> f32 {
204204
let factor = 10.0_f32.powi(y as i32);
205-
(x * factor).round() / factor
205+
(x * factor).trunc() / factor
206206
}
207207

208208
fn compute_truncate64(x: f64, y: i64) -> f64 {
209209
let factor = 10.0_f64.powi(y as i32);
210-
(x * factor).round() / factor
210+
(x * factor).trunc() / factor
211211
}
212212

213213
#[cfg(test)]
@@ -238,9 +238,9 @@ mod test {
238238

239239
assert_eq!(floats.len(), 5);
240240
assert_eq!(floats.value(0), 15.0);
241-
assert_eq!(floats.value(1), 1_234.268);
241+
assert_eq!(floats.value(1), 1_234.267);
242242
assert_eq!(floats.value(2), 1_233.12);
243-
assert_eq!(floats.value(3), 3.312_98);
243+
assert_eq!(floats.value(3), 3.312_97);
244244
assert_eq!(floats.value(4), -21.123_4);
245245
}
246246

@@ -263,9 +263,9 @@ mod test {
263263

264264
assert_eq!(floats.len(), 5);
265265
assert_eq!(floats.value(0), 5.0);
266-
assert_eq!(floats.value(1), 234.268);
266+
assert_eq!(floats.value(1), 234.267);
267267
assert_eq!(floats.value(2), 123.12);
268-
assert_eq!(floats.value(3), 123.312_98);
268+
assert_eq!(floats.value(3), 123.312_97);
269269
assert_eq!(floats.value(4), -321.123_1);
270270
}
271271

datafusion/sqllogictest/test_files/scalar.slt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ from small_floats;
11651165
----
11661166
0.447 0.4 0.447
11671167
0.707 0.7 0.707
1168-
0.837 0.8 0.837
1168+
0.836 0.8 0.836
11691169
1 1 1
11701170

11711171
## bitwise and

0 commit comments

Comments
 (0)