Repository navigation
Expand file tree
/
Copy pathdialect.py
More file actions
359 lines (287 loc) · 11.4 KB
/
Copy pathdialect.py
File metadata and controls
359 lines (287 loc) · 11.4 KB
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
"""Integration between SQLAlchemy and Hive.
Some code based on
https://github.com/zzzeek/sqlalchemy/blob/rel_0_5/lib/sqlalchemy/databases/sqlite.py
which is released under the MIT license.
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import datetime
import decimal
import logging
from decimal import Decimal
from dateutil.parser import parse
from sqlalchemy import exc
from sqlalchemy import processors
from sqlalchemy import types
from sqlalchemy.databases import mysql
from sqlalchemy.engine import default, Engine, Connection
from sqlalchemy.sql import compiler
from sqlalchemy.sql.compiler import SQLCompiler
from e6data_python_connector import e6data_grpc
from e6data_python_connector.common import UniversalSet
from e6data_python_connector.exceptions import *
_logger = logging.getLogger(__name__)
class E6dataStringTypeBase(types.TypeDecorator):
"""Translates strings returned by Thrift into something else"""
impl = types.String
def process_bind_param(self, value, dialect):
raise NotImplementedError("Writing to Hive not supported")
class E6dataDate(E6dataStringTypeBase):
"""Translates date strings to date objects"""
impl = types.DATE
def process_result_value(self, value, dialect):
return processors.str_to_date(value)
def result_processor(self, dialect, coltype):
def process(value):
if isinstance(value, datetime.datetime):
return value.date()
elif isinstance(value, datetime.date):
return value
elif value is not None:
return parse(value).date()
else:
return None
return process
def adapt(self, impltype, **kwargs):
return self.impl
class E6dataTimestamp(E6dataStringTypeBase):
"""Translates timestamp strings to datetime objects"""
impl = types.TIMESTAMP
def process_result_value(self, value, dialect):
return processors.str_to_datetime(value)
def result_processor(self, dialect, coltype):
def process(value):
if isinstance(value, datetime.datetime):
return value
elif value is not None:
return parse(value)
else:
return None
return process
def adapt(self, impltype, **kwargs):
return self.impl
class E6dataDecimal(E6dataStringTypeBase):
"""Translates strings to decimals"""
impl = types.DECIMAL
def process_result_value(self, value, dialect):
if value is not None:
return decimal.Decimal(value)
else:
return None
def result_processor(self, dialect, coltype):
def process(value):
if isinstance(value, Decimal):
return value
elif value is not None:
return Decimal(value)
else:
return None
return process
def adapt(self, impltype, **kwargs):
return self.impl
class E6dataIdentifierPreparer(compiler.IdentifierPreparer):
# Just quote everything to make things simpler / easier to upgrade
reserved_words = UniversalSet()
def __init__(self, dialect):
super(E6dataIdentifierPreparer, self).__init__(
dialect,
initial_quote='"',
)
_type_map = {
'boolean': types.Boolean,
'tinyint': mysql.MSTinyInteger,
'smallint': types.SmallInteger,
'integer': types.Integer,
'bigint': types.BigInteger,
'float': types.Float,
'double': types.Float,
'string': types.String,
'varchar': types.String,
'char': types.String,
'date': E6dataDate,
'timestamp': E6dataTimestamp,
'binary': types.String,
'array': types.String,
'map': types.String,
'struct': types.String,
'uniontype': types.String,
'decimal': E6dataDecimal,
}
class E6dataCompiler(SQLCompiler):
def visit_concat_op_binary(self, binary, operator, **kw):
return "concat(%s, %s)" % (self.process(binary.left), self.process(binary.right))
def visit_insert(self, *args, **kwargs):
raise NotSupportedError()
def visit_column(self, *args, **kwargs):
result = super(E6dataCompiler, self).visit_column(*args, **kwargs)
return result
def visit_char_length_func(self, fn, **kw):
return 'length{}'.format(self.function_argspec(fn, **kw))
class E6dataTypeCompiler(compiler.GenericTypeCompiler):
def visit_INTEGER(self, type_, **kwargs):
return 'INT'
def visit_NUMERIC(self, type_, **kwargs):
return 'DECIMAL'
def visit_CHAR(self, type_, **kwargs):
return 'STRING'
def visit_VARCHAR(self, type_, **kwargs):
return 'STRING'
def visit_NCHAR(self, type_, **kwargs):
return 'STRING'
def visit_TEXT(self, type_, **kwargs):
return 'STRING'
def visit_CLOB(self, type_, **kwargs):
return 'STRING'
def visit_BLOB(self, type_, **kwargs):
return 'BINARY'
def visit_TIME(self, type_, **kwargs):
return 'TIMESTAMP'
def visit_DATE(self, type_, **kwargs):
return 'DATE'
def visit_DATETIME(self, type_, **kwargs):
return 'TIMESTAMP'
class E6dataDialect(default.DefaultDialect):
preparer = E6dataIdentifierPreparer
statement_compiler = E6dataCompiler
supports_views = True
supports_alter = True
supports_pk_autoincrement = False
supports_default_values = False
supports_empty_insert = False
supports_native_decimal = True
supports_native_boolean = True
supports_unicode_statements = True
supports_unicode_binds = True
returns_unicode_strings = True
description_encoding = None
supports_multivalues_insert = True
type_compiler = E6dataTypeCompiler
supports_sane_rowcount = False
driver = 'e6data'
scheme = 'e6data'
catalog_name = None
cluster_uuid = None
secure = False
auto_resume = False
def _dialect_specific_select_one(self):
return "NOOP"
@classmethod
def dbapi(cls):
return e6data_grpc
def create_connect_args(self, url):
database = None
if url.query.get("schema"):
database = url.query.get("schema")
self.catalog_name = url.query.get("catalog")
self.cluster_uuid = url.query.get("cluster-uuid")
self.secure = url.query.get("secure") == "true"
self.auto_resume = url.query.get("auto-resume", "true") == "true" # default to True
if not self.catalog_name:
raise Exception('Please specify catalog in query parameter.')
# Extract grpc_options from query params
grpc_options = {}
for key, value in url.query.items():
# Skip known parameters that are not grpc_options
if key not in {"schema", "catalog", "cluster-uuid", "secure", "auto-resume"}:
grpc_options[key] = value
kwargs = {
"host": url.host,
"port": url.port,
"scheme": self.scheme,
"username": url.username or None,
"password": url.password or None,
"database": database,
"catalog": self.catalog_name,
"cluster_uuid": self.cluster_uuid,
'secure': self.secure,
'auto_resume': self.auto_resume,
'grpc_options': grpc_options
}
return [], kwargs
def get_schema_names(self, connection, **kw):
# Equivalent to SHOW DATABASES
# Rerouting to view names
if isinstance(connection, Engine):
cursor = connection.raw_connection().connection.cursor(catalog_name=self.catalog_name)
elif isinstance(connection, Connection):
cursor = connection.connection.cursor(catalog_name=self.catalog_name)
else:
raise Exception("Got type of object {typ}".format(typ=type(connection)))
client = cursor.connection
return client.get_schema_names(catalog=self.catalog_name)
def get_view_names(self, connection, schema=None, **kw):
return []
def _get_table_columns(self, connection, schema, table):
try:
if isinstance(connection, Engine):
cursor = connection.raw_connection().connection.cursor(catalog_name=self.catalog_name)
elif isinstance(connection, Connection):
cursor = connection.connection.cursor(catalog_name=self.catalog_name)
else:
raise Exception("Got type of object {typ}".format(typ=type(connection)))
client = cursor.connection
columns = client.get_columns(self, self.catalog_name, schema, table)
rows = list()
for column in columns:
row = dict()
row["col_name"] = column.get('fieldName')
row["data_type"] = column.get('fieldType')
rows.append(row)
return rows
except exc.OperationalError as e:
# Does the table exist?
raise e
def has_table(self, connection, table_name, schema=None, **kwargs):
return True
# try:
# self._get_table_columns(connection, schema, table_name)
# return True
# except Exception as e:
# return False
def get_columns(self, connection, table_name, schema, **kwargs):
if isinstance(connection, Engine):
cursor = connection.raw_connection().connection.cursor(catalog_name=self.catalog_name)
elif isinstance(connection, Connection):
cursor = connection.connection.cursor(catalog_name=self.catalog_name)
else:
raise Exception("Got type of object {typ}".format(typ=type(connection)))
client = cursor.connection
columns = client.get_columns(self.catalog_name, schema, table_name)
rows = list()
for column in columns:
row = dict()
row["name"] = column.get('fieldName')
row["type"] = lambda: column.get('fieldType')
rows.append(row)
return rows
def get_foreign_keys(self, connection, table_name, schema=None, **kw):
# Hive has no support for foreign keys.
return []
def get_pk_constraint(self, connection, table_name, schema=None, **kw):
# Hive has no support for primary keys.
return []
def get_indexes(self, connection, table_name, schema=None, **kw):
return []
def get_table_names(self, connection, schema=None, **kw):
# Hive does not provide functionality to query tableType
# This allows reflection to not crash at the cost of being inaccurate
if isinstance(connection, Engine):
cursor = connection.raw_connection().connection.cursor(catalog_name=self.catalog_name)
elif isinstance(connection, Connection):
cursor = connection.connection.cursor(catalog_name=self.catalog_name)
else:
raise Exception("Got type of object {typ}".format(typ=type(connection)))
client = cursor.connection
return client.get_tables(self.catalog_name, schema)
def do_rollback(self, dbapi_connection):
# No transactions for Hive
pass
def _check_unicode_returns(self, connection, additional_tests=None):
# We decode everything as UTF-8
return True
def _check_unicode_description(self, connection):
# We decode everything as UTF-8
return True
def do_ping(self, connection):
# We do not need the ping api as we are using http
return True