gabotechs commented on code in PR #3000:
URL: https://github.com/apache/iceberg-rust/pull/3000#discussion_r3820831189


##########
crates/examples/src/datafusion_session_catalog.rs:
##########
@@ -0,0 +1,303 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Connects a session-aware Iceberg catalog to DataFusion.
+//!
+//! Run with:
+//!
+//! ```text
+//! cargo run -p iceberg-examples --example datafusion-session-catalog
+//! ```
+//!
+//! The adapter at the bottom only makes the example self-contained. 
Applications
+//! should pass their own `SessionCatalog` implementation to the provider.
+
+use std::collections::HashMap;
+use std::sync::Arc;
+
+use async_trait::async_trait;
+use datafusion::catalog::Session as DataFusionSession;
+use datafusion::error::{DataFusionError, Result as DataFusionResult};
+use datafusion::prelude::{SessionConfig, SessionContext as 
DataFusionSessionContext};
+use iceberg::memory::{MEMORY_CATALOG_WAREHOUSE, MemoryCatalog, 
MemoryCatalogBuilder};
+use iceberg::spec::{NestedField, PrimitiveType, Schema, Type};
+use iceberg::table::Table;
+use iceberg::{
+    Catalog, CatalogBuilder, Namespace, NamespaceIdent, Result, SessionCatalog,
+    SessionContext as IcebergSessionContext, TableCommit, TableCreation, 
TableIdent,
+};
+use iceberg_datafusion::{IcebergCatalogProvider, SessionContextResolver};
+
+/// User metadata stored as an application-specific DataFusion extension.
+#[derive(Debug)]
+struct UserContext {
+    name: String,
+}
+
+/// Maps the application's DataFusion user context to an Iceberg session.
+#[derive(Debug)]
+struct UserSessionContextResolver;
+
+impl SessionContextResolver for UserSessionContextResolver {
+    fn resolve(&self, session: &dyn DataFusionSession) -> 
DataFusionResult<IcebergSessionContext> {
+        let user = session
+            .config()
+            .get_extension::<UserContext>()
+            .ok_or_else(|| {
+                DataFusionError::Configuration(
+                    "the DataFusion session has no UserContext 
extension".to_string(),
+                )
+            })?;
+
+        Ok(IcebergSessionContext::builder()
+            // Reusing the DataFusion session ID gives the catalog a stable key
+            // for session-scoped caches.
+            .session_id(session.session_id().to_string())
+            .identity(user.name.to_string())
+            .build())
+    }
+}
+

Review Comment:
   I think there's an opportunity of reducing a lot of the convolution by using 
DataFusion `ConfigOption` extensions. For example:
   
   ```rust
   datafusion::common::extensions_options! {
       pub struct IcebergOptions {
           /// The [IcebergSessionContext] `identity` field.
           pub identity: Option<String>, default = None
       }
   }
   ```
   
   With this, we could automatically map `IcebergOptions` to the relevant 
fields of `IcebergSessionContext` inside this project, without exposing this 
detail to users.
   
   From a public API standpoint, this crate would just offer this 
`IcebergOptions` as a native DataFusion `ConfigOptions` implementation, and 
under the hood this can be wired up internally to an `IcebergSessionContext`.
   
   This is the most DataFusion native way of threading custom config across the 
callstack.



##########
crates/integrations/datafusion/src/table/mod.rs:
##########
@@ -80,18 +83,21 @@ impl IcebergTableProvider {
     /// Loads the table once to get the initial schema, then stores the catalog
     /// reference for future metadata refreshes on each operation.
     pub(crate) async fn try_new(
-        catalog: Arc<dyn Catalog>,
+        catalog_access: CatalogAccess,

Review Comment:
   I think we can afford to leave this as a normal `Arc<dyn SessionCatalog>`, 
and just wrap it in the appropriate places with a `SessionBoundCatalog` 
implementation that automatically enriches the `IcebergSessionContext` under 
the hood based on whatever is present in the `DataFusionSessionConfig`.



-- 
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