Skip to content
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 10 commits into from Nov 16, 2020
106 changes: 106 additions & 0 deletions google/cloud/bigquery/_tqdm_helpers.py
@@ -0,0 +1,106 @@
# 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 concurrent.futures
import time
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."
)

_PROGRESS_BAR_UPDATE_INTERVAL = 0.5


def _get_progress_bar(progress_bar_type, description, total, unit):
Copy link
Contributor

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 _.

Suggested change
def _get_progress_bar(progress_bar_type, description, total, unit):
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


def _query_job_result_helper(query_job, progress_bar_type=None):
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's use active verbs in this helper name.

Suggested change
def _query_job_result_helper(query_job, progress_bar_type=None):
def wait_for_query(query_job, progress_bar_type=None):

"""Return query result and display a progress bar while the query running, if tqdm is installed."""
if progress_bar_type:
Copy link
Contributor

Choose a reason for hiding this comment

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

We can reduce the level of indentation if we exit early. Also, I think we'll want to pass through some keyword arguments to result(), correct?

Suggested change
if progress_bar_type:
if progress_bar_type is None:
return query_job.result()

Aside (not relevant for this PR): we'll eventually want to pass additional arguments to result() whenever we implement #296

start_time = time.time()
progress_bar = _get_progress_bar(
progress_bar_type, "Query is running", 1, "query"
)
if query_job.query_plan:
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be one level deeper. I'd like to see the while True loop, even when query_plan is not initially populated.

i = 0
while True:
total = len(query_job.query_plan)
query_job.reload() # Refreshes the state via a GET request.
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't we only want to call reload after result times out?

Also, why would we only call reload inside this if statement? The job might not have a query_plan until after reload is called in many cases.

current_stage = query_job.query_plan[i]
progress_bar.total = len(query_job.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 = query_job.result(
timeout=_PROGRESS_BAR_UPDATE_INTERVAL
)
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 = query_job.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 = query_job.result()

return query_result
7 changes: 5 additions & 2 deletions google/cloud/bigquery/job/query.py
Expand Up @@ -40,6 +40,7 @@
from google.cloud.bigquery.table import _table_arg_to_table_ref
from google.cloud.bigquery.table import TableReference
from google.cloud.bigquery.table import TimePartitioning
from google.cloud.bigquery._tqdm_helpers import _query_job_result_helper

from google.cloud.bigquery.job.base import _AsyncJob
from google.cloud.bigquery.job.base import _DONE_STATE
Expand Down Expand Up @@ -1236,7 +1237,8 @@ def to_arrow(

..versionadded:: 1.17.0
"""
return self.result().to_arrow(
query_result = _query_job_result_helper(self, progress_bar_type)
return query_result.to_arrow(
progress_bar_type=progress_bar_type,
bqstorage_client=bqstorage_client,
create_bqstorage_client=create_bqstorage_client,
Expand Down Expand Up @@ -1305,7 +1307,8 @@ def to_dataframe(
Raises:
ValueError: If the `pandas` library cannot be imported.
"""
return self.result().to_dataframe(
query_result = _query_job_result_helper(self, progress_bar_type)
return query_result.to_dataframe(
bqstorage_client=bqstorage_client,
dtypes=dtypes,
progress_bar_type=progress_bar_type,
Expand Down
41 changes: 5 additions & 36 deletions google/cloud/bigquery/table.py
Expand Up @@ -36,11 +36,6 @@
except ImportError: # pragma: NO COVER
pyarrow = None

try:
import tqdm
except ImportError: # pragma: NO COVER
tqdm = None

import google.api_core.exceptions
from google.api_core.page_iterator import HTTPIterator

Expand All @@ -50,6 +45,7 @@
from google.cloud.bigquery.schema import _build_schema_resource
from google.cloud.bigquery.schema import _parse_schema_resource
from google.cloud.bigquery.schema import _to_schema_fields
from google.cloud.bigquery._tqdm_helpers import _get_progress_bar
from google.cloud.bigquery.external_config import ExternalConfig
from google.cloud.bigquery.encryption_configuration import EncryptionConfiguration

Expand All @@ -68,10 +64,7 @@
"The pyarrow library is not installed, please install "
"pyarrow to use the to_arrow() function."
)
_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."
)

_TABLE_HAS_NO_SCHEMA = 'Table has no schema: call "client.get_table()"'


Expand Down Expand Up @@ -1374,32 +1367,6 @@ def total_rows(self):
"""int: The total number of rows in the table."""
return self._total_rows

def _get_progress_bar(self, progress_bar_type):
"""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

description = "Downloading"
unit = "rows"

try:
if progress_bar_type == "tqdm":
return tqdm.tqdm(desc=description, total=self.total_rows, unit=unit)
elif progress_bar_type == "tqdm_notebook":
return tqdm.tqdm_notebook(
desc=description, total=self.total_rows, unit=unit
)
elif progress_bar_type == "tqdm_gui":
return tqdm.tqdm_gui(desc=description, total=self.total_rows, 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

def _to_page_iterable(
self, bqstorage_download, tabledata_list_download, bqstorage_client=None
):
Expand Down Expand Up @@ -1511,7 +1478,9 @@ def to_arrow(
owns_bqstorage_client = bqstorage_client is not None

try:
progress_bar = self._get_progress_bar(progress_bar_type)
progress_bar = _get_progress_bar(
progress_bar_type, "Downloading", self.total_rows, "rows"
)

record_batches = []
for record_batch in self._to_arrow_iterable(
Expand Down