Skip to content

[branch-56] fix: keep nested Parquet binary fields out of the binary-to-string coercion (backport #27067) - #27754

Open
andygrove wants to merge 1 commit into
apache:branch-56from
andygrove:backport-27067-branch-56
Open

andygrove wants to merge 1 commit into
apache:branch-56from
andygrove:backport-27067-branch-56

Conversation

@andygrove

Copy link
Copy Markdown
Member

Which issue does this PR close?

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 the UTF8 annotation, so an invalid value becomes an unvalidated StringArray: a panic in debug builds and unsound data in release builds. #27067 fixed this on main. 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_coercion unit tests in datafusion-datasource-parquet and parquet.slt pass 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

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

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.30%. Comparing base (69d40d1) to head (07ad504).
⚠️ Report is 1 commits behind head on branch-56.

Flag Coverage Δ
all-tests 83.30% <100.00%> (+<0.01%) ⬆️
sqllogictests 60.85% <71.42%> (-0.01%) ⬇️

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     
Files with missing lines Coverage Δ
...tafusion/datasource-parquet/src/schema_coercion.rs 98.36% <100.00%> (-0.14%) ⬇️

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@xudong963 xudong963 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @andygrove

@viirya viirya left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +19 to +22
//! 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants