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::TableUpdate::UpgradeFormatVersion;
23use crate::spec::FormatVersion;
24use crate::table::Table;
25use crate::transaction::action::{ActionCommit, TransactionAction};
26use crate::{Error, ErrorKind, Result};
27
28/// A transaction action to upgrade a table's format version.
29///
30/// This action is used within a transaction to indicate that the
31/// table's format version should be upgraded to a specified version.
32/// The location remains optional until explicitly set via [`UpgradeFormatVersionAction::set_format_version`].
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 TransactionAction for UpgradeFormatVersionAction {
62    async fn commit(self: Arc<Self>, _table: &Table) -> Result<ActionCommit> {
63        let format_version = self.format_version.ok_or_else(|| {
64            Error::new(
65                ErrorKind::DataInvalid,
66                "FormatVersion is not set for UpgradeFormatVersionAction!",
67            )
68        })?;
69
70        Ok(ActionCommit::new(
71            vec![UpgradeFormatVersion { format_version }],
72            vec![],
73        ))
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use as_any::Downcast;
80
81    use crate::spec::FormatVersion;
82    use crate::transaction::Transaction;
83    use crate::transaction::action::ApplyTransactionAction;
84    use crate::transaction::upgrade_format_version::UpgradeFormatVersionAction;
85
86    #[test]
87    fn test_upgrade_format_version() {
88        let table = crate::transaction::tests::make_v1_table();
89        let tx = Transaction::new(&table);
90        let tx = tx
91            .upgrade_table_version()
92            .set_format_version(FormatVersion::V2)
93            .apply(tx)
94            .unwrap();
95
96        assert_eq!(tx.actions.len(), 1);
97
98        let action = (*tx.actions[0])
99            .downcast_ref::<UpgradeFormatVersionAction>()
100            .unwrap();
101
102        assert_eq!(action.format_version, Some(FormatVersion::V2));
103    }
104}