ZENOTME commented on code in PR #703: URL: https://github.com/apache/iceberg-rust/pull/703#discussion_r1860035033
########## crates/iceberg/src/writer/base_writer/equality_delete_writer.rs: ########## @@ -0,0 +1,502 @@ +// 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. + +//! This module provide `EqualityDeleteWriter`. + +use std::sync::Arc; + +use arrow_array::RecordBatch; +use arrow_schema::{DataType, Field, SchemaRef as ArrowSchemaRef}; +use itertools::Itertools; +use parquet::arrow::PARQUET_FIELD_ID_META_KEY; + +use crate::arrow::record_batch_projector::RecordBatchProjector; +use crate::arrow::schema_to_arrow_schema; +use crate::spec::{DataFile, SchemaRef, Struct}; +use crate::writer::file_writer::{FileWriter, FileWriterBuilder}; +use crate::writer::{IcebergWriter, IcebergWriterBuilder}; +use crate::{Error, ErrorKind, Result}; + +/// Builder for `EqualityDeleteWriter`. +#[derive(Clone)] +pub struct EqualityDeleteFileWriterBuilder<B: FileWriterBuilder> { + inner: B, +} + +impl<B: FileWriterBuilder> EqualityDeleteFileWriterBuilder<B> { + /// Create a new `EqualityDeleteFileWriterBuilder` using a `FileWriterBuilder`. + pub fn new(inner: B) -> Self { + Self { inner } + } +} + +/// Config for `EqualityDeleteWriter`. +pub struct EqualityDeleteWriterConfig { + // Field ids used to determine row equality in equality delete files. + equality_ids: Vec<i32>, + // Projector used to project the data chunk into specific fields. + projector: RecordBatchProjector, + partition_value: Struct, +} + +impl EqualityDeleteWriterConfig { + /// Create a new `DataFileWriterConfig` with equality ids. + pub fn new( + equality_ids: Vec<i32>, + original_schema: SchemaRef, + partition_value: Option<Struct>, + ) -> Result<Self> { + let original_arrow_schema = Arc::new(schema_to_arrow_schema(&original_schema)?); + let projector = RecordBatchProjector::new( + original_arrow_schema, + &equality_ids, + // The following rule comes from https://iceberg.apache.org/spec/#identifier-field-ids + // - The identifier field ids must be used for primitive types. + // - The identifier field ids must not be used for floating point types or nullable fields. + // - The identifier field ids can be nested field of struct but not nested field of nullable struct. + |field| { + // Only primitive type is allowed to be used for identifier field ids + if field.is_nullable() + || !field.data_type().is_primitive() + || matches!( + field.data_type(), + DataType::Float16 | DataType::Float32 | DataType::Float64 + ) + { + return Ok(None); + } + Ok(Some( + field + .metadata() + .get(PARQUET_FIELD_ID_META_KEY) + .ok_or_else(|| { + Error::new(ErrorKind::Unexpected, "Field metadata is missing.") + })? + .parse::<i64>() + .map_err(|e| Error::new(ErrorKind::Unexpected, e.to_string()))?, + )) + }, + |field: &Field| !field.is_nullable(), + )?; + Ok(Self { + equality_ids, + projector, + partition_value: partition_value.unwrap_or(Struct::empty()), + }) + } + + /// Return projected Schema + pub fn projected_schema_ref(&self) -> &ArrowSchemaRef { Review Comment: We should expose this interface so that users can use it. https://github.com/apache/iceberg-rust/pull/703#discussion_r1856341663 -- 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