jonathanc-n commented on code in PR #1040: URL: https://github.com/apache/iceberg-rust/pull/1040#discussion_r2002302621
########## crates/iceberg/src/arrow/record_batch_partition_splitter.rs: ########## @@ -0,0 +1,422 @@ +// 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::{create_transform_function, BoxedTransformFunction}; +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 { Review Comment: ```suggestion pub struct RecordBatchPartitionSplitter { ``` ########## crates/iceberg/src/arrow/record_batch_partition_splitter.rs: ########## @@ -0,0 +1,422 @@ +// 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::{create_transform_function, BoxedTransformFunction}; +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, +} + +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)>> { + let rows = self + .row_converter + .convert_columns(partition_columns) + .map_err(|e| Error::new(ErrorKind::DataInvalid, e.to_string()))?; + + // Group the batch by row value. + let mut group_ids = HashMap::new(); + rows.into_iter().enumerate().for_each(|(row_id, row)| { + group_ids.entry(row.owned()).or_insert(vec![]).push(row_id); + }); + + // Partition the batch with same partition partition_values + let mut partition_batches = Vec::with_capacity(group_ids.len()); + for (row, row_ids) in group_ids.into_iter() { + // generate the bool filter array from column_ids + let filter_array: BooleanArray = { + let mut filter = vec![false; batch.num_rows()]; + row_ids.into_iter().for_each(|row_id| { + filter[row_id] = true; + }); + filter.into() + }; + + // filter the RecordBatch + partition_batches.push(( + row, + filter_record_batch(batch, &filter_array) + .expect("We should guarantee the filter array is valid"), Review Comment: prefer to propogate error instead of expect ########## crates/iceberg/src/arrow/record_batch_partition_splitter.rs: ########## @@ -0,0 +1,422 @@ +// 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::{create_transform_function, BoxedTransformFunction}; +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 { Review Comment: and rest of code -- 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