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
21 changes: 11 additions & 10 deletions src/tower/_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from .tower_api_client.models import CatalogCredentials
from .utils.tables import (
make_table_name,
namespace_of,
namespace_or_default,
)

Expand Down Expand Up @@ -689,13 +690,13 @@ def create(self, schema: pa.Schema) -> Table:
>>> table.insert(new_data)
"""

namespace = namespace_or_default(self._namespace)
table_name = make_table_name(self._name, namespace)
table_name = make_table_name(self._name, self._namespace)
catalog = self._ensure_catalog_mode("read-write")

# We need to create the relevant namespace if it's missing from the
# resolved namespace.
catalog.create_namespace_if_not_exists(namespace)
# We need to create the relevant namespace if it's missing. Take it from
# the resolved table name, because a qualified name like "sales.orders"
# lives in "sales", not in the default namespace.
catalog.create_namespace_if_not_exists(namespace_of(table_name))

# Now that we're certain the namespace exists, we can create the
# underlying table. This will return an error if something went wrong
Expand Down Expand Up @@ -753,13 +754,13 @@ def create_if_not_exists(self, schema: pa.Schema) -> Table:
>>> table = tables("my_table", namespace="my_namespace").create_if_not_exists(schema)
"""

namespace = namespace_or_default(self._namespace)
table_name = make_table_name(self._name, namespace)
table_name = make_table_name(self._name, self._namespace)
catalog = self._ensure_catalog_mode("read-write")

# We need to create the relevant namespace if it's missing from the
# resolved namespace.
catalog.create_namespace_if_not_exists(namespace)
# We need to create the relevant namespace if it's missing. Take it from
# the resolved table name, because a qualified name like "sales.orders"
# lives in "sales", not in the default namespace.
catalog.create_namespace_if_not_exists(namespace_of(table_name))

# We have the catalog, so let's attempt to create the table. It should
# not return an error and instead just return the table if it already
Expand Down
5 changes: 5 additions & 0 deletions src/tower/utils/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ def make_table_name(name: str, namespace: Optional[str]) -> str:
return f"{namespace}.{name}"


def namespace_of(table_name: str) -> str:
"""Return the namespace part of a qualified table name ("sales.orders" -> "sales")."""
return table_name.rsplit(".", 1)[0]


def namespace_or_default(namespace: Optional[str]) -> str:
if namespace is None:
return "default"
Expand Down
25 changes: 25 additions & 0 deletions tests/tower/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1454,3 +1454,28 @@ def test_drop_with_catalog_errors(in_memory_catalog):
with pytest.raises(RuntimeError, match="Catalog connection failed"):
ref.drop()
mock_drop.assert_called_once()


@pytest.mark.parametrize("method", ["create", "create_if_not_exists"])
def test_create_with_qualified_name_creates_its_namespace(in_memory_catalog, method):
"""A qualified name like "sales.orders" lives in the "sales" namespace, so
that is the namespace to create, not the default one."""
schema = pa.schema([pa.field("id", pa.int64())])

ref = tower.tables("sales.orders", catalog=in_memory_catalog)
table = getattr(ref, method)(schema)

assert table._table.name() == ("sales", "orders")
assert in_memory_catalog.list_namespaces() == [("sales",)]
assert tower.tables("orders", catalog=in_memory_catalog, namespace="sales").load()


@pytest.mark.parametrize("method", ["create", "create_if_not_exists"])
def test_create_with_qualified_name_in_nested_namespace(in_memory_catalog, method):
schema = pa.schema([pa.field("id", pa.int64())])

ref = tower.tables("analytics.sales.orders", catalog=in_memory_catalog)
table = getattr(ref, method)(schema)

assert table._table.name() == ("analytics", "sales", "orders")
assert ("analytics", "sales") in in_memory_catalog.list_namespaces(("analytics",))
Loading