-
Couldn't load subscription status.
- Fork 380
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 |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ | |
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| # pylint: disable=keyword-arg-before-vararg | ||
| from __future__ import annotations | ||
|
|
||
| from enum import Enum | ||
| from typing import Annotated, Any, Callable, Dict, List, Optional, Union | ||
|
|
||
|
|
@@ -26,7 +28,8 @@ | |
| model_validator, | ||
| ) | ||
|
|
||
| from pyiceberg.schema import Schema | ||
| from pyiceberg.exceptions import ValidationError | ||
| from pyiceberg.schema import Schema, index_by_id | ||
| from pyiceberg.transforms import IdentityTransform, Transform, parse_transform | ||
| from pyiceberg.typedef import IcebergBaseModel | ||
| from pyiceberg.types import IcebergType | ||
|
|
@@ -168,6 +171,18 @@ 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})" | ||
|
|
||
| @staticmethod | ||
| def check_compatibility(sort_order: SortOrder, schema: Schema) -> None: | ||
|
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. It strikes me as a bit of an anti-pattern to have a static method whose first argument is of the type of its class, could we make this a normal method? Maybe renaming to |
||
| schema_ids = index_by_id(schema) | ||
| for field in sort_order.fields: | ||
| if source_field := schema_ids.get(field.source_id): | ||
| 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}") | ||
| else: | ||
| raise ValidationError(f"Cannot find source column for sort field: {field}") | ||
|
|
||
|
|
||
| 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 |
|---|---|---|
|
|
@@ -31,9 +31,14 @@ | |
| NoSuchNamespaceError, | ||
| NoSuchTableError, | ||
| TableAlreadyExistsError, | ||
| ValidationError, | ||
| ) | ||
| from pyiceberg.io import WAREHOUSE | ||
| from pyiceberg.partitioning import PartitionField, PartitionSpec | ||
| from pyiceberg.schema import Schema | ||
| from pyiceberg.table.sorting import SortOrder | ||
| from pyiceberg.transforms import DayTransform | ||
| from pyiceberg.types import IntegerType, NestedField, TimestampType | ||
| from tests.conftest import clean_up | ||
|
|
||
|
|
||
|
|
@@ -247,6 +252,59 @@ def test_table_exists(test_catalog: Catalog, table_schema_nested: Schema, databa | |
| assert test_catalog.table_exists((database_name, table_name)) is True | ||
|
|
||
|
|
||
| @pytest.mark.integration | ||
| @pytest.mark.parametrize("test_catalog", CATALOGS) | ||
| def test_incompatible_partitioned_schema_evolution( | ||
| test_catalog: Catalog, test_schema: Schema, test_partition_spec: PartitionSpec, database_name: str, table_name: str | ||
| ) -> None: | ||
| if isinstance(test_catalog, HiveCatalog): | ||
| pytest.skip("HiveCatalog does not support schema evolution") | ||
|
|
||
| identifier = (database_name, table_name) | ||
| test_catalog.create_namespace(database_name) | ||
| table = test_catalog.create_table(identifier, test_schema, partition_spec=test_partition_spec) | ||
| assert test_catalog.table_exists(identifier) | ||
|
|
||
| with pytest.raises(ValidationError): | ||
| with table.update_schema() as update: | ||
| update.delete_column("VendorID") | ||
|
|
||
| # Assert column was not dropped | ||
| assert "VendorID" in table.schema().column_names | ||
|
|
||
| with table.transaction() as transaction: | ||
| with transaction.update_spec() as spec_update: | ||
| spec_update.remove_field("VendorID") | ||
|
|
||
| with transaction.update_schema() as schema_update: | ||
| schema_update.delete_column("VendorID") | ||
|
|
||
| assert table.spec() == PartitionSpec(PartitionField(2, 1001, DayTransform(), "tpep_pickup_day"), spec_id=1) | ||
| assert table.schema() == Schema(NestedField(2, "tpep_pickup_datetime", TimestampType(), False)) | ||
|
Comment on lines
+255
to
+283
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. Part of me wonders if it makes sense to test this here? Like yes we should be testing commit_table generally with different types of updates on all catalogs but this test seems maybe a little further away? Not going to turn down more tests but just a thought! 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. I see your concern on adding catalog tests for every small edge case. I do see catalog tests as a way to check any action that writes/reads metadata.json so this fitted my current description, but Im open to get some consensus on what should be tested here 👍🏼 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. I think that is a very valid reason! |
||
|
|
||
|
|
||
| @pytest.mark.integration | ||
| @pytest.mark.parametrize("test_catalog", CATALOGS) | ||
| def test_incompatible_sorted_schema_evolution( | ||
| test_catalog: Catalog, test_schema: Schema, test_sort_order: SortOrder, database_name: str, table_name: str | ||
| ) -> None: | ||
| if isinstance(test_catalog, HiveCatalog): | ||
| pytest.skip("HiveCatalog does not support schema evolution") | ||
|
|
||
| identifier = (database_name, table_name) | ||
| test_catalog.create_namespace(database_name) | ||
| table = test_catalog.create_table(identifier, test_schema, sort_order=test_sort_order) | ||
| assert test_catalog.table_exists(identifier) | ||
|
|
||
| with pytest.raises(ValidationError): | ||
| with table.update_schema() as update: | ||
| update.delete_column("VendorID") | ||
|
|
||
| assert table.schema() == Schema( | ||
| NestedField(1, "VendorID", IntegerType(), False), NestedField(2, "tpep_pickup_datetime", TimestampType(), False) | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.integration | ||
| @pytest.mark.parametrize("test_catalog", CATALOGS) | ||
| def test_create_namespace(test_catalog: Catalog, database_name: str) -> None: | ||
|
|
||
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.
same here