Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit 7b059d0

Browse files
committed
Fix: remove redundant column objects when view_columns contains all those
duplicates by using UNION instead of UNION ALL. Add support for precision, scale, etc. args and kwargs to reflected column type objects. Fixes LocusEnergy/sqlalchemy-vertica-python#14.
1 parent 0b6d48e commit 7b059d0

1 file changed

Lines changed: 48 additions & 9 deletions

File tree

sqla_vertica_python/vertica_python.py

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def get_columns(self, connection, table_name, schema=None, **kw):
190190
FROM v_catalog.columns
191191
where table_name = '{table_name}'
192192
{schema_conditional}
193-
UNION ALL
193+
UNION
194194
SELECT
195195
column_name,
196196
data_type,
@@ -200,15 +200,54 @@ def get_columns(self, connection, table_name, schema=None, **kw):
200200
where table_name = '{table_name}'
201201
{schema_conditional}
202202
""".format(table_name=table_name, schema_conditional=schema_conditional)
203-
return [
204-
{
205-
'name': row.column_name,
206-
'type': self.ischema_names[row.data_type.upper().split('(')[0]],
207-
'nullable': row.is_nullable,
208-
'default': row.column_default,
209-
'primary_key': row.column_name in primary_key_columns
210-
} for row in connection.execute(column_select)
203+
colobjs = [
204+
self._get_column_info(
205+
row.column_name,
206+
row.data_type,
207+
row.is_nullable,
208+
row.column_default,
209+
(row.column_name in primary_key_columns)
210+
)
211+
for row in connection.execute(column_select)
211212
]
213+
return [ c for c in colobjs if c ]
214+
215+
def _get_column_info(self, name, data_type, is_nullable, default, is_primary_key):
216+
m = re.match(r'(\w+)(?:\((\d+)(?:,(\d+))?\))?', data_type)
217+
if not m:
218+
return {}
219+
typename = m.group(1).upper()
220+
typeobj = self.ischema_names[typename]
221+
typeargs = []
222+
typekwargs = {}
223+
if m.group(2):
224+
try:
225+
v = int(m.group(2))
226+
typeargs.append(v)
227+
except ValueError:
228+
pass
229+
if m.group(3):
230+
try:
231+
v = int(m.group(3))
232+
typeargs.append(v)
233+
except ValueError:
234+
pass
235+
236+
if 'TIMEZONE' in typename or 'TIME ZONE' in typename:
237+
typekwargs['timezone'] = True
238+
239+
if callable(typeobj):
240+
typeargs = tuple(typeargs)
241+
typeobj = typeobj(*typeargs, **typekwargs)
242+
243+
return {
244+
'name': name,
245+
'type': typeobj,
246+
'nullable': is_nullable,
247+
'default': default,
248+
'primary_key': is_primary_key
249+
}
250+
212251

213252
@reflection.cache
214253
def get_unique_constraints(self, connection, table_name, schema=None, **kw):

0 commit comments

Comments
 (0)