Skip to content

Commit 01efd05

Browse files
committed
feat(bigquery): add bigquery_create_vector_index sample to create an IVF vector index on embedding column
Problem: The BigQuery documentation (https://cloud.google.com/bigquery/docs/vector-index) needs a standalone, tested Python code sample demonstrating how to create an IVF vector index on embedding column (region tag `bigquery_create_vector_index`). Solution: - Add `create_vector_index(project_id: str, dataset_id: str, table_id: str) -> None` in `bigquery/vector_search/bigquery_create_vector_index.py` (Create an inverted file (IVF) vector index on an array embedding column in BigQuery using CREATE OR REPLACE VECTOR INDEX.) - Add unit tests (`bigquery/vector_search/bigquery_create_vector_index_test.py`) covering both the success path and API error handling (`GoogleAPICallError`)
1 parent 186be98 commit 01efd05

5 files changed

Lines changed: 169 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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_create_vector_index]
16+
import sys
17+
18+
from google.api_core import exceptions
19+
from google.cloud import bigquery
20+
21+
22+
def create_vector_index(
23+
project_id: str,
24+
dataset_id: str,
25+
table_id: str,
26+
) -> None:
27+
"""Creates an IVF vector index on an embedding column in 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 target table
32+
(for example, 'example_dataset').
33+
table_id: BigQuery table ID containing the embedding column
34+
(for example, 'example_embeddings').
35+
"""
36+
client = bigquery.Client(project=project_id)
37+
# Use a fixed vector index name on the caller-supplied embeddings table.
38+
index_name = "example_vector_index"
39+
table_ref = f"{project_id}.{dataset_id}.{table_id}"
40+
# IVF indexes partition vectors into inverted lists to accelerate approximate nearest-neighbor search.
41+
sql = f"""
42+
CREATE OR REPLACE VECTOR INDEX `{index_name}`
43+
ON `{table_ref}`(embedding)
44+
OPTIONS(index_type = 'IVF', distance_type = 'COSINE');
45+
"""
46+
47+
try:
48+
print(f"Creating vector index {index_name} on table: {table_ref}")
49+
query_job = client.query(sql)
50+
query_job.result()
51+
except exceptions.NotFound as err:
52+
print(
53+
f"error: table '{table_id}' or dataset '{dataset_id}' not found "
54+
f"for project '{project_id}': {err.message}",
55+
file=sys.stderr,
56+
)
57+
return
58+
except exceptions.GoogleAPICallError as err:
59+
print(
60+
f"error: failed to create vector index '{index_name}' on table "
61+
f"'{table_id}' in dataset '{dataset_id}' for project '{project_id}': {err.message}",
62+
file=sys.stderr,
63+
)
64+
return
65+
66+
print(f"Resource ID: {table_ref}")
67+
print(f"Index Name: {index_name}")
68+
print(f"Job ID: {query_job.job_id}")
69+
print(f"Statement Type: {query_job.statement_type}")
70+
71+
72+
# [END bigquery_create_vector_index]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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_create_vector_index
18+
from google.api_core import exceptions
19+
import pytest
20+
21+
22+
def test_create_vector_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_create_vector_index.bigquery,
34+
"Client",
35+
lambda project=None: mock_client,
36+
)
37+
38+
bigquery_create_vector_index.create_vector_index(
39+
project_id="example-project",
40+
dataset_id="example_dataset",
41+
table_id="example_embeddings",
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_create_vector_index_error(
51+
monkeypatch: pytest.MonkeyPatch,
52+
capsys: pytest.CaptureFixture[str],
53+
) -> None:
54+
mock_client = mock.MagicMock()
55+
mock_job = mock.MagicMock()
56+
mock_job.result.side_effect = exceptions.NotFound("resource error")
57+
mock_client.query.return_value = mock_job
58+
monkeypatch.setattr(
59+
bigquery_create_vector_index.bigquery,
60+
"Client",
61+
lambda project=None: mock_client,
62+
)
63+
64+
bigquery_create_vector_index.create_vector_index(
65+
project_id="example-project",
66+
dataset_id="example_dataset",
67+
table_id="example_embeddings",
68+
)
69+
70+
captured = capsys.readouterr()
71+
assert "error:" in captured.err
72+
assert "example-project" in captured.err
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"
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)