Skip to content

Commit 13281d3

Browse files
Xuanwoliurenjie1024Fokko
authored
feat: Add Catalog API (#54)
* feat: Add Catalog API Signed-off-by: Xuanwo <[email protected]> * remove get config Signed-off-by: Xuanwo <[email protected]> * Fix naming Signed-off-by: Xuanwo <[email protected]> * Use ref instead Signed-off-by: Xuanwo <[email protected]> * Move table out Signed-off-by: Xuanwo <[email protected]> * Fix typo Signed-off-by: Xuanwo <[email protected]> * Update crates/iceberg/src/spec/schema.rs Co-authored-by: Renjie Liu <[email protected]> * Make partition_spec optional Signed-off-by: Xuanwo <[email protected]> * Update crates/iceberg/src/table.rs Co-authored-by: Fokko Driesprong <[email protected]> * Fix sort Signed-off-by: Xuanwo <[email protected]> * Remove config Signed-off-by: Xuanwo <[email protected]> * Make clippy happy Signed-off-by: Xuanwo <[email protected]> --------- Signed-off-by: Xuanwo <[email protected]> Co-authored-by: Renjie Liu <[email protected]> Co-authored-by: Fokko Driesprong <[email protected]>
1 parent 8c55de4 commit 13281d3

File tree

5 files changed

+184
-1
lines changed

5 files changed

+184
-1
lines changed

crates/iceberg/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ keywords = ["iceberg"]
2929
[dependencies]
3030
anyhow = "1.0.72"
3131
apache-avro = "0.15"
32+
async-trait = "0.1"
3233
bimap = "0.6"
3334
bitvec = "1.0.1"
3435
chrono = "0.4"

crates/iceberg/src/catalog.rs

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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 {}

crates/iceberg/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ pub use error::Error;
2727
pub use error::ErrorKind;
2828
pub use error::Result;
2929

30+
/// There is no implementation for this trait, allow dead code for now, should
31+
/// be removed after we have one.
32+
#[allow(dead_code)]
33+
pub mod catalog;
34+
#[allow(dead_code)]
35+
pub mod table;
36+
3037
mod avro;
3138
pub mod io;
3239
pub mod spec;

crates/iceberg/src/spec/schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub struct SchemaBuilder {
6060
}
6161

6262
impl SchemaBuilder {
63-
/// Add fields to schem builder.
63+
/// Add fields to schema builder.
6464
pub fn with_fields(mut self, fields: impl IntoIterator<Item = NestedFieldRef>) -> Self {
6565
self.fields.extend(fields);
6666
self

crates/iceberg/src/table.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
//! Table API for Apache Iceberg
19+
20+
use crate::spec::TableMetadata;
21+
22+
/// Table represents a table in the catalog.
23+
pub struct Table {
24+
metadata_location: String,
25+
metadata: TableMetadata,
26+
}

0 commit comments

Comments
 (0)