Skip to main content

iceberg/cow_rewrite/
rewriter.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use arrow_array::RecordBatch;
19
20use crate::Result;
21
22/// Result of rewriting a single record batch.
23#[derive(Debug)]
24pub struct CowBatchRewrite {
25    /// Rewritten output batch, or `None` when the input batch is fully removed.
26    ///
27    /// Output batches must use the same schema as their input batch — the
28    /// planned snapshot's schema, which may be older than the table's current
29    /// schema. Rewriters must also preserve each source file's partition
30    /// values: this primitive writes replacements into the source file's
31    /// partition and does not repartition rows.
32    pub output: Option<RecordBatch>,
33    /// Whether the rewrite changed the input batch contents.
34    ///
35    /// Set this to `true` whenever `output` differs from the input batch,
36    /// including filtered rows, updated values, reordered rows, or `None`.
37    pub changed: bool,
38}
39
40/// Rewrites record batches for copy-on-write operations.
41///
42/// `rewrite_batch` is synchronous: it runs on the async runtime thread that
43/// drives the read/write pipeline, so implementations must not perform
44/// blocking work — async I/O such as catalog enrichment or cross-table
45/// lookups is not supported in this contract. The method stays sync (rather
46/// than returning a boxed future) so the trait remains object safe for
47/// `Arc<dyn CowBatchRewriter>`.
48pub trait CowBatchRewriter: Send + Sync {
49    /// Rewrites a record batch and reports whether it changed.
50    ///
51    /// `output: None` means the batch is fully removed, which is itself a
52    /// change: the orchestrator treats it as changed regardless of the
53    /// `changed` flag, so a rewriter cannot accidentally keep dropped rows
54    /// alive by reporting `changed: false` alongside a `None` output.
55    fn rewrite_batch(&self, batch: RecordBatch) -> Result<CowBatchRewrite>;
56}