liurenjie1024 commented on code in PR #287: URL: https://github.com/apache/iceberg-rust/pull/287#discussion_r1538988833
########## crates/iceberg/src/transform/temporal.rs: ########## @@ -39,6 +38,13 @@ const UNIX_EPOCH_YEAR: i32 = 1970; #[derive(Debug)] pub struct Year; +impl Year { + #[inline] + fn timestamp_to_year(timestamp: i64) -> i32 { + DateTime::from_timestamp_micros(timestamp).unwrap().year() - UNIX_EPOCH_YEAR Review Comment: We should not panic here. This is user input, and we should throw error. ########## crates/iceberg/src/transform/temporal.rs: ########## @@ -51,12 +57,48 @@ impl TransformFunction for Year { .unary(|v| v - UNIX_EPOCH_YEAR), )) } + + fn transform_literal(&self, input: &crate::spec::Datum) -> Result<Option<crate::spec::Datum>> { + let val = match input.literal() { + PrimitiveLiteral::Date(v) => Date32Type::to_naive_date(*v).year() - UNIX_EPOCH_YEAR, + PrimitiveLiteral::Timestamp(v) => Self::timestamp_to_year(*v), + PrimitiveLiteral::TimestampTZ(v) => Self::timestamp_to_year(*v), + _ => { + return Err(crate::Error::new( + crate::ErrorKind::FeatureUnsupported, + format!( + "Unsupported data type for year transform: {:?}", + input.data_type() + ), + )) + } + }; + Ok(Some(Datum::int(val))) + } } /// Extract a date or timestamp month, as months from 1970-01-01 #[derive(Debug)] pub struct Month; +impl Month { + #[inline] + fn timestamp_to_month(timestamp: i64) -> i32 { + // date: aaaa-aa-aa + // unix epoch date: 1970-01-01 + // if date > unix epoch date, delta month = (aa - 1) + 12 * (aaaa-1970) + // if date < unix epoch date, delta month = (12 - (aa - 1)) + 12 * (1970-aaaa-1) + let date = DateTime::from_timestamp_micros(timestamp).unwrap(); Review Comment: Ditto, timestamp maybe user input and we should not panic. ########## crates/iceberg/src/transform/temporal.rs: ########## @@ -51,12 +57,48 @@ impl TransformFunction for Year { .unary(|v| v - UNIX_EPOCH_YEAR), )) } + + fn transform_literal(&self, input: &crate::spec::Datum) -> Result<Option<crate::spec::Datum>> { + let val = match input.literal() { + PrimitiveLiteral::Date(v) => Date32Type::to_naive_date(*v).year() - UNIX_EPOCH_YEAR, + PrimitiveLiteral::Timestamp(v) => Self::timestamp_to_year(*v), + PrimitiveLiteral::TimestampTZ(v) => Self::timestamp_to_year(*v), + _ => { + return Err(crate::Error::new( + crate::ErrorKind::FeatureUnsupported, + format!( + "Unsupported data type for year transform: {:?}", + input.data_type() + ), + )) + } + }; + Ok(Some(Datum::int(val))) + } } /// Extract a date or timestamp month, as months from 1970-01-01 #[derive(Debug)] pub struct Month; +impl Month { + #[inline] + fn timestamp_to_month(timestamp: i64) -> i32 { + // date: aaaa-aa-aa + // unix epoch date: 1970-01-01 + // if date > unix epoch date, delta month = (aa - 1) + 12 * (aaaa-1970) + // if date < unix epoch date, delta month = (12 - (aa - 1)) + 12 * (1970-aaaa-1) + let date = DateTime::from_timestamp_micros(timestamp).unwrap(); + let unix_epoch_date = DateTime::from_timestamp_micros(0).unwrap(); Review Comment: Ditto. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org