liurenjie1024 commented on code in PR #79:
URL: https://github.com/apache/iceberg-rust/pull/79#discussion_r1423579372


##########
crates/iceberg/src/spec/manifest.rs:
##########
@@ -0,0 +1,1847 @@
+// 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.
+
+//! Manifest for Iceberg.
+use self::_const_schema::{manifest_schema_v1, manifest_schema_v2};
+
+use super::Literal;
+use super::{
+    FieldSummary, FormatVersion, ManifestContentType, ManifestListEntry, 
PartitionSpec, Schema,
+    Struct, UNASSIGNED_SEQ_NUMBER,
+};
+use crate::io::OutputFile;
+use crate::spec::PartitionField;
+use crate::{Error, ErrorKind};
+use apache_avro::{from_value, to_value, Reader as AvroReader, Writer as 
AvroWriter};
+use futures::AsyncWriteExt;
+use serde_json::to_vec;
+use std::cmp::min;
+use std::collections::HashMap;
+use std::str::FromStr;
+
+/// A manifest contains metadata and a list of entries.
+#[derive(Debug, PartialEq, Eq, Clone)]
+pub struct Manifest {
+    metadata: ManifestMetadata,
+    entries: Vec<ManifestEntry>,
+}
+
+impl Manifest {
+    /// Parse manifest from bytes of avro file.
+    pub fn parse_avro(bs: &[u8]) -> Result<Self, Error> {
+        let reader = AvroReader::new(bs)?;
+
+        // Parse manifest metadata
+        let meta = reader.user_metadata();
+        let metadata = ManifestMetadata::parse(meta)?;
+
+        // Parse manifest entries
+        let partition_type = 
metadata.partition_spec.partition_type(&metadata.schema)?;
+
+        let entries = match metadata.format_version {
+            FormatVersion::V1 => {
+                let schema = manifest_schema_v1(partition_type.clone())?;
+                let reader = AvroReader::with_schema(&schema, bs)?;
+                reader
+                    .into_iter()
+                    .map(|value| {
+                        from_value::<_serde::ManifestEntryV1>(&value?)?
+                            .try_into(&partition_type, &metadata.schema)
+                    })
+                    .collect::<Result<Vec<_>, Error>>()?
+            }
+            FormatVersion::V2 => {
+                let schema = manifest_schema_v2(partition_type.clone())?;
+                let reader = AvroReader::with_schema(&schema, bs)?;
+                reader
+                    .into_iter()
+                    .map(|value| {
+                        from_value::<_serde::ManifestEntryV2>(&value?)?
+                            .try_into(&partition_type, &metadata.schema)
+                    })
+                    .collect::<Result<Vec<_>, Error>>()?
+            }
+        };
+
+        Ok(Manifest { metadata, entries })
+    }
+}
+
+/// A manifest writer.
+pub struct ManifestWriter {
+    output: OutputFile,
+
+    snapshot_id: i64,
+
+    added_files: i32,
+    added_rows: i64,
+    existing_files: i32,
+    existing_rows: i64,
+    deleted_files: i32,
+    deleted_rows: i64,
+
+    seq_num: i64,
+    min_seq_num: Option<i64>,
+
+    key_metadata: Option<Vec<u8>>,
+
+    field_summary: HashMap<i32, FieldSummary>,
+}
+
+impl ManifestWriter {
+    /// Create a new manifest writer.
+    pub fn new(
+        output: OutputFile,
+        snapshot_id: i64,
+        seq_num: i64,
+        key_metadata: Option<Vec<u8>>,
+    ) -> Self {
+        Self {
+            output,
+            snapshot_id,
+            added_files: 0,
+            added_rows: 0,
+            existing_files: 0,
+            existing_rows: 0,
+            deleted_files: 0,
+            deleted_rows: 0,
+            seq_num,
+            min_seq_num: None,
+            key_metadata,
+            field_summary: HashMap::new(),
+        }
+    }
+
+    fn update_field_summary(&mut self, entry: &ManifestEntry) {
+        // Update field summary
+        if let Some(null) = &entry.data_file.null_value_counts {
+            for (&k, &v) in null {
+                let field_summary = self.field_summary.entry(k).or_default();
+                if v > 0 {
+                    field_summary.contains_null = true;
+                }
+            }
+        }
+        if let Some(nan) = &entry.data_file.nan_value_counts {
+            for (&k, &v) in nan {
+                let field_summary = self.field_summary.entry(k).or_default();
+                assert!(v >= 0);

Review Comment:
   If this check failed, e.g. some bug happens, the program will crash. So I 
would prefer to make this unsigned value type so that we can avoid such checks 
as much as possible.



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