Skip to content

Commit 990694e

Browse files
committed
clean code
1 parent 6005169 commit 990694e

1 file changed

Lines changed: 30 additions & 20 deletions

File tree

crates/iceberg/src/transform/temporal.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// under the License.
1717

1818
use super::TransformFunction;
19-
use crate::Result;
19+
use crate::{Error, ErrorKind, Result};
2020
use arrow::array::{Array, TimestampMicrosecondArray};
2121
use arrow::compute::binary;
2222
use arrow::datatypes;
@@ -28,24 +28,24 @@ use arrow::{
2828
use chrono::Datelike;
2929
use std::sync::Arc;
3030

31-
/// 719163 is the number of days from 0000-01-01 to 1970-01-01
32-
const EPOCH_DAY_FROM_CE: i32 = 719163;
33-
const DAY_PER_SECOND: f64 = 0.0000115741;
34-
const HOUR_PER_SECOND: f64 = 1_f64 / 3600.0;
35-
const BASE_YEAR: i32 = 1970;
31+
const DAY_FROM_UNIX_EPOCH: i32 = 719163;
32+
const HOUR_PER_SECOND: f64 = 1.0 / 3600.0;
33+
const DAY_PER_SECOND: f64 = 1.0 / 12.0 / HOUR_PER_SECOND;
34+
const UNIX_EPOCH_YEAR: i32 = 1970;
3635

3736
/// Extract a date or timestamp year, as years from 1970
3837
pub struct Year;
3938

4039
impl TransformFunction for Year {
4140
fn transform(&self, input: ArrayRef) -> Result<ArrayRef> {
42-
let array = year_dyn(&input).expect("Should not call transform in Year with non-date type");
41+
let array =
42+
year_dyn(&input).map_err(|err| Error::new(ErrorKind::Unexpected, format!("{err}")))?;
4343
Ok(Arc::<Int32Array>::new(
4444
array
4545
.as_any()
4646
.downcast_ref::<Int32Array>()
4747
.unwrap()
48-
.unary(|v| v - BASE_YEAR),
48+
.unary(|v| v - UNIX_EPOCH_YEAR),
4949
))
5050
}
5151
}
@@ -56,14 +56,14 @@ pub struct Month;
5656
impl TransformFunction for Month {
5757
fn transform(&self, input: ArrayRef) -> Result<ArrayRef> {
5858
let year_array =
59-
year_dyn(&input).expect("Should not call transform in Month with non-date type");
59+
year_dyn(&input).map_err(|err| Error::new(ErrorKind::Unexpected, format!("{err}")))?;
6060
let year_array: Int32Array = year_array
6161
.as_any()
6262
.downcast_ref::<Int32Array>()
6363
.unwrap()
64-
.unary(|v| 12 * (v - BASE_YEAR));
64+
.unary(|v| 12 * (v - UNIX_EPOCH_YEAR));
6565
let month_array =
66-
month_dyn(&input).expect("Should not call transform in Month with non-date type");
66+
month_dyn(&input).map_err(|err| Error::new(ErrorKind::Unexpected, format!("{err}")))?;
6767
Ok(Arc::<Int32Array>::new(
6868
binary(
6969
month_array.as_any().downcast_ref::<Int32Array>().unwrap(),
@@ -94,13 +94,18 @@ impl TransformFunction for Day {
9494
.unwrap()
9595
.unary(|v| -> i32 {
9696
datatypes::Date32Type::to_naive_date(v).num_days_from_ce()
97-
- EPOCH_DAY_FROM_CE
97+
- DAY_FROM_UNIX_EPOCH
9898
})
9999
}
100-
_ => unreachable!(
101-
"Should not call transform in Day with type {:?}",
102-
input.data_type()
103-
),
100+
_ => {
101+
return Err(Error::new(
102+
ErrorKind::Unexpected,
103+
format!(
104+
"Should not call internally for unsupport data type {:?}",
105+
input.data_type()
106+
),
107+
))
108+
}
104109
};
105110
Ok(Arc::new(res))
106111
}
@@ -117,10 +122,15 @@ impl TransformFunction for Hour {
117122
.downcast_ref::<TimestampMicrosecondArray>()
118123
.unwrap()
119124
.unary(|v| -> i32 { (v as f64 * HOUR_PER_SECOND / 1000.0 / 1000.0) as i32 }),
120-
_ => unreachable!(
121-
"Should not call transform in Day with type {:?}",
122-
input.data_type()
123-
),
125+
_ => {
126+
return Err(Error::new(
127+
ErrorKind::Unexpected,
128+
format!(
129+
"Should not call internally for unsupport data type {:?}",
130+
input.data_type()
131+
),
132+
))
133+
}
124134
};
125135
Ok(Arc::new(res))
126136
}

0 commit comments

Comments
 (0)