Skip to content

Commit 79ef281

Browse files
authored
feat(bigquery): add bigquery_federated_view_schema_cloud_sql sample to inspect Cloud SQL table schema via federated query (#14735)
Problem: The BigQuery documentation (https://cloud.google.com/bigquery/docs/cloud-sql-federated-queries) needs a standalone, tested Python code sample demonstrating how to inspect Cloud SQL table schema via federated query (region tag `bigquery_federated_view_schema_cloud_sql`). Solution: - Add `federated_view_schema_cloud_sql(project_id: str, connection_id: str) -> None` in `bigquery/federated_queries/bigquery_federated_view_schema_cloud_sql.py` (Query information_schema.columns in a Cloud SQL database from BigQuery using EXTERNAL_QUERY.) - Add unit tests (`bigquery/federated_queries/bigquery_federated_view_schema_cloud_sql_test.py`) covering both the success path and API error handling (`GoogleAPICallError`)
1 parent 45256ad commit 79ef281

2 files changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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_federated_view_schema_cloud_sql]
16+
import sys
17+
18+
from google.api_core import exceptions
19+
from google.cloud import bigquery
20+
21+
22+
def federated_view_schema_cloud_sql(
23+
project_id: str,
24+
connection_id: str,
25+
) -> None:
26+
"""Views a Cloud SQL table schema from BigQuery using EXTERNAL_QUERY.
27+
28+
Args:
29+
project_id: Google Cloud project ID (for example, 'example-project').
30+
connection_id: Fully qualified BigQuery Cloud SQL connection ID
31+
(for example, 'example-project.us.example-cloudsql-conn').
32+
"""
33+
client = bigquery.Client(project=project_id)
34+
# Query Cloud SQL information_schema.columns inside EXTERNAL_QUERY to inspect table column metadata.
35+
external_sql = (
36+
"SELECT column_name, data_type, is_nullable "
37+
"FROM information_schema.columns "
38+
"WHERE table_name = 'orders' "
39+
"ORDER BY ordinal_position ASC"
40+
)
41+
query = f"""
42+
SELECT
43+
column_name,
44+
data_type,
45+
is_nullable
46+
FROM EXTERNAL_QUERY('{connection_id}', "{external_sql}")
47+
ORDER BY column_name ASC;
48+
"""
49+
50+
try:
51+
print(f"Inspecting Cloud SQL table schema via connection: {connection_id}")
52+
query_job = client.query(query)
53+
rows = list(query_job.result())
54+
except exceptions.NotFound as err:
55+
print(
56+
f"error: Cloud SQL connection '{connection_id}' not found in project "
57+
f"'{project_id}': {err.message}",
58+
file=sys.stderr,
59+
)
60+
return
61+
except exceptions.GoogleAPICallError as err:
62+
print(
63+
f"error: could not inspect Cloud SQL table schema on connection "
64+
f"'{connection_id}' in project '{project_id}': {err.message}",
65+
file=sys.stderr,
66+
)
67+
return
68+
69+
print(f"Resource ID: {connection_id}")
70+
print(f"Columns Returned: {len(rows)}")
71+
print(f"Job ID: {query_job.job_id}")
72+
73+
74+
# [END bigquery_federated_view_schema_cloud_sql]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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_federated_view_schema_cloud_sql
18+
from google.api_core import exceptions
19+
import pytest
20+
21+
22+
def test_federated_view_schema_cloud_sql_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_federated_view_schema_cloud_sql.bigquery,
33+
"Client",
34+
lambda project=None: mock_client,
35+
)
36+
37+
bigquery_federated_view_schema_cloud_sql.federated_view_schema_cloud_sql(
38+
project_id="example-project",
39+
connection_id="example-project.us.example-cloudsql-conn",
40+
)
41+
42+
captured = capsys.readouterr()
43+
assert "Resource ID: example-project.us.example-cloudsql-conn" in captured.out
44+
assert "Columns Returned: 2" in captured.out
45+
assert mock_client.query.called
46+
47+
48+
def test_federated_view_schema_cloud_sql_error(
49+
monkeypatch: pytest.MonkeyPatch,
50+
capsys: pytest.CaptureFixture[str],
51+
) -> None:
52+
mock_client = mock.MagicMock()
53+
mock_job = mock.MagicMock()
54+
mock_job.result.side_effect = exceptions.NotFound("connection not found")
55+
mock_client.query.return_value = mock_job
56+
monkeypatch.setattr(
57+
bigquery_federated_view_schema_cloud_sql.bigquery,
58+
"Client",
59+
lambda project=None: mock_client,
60+
)
61+
62+
bigquery_federated_view_schema_cloud_sql.federated_view_schema_cloud_sql(
63+
project_id="example-project",
64+
connection_id="example-project.us.example-cloudsql-conn",
65+
)
66+
67+
captured = capsys.readouterr()
68+
assert "error:" in captured.err
69+
assert "example-cloudsql-conn" in captured.err

0 commit comments

Comments
 (0)