codephage2020 commented on code in PR #7934:
URL: https://github.com/apache/arrow-rs/pull/7934#discussion_r2212012414


##########
parquet-variant/src/variant/object.rs:
##########
@@ -217,23 +217,31 @@ impl<'m, 'v> VariantObject<'m, 'v> {
                 self.header.field_ids_start_byte() as 
_..self.first_field_offset_byte as _,
             )?;
 
-            let field_ids = map_bytes_to_offsets(field_id_buffer, 
self.header.field_id_size)
-                .collect::<Vec<_>>();
-
+            let mut field_ids_iter =
+                map_bytes_to_offsets(field_id_buffer, 
self.header.field_id_size);
             // Validate all field ids exist in the metadata dictionary and the 
corresponding field names are lexicographically sorted
             if self.metadata.is_sorted() {
                 // Since the metadata dictionary has unique and sorted field 
names, we can also guarantee this object's field names
                 // are lexicographically sorted by their field id ordering
-                if !field_ids.is_sorted() {
-                    return Err(ArrowError::InvalidArgumentError(
-                        "field names not sorted".to_string(),
-                    ));
-                }
+                let dictionary_size = self.metadata.dictionary_size();
+
+                if let Some(mut current_id) = field_ids_iter.next() {
+                    for next_id in field_ids_iter {
+                        if current_id >= dictionary_size {
+                            return Err(ArrowError::InvalidArgumentError(
+                                "field id is not valid".to_string(),
+                            ));
+                        }
+
+                        if next_id <= current_id {
+                            return Err(ArrowError::InvalidArgumentError(
+                                "field names not sorted".to_string(),
+                            ));
+                        }
+                        current_id = next_id;
+                    }
 
-                // Since field ids are sorted, if the last field is smaller 
than the dictionary size,
-                // we also know all field ids are smaller than the dictionary 
size and in-bounds.
-                if let Some(&last_field_id) = field_ids.last() {
-                    if last_field_id >= self.metadata.dictionary_size() {
+                    if current_id >= dictionary_size {

Review Comment:
   Sorry, I didn't expect this to be confusing. This loop has considered many 
ways of writing, and in the end, I referred to 
https://github.com/apache/arrow-rs/blob/main/parquet-variant/src/variant/list.rs#L225-L232.
 I think this is an elegant implementation. Perhaps I need to add comments or 
use the following writing style to make the code more readable.
   ```rust
   let mut previous_id: Option<u32> = None;
   
   for field_id in field_ids_iter {
       if field_id >= dictionary_size {
           return Err(ArrowError::InvalidArgumentError(
               "field id is not valid".to_string(),
           ));
       }
       if let Some(prev_id) = previous_id {
           if field_id <= prev_id {
               return Err(ArrowError::InvalidArgumentError(
                   "field names not sorted".to_string(),
               ));
           }
       }
       
       previous_id = Some(field_id);
   }
   ```



-- 
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: github-unsubscr...@arrow.apache.org

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

Reply via email to