iceberg/inspect/
metadata_table.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 super::{ManifestsTable, SnapshotsTable};
19use crate::table::Table;
20
21/// Metadata table is used to inspect a table's history, snapshots, and other metadata as a table.
22///
23/// References:
24/// - <https://github.com/apache/iceberg/blob/ac865e334e143dfd9e33011d8cf710b46d91f1e5/core/src/main/java/org/apache/iceberg/MetadataTableType.java#L23-L39>
25/// - <https://iceberg.apache.org/docs/latest/spark-queries/#querying-with-sql>
26/// - <https://py.iceberg.apache.org/api/#inspecting-tables>
27#[derive(Debug)]
28pub struct MetadataTable<'a>(&'a Table);
29
30/// Metadata table type.
31#[derive(Debug, Clone, strum::EnumIter)]
32pub enum MetadataTableType {
33    /// [`SnapshotsTable`]
34    Snapshots,
35    /// [`ManifestsTable`]
36    Manifests,
37}
38
39impl MetadataTableType {
40    /// Returns the string representation of the metadata table type.
41    pub fn as_str(&self) -> &str {
42        match self {
43            MetadataTableType::Snapshots => "snapshots",
44            MetadataTableType::Manifests => "manifests",
45        }
46    }
47
48    /// Returns all the metadata table types.
49    pub fn all_types() -> impl Iterator<Item = Self> {
50        use strum::IntoEnumIterator;
51        Self::iter()
52    }
53}
54
55impl TryFrom<&str> for MetadataTableType {
56    type Error = String;
57
58    fn try_from(value: &str) -> std::result::Result<Self, String> {
59        match value {
60            "snapshots" => Ok(Self::Snapshots),
61            "manifests" => Ok(Self::Manifests),
62            _ => Err(format!("invalid metadata table type: {value}")),
63        }
64    }
65}
66
67impl<'a> MetadataTable<'a> {
68    /// Creates a new metadata scan.
69    pub fn new(table: &'a Table) -> Self {
70        Self(table)
71    }
72
73    /// Get the snapshots table.
74    pub fn snapshots(&self) -> SnapshotsTable {
75        SnapshotsTable::new(self.0)
76    }
77
78    /// Get the manifests table.
79    pub fn manifests(&self) -> ManifestsTable {
80        ManifestsTable::new(self.0)
81    }
82}