Skip to content

Commit 727a957

Browse files
committed
feat(bigquery): add bigquery_query_biglake_gcs_table sample to query a permanent BigLake table on Cloud Storage
Problem: The BigQuery documentation (https://cloud.google.com/bigquery/docs/query-cloud-storage-using-biglake) needs a standalone, tested Python code sample demonstrating how to query a permanent BigLake table on Cloud Storage (region tag `bigquery_query_biglake_gcs_table`). Solution: - Add `query_biglake_gcs_table(project_id: str, dataset_id: str, table_id: str) -> None` in `bigquery/biglake/bigquery_query_biglake_gcs_table.py` (Query structured files stored in Cloud Storage through a permanent BigLake table backed by a Cloud Resource connection.) - Add unit tests (`bigquery/biglake/bigquery_query_biglake_gcs_table_test.py`) covering both the success path and API error handling (`GoogleAPICallError`)
1 parent 186be98 commit 727a957

5 files changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START bigquery_query_biglake_gcs_table]
16+
import sys
17+
18+
from google.api_core import exceptions
19+
from google.cloud import bigquery
20+
21+
22+
def query_biglake_gcs_table(
23+
project_id: str,
24+
dataset_id: str,
25+
table_id: str,
26+
) -> None:
27+
"""Queries a BigLake table backed by Cloud Storage using GoogleSQL.
28+
29+
Args:
30+
project_id: Google Cloud project ID (for example, 'example-project').
31+
dataset_id: BigQuery dataset ID containing the BigLake table
32+
(for example, 'example_dataset').
33+
table_id: BigLake table ID to query (for example, 'example_biglake_table').
34+
"""
35+
client = bigquery.Client(project=project_id)
36+
table_ref = f"{project_id}.{dataset_id}.{table_id}"
37+
# BigLake delegates Cloud Storage access to the table's Cloud Resource connection,
38+
# allowing standard SQL queries with fine-grained governance.
39+
sql = f"""
40+
SELECT
41+
id,
42+
label,
43+
score
44+
FROM `{table_ref}`
45+
ORDER BY score DESC, id ASC
46+
LIMIT 10;
47+
"""
48+
49+
try:
50+
print(f"Querying BigLake table: {table_ref}")
51+
query_job = client.query(sql)
52+
rows = list(query_job.result())
53+
except exceptions.NotFound as err:
54+
print(
55+
f"error: BigLake table '{table_id}' not found in dataset '{dataset_id}' "
56+
f"for project '{project_id}': {err.message}",
57+
file=sys.stderr,
58+
)
59+
return
60+
except exceptions.GoogleAPICallError as err:
61+
print(
62+
f"error: failed to query BigLake table '{table_id}' in dataset "
63+
f"'{dataset_id}' for project '{project_id}': {err.message}",
64+
file=sys.stderr,
65+
)
66+
return
67+
68+
print(f"Resource ID: {table_ref}")
69+
print(f"Job ID: {query_job.job_id}")
70+
print(f"Rows Returned: {len(rows)}")
71+
72+
73+
# [END bigquery_query_biglake_gcs_table]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from unittest import mock
16+
17+
import bigquery_query_biglake_gcs_table
18+
from google.api_core import exceptions
19+
import pytest
20+
21+
22+
def test_query_biglake_gcs_table_success(
23+
monkeypatch: pytest.MonkeyPatch,
24+
capsys: pytest.CaptureFixture[str],
25+
) -> None:
26+
mock_client = mock.MagicMock()
27+
mock_job = mock.MagicMock()
28+
mock_job.job_id = "example-job-123"
29+
mock_row = mock.MagicMock()
30+
mock_job.result.return_value = [mock_row]
31+
mock_client.query.return_value = mock_job
32+
monkeypatch.setattr(
33+
bigquery_query_biglake_gcs_table.bigquery,
34+
"Client",
35+
lambda project=None: mock_client,
36+
)
37+
38+
bigquery_query_biglake_gcs_table.query_biglake_gcs_table(
39+
project_id="example-project",
40+
dataset_id="example_dataset",
41+
table_id="example_biglake_table",
42+
)
43+
44+
captured = capsys.readouterr()
45+
assert "Resource ID:" in captured.out
46+
assert "example-project" in captured.out
47+
assert mock_client.query.called
48+
49+
50+
def test_query_biglake_gcs_table_error(
51+
monkeypatch: pytest.MonkeyPatch,
52+
capsys: pytest.CaptureFixture[str],
53+
) -> None:
54+
mock_client = mock.MagicMock()
55+
mock_job = mock.MagicMock()
56+
mock_job.result.side_effect = exceptions.NotFound("resource error")
57+
mock_client.query.return_value = mock_job
58+
monkeypatch.setattr(
59+
bigquery_query_biglake_gcs_table.bigquery,
60+
"Client",
61+
lambda project=None: mock_client,
62+
)
63+
64+
bigquery_query_biglake_gcs_table.query_biglake_gcs_table(
65+
project_id="example-project",
66+
dataset_id="example_dataset",
67+
table_id="example_biglake_table",
68+
)
69+
70+
captured = capsys.readouterr()
71+
assert "error:" in captured.err
72+
assert "example-project" in captured.err

‎bigquery/biglake/noxfile_config.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
TEST_CONFIG_OVERRIDE = {
16+
# You can opt out from the test for specific Python versions.
17+
"ignored_versions": ["2.7", "3.7", "3.8", "3.9", "3.10", "3.11"],
18+
# Enforce type hints on new samples.
19+
"enforce_type_hints": True,
20+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
21+
"pip_version_override": None,
22+
"envs": {},
23+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==9.0.3; python_version >= "3.10"

‎bigquery/biglake/requirements.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-cloud-bigquery==3.47.0
2+
google-cloud-bigquery-storage==2.43.0

0 commit comments

Comments
 (0)