mbutrovich opened a new issue, #11602:
URL: https://github.com/apache/arrow-rs/issues/11602

   ### Describe the bug
   
   Casting a `Timestamp` to a coarser unit divides with `/` 
([mod.rs](https://github.com/apache/arrow-rs/blob/72887ed91c80002bb7ec755ce1e964bab07bebea/arrow-cast/src/cast/mod.rs#L1874-L1877)),
 which rounds a timestamp before 1970 toward the epoch. 
`1969-12-31T23:59:59.999` becomes `1970-01-01T00:00:00`, a later second. The 
cast of the same value to `Date32` floors and gives `1969-12-31`, as #4211 
fixed for dates.
   
   ### To Reproduce
   
   ```rust
   use arrow_array::cast::AsArray;
   use arrow_array::types::{Date32Type, TimestampSecondType};
   use arrow_array::TimestampMillisecondArray;
   use arrow_cast::cast;
   use arrow_schema::{DataType, TimeUnit};
   
   fn main() {
       // 1969-12-31T23:59:59.999
       let ts = TimestampMillisecondArray::from(vec![-1]);
       let secs = cast(&ts, &DataType::Timestamp(TimeUnit::Second, 
None)).unwrap();
       let date = cast(&ts, &DataType::Date32).unwrap();
       println!("seconds: {:?}", 
secs.as_primitive::<TimestampSecondType>().values()); // [0]: 
1970-01-01T00:00:00
       println!("date: {:?}", date.as_primitive::<Date32Type>().values()); // 
[-1]: 1969-12-31
   }
   ```
   
   ### Expected behavior
   
   `[-1]` (`1969-12-31T23:59:59`), the second that contains the instant, 
consistent with the cast to `Date32` and with `add` of an interval to a 
timestamp, which also floors to the unit.
   
   ### Additional context
   
   The cast docs say only that precision is lost going to a coarser unit 
([mod.rs](https://github.com/apache/arrow-rs/blob/72887ed91c80002bb7ec755ce1e964bab07bebea/arrow-cast/src/cast/mod.rs#L650-L652)).
 Casts to a coarser `Time32` or `Time64` unit are not affected because times 
are not negative.
   
   Found with property-based tests that build arrays in every physical layout 
the Arrow format allows.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to