forked from googleapis/python-bigquery-pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.py
276 lines (234 loc) · 8.58 KB
/
load.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# Copyright (c) 2017 pandas-gbq Authors All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
"""Helper methods for loading data into BigQuery"""
import decimal
import io
from typing import Any, Callable, Dict, List, Optional
import db_dtypes
from google.cloud import bigquery
import pandas
import pyarrow.lib
from pandas_gbq import exceptions
import pandas_gbq.schema
def encode_chunk(dataframe):
"""Return a file-like object of CSV-encoded rows.
Args:
dataframe (pandas.DataFrame): A chunk of a dataframe to encode
"""
csv_buffer = io.StringIO()
dataframe.to_csv(
csv_buffer,
index=False,
header=False,
encoding="utf-8",
float_format="%.17g",
date_format="%Y-%m-%d %H:%M:%S.%f",
)
# Convert to a BytesIO buffer so that unicode text is properly handled.
# See: https://github.com/pydata/pandas-gbq/issues/106
body = csv_buffer.getvalue()
body = body.encode("utf-8")
return io.BytesIO(body)
def split_dataframe(dataframe, chunksize=None):
dataframe = dataframe.reset_index(drop=True)
if chunksize is None:
yield 0, dataframe
return
remaining_rows = len(dataframe)
total_rows = remaining_rows
start_index = 0
while start_index < total_rows:
end_index = start_index + chunksize
chunk = dataframe[start_index:end_index]
start_index += chunksize
remaining_rows = max(0, remaining_rows - chunksize)
yield remaining_rows, chunk
def cast_dataframe_for_parquet(
dataframe: pandas.DataFrame,
schema: Optional[Dict[str, Any]],
) -> pandas.DataFrame:
"""Cast columns to needed dtype when writing parquet files.
See: https://github.com/googleapis/python-bigquery-pandas/issues/421
"""
columns = schema.get("fields", [])
# Protect against an explicit None in the dictionary.
columns = columns if columns is not None else []
for column in columns:
# Schema can be a superset of the columns in the dataframe, so ignore
# columns that aren't present.
column_name = column.get("name")
if column_name not in dataframe.columns:
continue
# Skip array columns for now. Potentially casting the elements of the
# array would be possible, but not worth the effort until there is
# demand for it.
if column.get("mode", "NULLABLE").upper() == "REPEATED":
continue
column_type = column.get("type", "").upper()
if (
column_type == "DATE"
# Use extension dtype first so that it uses the correct equality operator.
and db_dtypes.DateDtype() != dataframe[column_name].dtype
):
cast_column = dataframe[column_name].astype(
dtype=db_dtypes.DateDtype(),
# Return the original column if there was an error converting
# to the dtype, such as is there is a date outside the
# supported range.
# https://github.com/googleapis/python-bigquery-pandas/issues/441
errors="ignore",
)
elif column_type in {"NUMERIC", "DECIMAL", "BIGNUMERIC", "BIGDECIMAL"}:
# decimal.Decimal does not support `None` or `pandas.NA` input, add
# support here.
# https://github.com/googleapis/python-bigquery-pandas/issues/719
def convert(x):
if pandas.isna(x): # true for `None` and `pandas.NA`
return decimal.Decimal("NaN")
else:
return decimal.Decimal(x)
cast_column = dataframe[column_name].map(convert)
else:
cast_column = None
if cast_column is not None:
dataframe = dataframe.assign(**{column_name: cast_column})
return dataframe
def load_parquet(
client: bigquery.Client,
dataframe: pandas.DataFrame,
destination_table_ref: bigquery.TableReference,
write_disposition: str,
location: Optional[str],
schema: Optional[Dict[str, Any]],
billing_project: Optional[str] = None,
):
job_config = bigquery.LoadJobConfig()
job_config.write_disposition = write_disposition
job_config.source_format = "PARQUET"
if schema is not None:
schema = pandas_gbq.schema.remove_policy_tags(schema)
job_config.schema = pandas_gbq.schema.to_google_cloud_bigquery(schema)
dataframe = cast_dataframe_for_parquet(dataframe, schema)
try:
client.load_table_from_dataframe(
dataframe,
destination_table_ref,
job_config=job_config,
location=location,
project=billing_project,
).result()
except pyarrow.lib.ArrowInvalid as exc:
raise exceptions.ConversionError(
"Could not convert DataFrame to Parquet."
) from exc
def load_csv(
dataframe: pandas.DataFrame,
write_disposition: str,
chunksize: Optional[int],
bq_schema: Optional[List[bigquery.SchemaField]],
load_chunk: Callable,
):
job_config = bigquery.LoadJobConfig()
job_config.write_disposition = write_disposition
job_config.source_format = "CSV"
job_config.allow_quoted_newlines = True
if bq_schema is not None:
job_config.schema = bq_schema
# TODO: Remove chunking feature for load jobs. Deprecated in 0.16.0.
chunks = split_dataframe(dataframe, chunksize=chunksize)
for remaining_rows, chunk in chunks:
yield remaining_rows
load_chunk(chunk, job_config)
def load_csv_from_dataframe(
client: bigquery.Client,
dataframe: pandas.DataFrame,
destination_table_ref: bigquery.TableReference,
write_disposition: str,
location: Optional[str],
chunksize: Optional[int],
schema: Optional[Dict[str, Any]],
billing_project: Optional[str] = None,
):
bq_schema = None
if schema is not None:
schema = pandas_gbq.schema.remove_policy_tags(schema)
bq_schema = pandas_gbq.schema.to_google_cloud_bigquery(schema)
def load_chunk(chunk, job_config):
client.load_table_from_dataframe(
chunk,
destination_table_ref,
job_config=job_config,
location=location,
project=billing_project,
).result()
return load_csv(dataframe, write_disposition, chunksize, bq_schema, load_chunk)
def load_csv_from_file(
client: bigquery.Client,
dataframe: pandas.DataFrame,
destination_table_ref: bigquery.TableReference,
write_disposition: str,
location: Optional[str],
chunksize: Optional[int],
schema: Optional[Dict[str, Any]],
billing_project: Optional[str] = None,
):
"""Manually encode a DataFrame to CSV and use the buffer in a load job.
This method is needed for writing with google-cloud-bigquery versions that
don't implment load_table_from_dataframe with the CSV serialization format.
"""
if schema is None:
schema = pandas_gbq.schema.generate_bq_schema(dataframe)
schema = pandas_gbq.schema.remove_policy_tags(schema)
bq_schema = pandas_gbq.schema.to_google_cloud_bigquery(schema)
def load_chunk(chunk, job_config):
try:
chunk_buffer = encode_chunk(chunk)
client.load_table_from_file(
chunk_buffer,
destination_table_ref,
job_config=job_config,
location=location,
project=billing_project,
).result()
finally:
chunk_buffer.close()
return load_csv(dataframe, write_disposition, chunksize, bq_schema, load_chunk)
def load_chunks(
client,
dataframe,
destination_table_ref,
chunksize=None,
schema=None,
location=None,
api_method="load_parquet",
write_disposition="WRITE_EMPTY",
billing_project: Optional[str] = None,
):
if api_method == "load_parquet":
load_parquet(
client,
dataframe,
destination_table_ref,
write_disposition,
location,
schema,
billing_project=billing_project,
)
# TODO: yield progress depending on result() with timeout
return [0]
elif api_method == "load_csv":
return load_csv_from_dataframe(
client,
dataframe,
destination_table_ref,
write_disposition,
location,
chunksize,
schema,
billing_project=billing_project,
)
else:
raise ValueError(
f"Got unexpected api_method: {api_method!r}, expected one of 'load_parquet', 'load_csv'."
)