xxchan commented on code in PR #879:
URL: https://github.com/apache/iceberg-rust/pull/879#discussion_r1907342718


##########
crates/integrations/datafusion/src/table/metadata_table.rs:
##########
@@ -0,0 +1,95 @@
+// 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::any::Any;
+use std::sync::Arc;
+
+use async_trait::async_trait;
+use datafusion::arrow::array::RecordBatch;
+use datafusion::arrow::datatypes::SchemaRef as ArrowSchemaRef;
+use datafusion::catalog::Session;
+use datafusion::datasource::{TableProvider, TableType};
+use datafusion::error::Result as DFResult;
+use datafusion::logical_expr::Expr;
+use datafusion::physical_plan::ExecutionPlan;
+use futures::StreamExt;
+use iceberg::inspect::MetadataTableType;
+use iceberg::table::Table;
+
+use crate::physical_plan::metadata_scan::IcebergMetadataScan;
+use crate::to_datafusion_error;
+
+/// Represents a [`TableProvider`] for the Iceberg [`Catalog`],
+/// managing access to a [`MetadataTable`].
+#[derive(Debug, Clone)]
+pub struct IcebergMetadataTableProvider {
+    pub(crate) table: Table,
+    pub(crate) r#type: MetadataTableType,
+}
+
+#[async_trait]
+impl TableProvider for IcebergMetadataTableProvider {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn schema(&self) -> ArrowSchemaRef {
+        let metadata_table = self.table.inspect();
+        match self.r#type {
+            MetadataTableType::Snapshots => 
metadata_table.snapshots().schema().into(),
+            MetadataTableType::Manifests => 
metadata_table.manifests().schema().into(),
+        }
+    }
+
+    fn table_type(&self) -> TableType {
+        TableType::Base
+    }
+
+    async fn scan(
+        &self,
+        _state: &dyn Session,
+        _projection: Option<&Vec<usize>>,
+        _filters: &[Expr],
+        _limit: Option<usize>,
+    ) -> DFResult<Arc<dyn ExecutionPlan>> {
+        Ok(Arc::new(IcebergMetadataScan::new(self.clone())))
+    }
+}
+
+impl IcebergMetadataTableProvider {
+    pub async fn scan(self) -> Vec<DFResult<RecordBatch>> {
+        let metadata_table = self.table.inspect();
+        let stream = match self.r#type {
+            MetadataTableType::Snapshots => 
metadata_table.snapshots().scan().await,
+            MetadataTableType::Manifests => 
metadata_table.manifests().scan().await,
+        };
+        let stream = match stream {
+            Ok(stream) => stream,
+            Err(e) => return vec![Err(to_datafusion_error(e))],
+        };
+        // It seems hard to
+        // convert Pin<Box<Stream<iceberg::Result<RecordBatch>>>
+        //      to Pin<Box<Stream<DFResult<RecordBatch>>>>
+        // So we just collect the record batches.
+        // Metadata tables should not have too many data so this should be 
fine.
+        let record_batches: Vec<DFResult<RecordBatch>> = stream
+            .map(|res| res.map_err(to_datafusion_error))
+            .collect::<Vec<_>>()
+            .await;

Review Comment:
   It's a little tricky to handle the ArrowRecordBatchStream introduced in #870 
🤔 



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to