Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh Presto schema by table_name and skip hive views #3900

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions redash/query_runner/presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,36 @@ def type(cls):

def get_schema(self, get_stats=False):
schema = {}
query = """
SELECT table_schema, table_name, column_name
FROM information_schema.columns
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
"""

results, error = self.run_query(query, None)

results, error = self.run_query("SHOW SCHEMAS", None)
if error is not None:
raise Exception("Failed getting schema.")

results = json_loads(results)
for schema_row in json_loads(results)['rows']:

if schema_row['Schema'] in ['pg_catalog', 'information_schema']:
continue

query_tables = 'SHOW TABLES FROM "{}"'.format(schema_row['Schema'])
results, error = self.run_query(query_tables, None)
if error is not None:
raise Exception("Failed getting schema. Failed executing {}".format(query_tables))

for row in results['rows']:
table_name = '{}.{}'.format(row['table_schema'], row['table_name'])
for table_row in json_loads(results)['rows']:

if table_name not in schema:
table_name = '{}.{}'.format(schema_row['Schema'], table_row['Table'])
schema[table_name] = {'name': table_name, 'columns': []}

schema[table_name]['columns'].append(row['column_name'])
query_columns = 'SHOW COLUMNS FROM "{}"."{}"'.format(schema_row['Schema'], table_row['Table'])
results, error = self.run_query(query_columns, None)
if error is not None:
schema.pop(table_name)
logger.warning(
"Failed getting columns for table {}, so skipping it. (probably it's an hive view :)".format(
table_name))
else:
for column_row in json_loads(results)['rows']:
schema[table_name]['columns'].append(column_row['Column'])

return schema.values()

Expand Down