geruh commented on code in PR #863:
URL: https://github.com/apache/iceberg-rust/pull/863#discussion_r2099144298


##########
crates/iceberg/src/inspect/entries.rs:
##########
@@ -0,0 +1,603 @@
+// 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::type_name;
+use std::string::ToString;
+use std::sync::Arc;
+
+use arrow_array::builder::{
+    ArrayBuilder, BooleanBuilder, Date32Builder, Decimal128Builder, 
FixedSizeBinaryBuilder,
+    Float32Builder, Float64Builder, Int32Builder, Int64Builder, 
LargeBinaryBuilder, ListBuilder,
+    MapBuilder, PrimitiveBuilder, StringBuilder, StructBuilder, 
TimestampMicrosecondBuilder,
+    TimestampNanosecondBuilder,
+};
+use arrow_array::types::{Int32Type, Int64Type};
+use arrow_array::{ArrowPrimitiveType, RecordBatch, StructArray};
+use arrow_schema::{DataType, Fields, TimeUnit};
+use async_stream::try_stream;
+use futures::StreamExt;
+use itertools::Itertools;
+use ordered_float::OrderedFloat;
+
+use crate::arrow::{schema_to_arrow_schema, type_to_arrow_type};
+use crate::inspect::metrics::ReadableMetricsStructBuilder;
+use crate::scan::ArrowRecordBatchStream;
+use crate::spec::{
+    DataFile, Datum, ManifestFile, NestedFieldRef, PrimitiveLiteral, Schema, 
Struct, TableMetadata,
+    Type,
+};
+use crate::table::Table;
+use crate::{Error, ErrorKind, Result};
+
+/// Entries table containing the entries of the current snapshot's manifest 
files.
+///
+/// The table has one row for each manifest file entry in the current 
snapshot's manifest list file.
+/// For reference, see the Java implementation of [`ManifestEntry`][1].
+///
+/// [1]: 
https://github.com/apache/iceberg/blob/apache-iceberg-1.7.1/core/src/main/java/org/apache/iceberg/ManifestEntry.java
+pub struct EntriesTable<'a> {
+    table: &'a Table,
+}
+
+impl<'a> EntriesTable<'a> {
+    /// Create a new Entries table instance.
+    pub fn new(table: &'a Table) -> Self {
+        Self { table }
+    }
+
+    /// Scan the manifest entries table.
+    pub async fn scan(&self) -> Result<ArrowRecordBatchStream> {
+        let current_snapshot = 
self.table.metadata().current_snapshot().ok_or_else(|| {
+            Error::new(
+                ErrorKind::Unexpected,
+                "Cannot scan entries for table without current snapshot",
+            )
+        })?;
+
+        let manifest_list = current_snapshot
+            .load_manifest_list(self.table.file_io(), self.table.metadata())
+            .await?;
+
+        // Copy to ensure that the stream can take ownership of these 
dependencies
+        let schema = self.schema();
+        let arrow_schema = Arc::new(schema_to_arrow_schema(&schema)?);
+        let table_metadata = self.table.metadata_ref();
+        let file_io = Arc::new(self.table.file_io().clone());
+        let readable_metrics_schema = schema
+            .field_by_name("readable_metrics")
+            .and_then(|field| field.field_type.clone().to_struct_type())
+            .unwrap();
+
+        Ok(try_stream! {
+            for manifest_file in manifest_list.entries() {
+                let mut status = Int32Builder::new();
+                let mut snapshot_id = Int64Builder::new();
+                let mut sequence_number = Int64Builder::new();
+                let mut file_sequence_number = Int64Builder::new();
+                let mut data_file = 
DataFileStructBuilder::new(&table_metadata)?;
+                let mut readable_metrics =
+                    ReadableMetricsStructBuilder::new(
+                    table_metadata.current_schema(), 
&readable_metrics_schema)?;
+
+                for manifest_entry in 
manifest_file.load_manifest(&file_io).await?.entries() {
+                    status.append_value(manifest_entry.status() as i32);
+                    snapshot_id.append_option(manifest_entry.snapshot_id());
+                    
sequence_number.append_option(manifest_entry.sequence_number());
+                    
file_sequence_number.append_option(manifest_entry.file_sequence_number());
+                    data_file.append(manifest_file, 
manifest_entry.data_file())?;
+                    readable_metrics.append(manifest_entry.data_file())?;
+                }
+
+                let batch = RecordBatch::try_new(arrow_schema.clone(), vec![
+                    Arc::new(status.finish()),
+                    Arc::new(snapshot_id.finish()),
+                    Arc::new(sequence_number.finish()),
+                    Arc::new(file_sequence_number.finish()),
+                    Arc::new(data_file.finish()),
+                    Arc::new(readable_metrics.finish()),
+                ])?;
+
+                yield batch;
+            }
+        }
+        .boxed())
+    }
+
+    /// Get the schema for the manifest entries table.
+    pub fn schema(&self) -> Schema {
+        let partition_type = 
crate::spec::partition_type(self.table.metadata()).unwrap();

Review Comment:
   Do we also want to ignore the partition field if it's unpartitioned here?
   
   
https://github.com/apache/iceberg/blob/7dbdfd33a667a721fbb21c7c7d06fec9daa30b88/core/src/main/java/org/apache/iceberg/BaseEntriesTable.java#L53



-- 
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