Skip to content

Fix fixed-size lists and renamed struct fields in nested lists and maps - #4436

Draft
rahulsmahadev wants to merge 4 commits into
apache:mainfrom
rahulsmahadev:rahul/fixed-size-list-of-struct-append
Draft

rahulsmahadev wants to merge 4 commits into
apache:mainfrom
rahulsmahadev:rahul/fixed-size-list-of-struct-append

Conversation

@rahulsmahadev

@rahulsmahadev rahulsmahadev commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for this change

Bug: appending a fixed-size list of structs fails. The struct branch of ArrowProjectionVisitor.list rebuilds the list from its first two buffers, which are the validity and offsets buffers only for a variable-size list. A FixedSizeListArray has no offsets buffer, so the rebuilt array is invalid:

import pyarrow as pa

t = pa.table({"v": pa.array([[{"a": 1}, {"a": 2}]], pa.list_(pa.struct([("a", pa.int32())]), 2))})
catalog.create_table("db.t", schema=t.schema).append(t)
# ArrowInvalid: Non-empty array but offsets are null

Fix: rebuild lists and maps around their projected children, preserving validity and offsets; fixed-size lists retain their list size instead of offsets. Lists and maps nested in other lists or maps were cast with the unprojected children, so a renamed struct field (e.g. a b) came back as null on PyArrow 25 and raised on PyArrow 18. A map with list values had the same gap. This follows #4065's null-list fix; fixed-size lists failed before it too.

Bug: with downcast-ns-timestamp-to-us-on-write on, appending a list of timestamp[ns] values with sub-microsecond digits fails. The list visitor downcasts the values but only keeps their type, then casts the original list with a safe cast:

# PYICEBERG_DOWNCAST_NS_TIMESTAMP_TO_US_ON_WRITE=True
t = pa.table({"v": pa.array([[1_000_001_001]], pa.list_(pa.timestamp("ns")))})
catalog.create_table("db.t", schema=t.schema).append(t)
# ArrowInvalid: Casting from timestamp[ns] to timestamp[us] would lose data: 1000001001

Fix: cast the values before rebuilding the list, as the map and struct branches do, so they are truncated to microseconds like a top-level column. Maps failed the same way on main, and the map rebuild above fixes them. This also fixes nested time64[ns] downcasts, supported since #4471.

Bug: a filtered to_arrow_batch_reader() scan or an upsert of a table with a map of structs aborts the process on PyArrow 23 to 25 (#4084). The map rebuild keeps the keys array, which a row filter can give an all-valid validity buffer, and PyArrow aborts when the reader casts such a map (apache/arrow#51029). Fix: drop that buffer from non-nested, non-dictionary keys, which are never null. This restores filtered reads of maps with ordinary string keys after the map rebuild.

Java's lists have no fixed length; Spark writes them through SparkParquetWriters, without an Arrow writer. This fixes PyArrow's shared read/write projection.

Known limit: PyArrow 18 still rejects some null fixed-size lists in its Parquet writer, as it does for fixed_size_list<int>.

Are these changes tested?

Yes, test__to_requested_schema_fixed_size_list_of_structs, test__to_requested_schema_list_of_lists_of_renamed_structs, test__to_requested_schema_map_of_lists_of_renamed_structs, test_append_fixed_size_list_of_structs and test_append_map_of_fixed_size_list_of_structs in tests/io/test_pyarrow.py. All five fail on main (invalid buffers, or a rename that raises on PyArrow 18 and returns nulls on 25) and pass with this change. test_append_map_with_struct_keys passes on main and guards the map rebuild for struct keys (apache/arrow#51029). test_append_lists_and_maps_of_ns_temporals appends timestamp[ns], timestamp[ns, UTC] and time64[ns] values with sub-microsecond digits in a list, large list, fixed-size list and map with the option on, and checks the stored microseconds. It fails on main with the ArrowInvalid above and passes with this change. test_filtered_batch_reader_and_upsert_of_maps reads maps with int, struct and map values through a filtered to_arrow_batch_reader() and upserts them. The struct case aborts on main with PyArrow 25, the others pass on main and guard the map rebuild, and all pass with this change.

Are there any user-facing changes?

Yes, fixed-size lists of structs can be appended, renamed struct fields in nested lists and maps are preserved on read and write, and with downcast-ns-timestamp-to-us-on-write on, timestamp[ns] and time64[ns] values in lists and maps are downcast on write instead of raising. Filtered to_arrow_batch_reader() scans and upserts of tables containing maps with non-dictionary string keys and struct values no longer abort on PyArrow 23 to 25. Dictionary-encoded string keys can still trigger the upstream Arrow abort, as on main.

Please add the changelog label.

This pull request and its description were written by Isaac.

The list visitor rebuilt struct lists from their first two buffers, which
are not offsets for a FixedSizeListArray, so the append failed. Rebuild
every list and map around its projected children, keeping only the buffers
its type has, so renamed struct fields inside nested lists and maps are no
longer dropped.

Project a struct without a validity mask when it has no nulls, so a rebuilt map with
struct keys stays valid (apache/arrow#51029).

Co-authored-by: Isaac <[email protected]>
@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

@rambleraptor, this fixes appending fixed-size lists of structs in ArrowProjectionVisitor. @DG47, this builds on your null-list handling for lists of structs in #4065. Could you take a look when you have a chance?

@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

@rambleraptor could you take a look when you have a chance? Thanks!

rahulsmahadev and others added 3 commits November 30, 2026 06:23
With downcast-ns-timestamp-to-us-on-write on, the list visitor rebuilt the
list around the values from before _cast_if_needed, so its final safe cast
raised ArrowInvalid on timestamp[ns] and time64[ns] values with
sub-microsecond digits. Cast the values first, as the map and struct
branches do.

Co-authored-by: Isaac <[email protected]>
A row filter can give map keys an all-valid validity buffer, which the
rebuilt map kept, and PyArrow 23 to 25 abort when to_arrow_batch_reader
casts such a map (apache/arrow#51029). This also fixes maps of structs,
which main rebuilt the same way (apache#4084).

Co-authored-by: Isaac <[email protected]>
@rahulsmahadev
rahulsmahadev marked this pull request as draft December 1, 2026 02:16

This branch has not been deployed

No deployments
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.

1 participant