Weijun-H commented on code in PR #9086:
URL: https://github.com/apache/arrow-rs/pull/9086#discussion_r2678964622


##########
arrow-json/src/reader/struct_array.rs:
##########
@@ -38,131 +41,171 @@ impl StructArrayDecoder {
         is_nullable: bool,
         struct_mode: StructMode,
     ) -> Result<Self, ArrowError> {
-        let decoders = struct_fields(&data_type)
-            .iter()
-            .map(|f| {
-                // If this struct nullable, need to permit nullability in 
child array
-                // StructArrayDecoder::decode verifies that if the child is 
not nullable
-                // it doesn't contain any nulls not masked by its parent
-                let nullable = f.is_nullable() || is_nullable;
-                make_decoder(
-                    f.data_type().clone(),
-                    coerce_primitive,
-                    strict_mode,
-                    nullable,
-                    struct_mode,
-                )
-            })
-            .collect::<Result<Vec<_>, ArrowError>>()?;
+        let (decoders, field_name_to_index) = {
+            let fields = struct_fields(&data_type);
+            let decoders = fields
+                .iter()
+                .map(|f| {
+                    // If this struct nullable, need to permit nullability in 
child array
+                    // StructArrayDecoder::decode verifies that if the child 
is not nullable
+                    // it doesn't contain any nulls not masked by its parent
+                    let nullable = f.is_nullable() || is_nullable;
+                    make_decoder(
+                        f.data_type().clone(),
+                        coerce_primitive,
+                        strict_mode,
+                        nullable,
+                        struct_mode,
+                    )
+                })
+                .collect::<Result<Vec<_>, ArrowError>>()?;
+            let field_name_to_index = if struct_mode == StructMode::ObjectOnly 
{
+                build_field_index(fields)
+            } else {
+                None
+            };
+            (decoders, field_name_to_index)
+        };
 
         Ok(Self {
             data_type,
             decoders,
             strict_mode,
             is_nullable,
             struct_mode,
+            field_name_to_index,
+            child_pos: Vec::new(),
         })
     }
 }
 
 impl ArrayDecoder for StructArrayDecoder {
     fn decode(&mut self, tape: &Tape<'_>, pos: &[u32]) -> Result<ArrayData, 
ArrowError> {
         let fields = struct_fields(&self.data_type);
-        let mut child_pos: Vec<_> = (0..fields.len()).map(|_| vec![0; 
pos.len()]).collect();
-
+        let row_count = pos.len();
+        let field_count = fields.len();
+        let total_len = field_count.checked_mul(row_count).ok_or_else(|| {
+            ArrowError::JsonError(format!(
+                "StructArrayDecoder child position buffer size overflow for 
rows={row_count} fields={field_count}"
+            ))
+        })?;
+        if total_len > self.child_pos.len() {
+            self.child_pos
+                .try_reserve(total_len - self.child_pos.len())
+                .map_err(|_| {
+                    ArrowError::JsonError(format!(
+                        "StructArrayDecoder child position buffer allocation 
failed for rows={row_count} fields={field_count}"
+                    ))
+                })?;
+        }
+        self.child_pos.resize(total_len, 0);

Review Comment:
   addressed in. 
https://github.com/apache/arrow-rs/pull/9086/commits/df9e710136015ead727c350d074a423a83478b5b



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to