DerGut commented on code in PR #3000: URL: https://github.com/apache/iceberg-rust/pull/3000#discussion_r3821091012
########## 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: That would indeed help the complexity of the integration implementation a lot. But this also means that users now have to set Iceberg-specific extension options in addition to their possibly already existing custom extension options, right? Definitely good to know that this is the DataFusion-way of doing things, this is exactly the context I was missing, thanks! 🙇 -- 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]
