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-12258-44a4bdb287dfcc7297c5fb4c98d8c6d8c43f6e70 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 9951b625879a19844ecd0b2dd352347a87ea26dd Author: dayan02dev <[email protected]> AuthorDate: Thu Nov 19 09:43:07 2026 +0000 fix(arrow-cast): floor Date64 to Date32 casts before the epoch (#12258) # Which issue does this PR close? - Closes #11853 # Rationale for this change The `(Date64, Date32)` cast computes `x / MILLISECONDS_IN_DAY`. Integer division truncates toward zero, so a Date64 value before 1970 that is not an exact midnight lands on the following day. For example, `-1` (displayed as `1969-12-31T23:59:59.999`) casts to `1970-01-01`. This happens in both safe and `safe: false` mode. The `Date64` docs say values need not be whole days and are treated like `Timestamp(Millisecond, None)`. arrow-rs produces such values itself: `cast("1969-12-31T12:00:00", Date64)` keeps the time of day. `Timestamp -> Date32` has rounded down since #4211, and `Timestamp -> Date64` since #11632. This cast was the remaining one that truncated. # What changes are included in this PR? The cast uses `x.div_euclid(MILLISECONDS_IN_DAY)` in both the safe and `safe: false` branches, as `timestamp_to_date32` does. In a local micro-benchmark this adds about 0.15 ns per value, the same sign fix-up the Timestamp path already pays. # Are these changes tested? Yes. `test_cast_date64_to_date32` now also casts `-1`, `-86400000` (an exact midnight, unchanged) and `-86400001`, and checks that `safe: false` gives the same array. Without the fix it fails with `left: -1, right: 0`. Reverting only the `safe: false` branch fails the second assertion. # Are there any significant user-facing changes? - Pre-1970 Date64 values that are not exact midnights now cast to the day that contains them, matching `Timestamp(Millisecond, None) -> Date32`. - At the bottom of the Date32 range, `i32::MIN * 86400000 - 1` used to cast to `i32::MIN`. That is one day off, because its day is `i32::MIN - 1`, which Date32 cannot represent. It is now out of range: null when safe, a `CastError` with `safe: false`. The Timestamp cast already behaves this way. The parquet writer's `coerce_types` path (`parquet/src/arrow/arrow_writer/mod.rs`) converts Date64 to INT32 with its own truncating division. This PR leaves it alone, so for pre-epoch non-midnight values it no longer agrees with `cast`. That could be a separate issue, since changing it changes what gets written to files. # AI usage disclosure The issue analysis, fix, test, and the benchmark numbers above were produced with an AI coding agent (Claude Code). A separate agent session re-checked them before submission. --- arrow-cast/src/cast/mod.rs | 6 ++++-- arrow-cast/tests/cast/date_time.rs | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/arrow-cast/src/cast/mod.rs b/arrow-cast/src/cast/mod.rs index 540df4aeff..504330c9ed 100644 --- a/arrow-cast/src/cast/mod.rs +++ b/arrow-cast/src/cast/mod.rs @@ -1632,10 +1632,12 @@ pub fn cast_with_options( (Date64, Date32) => { let array = array.as_primitive::<Date64Type>(); let result = if cast_options.safe { - array.unary_opt::<_, Date32Type>(|x| i32::try_from(x / MILLISECONDS_IN_DAY).ok()) + array.unary_opt::<_, Date32Type>(|x| { + i32::try_from(x.div_euclid(MILLISECONDS_IN_DAY)).ok() + }) } else { array.try_unary::<_, Date32Type, _>(|x| { - i32::try_from(x / MILLISECONDS_IN_DAY).map_err(|_| { + i32::try_from(x.div_euclid(MILLISECONDS_IN_DAY)).map_err(|_| { ArrowError::CastError(format!( "Cannot cast Date64 value {x} to Date32 without overflow" )) diff --git a/arrow-cast/tests/cast/date_time.rs b/arrow-cast/tests/cast/date_time.rs index 0b117d7f66..ec00ae6685 100644 --- a/arrow-cast/tests/cast/date_time.rs +++ b/arrow-cast/tests/cast/date_time.rs @@ -74,13 +74,30 @@ fn test_cast_date32_to_date64() { #[test] fn test_cast_date64_to_date32() { - let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]); + let a = Date64Array::from(vec![ + Some(864000000005), + Some(1545696000001), + None, + Some(-1), + Some(-86400000), + Some(-86400001), + ]); let array = Arc::new(a) as ArrayRef; let b = cast(&array, &DataType::Date32).unwrap(); let c = b.as_primitive::<Date32Type>(); assert_eq!(10000, c.value(0)); assert_eq!(17890, c.value(1)); assert!(c.is_null(2)); + assert_eq!(-1, c.value(3)); + assert_eq!(-1, c.value(4)); + assert_eq!(-2, c.value(5)); + + let options = CastOptions { + safe: false, + ..Default::default() + }; + let d = cast_with_options(&array, &DataType::Date32, &options).unwrap(); + assert_eq!(c, d.as_primitive::<Date32Type>()); } #[test]
