This repository has been archived by the owner on May 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
dialect.py
298 lines (253 loc) · 12 KB
/
dialect.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
import re
from textwrap import dedent
from typing import *
from sqlalchemy import exc, sql
from sqlalchemy.engine.base import Connection
from sqlalchemy.engine.default import DefaultDialect, DefaultExecutionContext
from sqlalchemy.engine.url import URL
from trino.auth import BasicAuthentication, JWTAuthentication
from trino.constants import HTTPS
from trino.dbapi import Cursor
from . import compiler
from . import datatype
from . import dbapi as trino_dbapi
from . import error
class TrinoDialect(DefaultDialect):
name = 'trino'
driver = 'rest'
statement_compiler = compiler.TrinoSQLCompiler
ddl_compiler = compiler.TrinoDDLCompiler
type_compiler = compiler.TrinoTypeCompiler
preparer = compiler.TrinoIdentifierPreparer
# Data Type
supports_native_enum = False
supports_native_boolean = True
supports_native_decimal = True
# Column options
supports_sequences = False
supports_comments = True
inline_comments = True
supports_default_values = False
# DDL
supports_alter = True
# DML
supports_empty_insert = False
supports_multivalues_insert = True
postfetch_lastrowid = False
# Version parser
__version_pattern = re.compile(r'(\d+).*')
@classmethod
def dbapi(cls):
"""
ref: https://www.python.org/dev/peps/pep-0249/#module-interface
"""
return trino_dbapi
def create_connect_args(self, url: URL) -> Tuple[List[Any], Dict[str, Any]]:
args, kwargs = super(TrinoDialect, self).create_connect_args(url) # type: List[Any], Dict[str, Any]
db_parts = kwargs.pop('database', 'system').split('/')
if len(db_parts) == 1:
kwargs['catalog'] = db_parts[0]
elif len(db_parts) == 2:
kwargs['catalog'] = db_parts[0]
kwargs['schema'] = db_parts[1]
else:
raise ValueError(f'Unexpected database format {url.database}')
username = kwargs.pop('username', 'anonymous')
session_user = kwargs.pop('sessionUser', username)
kwargs['user'] = session_user
password = kwargs.pop('password', None)
jwt_token = kwargs.pop('accessToken', None)
if password:
kwargs['auth'] = BasicAuthentication(username, password)
elif jwt_token:
kwargs['auth'] = JWTAuthentication(jwt_token)
if 'auth' in kwargs:
kwargs['http_scheme'] = HTTPS
return args, kwargs
def get_columns(self, connection: Connection,
table_name: str, schema: str = None, **kw) -> List[Dict[str, Any]]:
if not self.has_table(connection, table_name, schema):
raise exc.NoSuchTableError(f'schema={schema}, table={table_name}')
return self._get_columns(connection, table_name, schema, **kw)
def _get_columns(self, connection: Connection,
table_name: str, schema: str = None, **kw) -> List[Dict[str, Any]]:
schema = schema or self._get_default_schema_name(connection)
query = dedent('''
SELECT
"column_name",
"data_type",
"column_default",
UPPER("is_nullable") AS "is_nullable"
FROM "information_schema"."columns"
WHERE "table_schema" = :schema
AND "table_name" = :table
ORDER BY "ordinal_position" ASC
''').strip()
res = connection.execute(sql.text(query), schema=schema, table=table_name)
columns = []
for record in res:
column = dict(
name=record.column_name,
type=datatype.parse_sqltype(record.data_type),
nullable=record.is_nullable == 'YES',
default=record.column_default,
)
columns.append(column)
return columns
def get_pk_constraint(self, connection: Connection,
table_name: str, schema: str = None, **kw) -> Dict[str, Any]:
"""Trino has no support for primary keys. Returns a dummy"""
return dict(name=None, constrained_columns=[])
def get_primary_keys(self, connection: Connection,
table_name: str, schema: str = None, **kw) -> List[str]:
pk = self.get_pk_constraint(connection, table_name, schema)
return pk.get('constrained_columns') # type: List[str]
def get_foreign_keys(self, connection: Connection,
table_name: str, schema: str = None, **kw) -> List[Dict[str, Any]]:
"""Trino has no support for foreign keys. Returns an empty list."""
return []
def get_schema_names(self, connection: Connection, **kw) -> List[str]:
query = dedent('''
SELECT "schema_name"
FROM "information_schema"."schemata"
''').strip()
res = connection.execute(sql.text(query))
return [row.schema_name for row in res]
def get_table_names(self, connection: Connection, schema: str = None, **kw) -> List[str]:
schema = schema or self._get_default_schema_name(connection)
if schema is None:
raise exc.NoSuchTableError('schema is required')
query = dedent('''
SELECT "table_name"
FROM "information_schema"."tables"
WHERE "table_schema" = :schema
''').strip()
res = connection.execute(sql.text(query), schema=schema)
return [row.table_name for row in res]
def get_temp_table_names(self, connection: Connection, schema: str = None, **kw) -> List[str]:
"""Trino has no support for temporary tables. Returns an empty list."""
return []
def get_view_names(self, connection: Connection, schema: str = None, **kw) -> List[str]:
schema = schema or self._get_default_schema_name(connection)
if schema is None:
raise exc.NoSuchTableError('schema is required')
query = dedent('''
SELECT "table_name"
FROM "information_schema"."views"
WHERE "table_schema" = :schema
''').strip()
res = connection.execute(sql.text(query), schema=schema)
return [row.table_name for row in res]
def get_temp_view_names(self, connection: Connection, schema: str = None, **kw) -> List[str]:
"""Trino has no support for temporary views. Returns an empty list."""
return []
def get_view_definition(self, connection: Connection, view_name: str, schema: str = None, **kw) -> str:
schema = schema or self._get_default_schema_name(connection)
if schema is None:
raise exc.NoSuchTableError('schema is required')
query = dedent('''
SELECT "view_definition"
FROM "information_schema"."views"
WHERE "table_schema" = :schema
AND "table_name" = :view
''').strip()
res = connection.execute(sql.text(query), schema=schema, view=view_name)
return res.scalar()
def get_indexes(self, connection: Connection,
table_name: str, schema: str = None, **kw) -> List[Dict[str, Any]]:
if not self.has_table(connection, table_name, schema):
raise exc.NoSuchTableError(f'schema={schema}, table={table_name}')
partitioned_columns = self._get_columns(connection, f'{table_name}$partitions', schema, **kw)
partition_index = dict(
name='partition',
column_names=[col['name'] for col in partitioned_columns],
unique=False
)
return [partition_index, ]
def get_sequence_names(self, connection: Connection, schema: str = None, **kw) -> List[str]:
"""Trino has no support for sequences. Returns an empty list."""
return []
def get_unique_constraints(self, connection: Connection,
table_name: str, schema: str = None, **kw) -> List[Dict[str, Any]]:
"""Trino has no support for unique constraints. Returns an empty list."""
return []
def get_check_constraints(self, connection: Connection,
table_name: str, schema: str = None, **kw) -> List[Dict[str, Any]]:
"""Trino has no support for check constraints. Returns an empty list."""
return []
def get_table_comment(self, connection: Connection,
table_name: str, schema: str = None, **kw) -> Dict[str, Any]:
properties_table = self._get_full_table(f'{table_name}$properties', schema)
query = f'SELECT "comment" FROM {properties_table}'
try:
res = connection.execute(sql.text(query))
return dict(text=res.scalar())
except error.TrinoQueryError as e:
if e.error_name in (
error.NOT_FOUND,
error.COLUMN_NOT_FOUND,
error.TABLE_NOT_FOUND,
error.NOT_SUPPORTED
):
return dict(text=None)
raise
def has_schema(self, connection: Connection, schema: str) -> bool:
query = dedent('''
SELECT "schema_name"
FROM "information_schema"."schemata"
WHERE "schema_name" = :schema
''').strip()
res = connection.execute(sql.text(query), schema=schema)
return res.first() is not None
def has_table(self, connection: Connection,
table_name: str, schema: str = None, **kw) -> bool:
schema = schema or self._get_default_schema_name(connection)
if schema is None:
return False
query = dedent('''
SELECT "table_name"
FROM "information_schema"."tables"
WHERE "table_schema" = :schema
AND "table_name" = :table
''').strip()
res = connection.execute(sql.text(query), schema=schema, table=table_name)
return res.first() is not None
def has_sequence(self, connection: Connection,
sequence_name: str, schema: str = None, **kw) -> bool:
"""Trino has no support for sequence. Returns False indicate that given sequence does not exists."""
return False
def _get_server_version_info(self, connection: Connection) -> Tuple[int, ...]:
query = 'SELECT version()'
res = connection.execute(sql.text(query)).scalar()
match = self.__version_pattern.match(res)
version = int(match.group(1)) if match else 0
return tuple([version])
def _get_default_schema_name(self, connection: Connection) -> Optional[str]:
dbapi_connection: trino_dbapi.Connection = connection.connection
return dbapi_connection.schema
def do_execute(self, cursor: Cursor, statement: str, parameters: Tuple[Any, ...],
context: DefaultExecutionContext = None):
cursor.execute(statement, parameters)
if context and context.should_autocommit:
# SQL statement only submitted to Trino server when cursor.fetch*() is called.
# For DDL (CREATE/ALTER/DROP) and DML (INSERT/UPDATE/DELETE) statement, call cursor.description
# to force submit statement immediately.
cursor.description # noqa
def do_rollback(self, dbapi_connection: trino_dbapi.Connection):
if dbapi_connection.transaction is not None:
dbapi_connection.rollback()
def set_isolation_level(self, dbapi_conn: trino_dbapi.Connection, level: str) -> None:
dbapi_conn._isolation_level = getattr(trino_dbapi.IsolationLevel, level)
def get_isolation_level(self, dbapi_conn: trino_dbapi.Connection) -> str:
level_names = ['AUTOCOMMIT',
'READ_UNCOMMITTED',
'READ_COMMITTED',
'REPEATABLE_READ',
'SERIALIZABLE']
return level_names[dbapi_conn.isolation_level]
def _get_full_table(self, table_name: str, schema: str = None, quote: bool = True) -> str:
table_part = self.identifier_preparer.quote_identifier(table_name) if quote else table_name
if schema:
schema_part = self.identifier_preparer.quote_identifier(schema) if quote else schema
return f'{schema_part}.{table_part}'
return table_part