gabotechs commented on code in PR #49: URL: https://github.com/apache/datafusion-iceberg/pull/49#discussion_r4204795637
########## crates/datafusion/src/table_provider/static_table_provider.rs: ########## @@ -0,0 +1,345 @@ +// 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::sync::Arc; + +use async_trait::async_trait; +use datafusion::arrow::datatypes::SchemaRef as ArrowSchemaRef; +use datafusion::catalog::Session; +use datafusion::common::{exec_datafusion_err, not_impl_err}; +use datafusion::datasource::{TableProvider, TableType}; +use datafusion::error::Result; +use datafusion::logical_expr::dml::InsertOp; +use datafusion::logical_expr::{Expr, TableProviderFilterPushDown}; +use datafusion::physical_plan::ExecutionPlan; +use iceberg::arrow::schema_to_arrow_schema; +use iceberg::table::Table; + +use crate::error::to_datafusion_error; +use crate::read::scan::IcebergTableScan; + +/// Static table provider for read-only snapshot access. +/// +/// This provider holds a cached table instance and does not refresh metadata or support +/// write operations. Use this for consistent analytical queries, time-travel scenarios, +/// or when you want to avoid catalog overhead. +/// +/// For catalog-backed tables with write support and automatic refresh, use +/// [`IcebergTableProvider`](super::IcebergCatalogTableProvider) instead. +#[derive(Debug, Clone)] +pub struct IcebergStaticTableProvider { Review Comment: Factored out from [crates/datafusion/src/table/mod.rs](https://github.com/apache/datafusion-iceberg/pull/49/changes#diff-699df2f6cae6f75d8def93bb97cbd853dd022555744b1c3b36602495141495c0) untouched. ########## crates/datafusion/src/table_provider/catalog_table_provider.rs: ########## @@ -242,326 +228,29 @@ impl TableProvider for IcebergTableProvider { } } -/// Static table provider for read-only snapshot access. -/// -/// This provider holds a cached table instance and does not refresh metadata or support -/// write operations. Use this for consistent analytical queries, time-travel scenarios, -/// or when you want to avoid catalog overhead. -/// -/// For catalog-backed tables with write support and automatic refresh, use -/// [`IcebergTableProvider`] instead. -#[derive(Debug, Clone)] -pub struct IcebergStaticTableProvider { Review Comment: Moved to static_table_provider.rs untouched ########## crates/datafusion/src/table_provider/catalog_table_provider.rs: ########## @@ -242,326 +228,29 @@ impl TableProvider for IcebergTableProvider { } } -/// Static table provider for read-only snapshot access. -/// -/// This provider holds a cached table instance and does not refresh metadata or support -/// write operations. Use this for consistent analytical queries, time-travel scenarios, -/// or when you want to avoid catalog overhead. -/// -/// For catalog-backed tables with write support and automatic refresh, use -/// [`IcebergTableProvider`] instead. -#[derive(Debug, Clone)] -pub struct IcebergStaticTableProvider { - /// The static table instance (never refreshed) - table: Table, - /// Optional snapshot ID for this static view - snapshot_id: Option<i64>, - /// A reference-counted arrow `Schema` - schema: ArrowSchemaRef, -} - -impl IcebergStaticTableProvider { - /// Creates a static provider from a table instance. - /// - /// Uses the table's current snapshot for all queries. Does not support write operations. - pub async fn try_new_from_table(table: Table) -> Result<Self> { - let schema = Arc::new( - schema_to_arrow_schema(table.metadata().current_schema()) - .map_err(to_datafusion_error)?, - ); - Ok(IcebergStaticTableProvider { - table, - snapshot_id: None, - schema, - }) - } - - /// Creates a static provider for a specific table snapshot. - /// - /// Queries the specified snapshot for all operations. Useful for time-travel queries. - /// Does not support write operations. - pub async fn try_new_from_table_snapshot( - table: Table, - snapshot_id: i64, - ) -> Result<Self> { - let snapshot = table - .metadata() - .snapshot_by_id(snapshot_id) - .ok_or_else(|| { - exec_datafusion_err!( - "snapshot id {snapshot_id} not found in table {}", - table.identifier().name() - ) - })?; - let table_schema = snapshot - .schema(table.metadata()) - .map_err(to_datafusion_error)?; - let schema = - Arc::new(schema_to_arrow_schema(&table_schema).map_err(to_datafusion_error)?); - Ok(IcebergStaticTableProvider { - table, - snapshot_id: Some(snapshot_id), - schema, - }) - } - - /// The table as loaded when this provider was built. - pub fn table(&self) -> &Table { - &self.table - } - - /// The snapshot this provider reads, or `None` for the current snapshot of - /// [`Self::table`]. - pub fn snapshot_id(&self) -> Option<i64> { - self.snapshot_id - } -} - -#[async_trait] -impl TableProvider for IcebergStaticTableProvider { - fn schema(&self) -> ArrowSchemaRef { - self.schema.clone() - } - - fn table_type(&self) -> TableType { - TableType::Base - } - - async fn scan( - &self, - _state: &dyn Session, - projection: Option<&Vec<usize>>, - filters: &[Expr], - limit: Option<usize>, - ) -> Result<Arc<dyn ExecutionPlan>> { - // Use cached table (no refresh) - Ok(Arc::new(IcebergTableScan::new( - self.table.clone(), - self.snapshot_id, - self.schema.clone(), - projection, - filters, - limit, - )?)) - } - - fn supports_filters_pushdown( - &self, - filters: &[&Expr], - ) -> Result<Vec<TableProviderFilterPushDown>> { - // Push down all filters, as a single source of truth, the scanner will drop the filters which couldn't be push down - Ok(vec![TableProviderFilterPushDown::Inexact; filters.len()]) - } - - async fn insert_into( - &self, - _state: &dyn Session, - _input: Arc<dyn ExecutionPlan>, - _insert_op: InsertOp, - ) -> Result<Arc<dyn ExecutionPlan>> { - not_impl_err!( - "Write operations are not supported on IcebergStaticTableProvider. \ - Use IcebergTableProvider with a catalog for write support." - ) - } -} - #[cfg(test)] mod tests { use std::collections::HashMap; use std::sync::Arc; - use datafusion::common::Column; + use datafusion::datasource::TableProvider; use datafusion::error::DataFusionError; use datafusion::physical_plan::ExecutionPlan; use datafusion::prelude::SessionContext; - use iceberg::io::FileIO; use iceberg::memory::{MEMORY_CATALOG_WAREHOUSE, MemoryCatalogBuilder}; use iceberg::spec::{NestedField, PrimitiveType, Schema, Type}; - use iceberg::table::{StaticTable, Table}; - use iceberg::{Catalog, CatalogBuilder, NamespaceIdent, TableCreation, TableIdent}; + use iceberg::{CatalogBuilder, TableCreation}; use tempfile::TempDir; use super::*; - async fn get_test_table_from_metadata_file() -> Table { Review Comment: Moved to static_table_provider.rs untouched -- 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]
