ZENOTME commented on code in PR #1040:
URL: https://github.com/apache/iceberg-rust/pull/1040#discussion_r2308047691


##########
crates/iceberg/src/arrow/record_batch_partition_splitter.rs:
##########
@@ -0,0 +1,421 @@
+// 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::collections::HashMap;
+use std::sync::Arc;
+
+use arrow_array::{ArrayRef, BooleanArray, RecordBatch, StructArray};
+use arrow_row::{OwnedRow, RowConverter, SortField};
+use arrow_schema::{DataType, SchemaRef as ArrowSchemaRef};
+use arrow_select::filter::filter_record_batch;
+use itertools::Itertools;
+use parquet::arrow::PARQUET_FIELD_ID_META_KEY;
+
+use super::arrow_struct_to_literal;
+use super::record_batch_projector::RecordBatchProjector;
+use crate::arrow::type_to_arrow_type;
+use crate::spec::{Literal, PartitionSpecRef, SchemaRef, Struct, Type};
+use crate::transform::{BoxedTransformFunction, create_transform_function};
+use crate::{Error, ErrorKind, Result};
+
+/// The splitter used to split the record batch into multiple record batches 
by the partition spec.
+// # TODO
+// Remove this after partition writer supported.
+#[allow(dead_code)]
+pub struct RecordBatchPartitionSplitter {
+    schema: SchemaRef,
+    partition_spec: PartitionSpecRef,
+    projector: RecordBatchProjector,
+    transform_functions: Vec<BoxedTransformFunction>,
+    row_converter: RowConverter,
+}
+
+// # TODO
+// Remove this after partition writer supported.
+#[allow(dead_code)]
+impl RecordBatchPartitionSplitter {
+    pub fn new(
+        input_schema: ArrowSchemaRef,
+        iceberg_schema: SchemaRef,
+        partition_spec: PartitionSpecRef,
+    ) -> Result<Self> {
+        let projector = RecordBatchProjector::new(
+            input_schema,
+            &partition_spec
+                .fields()
+                .iter()
+                .map(|field| field.source_id)
+                .collect::<Vec<_>>(),
+            // The source columns, selected by ids, must be a primitive type 
and cannot be contained in a map or list, but may be nested in a struct.
+            // ref: https://iceberg.apache.org/spec/#partitioning
+            |field| {
+                if !field.data_type().is_primitive() {
+                    return Ok(None);
+                }
+                field
+                    .metadata()
+                    .get(PARQUET_FIELD_ID_META_KEY)
+                    .map(|s| {
+                        s.parse::<i64>()
+                            .map_err(|e| Error::new(ErrorKind::Unexpected, 
e.to_string()))
+                    })
+                    .transpose()
+            },
+            |_| true,
+        )?;
+        let transform_functions = partition_spec
+            .fields()
+            .iter()
+            .map(|field| create_transform_function(&field.transform))
+            .collect::<Result<Vec<_>>>()?;
+        let row_converter = RowConverter::new(
+            projector
+                .projected_schema_ref()
+                .fields()
+                .iter()
+                .map(|field| SortField::new(field.data_type().clone()))
+                .collect(),
+        )?;
+        Ok(Self {
+            schema: iceberg_schema,
+            partition_spec,
+            projector,
+            transform_functions,
+            row_converter,
+        })
+    }
+
+    /// Split the record batch into multiple record batches according to 
provided partition columns.
+    pub fn split_by_partition(
+        &self,
+        batch: &RecordBatch,
+        partition_columns: &[ArrayRef],
+    ) -> Result<Vec<(OwnedRow, RecordBatch)>> {

Review Comment:
   Good catch! I noticed that out `Struct` support `Hash`, so we don't need 
`OwnedRow`. I have remove them and use `Struct` directlty.



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