Skip to content

Commit

Permalink
Update SQLAlchemy dialect: now get_columns uses SHOW CREATE TABLE b…
Browse files Browse the repository at this point in the history
…ecause old method was broken.
  • Loading branch information
sdia-zz committed Oct 3, 2017
1 parent 0bcae75 commit da61f6c
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions pyathenajdbc/sqlalchemy_athena.py
Expand Up @@ -50,6 +50,7 @@ def visit_char_length_func(self, fn, **kw):
'BIGINT': BIGINT,
'DATE': DATE,
'TIMESTAMP': TIMESTAMP,
'STRING': STRINGTYPE
}


Expand Down Expand Up @@ -121,30 +122,40 @@ def get_columns(self, connection, table_name, schema=None, **kw):
# information_schema.columns fails when filtering with table_schema or table_name,
# if specifying a name that does not exist in table_schema or table_name.
schema = schema if schema else connection.connection.schema_name
query = """
SELECT
table_schema,
table_name,
column_name,
data_type,
is_nullable,
column_default,
ordinal_position,
comment
FROM information_schema.columns
"""
return [
{
'name': row.column_name,
'type': _TYPE_MAPPINGS.get(re.sub(r'^([A-Z]+)($|\(.+\)$)', r'\1',
row.data_type.upper()), NULLTYPE),
'nullable': True if row.is_nullable == 'YES' else False,
'default': row.column_default,
'ordinal_position': row.ordinal_position,
'comment': row.comment,
} for row in connection.execute(query).fetchall()
if row.table_schema == schema and row.table_name == table_name
]
query = 'SHOW CREATE TABLE {}.{}'.format(schema, table_name)

res = connection.execute(query).fetchall()
columns = []
ordinal = 0
for r in res:
row = r[0].upper().strip()
row_comment = None
if row.endswith(',') or row.endswith(')'):
row = row[:-1]

if 'COMMENT' in row:
row_split = row.split('COMMENT')
row_comment = row_split[-1]
row = row_split[0]

m = re.search(r'\`(.+)\`\s+(.+)\s*', row)
if not m:
continue

column_name = m.group(1).lower()
print(m.group(2))
column_type = _TYPE_MAPPINGS.get(m.group(2), NULLTYPE)
ordinal += 1
columns.append(dict(
name=column_name.lower(),
type=column_type,
ordinal_position=ordinal,
comment=row_comment,
nullable=None,
default=None))

return columns


def get_foreign_keys(self, connection, table_name, schema=None, **kw):
# Athena has no support for foreign keys.
Expand Down

0 comments on commit da61f6c

Please sign in to comment.