|
| 1 | +import pytest |
| 2 | + |
| 3 | +from pyiceberg.catalog import Catalog |
| 4 | +from pyiceberg.catalog.hive import ( |
| 5 | + HiveCatalog, |
| 6 | +) |
| 7 | +from pyiceberg.exceptions import NoSuchTableError, TableAlreadyExistsError |
| 8 | +from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionSpec |
| 9 | +from pyiceberg.schema import Schema |
| 10 | +from pyiceberg.table import Table |
| 11 | +from pyiceberg.types import ( |
| 12 | + BooleanType, |
| 13 | + DateType, |
| 14 | + IntegerType, |
| 15 | + NestedField, |
| 16 | + StringType, |
| 17 | +) |
| 18 | + |
| 19 | +TABLE_SCHEMA = Schema( |
| 20 | + NestedField(field_id=1, name="foo", field_type=BooleanType(), required=False), |
| 21 | + NestedField(field_id=2, name="bar", field_type=StringType(), required=False), |
| 22 | + NestedField(field_id=4, name="baz", field_type=IntegerType(), required=False), |
| 23 | + NestedField(field_id=10, name="qux", field_type=DateType(), required=False), |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +def _create_table( |
| 28 | + session_catalog: Catalog, |
| 29 | + identifier: str, |
| 30 | + format_version: int, |
| 31 | + location: str, |
| 32 | + partition_spec: PartitionSpec = UNPARTITIONED_PARTITION_SPEC, |
| 33 | + schema: Schema = TABLE_SCHEMA, |
| 34 | +) -> Table: |
| 35 | + try: |
| 36 | + session_catalog.drop_table(identifier=identifier) |
| 37 | + except NoSuchTableError: |
| 38 | + pass |
| 39 | + |
| 40 | + return session_catalog.create_table( |
| 41 | + identifier=identifier, |
| 42 | + schema=schema, |
| 43 | + location=location, |
| 44 | + properties={"format-version": str(format_version)}, |
| 45 | + partition_spec=partition_spec, |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.integration |
| 50 | +def test_hive_register_table( |
| 51 | + session_catalog: HiveCatalog, |
| 52 | +) -> None: |
| 53 | + identifier = "default.hive_register_table" |
| 54 | + location = "s3a://warehouse/default/hive_register_table" |
| 55 | + tbl = _create_table(session_catalog, identifier, 2, location) |
| 56 | + assert session_catalog.table_exists(identifier=identifier) |
| 57 | + session_catalog.drop_table(identifier=identifier) |
| 58 | + assert not session_catalog.table_exists(identifier=identifier) |
| 59 | + session_catalog.register_table(("default", "hive_register_table"), metadata_location=tbl.metadata_location) |
| 60 | + assert session_catalog.table_exists(identifier=identifier) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.integration |
| 64 | +def test_hive_register_table_existing( |
| 65 | + session_catalog: HiveCatalog, |
| 66 | +) -> None: |
| 67 | + identifier = "default.hive_register_table_existing" |
| 68 | + location = "s3a://warehouse/default/hive_register_table_existing" |
| 69 | + tbl = _create_table(session_catalog, identifier, 2, location) |
| 70 | + assert session_catalog.table_exists(identifier=identifier) |
| 71 | + # Assert that registering the table again raises TableAlreadyExistsError |
| 72 | + with pytest.raises(TableAlreadyExistsError): |
| 73 | + session_catalog.register_table(("default", "hive_register_table_existing"), metadata_location=tbl.metadata_location) |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.integration |
| 77 | +def test_rest_register_table( |
| 78 | + session_catalog: Catalog, |
| 79 | +) -> None: |
| 80 | + identifier = "default.rest_register_table" |
| 81 | + location = "s3a://warehouse/default/rest_register_table" |
| 82 | + tbl = _create_table(session_catalog, identifier, 2, location) |
| 83 | + assert session_catalog.table_exists(identifier=identifier) |
| 84 | + session_catalog.drop_table(identifier=identifier) |
| 85 | + assert not session_catalog.table_exists(identifier=identifier) |
| 86 | + session_catalog.register_table(identifier=identifier, metadata_location=tbl.metadata_location) |
| 87 | + assert session_catalog.table_exists(identifier=identifier) |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.integration |
| 91 | +def test_rest_register_table_existing( |
| 92 | + session_catalog: Catalog, |
| 93 | +) -> None: |
| 94 | + identifier = "default.rest_register_table_existing" |
| 95 | + location = "s3a://warehouse/default/rest_register_table_existing" |
| 96 | + tbl = _create_table(session_catalog, identifier, 2, location) |
| 97 | + assert session_catalog.table_exists(identifier=identifier) |
| 98 | + # Assert that registering the table again raises TableAlreadyExistsError |
| 99 | + with pytest.raises(TableAlreadyExistsError): |
| 100 | + session_catalog.register_table(identifier=identifier, metadata_location=tbl.metadata_location) |
0 commit comments