-
Notifications
You must be signed in to change notification settings - Fork 16
feat: support datafusion iceberg transform physical expr #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xxhZs
wants to merge
2
commits into
main
Choose a base branch
from
xxh/support-porject-for-iceberg-transfrom
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
227 changes: 227 additions & 0 deletions
227
core/src/executor/datafusion/datafusion_impl/iceberg_physical_expr.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| /* | ||
| * Copyright 2025 BergLoom | ||
| * | ||
| * Licensed 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 core::{any::Any, hash::Hash}; | ||
| use std::sync::Arc; | ||
|
|
||
| use datafusion::{ | ||
| arrow::{ | ||
| array::RecordBatch, | ||
| datatypes::{DataType, Schema}, | ||
| }, | ||
| common::internal_err, | ||
| logical_expr::ColumnarValue, | ||
| physical_plan::PhysicalExpr, | ||
| }; | ||
| use iceberg::{ | ||
| spec::Transform, | ||
| transform::{BoxedTransformFunction, create_transform_function}, | ||
| }; | ||
| use iceberg_datafusion::to_datafusion_error; | ||
| use std::fmt::Debug; | ||
|
|
||
| pub struct IcebergTransformPhysicalExpr { | ||
| name: String, | ||
| index: usize, | ||
| transform: BoxedTransformFunction, | ||
| } | ||
|
|
||
| impl Debug for IcebergTransformPhysicalExpr { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| f.debug_struct("IcebergTransformPhysicalExpr") | ||
| .field("name", &self.name) | ||
| .field("index", &self.index) | ||
| .finish() | ||
| } | ||
| } | ||
|
|
||
| impl std::fmt::Display for IcebergTransformPhysicalExpr { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
| f.debug_struct("IcebergPhysicalExpr") | ||
| .field("name", &self.name) | ||
| .field("index", &self.index) | ||
| .finish() | ||
| } | ||
| } | ||
|
|
||
| impl Hash for IcebergTransformPhysicalExpr { | ||
| fn hash<H: std::hash::Hasher>(&self, state: &mut H) { | ||
| self.name.hash(state); | ||
| self.index.hash(state); | ||
| } | ||
| } | ||
|
|
||
| impl PartialEq for IcebergTransformPhysicalExpr { | ||
| fn eq(&self, other: &Self) -> bool { | ||
| self.name == other.name && self.index == other.index | ||
| } | ||
| } | ||
|
|
||
| impl Eq for IcebergTransformPhysicalExpr {} | ||
|
|
||
| impl PhysicalExpr for IcebergTransformPhysicalExpr { | ||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
|
|
||
| fn data_type(&self, input_schema: &Schema) -> datafusion::error::Result<DataType> { | ||
| self.bounds_check(input_schema)?; | ||
| Ok(input_schema.field(self.index).data_type().clone()) | ||
| } | ||
|
|
||
| fn nullable(&self, input_schema: &Schema) -> datafusion::error::Result<bool> { | ||
| self.bounds_check(input_schema)?; | ||
| Ok(input_schema.field(self.index).is_nullable()) | ||
| } | ||
|
|
||
| fn evaluate(&self, batch: &RecordBatch) -> datafusion::error::Result<ColumnarValue> { | ||
| self.bounds_check(batch.schema().as_ref())?; | ||
| let res_array = self | ||
| .transform | ||
| .transform(batch.column(self.index).clone()) | ||
| .unwrap(); | ||
| Ok(ColumnarValue::Array(res_array.clone())) | ||
| } | ||
|
|
||
| fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> { | ||
| vec![] | ||
| } | ||
|
|
||
| fn with_new_children( | ||
| self: Arc<Self>, | ||
| _children: Vec<Arc<dyn PhysicalExpr>>, | ||
| ) -> datafusion::error::Result<Arc<dyn PhysicalExpr>> { | ||
| Ok(self) | ||
| } | ||
| } | ||
|
|
||
| impl IcebergTransformPhysicalExpr { | ||
| fn bounds_check(&self, input_schema: &Schema) -> datafusion::error::Result<()> { | ||
| if self.index < input_schema.fields.len() { | ||
| Ok(()) | ||
| } else { | ||
| internal_err!( | ||
| "PhysicalExpr Column references column '{}' at index {} (zero-based) but input schema only has {} columns: {:?}", | ||
| self.name, | ||
| self.index, | ||
| input_schema.fields.len(), | ||
| input_schema | ||
| .fields() | ||
| .iter() | ||
| .map(|f| f.name()) | ||
| .collect::<Vec<_>>() | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| pub fn new( | ||
| name: String, | ||
| index: usize, | ||
| transform: Transform, | ||
| ) -> datafusion::error::Result<Self> { | ||
| let transform = | ||
| create_transform_function(&transform).map_err(|err| to_datafusion_error(err))?; | ||
| Ok(Self { | ||
| name, | ||
| index, | ||
| transform, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use datafusion::arrow::{ | ||
| array::{Int32Array, RecordBatch}, | ||
| datatypes::{DataType, Field, Schema}, | ||
| }; | ||
| use iceberg::spec::Transform; | ||
| use std::sync::Arc; | ||
|
|
||
| #[test] | ||
| fn test_evaluate_identity() { | ||
| let schema = Schema::new(vec![Field::new("v1", DataType::Int32, false)]); | ||
| let v1 = Int32Array::from(vec![1, 2, 3]); | ||
| let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(v1.clone())]).unwrap(); | ||
| let expr = IcebergTransformPhysicalExpr::new("v1".to_string(), 0, Transform::Identity).unwrap(); | ||
| let result = expr.evaluate(&batch).unwrap(); | ||
| match result { | ||
| datafusion::logical_expr::ColumnarValue::Array(array) => { | ||
| let arr = array.as_any().downcast_ref::<Int32Array>().unwrap(); | ||
| assert_eq!(arr.values(), v1.values()); | ||
| } | ||
| _ => panic!("Expected array result"), | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_evaluate_bucket() { | ||
| let schema = Schema::new(vec![Field::new("v1", DataType::Int32, false)]); | ||
| let v1 = Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
| let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(v1.clone())]).unwrap(); | ||
| let expr = IcebergTransformPhysicalExpr::new("v1".to_string(), 0, Transform::Bucket(4)).unwrap(); | ||
| let result = expr.evaluate(&batch).unwrap(); | ||
| match result { | ||
| datafusion::logical_expr::ColumnarValue::Array(array) => { | ||
| let arr = array.as_any().downcast_ref::<Int32Array>().unwrap(); | ||
| assert_eq!( | ||
| arr.values(), | ||
| Int32Array::from(vec![0, 0, 3, 2, 3, 1, 3, 3, 3]).values() | ||
| ); | ||
| } | ||
| _ => panic!("Expected array result"), | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_evaluate_truncate() { | ||
| let schema = Schema::new(vec![Field::new("v1", DataType::Int32, false)]); | ||
| let v1 = Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
| let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(v1.clone())]).unwrap(); | ||
| let expr = IcebergTransformPhysicalExpr::new("v1".to_string(), 0, Transform::Truncate(3)).unwrap(); | ||
| let result = expr.evaluate(&batch).unwrap(); | ||
| match result { | ||
| datafusion::logical_expr::ColumnarValue::Array(array) => { | ||
| let arr = array.as_any().downcast_ref::<Int32Array>().unwrap(); | ||
| assert_eq!( | ||
| arr.values(), | ||
| Int32Array::from(vec![0, 0, 3, 3, 3, 6, 6, 6, 9]).values() | ||
| ); | ||
| } | ||
| _ => panic!("Expected array result"), | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_evaluate_void() { | ||
| let schema = Schema::new(vec![Field::new("v1", DataType::Int32, false)]); | ||
| let v1 = Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
| let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(v1.clone())]).unwrap(); | ||
| let expr = IcebergTransformPhysicalExpr::new("v1".to_string(), 0, Transform::Void).unwrap(); | ||
| let result = expr.evaluate(&batch).unwrap(); | ||
| match result { | ||
| datafusion::logical_expr::ColumnarValue::Array(array) => { | ||
| let arr = array.as_any().downcast_ref::<Int32Array>().unwrap(); | ||
| assert_eq!( | ||
| arr.values(), | ||
| Int32Array::from(vec![0, 0, 0, 0, 0, 0, 0, 0, 0]).values() | ||
| ); | ||
| } | ||
| _ => panic!("Expected array result"), | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /* | ||
| * Copyright 2025 BergLoom | ||
| * | ||
| * Licensed 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. | ||
| */ | ||
|
|
||
| pub mod file_scan_task_table_provider; | ||
| pub mod iceberg_file_task_scan; | ||
| pub mod iceberg_physical_expr; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling
.unwrap()on the transform result may cause a panic at runtime. Consider propagating errors (e.g., using?and mapping to a DataFusion error) instead of unwrapping.