Skip to content
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

fix: schema check of iceberg logical types #856

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions pyiceberg/table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,14 @@
TableVersion,
)
from pyiceberg.types import (
FixedType,
IcebergType,
ListType,
MapType,
NestedField,
PrimitiveType,
StructType,
UUIDType,
transform_dict_value_to_str,
)
from pyiceberg.utils.concurrent import ExecutorFactory
Expand All @@ -153,7 +155,21 @@
ALWAYS_TRUE = AlwaysTrue()
TABLE_ROOT_ID = -1

_JAVA_LONG_MAX = 9223372036854775807

def _apply_logical_conversion(table_schema: Schema, task_schema: Schema) -> Schema:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can defer this to a later PR, but just want to put it out here.

In PyIceberg we have a little bit an obsession with the visitor pattern. Using the SchemaWithPartnerVisitor you can traverse two schema's at once. An example is the ArrowProjectionVisitor and can be found in pyarrow.py. The ArrowAccessor does the lookups by ID, but I think here name makes more sense since we don't have the IDs (yet).

all_fields = []
for task_field in task_schema.fields:
table_field = table_schema.find_field(task_field.name)
new_type = None
if isinstance(table_field.field_type, UUIDType):
if isinstance(task_field.field_type, FixedType) and len(task_field.field_type) == 16:
new_type = UUIDType()
if new_type is not None:
all_fields.append(NestedField(task_field.field_id, task_field.name, new_type, task_field.required))
else:
all_fields.append(task_field)

return Schema(*all_fields)


def _check_schema_compatible(table_schema: Schema, other_schema: "pa.Schema") -> None:
Expand All @@ -176,7 +192,7 @@ def _check_schema_compatible(table_schema: Schema, other_schema: "pa.Schema") ->
raise ValueError(
f"PyArrow table contains more columns: {', '.join(sorted(additional_names))}. Update the schema first (hint, use union_by_name)."
) from e

task_schema = _apply_logical_conversion(table_schema, task_schema)
if table_schema.as_struct() != task_schema.as_struct():
from rich.console import Console
from rich.table import Table as RichTable
Expand Down