fix: reject duplicate field names during CSV and Parquet schema inference - #24387
fix: reject duplicate field names during CSV and Parquet schema inference#24387sovsparrow wants to merge 1 commit into
Conversation
Jefffrey
left a comment
There was a problem hiding this comment.
this makes sense to me; i hope its a niche enough bug to not cause too much issues for downstream users now that itll error, though i suppose the other solution of renaming the duplicates would be breaking too anyway
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #24387 +/- ##
==========================================
- Coverage 81.19% 81.19% -0.01%
==========================================
Files 1110 1110
Lines 388739 388815 +76
Branches 388739 388815 +76
==========================================
+ Hits 315636 315688 +52
- Misses 54515 54535 +20
- Partials 18588 18592 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Yep, that was my reasoning too. This seemed safer than renaming the duplicates, and silently dropping columns is the worst option imo. In the case of downstream issues, I’d be cool with following up. Ty. |
Which issue does this PR close?
Rationale for this change
When a Parquet file has multiple columns with the same name and compatible types, DataFusion merges them during schema inference. The scan succeeds without warning but returns only one of those columns.
PyArrow can produce such a file via its public API:
DataFusion 54.0.0 returns ['id', 'value'] for this file; the [100, 200, 300] column is missing.
Other readers either preserve the data or reject the file:
pyarrowParquetFile.read()pyarrowpq.read_table()ArrowInvalid: Multiple matches for FieldRef.Name(value)pyarrowdatasetArrowInvalid: Can't unify schema with duplicate field namesduckdbvalue_1polarsDuplicateErrordatafusionThe same bug is behind two issues #24381 and #12852. CSV and Parquet schema inference both pass each file's schema to
Schema::try_merge, which matches fields by name. Duplicate names in one file are merged before the scan. Thus, column(s) disappear silently.What changes are included in this PR?
This PR adds
ensure_unique_field_namestodatafusion-datasource. It is called for each inferred CSV and Parquet schema before the merge. It uses the existingSchemaError::DuplicateUnqualifiedFielderror and adds the file location to the message.Duplicate names are rejected. The PR does not rename columns or change how fields with unique names are merged across files.
Are these changes tested?
Yes. Two regression tests in order to cover the CSV and Parquet paths:
datafusion/core/src/datasource/file_format/parquet.rs—infer_schema_rejects_duplicate_field_namesdatafusion/core/src/datasource/file_format/csv.rs—infer_schema_rejects_duplicate_header_namesBoth tests create their inputs in a temporary directory; no fixtures are added. Both fail on
mainand pass with this change. Onmain, the CSV test infersSchema { fields: [id, value] }from a three-column header, matching the behavior reported in #12852.Are there any user-facing changes?
Yes. CSV and Parquet schema inference now returns a clear error when an inferred
file schema repeats a field name. The error names the file and the repeated
column instead of returning an incomplete schema.