Skip to content

Commit fadd31b

Browse files
committed
feat(bigquery): add bigquery_query_biglake_gcs_temp_table sample to query a temporary 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 temporary BigLake table on Cloud Storage (region tag `bigquery_query_biglake_gcs_temp_table`). Solution: - Add `query_biglake_gcs_temp_table(project_id: str, connection_id: str) -> None` in `bigquery/biglake/bigquery_query_biglake_gcs_temp_table.py` (Configure an ephemeral BigLake table definition over Cloud Storage files using ExternalConfig and query it within a single job.) - Add unit tests (`bigquery/biglake/bigquery_query_biglake_gcs_temp_table_test.py`) covering both the success path and API error handling (`GoogleAPICallError`)
1 parent 186be98 commit fadd31b

5 files changed

Lines changed: 185 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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_temp_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_temp_table(
23+
project_id: str,
24+
connection_id: str,
25+
) -> None:
26+
"""Configures and queries a temporary BigLake table backed by Cloud Storage.
27+
28+
Args:
29+
project_id: Google Cloud project ID (for example, 'example-project').
30+
connection_id: Fully qualified BigQuery Cloud Resource connection ID
31+
(for example, 'projects/example-project/locations/us/connections/example-conn').
32+
"""
33+
client = bigquery.Client(project=project_id)
34+
temp_table_name = "example_us_states_temp"
35+
source_uri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv"
36+
prefix_filter = "W%"
37+
38+
# Bind the Cloud Resource connection and explicit schema to a temporary CSV ExternalConfig.
39+
external_config = bigquery.ExternalConfig("CSV")
40+
external_config.source_uris = [source_uri]
41+
external_config.connection_id = connection_id
42+
external_config.schema = [
43+
bigquery.SchemaField("name", "STRING"),
44+
bigquery.SchemaField("post_abbr", "STRING"),
45+
]
46+
external_config.csv_options.skip_leading_rows = 1
47+
48+
job_config = bigquery.QueryJobConfig(
49+
table_definitions={temp_table_name: external_config},
50+
query_parameters=[
51+
bigquery.ScalarQueryParameter("prefix_filter", "STRING", prefix_filter),
52+
],
53+
)
54+
sql = f"""
55+
SELECT
56+
name,
57+
post_abbr
58+
FROM `{temp_table_name}`
59+
WHERE post_abbr LIKE @prefix_filter
60+
ORDER BY post_abbr ASC
61+
LIMIT 10;
62+
"""
63+
64+
try:
65+
print(f"Querying temporary BigLake table {temp_table_name} via {connection_id}")
66+
query_job = client.query(sql, job_config=job_config)
67+
rows = list(query_job.result())
68+
except exceptions.NotFound as err:
69+
print(
70+
f"error: BigLake connection '{connection_id}' or URI '{source_uri}' "
71+
f"not found in project '{project_id}': {err.message}",
72+
file=sys.stderr,
73+
)
74+
return
75+
except exceptions.GoogleAPICallError as err:
76+
print(
77+
f"error: failed to query temporary BigLake table via connection "
78+
f"'{connection_id}' in project '{project_id}': {err.message}",
79+
file=sys.stderr,
80+
)
81+
return
82+
83+
print(f"Resource ID: {connection_id}")
84+
print(f"Job ID: {query_job.job_id}")
85+
print(f"Rows Returned: {len(rows)}")
86+
print(f"Source URI: {source_uri}")
87+
88+
89+
# [END bigquery_query_biglake_gcs_temp_table]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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_temp_table
18+
from google.api_core import exceptions
19+
import pytest
20+
21+
22+
def test_query_biglake_gcs_temp_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_job.result.return_value = [mock.MagicMock(), mock.MagicMock()]
30+
mock_client.query.return_value = mock_job
31+
monkeypatch.setattr(
32+
bigquery_query_biglake_gcs_temp_table.bigquery,
33+
"Client",
34+
lambda project=None: mock_client,
35+
)
36+
37+
bigquery_query_biglake_gcs_temp_table.query_biglake_gcs_temp_table(
38+
project_id="example-project",
39+
connection_id="projects/example-project/locations/us/connections/example-conn",
40+
)
41+
42+
captured = capsys.readouterr()
43+
assert (
44+
"Resource ID: projects/example-project/locations/us/connections/example-conn"
45+
in captured.out
46+
)
47+
assert "Rows Returned: 2" in captured.out
48+
assert mock_client.query.called
49+
50+
51+
def test_query_biglake_gcs_temp_table_error(
52+
monkeypatch: pytest.MonkeyPatch,
53+
capsys: pytest.CaptureFixture[str],
54+
) -> None:
55+
mock_client = mock.MagicMock()
56+
mock_client.query.side_effect = exceptions.NotFound("connection not found")
57+
monkeypatch.setattr(
58+
bigquery_query_biglake_gcs_temp_table.bigquery,
59+
"Client",
60+
lambda project=None: mock_client,
61+
)
62+
63+
bigquery_query_biglake_gcs_temp_table.query_biglake_gcs_temp_table(
64+
project_id="example-project",
65+
connection_id="projects/example-project/locations/us/connections/example-conn",
66+
)
67+
68+
captured = capsys.readouterr()
69+
assert "error:" in captured.err
70+
assert "example-conn" 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)