diff --git a/crates/iceberg/src/table.rs b/crates/iceberg/src/table.rs index ebee670f4..14679fe93 100644 --- a/crates/iceberg/src/table.rs +++ b/crates/iceberg/src/table.rs @@ -25,7 +25,7 @@ use crate::io::object_cache::ObjectCache; use crate::io::FileIO; use crate::scan::TableScanBuilder; use crate::spec::{TableMetadata, TableMetadataRef}; -use crate::{Error, ErrorKind, Result, TableIdent}; +use crate::{Catalog, Error, ErrorKind, Result, TableIdent}; /// Builder to create table scan. pub struct TableBuilder { @@ -33,6 +33,7 @@ pub struct TableBuilder { metadata_location: Option, metadata: Option, identifier: Option, + catalog: Option>, readonly: bool, disable_cache: bool, cache_size_bytes: Option, @@ -45,6 +46,7 @@ impl TableBuilder { metadata_location: None, metadata: None, identifier: None, + catalog: None, readonly: false, disable_cache: false, cache_size_bytes: None, @@ -75,6 +77,12 @@ impl TableBuilder { self } + /// required - passes in the reference to the Catalog to use for the Table + pub fn catalog(mut self, catalog: Arc) -> Self { + self.catalog = Some(catalog); + self + } + /// specifies if the Table is readonly or not (default not) pub fn readonly(mut self, readonly: bool) -> Self { self.readonly = readonly; @@ -102,6 +110,7 @@ impl TableBuilder { metadata_location, metadata, identifier, + catalog, readonly, disable_cache, cache_size_bytes, @@ -128,6 +137,13 @@ impl TableBuilder { )); }; + let Some(catalog) = catalog else { + return Err(Error::new( + ErrorKind::DataInvalid, + "Catalog must be provided with TableBuilder.catalog()", + )); + }; + let object_cache = if disable_cache { Arc::new(ObjectCache::with_disabled_cache(file_io.clone())) } else if let Some(cache_size_bytes) = cache_size_bytes { @@ -144,6 +160,7 @@ impl TableBuilder { metadata_location, metadata, identifier, + catalog, readonly, object_cache, }) @@ -157,13 +174,14 @@ pub struct Table { metadata_location: Option, metadata: TableMetadataRef, identifier: TableIdent, + catalog: Arc, readonly: bool, object_cache: Arc, } impl Table { /// Returns a TableBuilder to build a table - pub fn builder() -> TableBuilder { + pub fn builder<'b>() -> TableBuilder { TableBuilder::new() } @@ -216,6 +234,18 @@ impl Table { pub fn reader_builder(&self) -> ArrowReaderBuilder { ArrowReaderBuilder::new(self.file_io.clone()) } + + /// Returns latest table metadata and updates current table metadata + pub async fn refresh(&mut self) -> Result { + let table = self.catalog.load_table(self.identifier()).await.unwrap(); + let metadata: TableMetadata = (*table.metadata).clone(); + + self.metadata = Arc::new(metadata.clone()); + self.file_io = table.file_io.clone(); + self.metadata_location = table.metadata_location.clone(); + + Ok(metadata) + } } /// `StaticTable` is a read-only table struct that can be created from a metadata file or from `TableMetaData` without a catalog.