From 555e9580f78822ce5a9f3ae224afe3c89ac199fd Mon Sep 17 00:00:00 2001 From: Alison Date: Fri, 28 Jul 2017 06:36:19 -0500 Subject: [PATCH] Add column type info to query runners (re #152, #23) --- redash/query_runner/athena.py | 4 ++-- redash/query_runner/mysql.py | 5 +++-- redash/query_runner/pg.py | 5 ++++- redash/query_runner/presto.py | 4 ++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/redash/query_runner/athena.py b/redash/query_runner/athena.py index eb2f619ad3..b549d8e97c 100644 --- a/redash/query_runner/athena.py +++ b/redash/query_runner/athena.py @@ -147,7 +147,7 @@ def get_schema(self, get_stats=False): schema = {} query = """ - SELECT table_schema, table_name, column_name + SELECT table_schema, table_name, column_name, data_type as column_type FROM information_schema.columns WHERE table_schema NOT IN ('information_schema') """ @@ -161,7 +161,7 @@ def get_schema(self, get_stats=False): table_name = '{0}.{1}'.format(row['table_schema'], row['table_name']) if table_name not in schema: schema[table_name] = {'name': table_name, 'columns': []} - schema[table_name]['columns'].append(row['column_name']) + schema[table_name]['columns'].append(row['column_name'] + ' (' + row['column_type'] + ')') return schema.values() diff --git a/redash/query_runner/mysql.py b/redash/query_runner/mysql.py index 9815100b4e..3f5f9d310b 100644 --- a/redash/query_runner/mysql.py +++ b/redash/query_runner/mysql.py @@ -106,7 +106,8 @@ def _get_tables(self, schema): query = """ SELECT col.table_schema as table_schema, col.table_name as table_name, - col.column_name as column_name + col.column_name as column_name, + col.column_type as column_type FROM `information_schema`.`columns` col WHERE col.table_schema NOT IN ('information_schema', 'performance_schema', 'mysql', 'sys'); """ @@ -127,7 +128,7 @@ def _get_tables(self, schema): if table_name not in schema: schema[table_name] = {'name': table_name, 'columns': []} - schema[table_name]['columns'].append(row['column_name']) + schema[table_name]['columns'].append(row['column_name'] + ' (' + row['column_type'] + ')') return schema.values() diff --git a/redash/query_runner/pg.py b/redash/query_runner/pg.py index a281bb41bc..32c4f8b309 100644 --- a/redash/query_runner/pg.py +++ b/redash/query_runner/pg.py @@ -108,7 +108,7 @@ def _get_definitions(self, schema, query): if table_name not in schema: schema[table_name] = {'name': table_name, 'columns': []} - schema[table_name]['columns'].append(row['column_name']) + schema[table_name]['columns'].append(row['column_name'] + ' (' + row['column_type'] + ')') def _get_tables(self, schema): ''' @@ -128,6 +128,7 @@ def _get_tables(self, schema): query = """ SELECT s.nspname as table_schema, c.relname as table_name, + t.typname as column_type, a.attname as column_name FROM pg_class c JOIN pg_namespace s @@ -137,6 +138,8 @@ def _get_tables(self, schema): ON a.attrelid = c.oid AND a.attnum > 0 AND NOT a.attisdropped + JOIN pg_type t + ON c.reltype = t.oid WHERE c.relkind IN ('r', 'v', 'm', 'f', 'p') """ diff --git a/redash/query_runner/presto.py b/redash/query_runner/presto.py index a84e9f0106..5d84b1e803 100644 --- a/redash/query_runner/presto.py +++ b/redash/query_runner/presto.py @@ -78,7 +78,7 @@ def type(cls): def get_schema(self, get_stats=False): schema = {} query = """ - SELECT table_schema, table_name, column_name + SELECT table_schema, table_name, column_name, data_type as column_type FROM information_schema.columns WHERE table_schema NOT IN ('pg_catalog', 'information_schema') """ @@ -96,7 +96,7 @@ def get_schema(self, get_stats=False): if table_name not in schema: schema[table_name] = {'name': table_name, 'columns': []} - schema[table_name]['columns'].append(row['column_name']) + schema[table_name]['columns'].append(row['column_name'] + ' (' + row['column_type'] + ')') return schema.values()