diff --git a/src/tower/_tables.py b/src/tower/_tables.py index 78fd73ce..b94aba81 100644 --- a/src/tower/_tables.py +++ b/src/tower/_tables.py @@ -41,6 +41,7 @@ from .tower_api_client.models import CatalogCredentials from .utils.tables import ( make_table_name, + namespace_of, namespace_or_default, ) @@ -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 @@ -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 diff --git a/src/tower/utils/tables.py b/src/tower/utils/tables.py index 49db08f5..a2cead97 100644 --- a/src/tower/utils/tables.py +++ b/src/tower/utils/tables.py @@ -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" diff --git a/tests/tower/test_tables.py b/tests/tower/test_tables.py index 3d574988..b68d9b8e 100644 --- a/tests/tower/test_tables.py +++ b/tests/tower/test_tables.py @@ -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",))