fvaleye commented on code in PR #1602:
URL: https://github.com/apache/iceberg-rust/pull/1602#discussion_r2293249628


##########
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)?;

Review Comment:
   Good, thanks for sharing. I will use #1040 when merged!



-- 
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]

Reply via email to