Skip to content

fix: let BigQuery infer dataset location for view creation#5477

Draft
caohy1988 wants to merge 2 commits intogoogle:mainfrom
caohy1988:fix/bqaa-auto-detect-dataset-location
Draft

fix: let BigQuery infer dataset location for view creation#5477
caohy1988 wants to merge 2 commits intogoogle:mainfrom
caohy1988:fix/bqaa-auto-detect-dataset-location

Conversation

@caohy1988
Copy link
Copy Markdown

@caohy1988 caohy1988 commented Apr 24, 2026

Summary

Fixes #5476

The plugin passed location=self.location (default "US") to bigquery.Client(). This only affects query job routing — not table CRUD or the Storage Write API. When a dataset lives in a non-US region, view creation DDL queries silently fail with a location mismatch.

Fix

Remove location=self.location from the bigquery.Client() constructor. When the client has no default location and client.query(sql) is called without an explicit location parameter, the BQ API infers the job location from the dataset referenced in the DDL statement.

Traced through the BQ Python client:

  1. client.query(sql)location param defaults to None, falls back to self.location which is also None
  2. _to_query_request() — when location is None, the "location" key is not included in the API request
  3. BQ API — infers location from the dataset referenced in CREATE OR REPLACE VIEW

One line of production code changed. Everything else is test updates.

What changes

Operation Before After
Table CRUD Works Same
Storage Write API Works Same
View creation (non-US dataset) Silent failure Works — BQ infers location

Test plan

  • 211 tests pass, 0 regressions
  • test_client_created_without_location — verifies bigquery.Client() is called without location kwarg
  • test_view_query_omits_location — verifies client.query() is called without explicit location
  • test_view_error_still_logged — view creation errors don't crash initialization

🤖 Generated with Claude Code

The plugin previously passed a configured location (default "US") to
bigquery.Client(location=...).  This only affects query job routing,
not table CRUD or the Storage Write API.  When the dataset lives in a
non-US region, view creation DDL queries silently fail with a location
mismatch — data writes succeed but analytics views are missing.

The fix auto-detects the dataset's actual location via get_dataset()
during initialization and passes it explicitly to client.query() for
view creation.  Falls back to the configured location when dataset
metadata cannot be resolved.

Fixes google#5476

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 24, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@adk-bot adk-bot added the tools [Component] This issue is related to tools label Apr 24, 2026
@adk-bot
Copy link
Copy Markdown
Collaborator

adk-bot commented Apr 24, 2026

Response from ADK Triaging Agent

Hello @caohy1988, thank you for creating this PR!

Before we can review your contribution, you'll need to sign the Contributor License Agreement (CLA). You can do so at https://cla.developers.google.com/.

Once the CLA is signed, we will be able to proceed with the review. Thanks!

)
try:
self.client.query(sql).result()
self.client.query(sql, location=self._resolved_location).result()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this needed?

Does it work if we always omit location, and bq client can resolve the location?

@caohy1988
Copy link
Copy Markdown
Author

Is this needed? Does it work if we always omit location, and bq client can resolve the location?

You're right — the explicit location= pass is unnecessary. I traced the full call chain in the BQ Python client:

  1. client.query(sql) — when location param is omitted, falls back to self.location (client.py:89-90)
  2. Since we now create bigquery.Client(project=..., credentials=...) without location, self.location is None
  3. _to_query_request() — when location is None, the "location" key is not included in the API request body (_job_helpers.py:35-36)
  4. The BQ API receives a request with no location field and infers location from the dataset referenced in the CREATE OR REPLACE VIEW DDL

So the simplest fix is just removing location=self.location from the bigquery.Client() constructor — which this PR already does. The _resolved_location / get_dataset() machinery is unnecessary overhead.

I'll simplify: remove _resolved_location, the get_dataset() call, and the explicit location= in client.query(). The only production change becomes removing location=self.location from the bigquery.Client() constructor.

Per review feedback: when bigquery.Client is created without a location
parameter, client.query() sends no location field in the API request.
BigQuery infers the job location from the dataset referenced in the DDL
statement.  This is simpler than auto-detecting via get_dataset().

The only production change from origin/main is removing
location=self.location from the bigquery.Client() constructor call.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@caohy1988 caohy1988 changed the title fix: auto-detect dataset location for BigQuery analytics view creation fix: let BigQuery infer dataset location for view creation Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tools [Component] This issue is related to tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: auto-detect dataset location in BigQuery analytics plugin

3 participants