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
9 changes: 6 additions & 3 deletions pyiceberg/catalog/bigquery_metastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from google.oauth2 import service_account
from typing_extensions import override

from pyiceberg.catalog import WAREHOUSE_LOCATION, MetastoreCatalog, PropertiesUpdateSummary
from pyiceberg.catalog import LOCATION, WAREHOUSE_LOCATION, MetastoreCatalog, PropertiesUpdateSummary
from pyiceberg.exceptions import NamespaceAlreadyExistsError, NoSuchNamespaceError, NoSuchTableError, TableAlreadyExistsError
from pyiceberg.io import load_file_io
from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionSpec
Expand Down Expand Up @@ -344,8 +344,11 @@ def load_namespace_properties(self, namespace: str | Identifier) -> Properties:
try:
dataset = self.client.get_dataset(DatasetReference(project=self.project_id, dataset_id=dataset_name))

if dataset and dataset.external_catalog_dataset_options:
return dataset.external_catalog_dataset_options.to_api_repr()
if dataset and (options := dataset.external_catalog_dataset_options):
properties = dict(options.parameters or {})
if location := options.default_storage_location_uri:
properties[LOCATION] = location
return properties
except NotFound as e:
raise NoSuchNamespaceError(f"Namespace {namespace} not found") from e
return {}
Expand Down
46 changes: 46 additions & 0 deletions tests/catalog/test_bigquery_metastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from google.cloud.bigquery.external_config import ExternalCatalogDatasetOptions, ExternalCatalogTableOptions
from pytest_mock import MockFixture

from pyiceberg.catalog import LOCATION
from pyiceberg.catalog.bigquery_metastore import ICEBERG_TABLE_TYPE_VALUE, TABLE_TYPE_PROP, BigQueryMetastoreCatalog
from pyiceberg.exceptions import NamespaceAlreadyExistsError, NoSuchNamespaceError, NoSuchTableError
from pyiceberg.schema import Schema
Expand Down Expand Up @@ -137,6 +138,51 @@ def test_create_namespace_already_exists(mocker: MockFixture, gcp_dataset_name:
catalog.create_namespace(gcp_dataset_name)


def test_load_namespace_properties_returns_flat_parameters(mocker: MockFixture, gcp_dataset_name: str) -> None:
client_mock = MagicMock()

# Round-trip the Dataset that create_namespace persists back through get_dataset,
# mimicking how BigQuery stores and returns the namespace.
def create_dataset(dataset: Dataset) -> Dataset:
client_mock.get_dataset.return_value = dataset
return dataset

client_mock.create_dataset.side_effect = create_dataset

mocker.patch("pyiceberg.catalog.bigquery_metastore.Client", return_value=client_mock)
mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"})

test_catalog = BigQueryMetastoreCatalog(
"test_catalog", **{"gcp.bigquery.project-id": "my-project", "warehouse": "gs://test-bucket/"}
)

properties = {"owner": "test-owner", "comment": "namespace comment", LOCATION: "gs://stale-location/"}
test_catalog.create_namespace(namespace=gcp_dataset_name, properties=properties)

# The authoritative dataset location overrides a stale parameter, matching Java.
expected = {**properties, LOCATION: f"gs://test-bucket/{gcp_dataset_name}.db"}
assert test_catalog.load_namespace_properties(gcp_dataset_name) == expected


def test_load_namespace_properties_without_parameters(mocker: MockFixture, gcp_dataset_name: str) -> None:
client_mock = MagicMock()

mocker.patch("pyiceberg.catalog.bigquery_metastore.Client", return_value=client_mock)
mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"})

test_catalog = BigQueryMetastoreCatalog("test_catalog", **{"gcp.bigquery.project-id": "my-project"})

for location in (None, "", "gs://test-bucket/iceberg-dataset"):
dataset = dataset_mock()
dataset.external_catalog_dataset_options = ExternalCatalogDatasetOptions(default_storage_location_uri=location)
client_mock.get_dataset.return_value = dataset
expected = {LOCATION: location} if location else {}
assert test_catalog.load_namespace_properties(gcp_dataset_name) == expected

client_mock.get_dataset.return_value = Dataset(DatasetReference(dataset_id=gcp_dataset_name, project="test-project"))
assert test_catalog.load_namespace_properties(gcp_dataset_name) == {}


def test_list_tables(mocker: MockFixture, gcp_dataset_name: str) -> None:
client_mock = MagicMock()

Expand Down