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            MetadataTableType::History => metadata_table.history().schema(),
53        };
54        schema_to_arrow_schema(&schema).unwrap().into()
55    }
56
57    fn table_type(&self) -> TableType {
58        TableType::Base
59    }
60
61    async fn scan(
62        &self,
63        _state: &dyn Session,
64        _projection: Option<&Vec<usize>>,
65        _filters: &[Expr],
66        _limit: Option<usize>,
67    ) -> DFResult<Arc<dyn ExecutionPlan>> {
68        Ok(Arc::new(IcebergMetadataScan::new(self.clone())))
69    }
70}
71
72impl IcebergMetadataTableProvider {
73    pub async fn scan(self) -> DFResult<BoxStream<'static, DFResult<RecordBatch>>> {
74        let metadata_table = self.table.inspect();
75        let stream = match self.r#type {
76            MetadataTableType::Snapshots => metadata_table.snapshots().scan().await,
77            MetadataTableType::Manifests => metadata_table.manifests().scan().await,
78            MetadataTableType::History => metadata_table.history().scan().await,
79        }
80        .map_err(to_datafusion_error)?;
81        let stream = stream.map_err(to_datafusion_error);
82        Ok(Box::pin(stream))
83    }
84}