Skip to content

Commit 260d2cb

Browse files
committed
fix(bigquery): restore IF NOT EXISTS in create_search_index and clean up test mocks
Problem: Following review feedback across the BigQuery sample PRs (#14714, #14715, #14717): - `bigquery_create_search_index.py` should use `CREATE SEARCH INDEX IF NOT EXISTS` and handle `exceptions.NotFound` when the parent table or dataset is missing. - `bigquery_drop_vector_index.py` `NotFound` error message should reflect that `DROP VECTOR INDEX IF EXISTS` raises `NotFound` when the parent table or dataset is missing. - Unit tests in `bigquery_create_search_index_test.py`, `bigquery_drop_vector_index_test.py`, and `bigquery_query_manifest_external_table_test.py` defined unused mock attributes (`mock_row` on DDL statements and `mock_job.statement_type` on SELECT queries). Solution: - Restore `CREATE SEARCH INDEX IF NOT EXISTS` and catch `exceptions.NotFound` in `bigquery/search/bigquery_create_search_index.py` and its unit test. - Update the `NotFound` error message in `bigquery/vector_search/bigquery_drop_vector_index.py`. - Remove unused `mock_row` and `mock_job.statement_type` setup in the three corresponding unit test files.
1 parent b908495 commit 260d2cb

5 files changed

Lines changed: 17 additions & 14 deletions

‎bigquery/biglake/bigquery_query_manifest_external_table_test.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def test_query_manifest_external_table_success(
2626
mock_client = mock.MagicMock()
2727
mock_job = mock.MagicMock()
2828
mock_job.job_id = "example-job-123"
29-
mock_job.statement_type = "SCRIPT"
3029
mock_row = mock.MagicMock()
3130
mock_job.result.return_value = [mock_row]
3231
mock_client.query.return_value = mock_job
@@ -54,7 +53,9 @@ def test_query_manifest_external_table_error(
5453
capsys: pytest.CaptureFixture[str],
5554
) -> None:
5655
mock_client = mock.MagicMock()
57-
mock_client.query.side_effect = exceptions.NotFound("resource error")
56+
mock_job = mock.MagicMock()
57+
mock_job.result.side_effect = exceptions.NotFound("resource error")
58+
mock_client.query.return_value = mock_job
5859
monkeypatch.setattr(
5960
bigquery_query_manifest_external_table.bigquery,
6061
"Client",

‎bigquery/search/bigquery_create_search_index.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,18 @@ def create_search_index(
3838
table_ref = f"{project_id}.{dataset_id}.{table_id}"
3939
# Indexing ALL COLUMNS enables full-text SEARCH queries across every STRING or JSON field.
4040
sql = f"""
41-
CREATE SEARCH INDEX `{index_name}`
41+
CREATE SEARCH INDEX IF NOT EXISTS `{index_name}`
4242
ON `{table_ref}`(ALL COLUMNS);
4343
"""
4444

4545
try:
4646
print(f"Creating search index {index_name} on table: {table_ref}")
4747
query_job = client.query(sql)
4848
query_job.result()
49-
except exceptions.AlreadyExists as err:
49+
except exceptions.NotFound as err:
5050
print(
51-
f"error: search index '{index_name}' already exists on table "
52-
f"'{table_id}' in dataset '{dataset_id}' for project '{project_id}': {err.message}",
51+
f"error: table '{table_id}' or dataset '{dataset_id}' not found "
52+
f"for project '{project_id}': {err.message}",
5353
file=sys.stderr,
5454
)
5555
return

‎bigquery/search/bigquery_create_search_index_test.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ def test_create_search_index_success(
2727
mock_job = mock.MagicMock()
2828
mock_job.job_id = "example-job-123"
2929
mock_job.statement_type = "SCRIPT"
30-
mock_row = mock.MagicMock()
31-
mock_job.result.return_value = [mock_row]
30+
mock_job.result.return_value = []
3231
mock_client.query.return_value = mock_job
3332
monkeypatch.setattr(
3433
bigquery_create_search_index.bigquery,
@@ -53,7 +52,9 @@ def test_create_search_index_error(
5352
capsys: pytest.CaptureFixture[str],
5453
) -> None:
5554
mock_client = mock.MagicMock()
56-
mock_client.query.side_effect = exceptions.AlreadyExists("resource error")
55+
mock_job = mock.MagicMock()
56+
mock_job.result.side_effect = exceptions.NotFound("resource error")
57+
mock_client.query.return_value = mock_job
5758
monkeypatch.setattr(
5859
bigquery_create_search_index.bigquery,
5960
"Client",

‎bigquery/vector_search/bigquery_drop_vector_index.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def drop_vector_index(
4545
query_job.result()
4646
except exceptions.NotFound as err:
4747
print(
48-
f"error: table '{table_id}' or vector index '{index_name}' not found in "
49-
f"dataset '{dataset_id}' for project '{project_id}': {err.message}",
48+
f"error: table '{table_id}' or dataset '{dataset_id}' not found in "
49+
f"project '{project_id}': {err.message}",
5050
file=sys.stderr,
5151
)
5252
return

‎bigquery/vector_search/bigquery_drop_vector_index_test.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ def test_drop_vector_index_success(
2727
mock_job = mock.MagicMock()
2828
mock_job.job_id = "example-job-123"
2929
mock_job.statement_type = "SCRIPT"
30-
mock_row = mock.MagicMock()
31-
mock_job.result.return_value = [mock_row]
30+
mock_job.result.return_value = []
3231
mock_client.query.return_value = mock_job
3332
monkeypatch.setattr(
3433
bigquery_drop_vector_index.bigquery,
@@ -53,7 +52,9 @@ def test_drop_vector_index_error(
5352
capsys: pytest.CaptureFixture[str],
5453
) -> None:
5554
mock_client = mock.MagicMock()
56-
mock_client.query.side_effect = exceptions.NotFound("resource error")
55+
mock_job = mock.MagicMock()
56+
mock_job.result.side_effect = exceptions.NotFound("resource error")
57+
mock_client.query.return_value = mock_job
5758
monkeypatch.setattr(
5859
bigquery_drop_vector_index.bigquery,
5960
"Client",

0 commit comments

Comments
 (0)