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

Use temporary file in load_table_from_dataframe #7545

Merged
merged 3 commits into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions bigquery/docs/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
import pytest
import six

try:
import fastparquet
except (ImportError, AttributeError):
fastparquet = None
try:
import pandas
except (ImportError, AttributeError):
Expand Down Expand Up @@ -3108,8 +3112,15 @@ def test_list_rows_as_dataframe(client):


@pytest.mark.skipif(pandas is None, reason="Requires `pandas`")
@pytest.mark.skipif(pyarrow is None, reason="Requires `pyarrow`")
def test_load_table_from_dataframe(client, to_delete):
@pytest.mark.parametrize("parquet_engine", ["pyarrow", "fastparquet"])
def test_load_table_from_dataframe(client, to_delete, parquet_engine):
if parquet_engine == "pyarrow" and pyarrow is None:
pytest.skip("Requires `pyarrow`")
if parquet_engine == "fastparquet" and fastparquet is None:
pytest.skip("Requires `fastparquet`")

pandas.set_option("io.parquet.engine", parquet_engine)

dataset_id = "load_table_from_dataframe_{}".format(_millis())
dataset = bigquery.Dataset(client.dataset(dataset_id))
client.create_dataset(dataset)
Expand Down
39 changes: 25 additions & 14 deletions bigquery/google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import functools
import gzip
import os
import tempfile
import uuid

import six
Expand Down Expand Up @@ -1124,10 +1125,10 @@ def load_table_from_dataframe(
Raises:
ImportError:
If a usable parquet engine cannot be found. This method
requires :mod:`pyarrow` to be installed.
requires :mod:`pyarrow` or :mod:`fastparquet` to be
installed.
"""
buffer = six.BytesIO()
dataframe.to_parquet(buffer)
job_id = _make_job_id(job_id, job_id_prefix)

if job_config is None:
job_config = job.LoadJobConfig()
Expand All @@ -1136,17 +1137,27 @@ def load_table_from_dataframe(
if location is None:
location = self.location

return self.load_table_from_file(
buffer,
destination,
num_retries=num_retries,
rewind=True,
job_id=job_id,
job_id_prefix=job_id_prefix,
location=location,
project=project,
job_config=job_config,
)
tmpfd, tmppath = tempfile.mkstemp(suffix="_job_{}.parquet".format(job_id[:8]))
os.close(tmpfd)

try:
dataframe.to_parquet(tmppath)

with open(tmppath, "rb") as parquet_file:
return self.load_table_from_file(
parquet_file,
destination,
num_retries=num_retries,
rewind=True,
job_id=job_id,
job_id_prefix=job_id_prefix,
location=location,
project=project,
job_config=job_config,
)

finally:
os.remove(tmppath)

def _do_resumable_upload(self, stream, metadata, num_retries):
"""Perform a resumable upload.
Expand Down
2 changes: 1 addition & 1 deletion bigquery/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def snippets(session):
session.install('-e', local_dep)
session.install('-e', os.path.join('..', 'storage'))
session.install('-e', os.path.join('..', 'test_utils'))
session.install('-e', '.[pandas, pyarrow]')
session.install('-e', '.[pandas, pyarrow, fastparquet]')

# Run py.test against the snippets tests.
session.run(
Expand Down
1 change: 1 addition & 0 deletions bigquery/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
# Exclude PyArrow dependency from Windows Python 2.7.
'pyarrow: platform_system != "Windows" or python_version >= "3.4"':
'pyarrow>=0.4.1',
'fastparquet': ['fastparquet', 'python-snappy'],
}


Expand Down
14 changes: 5 additions & 9 deletions bigquery/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4658,17 +4658,15 @@ def test_load_table_from_dataframe(self):
self.TABLE_REF,
num_retries=_DEFAULT_NUM_RETRIES,
rewind=True,
job_id=None,
job_id=mock.ANY,
job_id_prefix=None,
location=None,
project=None,
job_config=mock.ANY,
)

sent_file = load_table_from_file.mock_calls[0][1][1]
sent_bytes = sent_file.getvalue()
assert isinstance(sent_bytes, bytes)
assert len(sent_bytes) > 0
assert sent_file.closed

sent_config = load_table_from_file.mock_calls[0][2]["job_config"]
assert sent_config.source_format == job.SourceFormat.PARQUET
Expand All @@ -4695,17 +4693,15 @@ def test_load_table_from_dataframe_w_client_location(self):
self.TABLE_REF,
num_retries=_DEFAULT_NUM_RETRIES,
rewind=True,
job_id=None,
job_id=mock.ANY,
job_id_prefix=None,
location=self.LOCATION,
project=None,
job_config=mock.ANY,
)

sent_file = load_table_from_file.mock_calls[0][1][1]
sent_bytes = sent_file.getvalue()
assert isinstance(sent_bytes, bytes)
assert len(sent_bytes) > 0
assert sent_file.closed

sent_config = load_table_from_file.mock_calls[0][2]["job_config"]
assert sent_config.source_format == job.SourceFormat.PARQUET
Expand Down Expand Up @@ -4735,7 +4731,7 @@ def test_load_table_from_dataframe_w_custom_job_config(self):
self.TABLE_REF,
num_retries=_DEFAULT_NUM_RETRIES,
rewind=True,
job_id=None,
job_id=mock.ANY,
job_id_prefix=None,
location=self.LOCATION,
project=None,
Expand Down