Repository navigation
GH-3970: Match the file's list and map structure when converting an Avro requested projection - #3971
shivadarshan-devadiga wants to merge 5 commits into
Conversation
…g an Avro requested projection
|
@wgtmac Could you please approve the workflows and take a look when you have a moment? Thanks! |
…ing with the file
|
For a legacy two-level LIST with |
|
Thanks @shihchengwei-lab, you were right. With 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. |
|
Thanks for digging into this and adding the regression test! |
|
Gentle ping @wgtmac Can you please take a look? Thanks! |
…ey types, and check lists read without an Avro schema
|
@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
|
@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 All |
| } | ||
| GroupType aligned = alignListStructures(projected, file, schema, readSchema, oldListStructure) | ||
| .asGroupType(); | ||
| return Types.buildGroup(repetition) |
There was a problem hiding this comment.
A nested MAP mismatch can still get renamed here. With an array<map<int>> projection, an inner two-level LIST named pair with repeated key_value records matches the rebuilt MAP paths. Duplicate keys are silently collapsed even though the file field is a LIST.
| Type wrappedElement = alignElement( | ||
| projectedElement, fileRepeatedGroup.getType(0), elementSchema, readElementSchema, oldListStructure); | ||
| GroupType wrapper = wrappedElement == null ? null : fileRepeatedGroup.withNewFields(wrappedElement); | ||
| if (wrapper != null && !AvroRecordConverter.isElementType(wrapper, readElement)) { |
There was a problem hiding this comment.
The new wrapper-name test only covers primitive elements. If the same valid string wrapper contains a record element, this probe tries to create an Avro record named string and throws. The whole read fails, including previously readable fields such as id.
| // parquet-cli does, rather than with a schema converted from the aligned projection, which can | ||
| // have another shape, such as records for the wrappers of a list's elements. | ||
| materializedSchema = avroRequestedProjection; | ||
| metadata.put(AVRO_READ_SCHEMA_METADATA_KEY, requestedProjectionString); |
There was a problem hiding this comment.
This changes the output schema even for files without LIST or MAP. With only a projection and no Avro footer schema, a projected BINARY (ENUM) column used to materialize as a string. The same projection now returns an Avro enum value.
| // only the projected columns: a column the projection doesn't select, such as a MAP with integer | ||
| // keys, can have a type that Avro can not represent. | ||
| GroupType fileRepeatedGroup = fileRepeated.asGroupType(); | ||
| Type wrappedElement = alignElement( |
There was a problem hiding this comment.
The new tuple test covers a primitive child. For points_tuple { group x { int32 a; } } and its matching Avro record, the wrapper probe adds another x level. The resulting projection asks for a leaf path that does not exist in the file.
| return null; | ||
| } | ||
| Schema record = AvroSchemaConverter.getNonNull(readRecord); | ||
| Schema.Field field = record.getType() == Schema.Type.RECORD ? record.getField(name) : null; |
There was a problem hiding this comment.
Both materializers resolve Avro field aliases. This lookup misses the read element schema when the LIST field is renamed with an alias, so the same valid projection can fall back to the old path and return null.
Rationale for this change
With an Avro requested projection,
AvroReadSupportconverts the projection to Parquet with a single list structure for the whole file:repeated <element> array, or the 3-levellist/elementnames when auto-detection (#3816) applies. Maps always getkey_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 asnullwithout an error. That covers 2-level lists, single-field groups such asbagor older PyArrow'slist/item, standard lists in a file that also has one of those, and maps namedmap(MAP_KEY_VALUE).parquet-clialways reads with a projection, socat,head,to-avroandconvertprintnullfor these columns.What changes are included in this PR?
After converting the projection,
AvroReadSupport.initwalks it together with the file schema and the Avro projection:MAPorMAP_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
AvroCollectionConverterdoes when reading. It usesAvroRecordConverter.isElementType, with the Avro schema the converters read with, so the projection and the converters agree:array, or a repeated child make the repeated field the element.When the converted projection is kept, as before:
Without a read schema or an Avro schema in the file, the converters read with the requested projection, as
parquet-clidoes. Before, they read with an Avro schema converted from the projection.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 typesas 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
TestArrayCompatibilityread with a requested projection, with the matching read schema unless noted.List and map shapes:
testProjectionReadsTwoLevelListOfPrimitives:repeated binary element.testProjectionReadsSingleFieldGroupsWithOtherNames:bag/xandlist/item, including a null element.testProjectionReadsMixedListStructures: a standard list and alist/itemlist in one file.testProjectionPrunesRecordsInNonStandardList: a record element with only one of its fields projected.testProjectionReadsMapWithOtherNames:map (MAP_KEY_VALUE).Nesting:
testProjectionReadsNestedNonStandardLists: alist/itemlist whose elements are 2-level lists.testProjectionReadsMapOfNonStandardLists: a map whose values arebaglists, 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: anint32list read with a string element type still fails withincompatible types.testProjectionKeepsNonNullElementsRequired: optionallist/itemelements read with a non-null element type still fail withincompatible types.testProjectionStillChecksMapKeyTypes: afixed_len_byte_arraymap key read with a string key still fails withincompatible 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: projectingitems[].xnext to an unselected MAP withint32keys.Only a requested projection:
testProjectionWithoutAvroSchemaKeepsElementTypes: a 2-level list and alist/itemlist.testProjectionReadsListWrapperNamedLikeAnAvroType: a wrapper namedstring.Not a map:
testProjectionDoesNotReadListOfKeyValueRecordsAsMap: a two-level LIST of key-value records isn't read as a MAP.testProjectionStillChecksElementTypesguards a type check the base already enforces. The six tests added or tightened in this revision all fail on the previous revision.ReadCliTest.catReadsNonStandardListsWithoutAvroSchemachecksparquet catend to end, with and without--columns, for a 2-level list and alist/itemlist. Allparquet-avro(436, 2 skipped) andparquet-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 includesparquet-clion such files.Closes #3970