|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +import os |
| 18 | + |
| 19 | +import pytest |
| 20 | +from pytest_mock import MockFixture |
| 21 | + |
| 22 | +from pyiceberg.catalog.bigquery_metastore import BigQueryMetastoreCatalog |
| 23 | +from pyiceberg.exceptions import NoSuchNamespaceError, NoSuchTableError |
| 24 | +from pyiceberg.io import load_file_io |
| 25 | +from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC |
| 26 | +from pyiceberg.schema import Schema |
| 27 | +from pyiceberg.serializers import ToOutputFile |
| 28 | +from pyiceberg.table.metadata import new_table_metadata |
| 29 | +from pyiceberg.table.sorting import UNSORTED_SORT_ORDER |
| 30 | +from tests.conftest import BQ_TABLE_METADATA_LOCATION_REGEX |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.skipif(os.environ.get("GCP_CREDENTIALS") is None) |
| 34 | +def test_create_table_with_database_location( |
| 35 | + mocker: MockFixture, _bucket_initialize: None, table_schema_nested: Schema, gcp_dataset_name: str, table_name: str |
| 36 | +) -> None: |
| 37 | + mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"}) |
| 38 | + |
| 39 | + catalog_name = "test_ddb_catalog" |
| 40 | + identifier = (gcp_dataset_name, table_name) |
| 41 | + test_catalog = BigQueryMetastoreCatalog( |
| 42 | + catalog_name, |
| 43 | + **{ |
| 44 | + "gcp.bigquery.project-id": "alexstephen-test-1", |
| 45 | + "warehouse": "gs://alexstephen-test-bq-bucket/", |
| 46 | + "gcp.bigquery.credentials-info": os.environ["GCP_CREDENTIALS"], |
| 47 | + }, |
| 48 | + ) |
| 49 | + test_catalog.create_namespace(namespace=gcp_dataset_name) |
| 50 | + table = test_catalog.create_table(identifier, table_schema_nested) |
| 51 | + assert table.name() == identifier |
| 52 | + assert BQ_TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location) |
| 53 | + |
| 54 | + tables_in_namespace = test_catalog.list_tables(namespace=gcp_dataset_name) |
| 55 | + assert identifier in tables_in_namespace |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.skipif(os.environ.get("GCP_CREDENTIALS") is None) |
| 59 | +def test_drop_table_with_database_location( |
| 60 | + mocker: MockFixture, _bucket_initialize: None, table_schema_nested: Schema, gcp_dataset_name: str, table_name: str |
| 61 | +) -> None: |
| 62 | + mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"}) |
| 63 | + |
| 64 | + catalog_name = "test_ddb_catalog" |
| 65 | + identifier = (gcp_dataset_name, table_name) |
| 66 | + test_catalog = BigQueryMetastoreCatalog( |
| 67 | + catalog_name, |
| 68 | + **{ |
| 69 | + "gcp.bigquery.project-id": "alexstephen-test-1", |
| 70 | + "warehouse": "gs://alexstephen-test-bq-bucket/", |
| 71 | + "gcp.bigquery.credentials-info": os.environ["GCP_CREDENTIALS"], |
| 72 | + }, |
| 73 | + ) |
| 74 | + test_catalog.create_namespace(namespace=gcp_dataset_name) |
| 75 | + test_catalog.create_table(identifier, table_schema_nested) |
| 76 | + test_catalog.drop_table(identifier) |
| 77 | + |
| 78 | + tables_in_namespace_after_drop = test_catalog.list_tables(namespace=gcp_dataset_name) |
| 79 | + assert identifier not in tables_in_namespace_after_drop |
| 80 | + |
| 81 | + # Expect that the table no longer exists. |
| 82 | + try: |
| 83 | + test_catalog.load_table(identifier) |
| 84 | + raise AssertionError() |
| 85 | + except NoSuchTableError: |
| 86 | + assert True |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.skipif(os.environ.get("GCP_CREDENTIALS") is None) |
| 90 | +def test_create_and_drop_namespace( |
| 91 | + mocker: MockFixture, _bucket_initialize: None, table_schema_nested: Schema, gcp_dataset_name: str, table_name: str |
| 92 | +) -> None: |
| 93 | + mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"}) |
| 94 | + |
| 95 | + # Create namespace. |
| 96 | + catalog_name = "test_ddb_catalog" |
| 97 | + test_catalog = BigQueryMetastoreCatalog( |
| 98 | + catalog_name, |
| 99 | + **{ |
| 100 | + "gcp.bigquery.project-id": "alexstephen-test-1", |
| 101 | + "warehouse": "gs://alexstephen-test-bq-bucket/", |
| 102 | + "gcp.bigquery.credentials-info": os.environ["GCP_CREDENTIALS"], |
| 103 | + }, |
| 104 | + ) |
| 105 | + test_catalog.create_namespace(namespace=gcp_dataset_name) |
| 106 | + |
| 107 | + # Ensure that the namespace exists. |
| 108 | + namespaces = test_catalog.list_namespaces() |
| 109 | + assert (gcp_dataset_name,) in namespaces |
| 110 | + |
| 111 | + # Drop the namespace and ensure it does not exist. |
| 112 | + test_catalog.drop_namespace(namespace=gcp_dataset_name) |
| 113 | + namespaces_after_drop = test_catalog.list_namespaces() |
| 114 | + assert (gcp_dataset_name,) not in namespaces_after_drop |
| 115 | + |
| 116 | + # Verify with load_namespace_properties as well |
| 117 | + with pytest.raises(NoSuchNamespaceError): |
| 118 | + test_catalog.load_namespace_properties(gcp_dataset_name) |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.skipif(os.environ.get("GCP_CREDENTIALS") is None) |
| 122 | +def test_register_table( |
| 123 | + mocker: MockFixture, _bucket_initialize: None, table_schema_nested: Schema, gcp_dataset_name: str, table_name: str |
| 124 | +) -> None: |
| 125 | + mocker.patch.dict(os.environ, values={"PYICEBERG_LEGACY_CURRENT_SNAPSHOT_ID": "True"}) |
| 126 | + |
| 127 | + catalog_name = "test_bq_register_catalog" |
| 128 | + identifier = (gcp_dataset_name, table_name) |
| 129 | + warehouse_path = "gs://alexstephen-test-bq-bucket/" # Matches conftest BUCKET_NAME for GCS interaction |
| 130 | + |
| 131 | + test_catalog = BigQueryMetastoreCatalog( |
| 132 | + catalog_name, |
| 133 | + **{ |
| 134 | + "gcp.bigquery.project-id": "alexstephen-test-1", |
| 135 | + "warehouse": "gs://alexstephen-test-bq-bucket/", |
| 136 | + "gcp.bigquery.credentials-info": os.environ["GCP_CREDENTIALS"], |
| 137 | + }, |
| 138 | + ) |
| 139 | + |
| 140 | + test_catalog.create_namespace(namespace=gcp_dataset_name) |
| 141 | + |
| 142 | + # Manually create a metadata file in GCS |
| 143 | + table_gcs_location = f"{warehouse_path.rstrip('/')}/{gcp_dataset_name}.db/{table_name}" |
| 144 | + # Construct a unique metadata file name similar to how pyiceberg would |
| 145 | + metadata_file_name = "00000-aaaaaaaa-aaaa-4aaa-aaaa-aaaaaaaaaaaa.metadata.json" |
| 146 | + metadata_gcs_path = f"{table_gcs_location}/metadata/{metadata_file_name}" |
| 147 | + |
| 148 | + metadata = new_table_metadata( |
| 149 | + location=table_gcs_location, |
| 150 | + schema=table_schema_nested, |
| 151 | + properties={}, |
| 152 | + partition_spec=UNPARTITIONED_PARTITION_SPEC, |
| 153 | + sort_order=UNSORTED_SORT_ORDER, |
| 154 | + ) |
| 155 | + io = load_file_io(properties=test_catalog.properties, location=metadata_gcs_path) |
| 156 | + test_catalog._write_metadata(metadata, io, metadata_gcs_path) |
| 157 | + ToOutputFile.table_metadata(metadata, io.new_output(metadata_gcs_path), overwrite=True) |
| 158 | + |
| 159 | + # Register the table |
| 160 | + registered_table = test_catalog.register_table(identifier, metadata_gcs_path) |
| 161 | + |
| 162 | + assert registered_table.name() == identifier |
| 163 | + assert registered_table.metadata_location == metadata_gcs_path |
| 164 | + assert registered_table.metadata.location == table_gcs_location |
| 165 | + assert BQ_TABLE_METADATA_LOCATION_REGEX.match(registered_table.metadata_location) |
| 166 | + |
| 167 | + # Verify table exists and is loadable |
| 168 | + loaded_table = test_catalog.load_table(identifier) |
| 169 | + assert loaded_table.name() == registered_table.name() |
| 170 | + assert loaded_table.metadata_location == metadata_gcs_path |
| 171 | + |
| 172 | + # Clean up |
| 173 | + test_catalog.drop_table(identifier) |
| 174 | + test_catalog.drop_namespace(gcp_dataset_name) |
0 commit comments