This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
sqlalchemy_athena.py
394 lines (323 loc) · 11.7 KB
/
sqlalchemy_athena.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 re
from sqlalchemy import exc, util
from sqlalchemy.engine import Engine, reflection
from sqlalchemy.engine.default import DefaultDialect
from sqlalchemy.sql.compiler import (
DDLCompiler,
GenericTypeCompiler,
IdentifierPreparer,
SQLCompiler,
)
from sqlalchemy.sql.sqltypes import (
BIGINT,
BINARY,
BOOLEAN,
DATE,
DECIMAL,
FLOAT,
INTEGER,
NULLTYPE,
STRINGTYPE,
TIMESTAMP,
)
import pyathenajdbc
class UniversalSet(object):
"""UniversalSet
https://github.com/dropbox/PyHive/blob/master/pyhive/common.py"""
def __contains__(self, item):
return True
class AthenaDMLIdentifierPreparer(IdentifierPreparer):
"""PrestoIdentifierPreparer
https://github.com/dropbox/PyHive/blob/master/pyhive/sqlalchemy_presto.py"""
reserved_words = UniversalSet()
class AthenaDDLIdentifierPreparer(IdentifierPreparer):
def __init__(
self,
dialect,
initial_quote="`",
final_quote=None,
escape_quote="`",
quote_case_sensitive_collations=True,
omit_schema=False,
):
super(AthenaDDLIdentifierPreparer, self).__init__(
dialect=dialect,
initial_quote=initial_quote,
final_quote=final_quote,
escape_quote=escape_quote,
quote_case_sensitive_collations=quote_case_sensitive_collations,
omit_schema=omit_schema,
)
class AthenaStatementCompiler(SQLCompiler):
"""PrestoCompiler
https://github.com/dropbox/PyHive/blob/master/pyhive/sqlalchemy_presto.py"""
def visit_char_length_func(self, fn, **kw):
return "length{0}".format(self.function_argspec(fn, **kw))
class AthenaTypeCompiler(GenericTypeCompiler):
def visit_FLOAT(self, type_, **kw):
return self.visit_REAL(type_, **kw)
def visit_REAL(self, type_, **kw):
return "DOUBLE"
def visit_NUMERIC(self, type_, **kw):
return self.visit_DECIMAL(type_, **kw)
def visit_DECIMAL(self, type_, **kw):
if type_.precision is None:
return "DECIMAL"
elif type_.scale is None:
return "DECIMAL(%(precision)s)" % {"precision": type_.precision}
else:
return "DECIMAL(%(precision)s, %(scale)s)" % {
"precision": type_.precision,
"scale": type_.scale,
}
def visit_INTEGER(self, type_, **kw):
return "INTEGER"
def visit_SMALLINT(self, type_, **kw):
return "SMALLINT"
def visit_BIGINT(self, type_, **kw):
return "BIGINT"
def visit_TIMESTAMP(self, type_, **kw):
return "TIMESTAMP"
def visit_DATETIME(self, type_, **kw):
return self.visit_TIMESTAMP(type_, **kw)
def visit_DATE(self, type_, **kw):
return "DATE"
def visit_TIME(self, type_, **kw):
raise exc.CompileError("Data type `{0}` is not supported".format(type_))
def visit_CLOB(self, type_, **kw):
return self.visit_BINARY(type_, **kw)
def visit_NCLOB(self, type_, **kw):
return self.visit_BINARY(type_, **kw)
def visit_CHAR(self, type_, **kw):
return self._render_string_type(type_, "CHAR")
def visit_NCHAR(self, type_, **kw):
return self._render_string_type(type_, "CHAR")
def visit_VARCHAR(self, type_, **kw):
return self._render_string_type(type_, "VARCHAR")
def visit_NVARCHAR(self, type_, **kw):
return self._render_string_type(type_, "VARCHAR")
def visit_TEXT(self, type_, **kw):
return "STRING"
def visit_BLOB(self, type_, **kw):
return self.visit_BINARY(type_, **kw)
def visit_BINARY(self, type_, **kw):
return "BINARY"
def visit_VARBINARY(self, type_, **kw):
return self.visit_BINARY(type_, **kw)
def visit_BOOLEAN(self, type_, **kw):
return "BOOLEAN"
class AthenaDDLCompiler(DDLCompiler):
@property
def preparer(self):
return self._preparer
@preparer.setter
def preparer(self, value):
pass
def __init__(
self,
dialect,
statement,
schema_translate_map=None,
compile_kwargs=util.immutabledict(),
):
self._preparer = AthenaDDLIdentifierPreparer(dialect)
super(AthenaDDLCompiler, self).__init__(
dialect=dialect,
statement=statement,
schema_translate_map=schema_translate_map,
compile_kwargs=compile_kwargs,
)
def visit_create_table(self, create):
table = create.element
preparer = self.preparer
text = "\nCREATE EXTERNAL "
text += "TABLE " + preparer.format_table(table) + " "
text += "("
separator = "\n"
for create_column in create.columns:
column = create_column.element
try:
processed = self.process(create_column)
if processed is not None:
text += separator
separator = ", \n"
text += "\t" + processed
except exc.CompileError as ce:
util.raise_from_cause(
exc.CompileError(
util.u("(in table '{0}', column '{1}'): {2}").format(
table.description, column.name, ce.args[0]
)
)
)
const = self.create_table_constraints(
table,
_include_foreign_key_constraints=create.include_foreign_key_constraints,
)
if const:
text += separator + "\t" + const
text += "\n)\n%s\n\n" % self.post_create_table(table)
return text
def post_create_table(self, table):
raw_connection = table.bind.raw_connection()
# TODO Supports orc, avro, json, csv or tsv format
text = "STORED AS PARQUET\n"
location = (
raw_connection._driver_kwargs["S3Location"]
if "S3Location" in raw_connection._driver_kwargs
else raw_connection._driver_kwargs.get("S3OutputLocation")
)
if not location:
raise exc.CompileError(
"`S3Location` or `S3OutputLocation` parameter is required"
" in the connection string."
)
schema = table.schema if table.schema else raw_connection.schema_name
text += "LOCATION '{0}{1}/{2}/'\n".format(location, schema, table.name)
compression = raw_connection._driver_kwargs.get("compression")
if compression:
text += "TBLPROPERTIES ('parquet.compress'='{0}')\n".format(
compression.upper()
)
return text
_TYPE_MAPPINGS = {
"boolean": BOOLEAN,
"real": FLOAT,
"float": FLOAT,
"double": FLOAT,
"tinyint": INTEGER,
"smallint": INTEGER,
"integer": INTEGER,
"bigint": BIGINT,
"decimal": DECIMAL,
"char": STRINGTYPE,
"varchar": STRINGTYPE,
"array": STRINGTYPE,
"row": STRINGTYPE, # StructType
"varbinary": BINARY,
"map": STRINGTYPE,
"date": DATE,
"timestamp": TIMESTAMP,
}
class AthenaDialect(DefaultDialect):
name = "awsathena"
driver = "jdbc"
preparer = AthenaDMLIdentifierPreparer
statement_compiler = AthenaStatementCompiler
ddl_compiler = AthenaDDLCompiler
type_compiler = AthenaTypeCompiler
default_paramstyle = pyathenajdbc.paramstyle
supports_alter = False
supports_pk_autoincrement = False
supports_default_values = False
supports_empty_insert = False
supports_multivalues_insert = True
supports_native_decimal = True
supports_native_boolean = True
supports_unicode_statements = True
supports_unicode_binds = True
returns_unicode_strings = True
description_encoding = None
postfetch_lastrowid = False
_pattern_column_type = re.compile(r"^([a-zA-Z]+)($|\(.+\)$)")
@classmethod
def dbapi(cls):
return pyathenajdbc
def _raw_connection(self, connection):
if isinstance(connection, Engine):
return connection.raw_connection()
return connection.connection
def create_connect_args(self, url):
# Connection string format:
# awsathena+jdbc://
# {User}:{Password}@athena.{AwsRegion}.amazonaws.com:443/
# {Schema}?S3OutputLocation={S3OutputLocation}&driver_path={driver_path}&...
opts = {
"User": url.username if url.username else None,
"Password": url.password if url.password else None,
"AwsRegion": re.sub(
r"^athena\.([a-z0-9-]+)\.amazonaws\.(com|com.cn)$", r"\1", url.host
),
"Schema": url.database if url.database else "default",
}
opts.update(url.query)
return [[], opts]
@reflection.cache
def get_schema_names(self, connection, **kw):
query = """
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name NOT IN ('information_schema')
"""
return [row.schema_name for row in connection.execute(query).fetchall()]
@reflection.cache
def get_table_names(self, connection, schema=None, **kw):
raw_connection = self._raw_connection(connection)
schema = schema if schema else raw_connection.schema_name
query = """
SELECT table_name
FROM information_schema.tables
WHERE table_schema = '{0}'
""".format(
schema
)
return [row.table_name for row in connection.execute(query).fetchall()]
def has_table(self, connection, table_name, schema=None):
table_names = self.get_table_names(connection, schema)
if table_name in table_names:
return True
return False
@reflection.cache
def get_columns(self, connection, table_name, schema=None, **kw):
raw_connection = self._raw_connection(connection)
schema = schema if schema else raw_connection.schema_name
query = """
SELECT
table_schema,
table_name,
column_name,
data_type,
is_nullable,
column_default,
ordinal_position,
comment
FROM information_schema.columns
WHERE table_schema = '{schema}'
AND table_name = '{table}'
""".format(
schema=schema, table=table_name
)
return [
{
"name": row.column_name,
"type": _TYPE_MAPPINGS.get(
self._get_column_type(row.data_type), NULLTYPE
),
"nullable": True if row.is_nullable == "YES" else False,
"default": row.column_default,
"ordinal_position": row.ordinal_position,
"comment": row.comment,
}
for row in connection.execute(query).fetchall()
]
def _get_column_type(self, type_):
return self._pattern_column_type.sub(r"\1", type_)
def get_foreign_keys(self, connection, table_name, schema=None, **kw):
# Athena has no support for foreign keys.
return []
def get_pk_constraint(self, connection, table_name, schema=None, **kw):
# Athena has no support for primary keys.
return []
def get_indexes(self, connection, table_name, schema=None, **kw):
# Athena has no support for indexes.
return []
def do_rollback(self, dbapi_connection):
# No transactions for Athena
pass
def _check_unicode_returns(self, connection, additional_tests=None):
# Requests gives back Unicode strings
return True
def _check_unicode_description(self, connection):
# Requests gives back Unicode strings
return True