mardgez13 opened a new pull request, #54306:
URL: https://github.com/apache/arrow/pull/54306

   ### Rationale for this change
   
   `StreamReader` and `StreamWriter` only support flat schemas, but their 
constructors assumed it without checking: they looped over 
`schema->num_columns()` (leaf columns) and took `group_node->field(i)` (root 
fields), static-casting each to `PrimitiveNode`. With a nested schema this read 
past the end of the root group's field vector, or cast a `GroupNode` to 
`PrimitiveNode`. For `StreamReader` the schema comes from the file being read, 
so a file with a nested schema crashed the reader instead of raising 
`ParquetException`.
   
   ### What changes are included in this PR?
   
   - `StreamReader` and `StreamWriter` constructors iterate over the fields of 
the root group node and throw `ParquetException` if any of them is not a 
primitive node. Once every root field is primitive, the number of root fields 
equals the number of leaf columns, so the rest of the classes is unchanged.
   - The constructor docs, the class docs and `docs/source/cpp/parquet.rst` 
mention that nested schemas are not supported.
   
   ### Are these changes tested?
   
   Yes. `StreamReaderNestedSchema.ThrowsOnConstruction` and 
`StreamWriterNestedSchema.ThrowsOnConstruction` cover a root holding a group of 
two leaves (more leaf columns than root fields) and a root holding a group of 
one leaf plus a leaf (as many leaf columns as root fields, but one root field 
is a group). Without the fix, the first case is a heap-buffer-overflow under 
ASan and the second does not throw. `parquet-reader-test` and 
`parquet-writer-test` pass in an ASan/UBSan debug build.
   
   ### Are there any user-facing changes?
   
   The constructors now throw `ParquetException` for a schema with nested 
(group) fields. Such schemas were never supported; a schema whose only group 
fields were empty and came after the primitive fields happened to work before 
and is now rejected too.
   
   **This PR contains a "Critical Fix".** `StreamReader` read out of bounds 
when constructed on a Parquet file with a nested schema.
   
   ### Was AI used for this PR?
   
   In accordance to the [AI generation 
guidelines](https://arrow.apache.org/docs/dev/developers/overview.html#ai-generated-code),
 please disclose below whether and how AI was used in this PR.
   
   **PR code and description written by:**
   
   - [ ] Human
   - [x] AI
   
   **Reviewed before submission by:**
   
   - [ ] Human
   - [x] AI
   - [ ] Not reviewed
   
   An AI coding assistant found the issue, wrote the fix, the regression test 
and this description, and ran the tests; a separate AI pass reviewed the diff.
   
   * GitHub Issue: #54305
   


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