blackmwk commented on code in PR #3230: URL: https://github.com/apache/iceberg-rust/pull/3230#discussion_r4069950804
########## crates/iceberg/src/writer/base_writer/position_delete_input.rs: ########## @@ -0,0 +1,320 @@ +// 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. + +//! Accumulates position delete rows and renders them as a spec-conforming [`RecordBatch`]. +//! +//! [`PositionDeletes`] collects `(file_path, pos)` pairs and, on +//! [`to_record_batch`](PositionDeletes::to_record_batch), emits the two required position +//! delete columns — `file_path` (`Utf8`) and `pos` (`Int64`) — sorted by `(file_path, pos)` +//! with duplicate pairs removed, as the spec requires. That batch is exactly what +//! [`PositionDeleteFileWriter`](super::position_delete_writer::PositionDeleteFileWriter) +//! accepts, so it can be handed straight to that writer. +//! +//! Position delete files are a v2 construct. v3 tables use deletion vectors and forbid adding +//! new position delete files, so a format-version gate must be applied at the +//! transaction/commit layer before routing v3 writes here. + +// Nothing wires `PositionDeletes` into a writer yet; the write-path plumbing lands in a follow-up. +#![allow(dead_code)] + +use std::collections::BTreeMap; +use std::sync::Arc; + +use arrow_array::builder::{Int64Builder, StringBuilder}; +use arrow_array::{ArrayRef, RecordBatch}; +use roaring::RoaringTreemap; + +use crate::{Error, ErrorKind, Result}; + +/// Accumulates `(file_path, pos)` position delete rows. +/// +/// Keying paths in a [`BTreeMap`] and positions in a [`RoaringTreemap`] deduplicates repeated +/// `(file_path, pos)` pairs and, when iterated, yields the spec-required ascending +/// `(file_path, pos)` order for free, so callers may [`insert`](Self::insert) rows in any +/// order. +#[derive(Debug, Default)] +pub(crate) struct PositionDeletes { + rows: BTreeMap<String, RoaringTreemap>, +} + +impl PositionDeletes { + /// Creates an empty accumulator. + pub(crate) fn new() -> Self { + Self::default() + } + + /// Records that row `pos` of `path` is deleted. `pos` is a row position, so it is + /// non-negative by construction. Re-inserting the same `(path, pos)` is a no-op. + pub(crate) fn insert(&mut self, path: impl Into<String>, pos: u64) { + self.rows.entry(path.into()).or_default().insert(pos); + } + + /// Returns whether no positions have been recorded. `insert` never leaves an empty + /// treemap behind, so an empty map means no positions. + pub(crate) fn is_empty(&self) -> bool { + self.rows.is_empty() + } + + /// Returns the total number of recorded positions across all paths. + pub(crate) fn len(&self) -> usize { + self.rows + .values() + .map(|positions| positions.len()) + .sum::<u64>() as usize + } + + /// Renders the accumulated rows as a spec-conforming position delete [`RecordBatch`]. + /// + /// The batch has exactly the two required position delete columns, built against the + /// writer's shared [`position_delete_arrow_schema`](super::position_delete_writer::position_delete_arrow_schema) + /// so the reserved field ids stay wired in. Rows are emitted sorted by `(file_path, pos)` + /// with duplicate pairs removed. An empty accumulator yields a valid 0-row batch. + pub(crate) fn to_record_batch(&self) -> Result<RecordBatch> { + let mut path_builder = StringBuilder::new(); + let mut pos_builder = Int64Builder::new(); + // The `BTreeMap` iterates paths ascending and each `RoaringTreemap` iterates positions + // ascending, so the columns come out sorted by `(file_path, pos)` and deduplicated. + for (path, positions) in &self.rows { + for pos in positions.iter() { Review Comment: It's doesn't have to be a blocker. ########## crates/iceberg/src/writer/base_writer/position_delete_input.rs: ########## @@ -0,0 +1,320 @@ +// 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. + +//! Accumulates position delete rows and renders them as a spec-conforming [`RecordBatch`]. +//! +//! [`PositionDeletes`] collects `(file_path, pos)` pairs and, on +//! [`to_record_batch`](PositionDeletes::to_record_batch), emits the two required position +//! delete columns — `file_path` (`Utf8`) and `pos` (`Int64`) — sorted by `(file_path, pos)` +//! with duplicate pairs removed, as the spec requires. That batch is exactly what +//! [`PositionDeleteFileWriter`](super::position_delete_writer::PositionDeleteFileWriter) +//! accepts, so it can be handed straight to that writer. +//! +//! Position delete files are a v2 construct. v3 tables use deletion vectors and forbid adding +//! new position delete files, so a format-version gate must be applied at the +//! transaction/commit layer before routing v3 writes here. + +// Nothing wires `PositionDeletes` into a writer yet; the write-path plumbing lands in a follow-up. +#![allow(dead_code)] + +use std::collections::BTreeMap; +use std::sync::Arc; + +use arrow_array::builder::{Int64Builder, StringBuilder}; +use arrow_array::{ArrayRef, RecordBatch}; +use roaring::RoaringTreemap; + +use crate::{Error, ErrorKind, Result}; + +/// Accumulates `(file_path, pos)` position delete rows. +/// +/// Keying paths in a [`BTreeMap`] and positions in a [`RoaringTreemap`] deduplicates repeated +/// `(file_path, pos)` pairs and, when iterated, yields the spec-required ascending +/// `(file_path, pos)` order for free, so callers may [`insert`](Self::insert) rows in any +/// order. +#[derive(Debug, Default)] +pub(crate) struct PositionDeletes { + rows: BTreeMap<String, RoaringTreemap>, +} + +impl PositionDeletes { + /// Creates an empty accumulator. + pub(crate) fn new() -> Self { + Self::default() + } + + /// Records that row `pos` of `path` is deleted. `pos` is a row position, so it is + /// non-negative by construction. Re-inserting the same `(path, pos)` is a no-op. + pub(crate) fn insert(&mut self, path: impl Into<String>, pos: u64) { Review Comment: Should we fail when duplicates found? ########## crates/iceberg/src/writer/base_writer/position_delete_input.rs: ########## @@ -0,0 +1,320 @@ +// 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. + +//! Accumulates position delete rows and renders them as a spec-conforming [`RecordBatch`]. +//! +//! [`PositionDeletes`] collects `(file_path, pos)` pairs and, on +//! [`to_record_batch`](PositionDeletes::to_record_batch), emits the two required position +//! delete columns — `file_path` (`Utf8`) and `pos` (`Int64`) — sorted by `(file_path, pos)` +//! with duplicate pairs removed, as the spec requires. That batch is exactly what +//! [`PositionDeleteFileWriter`](super::position_delete_writer::PositionDeleteFileWriter) +//! accepts, so it can be handed straight to that writer. +//! +//! Position delete files are a v2 construct. v3 tables use deletion vectors and forbid adding +//! new position delete files, so a format-version gate must be applied at the +//! transaction/commit layer before routing v3 writes here. + +// Nothing wires `PositionDeletes` into a writer yet; the write-path plumbing lands in a follow-up. +#![allow(dead_code)] + +use std::collections::BTreeMap; +use std::sync::Arc; + +use arrow_array::builder::{Int64Builder, StringBuilder}; +use arrow_array::{ArrayRef, RecordBatch}; +use roaring::RoaringTreemap; + +use crate::{Error, ErrorKind, Result}; + +/// Accumulates `(file_path, pos)` position delete rows. +/// +/// Keying paths in a [`BTreeMap`] and positions in a [`RoaringTreemap`] deduplicates repeated +/// `(file_path, pos)` pairs and, when iterated, yields the spec-required ascending +/// `(file_path, pos)` order for free, so callers may [`insert`](Self::insert) rows in any +/// order. +#[derive(Debug, Default)] +pub(crate) struct PositionDeletes { + rows: BTreeMap<String, RoaringTreemap>, +} + +impl PositionDeletes { + /// Creates an empty accumulator. + pub(crate) fn new() -> Self { + Self::default() + } + + /// Records that row `pos` of `path` is deleted. `pos` is a row position, so it is + /// non-negative by construction. Re-inserting the same `(path, pos)` is a no-op. + pub(crate) fn insert(&mut self, path: impl Into<String>, pos: u64) { + self.rows.entry(path.into()).or_default().insert(pos); + } + + /// Returns whether no positions have been recorded. `insert` never leaves an empty + /// treemap behind, so an empty map means no positions. + pub(crate) fn is_empty(&self) -> bool { + self.rows.is_empty() + } + + /// Returns the total number of recorded positions across all paths. + pub(crate) fn len(&self) -> usize { + self.rows + .values() + .map(|positions| positions.len()) + .sum::<u64>() as usize + } + + /// Renders the accumulated rows as a spec-conforming position delete [`RecordBatch`]. + /// + /// The batch has exactly the two required position delete columns, built against the + /// writer's shared [`position_delete_arrow_schema`](super::position_delete_writer::position_delete_arrow_schema) + /// so the reserved field ids stay wired in. Rows are emitted sorted by `(file_path, pos)` + /// with duplicate pairs removed. An empty accumulator yields a valid 0-row batch. + pub(crate) fn to_record_batch(&self) -> Result<RecordBatch> { + let mut path_builder = StringBuilder::new(); + let mut pos_builder = Int64Builder::new(); + // The `BTreeMap` iterates paths ascending and each `RoaringTreemap` iterates positions + // ascending, so the columns come out sorted by `(file_path, pos)` and deduplicated. + for (path, positions) in &self.rows { + for pos in positions.iter() { Review Comment: nit: I found an interesting mathod `append_value_n`, which could be used to append same string n times. I assume this is an optimization that could utilize RLE encoding, it would be nice to have a try, wdyt? ########## crates/iceberg/src/writer/base_writer/position_delete_input.rs: ########## @@ -0,0 +1,320 @@ +// 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. + +//! Accumulates position delete rows and renders them as a spec-conforming [`RecordBatch`]. +//! +//! [`PositionDeletes`] collects `(file_path, pos)` pairs and, on +//! [`to_record_batch`](PositionDeletes::to_record_batch), emits the two required position +//! delete columns — `file_path` (`Utf8`) and `pos` (`Int64`) — sorted by `(file_path, pos)` +//! with duplicate pairs removed, as the spec requires. That batch is exactly what +//! [`PositionDeleteFileWriter`](super::position_delete_writer::PositionDeleteFileWriter) +//! accepts, so it can be handed straight to that writer. +//! +//! Position delete files are a v2 construct. v3 tables use deletion vectors and forbid adding +//! new position delete files, so a format-version gate must be applied at the +//! transaction/commit layer before routing v3 writes here. + +// Nothing wires `PositionDeletes` into a writer yet; the write-path plumbing lands in a follow-up. +#![allow(dead_code)] + +use std::collections::BTreeMap; +use std::sync::Arc; + +use arrow_array::builder::{Int64Builder, StringBuilder}; +use arrow_array::{ArrayRef, RecordBatch}; +use roaring::RoaringTreemap; + +use crate::{Error, ErrorKind, Result}; + +/// Accumulates `(file_path, pos)` position delete rows. +/// +/// Keying paths in a [`BTreeMap`] and positions in a [`RoaringTreemap`] deduplicates repeated +/// `(file_path, pos)` pairs and, when iterated, yields the spec-required ascending +/// `(file_path, pos)` order for free, so callers may [`insert`](Self::insert) rows in any +/// order. +#[derive(Debug, Default)] +pub(crate) struct PositionDeletes { + rows: BTreeMap<String, RoaringTreemap>, +} + +impl PositionDeletes { + /// Creates an empty accumulator. + pub(crate) fn new() -> Self { + Self::default() + } + + /// Records that row `pos` of `path` is deleted. `pos` is a row position, so it is + /// non-negative by construction. Re-inserting the same `(path, pos)` is a no-op. + pub(crate) fn insert(&mut self, path: impl Into<String>, pos: u64) { + self.rows.entry(path.into()).or_default().insert(pos); + } + + /// Returns whether no positions have been recorded. `insert` never leaves an empty + /// treemap behind, so an empty map means no positions. + pub(crate) fn is_empty(&self) -> bool { + self.rows.is_empty() + } + + /// Returns the total number of recorded positions across all paths. + pub(crate) fn len(&self) -> usize { + self.rows + .values() + .map(|positions| positions.len()) + .sum::<u64>() as usize + } + + /// Renders the accumulated rows as a spec-conforming position delete [`RecordBatch`]. + /// + /// The batch has exactly the two required position delete columns, built against the + /// writer's shared [`position_delete_arrow_schema`](super::position_delete_writer::position_delete_arrow_schema) + /// so the reserved field ids stay wired in. Rows are emitted sorted by `(file_path, pos)` + /// with duplicate pairs removed. An empty accumulator yields a valid 0-row batch. + pub(crate) fn to_record_batch(&self) -> Result<RecordBatch> { Review Comment: I assume we should return an iterator of record batch where each record batch should have a fixed size? If we expand all rows into one record batch, I'm worrying about the memory consumption. The fixed size could be a config passed to it. -- 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]
