From 07ad5041b711f38d6f7add08cc07b204c5ab8be9 Mon Sep 17 00:00:00 2001 From: Andy Grove Date: Sun, 22 Nov 2026 15:22:06 +0000 Subject: [PATCH] fix: keep nested Parquet binary fields out of the binary-to-string coercion (#27067) ## Which issue does this PR close? - Closes #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`. #25193 made the file-schema coercion recurse into nested types so that the view coercions (`Utf8` to `Utf8View`, `Binary` to `BinaryView`) reach nested fields (#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. #25342 already kept map keys and values out of it for this reason. #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 #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 (#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` file and a `struct` 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 #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 (cherry picked from commit 9232c06fcb67604ad199c44c75c8cee100a856fd) --- .../datasource-parquet/src/schema_coercion.rs | 65 ++++++++++++------- .../sqllogictest/test_files/parquet.slt | 59 +++++++++++++++++ 2 files changed, 99 insertions(+), 25 deletions(-) diff --git a/datafusion/datasource-parquet/src/schema_coercion.rs b/datafusion/datasource-parquet/src/schema_coercion.rs index 43816a9289ba5..e77e2e653265b 100644 --- a/datafusion/datasource-parquet/src/schema_coercion.rs +++ b/datafusion/datasource-parquet/src/schema_coercion.rs @@ -16,9 +16,10 @@ //! build the string array itself. It only validates UTF-8 for columns carrying //! the `UTF8` logical annotation, which such a column by definition does not, //! so invalid bytes surface as an invalid string array rather than an error. -//! That is long-standing behaviour for top-level and struct fields; it is not -//! extended to map children, whose positional matching is newer, so binary map -//! values keep going through the validating cast instead. +//! 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. use std::cell::RefCell; use std::collections::{HashMap, HashSet}; @@ -179,28 +180,25 @@ fn coerce_data_type( (BinaryView, Binary | LargeBinary) => Some(BinaryView), // Struct children match by name (Struct(table_fields), Struct(file_fields)) => { - coerce_fields_by_name(table_fields, file_fields, binary_to_string, enable_rle) + coerce_fields_by_name(table_fields, file_fields, false, enable_rle) .map(Struct) } // List-like children match by position, regardless of their names. // The container kind and FixedSizeList width always come from the file. (List(table_child), List(file_child)) => { - coerce_child(table_child, file_child, binary_to_string, enable_rle).map(List) + coerce_child(table_child, file_child, enable_rle).map(List) } (LargeList(table_child), LargeList(file_child)) => { - coerce_child(table_child, file_child, binary_to_string, enable_rle) - .map(LargeList) + coerce_child(table_child, file_child, enable_rle).map(LargeList) } (ListView(table_child), ListView(file_child)) => { - coerce_child(table_child, file_child, binary_to_string, enable_rle) - .map(ListView) + coerce_child(table_child, file_child, enable_rle).map(ListView) } (LargeListView(table_child), LargeListView(file_child)) => { - coerce_child(table_child, file_child, binary_to_string, enable_rle) - .map(LargeListView) + coerce_child(table_child, file_child, enable_rle).map(LargeListView) } (FixedSizeList(table_child, _), FixedSizeList(file_child, size)) => { - coerce_child(table_child, file_child, binary_to_string, enable_rle) + coerce_child(table_child, file_child, enable_rle) .map(|child| FixedSizeList(child, *size)) } // Map keys and values match by position: Parquet always names them @@ -220,17 +218,16 @@ fn coerce_data_type( } /// Coerce a single nested child field, keeping everything but its data type -/// from `file_child`. +/// from `file_child`. Nested binary is never read as a string (see module docs). fn coerce_child( table_child: &FieldRef, file_child: &FieldRef, - binary_to_string: bool, enable_rle: bool, ) -> Option { coerce_data_type( table_child.data_type(), file_child.data_type(), - binary_to_string, + false, enable_rle, ) .map(|new_type| field_with_new_type(file_child, new_type)) @@ -257,7 +254,7 @@ fn coerce_map_entries( } let fields = coerce_fields(file_fields, |idx, file_child| { - coerce_child(&table_fields[idx], file_child, false, false) + coerce_child(&table_fields[idx], file_child, false) })?; Some(field_with_new_type(file_entries, DataType::Struct(fields))) @@ -1279,7 +1276,16 @@ mod tests { true, )]) }; - for (table_type, file_types) in [ + let list_schema = |data_type| { + Schema::new(vec![Field::new_list( + "list", + Field::new_list_field(data_type, true), + true, + )]) + }; + // The view coercions apply to nested fields, but nested binary fields + // are never read as strings (see module docs) + for (table_type, file_types, coerced) in [ ( DataType::Utf8, vec![ @@ -1287,6 +1293,7 @@ mod tests { DataType::LargeBinary, DataType::BinaryView, ], + false, ), ( DataType::LargeUtf8, @@ -1295,6 +1302,7 @@ mod tests { DataType::LargeBinary, DataType::BinaryView, ], + false, ), ( DataType::Utf8View, @@ -1302,22 +1310,29 @@ mod tests { DataType::Binary, DataType::LargeBinary, DataType::BinaryView, - DataType::Utf8, - DataType::LargeUtf8, ], + false, + ), + ( + DataType::Utf8View, + vec![DataType::Utf8, DataType::LargeUtf8], + true, ), ( DataType::BinaryView, vec![DataType::Binary, DataType::LargeBinary], + true, ), ] { for file_type in file_types { - let file_schema = nested_schema(file_type); - let table_schema = nested_schema(table_type.clone()); - assert_eq!( - apply_file_schema_type_coercions(&table_schema, &file_schema), - Some(table_schema) - ); + for schema in [nested_schema, list_schema] { + let file_schema = schema(file_type.clone()); + let table_schema = schema(table_type.clone()); + assert_eq!( + apply_file_schema_type_coercions(&table_schema, &file_schema), + coerced.then_some(table_schema) + ); + } } } let file_schema = nested_schema(DataType::Int32); diff --git a/datafusion/sqllogictest/test_files/parquet.slt b/datafusion/sqllogictest/test_files/parquet.slt index b02672e50ac9c..f868cfb89ef7f 100644 --- a/datafusion/sqllogictest/test_files/parquet.slt +++ b/datafusion/sqllogictest/test_files/parquet.slt @@ -550,6 +550,65 @@ physical_plan statement ok drop table binary_as_string_both; +## Read nested binary fields through a table schema that declares them as +## strings (https://github.com/apache/datafusion/issues/27065) + +query I +COPY ( + SELECT + [X'6F6E65', X'74776F'] AS list_col, + named_struct('value', X'7468726565') AS struct_col +) +TO 'test_files/scratch/parquet/nested_binary_as_string/valid.parquet' +STORED AS PARQUET; +---- +1 + +query I +COPY ( + SELECT + [X'6F6E65', X'FF'] AS list_col, + named_struct('value', X'FF') AS struct_col +) +TO 'test_files/scratch/parquet/nested_binary_as_string/invalid.parquet' +STORED AS PARQUET; +---- +1 + +statement ok +CREATE EXTERNAL TABLE nested_binary_as_string ( + list_col VARCHAR[], + struct_col STRUCT +) +STORED AS PARQUET LOCATION 'test_files/scratch/parquet/nested_binary_as_string/valid.parquet'; + +# NB the data is read and displayed as strings +query ??TT +SELECT list_col, struct_col, arrow_typeof(list_col), arrow_typeof(struct_col) +FROM nested_binary_as_string; +---- +[one, two] {value: three} List(Utf8View) Struct("value": Utf8View) + +statement ok +CREATE EXTERNAL TABLE nested_binary_as_string_invalid ( + list_col VARCHAR[], + struct_col STRUCT +) +STORED AS PARQUET LOCATION 'test_files/scratch/parquet/nested_binary_as_string/invalid.parquet'; + +# Invalid UTF-8 is an error, not an invalid string array +query error Encountered non UTF-8 data +SELECT list_col FROM nested_binary_as_string_invalid; + +query error Encountered non UTF-8 data +SELECT struct_col FROM nested_binary_as_string_invalid; + +statement ok +DROP TABLE nested_binary_as_string; + +statement ok +DROP TABLE nested_binary_as_string_invalid; + # Read a parquet file with binary data in a FixedSizeBinary column # by default, the data is read as binary