Skip to content

Commit 829d3fd

Browse files
authored
feat(bigquery): add bigquery_drop_search_index sample to drop a search index from a table (#14732)
Problem: The BigQuery documentation (https://cloud.google.com/bigquery/docs/search-index) needs a standalone, tested Python code sample demonstrating how to drop a search index from a table (region tag `bigquery_drop_search_index`). Solution: - Add `drop_search_index(project_id: str, dataset_id: str, table_id: str) -> None` in `bigquery/search/bigquery_drop_search_index.py` (Remove a search index from a BigQuery table using DROP SEARCH INDEX IF EXISTS.) - Add unit tests (`bigquery/search/bigquery_drop_search_index_test.py`) covering both the success path and API error handling (`GoogleAPICallError`)
1 parent da977a4 commit 829d3fd

2 files changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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_drop_search_index]
16+
import sys
17+
18+
from google.api_core import exceptions
19+
from google.cloud import bigquery
20+
21+
22+
def drop_search_index(
23+
project_id: str,
24+
dataset_id: str,
25+
table_id: str,
26+
) -> None:
27+
"""Drops a full-text search index from a BigQuery table.
28+
29+
Args:
30+
project_id: Google Cloud project ID (for example, 'example-project').
31+
dataset_id: BigQuery dataset ID containing the indexed table
32+
(for example, 'example_dataset').
33+
table_id: BigQuery table ID from which to drop the search index
34+
(for example, 'example_table').
35+
"""
36+
client = bigquery.Client(project=project_id)
37+
# Target the example_search_index created on the table.
38+
index_name = "example_search_index"
39+
table_ref = f"{project_id}.{dataset_id}.{table_id}"
40+
sql = f"DROP SEARCH INDEX IF EXISTS `{index_name}` ON `{table_ref}`;"
41+
42+
try:
43+
print(f"Dropping search index {index_name} from table: {table_ref}")
44+
query_job = client.query(sql)
45+
query_job.result()
46+
except exceptions.NotFound as err:
47+
print(
48+
f"error: table '{table_id}' or dataset '{dataset_id}' not found in "
49+
f"project '{project_id}': {err.message}",
50+
file=sys.stderr,
51+
)
52+
return
53+
except exceptions.GoogleAPICallError as err:
54+
print(
55+
f"error: failed to drop search index '{index_name}' on table '{table_id}' "
56+
f"in dataset '{dataset_id}' for project '{project_id}': {err.message}",
57+
file=sys.stderr,
58+
)
59+
return
60+
61+
print(f"Resource ID: {table_ref}")
62+
print(f"Index Name: {index_name}")
63+
print(f"Job ID: {query_job.job_id}")
64+
print(f"Statement Type: {query_job.statement_type}")
65+
66+
67+
# [END bigquery_drop_search_index]
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_drop_search_index
18+
from google.api_core import exceptions
19+
import pytest
20+
21+
22+
def test_drop_search_index_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.statement_type = "SCRIPT"
30+
mock_job.result.return_value = []
31+
mock_client.query.return_value = mock_job
32+
monkeypatch.setattr(
33+
bigquery_drop_search_index.bigquery,
34+
"Client",
35+
lambda project=None: mock_client,
36+
)
37+
38+
bigquery_drop_search_index.drop_search_index(
39+
project_id="example-project",
40+
dataset_id="example_dataset",
41+
table_id="example_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_drop_search_index_error(
51+
monkeypatch: pytest.MonkeyPatch,
52+
capsys: pytest.CaptureFixture[str],
53+
) -> None:
54+
mock_client = mock.MagicMock()
55+
mock_client.query.side_effect = exceptions.NotFound("resource error")
56+
monkeypatch.setattr(
57+
bigquery_drop_search_index.bigquery,
58+
"Client",
59+
lambda project=None: mock_client,
60+
)
61+
62+
bigquery_drop_search_index.drop_search_index(
63+
project_id="example-project",
64+
dataset_id="example_dataset",
65+
table_id="example_table",
66+
)
67+
68+
captured = capsys.readouterr()
69+
assert "error:" in captured.err
70+
assert "example-project" in captured.err

0 commit comments

Comments
 (0)