Skip to content

feat: Add refresh to get get updated TableMetadata #1154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions crates/iceberg/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ 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 {
file_io: Option<FileIO>,
metadata_location: Option<String>,
metadata: Option<TableMetadataRef>,
identifier: Option<TableIdent>,
catalog: Option<Arc<dyn Catalog>>,
readonly: bool,
disable_cache: bool,
cache_size_bytes: Option<u64>,
Expand All @@ -45,6 +46,7 @@ impl TableBuilder {
metadata_location: None,
metadata: None,
identifier: None,
catalog: None,
readonly: false,
disable_cache: false,
cache_size_bytes: None,
Expand Down Expand Up @@ -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<dyn Catalog>) -> 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;
Expand Down Expand Up @@ -102,6 +110,7 @@ impl TableBuilder {
metadata_location,
metadata,
identifier,
catalog,
readonly,
disable_cache,
cache_size_bytes,
Expand All @@ -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 {
Expand All @@ -144,6 +160,7 @@ impl TableBuilder {
metadata_location,
metadata,
identifier,
catalog,
readonly,
object_cache,
})
Expand All @@ -157,13 +174,14 @@ pub struct Table {
metadata_location: Option<String>,
metadata: TableMetadataRef,
identifier: TableIdent,
catalog: Arc<dyn Catalog>,
readonly: bool,
object_cache: Arc<ObjectCache>,
}

impl Table {
/// Returns a TableBuilder to build a table
pub fn builder() -> TableBuilder {
pub fn builder<'b>() -> TableBuilder {
TableBuilder::new()
}

Expand Down Expand Up @@ -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<TableMetadata> {
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.
Expand Down
Loading