Repository navigation
Fix inspect file and partition tables for UUID columns - #4358
rahulsmahadev wants to merge 10 commits into
Conversation
inspect.files(), entries() and partitions() raised ArrowNotImplementedError on tables with a uuid column: pa.Table.from_pylist cannot build the arrow.uuid type nested in readable_metrics or a partition struct. Build these tables from the storage types and cast them to the schema, so the output types are unchanged. UUID partition values are passed as their 16 bytes, which pyarrow 18 requires. Co-authored-by: Isaac <no-reply@databricks.com>
|
@geruh @soumya-ghosh could you take a look when you have a chance? It fixes the file and partition metadata tables from #1626 for tables with uuid columns. |
Pass the decoded lower and upper bounds through _storage_value, like the partition values, so a uuid bound is built from its 16 bytes even when from_bytes returns a uuid.UUID (proposed in apache#4370). Co-authored-by: Isaac <no-reply@databricks.com>
|
@ebyhr could you take a look when you have a chance? Thanks! |
|
@rambleraptor could you take a look when you have a chance? Thanks! |
rambleraptor
left a comment
There was a problem hiding this comment.
Couple edge cases we should think through. Thanks for doing this!
| return _storage_value(from_bytes(field_type, bound)) if bound is not None else None | ||
|
|
||
|
|
||
| def _storage_type(data_type: pa.DataType) -> pa.DataType: |
There was a problem hiding this comment.
This doesn't appear to work with lists or maps, so UUIDs inside of those will still crash.
There was a problem hiding this comment.
Good catch, thanks. readable_metrics types the bounds of a top-level list or map column with the column's type, so list<uuid> and map<string, uuid> columns still failed. _storage_type now also replaces extension types inside lists and maps, and test_inspect_files_and_entries_with_uuid_in_list_and_map covers both. It fails without the change with ArrowNotImplementedError: extension.
There was a problem hiding this comment.
I'm pretty sure that we want to convert this into a visitor:
iceberg-python/pyiceberg/io/pyarrow.py
Line 1418 in 7f4391b
There was a problem hiding this comment.
Thanks @Fokko, done in ec54f8a and 8701f6c. _ConvertToStorageTypes in pyarrow.py is a PyArrowSchemaVisitor that replaces extension types with their storage types and keeps everything else, so _storage_type and _storage_field are gone. _from_pylist applies it to the nested fields only, since PyArrow fails only for an extension type nested in a struct, list or map (apache/arrow#34985), and the values go through the existing _to_arrow_value.
|
|
||
| if isinstance(data_type, pa.BaseExtensionType): | ||
| return data_type.storage_type | ||
| if pa.types.is_struct(data_type): |
There was a problem hiding this comment.
This is striping valid information from geography / geometry types.
There was a problem hiding this comment.
Thanks for checking. The rows are only built with the storage types: _from_pylist casts the result back to the full schema, so the geoarrow type keeps its metadata. I checked a geometry bound ({"crs": "srid:3857"}) and a geography bound ({"edges": "spherical", "crs": "srid:4269"}): the output schema equals the original and the WKB value is unchanged. With pa.Table.from_pylist, which main uses, both fail with ArrowNotImplementedError: extension when geoarrow-types is installed, so this also fixes these tables for geo columns. Happy to add a test for it if you'd like.
readable_metrics types the bounds of a top-level list or map column with the column's type, so a list<uuid> or map<string, uuid> column still failed with ArrowNotImplementedError: extension. Co-authored-by: Isaac <no-reply@databricks.com>
Fokko
left a comment
There was a problem hiding this comment.
Thanks @rahulsmahadev, nice fix. Building the metadata tables with storage types and casting back keeps the output schema unchanged while avoiding the pyarrow extension-type-in-struct limitation, and the four new tests cover files, entries, partitions, list/map and empty tables. CI green.
One nit inline on list coverage.
| def _storage_type(data_type: pa.DataType) -> pa.DataType: | ||
| """Return the storage type of a data type, replacing extension types inside structs, lists and maps.""" | ||
| import pyarrow as pa | ||
|
|
||
| if isinstance(data_type, pa.BaseExtensionType): | ||
| return data_type.storage_type | ||
| if pa.types.is_struct(data_type): | ||
| return pa.struct([_storage_field(field) for field in data_type]) | ||
| if pa.types.is_large_list(data_type): | ||
| return pa.large_list(_storage_field(data_type.value_field)) | ||
| if pa.types.is_map(data_type): | ||
| return pa.map_(_storage_field(data_type.key_field), _storage_field(data_type.item_field)) | ||
| return data_type | ||
|
|
||
|
|
There was a problem hiding this comment.
Nit: only large_list is unwrapped here. schema_to_pyarrow emits large_list for ListType, so this is fine for schemas built by PyIceberg, but a plain list or fixed_size_list with an extension element (hand-built arrow schema) would still hit the ArrowNotImplementedError. Is large_list guaranteed here, or should the other list kinds be unwrapped too?
| def _storage_type(data_type: pa.DataType) -> pa.DataType: | |
| """Return the storage type of a data type, replacing extension types inside structs, lists and maps.""" | |
| import pyarrow as pa | |
| if isinstance(data_type, pa.BaseExtensionType): | |
| return data_type.storage_type | |
| if pa.types.is_struct(data_type): | |
| return pa.struct([_storage_field(field) for field in data_type]) | |
| if pa.types.is_large_list(data_type): | |
| return pa.large_list(_storage_field(data_type.value_field)) | |
| if pa.types.is_map(data_type): | |
| return pa.map_(_storage_field(data_type.key_field), _storage_field(data_type.item_field)) | |
| return data_type | |
| if isinstance(data_type, pa.BaseExtensionType): | |
| return data_type.storage_type | |
| if pa.types.is_struct(data_type): | |
| return pa.struct([_storage_field(field) for field in data_type]) | |
| if pa.types.is_large_list(data_type) or pa.types.is_list(data_type) or pa.types.is_fixed_size_list(data_type): | |
| return pa.large_list(_storage_field(data_type.value_field)) | |
| if pa.types.is_map(data_type): | |
| return pa.map_(_storage_field(data_type.key_field), _storage_field(data_type.item_field)) | |
| return data_type |
There was a problem hiding this comment.
Done in 48ab9c6. Plain list and fixed_size_list are unwrapped too and keep their kind, so a fixed size list stays fixed size. PyIceberg schemas only produce large_list, so this matters for hand-built schemas. Added test_from_pylist_casts_uuid_inside_plain_and_fixed_size_lists, which fails before with ArrowNotImplementedError: extension.
Only large_list was unwrapped, so a plain list or fixed_size_list with an extension element still raised ArrowNotImplementedError when the metadata rows were built. Each list keeps its own kind. PyIceberg schemas only produce large_list, so this matters for hand-built schemas. Co-authored-by: Isaac <no-reply@databricks.com>
Keep the partition keys by current field name from main and the storage values from this branch. Co-authored-by: Isaac <no-reply@databricks.com>
Replace the hand-written _storage_type and _storage_field helpers with _ConvertToStorageTypes, a PyArrowSchemaVisitor next to _ConvertViewTypes that replaces extension types with their storage types and keeps everything else. Co-authored-by: Isaac <no-reply@databricks.com>
PyArrow fails only for extension types nested in a struct, list or map (apache/arrow#34985), so _from_pylist gives only nested fields their storage types; visiting the dictionary-typed file_format logged a misleading warning on every files() call. Partition values and bounds use the existing _to_arrow_value. Co-authored-by: Isaac <no-reply@databricks.com>
|
@rambleraptor both points should be covered now. Lists and maps, including plain and fixed-size lists, go through a |
Co-authored-by: Isaac <no-reply@databricks.com>
apache#4272 returns Schema.empty_table() for snapshots without manifests, which PyArrow cannot build for the arrow.uuid bounds in readable_metrics. Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
Rationale for this change
Bug: the file and entry metadata tables raise on any table with a
uuidcolumn, with or without data, andpartitions()raises for a uuid identity partition:readable_metricsandpartitiontype uuid values asarrow.uuid, and PyArrow cannot convert Python values into an extension type nested in a struct, list or map (apache/arrow#34985). This breaksentries(),files(),data_files(),delete_files(), theall_*variants, andpartitions()for a uuid identity partition.Fix: build the nested fields with their storage types, using a new
PyArrowSchemaVisitor(_ConvertToStorageTypes), and cast the table to the unchanged schema. Partition values and bounds go through the existing_to_arrow_value, because PyArrow 18 needs the 16 bytes.readable_metricslists the top-level columns, so a struct, list or map column gets bounds of its own type that are always null, while Java lists every primitive column (MetricsUtil.readableMetricsSchema).Are these changes tested?
Yes.
test_inspect_files_and_entries_render_uuid_bounds,test_inspect_files_of_empty_table_with_uuid_column,test_inspect_files_and_entries_with_uuid_in_list_and_mapandtest_inspect_partitions_and_files_render_uuid_partitionfail on main withArrowNotImplementedError: extensionand pass with this change on PyArrow 25 and 18;test_from_pylist_casts_uuid_inside_plain_and_fixed_size_listscovers plain and fixed-size lists.Are there any user-facing changes?
Bug fix: these metadata tables no longer raise on tables with a uuid column, and their schema is unchanged.
This pull request and its description were written by Isaac.