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