Skip to content

fix(parquet): keep virtual columns in the schema reported with a schema hint - #11047

Open
bharadwaj-pendyala wants to merge 1 commit into
apache:mainfrom
bharadwaj-pendyala:fix/supplied-schema-virtual-columns
Open

fix(parquet): keep virtual columns in the schema reported with a schema hint#11047
bharadwaj-pendyala wants to merge 1 commit into
apache:mainfrom
bharadwaj-pendyala:fix/supplied-schema-virtual-columns

Conversation

@bharadwaj-pendyala

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

ArrowReaderMetadata::with_supplied_schema already treats virtual columns as extra fields on top of the hint. It passes them to parquet_to_arrow_field_levels_with_virtual, and its own length check reads supplied_schema.fields().len() + virtual_columns.len(). Then it returns schema: supplied_schema, which doesn't have them.

So the two branches of try_new disagree. Without a hint you get a schema with the virtual fields in it; add a hint and they vanish, even though the reader still decodes them. On the file from the issue that means metadata.schema() reports one field while every batch that metadata produces has two:

metadata.schema().fields()  [value]
batch.schema().fields()     [value, row_number]

@limenilbuz asked for either of two behaviours: include the virtual columns in the reported schema, or stop erroring when the hint itself contains them. This does the first. The second is a change to what with_schema accepts, and the length check here already assumes virtual fields live outside the hint, so the first is the one that makes the function agree with itself.

What changes are included in this PR?

The returned schema is now the supplied fields followed by the virtual fields, keeping the supplied schema's key/value metadata. When no virtual columns are requested the supplied schema is returned untouched, so nothing changes for that path.

parquet_to_arrow_field_levels_with_virtual appends virtual columns to the root in the order given and clones them unchanged (parquet/src/arrow/schema/mod.rs:223), so appending them here in the same order lines the reported schema up with field_levels.

Are these changes tested?

Yes. test_supplied_schema_keeps_virtual_columns builds metadata from a hint plus two virtual fields and checks the field order, that the hint's schema metadata survives, and that metadata.schema() agrees with the fields of the batch the reader emits. It fails on f9e02ba with:

left: [Field { name: "value", data_type: Int64 }]
right: [Field { name: "value", .. }, Field { name: "row_number", .. }, Field { name: "row_group_index", .. }]

cargo test -p parquet --lib is 1381 passed, 0 failed. cargo fmt --all -- --check and cargo clippy -p parquet --all-targets are both clean.

Are there any user-facing changes?

ArrowReaderMetadata::schema(), and the builder schema derived from it, gain the virtual fields when a hint and virtual columns are combined. That's the fix, but it is a field-count change on a public accessor, so it's worth calling out. Physical column indices are unaffected and no crate in the tree combines those two options.

One thing I left alone: with an explicit projection the async reader's schema() drops virtual fields while the batches still carry them. That reproduces with and without a schema hint, so it's a separate bug from this one and I didn't touch it here.

…ma hint

with_supplied_schema passes virtual_columns to
parquet_to_arrow_field_levels_with_virtual and counts them in its own
length check, but returned the bare supplied schema, so
ArrowReaderMetadata::schema() dropped them. The no-hint branch of
try_new keeps them, and the reader decodes them either way.

Append the virtual fields to the supplied fields, preserving the
supplied schema's metadata.

Closes apache#11046
@github-actions github-actions Bot added the parquet Changes to the parquet crate label Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parquet Changes to the parquet crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Providing schema hint to ArrowReaderMetadata causes resulting schema to omit virtual fields

1 participant