hililiwei commented on code in PR #8174:
URL: https://github.com/apache/iceberg/pull/8174#discussion_r1282868971
##########
python/tests/conftest.py:
##########
@@ -153,13 +153,19 @@ def table_schema_nested() -> Schema:
NestedField(
field_id=11,
name="location",
- field_type=ListType(
- element_id=12,
- element_type=StructType(
+ field_type=MapType(
Review Comment:
reverted.
##########
python/pyiceberg/schema.py:
##########
@@ -1082,44 +1101,40 @@ def build_position_accessors(schema_or_type:
Union[Schema, IcebergType]) -> Dict
return visit(schema_or_type, _BuildPositionAccessors())
-class _FindLastFieldId(SchemaVisitor[int]):
- """Traverses the schema to get the highest field-id."""
-
- def schema(self, schema: Schema, struct_result: int) -> int:
- return struct_result
-
- def struct(self, struct: StructType, field_results: List[int]) -> int:
- return max(field_results)
-
- def field(self, field: NestedField, field_result: int) -> int:
- return max(field.field_id, field_result)
-
- def list(self, list_type: ListType, element_result: int) -> int:
- return element_result
+@singledispatch
+def assign_fresh_schema_ids(schema_or_type: Union[Schema, IcebergType],
next_id: Optional[Callable[[], int]] = None) -> Schema:
+ """Traverses the schema, and sets new IDs."""
+ raise ValueError(f"Unsupported type: {schema_or_type}")
- def map(self, map_type: MapType, key_result: int, value_result: int) ->
int:
- return max(key_result, value_result)
- def primitive(self, primitive: PrimitiveType) -> int:
- return 0
+@assign_fresh_schema_ids.register(Schema)
+def _(schema: Schema, next_id: Optional[Callable[[], int]] = None) -> Schema:
+ """Traverses the schema, and sets new IDs."""
+ return pre_order_visit(schema, _SetFreshIDs(next_id_func=next_id))
-def assign_fresh_schema_ids(schema: Schema) -> Schema:
+@assign_fresh_schema_ids.register(IcebergType)
+def _(type_var: IcebergType, next_id: Optional[Callable[[], int]] = None) ->
Schema:
"""Traverses the schema, and sets new IDs."""
- return pre_order_visit(schema, _SetFreshIDs())
+ return pre_order_visit(type_var, _SetFreshIDs(next_id_func=next_id))
class _SetFreshIDs(PreOrderSchemaVisitor[IcebergType]):
"""Traverses the schema and assigns monotonically increasing ids."""
counter: itertools.count # type: ignore
reserved_ids: Dict[int, int]
+ next_id_func: Optional[Callable[[], int]] = None
- def __init__(self, start: int = 1) -> None:
+ def __init__(self, start: int = 1, next_id_func: Optional[Callable[[],
int]] = None) -> None:
self.counter = itertools.count(start)
self.reserved_ids = {}
+ self.next_id_func = next_id_func
def _get_and_increment(self) -> int:
+ if self.next_id_func:
+ return self.next_id_func()
Review Comment:
done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]