-
Notifications
You must be signed in to change notification settings - Fork 426
Block schema field drop if it is reference by an active partition or sort field #2410
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 |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| model_validator, | ||
| ) | ||
|
|
||
| from pyiceberg.exceptions import ValidationError | ||
| from pyiceberg.schema import Schema | ||
| from pyiceberg.transforms import IdentityTransform, Transform, parse_transform | ||
| from pyiceberg.typedef import IcebergBaseModel | ||
|
|
@@ -169,6 +170,16 @@ def __repr__(self) -> str: | |
| fields = f"{', '.join(repr(column) for column in self.fields)}, " if self.fields else "" | ||
| return f"SortOrder({fields}order_id={self.order_id})" | ||
|
|
||
| def check_compatible(self, schema: Schema) -> None: | ||
| for field in self.fields: | ||
| source_field = schema._lazy_id_to_field.get(field.source_id) | ||
| if source_field is None: | ||
| raise ValidationError(f"Cannot find source column for sort field: {field}") | ||
| if not source_field.field_type.is_primitive: | ||
| raise ValidationError(f"Cannot sort by non-primitive source field: {source_field}") | ||
| if not field.transform.can_transform(source_field.field_type): | ||
| raise ValidationError(f"Invalid source type {source_field.field_type} for transform: {field.transform}") | ||
|
Contributor
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. nit: should we include |
||
|
|
||
|
|
||
| UNSORTED_SORT_ORDER_ID = 0 | ||
| UNSORTED_SORT_ORDER = SortOrder(order_id=UNSORTED_SORT_ORDER_ID) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -706,6 +706,12 @@ def update_table_metadata( | |
| if base_metadata.last_updated_ms == new_metadata.last_updated_ms: | ||
| new_metadata = new_metadata.model_copy(update={"last_updated_ms": datetime_to_millis(datetime.now().astimezone())}) | ||
|
|
||
| # Check correctness of partition spec, and sort order | ||
| new_metadata.spec().check_compatible(new_metadata.schema()) | ||
|
|
||
| if sort_order := new_metadata.sort_order_by_id(new_metadata.default_sort_order_id): | ||
|
Contributor
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. nit: i think we can add a sort_order() function to TableMetadata class, i notice its only available in Table. but we can refactor later then we can do |
||
| sort_order.check_compatible(new_metadata.schema()) | ||
|
|
||
| if enforce_validation: | ||
| return TableMetadataUtil.parse_obj(new_metadata.model_dump()) | ||
| else: | ||
|
|
||
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.
nit: doesnt look like
allow_missing_fieldsis used anywhere, should we still keep it?