Skip to content

Commit 014b46c

Browse files
committed
feat(bigquery): add bigquery_search_exclude_columns sample to search a table while excluding specific columns
Problem: The BigQuery documentation (https://cloud.google.com/bigquery/docs/search) needs a standalone, tested Python code sample demonstrating how to search a table while excluding specific columns (region tag `bigquery_search_exclude_columns`). Solution: - Add `search_exclude_columns(project_id: str) -> None` in `bigquery/search/bigquery_search_exclude_columns.py` (Search all columns of a BigQuery table except specified excluded columns using SELECT AS STRUCT * EXCEPT(...).) - Add unit tests (`bigquery/search/bigquery_search_exclude_columns_test.py`) covering both the success path and API error handling (`GoogleAPICallError`)
1 parent 186be98 commit 014b46c

5 files changed

Lines changed: 174 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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_search_exclude_columns]
16+
import sys
17+
18+
from google.api_core import exceptions
19+
from google.cloud import bigquery
20+
21+
22+
def search_exclude_columns(
23+
project_id: str,
24+
) -> None:
25+
"""Searches a BigQuery table while excluding specific columns using EXCEPT.
26+
27+
Args:
28+
project_id: Google Cloud project ID used to bill the query job
29+
(for example, 'example-project').
30+
"""
31+
client = bigquery.Client(project=project_id)
32+
# Query the public theLook eCommerce dataset in bigquery-public-data
33+
# (https://cloud.google.com/bigquery/public-data).
34+
table_ref = "bigquery-public-data.thelook_ecommerce.users"
35+
# Exclude street_address, city, state, and country from the searched fields using SELECT AS STRUCT ... EXCEPT.
36+
search_query = "Nelson Apt"
37+
job_config = bigquery.QueryJobConfig(
38+
query_parameters=[
39+
bigquery.ScalarQueryParameter("search_query", "STRING", search_query),
40+
]
41+
)
42+
sql = f"""
43+
SELECT
44+
u.id,
45+
u.first_name,
46+
u.last_name,
47+
u.email
48+
FROM `{table_ref}` AS u
49+
WHERE SEARCH(
50+
(SELECT AS STRUCT u.* EXCEPT (street_address, city, state, country)),
51+
@search_query
52+
)
53+
ORDER BY u.id ASC
54+
LIMIT 10;
55+
"""
56+
57+
try:
58+
print(
59+
f"Searching {table_ref} excluding address columns for term: {search_query}"
60+
)
61+
query_job = client.query(sql, job_config=job_config)
62+
rows = list(query_job.result())
63+
except exceptions.NotFound as err:
64+
print(
65+
f"error: table '{table_ref}' not found when querying from project "
66+
f"'{project_id}': {err.message}",
67+
file=sys.stderr,
68+
)
69+
return
70+
except exceptions.GoogleAPICallError as err:
71+
print(
72+
f"error: failed to search '{table_ref}' with excluded columns in project "
73+
f"'{project_id}': {err.message}",
74+
file=sys.stderr,
75+
)
76+
return
77+
78+
print(f"Resource ID: {table_ref}")
79+
print(f"Job ID: {query_job.job_id}")
80+
print(f"Matching Rows: {len(rows)}")
81+
print(f"Search Term: {search_query}")
82+
83+
84+
# [END bigquery_search_exclude_columns]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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_search_exclude_columns
18+
from google.api_core import exceptions
19+
import pytest
20+
21+
22+
def test_search_exclude_columns_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 = []
30+
mock_client.query.return_value = mock_job
31+
monkeypatch.setattr(
32+
bigquery_search_exclude_columns.bigquery,
33+
"Client",
34+
lambda project=None: mock_client,
35+
)
36+
37+
bigquery_search_exclude_columns.search_exclude_columns(
38+
project_id="example-project",
39+
)
40+
41+
captured = capsys.readouterr()
42+
assert "Resource ID: bigquery-public-data.thelook_ecommerce.users" in captured.out
43+
assert "Matching Rows: 0" in captured.out
44+
assert mock_client.query.called
45+
46+
47+
def test_search_exclude_columns_error(
48+
monkeypatch: pytest.MonkeyPatch,
49+
capsys: pytest.CaptureFixture[str],
50+
) -> None:
51+
mock_client = mock.MagicMock()
52+
mock_client.query.side_effect = exceptions.NotFound("resource error")
53+
monkeypatch.setattr(
54+
bigquery_search_exclude_columns.bigquery,
55+
"Client",
56+
lambda project=None: mock_client,
57+
)
58+
59+
bigquery_search_exclude_columns.search_exclude_columns(
60+
project_id="example-project",
61+
)
62+
63+
captured = capsys.readouterr()
64+
assert "error:" in captured.err
65+
assert "example-project" in captured.err

‎bigquery/search/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/search/requirements.txt‎

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

0 commit comments

Comments
 (0)