Skip to content

Commit 02b2ed3

Browse files
committed
fix int96 conversion to read timestamps correctly
1 parent 2aeea24 commit 02b2ed3

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

rust/datafusion/src/datasource/parquet.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,14 @@ impl ParquetFile {
340340

341341
/// convert a parquet timestamp in nanoseconds to a timestamp with milliseconds
342342
fn convert_int96_timestamp(v: &[u32]) -> i64 {
343-
let value: u128 = (v[0] as u128) << 64 | (v[1] as u128) << 32 | (v[2] as u128);
344-
(value / 1000000) as i64
343+
const JULIAN_DAY_OF_EPOCH: i64 = 2_440_588;
344+
const SECONDS_PER_DAY: i64 = 86_400;
345+
const MILLIS_PER_SECOND: i64 = 1_000;
346+
347+
let day = v[2] as i64;
348+
let nanoseconds = ((v[1] as i64) << 32) + v[0] as i64;
349+
let seconds = (day - JULIAN_DAY_OF_EPOCH) * SECONDS_PER_DAY;
350+
seconds * MILLIS_PER_SECOND + nanoseconds / 1_000_000
345351
}
346352

347353
impl RecordBatchIterator for ParquetFile {
@@ -469,7 +475,7 @@ mod tests {
469475
values.push(array.value(i));
470476
}
471477

472-
assert_eq!("[2, 7842670136425819125, 2, 7842670136425819125, 2, 7842670136425819125, 2, 7842670136425819125]", format!("{:?}", values));
478+
assert_eq!("[1235865600000, 1235865660000, 1238544000000, 1238544060000, 1233446400000, 1233446460000, 1230768000000, 1230768060000]", format!("{:?}", values));
473479
}
474480

475481
#[test]

0 commit comments

Comments
 (0)