-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdialect.py
470 lines (372 loc) · 17 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# sqlalchemy_teradata/dialect.py
# Copyright (C) 2015-2016 by Teradata
# <see AUTHORS file>
#
# This module is part of sqlalchemy-teradata and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
from sqlalchemy.engine import default
from sqlalchemy import pool, String, Numeric
from sqlalchemy.sql import select, and_, or_
from sqlalchemy_teradata.compiler import TeradataCompiler, TeradataDDLCompiler, TeradataTypeCompiler
from sqlalchemy_teradata.base import TeradataIdentifierPreparer, TeradataExecutionContext
from sqlalchemy.sql.expression import text, table, column, asc
from sqlalchemy import Table, Column, Index
import sqlalchemy.types as sqltypes
import sqlalchemy_teradata.types as tdtypes
from itertools import groupby
# ischema names is used for reflecting columns (see get_columns in the dialect)
ischema_names = {
None: sqltypes.NullType,
'cf': tdtypes.CHAR,
'cv': tdtypes.VARCHAR,
'uf': sqltypes.NCHAR,
'uv': sqltypes.NVARCHAR,
'co': tdtypes.CLOB,
'n' : tdtypes.NUMERIC,
'd' : tdtypes.DECIMAL,
'i' : sqltypes.INTEGER,
'i1': tdtypes.BYTEINT,
'i2': sqltypes.SMALLINT,
'i8': sqltypes.BIGINT,
'f' : sqltypes.FLOAT,
'da': sqltypes.DATE,
'ts': tdtypes.TIMESTAMP,
'sz': tdtypes.TIMESTAMP, #Added timestamp with timezone
'at': tdtypes.TIME,
'tz': tdtypes.TIMESTAMP, #Added time with timezone
#Expreimental - Binary
'bf': sqltypes.BINARY,
'bv': sqltypes.VARBINARY,
'bo': sqltypes.BLOB
} #TODO: add the interval types and blob
stringtypes=[ t for t in ischema_names if issubclass(ischema_names[t],sqltypes.String)]
class TeradataDialect(default.DefaultDialect):
name = 'teradata'
driver = 'teradata'
default_paramstyle = 'qmark'
poolclass = pool.SingletonThreadPool
statement_compiler = TeradataCompiler
ddl_compiler = TeradataDDLCompiler
type_compiler = TeradataTypeCompiler
preparer = TeradataIdentifierPreparer
execution_ctx_cls = TeradataExecutionContext
supports_native_boolean = False
supports_native_decimal = True
supports_unicode_statements = True
supports_unicode_binds = True
postfetch_lastrowid = False
implicit_returning = False
preexecute_autoincrement_sequences = False
construct_arguments = [
(Table, {
"post_create": None,
"postfixes": None
}),
(Index, {
"order_by": None,
"loading": None
}),
(Column, {
"compress": None,
"identity": None
})
]
def __init__(self, **kwargs):
super(TeradataDialect, self).__init__(**kwargs)
def create_connect_args(self, url):
if url is not None:
params = super(TeradataDialect, self).create_connect_args(url)[1]
cargs = ("Teradata", params['host'], params.get('username', ''), params.get('password', ''))
cparams = {p:params[p] for p in params if p not in\
['host', 'username', 'password']}
return (cargs, cparams)
@classmethod
def dbapi(cls):
""" Hook to the dbapi2.0 implementation's module"""
from teradata import tdodbc
return tdodbc
def normalize_name(self, name, **kw):
if name is not None:
return name.strip().lower()
return name
def has_table(self, connection, table_name, schema=None):
if schema is None:
schema=self.default_schema_name
stmt = select([column('tablename')],
from_obj=[text('dbc.tablesvx')]).where(
and_(text('DatabaseName=:schema'),
text('TableName=:table_name')))
res = connection.execute(stmt, schema=schema, table_name=table_name).fetchone()
return res is not None
def _resolve_type(self, t, **kw):
"""
Resolve types for String, Numeric, Date/Time, etc. columns
"""
t = self.normalize_name(t)
if t in ischema_names:
#print(t,ischema_names[t])
t = ischema_names[t]
if issubclass(t, sqltypes.String):
return t(length=kw['length']/2 if kw['chartype']=='UNICODE' else kw['length'],\
charset=kw['chartype'])
elif issubclass(t, sqltypes.Numeric):
return t(precision=kw['prec'], scale=kw['scale'])
elif issubclass(t, sqltypes.Time) or issubclass(t, sqltypes.DateTime):
#Timezone
tz=kw['fmt'][-1]=='Z'
#Precision
prec = kw['fmt']
#For some timestamps and dates, there is no precision, or indicatd in scale
prec = prec[prec.index('(') + 1: prec.index(')')] if '(' in prec else 0
prec = kw['scale'] if prec=='F' else int(prec)
#prec = int(prec[prec.index('(') + 1: prec.index(')')]) if '(' in prec else 0
return t(precision=prec,timezone=tz)
elif issubclass(t, sqltypes.Interval):
return t(day_precision=kw['prec'],second_precision=kw['scale'])
else:
return t() # For types like Integer, ByteInt
return ischema_names[None]
def _get_column_info(self, row):
"""
Resolves the column information for get_columns given a row.
"""
chartype = {
0: None,
1: 'LATIN',
2: 'UNICODE',
3: 'KANJISJIS',
4: 'GRAPHIC'}
#Handle unspecified characterset and disregard chartypes specified for non-character types (e.g. binary, json)
typ = self._resolve_type(row['columntype'],\
length=int(row['columnlength'] or 0),\
chartype=chartype[row['chartype'] if row['chartype'] in stringtypes else 0],\
prec=int(row['decimaltotaldigits'] or 0),\
scale=int(row['decimalfractionaldigits'] or 0),\
fmt=row['columnformat'])
autoinc = row['idcoltype'] in ('GA', 'GD')
return {
'name': self.normalize_name(row['columnname']),
'type': typ,
'nullable': row['nullable'] == u'Y',
'default': row['defaultvalue'],
'attrs': {
'columnformat':row['columnformat']},
'autoincrement': autoinc
}
def get_columns(self, connection, table_name, schema=None, **kw):
helpView=False
if schema is None:
schema = self.default_schema_name
if int(self.server_version_info.split('.')[0])<16:
dbc_columninfo='dbc.ColumnsV'
#Check if the object us a view
stmt = select([column('tablekind')],\
from_obj=[text('dbc.tablesV')]).where(\
and_(text('DatabaseName=:schema'),\
text('TableName=:table_name'),\
text("tablekind='V'")))
res = connection.execute(stmt, schema=schema, table_name=table_name).rowcount
helpView = (res==1)
else:
dbc_columninfo='dbc.ColumnsQV'
stmt = select([column('columnname'), column('columntype'),\
column('columnlength'), column('chartype'),\
column('decimaltotaldigits'), column('decimalfractionaldigits'),\
column('columnformat'),\
column('nullable'), column('defaultvalue'), column('idcoltype')],\
from_obj=[text(dbc_columninfo)]).where(\
and_(text('DatabaseName=:schema'),\
text('TableName=:table_name')))
res = connection.execute(stmt, schema=schema, table_name=table_name).fetchall()
#If this is a view in pre-16 version, get types for individual columns
if helpView:
res=[self._get_column_help(connection, schema,table_name,r['columnname']) for r in res]
return [self._get_column_info(row) for row in res]
def _get_default_schema_name(self, connection):
return self.normalize_name(
connection.execute('select database').scalar())
def _get_column_help(self, connection, schema,table_name,column_name):
stmt='help column '+schema+'.'+table_name+'.'+column_name
res = connection.execute(stmt).fetchall()[0]
return {'columnname':res['Column Name'],
'columntype':res['Type'],
'columnlength':res['Max Length'],
'chartype':res['Char Type'],
'decimaltotaldigits':res['Decimal Total Digits'],
'decimalfractionaldigits':res['Decimal Fractional Digits'],
'columnformat':res['Format'],
'nullable':res['Nullable'],
'defaultvalue':None,
'idcoltype':res['IdCol Type']
}
def get_table_names(self, connection, schema=None, **kw):
if schema is None:
schema = self.default_schema_name
stmt = select([column('tablename')],
from_obj=[text('dbc.TablesVX')]).where(
and_(text('DatabaseName = :schema'),
or_(text('tablekind=\'T\''),
text('tablekind=\'O\''))))
res = connection.execute(stmt, schema=schema).fetchall()
return [self.normalize_name(name['tablename']) for name in res]
def get_schema_names(self, connection, **kw):
stmt = select([column('username')],
from_obj=[text('dbc.UsersV')],
order_by=[text('username')])
res = connection.execute(stmt).fetchall()
return [self.normalize_name(name['username']) for name in res]
def get_view_definition(self, connection, view_name, schema=None, **kw):
if schema is None:
schema = self.default_schema_name
res = connection.execute('show table {}.{}'.format(schema, view_name)).scalar()
return self.normalize_name(res)
def get_view_names(self, connection, schema=None, **kw):
if schema is None:
schema = self.default_schema_name
stmt = select([column('tablename')],
from_obj=[text('dbc.TablesVX')]).where(
and_(text('DatabaseName = :schema'),
text('tablekind=\'V\'')))
res = connection.execute(stmt, schema=schema).fetchall()
return [self.normalize_name(name['tablename']) for name in res]
def get_pk_constraint(self, connection, table_name, schema=None, **kw):
"""
Override
TODO: Check if we need PRIMARY Indices or PRIMARY KEY Indices
TODO: Check for border cases (No PK Indices)
"""
if schema is None:
schema = self.default_schema_name
stmt = select([column('ColumnName'), column('IndexName')],
from_obj=[text('dbc.Indices')]).where(
and_(text('DatabaseName = :schema'),
text('TableName=:table'),
text('IndexType=:indextype'))
).order_by(asc(column('IndexNumber')))
# K for Primary Key
res = connection.execute(stmt, schema=schema, table=table_name, indextype='K').fetchall()
index_columns = list()
index_name = None
for index_column in res:
index_columns.append(self.normalize_name(index_column['ColumnName']))
index_name = self.normalize_name(index_column['IndexName']) # There should be just one IndexName
return {
"constrained_columns": index_columns,
"name": index_name
}
def get_unique_constraints(self, connection, table_name, schema=None, **kw):
"""
Overrides base class method
"""
if schema is None:
schema = self.default_schema_name
stmt = select([column('ColumnName'), column('IndexName')], from_obj=[text('dbc.Indices')]) \
.where(and_(text('DatabaseName = :schema'),
text('TableName=:table'),
text('IndexType=:indextype'))) \
.order_by(asc(column('IndexName')))
# U for Unique
res = connection.execute(stmt, schema=schema, table=table_name, indextype='U').fetchall()
def grouper(fk_row):
return {
'name': self.normalize_name(fk_row['IndexName']),
}
unique_constraints = list()
for constraint_info, constraint_cols in groupby(res, grouper):
unique_constraint = {
'name': self.normalize_name(constraint_info['name']),
'column_names': list()
}
for constraint_col in constraint_cols:
unique_constraint['column_names'].append(self.normalize_name(constraint_col['ColumnName']))
unique_constraints.append(unique_constraint)
return unique_constraints
def get_foreign_keys(self, connection, table_name, schema=None, **kw):
"""
Overrides base class method
"""
if schema is None:
schema = self.default_schema_name
stmt = select([column('IndexID'), column('IndexName'), column('ChildKeyColumn'), column('ParentDB'),
column('ParentTable'), column('ParentKeyColumn')],
from_obj=[text('DBC.All_RI_ChildrenV')]) \
.where(and_(text('ChildTable = :table'),
text('ChildDB = :schema'))) \
.order_by(asc(column('IndexID')))
res = connection.execute(stmt, schema=schema, table=table_name).fetchall()
def grouper(fk_row):
return {
'name': fk_row.IndexName or fk_row.IndexID, #ID if IndexName is None
'schema': fk_row.ParentDB,
'table': fk_row.ParentTable
}
# TODO: Check if there's a better way
fk_dicts = list()
for constraint_info, constraint_cols in groupby(res, grouper):
fk_dict = {
'name': constraint_info['name'],
'constrained_columns': list(),
'referred_table': constraint_info['table'],
'referred_schema': constraint_info['schema'],
'referred_columns': list()
}
for constraint_col in constraint_cols:
fk_dict['constrained_columns'].append(self.normalize_name(constraint_col['ChildKeyColumn']))
fk_dict['referred_columns'].append(self.normalize_name(constraint_col['ParentKeyColumn']))
fk_dicts.append(fk_dict)
return fk_dicts
def get_indexes(self, connection, table_name, schema=None, **kw):
"""
Overrides base class method
"""
if schema is None:
schema = self.default_schema_name
stmt = select(["*"], from_obj=[text('dbc.Indices')]) \
.where(and_(text('DatabaseName = :schema'),
text('TableName=:table'))) \
.order_by(asc(column('IndexName')))
res = connection.execute(stmt, schema=schema, table=table_name).fetchall()
def grouper(fk_row):
return {
'name': fk_row.IndexName or fk_row.IndexNumber, # If IndexName is None TODO: Check what to do
'unique': True if fk_row.UniqueFlag == 'Y' else False
}
# TODO: Check if there's a better way
indices = list()
for index_info, index_cols in groupby(res, grouper):
index_dict = {
'name': index_info['name'],
'column_names': list(),
'unique': index_info['unique']
}
for index_col in index_cols:
index_dict['column_names'].append(self.normalize_name(index_col['ColumnName']))
indices.append(index_dict)
return indices
def get_transaction_mode(self, connection, **kw):
"""
Returns the transaction mode set for the current session.
T = TDBS
A = ANSI
"""
stmt = select([text('transaction_mode')],\
from_obj=[text('dbc.sessioninfov')]).\
where(text('sessionno=SESSION'))
res = connection.execute(stmt).scalar()
return res
def _get_server_version_info(self, connection, **kw):
"""
Returns the Teradata Database software version.
"""
stmt = select([text('InfoData')],\
from_obj=[text('dbc.dbcinfov')]).\
where(text('InfoKey=\'VERSION\''))
res = connection.execute(stmt).scalar()
return res
def conn_supports_autocommit(self, connection, **kw):
"""
Returns True if autocommit is used for this connection (underlying Teradata session)
else False
"""
return self.get_transaction_mode(connection) == 'T'
dialect = TeradataDialect