-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathhive.py
314 lines (270 loc) · 11.4 KB
/
hive.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
import ast
import logging
import os
import re
from functools import partial
from typing import Any, Union
import dask.dataframe as dd
try:
from pyhive import hive
except ImportError: # pragma: no cover
hive = None
try:
import sqlalchemy
except ImportError: # pragma: no cover
sqlalchemy = None
from dask_sql.input_utils.base import BaseInputPlugin
from dask_sql.mappings import cast_column_type, sql_to_python_type
logger = logging.Logger(__name__)
class HiveInputPlugin(BaseInputPlugin):
"""Input Plugin from Hive"""
def is_correct_input(
self, input_item: Any, table_name: str, format: str = None, **kwargs
):
is_sqlalchemy_hive = sqlalchemy and isinstance(
input_item, sqlalchemy.engine.base.Connection
)
is_hive_cursor = hive and isinstance(input_item, hive.Cursor)
return is_sqlalchemy_hive or is_hive_cursor or format == "hive"
def to_dc(
self,
input_item: Any,
table_name: str,
format: str = None,
gpu: bool = False,
**kwargs,
):
if gpu: # pragma: no cover
raise Exception("Hive does not support gpu")
table_name = kwargs.pop("hive_table_name", table_name)
schema = kwargs.pop("hive_schema_name", "default")
parsed = self._parse_hive_table_description(input_item, schema, table_name)
(
column_information,
table_information,
storage_information,
partition_information,
) = parsed
logger.debug("Extracted hive information: ")
logger.debug(f"column information: {column_information}")
logger.debug(f"table information: {table_information}")
logger.debug(f"storage information: {storage_information}")
logger.debug(f"partition information: {partition_information}")
# Convert column information
column_information = {
col: sql_to_python_type(col_type.upper())
for col, col_type in column_information.items()
}
# Extract format information
if "InputFormat" in storage_information:
format = storage_information["InputFormat"].split(".")[-1]
# databricks format is different, see https://github.com/dask-contrib/dask-sql/issues/83
elif "InputFormat" in table_information: # pragma: no cover
format = table_information["InputFormat"].split(".")[-1]
else: # pragma: no cover
raise RuntimeError(
"Do not understand the output of 'DESCRIBE FORMATTED <table>'"
)
if (
format == "TextInputFormat" or format == "SequenceFileInputFormat"
): # pragma: no cover
storage_description = storage_information.get("Storage Desc Params", {})
read_function = partial(
dd.read_csv,
sep=storage_description.get("field.delim", ","),
header=None,
)
elif format == "ParquetInputFormat" or format == "MapredParquetInputFormat":
read_function = dd.read_parquet
elif format == "OrcInputFormat": # pragma: no cover
read_function = dd.read_orc
elif format == "JsonInputFormat": # pragma: no cover
read_function = dd.read_json
else: # pragma: no cover
raise AttributeError(f"Do not understand hive's table format {format}")
def _normalize(loc):
if loc.startswith("dbfs:/") and not loc.startswith(
"dbfs://"
): # pragma: no cover
# dask (or better: fsspec) needs to have the URL in a specific form
# starting with two // after the protocol
loc = f"dbfs://{loc.lstrip('dbfs:')}"
# file:// is not a known protocol
loc = loc.lstrip("file:")
# Only allow files which do not start with . or _
# Especially, not allow the _SUCCESS files
return os.path.join(loc, "[A-Za-z0-9-]*")
def wrapped_read_function(location, column_information, **kwargs):
location = _normalize(location)
logger.debug(f"Reading in hive data from {location}")
if format == "ParquetInputFormat" or format == "MapredParquetInputFormat":
# Hack needed for parquet files.
# If the folder structure is like .../col=3/...
# parquet wants to read in the partition information.
# However, we add the partition information by ourself
# which will lead to problems afterwards
# Therefore tell parquet to only read in the columns
# we actually care right now
kwargs.setdefault("columns", list(column_information.keys()))
else: # pragma: no cover
# prevent python to optimize it away and make coverage not respect the
# pragma
dummy = 0 # noqa: F841
df = read_function(location, **kwargs)
logger.debug(f"Applying column information: {column_information}")
df = df.rename(columns=dict(zip(df.columns, column_information.keys())))
for col, expected_type in column_information.items():
df = cast_column_type(df, col, expected_type)
return df
if partition_information:
partition_list = self._parse_hive_partition_description(
input_item, schema, table_name
)
logger.debug(f"Reading in partitions from {partition_list}")
tables = []
for partition in partition_list:
parsed = self._parse_hive_table_description(
input_item, schema, table_name, partition=partition
)
(
partition_column_information,
partition_table_information,
_,
_,
) = parsed
location = partition_table_information["Location"]
table = wrapped_read_function(
location, partition_column_information, **kwargs
)
# Now add the additional partition columns
partition_values = ast.literal_eval(
partition_table_information["Partition Value"]
)
logger.debug(
f"Applying additional partition information as columns: {partition_information}"
)
partition_id = 0
for partition_key, partition_type in partition_information.items():
table[partition_key] = partition_values[partition_id]
table = cast_column_type(table, partition_key, partition_type)
partition_id += 1
tables.append(table)
return dd.concat(tables)
location = table_information["Location"]
df = wrapped_read_function(location, column_information, **kwargs)
return df
def _escape_partition(self, partition: str): # pragma: no cover
"""
Given a partition string like `key=value` escape the string properly for Hive.
Wrap anything but digits in quotes. Don't wrap the column name.
"""
contains_only_digits = re.compile(r"^\d+$")
try:
k, v = partition.split("=")
if re.match(contains_only_digits, v):
escaped_value = v
else:
escaped_value = f'"{v}"'
return f"{k}={escaped_value}"
except ValueError:
logger.warning(f"{partition} didn't contain a `=`")
return partition
def _parse_hive_table_description(
self,
cursor: Union["sqlalchemy.engine.base.Connection", "hive.Cursor"],
schema: str,
table_name: str,
partition: str = None,
):
"""
Extract all information from the output
of the DESCRIBE FORMATTED call, which is unfortunately
in a format not easily readable by machines.
"""
cursor.execute(f"USE {schema}")
if partition:
partition = self._escape_partition(partition)
result = self._fetch_all_results(
cursor, f"DESCRIBE FORMATTED {table_name} PARTITION ({partition})"
)
else:
result = self._fetch_all_results(cursor, f"DESCRIBE FORMATTED {table_name}")
logger.debug(f"Got information from hive: {result}")
table_information = {}
column_information = {} # using the fact that dicts are insertion ordered
storage_information = {}
partition_information = {}
mode = "column"
last_field = None
for key, value, value2 in result:
key = key.strip().rstrip(":") if key else ""
value = value.strip() if value else ""
value2 = value2.strip() if value2 else ""
# That is just a comment line, we can skip it
if key == "# col_name":
continue
if (
key == "# Detailed Table Information"
or key == "# Detailed Partition Information"
):
mode = "table"
elif key == "# Storage Information":
mode = "storage"
elif key == "# Partition Information":
mode = "partition"
elif key.startswith("#"):
mode = None # pragma: no cover
elif key:
if not value:
value = dict()
if mode == "column":
column_information[key] = value
last_field = column_information[key]
elif mode == "storage":
storage_information[key] = value
last_field = storage_information[key]
elif mode == "table":
table_information[key] = value
last_field = table_information[key]
elif mode == "partition":
partition_information[key] = value
last_field = partition_information[key]
else: # pragma: no cover
# prevent python to optimize it away and make coverage not respect the
# pragma
dummy = 0 # noqa: F841
elif value and last_field is not None:
last_field[value] = value2
return (
column_information,
table_information,
storage_information,
partition_information,
)
def _parse_hive_partition_description(
self,
cursor: Union["sqlalchemy.engine.base.Connection", "hive.Cursor"],
schema: str,
table_name: str,
):
"""
Extract all partition informaton for a given table
"""
cursor.execute(f"USE {schema}")
result = self._fetch_all_results(cursor, f"SHOW PARTITIONS {table_name}")
return [row[0] for row in result]
def _fetch_all_results(
self,
cursor: Union["sqlalchemy.engine.base.Connection", "hive.Cursor"],
sql: str,
):
"""
The pyhive.Cursor and the sqlalchemy connection behave slightly different.
The former has the fetchall method on the cursor,
whereas the latter on the executed query.
"""
result = cursor.execute(sql)
try:
return result.fetchall()
except AttributeError: # pragma: no cover
return cursor.fetchall()