fvaleye commented on code in PR #1602: URL: https://github.com/apache/iceberg-rust/pull/1602#discussion_r2291114395
########## crates/integrations/datafusion/src/physical_plan/project.rs: ########## @@ -0,0 +1,661 @@ +// 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 std::any::Any; +use std::fmt::{Debug, Formatter}; +use std::sync::Arc; + +use datafusion::arrow::array::{ArrayRef, RecordBatch}; +use datafusion::arrow::datatypes::{Field, Schema as ArrowSchema, SchemaRef as ArrowSchemaRef}; +use datafusion::common::Result as DFResult; +use datafusion::error::DataFusionError; +use datafusion::execution::{SendableRecordBatchStream, TaskContext}; +use datafusion::physical_expr::EquivalenceProperties; +use datafusion::physical_plan::execution_plan::{Boundedness, EmissionType}; +use datafusion::physical_plan::stream::RecordBatchStreamAdapter; +use datafusion::physical_plan::{ + DisplayAs, DisplayFormatType, ExecutionPlan, ExecutionPlanProperties, PlanProperties, + execute_input_stream, +}; +use futures::StreamExt; +use iceberg::spec::{PartitionSpec, Schema}; + +use crate::to_datafusion_error; + +/// Prefix for partition column names to avoid collisions with regular columns +const PARTITION_COLUMN_PREFIX: &str = "__partition_"; + +/// An execution plan node that calculates partition values for Iceberg tables. +/// +/// This execution plan takes input data from a child execution plan and adds partition columns +/// based on the table's partition specification. The partition values are computed by applying +/// the appropriate transforms to the source columns. +/// +/// The output schema includes all original columns plus additional partition columns. +#[derive(Debug, Clone)] +pub(crate) struct IcebergProjectExec { + input: Arc<dyn ExecutionPlan>, + partition_spec: Arc<PartitionSpec>, + table_schema: Arc<Schema>, + output_schema: ArrowSchemaRef, + plan_properties: PlanProperties, +} + +/// IcebergProjectExec is responsible for calculating partition values for Iceberg tables. +/// It takes input data from a child execution plan and adds partition columns based on the table's +/// partition specification. The partition values are computed by applying the appropriate transforms +/// to the source columns. The output schema includes all original columns plus additional partition +/// columns. +impl IcebergProjectExec { + pub fn new( + input: Arc<dyn ExecutionPlan>, + partition_spec: Arc<PartitionSpec>, + table_schema: Arc<Schema>, + ) -> DFResult<Self> { + let output_schema = + Self::create_output_schema(&input.schema(), &partition_spec, &table_schema)?; + let plan_properties = Self::compute_properties(&input, output_schema.clone()); + + Ok(Self { + input, + partition_spec, + table_schema, + output_schema, + plan_properties, + }) + } + + /// Compute the plan properties for this execution plan + fn compute_properties( + input: &Arc<dyn ExecutionPlan>, + schema: ArrowSchemaRef, + ) -> PlanProperties { + PlanProperties::new( + EquivalenceProperties::new(schema), + input.output_partitioning().clone(), + EmissionType::Incremental, + Boundedness::Bounded, + ) + } + + /// Create the output schema by adding partition columns to the input schema + fn create_output_schema( + input_schema: &ArrowSchema, + partition_spec: &PartitionSpec, + table_schema: &Schema, + ) -> DFResult<ArrowSchemaRef> { + if partition_spec.is_unpartitioned() { + return Ok(Arc::new(input_schema.clone())); + } + + let mut fields: Vec<Arc<Field>> = input_schema.fields().to_vec(); + + let partition_struct = partition_spec + .partition_type(table_schema) + .map_err(to_datafusion_error)?; + + for (idx, pf) in partition_spec.fields().iter().enumerate() { + let struct_field = partition_struct.fields().get(idx).ok_or_else(|| { + DataFusionError::Internal( + "Partition field index out of bounds when creating output schema".to_string(), + ) + })?; + let arrow_type = iceberg::arrow::type_to_arrow_type(&struct_field.field_type) + .map_err(to_datafusion_error)?; + let partition_column_name = Self::create_partition_column_name(&pf.name); + let nullable = !struct_field.required; + fields.push(Arc::new(Field::new( + &partition_column_name, + arrow_type, + nullable, + ))); + } + Ok(Arc::new(ArrowSchema::new(fields))) + } + + /// Calculate partition values for a record batch + fn calculate_partition_values(&self, batch: &RecordBatch) -> DFResult<Vec<ArrayRef>> { + if self.partition_spec.is_unpartitioned() { + return Ok(vec![]); + } + + let batch_schema = batch.schema(); + let mut partition_values = Vec::with_capacity(self.partition_spec.fields().len()); + + for pf in self.partition_spec.fields() { + // Find the source field in the table schema + let source_field = self.table_schema.field_by_id(pf.source_id).ok_or_else(|| { + DataFusionError::Internal(format!( + "Source field not found with id {} when calculating partition values", + pf.source_id + )) + })?; + + let field_path = Self::find_field_path(&self.table_schema, source_field.id)?; + let index_path = Self::resolve_arrow_index_path(batch_schema.as_ref(), &field_path)?; + + let source_column = Self::extract_column_by_index_path(batch, &index_path)?; + + let transform_fn = iceberg::transform::create_transform_function(&pf.transform) + .map_err(to_datafusion_error)?; + let partition_value = transform_fn + .transform(source_column) + .map_err(to_datafusion_error)?; + + partition_values.push(partition_value); + } + Ok(partition_values) + } + + /// Extract a column by an index path + fn extract_column_by_index_path( + batch: &RecordBatch, + index_path: &[usize], + ) -> DFResult<ArrayRef> { + if index_path.is_empty() { + return Err(DataFusionError::Internal( + "Empty index path when extracting partition column".to_string(), + )); + } + + let mut current_column = batch.column(*index_path.first().unwrap()).clone(); + for child_index in &index_path[1..] { + // We only support traversing nested Structs. Provide explicit errors for unsupported + // nested container types to fail early and clearly. + let dt = current_column.data_type(); + match dt { + datafusion::arrow::datatypes::DataType::Struct(_) => { + let struct_array = current_column + .as_any() + .downcast_ref::<datafusion::arrow::array::StructArray>() + .ok_or_else(|| { + DataFusionError::Internal(format!( + "Failed to downcast to StructArray while traversing index path {:?} for partition column extraction", + index_path + )) + })?; + current_column = struct_array.column(*child_index).clone(); + } + datafusion::arrow::datatypes::DataType::List(_) + | datafusion::arrow::datatypes::DataType::LargeList(_) + | datafusion::arrow::datatypes::DataType::FixedSizeList(_, _) + | datafusion::arrow::datatypes::DataType::Map(_, _) => { + return Err(DataFusionError::NotImplemented(format!( + "Partitioning on nested list/map types is not supported (encountered {:?}) while traversing index path {:?}", + dt, index_path + ))); + } + other => { + return Err(DataFusionError::Internal(format!( + "Expected struct array while traversing index path {:?} for partition column, got {:?}", + index_path, other + ))); + } + } + } + Ok(current_column) + } + + /// Find the path to a field by its ID (e.g., ["address", "city"]) in the Iceberg schema + fn find_field_path(table_schema: &Schema, field_id: i32) -> DFResult<Vec<String>> { Review Comment: We might need to consider this function as well @CTTY following our discussion [here](https://github.com/apache/iceberg-rust/pull/1602/files#r2289641476). It may not be the right place at the moment. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
