|
15 | 15 | # specific language governing permissions and limitations
|
16 | 16 | # under the License.
|
17 | 17 | # pylint:disable=redefined-outer-name
|
| 18 | +from typing import Optional |
18 | 19 |
|
19 | 20 | import pytest
|
20 | 21 |
|
@@ -63,12 +64,19 @@ def _table_v2(catalog: Catalog) -> Table:
|
63 | 64 | return _create_table_with_schema(catalog, schema_with_timestamp, "2")
|
64 | 65 |
|
65 | 66 |
|
66 |
| -def _create_table_with_schema(catalog: Catalog, schema: Schema, format_version: str) -> Table: |
| 67 | +def _create_table_with_schema( |
| 68 | + catalog: Catalog, schema: Schema, format_version: str, partition_spec: Optional[PartitionSpec] = None |
| 69 | +) -> Table: |
67 | 70 | tbl_name = "default.test_schema_evolution"
|
68 | 71 | try:
|
69 | 72 | catalog.drop_table(tbl_name)
|
70 | 73 | except NoSuchTableError:
|
71 | 74 | pass
|
| 75 | + |
| 76 | + if partition_spec: |
| 77 | + return catalog.create_table( |
| 78 | + identifier=tbl_name, schema=schema, partition_spec=partition_spec, properties={"format-version": format_version} |
| 79 | + ) |
72 | 80 | return catalog.create_table(identifier=tbl_name, schema=schema, properties={"format-version": format_version})
|
73 | 81 |
|
74 | 82 |
|
@@ -589,3 +597,49 @@ def test_partition_schema_field_name_conflict(catalog: Catalog) -> None:
|
589 | 597 |
|
590 | 598 | table.update_spec().add_field("id", IdentityTransform(), "id").commit()
|
591 | 599 | table.update_spec().add_field("event_ts", YearTransform(), "event_year").commit()
|
| 600 | + |
| 601 | + |
| 602 | +@pytest.mark.integration |
| 603 | +@pytest.mark.parametrize("catalog", [pytest.lazy_fixture("session_catalog_hive"), pytest.lazy_fixture("session_catalog")]) |
| 604 | +def test_partition_validation_during_table_creation(catalog: Catalog) -> None: |
| 605 | + schema = Schema( |
| 606 | + NestedField(1, "id", LongType(), required=False), |
| 607 | + NestedField(2, "event_ts", TimestampType(), required=False), |
| 608 | + NestedField(3, "another_ts", TimestampType(), required=False), |
| 609 | + NestedField(4, "str", StringType(), required=False), |
| 610 | + ) |
| 611 | + |
| 612 | + partition_spec = PartitionSpec( |
| 613 | + PartitionField(source_id=2, field_id=1000, transform=YearTransform(), name="another_ts"), spec_id=1 |
| 614 | + ) |
| 615 | + with pytest.raises(ValueError, match="Cannot create partition from name that exists in schema: another_ts"): |
| 616 | + _create_table_with_schema(catalog, schema, "2", partition_spec) |
| 617 | + |
| 618 | + partition_spec = PartitionSpec( |
| 619 | + PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="id"), spec_id=1 |
| 620 | + ) |
| 621 | + _create_table_with_schema(catalog, schema, "2", partition_spec) |
| 622 | + |
| 623 | + |
| 624 | +@pytest.mark.integration |
| 625 | +@pytest.mark.parametrize("catalog", [pytest.lazy_fixture("session_catalog_hive"), pytest.lazy_fixture("session_catalog")]) |
| 626 | +def test_schema_evolution_partition_conflict(catalog: Catalog) -> None: |
| 627 | + schema = Schema( |
| 628 | + NestedField(1, "id", LongType(), required=False), |
| 629 | + NestedField(2, "event_ts", TimestampType(), required=False), |
| 630 | + ) |
| 631 | + partition_spec = PartitionSpec( |
| 632 | + PartitionField(source_id=2, field_id=1000, transform=YearTransform(), name="event_year"), |
| 633 | + PartitionField(source_id=2, field_id=1001, transform=IdentityTransform(), name="first_name"), |
| 634 | + PartitionField(source_id=1, field_id=1002, transform=IdentityTransform(), name="id"), |
| 635 | + spec_id=1, |
| 636 | + ) |
| 637 | + table = _create_table_with_schema(catalog, schema, "2", partition_spec) |
| 638 | + |
| 639 | + with pytest.raises(ValueError, match="Cannot add column with name that conflicts with partition field: event_year"): |
| 640 | + table.update_schema().add_column("event_year", StringType()).commit() |
| 641 | + |
| 642 | + with pytest.raises(ValueError, match="Cannot add column with name that conflicts with partition field: first_name"): |
| 643 | + table.update_schema().add_column("first_name", StringType()).commit() |
| 644 | + |
| 645 | + table.update_schema().add_column("other_field", StringType()).commit() |
0 commit comments