NoahKusaba commented on code in PR #2638: URL: https://github.com/apache/datafusion-ballista/pull/2638#discussion_r4432252134
########## integrations/iceberg/src/catalog.rs: ########## @@ -0,0 +1,204 @@ +// 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. + +//! Pins catalog-backed tables to one version while a query is planned. + +use std::sync::Arc; + +use async_trait::async_trait; +use datafusion::arrow::datatypes::SchemaRef; +use datafusion::catalog::{ + CatalogProvider, CatalogProviderList, SchemaProvider, Session, TableProvider, +}; +use datafusion::common::{Result, not_impl_err}; +use datafusion::datasource::TableType; +use datafusion::datasource::source::DataSourceExec; +use datafusion::logical_expr::dml::InsertOp; +use datafusion::logical_expr::{Expr, TableProviderFilterPushDown}; +use datafusion::physical_plan::ExecutionPlan; +use iceberg::table::Table; +use iceberg_datafusion::read::IcebergDataSource; +use iceberg_datafusion::{IcebergCatalogTableProvider, to_datafusion_error}; + +/// A session's catalogs, serving each catalog-backed Iceberg table as a +/// [`PinnedTable`]. +/// +/// DataFusion looks up each table a query names once, while planning it, so +/// every scan of a table in the query reads the same version, and encoding the +/// plan needs no I/O. +#[derive(Debug)] +pub(crate) struct PinningCatalogList(pub(crate) Arc<dyn CatalogProviderList>); + +impl CatalogProviderList for PinningCatalogList { + fn register_catalog( + &self, + name: String, + catalog: Arc<dyn CatalogProvider>, + ) -> Option<Arc<dyn CatalogProvider>> { + self.0.register_catalog(name, catalog) + } + + fn catalog_names(&self) -> Vec<String> { + self.0.catalog_names() + } + + fn catalog(&self, name: &str) -> Option<Arc<dyn CatalogProvider>> { + let catalog = self.0.catalog(name)?; + Some(Arc::new(PinningCatalog(catalog))) + } +} + +#[derive(Debug)] +struct PinningCatalog(Arc<dyn CatalogProvider>); + +impl CatalogProvider for PinningCatalog { + fn schema_names(&self) -> Vec<String> { + self.0.schema_names() + } + + fn schema(&self, name: &str) -> Option<Arc<dyn SchemaProvider>> { + let schema = self.0.schema(name)?; + Some(Arc::new(PinningSchema(schema))) + } + + fn register_schema( + &self, + name: &str, + schema: Arc<dyn SchemaProvider>, + ) -> Result<Option<Arc<dyn SchemaProvider>>> { + self.0.register_schema(name, schema) + } + + fn deregister_schema( + &self, + name: &str, + cascade: bool, + ) -> Result<Option<Arc<dyn SchemaProvider>>> { + self.0.deregister_schema(name, cascade) + } +} + +#[derive(Debug)] +struct PinningSchema(Arc<dyn SchemaProvider>); + +#[async_trait] +impl SchemaProvider for PinningSchema { + fn owner_name(&self) -> Option<&str> { + self.0.owner_name() + } + + fn table_names(&self) -> Vec<String> { + self.0.table_names() + } + + async fn table(&self, name: &str) -> Result<Option<Arc<dyn TableProvider>>> { Review Comment: Opened #2726 to track this. -- 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]
