Skip to content

CSVReader behavior with dataset that has duplicate column headers is confusing #12852

Closed

Description

@alex

Describe the bug

When working with a CSV file that has duplicate column headers (e.g., https://github.com/openssl/openssl/blob/master/test/recipes/80-test_cmp_http_data/test_connection.csv) the behavior is confusing.

Specifically, any query against a table backed by that file produces the error:

Arrow error: Csv error: incorrect number of fields for line 1, expected 14 got more than 14

However, all rows in that CSV have 20 fields.

Based on looking at the results of a limit 0 query, I can see that the schema is effectively dropping all duplicate columns from the expected schema, and therefore the following rows do not have the expected number of cells.

To Reproduce

Run queries against the linked CSV file.

Expected behavior

I believe expected behavior would be to either a) automatically rename those columns (adding _{n} perhaps), or b) provide a clear error that schemas with duplicate column names are not supported.

Additional context

No response

Activity

  1. added
    bugSomething isn't working
    on Oct 10, 2024
  2. jpsrobertson commented on Nov 12, 2024

    @jpsrobertson

    bumping as i'm also experiencing this problem!

  3. Rafferty97 commented on Nov 12, 2024

    @Rafferty97
    Contributor

    This looks to be caused by the call to Schema::try_merge at datasource/file_format/csv.rs:328. That function merges schemas together by joining on field name, so a side effect is the merging of fields within a single schema that happen to have the same name.

    I think an important question is whether it makes sense for a schema to have multiple fields with the same name in the first place, or whether it should be disallowed. While maybe it's defensible given fields can be accessed by index, it does make it pretty hard to sensibly merge schemas based on field name as Schema::try_merge does.

  4. alex commented on Nov 12, 2024

    @alex
    Author

    From the perspective of a user, I think the ideal default behavior would be register a clean error in that case.

    But I'd also like the ability to somehow map the duplicate field names to something (e.g., {field}_{i}). If it just did that by default, that'd also be fine.

    But sadly, CSVs with duplicate columns names are kind of common.

  5. Rafferty97 commented on Nov 12, 2024

    @Rafferty97
    Contributor

    Having thought about it some more, I think the use of Schema::try_merge is actually incorrect for CSV files, because the CSV reading process assumes that the fields in the Schema are in the same order as they appear in the file.

    So, if two CSVs are read in with the same columns but out of order, this will cause data to appear in the wrong columns. This might error out if there is a mismatch in types, but could also just silently return bogus data.

    My intuition is that the code needs to be changed to merge CSV schemas based on field index not field name.

  6. added a commit that references this issue on Aug 20, 2026
    12ba4b7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions