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


##########
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:
   Sorry, I forgot to better explain this in the PR description.
   
   There's really two changes in this PR that I was thinking of contributing as 
[separate PRs](https://github.com/apache/iceberg-rust/pull/2999) but ended up 
including here in separate commits instead.
   
   1. integrate the `SessionCatalog` with the `Iceberg{Catalog, Schema, 
Table}Provider` implementations (first [set of 
commits](https://github.com/apache/iceberg-rust/pull/3000/changes/1a8714d159f6f7d2ba3c8f33a1935770e1844750..34679090fdb5670dec2f1b83bd072d4a3c94e20a))
   2. add a `SessionBoundCatalog` to allow passing a `SessionCatalog` to the 
[`IcebergCommitExec`](https://github.com/apache/iceberg-rust/blob/e486a8b18eab37e34c4113e4260b0f7ca94b1f61/crates/integrations/datafusion/src/physical_plan/commit.rs#L43)
 and use it across the providers to save lines
   
   The total diff obscures this. On the one hand, the addition of the 
`SessionBoundCatalog` is [necessary to commit an 
insert](https://github.com/apache/iceberg-rust/blob/e486a8b18eab37e34c4113e4260b0f7ca94b1f61/crates/integrations/datafusion/src/physical_plan/commit.rs#L250),
 but on the other, its been extended as a convenience to reduce repetetive 
statements [like 
this](https://github.com/DerGut/iceberg-rust/blob/34679090fdb5670dec2f1b83bd072d4a3c94e20a/crates/integrations/datafusion/src/table/mod.rs#L93):
   ```rust
   let table = match &catalog {
               CatalogAccess::Direct(catalog) => 
catalog.load_table(&table_ident).await?,
               CatalogAccess::SessionAware {
                   catalog,
                   resolver: _,
                   fallback_context,
               } => catalog.load_table(&fallback_context, &table_ident).await?,
           };
   ```
   into something like
   
   ```rust
   let table = catalog_access
               .without_session()
               .load_table(&table_ident)
               .await?;
   ```
   
   > We don't know when to call with_session and when to call without_session
   
   The API is essentially: whenever we have a `datafusion::catalog::Session` in 
scope, use `CatalogAccess:with_session`, if not, use 
`CatalogAccess::without_session`. But the match statement above would be 
equivalent.



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