blackmwk commented on code in PR #3230: URL: https://github.com/apache/iceberg-rust/pull/3230#discussion_r4080500060
########## 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: After second thought, I suggest to do two checks: 1. if pos could be safely converted to i64. This is a small win over current approach since we could detect the error earlier. 2. I think we need to return an error when duplicates found. Position deletes are typically used to delete position in dml statement, and if duplicates found, it should be a bug. At least, we should be these two checks using debug_assert! so that we could found bugs early in debug mode, wdyt? -- 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]
