Skip to content

Commit 145afdf

Browse files
authored
feat(catalog): Add register_table to Catalog trait (#1509)
## Which issue does this PR close? - Part of #1508 ## What changes are included in this PR? - Added `register_table` to `Catalog` trait - Implemented `register_table` for `MemoryCatalog` ## Are these changes tested? Added an unit test for the implementation in `MemoryCatalog`
1 parent a50bb22 commit 145afdf

File tree

7 files changed

+122
-0
lines changed

7 files changed

+122
-0
lines changed

crates/catalog/glue/src/catalog.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,17 @@ impl Catalog for GlueCatalog {
628628
}
629629
}
630630

631+
async fn register_table(
632+
&self,
633+
_table_ident: &TableIdent,
634+
_metadata_location: String,
635+
) -> Result<Table> {
636+
Err(Error::new(
637+
ErrorKind::FeatureUnsupported,
638+
"Registering a table is not supported yet",
639+
))
640+
}
641+
631642
async fn update_table(&self, _commit: TableCommit) -> Result<Table> {
632643
Err(Error::new(
633644
ErrorKind::FeatureUnsupported,

crates/catalog/hms/src/catalog.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,17 @@ impl Catalog for HmsCatalog {
504504
Ok(())
505505
}
506506

507+
async fn register_table(
508+
&self,
509+
_table_ident: &TableIdent,
510+
_metadata_location: String,
511+
) -> Result<Table> {
512+
Err(Error::new(
513+
ErrorKind::FeatureUnsupported,
514+
"Registering a table is not supported yet",
515+
))
516+
}
517+
507518
async fn update_table(&self, _commit: TableCommit) -> Result<Table> {
508519
Err(Error::new(
509520
ErrorKind::FeatureUnsupported,

crates/catalog/rest/src/catalog.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,17 @@ impl Catalog for RestCatalog {
740740
}
741741
}
742742

743+
async fn register_table(
744+
&self,
745+
_table_ident: &TableIdent,
746+
_metadata_location: String,
747+
) -> Result<Table> {
748+
Err(Error::new(
749+
ErrorKind::FeatureUnsupported,
750+
"Registering a table is not supported yet",
751+
))
752+
}
753+
743754
async fn update_table(&self, mut commit: TableCommit) -> Result<Table> {
744755
let context = self.context().await?;
745756

crates/catalog/s3tables/src/catalog.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,17 @@ impl Catalog for S3TablesCatalog {
473473
Ok(())
474474
}
475475

476+
async fn register_table(
477+
&self,
478+
_table_ident: &TableIdent,
479+
_metadata_location: String,
480+
) -> Result<Table> {
481+
Err(Error::new(
482+
ErrorKind::FeatureUnsupported,
483+
"Registering a table is not supported yet",
484+
))
485+
}
486+
476487
/// Updates an existing table within the s3tables catalog.
477488
///
478489
/// This function is still in development and will always return an error.

crates/catalog/sql/src/catalog.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,17 @@ impl Catalog for SqlCatalog {
769769
Ok(())
770770
}
771771

772+
async fn register_table(
773+
&self,
774+
_table_ident: &TableIdent,
775+
_metadata_location: String,
776+
) -> Result<Table> {
777+
Err(Error::new(
778+
ErrorKind::FeatureUnsupported,
779+
"Registering a table is not supported yet",
780+
))
781+
}
782+
772783
async fn update_table(&self, _commit: TableCommit) -> Result<Table> {
773784
Err(Error::new(
774785
ErrorKind::FeatureUnsupported,

crates/iceberg/src/catalog/memory/catalog.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,26 @@ impl Catalog for MemoryCatalog {
276276
Ok(())
277277
}
278278

279+
async fn register_table(
280+
&self,
281+
table_ident: &TableIdent,
282+
metadata_location: String,
283+
) -> Result<Table> {
284+
let mut root_namespace_state = self.root_namespace_state.lock().await;
285+
root_namespace_state.insert_new_table(&table_ident.clone(), metadata_location.clone())?;
286+
287+
let input_file = self.file_io.new_input(metadata_location.clone())?;
288+
let metadata_content = input_file.read().await?;
289+
let metadata = serde_json::from_slice::<TableMetadata>(&metadata_content)?;
290+
291+
Table::builder()
292+
.file_io(self.file_io.clone())
293+
.metadata_location(metadata_location)
294+
.metadata(metadata)
295+
.identifier(table_ident.clone())
296+
.build()
297+
}
298+
279299
/// Update a table to the catalog.
280300
async fn update_table(&self, _commit: TableCommit) -> Result<Table> {
281301
Err(Error::new(
@@ -1697,4 +1717,48 @@ mod tests {
16971717
),
16981718
);
16991719
}
1720+
1721+
#[tokio::test]
1722+
async fn test_register_table() {
1723+
// Create a catalog and namespace
1724+
let catalog = new_memory_catalog();
1725+
let namespace_ident = NamespaceIdent::new("test_namespace".into());
1726+
create_namespace(&catalog, &namespace_ident).await;
1727+
1728+
// Create a table to get a valid metadata file
1729+
let source_table_ident = TableIdent::new(namespace_ident.clone(), "source_table".into());
1730+
create_table(&catalog, &source_table_ident).await;
1731+
1732+
// Get the metadata location from the source table
1733+
let source_table = catalog.load_table(&source_table_ident).await.unwrap();
1734+
let metadata_location = source_table.metadata_location().unwrap().to_string();
1735+
1736+
// Register a new table using the same metadata location
1737+
let register_table_ident =
1738+
TableIdent::new(namespace_ident.clone(), "register_table".into());
1739+
let registered_table = catalog
1740+
.register_table(&register_table_ident, metadata_location.clone())
1741+
.await
1742+
.unwrap();
1743+
1744+
// Verify the registered table has the correct identifier
1745+
assert_eq!(registered_table.identifier(), &register_table_ident);
1746+
1747+
// Verify the registered table has the correct metadata location
1748+
assert_eq!(
1749+
registered_table.metadata_location().unwrap().to_string(),
1750+
metadata_location
1751+
);
1752+
1753+
// Verify the table exists in the catalog
1754+
assert!(catalog.table_exists(&register_table_ident).await.unwrap());
1755+
1756+
// Verify we can load the registered table
1757+
let loaded_table = catalog.load_table(&register_table_ident).await.unwrap();
1758+
assert_eq!(loaded_table.identifier(), &register_table_ident);
1759+
assert_eq!(
1760+
loaded_table.metadata_location().unwrap().to_string(),
1761+
metadata_location
1762+
);
1763+
}
17001764
}

crates/iceberg/src/catalog/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ pub trait Catalog: Debug + Sync + Send {
100100
/// Rename a table in the catalog.
101101
async fn rename_table(&self, src: &TableIdent, dest: &TableIdent) -> Result<()>;
102102

103+
/// Register an existing table to the catalog.
104+
async fn register_table(&self, table: &TableIdent, metadata_location: String) -> Result<Table>;
105+
103106
/// Update a table to the catalog.
104107
async fn update_table(&self, commit: TableCommit) -> Result<Table>;
105108
}

0 commit comments

Comments
 (0)