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-12029-59dac8a4d20e0330aeeea8c895978e85ff2df69d in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit a041d88b6251d35db099945680e831b8666df6c7 Author: EaswarKilari <[email protected]> AuthorDate: Sun Nov 8 02:44:38 2026 +0000 fix(arrow-arith): match DuckDB's rounding when multiplying an interval by a float (#12029) # Which issue does this PR close? - Closes #12004 # Rationale for this change `interval_mul_f64` is adapted from DuckDB's `INTERVAL * DOUBLE`, but it computes the sub-day remainder as `days_product.fract() + month_remainder.fract()`, which reassociates DuckDB's `days * factor - days + month_remainder - day_remainder`. Floating-point addition is not associative, so some results differ from DuckDB by one nanosecond. As discussed in the issue, align with DuckDB/PostgreSQL's order. # What changes are included in this PR? Compute the seconds remainder in DuckDB's order of operations. Months and days are unaffected. # Are these changes tested? Yes, a new regression test covers the case from the issue, the opposite rounding direction, a negative factor, and division (which multiplies by the reciprocal). It fails without the fix. # Are there any significant user-facing changes? Some `interval * f64` and `interval / f64` results change by one nanosecond to match DuckDB. AI usage: an AI assistant helped trace the code and write the regression test. I checked the fix against DuckDB's implementation and confirmed it matches DuckDB's order of operations across the issue's 212,160-case grid. --- arrow-arith/src/numeric.rs | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/arrow-arith/src/numeric.rs b/arrow-arith/src/numeric.rs index 77239641e2..c69ef0afe1 100644 --- a/arrow-arith/src/numeric.rs +++ b/arrow-arith/src/numeric.rs @@ -777,8 +777,12 @@ fn interval_mul_f64( let month_remainder_days = month_remainder .to_i32() .ok_or_else(|| overflow("month remainder"))?; - let mut seconds_remainder = - timestamp_round((days_product.fract() + month_remainder.fract()) * SECONDS_PER_DAY); + // Same order of operations as DuckDB; float arithmetic isn't associative and we're aiming + // to replicate DuckDB accuracy + let mut seconds_remainder = timestamp_round( + (days_product - f64::from(days) + month_remainder - f64::from(month_remainder_days)) + * SECONDS_PER_DAY, + ); if seconds_remainder.abs() >= SECONDS_PER_DAY { let remainder_days = (seconds_remainder / SECONDS_PER_DAY) @@ -2238,6 +2242,37 @@ mod tests { ); } + #[test] + fn test_interval_mul_div_f64_matches_duckdb_rounding() { + // Regression test for https://github.com/apache/arrow-rs/issues/12004: each of these + // results is off by 1 ns unless the sub-day remainder is summed in DuckDB's order. + let interval = IntervalMonthDayNanoArray::from(vec![ + IntervalMonthDayNanoType::make_value(1, 1, 0), + IntervalMonthDayNanoType::make_value(1, 1, 0), + IntervalMonthDayNanoType::make_value(1, 2, 0), + IntervalMonthDayNanoType::make_value(1, 1, 0), + ]); + let factor = Float64Array::from(vec![32. / 7., 10. / 13., 5. / 7., -32. / 7.]); + let expected = IntervalMonthDayNanoArray::from(vec![ + IntervalMonthDayNanoType::make_value(4, 21, 61_714_285_726_628), + IntervalMonthDayNanoType::make_value(0, 23, 73_107_692_314_339), + IntervalMonthDayNanoType::make_value(0, 22, 74_057_142_894_171), + IntervalMonthDayNanoType::make_value(-4, -21, -61_714_285_726_628), + ]); + assert_eq!(mul(&interval, &factor).unwrap().as_ref(), &expected); + + // Division multiplies by the reciprocal, so it follows the same order. + let interval = + IntervalMonthDayNanoArray::from(vec![IntervalMonthDayNanoType::make_value(1, 1, 0)]); + let factor = Float64Array::new_scalar(7. / 32.); + let expected = IntervalMonthDayNanoArray::from(vec![IntervalMonthDayNanoType::make_value( + 4, + 21, + 61_714_285_726_628, + )]); + assert_eq!(div(&interval, &factor).unwrap().as_ref(), &expected); + } + #[test] fn test_interval_mul_div_f64_errors() { let factor = Float64Array::new_scalar(2.);
