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
feat: add progress bar for to_arrow method #352
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
05f101e
feat: add progress bar for to_arrow method
HemangChothani 288739f
feat: add progress bar for to_dataframe
HemangChothani d94894c
Merge branch 'master' of https://github.com/googleapis/python-bigquer…
HemangChothani fa1bb49
feat: add default progress bar and unit test
HemangChothani 485bcf5
Merge branch 'master' of https://github.com/googleapis/python-bigquer…
HemangChothani f9b2ff7
feat: resolve conflict and move code to _tqdm_helper file
HemangChothani 481fa05
feat: nit
HemangChothani c1ee80e
Merge branch 'master' of https://github.com/googleapis/python-bigquer…
HemangChothani 47d393c
feat: result timout for without queryplan
HemangChothani e1d017c
Merge branch 'master' of https://github.com/googleapis/python-bigquer…
HemangChothani File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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,49 @@ | ||
# Copyright 2019 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. | ||
|
||
"""Shared helper functions for tqdm progress bar.""" | ||
|
||
import warnings | ||
|
||
try: | ||
import tqdm | ||
except ImportError: # pragma: NO COVER | ||
tqdm = None | ||
|
||
_NO_TQDM_ERROR = ( | ||
"A progress bar was requested, but there was an error loading the tqdm " | ||
"library. Please install tqdm to use the progress bar functionality." | ||
) | ||
|
||
|
||
def _get_progress_bar(progress_bar_type, description, total, unit): | ||
"""Construct a tqdm progress bar object, if tqdm is installed.""" | ||
if tqdm is None: | ||
if progress_bar_type is not None: | ||
warnings.warn(_NO_TQDM_ERROR, UserWarning, stacklevel=3) | ||
return None | ||
|
||
try: | ||
if progress_bar_type == "tqdm": | ||
return tqdm.tqdm(desc=description, total=total, unit=unit) | ||
elif progress_bar_type == "tqdm_notebook": | ||
return tqdm.tqdm_notebook(desc=description, total=total, unit=unit) | ||
elif progress_bar_type == "tqdm_gui": | ||
return tqdm.tqdm_gui(desc=description, total=total, unit=unit) | ||
except (KeyError, TypeError): | ||
# Protect ourselves from any tqdm errors. In case of | ||
# unexpected tqdm behavior, just fall back to showing | ||
# no progress bar. | ||
warnings.warn(_NO_TQDM_ERROR, UserWarning, stacklevel=3) | ||
return None |
This file contains 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 |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
import copy | ||
import re | ||
import threading | ||
import time | ||
|
||
import requests | ||
import six | ||
|
@@ -52,6 +53,7 @@ | |
from google.cloud.bigquery.table import TableListItem | ||
from google.cloud.bigquery.table import TableReference | ||
from google.cloud.bigquery.table import TimePartitioning | ||
from google.cloud.bigquery._tqdm_helpers import _get_progress_bar | ||
|
||
_DONE_STATE = "DONE" | ||
_STOPPED_REASON = "stopped" | ||
|
@@ -3335,7 +3337,53 @@ def to_arrow( | |
|
||
..versionadded:: 1.17.0 | ||
""" | ||
return self.result().to_arrow( | ||
if progress_bar_type: | ||
start_time = time.time() | ||
progress_bar = _get_progress_bar( | ||
progress_bar_type, "Query is running", 1, "query" | ||
) | ||
if self.query_plan: | ||
i = 0 | ||
while True: | ||
total = len(self.query_plan) | ||
self.reload() # Refreshes the state via a GET request. | ||
current_stage = self.query_plan[i] | ||
progress_bar.total = len(self.query_plan) | ||
progress_bar.set_description( | ||
"Query executing stage {} and status {} : {:0.2f}s".format( | ||
current_stage.name, | ||
current_stage.status, | ||
time.time() - start_time, | ||
), | ||
) | ||
|
||
try: | ||
query_result = self.result(timeout=0.5) | ||
progress_bar.update(total) | ||
progress_bar.set_description( | ||
"Query complete after {:0.2f}s".format( | ||
time.time() - start_time | ||
), | ||
) | ||
break | ||
except concurrent.futures.TimeoutError: | ||
if current_stage.status == "COMPLETE": | ||
if i < total - 1: | ||
progress_bar.update(i + 1) | ||
i += 1 | ||
continue | ||
|
||
else: | ||
query_result = self.result() | ||
progress_bar.set_description( | ||
"Query complete after {:0.2f}s".format(time.time() - start_time), | ||
) | ||
progress_bar.update(1) | ||
progress_bar.close() | ||
else: | ||
query_result = self.result() | ||
|
||
return query_result.to_arrow( | ||
progress_bar_type=progress_bar_type, | ||
bqstorage_client=bqstorage_client, | ||
create_bqstorage_client=create_bqstorage_client, | ||
|
@@ -3404,7 +3452,51 @@ def to_dataframe( | |
Raises: | ||
ValueError: If the `pandas` library cannot be imported. | ||
""" | ||
return self.result().to_dataframe( | ||
if progress_bar_type: | ||
start_time = time.time() | ||
progress_bar = _get_progress_bar( | ||
progress_bar_type, "Query is running", 1, "query" | ||
) | ||
if self.query_plan: | ||
i = 0 | ||
while True: | ||
total = len(self.query_plan) | ||
self.reload() # Refreshes the state via a GET request. | ||
|
||
current_stage = self.query_plan[i] | ||
progress_bar.set_description( | ||
"Query executing stage {} and status {} : {:0.2f}s".format( | ||
current_stage.name, | ||
current_stage.status, | ||
time.time() - start_time, | ||
) | ||
) | ||
|
||
try: | ||
query_result = self.result(timeout=0.5) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should make constants for this ( |
||
progress_bar.update(total) | ||
progress_bar.set_description( | ||
"Query complete after {:0.2f}s".format( | ||
time.time() - start_time | ||
) | ||
) | ||
break | ||
except concurrent.futures.TimeoutError: | ||
if current_stage.status == "COMPLETE": | ||
if i < total - 1: | ||
progress_bar.update(i + 1) | ||
i += 1 | ||
continue | ||
else: | ||
query_result = self.result() | ||
progress_bar.set_description( | ||
"Query complete after {:0.2f}s".format(time.time() - start_time), | ||
) | ||
progress_bar.update(1) | ||
progress_bar.close() | ||
else: | ||
query_result = self.result() | ||
return query_result.to_dataframe( | ||
bqstorage_client=bqstorage_client, | ||
dtypes=dtypes, | ||
progress_bar_type=progress_bar_type, | ||
|
This file contains 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
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.
We want to use these helpers from other modules, let's remove the (redundant because of private module) leading
_
.