Skip to main content

iceberg/transaction/
upgrade_format_version.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 std::sync::Arc;
19
20use async_trait::async_trait;
21
22use crate::Result;
23use crate::TableUpdate::UpgradeFormatVersion;
24use crate::error::invalid_data;
25use crate::spec::FormatVersion;
26use crate::table::Table;
27use crate::transaction::action::{ActionCommit, TransactionAction};
28
29/// A transaction action to upgrade a table's format version.
30///
31/// This action is used within a transaction to indicate that the
32/// table's format version should be upgraded to a specified version.
33/// The location remains optional until explicitly set via [`UpgradeFormatVersionAction::set_format_version`].
34pub struct UpgradeFormatVersionAction {
35    format_version: Option<FormatVersion>,
36}
37
38impl UpgradeFormatVersionAction {
39    /// Creates a new `UpgradeFormatVersionAction` with no version set.
40    pub(crate) fn new() -> Self {
41        UpgradeFormatVersionAction {
42            format_version: None,
43        }
44    }
45
46    /// Sets the target format version for the upgrade.
47    ///
48    /// # Arguments
49    ///
50    /// * `format_version` - The version to upgrade the table format to.
51    ///
52    /// # Returns
53    ///
54    /// Returns the updated `UpgradeFormatVersionAction` with the format version set.
55    pub fn set_format_version(mut self, format_version: FormatVersion) -> Self {
56        self.format_version = Some(format_version);
57        self
58    }
59}
60
61#[async_trait]
62impl TransactionAction for UpgradeFormatVersionAction {
63    async fn commit(self: Arc<Self>, _table: &Table) -> Result<ActionCommit> {
64        let format_version = self.format_version.ok_or_else(|| {
65            invalid_data!("FormatVersion is not set for UpgradeFormatVersionAction!")
66        })?;
67
68        Ok(ActionCommit::new(
69            vec![UpgradeFormatVersion { format_version }],
70            vec![],
71        ))
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use as_any::Downcast;
78
79    use crate::spec::FormatVersion;
80    use crate::transaction::Transaction;
81    use crate::transaction::action::ApplyTransactionAction;
82    use crate::transaction::upgrade_format_version::UpgradeFormatVersionAction;
83
84    #[test]
85    fn test_upgrade_format_version() {
86        let table = crate::transaction::tests::make_v1_table();
87        let tx = Transaction::new(&table);
88        let tx = tx
89            .upgrade_table_version()
90            .set_format_version(FormatVersion::V2)
91            .apply(tx)
92            .unwrap();
93
94        assert_eq!(tx.actions.len(), 1);
95
96        let action = (*tx.actions[0])
97            .downcast_ref::<UpgradeFormatVersionAction>()
98            .unwrap();
99
100        assert_eq!(action.format_version, Some(FormatVersion::V2));
101    }
102}