Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyiceberg/catalog/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ def _create_table(
if location:
location = location.rstrip("/")
request = CreateTableRequest(
name=namespace_and_table["table"],
name=self._identifier_to_validated_tuple(identifier)[-1],
location=location,
table_schema=fresh_schema,
partition_spec=fresh_partition_spec,
Expand Down Expand Up @@ -869,7 +869,7 @@ def register_table(self, identifier: str | Identifier, metadata_location: str) -
self._check_endpoint(Capability.V1_REGISTER_TABLE)
namespace_and_table = self._split_identifier_for_path(identifier)
request = RegisterTableRequest(
name=namespace_and_table["table"],
name=self._identifier_to_validated_tuple(identifier)[-1],
metadata_location=metadata_location,
)
serialized_json = request.model_dump_json().encode(UTF8)
Expand Down
140 changes: 140 additions & 0 deletions tests/integration/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from pyiceberg.table.metadata import INITIAL_SPEC_ID
from pyiceberg.table.sorting import INITIAL_SORT_ORDER_ID, SortField, SortOrder
from pyiceberg.transforms import BucketTransform, DayTransform, IdentityTransform
from pyiceberg.typedef import Identifier
from pyiceberg.types import IntegerType, LongType, NestedField, TimestampType, UUIDType
from tests.conftest import clean_up

Expand Down Expand Up @@ -635,3 +636,142 @@ def test_rest_custom_namespace_separator(rest_catalog: RestCatalog, table_schema

loaded_table = rest_catalog.load_table(identifier=full_table_identifier_tuple)
assert loaded_table.name() == full_table_identifier_tuple


def _namespace_exists(catalog: Catalog, namespace: str | Identifier) -> bool:
try:
catalog.load_namespace_properties(namespace)
return True
except NoSuchNamespaceError:
return False
Comment on lines +641 to +646
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just add namespace_exists to all the catalog implementations?
similar to the one in the rest catalog,

@retry(**_RETRY_ARGS)
def namespace_exists(self, namespace: str | Identifier) -> bool:
namespace_tuple = self._check_valid_namespace_identifier(namespace)
namespace = self._encode_namespace_path(namespace_tuple)
# fallback in order to work with older rest catalog implementations
if Capability.V1_NAMESPACE_EXISTS not in self._supported_endpoints:
try:
self.load_namespace_properties(namespace_tuple)
return True
except NoSuchNamespaceError:
return False
response = self._session.head(self.url(Endpoints.namespace_exists, namespace=namespace))
if response.status_code == 404:
return False
elif response.status_code in (200, 204):
return True
try:
response.raise_for_status()
except HTTPError as exc:
_handle_non_200_response(exc, {})
return False

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree 100% with this. I created #2969. I can make a separate PR, but I feel like it'll potentially muddy this one to add it here. How does that sound?



@pytest.mark.integration
@pytest.mark.parametrize("test_catalog", CATALOGS)
def test_namespace_with_slash(test_catalog: Catalog) -> None:
if isinstance(test_catalog, HiveCatalog):
pytest.skip(f"{type(test_catalog).__name__} does not support slash in namespace")

namespace = ("new/db",)

if _namespace_exists(test_catalog, namespace):
test_catalog.drop_namespace(namespace)

assert not _namespace_exists(test_catalog, namespace)

test_catalog.create_namespace(namespace)
assert _namespace_exists(test_catalog, namespace)

properties = test_catalog.load_namespace_properties(namespace)
assert properties is not None

test_catalog.drop_namespace(namespace)
assert not _namespace_exists(test_catalog, namespace)


@pytest.mark.integration
@pytest.mark.parametrize("test_catalog", CATALOGS)
def test_namespace_with_dot(test_catalog: Catalog) -> None:
if isinstance(test_catalog, (HiveCatalog, SqlCatalog)):
pytest.skip(f"{type(test_catalog).__name__} does not support dot in namespace")

namespace = ("new.db",)

if _namespace_exists(test_catalog, namespace):
test_catalog.drop_namespace(namespace)

assert not _namespace_exists(test_catalog, namespace)

test_catalog.create_namespace(namespace)
assert _namespace_exists(test_catalog, namespace)

# REST Catalog fixture treats this as a hierarchical namespace.
# Calling list namespaces will get `new`, not `new.db`.
if isinstance(test_catalog, RestCatalog):
namespaces = test_catalog.list_namespaces()
assert ("new",) in namespaces or ("new.db",) in namespaces
else:
assert namespace in test_catalog.list_namespaces()

properties = test_catalog.load_namespace_properties(namespace)
assert properties is not None

test_catalog.drop_namespace(namespace)
assert not _namespace_exists(test_catalog, namespace)


@pytest.mark.integration
@pytest.mark.parametrize("test_catalog", CATALOGS)
def test_table_name_with_slash(test_catalog: Catalog, table_schema_simple: Schema) -> None:
if isinstance(test_catalog, (HiveCatalog, SqlCatalog)):
pytest.skip(f"{type(test_catalog).__name__} does not support slash in table name")

namespace = ("ns_slash",)
table_ident = ("ns_slash", "tab/le")

if not _namespace_exists(test_catalog, namespace):
test_catalog.create_namespace(namespace)

if test_catalog.table_exists(table_ident):
test_catalog.drop_table(table_ident)

assert not test_catalog.table_exists(table_ident)

test_catalog.create_table(table_ident, table_schema_simple)
assert test_catalog.table_exists(table_ident)

table = test_catalog.load_table(table_ident)
assert table.schema().as_struct() == table_schema_simple.as_struct()

test_catalog.drop_table(table_ident)
assert not test_catalog.table_exists(table_ident)


@pytest.mark.integration
@pytest.mark.parametrize("test_catalog", CATALOGS)
def test_table_name_with_dot(test_catalog: Catalog, table_schema_simple: Schema) -> None:
if isinstance(test_catalog, (HiveCatalog, SqlCatalog)):
pytest.skip(f"{type(test_catalog).__name__} does not support dot in table name")

namespace = ("ns_dot",)
table_ident = ("ns_dot", "ta.ble")

if not _namespace_exists(test_catalog, namespace):
test_catalog.create_namespace(namespace)

if test_catalog.table_exists(table_ident):
test_catalog.drop_table(table_ident)

assert not test_catalog.table_exists(table_ident)

test_catalog.create_table(table_ident, table_schema_simple)
assert test_catalog.table_exists(table_ident)

assert table_ident in test_catalog.list_tables(namespace)

table = test_catalog.load_table(table_ident)
assert table.schema().as_struct() == table_schema_simple.as_struct()

test_catalog.drop_table(table_ident)
assert not test_catalog.table_exists(table_ident)


@pytest.mark.integration
@pytest.mark.parametrize("test_catalog", CATALOGS)
def test_drop_missing_table(test_catalog: Catalog, database_name: str) -> None:
test_catalog.create_namespace_if_not_exists(database_name)
table_ident = (database_name, "missing_table")
assert not test_catalog.table_exists(table_ident)
with pytest.raises(NoSuchTableError):
test_catalog.drop_table(table_ident)


@pytest.mark.integration
@pytest.mark.parametrize("test_catalog", CATALOGS)
def test_drop_nonexistent_namespace(test_catalog: Catalog) -> None:
if isinstance(test_catalog, HiveCatalog):
pytest.skip("HiveCatalog raises NoSuchObjectException instead of NoSuchNamespaceError")

namespace = ("non_existent_namespace",)
with pytest.raises(NoSuchNamespaceError):
test_catalog.drop_namespace(namespace)