Skip to content

GH-3970: Match the file's list and map structure when converting an Avro requested projection - #3971

Open
shivadarshan-devadiga wants to merge 7 commits into
apache:masterfrom
shivadarshan-devadiga:GH-cli-nonstandard-lists
Open

shivadarshan-devadiga wants to merge 7 commits into
apache:masterfrom
shivadarshan-devadiga:GH-cli-nonstandard-lists

Conversation

@shivadarshan-devadiga

@shivadarshan-devadiga shivadarshan-devadiga commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for this change

With an Avro requested projection, AvroReadSupport converts the projection to Parquet with a single list structure for the whole file: repeated <element> array, or the 3-level list / element names when auto-detection (#3816) applies. Maps always get key_value. A list or map that the file wrote with other repeated or element names is then missing from the projection's point of view, so it reads as null without an error. That covers 2-level lists, single-field groups such as bag or older PyArrow's list / item, standard lists in a file that also has one of those, and maps named map (MAP_KEY_VALUE). parquet-cli always reads with a projection, so cat, head, to-avro and convert print null for these columns.

What changes are included in this PR?

After converting the projection, AvroReadSupport.init walks it together with the file schema and the Avro projection:

  • Lists: for each projected list, it uses the file's repeated field, and the element inside it for 3-level lists, keeping the projected fields of record elements.
  • Maps: for each projected map that the file annotates as a map (MAP or MAP_KEY_VALUE), it does the same with the file's key-value group.

It decides whether a list's repeated field is the element the way AvroCollectionConverter does when reading. It uses AvroRecordConverter.isElementType, with the Avro schema the converters read with, so the projection and the converters agree:

  • Structural rules first. A primitive, more than one field, the name array, or a repeated child make the repeated field the element.
  • Otherwise, the repeated field is a wrapper of its single field, unless the element is a record that the repeated field itself matches.
  • Projected columns only. The checks see only the projected columns, so an unselected column that Avro can't represent doesn't matter.

When the converted projection is kept, as before:

  • a list or map whose file structure doesn't match the projection's shape, including a group of another kind (a MAP projection over a LIST, for example);
  • a list where pruning changes the converter's decision;
  • a list whose repeated field can't be converted to Avro to check it, such as a wrapper named string around a record element;
  • a record field that is a repeated LIST, which the spec doesn't allow.

Without a read schema or an Avro schema in the file, init converts the projection to Avro before aligning it and reads with that schema, which is the schema the base reads with, so the records' schema doesn't change.

Only the names come from the file. Each element keeps its projected type, so the requested schema is still checked against the file. A type that doesn't match fails with incompatible types as before, and so does a map key that isn't a string. Each element's repetition is the one the converted projection gives it: repeated if the file's repeated field is the element, otherwise optional only if the Avro schema allows null. So files whose lists already match the converted projection get the same projection as before, and a non-null Avro element type still fails against optional elements in the file.

Are these changes tested?

Yes. New tests in TestArrayCompatibility read with a requested projection, with the matching read schema unless noted.

List and map shapes:

  • testProjectionReadsTwoLevelListOfPrimitives: repeated binary element.
  • testProjectionReadsSingleFieldGroupsWithOtherNames: bag / x and list / item, including a null element.
  • testProjectionReadsMixedListStructures: a standard list and a list / item list in one file.
  • testProjectionPrunesRecordsInNonStandardList: a record element with only one of its fields projected.
  • testProjectionReadsMapWithOtherNames: map (MAP_KEY_VALUE).
  • testProjectionReadsSingleFieldTupleWithGroupField: points_tuple { x { a } } as the element.

Nesting:

  • testProjectionReadsNestedNonStandardLists: a list / item list whose elements are 2-level lists.
  • testProjectionReadsMapOfNonStandardLists: a map whose values are bag lists, including a null element.
  • testProjectionReadsRepeatedListAsElementOfTwoLevelList: a repeated LIST as the element of a 2-level list, also with only a requested projection.
  • testProjectionReadsDeeplyNestedTwoLevelLists: 26 nested lists, within a time bound, with and without a read schema.
  • testProjectionReadsSingleFieldTupleAsElement: points_tuple { x } as the element of a list of records, with and without a read schema.

Type checks still apply:

  • testProjectionStillChecksElementTypes: an int32 list read with a string element type still fails with incompatible types.
  • testProjectionKeepsNonNullElementsRequired: optional list / item elements read with a non-null element type still fail with incompatible types.
  • testProjectionStillChecksMapKeyTypes: a fixed_len_byte_array map key read with a string key still fails with incompatible types.

Read schema and columns:

  • testProjectionOfTwoLevelRecordsFollowsTheReadSchema: a 2-level list of records with a field projected away, under three read schemas, including one that requires that field.
  • testProjectionIgnoresUnselectedColumnsAvroCannotRepresent: projecting items[].x next to an unselected MAP with int32 keys.
  • testProjectionFollowsAliasesOfTheReadSchema: a list renamed in the read schema with the file's name as an alias.

Only a requested projection, where the records have the schema converted from the projection, as before:

  • testProjectionWithoutAvroSchemaKeepsElementTypes: a 2-level list and a list / item list.
  • testProjectionReadsListWrapperNamedLikeAnAvroType: a wrapper named string.
  • testProjectionWithoutAvroSchemaKeepsTheConvertedSchema: a BINARY (ENUM) column stays a string with the record converters.

Not a map, and lists that can't be checked:

  • testProjectionDoesNotReadListOfKeyValueRecordsAsMap and testProjectionDoesNotReadNestedListOfKeyValueRecordsAsMap: two-level LISTs of key-value records, also as a list element, aren't read as a MAP.
  • testProjectionKeepsListWrapperNamedLikeAnAvroTypeForRecords: a string wrapper around a record element doesn't fail the read.

testProjectionStillChecksElementTypes guards a type check the base already enforces. The six tests added or tightened in this revision all fail on the previous revision. ReadCliTest.catReadsNonStandardListsWithoutAvroSchema checks parquet cat end to end, with and without --columns, for a 2-level list and a list / item list. All parquet-avro (441, 2 skipped) and parquet-cli (93, 2 skipped) tests pass.

Are there any user-facing changes?

Yes. Reading with an Avro requested projection now returns the values of lists and maps written with non-standard repeated or element names, instead of null. This includes parquet-cli on such files.

Closes #3970

@shivadarshan-devadiga

shivadarshan-devadiga commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

@wgtmac Could you please approve the workflows and take a look when you have a moment? Thanks!

@shihchengwei-lab

Copy link
Copy Markdown

For a legacy two-level LIST with repeated group point { x, y }, projection alignment sees both fields and treats point as the element. If the projection selects only x but the Avro read schema still requires y with no default, what keeps isElementType() from treating the projected point{x} as a wrapper during materialization? I haven’t tested this combination. Projection alignment · Read-time call · Element check

@shivadarshan-devadiga

Copy link
Copy Markdown
Contributor Author

Thanks @shihchengwei-lab, you were right. With y required in the read schema and no default, isElementType checked the pruned point{x} against the read schema, took it for a wrapper and failed with ClassCastException: required int32 x is not a group; before this change that case read the list as null.

The alignment now takes the schema the converters read with (the read schema, else the file's Avro schema) and keeps a list's converted projection when the converter would classify the aligned repeated field differently.
testProjectionOfTwoLevelRecordsFollowsTheReadSchema covers it: with the projection as read schema it reads [{x: 1}], with y defaulted [{x: 1, y: 7}], and with y required the list reads as null as before, instead of failing.

@shihchengwei-lab

Copy link
Copy Markdown

Thanks for digging into this and adding the regression test!

@shivadarshan-devadiga

Copy link
Copy Markdown
Contributor Author

Gentle ping @wgtmac Can you please take a look? Thanks!

…ey types, and check lists read without an Avro schema
@shivadarshan-devadiga

Copy link
Copy Markdown
Contributor Author

@wgtmac Thank you for the detailed review. I have addressed your review comments and made changes accordingly. Can you please re-review? Thanks!

…ema, and decide list elements structurally first
@shivadarshan-devadiga

Copy link
Copy Markdown
Contributor Author

@wgtmac Thanks for the careful review. I've addressed all six in fa164a6. The main change is that without a read schema or an Avro schema in the file, the read now uses the requested projection, as parquet-cli does. That removed the probe that most of these came from. AvroReadSupport is smaller than in the previous revision.

All parquet-avro tests pass (436, 2 skipped) and all parquet-cli tests pass (93, 2 skipped). Each of the six new or tightened tests fails on the previous revision. Could you please take another look when you have a moment?

…n only groups of the same kind, and follow read schema aliases
@shivadarshan-devadiga

shivadarshan-devadiga commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

@wgtmac Thanks again for the careful review. I've addressed all five in d753b4c . Without a read schema or an Avro footer schema, the read now keeps the schema the base converts from the projection, so the records' schema doesn't change. The alignment no longer renames a group onto one of another kind, and it keeps the converted projection when it can't check a list.

All parquet-avro tests pass (441, 2 skipped) and all parquet-cli tests pass (93, 2 skipped). Each of the five new tests fails on the previous revision. Could you please take another look when you have a moment?

…ad the converted schema without JSON, and name element records after the file
@shivadarshan-devadiga

Copy link
Copy Markdown
Contributor Author

@wgtmac Thanks again for the careful review. I've addressed all six in 32be985:

  • The alignment now keeps the converted projection unless the converters can read the aligned list: its fields have the file's group or primitive shape, and the read schema can read the element it picks.
  • prepareForRead no longer reads the converted schema back from JSON.
  • When the converters read with the schema converted from the projection, a list's element record takes the file's name of the element.
  • A legacy MAP with unannotated BINARY keys isn't selected.
  • AvroRecordConverter.isElementType treats a group whose name can't be an Avro record name as a wrapper instead of throwing.

All parquet-avro tests pass (445, 2 skipped) and all parquet-cli tests pass (93, 2 skipped). Each of the six new or extended tests fails on the previous revision. Could you please take another look when you have a moment?

@shivadarshan-devadiga

Copy link
Copy Markdown
Contributor Author

Gentle ping @wgtmac Can you please take a look whenever you get a moment? Thanks!

@shivadarshan-devadiga

shivadarshan-devadiga commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Gentle ping @wgtmac @Fokko Can you please take a look whenever you get a moment? Thanks!

@wgtmac

wgtmac commented Dec 4, 2026

Copy link
Copy Markdown
Member

I think this is a real bug and fixing it may be difficult. However, I’m concerned that the recent revisions have addressed individual examples while introducing regressions in cases that already worked.

Before updating the code again, I would suggest that you could understand the root cause of this issue, and any limitation and implication of the proposed fix.

Please check those cases before requesting another review. AI tools can help you find counter-examples, but I’d like to understand your reasoning about why the fix is correct and any trade-off has been made, and then comment with your own words. That would make review much easier and more efficient.

@shivadarshan-devadiga

Copy link
Copy Markdown
Contributor Author

Thanks @wgtmac.
I agree that I kept fixing the examples one at a time without re-thinking about the edge cases or regression that new change might bring; so I'll stop updating the code until we agree on a particular approach to fix the issue so that it can be contained without introducing new regression or doing multiple times back and forth.

As I understand the root cause: AvroReadSupport.init converts the Avro projection into a Parquet requested schema with AvroSchemaConverter & that always uses one list layout, whatever the file uses. The requested schema is then matched to the file by field name. If the file's repeated field has another name (element, bag, list, ...), the column counts as missing and reads as null. If the name matches but the shape doesn't, the read fails with incompatible types.

My PR reshapes the converted projection afterwards to use the file's names. To do that, it has to decide for each list whether the file's repeated group is the element or a wrapper, which repeats what AvroRecordConverter and the LIST compatibility rules already decide. Whenever my copy and the converters disagree, something that used to work breaks, and that's what kept happening across the revisions.

To confirm this more accurately I ran a thorough end to end testing on all possible paths; I compared the base and this PR on 964 reads: 41 files with different list and map shapes, the four combinations of write-old-list-structure and add-list-element-records, and projections with and without a read schema. I found no read that was correct before and is different now and reads without a projection don't change. But there are cases still not yet fixed. Six reads that used to return null lists now fail with Avro field 'a' not found: 2-level lists of records with write-old-list-structure=false. And with the default add-list-element-records=true, projection-only reads of most list shapes are still wrong on both.

So I think the approach I have taken itself is the problem. Instead of converting the projection and then reshaping it, I'd like to build the requested schema by pruning the file's own schema to the projected fields and leave the element types to the converter, as a read without a projection already does. Lists and maps would then keep the file's exact structure. The trade-off is that some type checks that happen during conversion today, such as the incompatible element-type tests, would move to the converter & projected fields missing from the file need separate handling.

Would that direction work for you before I rewrite the code? If not we can discuss further on the best possible approach and then only I will start doing the code change.

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.

parquet-avro: a requested projection reads lists and maps as null unless the file uses the projection's list structure

3 participants