Skip to main content

iceberg_datafusion/table/
metadata_table.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::sync::Arc;
19
20use async_trait::async_trait;
21use datafusion::arrow::array::RecordBatch;
22use datafusion::arrow::datatypes::SchemaRef as ArrowSchemaRef;
23use datafusion::catalog::Session;
24use datafusion::datasource::{TableProvider, TableType};
25use datafusion::error::Result as DFResult;
26use datafusion::logical_expr::Expr;
27use datafusion::physical_plan::ExecutionPlan;
28use futures::TryStreamExt;
29use futures::stream::BoxStream;
30use iceberg::arrow::schema_to_arrow_schema;
31use iceberg::inspect::MetadataTableType;
32use iceberg::table::Table;
33
34use crate::physical_plan::metadata_scan::IcebergMetadataScan;
35use crate::to_datafusion_error;
36
37/// Represents a [`TableProvider`] for the Iceberg [`Catalog`],
38/// managing access to a [`MetadataTable`].
39#[derive(Debug, Clone)]
40pub struct IcebergMetadataTableProvider {
41    pub(crate) table: Table,
42    pub(crate) r#type: MetadataTableType,
43}
44
45#[async_trait]
46impl TableProvider for IcebergMetadataTableProvider {
47    fn schema(&self) -> ArrowSchemaRef {
48        let metadata_table = self.table.inspect();
49        let schema = match self.r#type {
50            MetadataTableType::Snapshots => metadata_table.snapshots().schema(),
51            MetadataTableType::Manifests => metadata_table.manifests().schema(),
52        };
53        schema_to_arrow_schema(&schema).unwrap().into()
54    }
55
56    fn table_type(&self) -> TableType {
57        TableType::Base
58    }
59
60    async fn scan(
61        &self,
62        _state: &dyn Session,
63        _projection: Option<&Vec<usize>>,
64        _filters: &[Expr],
65        _limit: Option<usize>,
66    ) -> DFResult<Arc<dyn ExecutionPlan>> {
67        Ok(Arc::new(IcebergMetadataScan::new(self.clone())))
68    }
69}
70
71impl IcebergMetadataTableProvider {
72    pub async fn scan(self) -> DFResult<BoxStream<'static, DFResult<RecordBatch>>> {
73        let metadata_table = self.table.inspect();
74        let stream = match self.r#type {
75            MetadataTableType::Snapshots => metadata_table.snapshots().scan().await,
76            MetadataTableType::Manifests => metadata_table.manifests().scan().await,
77        }
78        .map_err(to_datafusion_error)?;
79        let stream = stream.map_err(to_datafusion_error);
80        Ok(Box::pin(stream))
81    }
82}