Repository navigation
Conversation
…ercion (apache#27067) ## Which issue does this PR close? - Closes apache#27065. ## Rationale for this change Reading a Parquet file with a nested BINARY field (a list element or a struct field) through a table schema that declares that field as a string produces a string array that was never checked for valid UTF-8. When the bytes are not valid UTF-8, the query panics in a debug build. In a release build it returns an invalid `StringArray`, which is unsound for anything that reads the values as `&str`. On 55.2.0 the same query fails with `Encountered non UTF-8 data`. apache#25193 made the file-schema coercion recurse into nested types so that the view coercions (`Utf8` to `Utf8View`, `Binary` to `BinaryView`) reach nested fields (apache#25192). The binary-to-string coercion came along with it. That coercion hands the Parquet reader a string type, and the reader only validates UTF-8 for columns annotated `UTF8`, which a BINARY column is not. apache#25342 already kept map keys and values out of it for this reason. apache#25193 is also on `branch-56`, so this fix should be backported for 56.1.0. ## What changes are included in this PR? - `coerce_data_type` applies the binary-to-string coercion to top-level fields only, as before apache#25193. Struct fields and list-like children are coerced with it turned off, so nested binary fields stay binary in the file schema and are converted by the validating cast. The view coercions still apply at every level. - `coerce_child` loses its `binary_to_string` parameter, since every caller now passes `false`. - The module docs describe the rule for nested fields. The top-level case is unchanged: a top-level binary column read as a string still goes through the reader without validation, as it has for a long time. Fixing that needs the Parquet reader to validate whenever the target type is a string. That is apache/arrow-rs#11140, with a fix in review in apache/arrow-rs#11144. Skipping the coercion for top-level columns would lose statistics pruning for `binary_as_string` tables (apache#27068). `PruningPredicate` does not prune on nested fields, so this change loses no pruning. Once the reader validates, nested binary-to-string coercion is safe again, but lifting this restriction would change behavior for Comet. Comet replaces invalid UTF-8 in nested values with U+FFFD, which only works when they arrive as binary (apache/datafusion-comet#7469). So the restriction should stay until the reader can also replace invalid UTF-8 (apache/arrow-rs#12047). ## What is the testing strategy for this PR? - `nested_binary_read_as_string_rejects_invalid_utf8` in `datafusion/core/tests/parquet/schema_coercion.rs` scans a `list<binary>` file and a `struct<binary>` file holding invalid UTF-8 through a table schema that declares those fields as strings, and expects a `non UTF-8 data` error. Without the fix it panics in the Parquet reader with the error from apache#27065. - `nested_coercion_preserves_leaf_conversion_rules` now checks that nested view coercions still apply, and that nested binary-to-string coercions do not, for both struct and list children. ## Are there any user-facing changes? A query that reads invalid UTF-8 from a nested binary field as a string returns an error instead of panicking or producing invalid data, which restores the 55.2.0 behavior. Valid data reads as before, through the cast instead of the reader. No public API changes. --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 9232c06)
Codecov Report✅ All modified and coverable lines are covered by tests.
Flags with carried forward coverage won't be shown. Click here to find out more. @@ Coverage Diff @@
## branch-56 #27754 +/- ##
=============================================
- Coverage 83.30% 83.30% -0.01%
=============================================
Files 1170 1170
Lines 476209 476216 +7
Branches 476209 476216 +7
=============================================
+ Hits 396694 396699 +5
- Misses 55487 55488 +1
- Partials 24028 24029 +1
... and 6 files with indirect coverage changes 🚀 New features to boost your workflow:
|
viirya
left a comment
There was a problem hiding this comment.
Clean cherry-pick of #27067; the only differences from 9232c06 are import context lines, and #25193/#25342 are both in 56.0.0 and not in 55.2.0, so this is a 56-only regression worth backporting.
One thing to note for whoever merges: the commit message is inherited from the squash on main and still mentions nested_binary_read_as_string_rejects_invalid_utf8 in datafusion/core/tests/parquet/schema_coercion.rs. That test was later replaced by the parquet.slt cases, so it would be good to drop that bullet if this gets squash-merged with the commit body.
A small doc follow-up for main (not needed here, keeping the backport identical is better): see the inline note.
| //! This coercion therefore only applies to top-level fields, where it is | ||
| //! long-standing behaviour. Nested binary fields (struct fields, list elements | ||
| //! and map keys and values) are never read as strings: they keep going through | ||
| //! the validating cast instead. The view coercions still apply at every level. |
There was a problem hiding this comment.
These docs now say nested binary is never read as a string, but the apply_file_schema_type_coercions doc still says "The same coercions are applied recursively to nested types", and the coerce_map_entries doc still gives a map-specific reason that now applies to every nested field. Worth tidying both on main in a follow-up. Nothing to change in this backport.
Which issue does this PR close?
branch-56.Rationale for this change
#25193, which is in 56.0.0, made the Parquet file-schema coercion recurse into nested types. So a nested BINARY field that the table schema declares as a string is decoded straight into
Utf8. The reader validates UTF-8 only for columns with theUTF8annotation, so an invalid value becomes an unvalidatedStringArray: a panic in debug builds and unsound data in release builds. #27067 fixed this onmain. DataFusion Comet needs it in a 56.0.x release before it can upgrade (apache/datafusion-comet#7249).What changes are included in this PR?
A cherry-pick of #27067 (
9232c06) with-x. It applied cleanly.What is the testing strategy for this PR?
The
schema_coercionunit tests indatafusion-datasource-parquetandparquet.sltpass on this branch.Are there any user-facing changes?
Nested BINARY fields are no longer coerced to strings by the file-schema coercion, as on
main.🤖 Generated with Claude Code