Skip to content

Commit 16f4a7f

Browse files
authored
fix: #5599 (#5603)
1 parent 1852c33 commit 16f4a7f

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

arrow-array/src/array/primitive_array.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ impl<T: ArrowPrimitiveType> std::fmt::Debug for PrimitiveArray<T> {
10971097
write!(f, "PrimitiveArray<{data_type:?}>\n[\n")?;
10981098
print_long_array(self, f, |array, index, f| match data_type {
10991099
DataType::Date32 | DataType::Date64 => {
1100-
let v = self.value(index).to_isize().unwrap() as i64;
1100+
let v = self.value(index).to_i64().unwrap();
11011101
match as_date::<T>(v) {
11021102
Some(date) => write!(f, "{date:?}"),
11031103
None => {
@@ -1109,7 +1109,7 @@ impl<T: ArrowPrimitiveType> std::fmt::Debug for PrimitiveArray<T> {
11091109
}
11101110
}
11111111
DataType::Time32(_) | DataType::Time64(_) => {
1112-
let v = self.value(index).to_isize().unwrap() as i64;
1112+
let v = self.value(index).to_i64().unwrap();
11131113
match as_time::<T>(v) {
11141114
Some(time) => write!(f, "{time:?}"),
11151115
None => {
@@ -1121,7 +1121,7 @@ impl<T: ArrowPrimitiveType> std::fmt::Debug for PrimitiveArray<T> {
11211121
}
11221122
}
11231123
DataType::Timestamp(_, tz_string_opt) => {
1124-
let v = self.value(index).to_isize().unwrap() as i64;
1124+
let v = self.value(index).to_i64().unwrap();
11251125
match tz_string_opt {
11261126
// for Timestamp with TimeZone
11271127
Some(tz_string) => {

arrow-buffer/src/native.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ pub trait ArrowNativeType:
7575
/// in truncation/overflow
7676
fn to_isize(self) -> Option<isize>;
7777

78+
/// Convert native type to i64.
79+
///
80+
/// Returns `None` if [`Self`] is not an integer or conversion would result
81+
/// in truncation/overflow
82+
fn to_i64(self) -> Option<i64>;
83+
7884
/// Convert native type from i32.
7985
///
8086
/// Returns `None` if [`Self`] is not `i32`
@@ -119,6 +125,11 @@ macro_rules! native_integer {
119125
self.try_into().ok()
120126
}
121127

128+
#[inline]
129+
fn to_i64(self) -> Option<i64> {
130+
self.try_into().ok()
131+
}
132+
122133
#[inline]
123134
fn as_usize(self) -> usize {
124135
self as _
@@ -170,6 +181,11 @@ macro_rules! native_float {
170181
None
171182
}
172183

184+
#[inline]
185+
fn to_i64(self) -> Option<i64> {
186+
None
187+
}
188+
173189
#[inline]
174190
fn as_usize($s) -> usize {
175191
$as_usize
@@ -212,6 +228,10 @@ impl ArrowNativeType for i256 {
212228
fn to_isize(self) -> Option<isize> {
213229
self.to_i128()?.try_into().ok()
214230
}
231+
232+
fn to_i64(self) -> Option<i64> {
233+
self.to_i128()?.try_into().ok()
234+
}
215235
}
216236

217237
/// Allows conversion from supported Arrow types to a byte slice.

0 commit comments

Comments
 (0)