-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathresult_set.py
394 lines (344 loc) · 12.9 KB
/
result_set.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# -*- coding: utf-8 -*-
import logging
from collections import abc
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
Iterable,
Iterator,
List,
Optional,
Tuple,
Type,
Union,
)
from pyathena import OperationalError
from pyathena.converter import Converter
from pyathena.error import ProgrammingError
from pyathena.model import AthenaQueryExecution
from pyathena.result_set import AthenaResultSet
from pyathena.util import RetryConfig, parse_output_location
if TYPE_CHECKING:
from pandas import DataFrame
from pandas.io.parsers import TextFileReader
from pyathena.connection import Connection
_logger = logging.getLogger(__name__) # type: ignore
def _no_trunc_date(df: "DataFrame") -> "DataFrame":
return df
class DataFrameIterator(abc.Iterator): # type: ignore
def __init__(
self,
reader: Union["TextFileReader", "DataFrame"],
trunc_date: Callable[["DataFrame"], "DataFrame"],
) -> None:
from pandas import DataFrame
if isinstance(reader, DataFrame):
self._reader = iter([reader])
else:
self._reader = reader
self._trunc_date = trunc_date
def __next__(self):
try:
df = next(self._reader)
return self._trunc_date(df)
except StopIteration:
self.close()
raise
def __iter__(self):
return self
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
def close(self) -> None:
from pandas.io.parsers import TextFileReader
if isinstance(self._reader, TextFileReader):
self._reader.close()
def iterrows(self) -> Iterator[Any]:
for df in self:
for row in enumerate(df.to_dict("records")):
yield row
def get_chunk(self, size=None):
from pandas.io.parsers import TextFileReader
if isinstance(self._reader, TextFileReader):
return self._reader.get_chunk(size)
else:
return next(self._reader)
class AthenaPandasResultSet(AthenaResultSet):
_parse_dates: List[str] = [
"date",
"time",
"time with time zone",
"timestamp",
"timestamp with time zone",
]
def __init__(
self,
connection: "Connection",
converter: Converter,
query_execution: AthenaQueryExecution,
arraysize: int,
retry_config: RetryConfig,
keep_default_na: bool = False,
na_values: Optional[Iterable[str]] = ("",),
quoting: int = 1,
unload: bool = False,
unload_location: Optional[str] = None,
engine: str = "auto",
chunksize: Optional[int] = None,
**kwargs,
) -> None:
super(AthenaPandasResultSet, self).__init__(
connection=connection,
converter=converter,
query_execution=query_execution,
arraysize=1, # Fetch one row to retrieve metadata
retry_config=retry_config,
)
self._rows.clear() # Clear pre_fetch data
self._arraysize = arraysize
self._keep_default_na = keep_default_na
self._na_values = na_values
self._quoting = quoting
self._unload = unload
self._unload_location = unload_location
self._engine = engine
self._chunksize = chunksize
self._data_manifest: List[str] = []
self._kwargs = kwargs
self._fs = self.__s3_file_system()
if self.state == AthenaQueryExecution.STATE_SUCCEEDED and self.output_location:
df = self._as_pandas()
if self.is_unload:
trunc_date = _no_trunc_date
else:
trunc_date = self._trunc_date
self._df_iter = DataFrameIterator(df, trunc_date)
else:
import pandas as pd
self._df_iter = DataFrameIterator(pd.DataFrame(), _no_trunc_date)
self._iterrows = self._df_iter.iterrows()
def _get_engine(self) -> "str":
if self._engine == "auto":
import importlib
error_msgs = ""
for engine in ["pyarrow", "fastparquet"]:
try:
module = importlib.import_module(engine)
return module.__name__
except ImportError as e:
error_msgs += f"\n - {str(e)}"
raise ImportError(
"Unable to find a usable engine; tried using: 'pyarrow', 'fastparquet'.\n"
"A suitable version of pyarrow or fastparquet is required for parquet support.\n"
"Trying to import the above resulted in these errors:"
f"{error_msgs}"
)
else:
return self._engine
def __s3_file_system(self):
from s3fs import S3FileSystem
return S3FileSystem(
profile=self.connection.profile_name,
client_kwargs={
"region_name": self.connection.region_name,
**self.connection._client_kwargs,
},
)
@property
def is_unload(self):
return self._unload and self.query and self.query.strip().upper().startswith("UNLOAD")
@property
def dtypes(self) -> Dict[str, Type[Any]]:
description = self.description if self.description else []
return {
d[0]: self._converter.types[d[1]] for d in description if d[1] in self._converter.types
}
@property
def converters(
self,
) -> Dict[Optional[Any], Callable[[Optional[str]], Optional[Any]]]:
description = self.description if self.description else []
return {
d[0]: self._converter.get(d[1]) for d in description if d[1] in self._converter.mappings
}
@property
def parse_dates(self) -> List[Optional[Any]]:
description = self.description if self.description else []
return [d[0] for d in description if d[1] in self._parse_dates]
def _trunc_date(self, df: "DataFrame") -> "DataFrame":
description = self.description if self.description else []
times = [d[0] for d in description if d[1] in ("time", "time with time zone")]
if times:
df.loc[:, times] = df.loc[:, times].apply(lambda r: r.dt.time)
return df
def fetchone(
self,
) -> Optional[Union[Tuple[Optional[Any], ...], Dict[Any, Optional[Any]]]]:
try:
row = next(self._iterrows)
except StopIteration:
return None
else:
self._rownumber = row[0] + 1
description = self.description if self.description else []
return tuple([row[1][d[0]] for d in description])
def fetchmany(
self, size: Optional[int] = None
) -> List[Union[Tuple[Optional[Any], ...], Dict[Any, Optional[Any]]]]:
if not size or size <= 0:
size = self._arraysize
rows = []
for _ in range(size):
row = self.fetchone()
if row:
rows.append(row)
else:
break
return rows
def fetchall(
self,
) -> List[Union[Tuple[Optional[Any], ...], Dict[Any, Optional[Any]]]]:
rows = []
while True:
row = self.fetchone()
if row:
rows.append(row)
else:
break
return rows
def _read_csv(self) -> Union["TextFileReader", "DataFrame"]:
import pandas as pd
if not self.output_location:
raise ProgrammingError("OutputLocation is none or empty.")
if not self.output_location.endswith((".csv", ".txt")):
return pd.DataFrame()
length = self._get_content_length()
if length and self.output_location.endswith(".txt"):
sep = "\t"
header = None
description = self.description if self.description else []
names = [d[0] for d in description]
elif length and self.output_location.endswith(".csv"):
sep = ","
header = 0
names = None
else:
return pd.DataFrame()
try:
return pd.read_csv(
self.output_location,
sep=sep,
header=header,
names=names,
dtype=self.dtypes,
converters=self.converters,
parse_dates=self.parse_dates,
infer_datetime_format=True,
skip_blank_lines=False,
keep_default_na=self._keep_default_na,
na_values=self._na_values,
quoting=self._quoting,
storage_options={
"profile": self.connection.profile_name,
"client_kwargs": {
"region_name": self.connection.region_name,
**self.connection._client_kwargs,
},
},
chunksize=self._chunksize,
**self._kwargs,
)
except Exception as e:
_logger.exception(f"Failed to read {self.output_location}.")
raise OperationalError(*e.args) from e
def _read_parquet(self, engine) -> "DataFrame":
import pandas as pd
self._data_manifest = self._read_data_manifest()
if not self._data_manifest:
return pd.DataFrame()
if not self._unload_location:
self._unload_location = "/".join(self._data_manifest[0].split("/")[:-1]) + "/"
if engine == "pyarrow":
unload_location = self._unload_location
kwargs = {
"use_threads": True,
"use_legacy_dataset": False,
}
elif engine == "fastparquet":
unload_location = f"{self._unload_location}*"
kwargs = dict()
else:
raise ProgrammingError("Engine must be one of `pyarrow`, `fastparquet`.")
kwargs.update(self._kwargs)
try:
return pd.read_parquet(
unload_location,
engine=self._engine,
storage_options={
"profile": self.connection.profile_name,
"client_kwargs": {
"region_name": self.connection.region_name,
**self.connection._client_kwargs,
},
},
use_nullable_dtypes=False,
**kwargs,
)
except Exception as e:
_logger.exception(f"Failed to read {self.output_location}.")
raise OperationalError(*e.args) from e
def _read_parquet_schema(self, engine) -> Tuple[Dict[str, Any], ...]:
if engine == "pyarrow":
from pyarrow import parquet
from pyathena.arrow.util import to_column_info
if not self._unload_location:
raise ProgrammingError("UnloadLocation is none or empty.")
bucket, key = parse_output_location(self._unload_location)
try:
dataset = parquet.ParquetDataset(
f"{bucket}/{key}", filesystem=self._fs, use_legacy_dataset=False
)
return to_column_info(dataset.schema)
except Exception as e:
_logger.exception(f"Failed to read schema {bucket}/{key}.")
raise OperationalError(*e.args) from e
elif engine == "fastparquet":
from fastparquet import ParquetFile
# TODO: https://github.com/python/mypy/issues/1153
from pyathena.fastparquet.util import to_column_info # type: ignore
if not self._data_manifest:
self._data_manifest = self._read_data_manifest()
bucket, key = parse_output_location(self._data_manifest[0])
try:
file = ParquetFile(f"{bucket}/{key}", open_with=self._fs.open)
return to_column_info(file.schema)
except Exception as e:
_logger.exception(f"Failed to read schema {bucket}/{key}.")
raise OperationalError(*e.args) from e
else:
raise ProgrammingError("Engine must be one of `pyarrow`, `fastparquet`.")
def _as_pandas(self) -> Union["TextFileReader", "DataFrame"]:
if self.is_unload:
engine = self._get_engine()
df = self._read_parquet(engine)
if df.empty:
self._metadata = tuple()
else:
self._metadata = self._read_parquet_schema(engine)
else:
df = self._read_csv()
return df
def as_pandas(self) -> Union[DataFrameIterator, "DataFrame"]:
if self._chunksize is None:
return next(self._df_iter)
else:
return self._df_iter
def close(self) -> None:
import pandas as pd
super(AthenaPandasResultSet, self).close()
self._df_iter = DataFrameIterator(pd.DataFrame(), _no_trunc_date)
self._iterrows = enumerate([])
self._data_manifest = []