From 4f38d42182028b1c49cee6991a7590c57a3203fa Mon Sep 17 00:00:00 2001 From: Noriaki Katayama Date: Mon, 6 Jul 2015 18:22:23 +0900 Subject: [PATCH 1/3] add presto --- redash/query_runner/presto.py | 76 +++++++++++++++++++++++++++++++++++ redash/settings.py | 1 + 2 files changed, 77 insertions(+) create mode 100644 redash/query_runner/presto.py diff --git a/redash/query_runner/presto.py b/redash/query_runner/presto.py new file mode 100644 index 0000000000..65c6dcac05 --- /dev/null +++ b/redash/query_runner/presto.py @@ -0,0 +1,76 @@ +import json + +from redash.utils import JSONEncoder +from redash.query_runner import * + +import logging +logger = logging.getLogger(__name__) + +try: + from pyhive import presto + enabled = True + +except ImportError: + logger.warning("Missing dependencies. Please install PyHive.") + logger.warning("You can use pip: pip install pyhive") + enabled = False + +types_map = { + 'INTEGER': TYPE_INTEGER, + 'FLOAT': TYPE_FLOAT, + 'BOOLEAN': TYPE_BOOLEAN, + 'STRING': TYPE_STRING, + 'TIMESTAMP': TYPE_DATETIME, +} + +class Presto(BaseQueryRunner): + @classmethod + def configuration_schema(cls): + return { + 'type': 'object', + 'properties': { + 'host': { + 'type': 'string' + }, + 'port': { + 'type': 'number' + }, + 'schema': { + 'type': 'string' + } + }, + 'required': ['host'] + } + + @classmethod + def enabled(cls): + return enabled + + @classmethod + def annotate_query(cls): + return False + + @classmethod + def type(cls): + return "presto" + + def __init__(self, configuration_json): + super(Presto, self).__init__(configuration_json) + + def run_query(self, query): + cursor = presto.connect(host=self.configuration['host'], schema='jp').cursor() + + try: + cursor.execute(query) + for i, row in enumerate(cursor.fetchone()): + logger.debug(row) + + error = None + except Exception, ex: + json_data = None + error = ex.message + + return json_data, error + + +register(Presto) diff --git a/redash/settings.py b/redash/settings.py index 4cb0366d62..8611869831 100644 --- a/redash/settings.py +++ b/redash/settings.py @@ -87,6 +87,7 @@ def parse_boolean(str): 'redash.query_runner.script', 'redash.query_runner.url', 'redash.query_runner.influx_db', + 'redash.query_runner.presto', ]))) # Features: From 6338be3811823b73d9d556ccf5fe27ab50115a88 Mon Sep 17 00:00:00 2001 From: Noriaki Katayama Date: Wed, 8 Jul 2015 10:33:55 +0900 Subject: [PATCH 2/3] modified response --- redash/query_runner/presto.py | 41 ++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/redash/query_runner/presto.py b/redash/query_runner/presto.py index 65c6dcac05..30fe9f8507 100644 --- a/redash/query_runner/presto.py +++ b/redash/query_runner/presto.py @@ -15,12 +15,15 @@ logger.warning("You can use pip: pip install pyhive") enabled = False -types_map = { - 'INTEGER': TYPE_INTEGER, - 'FLOAT': TYPE_FLOAT, - 'BOOLEAN': TYPE_BOOLEAN, - 'STRING': TYPE_STRING, - 'TIMESTAMP': TYPE_DATETIME, +PRESTO_TYPES_MAPPING = { + "integer" : TYPE_INTEGER, + "long" : TYPE_INTEGER, + "float" : TYPE_FLOAT, + "double" : TYPE_FLOAT, + "boolean" : TYPE_BOOLEAN, + "string" : TYPE_STRING, + "varchar": TYPE_STRING, + "date" : TYPE_DATE, } class Presto(BaseQueryRunner): @@ -37,6 +40,12 @@ def configuration_schema(cls): }, 'schema': { 'type': 'string' + }, + 'catalog': { + 'type': 'string' + }, + 'username': { + 'type': 'string' } }, 'required': ['host'] @@ -58,13 +67,26 @@ def __init__(self, configuration_json): super(Presto, self).__init__(configuration_json) def run_query(self, query): - cursor = presto.connect(host=self.configuration['host'], schema='jp').cursor() + connection = presto.connect( + host=self.configuration.get('host', ''), + port=self.configuration.get('port', 8080), + username=self.configuration.get('username', 'redash'), + catalog=self.configuration.get('catalog', 'hive'), + schema=self.configuration.get('schema', 'default')) + + cursor = connection.cursor() try: cursor.execute(query) - for i, row in enumerate(cursor.fetchone()): - logger.debug(row) + columns_data = [(row[0], row[1]) for row in cursor.description] + + columns = [{'name': col[0], + 'friendly_name': col[0], + 'type': PRESTO_TYPES_MAPPING.get(col[1], None)} for col in columns_data] + rows = [dict(zip(([c[0] for c in columns_data]), r)) for i, r in enumerate(cursor.fetchall())] + data = {'columns': columns, 'rows': rows} + json_data = json.dumps(data, cls=JSONEncoder) error = None except Exception, ex: json_data = None @@ -72,5 +94,4 @@ def run_query(self, query): return json_data, error - register(Presto) From 93d6b01fbf9040f01c79e7ecc8eda72a492059c1 Mon Sep 17 00:00:00 2001 From: Noriaki Katayama Date: Tue, 14 Jul 2015 16:59:25 +0900 Subject: [PATCH 3/3] add bigint --- redash/query_runner/presto.py | 1 + 1 file changed, 1 insertion(+) diff --git a/redash/query_runner/presto.py b/redash/query_runner/presto.py index 30fe9f8507..f37fb254f6 100644 --- a/redash/query_runner/presto.py +++ b/redash/query_runner/presto.py @@ -18,6 +18,7 @@ PRESTO_TYPES_MAPPING = { "integer" : TYPE_INTEGER, "long" : TYPE_INTEGER, + "bigint" : TYPE_INTEGER, "float" : TYPE_FLOAT, "double" : TYPE_FLOAT, "boolean" : TYPE_BOOLEAN,