Skip to content

feat(bigquery): add bigquery_search_subset_columns sample to search a… - #14737

Open
iennae wants to merge 1 commit into
mainfrom
feat/bq-search-subset-columns
Open

iennae wants to merge 1 commit into
mainfrom
feat/bq-search-subset-columns

Conversation

@iennae

@iennae iennae commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

… specific subset of table columns

Problem:
The BigQuery documentation (https://cloud.google.com/bigquery/docs/search) needs a standalone, tested Python code sample demonstrating how to search a specific subset of table columns (region tag bigquery_search_subset_columns).

Solution:

  • Add search_subset_columns(project_id: str) -> None in bigquery/search/bigquery_search_subset_columns.py (Restrict a BigQuery SEARCH query to a specific struct of table columns.)
  • Add unit tests (bigquery/search/bigquery_search_subset_columns_test.py) covering both the success path and API error handling (GoogleAPICallError)

Description

Fixes #

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended.

Compliance & Style


Post-Approval Actions

  • Please merge this PR for me once it is approved

@iennae
iennae requested review from a team and chalmerlowe as code owners November 2, 2026 23:22
@snippet-bot

snippet-bot Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label Bot added api: bigquery Issues related to the BigQuery API. samples Issues that are directly related to samples. labels Nov 2, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new BigQuery sample script, bigquery_search_subset_columns.py, which demonstrates how to search a specific subset of columns in a table using the SEARCH function, along with its corresponding unit tests and configuration files. The feedback suggests updating noxfile_config.py to ensure only the oldest and newest supported Python versions are tested, and refactoring the unit tests to use mock.MagicMock instead of fragile lambda functions for mocking the BigQuery client constructor.

Comment thread bigquery/search/noxfile_config.py Resolved
Comment on lines +32 to +36
monkeypatch.setattr(
bigquery_search_subset_columns.bigquery,
"Client",
lambda project=None: mock_client,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using a lambda with a specific signature like lambda project=None to mock a class constructor is fragile. If the initialization arguments of bigquery.Client change or if other arguments are passed, this will raise a TypeError. Using mock.MagicMock(return_value=mock_client) is the standard, robust way to mock a class constructor in Python.

Suggested change
monkeypatch.setattr(
bigquery_search_subset_columns.bigquery,
"Client",
lambda project=None: mock_client,
)
monkeypatch.setattr(
bigquery_search_subset_columns.bigquery,
"Client",
mock.MagicMock(return_value=mock_client),
)

Comment on lines +54 to +58
monkeypatch.setattr(
bigquery_search_subset_columns.bigquery,
"Client",
lambda project=None: mock_client,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using a lambda with a specific signature like lambda project=None to mock a class constructor is fragile. If the initialization arguments of bigquery.Client change or if other arguments are passed, this will raise a TypeError. Using mock.MagicMock(return_value=mock_client) is the standard, robust way to mock a class constructor in Python.

Suggested change
monkeypatch.setattr(
bigquery_search_subset_columns.bigquery,
"Client",
lambda project=None: mock_client,
)
monkeypatch.setattr(
bigquery_search_subset_columns.bigquery,
"Client",
mock.MagicMock(return_value=mock_client),
)

@iennae
iennae force-pushed the feat/bq-search-subset-columns branch 2 times, most recently from 1a7a75a to 77f1f98 Compare November 3, 2026 02:44
… specific subset of table columns

Problem:
The BigQuery documentation (https://cloud.google.com/bigquery/docs/search)
needs a standalone, tested Python code sample demonstrating how to
search a specific subset of table columns (region tag `bigquery_search_subset_columns`).

Solution:
- Add `search_subset_columns(project_id: str) -> None` in `bigquery/search/bigquery_search_subset_columns.py`
  (Restrict a BigQuery SEARCH query to a specific struct of table columns.)
- Add unit tests (`bigquery/search/bigquery_search_subset_columns_test.py`) covering both the success
  path and API error handling (`GoogleAPICallError`)
@iennae
iennae force-pushed the feat/bq-search-subset-columns branch from 77f1f98 to f65af11 Compare November 3, 2026 23:32

@XrossFox XrossFox left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: bigquery Issues related to the BigQuery API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants