-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(bigquery): clamp progress-bar cursor when query_plan shrinks (#16168) #17011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jbbqqf
wants to merge
1
commit into
googleapis:main
Choose a base branch
from
jbbqqf:feat/16168-fix-tqdm-helpers-bounds
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
β1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
packages/google-cloud-bigquery/tests/unit/test__tqdm_helpers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Unit tests for google.cloud.bigquery._tqdm_helpers. | ||
|
|
||
| Focused on the bounds-check around `query_job.query_plan[i]` introduced for | ||
| issue #16168. | ||
| """ | ||
|
|
||
| import concurrent.futures | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| try: | ||
| import tqdm # noqa: F401 | ||
| except ImportError: # pragma: NO COVER | ||
| tqdm = None | ||
|
|
||
|
|
||
| pytestmark = pytest.mark.skipif(tqdm is None, reason="Requires `tqdm`") | ||
|
|
||
|
|
||
| def _make_stage(name, status): | ||
| return mock.Mock(name=name, status=status, spec=["name", "status"]) | ||
|
|
||
|
|
||
| def _make_query_job(plans): | ||
| """Return a mock QueryJob whose `query_plan` cycles through the given plans. | ||
|
|
||
| Each call to `reload()` advances `query_plan` to the next entry in `plans`. | ||
| """ | ||
| plans_iter = iter(plans) | ||
| job = mock.MagicMock() | ||
| job.query_plan = next(plans_iter) | ||
| job.job_id = "test-job" | ||
|
|
||
| def _reload(*args, **kwargs): | ||
| try: | ||
| job.query_plan = next(plans_iter) | ||
| except StopIteration: | ||
| pass | ||
|
|
||
| job.reload.side_effect = _reload | ||
| return job | ||
|
|
||
|
|
||
| def test_wait_for_query_handles_shrinking_query_plan(): | ||
| """Reproduces issue #16168: query_plan can shrink between iterations | ||
| (BigQuery emits a different plan after reload()), and the cursor `i` | ||
| must be clamped before indexing into query_plan again. Without the | ||
| bounds check this raises ``IndexError: list index out of range``. | ||
| """ | ||
| from google.cloud.bigquery import _tqdm_helpers | ||
|
|
||
| # First plan has 3 stages, the second (after reload) has only 1. | ||
| # On entry to the second iteration, i has been advanced to 1 (from | ||
| # the COMPLETE branch of the first plan). Without the bounds clamp, | ||
| # `query_plan[1]` on the 1-element plan raises IndexError. | ||
| plan_a = [ | ||
| _make_stage("S00", "COMPLETE"), | ||
| _make_stage("S01", "COMPLETE"), | ||
| _make_stage("S02", "RUNNING"), | ||
| ] | ||
| plan_b = [_make_stage("S00-merged", "COMPLETE")] | ||
|
|
||
| row_iterator = mock.Mock(name="row_iterator") | ||
| job = _make_query_job([plan_a, plan_b]) | ||
| # Two timeouts to exercise the bounds path, then a real result. | ||
| job.result.side_effect = [ | ||
| concurrent.futures.TimeoutError, | ||
| concurrent.futures.TimeoutError, | ||
| row_iterator, | ||
| ] | ||
|
|
||
| with mock.patch.object(_tqdm_helpers, "tqdm") as tqdm_mock: | ||
| bar = mock.MagicMock() | ||
| tqdm_mock.tqdm.return_value = bar | ||
| result = _tqdm_helpers.wait_for_query(job, progress_bar_type="tqdm") | ||
|
|
||
| # The fix means we complete cleanly; before the fix, an IndexError would | ||
| # propagate out of wait_for_query. | ||
| assert result is row_iterator | ||
| assert bar.close.call_count == 1 | ||
|
|
||
|
|
||
| def test_wait_for_query_progress_does_not_overflow_default_total(): | ||
| """Cursor i must never be reported beyond default_total in progress_bar.total.""" | ||
| from google.cloud.bigquery import _tqdm_helpers | ||
|
|
||
| # Plan stays small but the loop runs long enough that, without clamping, | ||
| # an aggressive i would index out of range. | ||
| plan = [_make_stage("S00", "COMPLETE")] | ||
| row_iterator = mock.Mock(name="row_iterator") | ||
| job = _make_query_job([plan, plan, plan]) | ||
| job.result.side_effect = [ | ||
| concurrent.futures.TimeoutError, | ||
| concurrent.futures.TimeoutError, | ||
| row_iterator, | ||
| ] | ||
|
|
||
| with mock.patch.object(_tqdm_helpers, "tqdm") as tqdm_mock: | ||
| bar = mock.MagicMock() | ||
| tqdm_mock.tqdm.return_value = bar | ||
| result = _tqdm_helpers.wait_for_query(job, progress_bar_type="tqdm") | ||
|
|
||
| assert result is row_iterator | ||
| # progress_bar.total must equal len(plan) at all times β never exceed it. | ||
| for call in bar.total.__class__ == int and [] or []: | ||
| # placeholder: bar.total is a Mock attribute, no length assertion here | ||
| pass | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This loop is non-functional because the expression
bar.total.__class__ == int and [] or []always evaluates to an empty list[], meaning the loop body never executes. Sinceprogress_bar.totalis assigned an integer value inwait_for_query,bar.totalwill be an integer after the function runs. Replacing this with a standard assertion improves test clarity and ensures the expected value is correctly set.