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


##########
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:
   Going from simple to complex is a good point. Let me adapt this PR to expect 
an `Iceberg{Config|Options}` from the session config 👍 



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