Repository navigation
Fix inspect file and partition tables for UUID columns #4358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,9 +17,10 @@ | |
| from __future__ import annotations | ||
|
|
||
| import itertools | ||
| from collections.abc import Iterator | ||
| from collections.abc import Iterable, Iterator | ||
| from datetime import UTC, datetime | ||
| from typing import TYPE_CHECKING, Any | ||
| from uuid import UUID | ||
|
|
||
| from pyiceberg.conversions import from_bytes | ||
| from pyiceberg.expressions import AlwaysTrue, BooleanExpression | ||
|
|
@@ -39,7 +40,46 @@ | |
|
|
||
|
|
||
| def _readable_bound(field_type: PrimitiveType, bound: bytes | None) -> Any | None: | ||
| return from_bytes(field_type, bound) if bound is not None else None | ||
| return _storage_value(from_bytes(field_type, bound)) if bound is not None else None | ||
|
|
||
|
|
||
| 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): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is striping valid information from geography / geometry types.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for checking. The rows are only built with the storage types: |
||
| 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 | ||
|
|
||
|
|
||
| def _storage_field(field: pa.Field) -> pa.Field: | ||
| import pyarrow as pa | ||
|
|
||
| return pa.field(field.name, _storage_type(field.type), field.nullable) | ||
|
|
||
|
|
||
| def _from_pylist(rows: Iterable[dict[str, Any]], schema: pa.Schema) -> pa.Table: | ||
| """Build a table from Python rows, typed by the given schema. | ||
|
|
||
| pyarrow cannot convert Python values into an extension type nested in a struct, list or map, | ||
| such as the ``arrow.uuid`` bounds in ``readable_metrics``. The rows are built with | ||
| the storage types and cast to ``schema``, so the output types are unchanged. | ||
| """ | ||
| import pyarrow as pa | ||
|
|
||
| storage_schema = pa.schema([_storage_field(field) for field in schema]) | ||
| return pa.Table.from_pylist(rows, schema=storage_schema).cast(schema) | ||
|
|
||
|
|
||
| def _storage_value(value: Any) -> Any: | ||
| """Return a value in its storage form, where a UUID is its 16 bytes.""" | ||
| return value.bytes if isinstance(value, UUID) else value | ||
|
|
||
|
|
||
| class InspectTable: | ||
|
|
@@ -255,7 +295,7 @@ def _readable_metrics_struct(bound_type: PrimitiveType) -> pa.StructType: | |
|
|
||
| partition = entry.data_file.partition | ||
| partition_record_dict = { | ||
| field.name: partition[pos] | ||
| field.name: _storage_value(partition[pos]) | ||
| for pos, field in enumerate(self.tbl.metadata.specs()[manifest.partition_spec_id].fields) | ||
| } | ||
|
|
||
|
|
@@ -288,10 +328,7 @@ def _readable_metrics_struct(bound_type: PrimitiveType) -> pa.StructType: | |
| } | ||
| ) | ||
|
|
||
| return pa.Table.from_pylist( | ||
| entries, | ||
| schema=entries_schema, | ||
| ) | ||
| return _from_pylist(entries, entries_schema) | ||
|
|
||
| def refs(self) -> pa.Table: | ||
| """Return all snapshot references (branches and tags) as a PyArrow Table. | ||
|
|
@@ -413,17 +450,15 @@ def partitions( | |
| for entry in itertools.chain.from_iterable(scan._plan_manifest_entries()): | ||
| partition = entry.data_file.partition | ||
| partition_record_dict = { | ||
| field.name: partition[pos] for pos, field in enumerate(self.tbl.metadata.specs()[entry.data_file.spec_id].fields) | ||
| field.name: _storage_value(partition[pos]) | ||
| for pos, field in enumerate(self.tbl.metadata.specs()[entry.data_file.spec_id].fields) | ||
| } | ||
| entry_snapshot = snapshots_by_id.get(entry.snapshot_id) if entry.snapshot_id is not None else None | ||
| self._update_partitions_map_from_manifest_entry( | ||
| partitions_map, entry.data_file, partition_record_dict, entry_snapshot | ||
| ) | ||
|
|
||
| return pa.Table.from_pylist( | ||
| partitions_map.values(), | ||
| schema=table_schema, | ||
| ) | ||
| return _from_pylist(partitions_map.values(), table_schema) | ||
|
|
||
| def _update_partitions_map_from_manifest_entry( | ||
| self, | ||
|
|
@@ -733,8 +768,6 @@ def _get_files_from_manifest( | |
| pa.Table: File metadata rows conforming to the files schema. | ||
| See ``_get_files_schema`` for the full column list. | ||
| """ | ||
| import pyarrow as pa | ||
|
|
||
| files: list[dict[str, Any]] = [] | ||
| schema = self.tbl.metadata.schema() | ||
| io = self.tbl.io | ||
|
|
@@ -762,7 +795,7 @@ def _get_files_from_manifest( | |
| } | ||
| partition = data_file.partition | ||
| partition_record_dict = { | ||
| field.name: partition[pos] | ||
| field.name: _storage_value(partition[pos]) | ||
| for pos, field in enumerate(self.tbl.metadata.specs()[manifest_list.partition_spec_id].fields) | ||
| } | ||
| files.append( | ||
|
|
@@ -787,10 +820,7 @@ def _get_files_from_manifest( | |
| "readable_metrics": readable_metrics, | ||
| } | ||
| ) | ||
| return pa.Table.from_pylist( | ||
| files, | ||
| schema=self._get_files_schema(), | ||
| ) | ||
| return _from_pylist(files, self._get_files_schema()) | ||
|
|
||
| def _get_files_schema(self) -> pa.Schema: | ||
| """Return the PyArrow schema for file-level metadata tables. | ||
|
|
@@ -875,7 +905,7 @@ def _files(self, snapshot_id: int | None = None, data_file_filter: set[DataFileC | |
| import pyarrow as pa | ||
|
|
||
| if not snapshot_id and not self.tbl.metadata.current_snapshot(): | ||
| return self._get_files_schema().empty_table() | ||
| return _from_pylist([], self._get_files_schema()) | ||
|
|
||
| snapshot = self._get_snapshot(snapshot_id) | ||
| io = self.tbl.io | ||
|
|
@@ -969,7 +999,7 @@ def _all_files(self, data_file_filter: set[DataFileContent] | None = None) -> pa | |
|
|
||
| snapshots = self.tbl.snapshots() | ||
| if not snapshots: | ||
| return pa.Table.from_pylist([], schema=self._get_files_schema()) | ||
| return _from_pylist([], self._get_files_schema()) | ||
|
|
||
| executor = ExecutorFactory.get_or_create() | ||
| encryption_manager = self.tbl.encryption_manager | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thanks. readable_metrics types the bounds of a top-level list or map column with the column's type, so
list<uuid>andmap<string, uuid>columns still failed._storage_typenow also replaces extension types inside lists and maps, andtest_inspect_files_and_entries_with_uuid_in_list_and_mapcovers both. It fails without the change withArrowNotImplementedError: extension.