-
Notifications
You must be signed in to change notification settings - Fork 247
/
sqlalchemy.py
303 lines (243 loc) · 11 KB
/
sqlalchemy.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
# Copyright 2014 Cloudera Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# some inspiration from Dropbox's PyHive
from __future__ import absolute_import
import re
from sqlalchemy.dialects import registry
from sqlalchemy.engine.default import DefaultDialect, DefaultExecutionContext
from sqlalchemy.sql.compiler import (DDLCompiler, GenericTypeCompiler,
IdentifierPreparer)
from sqlalchemy.types import (BOOLEAN, SMALLINT, BIGINT, TIMESTAMP, FLOAT,
DECIMAL, Integer, Float, String, CHAR, VARCHAR,
DATE)
from sqlalchemy import text
registry.register('impala', 'impala.sqlalchemy', 'ImpalaDialect')
registry.register('impala4', 'impala.sqlalchemy', 'Impala4Dialect')
class TINYINT(Integer):
__visit_name__ = 'TINYINT'
class INT(Integer):
__visit_name__ = 'INT'
class DOUBLE(Float):
__visit_name__ = 'DOUBLE'
class STRING(String):
__visit_name__ = 'STRING'
class ImpalaDDLCompiler(DDLCompiler):
# Omit support for foreign keys due to Impala's limited support - https://issues.apache.org/jira/browse/IMPALA-2112
def visit_foreign_key_constraint(self, constraint):
return None
# Omit support for primary keys due to Impala's limited support - https://issues.apache.org/jira/browse/IMPALA-2112
def visit_primary_key_constraint(self, constraint):
return None
def get_column_specification(self, column, **kwargs):
column.nullable = True # Prevent adding NOT NULL constraints; since all columns in Impala are nullable
colspec = super(ImpalaDDLCompiler, self).get_column_specification(
column, **kwargs
)
return colspec
def post_create_table(self, table):
"""Build table-level CREATE options."""
table_opts = []
if 'impala_partitioned_by' in table.kwargs:
table_opts.append('PARTITIONED BY %s' % table.kwargs.get('impala_partitioned_by'))
if 'impala_partition_by' in table.kwargs:
table_opts.append('PARTITION BY %s' % table.kwargs.get('impala_partition_by'))
if 'impala_stored_as' in table.kwargs:
table_opts.append('STORED AS %s' % table.kwargs.get('impala_stored_as'))
if 'impala_table_properties' in table.kwargs:
table_properties = ["'{0}' = '{1}'".format(property_, value)
for property_, value
in sorted(table.kwargs.get('impala_table_properties', {}).items())]
table_opts.append('TBLPROPERTIES (%s)' % ', '.join(table_properties))
return '\n%s' % '\n'.join(table_opts)
# Impala type compiler for Impala <= 3.4.0
class ImpalaTypeCompiler(GenericTypeCompiler):
# pylint: disable=unused-argument
# https://docs.cloudera.com/documentation/enterprise/6/6.3/topics/impala_porting.html
def visit_TEXT(self, type_):
return 'STRING'
visit_NCHAR = visit_TEXT
visit_NVARCHAR = visit_TEXT
def visit_DATETIME(self, type_):
return 'TIMESTAMP'
# Impala <= 3.4.0 only supports the TIMESTAMP type
visit_TIME = visit_DATETIME
visit_DATE = visit_DATETIME
def visit_TINYINT(self, type_):
return 'TINYINT'
def visit_INT(self, type_):
return 'INT'
def visit_DOUBLE(self, type_):
return 'DOUBLE'
def visit_STRING(self, type_):
return 'STRING'
# Impala type compiler for Impala >= 4.0
class Impala4TypeCompiler(ImpalaTypeCompiler):
# Impala >= 4.0 supports the DATE type - https://issues.apache.org/jira/browse/IMPALA-6169
def visit_DATE(self, type_):
return 'DATE'
class ImpalaIdentifierPreparer(IdentifierPreparer):
# https://github.com/cloudera/Impala/blob/master/fe/src/main/jflex/sql-scanner.flex
reserved_words = frozenset([
'add', 'aggregate', 'all', 'alter', 'analytic', 'and', 'anti',
'api_version', 'array', 'as', 'asc', 'avro', 'between', 'bigint',
'binary', 'boolean', 'by', 'cached', 'case', 'cast', 'change', 'char',
'class', 'close_fn', 'column', 'columns', 'comment', 'compute',
'create', 'cross', 'current', 'data', 'database', 'databases', 'date',
'datetime', 'decimal', 'delimited', 'desc', 'describe', 'distinct',
'div', 'double', 'drop', 'else', 'end', 'escaped', 'exists', 'explain',
'external', 'false', 'fields', 'fileformat', 'finalize_fn', 'first',
'float', 'following', 'for', 'format', 'formatted', 'from', 'full',
'function', 'functions', 'grant', 'group', 'having', 'if', 'in',
'init_fn', 'inner', 'inpath', 'insert', 'int', 'integer',
'intermediate', 'interval', 'into', 'invalidate', 'is', 'join', 'last',
'left', 'like', 'limit', 'lines', 'load', 'location', 'map',
'merge_fn', 'metadata', 'not', 'null', 'nulls', 'offset', 'on', 'or',
'order', 'outer', 'over', 'overwrite', 'parquet', 'parquetfile',
'partition', 'partitioned', 'partitions', 'preceding', 'prepare_fn',
'produced', 'range', 'rcfile', 'real', 'refresh', 'regexp', 'rename',
'replace', 'returns', 'revoke', 'right', 'rlike', 'role', 'roles',
'row', 'rows', 'schema', 'schemas', 'select', 'semi', 'sequencefile',
'serdeproperties', 'serialize_fn', 'set', 'show', 'smallint', 'stats',
'stored', 'straight_join', 'string', 'struct', 'symbol', 'table',
'tables', 'tblproperties', 'terminated', 'textfile', 'then',
'timestamp', 'tinyint', 'to', 'true', 'unbounded', 'uncached', 'union',
'update_fn', 'use', 'using', 'values', 'varchar', 'view', 'when',
'where', 'with'])
legal_characters = re.compile(r'^[A-Z0-9_]+$', re.I)
def __init__(self, dialect):
super(ImpalaIdentifierPreparer, self).__init__(dialect,
initial_quote='`')
_impala_type_to_sqlalchemy_type = {
'BOOLEAN': BOOLEAN,
'TINYINT': TINYINT,
'SMALLINT': SMALLINT,
'INT': INT,
'BIGINT': BIGINT,
'TIMESTAMP': TIMESTAMP,
'FLOAT': FLOAT,
'DOUBLE': DOUBLE,
'STRING': STRING,
'DECIMAL': DECIMAL,
'CHAR': CHAR,
'VARCHAR': VARCHAR,
'DATE': DATE
}
class ImpalaExecutionContext(DefaultExecutionContext):
def create_cursor(self):
self._is_server_side = False
cursor_configuration = self.execution_options.get('cursor_configuration', {})
return self._dbapi_connection.cursor(configuration=cursor_configuration)
# Dialect to be used with Impala <= 3.4.0
class ImpalaDialect(DefaultDialect):
name = 'impala'
driver = 'impala'
paramstyle = 'pyformat'
preparer = ImpalaIdentifierPreparer
max_identifier_length = 128
supports_sane_rowcount = False
supports_sane_multi_rowcount = False
supports_multivalues_insert = True
supports_sequences = False
supports_native_decimal = True
supports_native_boolean = True
supports_native_enum = False
supports_default_values = False
returns_unicode_strings = True
ddl_compiler = ImpalaDDLCompiler
type_compiler = ImpalaTypeCompiler
execution_ctx_cls = ImpalaExecutionContext
@classmethod
def dbapi(cls):
# pylint: disable=method-hidden
import impala.dbapi
return impala.dbapi
def create_connect_args(self, url):
kwargs = {
'host': url.host,
'port': url.port,
'user': url.username,
'password': url.password,
'database': url.database,
}
kwargs.update(url.query)
return ([], kwargs)
def initialize(self, connection):
self.default_schema_name = connection.connection.default_db
def _get_server_version_info(self, connection):
raw = connection.execute(text('select version()')).scalar()
v = raw.split()[2]
m = re.match(r'.*?(\d{1,3})\.(\d{1,3})\.(\d{1,3}).*', v)
return tuple([int(x) for x in m.group(1, 2, 3) if x is not None])
def has_table(self, connection, table_name, schema=None):
tables = self.get_table_names(connection, schema)
if table_name in tables:
return True
return False
def get_table_names(self, connection, schema=None, **kw):
query = 'SHOW TABLES'
if schema is not None:
escaped_schema = re.sub(r"^[\`]{1,}|[\`]{1,}$", '', schema)
escaped_schema = re.sub(r"^|$", '`', escaped_schema)
query += ' IN %s' % escaped_schema
tables = [
tup[1] if len(tup) > 1 else tup[0]
for tup in connection.execute(text(query)).fetchall()
]
return tables
def get_view_names(self, connection, schema=None, **kw):
# Impala doesn't distinguish between tables and view when calling
# SHOW TABLES. So return a blank list.
return []
def get_schema_names(self, connection, **kw):
rp = connection.execute(text("SHOW SCHEMAS"))
return [r[0] for r in rp]
def get_columns(self, connection, table_name, schema=None, **kwargs):
# pylint: disable=unused-argument
name = table_name
if schema is not None:
name = '%s.%s' % (schema, name)
query = 'SELECT * FROM %s LIMIT 0' % name
cursor = connection.execute(text(query))
schema = cursor.cursor.description
# We need to fetch the empty results otherwise these queries remain in
# flight
cursor.fetchall()
column_info = []
# `select * from table` results in 'tablename.columnname', so strip off 'tablename.'
for col in schema:
column_info.append({
'name': col[0].split('.')[-1].strip() if '.' in col[0] else col[0],
'type': _impala_type_to_sqlalchemy_type[col[1]],
'nullable': True,
'autoincrement': False})
return column_info
def get_pk_constraint(self, connection, table_name, schema=None, **kw):
# pylint: disable=unused-argument
# no primary keys in impala
return {'constrained_columns': [], 'name': None}
def get_foreign_keys(self, connection, table_name, schema=None, **kw):
# no foreign keys in impala
return []
def get_indexes(self, connection, table_name, schema=None, **kw):
# no indexes in impala
# TODO(laserson): handle partitions, like in PyHive
return []
def do_rollback(self, dbapi_connection):
# no transactions in impala
pass
# Dialect to be used with Impala >= 4.0
class Impala4Dialect(ImpalaDialect):
name = 'impala4'
driver = 'impala4'
type_compiler = Impala4TypeCompiler