Skip to content

Commit

Permalink
Use where clause to limit columns returned during reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jul 12, 2021
1 parent 3354f68 commit 12a5298
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions sqlalchemy_redshift/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,9 @@ def _get_redshift_relation(self, connection, table_name,

def _get_redshift_columns(self, connection, table_name, schema=None, **kw):
info_cache = kw.get('info_cache')
all_columns = self._get_all_column_info(connection,
all_columns = self._get_table_column_info(connection,
table_name,
schema,
info_cache=info_cache)
key = RelationKey(table_name, schema, connection)
if key not in all_columns.keys():
Expand Down Expand Up @@ -745,7 +747,8 @@ def _get_all_relation_info(self, connection, **kw):
return relations

@reflection.cache
def _get_all_column_info(self, connection, **kw):
def _get_table_column_info(self, connection, table_name, schema=None, **kw):
schema_clause = "AND schema = :schema" if schema else ""
all_columns = defaultdict(list)
with connection.connect() as cc:
result = cc.execute("""
Expand Down Expand Up @@ -776,6 +779,8 @@ def _get_all_column_info(self, connection, **kw):
WHERE n.nspname !~ '^pg_'
AND att.attnum > 0
AND NOT att.attisdropped
AND table_name = :table_name
{schema}
UNION
SELECT
view_schema as "schema",
Expand All @@ -799,8 +804,10 @@ def _get_all_column_info(self, connection, **kw):
col_name name,
col_type varchar,
col_num int)
WHERE table_name = :table_name
{schema}
ORDER BY "schema", "table_name", "attnum";
""")
""".format(schema=schema_clause), {'table_name': table_name, 'schema': schema})
for col in result:
key = RelationKey(col.table_name, col.schema, connection)
all_columns[key].append(col)
Expand Down Expand Up @@ -950,4 +957,4 @@ def visit_delete_stmt(element, compiler, **kwargs):
return 'DELETE FROM {table}{using}{where}'.format(
table=delete_stmt_table,
using=usingclause,
where=whereclause)
where=whereclause)

0 comments on commit 12a5298

Please sign in to comment.