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::{HistoryTable, 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 /// [`HistoryTable`]
38 History,
39}
40
41impl MetadataTableType {
42 /// Returns the string representation of the metadata table type.
43 pub fn as_str(&self) -> &str {
44 match self {
45 MetadataTableType::Snapshots => "snapshots",
46 MetadataTableType::Manifests => "manifests",
47 MetadataTableType::History => "history",
48 }
49 }
50
51 /// Returns all the metadata table types.
52 pub fn all_types() -> impl Iterator<Item = Self> {
53 use strum::IntoEnumIterator;
54 Self::iter()
55 }
56}
57
58impl TryFrom<&str> for MetadataTableType {
59 type Error = String;
60
61 fn try_from(value: &str) -> Result<Self, String> {
62 match value {
63 "snapshots" => Ok(Self::Snapshots),
64 "manifests" => Ok(Self::Manifests),
65 "history" => Ok(Self::History),
66 _ => Err(format!("invalid metadata table type: {value}")),
67 }
68 }
69}
70
71impl<'a> MetadataTable<'a> {
72 /// Creates a new metadata scan.
73 pub fn new(table: &'a Table) -> Self {
74 Self(table)
75 }
76
77 /// Get the snapshots table.
78 pub fn snapshots(&self) -> SnapshotsTable<'_> {
79 SnapshotsTable::new(self.0)
80 }
81
82 /// Get the manifests table.
83 pub fn manifests(&self) -> ManifestsTable<'_> {
84 ManifestsTable::new(self.0)
85 }
86
87 /// Get the history table.
88 pub fn history(&self) -> HistoryTable<'_> {
89 HistoryTable::new(self.0)
90 }
91}