gabotechs commented on code in PR #152:
URL: 
https://github.com/apache/datafusion-iceberg/pull/152#discussion_r4770842945


##########
crates/datafusion/src/catalog/provider.rs:
##########
@@ -19,69 +19,50 @@ use std::collections::HashMap;
 use std::sync::Arc;
 
 use datafusion::catalog::{CatalogProvider, SchemaProvider};
+use datafusion::common::config_err;
 use datafusion::error::Result;
 use futures::future::try_join_all;
-use iceberg::{Catalog, NamespaceIdent};
+use iceberg::table::Table;
+use iceberg::{Catalog, TableIdent};
 
 use super::schema::IcebergSchemaProvider;
 use crate::to_datafusion_error;
 
-/// Provides an interface to manage and access multiple schemas
-/// within an Iceberg [`Catalog`].
-///
-/// Acts as a centralized catalog provider that aggregates
-/// multiple [`SchemaProvider`], each associated with distinct namespaces.
+/// Exposes Iceberg namespaces as DataFusion schemas.
 #[derive(Debug)]
 pub struct IcebergCatalogProvider {
-    /// A `HashMap` where keys are namespace names
-    /// and values are dynamic references to objects implementing the
-    /// [`SchemaProvider`] trait.
-    schemas: HashMap<String, Arc<dyn SchemaProvider>>,
+    schemas: HashMap<String, Arc<IcebergSchemaProvider>>,
 }
 
 impl IcebergCatalogProvider {
-    /// Asynchronously tries to construct a new [`IcebergCatalogProvider`]
-    /// using the given client to fetch and initialize schema providers for
-    /// each namespace in the Iceberg [`Catalog`].
-    ///
-    /// This method retrieves the list of namespace names
-    /// attempts to create a schema provider for each namespace, and
-    /// collects these providers into a `HashMap`.
-    pub async fn try_new(client: Arc<dyn Catalog>) -> Result<Self> {
-        // TODO:
-        // Schemas and providers should be cached and evicted based on time
-        // As of right now; schemas might become stale.
-        let schema_names: Vec<_> = client
-            .list_namespaces(None)
-            .await
-            .map_err(to_datafusion_error)?
-            .iter()
-            .flat_map(|ns| ns.as_ref().clone())
-            .collect();
-
-        let providers = try_join_all(
-            schema_names
-                .iter()
-                .map(|name| {
-                    IcebergSchemaProvider::try_new(
-                        client.clone(),
-                        NamespaceIdent::new(name.clone()),
-                    )
-                })
-                .collect::<Vec<_>>(),
-        )
-        .await?;
+    /// Starts a live catalog builder with a REST client, in-memory catalog, 
or caching wrapper.
+    /// By default, building the provider discovers namespaces and table names.
+    pub fn from_catalog(catalog: Arc<dyn Catalog>) -> 
IcebergCatalogProviderBuilder {

Review Comment:
   `from_` methods typically return `Self` in Rust. For this one, I'd probably 
call it `builder_from_calalog`, or `catalog_builder` being consistent with the 
method below.



##########
crates/datafusion/src/catalog/schema.rs:
##########
@@ -20,83 +20,92 @@ use std::sync::Arc;
 use async_trait::async_trait;
 use dashmap::DashMap;
 use datafusion::catalog::SchemaProvider;
-use datafusion::common::{exec_datafusion_err, exec_err, plan_datafusion_err};
+use datafusion::common::{
+    config_err, exec_datafusion_err, exec_err, not_impl_datafusion_err,
+    plan_datafusion_err,
+};
 use datafusion::datasource::TableProvider;
 use datafusion::error::Result;
 use datafusion::execution::TaskContext;
 use datafusion::prelude::SessionContext;
 use futures::TryStreamExt;
-use futures::future::try_join_all;
 use iceberg::arrow::arrow_schema_to_schema_auto_assign_ids;
 use iceberg::inspect::MetadataTableType;
 use iceberg::spec::FormatVersion;
+use iceberg::table::Table;
 use iceberg::{Catalog, NamespaceIdent, TableCreation, TableIdent};
 
-use crate::table_provider::IcebergCatalogTableProvider;
+use crate::metadata::IcebergMetadataTableProvider;
+use crate::table_provider::IcebergTableProvider;
 use crate::to_datafusion_error;
 
-/// Represents a [`SchemaProvider`] for the Iceberg [`Catalog`], managing
-/// access to table providers within a specific namespace.
+/// An Iceberg namespace. Each live lookup loads current metadata from its 
catalog.
 #[derive(Debug)]
 pub(crate) struct IcebergSchemaProvider {
-    /// Reference to the Iceberg catalog
-    catalog: Arc<dyn Catalog>,
-    /// The namespace this schema represents
-    namespace: NamespaceIdent,
-    /// A concurrent map where keys are table names
-    /// and values are dynamic references to objects implementing the
-    /// [`TableProvider`] trait.
-    /// Wrapped in Arc to allow sharing across async boundaries in 
register_table.
-    tables: Arc<DashMap<String, Arc<IcebergCatalogTableProvider>>>,
+    pub(super) namespace: NamespaceIdent,
+    ddl_catalog: Option<Arc<dyn Catalog>>,
+    tables: Arc<DashMap<String, Registration>>,
+}
+
+#[derive(Clone, Debug)]
+enum Registration {
+    Live(Arc<dyn Catalog>),
+    Static(Arc<IcebergTableProvider>),

Review Comment:
   The structuring here is a bit weird:
   
   This claims to be a static registration, but it might actually be an 
`IcebergTableProvider` backed by a `Catalog` source, which I assume it's not 
static.
   
   Don't understand this well enough yet though, I'll keep reading.



##########
crates/datafusion/src/catalog/schema.rs:
##########
@@ -226,20 +245,19 @@ impl SchemaProvider for IcebergSchemaProvider {
         let result = tokio::task::spawn_blocking(move || {
             let rt = tokio::runtime::Handle::current();
             rt.block_on(async move {
-                let table_ident = TableIdent::new(namespace, 
table_name.clone());
-
-                // Drop the table from the Iceberg catalog
+                // Load the table before dropping it, to return its provider
+                let ident = TableIdent::new(namespace, table_name.clone());

Review Comment:
   Unrelated to this PR: we should really get rid of this blocking calls...



##########
crates/datafusion/src/catalog/schema.rs:
##########
@@ -20,83 +20,92 @@ use std::sync::Arc;
 use async_trait::async_trait;
 use dashmap::DashMap;
 use datafusion::catalog::SchemaProvider;
-use datafusion::common::{exec_datafusion_err, exec_err, plan_datafusion_err};
+use datafusion::common::{
+    config_err, exec_datafusion_err, exec_err, not_impl_datafusion_err,
+    plan_datafusion_err,
+};
 use datafusion::datasource::TableProvider;
 use datafusion::error::Result;
 use datafusion::execution::TaskContext;
 use datafusion::prelude::SessionContext;
 use futures::TryStreamExt;
-use futures::future::try_join_all;
 use iceberg::arrow::arrow_schema_to_schema_auto_assign_ids;
 use iceberg::inspect::MetadataTableType;
 use iceberg::spec::FormatVersion;
+use iceberg::table::Table;
 use iceberg::{Catalog, NamespaceIdent, TableCreation, TableIdent};
 
-use crate::table_provider::IcebergCatalogTableProvider;
+use crate::metadata::IcebergMetadataTableProvider;
+use crate::table_provider::IcebergTableProvider;
 use crate::to_datafusion_error;
 
-/// Represents a [`SchemaProvider`] for the Iceberg [`Catalog`], managing
-/// access to table providers within a specific namespace.
+/// An Iceberg namespace. Each live lookup loads current metadata from its 
catalog.
 #[derive(Debug)]
 pub(crate) struct IcebergSchemaProvider {
-    /// Reference to the Iceberg catalog
-    catalog: Arc<dyn Catalog>,
-    /// The namespace this schema represents
-    namespace: NamespaceIdent,
-    /// A concurrent map where keys are table names
-    /// and values are dynamic references to objects implementing the
-    /// [`TableProvider`] trait.
-    /// Wrapped in Arc to allow sharing across async boundaries in 
register_table.
-    tables: Arc<DashMap<String, Arc<IcebergCatalogTableProvider>>>,
+    pub(super) namespace: NamespaceIdent,
+    ddl_catalog: Option<Arc<dyn Catalog>>,
+    tables: Arc<DashMap<String, Registration>>,
+}
+
+#[derive(Clone, Debug)]
+enum Registration {
+    Live(Arc<dyn Catalog>),
+    Static(Arc<IcebergTableProvider>),

Review Comment:
   Ok, I see this is always `Static`, although not enforced at compile time. 
That's fine.



##########
crates/datafusion/src/catalog/schema.rs:
##########
@@ -127,28 +136,48 @@ impl SchemaProvider for IcebergSchemaProvider {
     }
 
     async fn table(&self, name: &str) -> Result<Option<Arc<dyn 
TableProvider>>> {
-        if let Some((table_name, metadata_table_name)) = name.split_once('$') {
-            let metadata_table_type = 
MetadataTableType::try_from(metadata_table_name)
-                .map_err(|e| plan_datafusion_err!("{e}"))?;
-            if let Some(table) = self.tables.get(table_name) {
-                let metadata_table = 
table.metadata_table(metadata_table_type).await?;
-                return Ok(Some(Arc::new(metadata_table)));
-            } else {
-                return Ok(None);
+        let (table_name, metadata_table_type) = match name.split_once('$') {

Review Comment:
   I see this was here before already, but that magic '$' could use some links 
to iceberg docs.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to