|
| 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 | + |
| 18 | +//! Catalog API for Apache Iceberg |
| 19 | +
|
| 20 | +use crate::spec::{PartitionSpec, Schema, SortOrder}; |
| 21 | +use crate::table::Table; |
| 22 | +use crate::Result; |
| 23 | +use async_trait::async_trait; |
| 24 | +use std::collections::HashMap; |
| 25 | + |
| 26 | +/// The catalog API for Iceberg Rust. |
| 27 | +#[async_trait] |
| 28 | +pub trait Catalog { |
| 29 | + /// List namespaces from table. |
| 30 | + async fn list_namespaces(&self, parent: Option<&NamespaceIdent>) |
| 31 | + -> Result<Vec<NamespaceIdent>>; |
| 32 | + |
| 33 | + /// Create a new namespace inside the catalog. |
| 34 | + async fn create_namespace( |
| 35 | + &self, |
| 36 | + namespace: &NamespaceIdent, |
| 37 | + properties: HashMap<String, String>, |
| 38 | + ) -> Result<Namespace>; |
| 39 | + |
| 40 | + /// Get a namespace information from the catalog. |
| 41 | + async fn get_namespace(&self, namespace: &NamespaceIdent) -> Result<Namespace>; |
| 42 | + |
| 43 | + /// Update a namespace inside the catalog. |
| 44 | + /// |
| 45 | + /// # Behavior |
| 46 | + /// |
| 47 | + /// The properties must be the full set of namespace. |
| 48 | + async fn update_namespace( |
| 49 | + &self, |
| 50 | + namespace: &NamespaceIdent, |
| 51 | + properties: HashMap<String, String>, |
| 52 | + ) -> Result<()>; |
| 53 | + |
| 54 | + /// Drop a namespace from the catalog. |
| 55 | + async fn drop_namespace(&self, namespace: &NamespaceIdent) -> Result<()>; |
| 56 | + |
| 57 | + /// List tables from namespace. |
| 58 | + async fn list_tables(&self, namespace: &NamespaceIdent) -> Result<Vec<TableIdent>>; |
| 59 | + |
| 60 | + /// Create a new table inside the namespace. |
| 61 | + async fn create_table( |
| 62 | + &self, |
| 63 | + namespace: &NamespaceIdent, |
| 64 | + creation: TableCreation, |
| 65 | + ) -> Result<Table>; |
| 66 | + |
| 67 | + /// Load table from the catalog. |
| 68 | + async fn load_table(&self, table: &TableIdent) -> Result<Table>; |
| 69 | + |
| 70 | + /// Drop a table from the catalog. |
| 71 | + async fn drop_table(&self, table: &TableIdent) -> Result<()>; |
| 72 | + |
| 73 | + /// Check if a table exists in the catalog. |
| 74 | + async fn stat_table(&self, table: &TableIdent) -> Result<bool>; |
| 75 | + |
| 76 | + /// Rename a table in the catalog. |
| 77 | + async fn rename_table(&self, src: &TableIdent, dest: &TableIdent) -> Result<()>; |
| 78 | + |
| 79 | + /// Update a table to the catalog. |
| 80 | + async fn update_table(&self, table: &TableIdent, commit: TableCommit) -> Result<Table>; |
| 81 | + |
| 82 | + /// Update multiple tables to the catalog as an atomic operation. |
| 83 | + async fn update_tables(&self, tables: &[(TableIdent, TableCommit)]) -> Result<()>; |
| 84 | +} |
| 85 | + |
| 86 | +/// NamespaceIdent represents the identifier of a namespace in the catalog. |
| 87 | +pub struct NamespaceIdent(Vec<String>); |
| 88 | + |
| 89 | +/// Namespace represents a namespace in the catalog. |
| 90 | +pub struct Namespace { |
| 91 | + name: NamespaceIdent, |
| 92 | + properties: HashMap<String, String>, |
| 93 | +} |
| 94 | + |
| 95 | +/// TableIdent represents the identifier of a table in the catalog. |
| 96 | +pub struct TableIdent { |
| 97 | + namespace: NamespaceIdent, |
| 98 | + name: String, |
| 99 | +} |
| 100 | + |
| 101 | +/// TableCreation represents the creation of a table in the catalog. |
| 102 | +pub struct TableCreation { |
| 103 | + name: String, |
| 104 | + location: String, |
| 105 | + schema: Schema, |
| 106 | + partition_spec: Option<PartitionSpec>, |
| 107 | + sort_order: SortOrder, |
| 108 | + properties: HashMap<String, String>, |
| 109 | +} |
| 110 | + |
| 111 | +/// TableCommit represents the commit of a table in the catalog. |
| 112 | +pub struct TableCommit { |
| 113 | + ident: TableIdent, |
| 114 | + requirements: Vec<TableRequirement>, |
| 115 | + updates: Vec<TableUpdate>, |
| 116 | +} |
| 117 | + |
| 118 | +/// TableRequirement represents a requirement for a table in the catalog. |
| 119 | +pub enum TableRequirement { |
| 120 | + /// The table must not already exist; used for create transactions |
| 121 | + NotExist, |
| 122 | + /// The table UUID must match the requirement. |
| 123 | + UuidMatch(String), |
| 124 | + /// The table branch or tag identified by the requirement's `reference` must |
| 125 | + /// reference the requirement's `snapshot-id`. |
| 126 | + RefSnapshotIdMatch { |
| 127 | + /// The reference of the table to assert. |
| 128 | + reference: String, |
| 129 | + /// The snapshot id of the table to assert. |
| 130 | + /// If the id is `None`, the ref must not already exist. |
| 131 | + snapshot_id: Option<i64>, |
| 132 | + }, |
| 133 | + /// The table's last assigned column id must match the requirement. |
| 134 | + LastAssignedFieldIdMatch(i64), |
| 135 | + /// The table's current schema id must match the requirement. |
| 136 | + CurrentSchemaIdMatch(i64), |
| 137 | + /// The table's last assigned partition id must match the |
| 138 | + /// requirement. |
| 139 | + LastAssignedPartitionIdMatch(i64), |
| 140 | + /// The table's default spec id must match the requirement. |
| 141 | + DefaultSpecIdMatch(i64), |
| 142 | + /// The table's default sort order id must match the requirement. |
| 143 | + DefaultSortOrderIdMatch(i64), |
| 144 | +} |
| 145 | + |
| 146 | +/// TableUpdate represents an update to a table in the catalog. |
| 147 | +/// |
| 148 | +/// TODO: we should fill with UpgradeFormatVersionUpdate, AddSchemaUpdate and so on. |
| 149 | +pub enum TableUpdate {} |
0 commit comments