Skip to content

Commit 0424eaa

Browse files
authored
fix: Nested arrays should not get a field in lookup (#18745)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #18744 . ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> Fixes a critical bug, as well as behaves more like avro expects in regards to arrays. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> Removed the sub label from arrays in general ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yes ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> Not really
1 parent 4198c5a commit 0424eaa

File tree

1 file changed

+91
-7
lines changed

1 file changed

+91
-7
lines changed

datafusion/datasource-avro/src/avro_to_arrow/arrow_array_reader.rs

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,8 @@ impl<R: Read> AvroArrowArrayReader<'_, R> {
132132
}
133133
}
134134
AvroSchema::Array(schema) => {
135-
let sub_parent_field_name = format!("{parent_field_name}.element");
136135
Self::child_schema_lookup(
137-
&sub_parent_field_name,
136+
parent_field_name,
138137
&schema.items,
139138
schema_lookup,
140139
)?;
@@ -596,10 +595,7 @@ impl<R: Read> AvroArrowArrayReader<'_, R> {
596595
})
597596
.collect();
598597

599-
let sub_parent_field_name =
600-
format!("{}.{}", parent_field_name, list_field.name());
601-
let arrays =
602-
self.build_struct_array(&rows, &sub_parent_field_name, fields)?;
598+
let arrays = self.build_struct_array(&rows, parent_field_name, fields)?;
603599
let data_type = DataType::Struct(fields.clone());
604600
ArrayDataBuilder::new(data_type)
605601
.len(rows.len())
@@ -1038,7 +1034,7 @@ where
10381034
mod test {
10391035
use crate::avro_to_arrow::{Reader, ReaderBuilder};
10401036
use arrow::array::Array;
1041-
use arrow::datatypes::DataType;
1037+
use arrow::datatypes::{DataType, Fields};
10421038
use arrow::datatypes::{Field, TimeUnit};
10431039
use datafusion_common::assert_batches_eq;
10441040
use datafusion_common::cast::{
@@ -1720,4 +1716,92 @@ mod test {
17201716
assert_eq!(2, num_batches);
17211717
assert_eq!(28, sum_id);
17221718
}
1719+
1720+
#[test]
1721+
fn test_list_of_structs_with_custom_field_name() {
1722+
let schema = apache_avro::Schema::parse_str(
1723+
r#"
1724+
{
1725+
"type": "record",
1726+
"name": "root",
1727+
"fields": [
1728+
{
1729+
"name": "items",
1730+
"type": {
1731+
"type": "array",
1732+
"items": {
1733+
"type": "record",
1734+
"name": "item_record",
1735+
"fields": [
1736+
{
1737+
"name": "id",
1738+
"type": "long"
1739+
},
1740+
{
1741+
"name": "name",
1742+
"type": "string"
1743+
}
1744+
]
1745+
}
1746+
}
1747+
}
1748+
]
1749+
}"#,
1750+
)
1751+
.unwrap();
1752+
1753+
let r1 = apache_avro::to_value(serde_json::json!({
1754+
"items": [
1755+
{
1756+
"id": 1,
1757+
"name": "first"
1758+
},
1759+
{
1760+
"id": 2,
1761+
"name": "second"
1762+
}
1763+
]
1764+
}))
1765+
.unwrap()
1766+
.resolve(&schema)
1767+
.unwrap();
1768+
1769+
let mut w = apache_avro::Writer::new(&schema, vec![]);
1770+
w.append(r1).unwrap();
1771+
let bytes = w.into_inner().unwrap();
1772+
1773+
// Create an Arrow schema where the list field is NOT named "element"
1774+
let arrow_schema = Arc::new(arrow::datatypes::Schema::new(vec![Field::new(
1775+
"items",
1776+
DataType::List(Arc::new(Field::new(
1777+
"item", // This is NOT "element"
1778+
DataType::Struct(Fields::from(vec![
1779+
Field::new("id", DataType::Int64, false),
1780+
Field::new("name", DataType::Utf8, false),
1781+
])),
1782+
false,
1783+
))),
1784+
false,
1785+
)]));
1786+
1787+
let mut reader = ReaderBuilder::new()
1788+
.with_schema(arrow_schema)
1789+
.with_batch_size(10)
1790+
.build(std::io::Cursor::new(bytes))
1791+
.unwrap();
1792+
1793+
// This used to fail because schema_lookup would have "items.element.id" and "items.element.name"
1794+
// but build_struct_array will try to look up "items.item.id" and "items.item.name",
1795+
// Now it it is simply "items.id" and "items.name"
1796+
let batch = reader.next().unwrap().unwrap();
1797+
1798+
let expected = [
1799+
"+-----------------------------------------------+",
1800+
"| items |",
1801+
"+-----------------------------------------------+",
1802+
"| [{id: 1, name: first}, {id: 2, name: second}] |",
1803+
"+-----------------------------------------------+",
1804+
];
1805+
assert_batches_eq!(expected, &[batch]);
1806+
}
17231807
}

0 commit comments

Comments
 (0)