CTTY commented on code in PR #3000: URL: https://github.com/apache/iceberg-rust/pull/3000#discussion_r3799485096
########## crates/integrations/datafusion/src/catalog_access.rs: ########## @@ -0,0 +1,192 @@ +// 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. + +use std::collections::HashMap; +use std::sync::Arc; + +use async_trait::async_trait; +use datafusion::catalog::Session; +use datafusion::error::Result as DFResult; +use iceberg::table::Table; +use iceberg::{ + Catalog, Namespace, NamespaceIdent, Result, SessionCatalog, SessionContext, TableCommit, + TableCreation, TableIdent, +}; + +use crate::SessionContextResolver; + +/// Describes how the DataFusion integration accesses an Iceberg catalog. +/// +/// A catalog can either be accessed directly through [`Catalog`] or through a +/// [`SessionCatalog`] bound to an Iceberg [`SessionContext`]. Operations that +/// receive a DataFusion session resolve and bind its context once. Operations +/// for which DataFusion provides no session reuse one anonymous fallback +/// context so session-scoped catalog state remains stable across those calls. +#[derive(Clone, Debug)] +pub(crate) enum CatalogAccess { + /// A catalog accessed directly through the [`Catalog`] API. + Direct(Arc<dyn Catalog>), + + /// A session-aware catalog, its resolver, and the stable context used by + /// DataFusion APIs that do not expose a session. + SessionAware { + catalog: Arc<dyn SessionCatalog>, + resolver: Arc<dyn SessionContextResolver>, + fallback_context: SessionContext, + }, +} + +impl CatalogAccess { + pub(crate) fn with_session(&self, session: &dyn Session) -> DFResult<Arc<dyn Catalog>> { Review Comment: The current design looks correct function wise, but I have some concerns and maybe we can improve this: - We don't know when to call `with_session` and when to call `without_session`, my expectation is iceberg always tries to resolve the session while we have the query context, and the result depends on whether the underlying catalog type is a session catalog. This way we don't need to call something like "without_session" when we don't have the query context (e.g. IcebergTableProvider) - There are many abstractions hanging around and it's hard for users to figure out what to implement. `SessionContextResolver` feels like something that should come with `SessionCatalog` implementation or extension. Having two of them in parallel in APIs like below will shift the responsibility of resolving df session to users. ```rust pub async fn try_new_with_session_catalog( catalog: Arc<dyn SessionCatalog>, resolver: Arc<dyn SessionContextResolver>, ) ``` - Do we plan to have default implementations for SessionContextResolver? How do users use RestSessionCatalog out of the box? ########## crates/integrations/datafusion/src/lib.rs: ########## @@ -15,16 +15,36 @@ // specific language governing permissions and limitations // under the License. -mod catalog; -pub use catalog::*; - mod error; pub use error::*; +mod catalog_access; +mod catalog_provider; +pub use catalog_provider::*; pub mod physical_plan; -mod schema; +mod schema_provider; pub mod table; pub use table::table_provider_factory::IcebergTableProviderFactory; pub use table::*; pub(crate) mod task_writer; + +use std::fmt; + +use datafusion::catalog::Session as DFSession; +use datafusion::error::Result as DFResult; +use iceberg::SessionContext; + +/// Resolves an Iceberg [`SessionContext`] from a DataFusion session. +/// +/// The DataFusion integration calls the resolver once while planning each +/// session-aware scan or insert. The returned context is bound to that +/// operation and, for inserts, is retained through transaction commit. +/// Implementations should therefore return a stable Iceberg session identity +/// for repeated operations from the same DataFusion session. +pub trait SessionContextResolver: fmt::Debug + Send + Sync { Review Comment: I'm not sure I understand why do we need to have a separate trait to customize this. I thought the ability to resolve Datafusion context should be an extension of SessionCatalog. -- 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]
