liurenjie1024 commented on code in PR #42: URL: https://github.com/apache/iceberg-rust/pull/42#discussion_r1308471018
########## crates/iceberg/src/transform/temporal.rs: ########## @@ -0,0 +1,329 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use super::TransformFunction; +use crate::Result; +use arrow::array::{Array, TimestampMicrosecondArray}; +use arrow::compute::binary; +use arrow::datatypes; +use arrow::datatypes::DataType; +use arrow::{ + array::{ArrayRef, Date32Array, Int32Array}, + compute::{month_dyn, year_dyn}, +}; +use chrono::Datelike; +use std::sync::Arc; + +/// 719163 is the number of days from 0000-01-01 to 1970-01-01 +const EPOCH_DAY_FROM_CE: i32 = 719163; +const DAY_PER_SECOND: f64 = 0.0000115741; +const HOUR_PER_SECOND: f64 = 1_f64 / 3600.0; +const BASE_YEAR: i32 = 1970; + +/// Extract a date or timestamp year, as years from 1970 +pub struct Year; + +impl TransformFunction for Year { + fn transform(&self, input: ArrayRef) -> Result<ArrayRef> { + let array = year_dyn(&input).expect("Should not call transform in Year with non-date type"); + Ok(Arc::<Int32Array>::new( + array + .as_any() + .downcast_ref::<Int32Array>() + .unwrap() + .unary(|v| v - BASE_YEAR), + )) + } +} + +/// Extract a date or timestamp month, as months from 1970-01-01 +pub struct Month; + +impl TransformFunction for Month { + fn transform(&self, input: ArrayRef) -> Result<ArrayRef> { + let year_array = + year_dyn(&input).expect("Should not call transform in Month with non-date type"); Review Comment: As above. ########## crates/iceberg/src/transform/temporal.rs: ########## @@ -0,0 +1,329 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use super::TransformFunction; +use crate::Result; +use arrow::array::{Array, TimestampMicrosecondArray}; +use arrow::compute::binary; +use arrow::datatypes; +use arrow::datatypes::DataType; +use arrow::{ + array::{ArrayRef, Date32Array, Int32Array}, + compute::{month_dyn, year_dyn}, +}; +use chrono::Datelike; +use std::sync::Arc; + +/// 719163 is the number of days from 0000-01-01 to 1970-01-01 +const EPOCH_DAY_FROM_CE: i32 = 719163; +const DAY_PER_SECOND: f64 = 0.0000115741; +const HOUR_PER_SECOND: f64 = 1_f64 / 3600.0; +const BASE_YEAR: i32 = 1970; + +/// Extract a date or timestamp year, as years from 1970 +pub struct Year; + +impl TransformFunction for Year { + fn transform(&self, input: ArrayRef) -> Result<ArrayRef> { + let array = year_dyn(&input).expect("Should not call transform in Year with non-date type"); Review Comment: Hmm, should we really make it panic here? As a public interface, I don't think it's good practice to panic, it's better to return InvalidData error? cc @ZENOTME @Xuanwo ########## crates/iceberg/src/transform/temporal.rs: ########## @@ -0,0 +1,329 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use super::TransformFunction; +use crate::Result; +use arrow::array::{Array, TimestampMicrosecondArray}; +use arrow::compute::binary; +use arrow::datatypes; +use arrow::datatypes::DataType; +use arrow::{ + array::{ArrayRef, Date32Array, Int32Array}, + compute::{month_dyn, year_dyn}, +}; +use chrono::Datelike; +use std::sync::Arc; + +/// 719163 is the number of days from 0000-01-01 to 1970-01-01 +const EPOCH_DAY_FROM_CE: i32 = 719163; +const DAY_PER_SECOND: f64 = 0.0000115741; +const HOUR_PER_SECOND: f64 = 1_f64 / 3600.0; +const BASE_YEAR: i32 = 1970; Review Comment: ```suggestion const EPOCH_YEAR: i32 = 1970; ``` ########## crates/iceberg/src/transform/temporal.rs: ########## @@ -0,0 +1,329 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use super::TransformFunction; +use crate::Result; +use arrow::array::{Array, TimestampMicrosecondArray}; +use arrow::compute::binary; +use arrow::datatypes; +use arrow::datatypes::DataType; +use arrow::{ + array::{ArrayRef, Date32Array, Int32Array}, + compute::{month_dyn, year_dyn}, +}; +use chrono::Datelike; +use std::sync::Arc; + +/// 719163 is the number of days from 0000-01-01 to 1970-01-01 +const EPOCH_DAY_FROM_CE: i32 = 719163; +const DAY_PER_SECOND: f64 = 0.0000115741; +const HOUR_PER_SECOND: f64 = 1_f64 / 3600.0; +const BASE_YEAR: i32 = 1970; + +/// Extract a date or timestamp year, as years from 1970 +pub struct Year; + +impl TransformFunction for Year { + fn transform(&self, input: ArrayRef) -> Result<ArrayRef> { + let array = year_dyn(&input).expect("Should not call transform in Year with non-date type"); + Ok(Arc::<Int32Array>::new( + array + .as_any() + .downcast_ref::<Int32Array>() + .unwrap() + .unary(|v| v - BASE_YEAR), + )) + } +} + +/// Extract a date or timestamp month, as months from 1970-01-01 +pub struct Month; + +impl TransformFunction for Month { + fn transform(&self, input: ArrayRef) -> Result<ArrayRef> { + let year_array = + year_dyn(&input).expect("Should not call transform in Month with non-date type"); + let year_array: Int32Array = year_array + .as_any() + .downcast_ref::<Int32Array>() + .unwrap() + .unary(|v| 12 * (v - BASE_YEAR)); + let month_array = + month_dyn(&input).expect("Should not call transform in Month with non-date type"); + Ok(Arc::<Int32Array>::new( + binary( + month_array.as_any().downcast_ref::<Int32Array>().unwrap(), + year_array.as_any().downcast_ref::<Int32Array>().unwrap(), + // Compute month from 1970-01-01, so minus 1 here. + |a, b| a + b - 1, + ) + .unwrap(), + )) + } +} + +/// Extract a date or timestamp day, as days from 1970-01-01 +pub struct Day; + +impl TransformFunction for Day { + fn transform(&self, input: ArrayRef) -> Result<ArrayRef> { + let res: Int32Array = match input.data_type() { + DataType::Timestamp(datatypes::TimeUnit::Microsecond, _) => input + .as_any() + .downcast_ref::<TimestampMicrosecondArray>() + .unwrap() + .unary(|v| -> i32 { (v as f64 / 1000.0 / 1000.0 * DAY_PER_SECOND) as i32 }), + DataType::Date32 => { + input + .as_any() + .downcast_ref::<Date32Array>() + .unwrap() + .unary(|v| -> i32 { + datatypes::Date32Type::to_naive_date(v).num_days_from_ce() + - EPOCH_DAY_FROM_CE + }) + } + _ => unreachable!( + "Should not call transform in Day with type {:?}", + input.data_type() + ), + }; + Ok(Arc::new(res)) + } +} + +/// Extract a timestamp hour, as hours from 1970-01-01 00:00:00 +pub struct Hour; + +impl TransformFunction for Hour { + fn transform(&self, input: ArrayRef) -> Result<ArrayRef> { + let res: Int32Array = match input.data_type() { + DataType::Timestamp(datatypes::TimeUnit::Microsecond, _) => input + .as_any() + .downcast_ref::<TimestampMicrosecondArray>() + .unwrap() + .unary(|v| -> i32 { (v as f64 * HOUR_PER_SECOND / 1000.0 / 1000.0) as i32 }), + _ => unreachable!( Review Comment: As above. -- 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