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

Add phoenix query runner. #3153

Merged
merged 4 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
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
Binary file added client/app/assets/images/db-logos/phoenix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
121 changes: 121 additions & 0 deletions redash/query_runner/phoenix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from redash.query_runner import *
from redash.utils import json_dumps, json_loads

import logging
logger = logging.getLogger(__name__)

try:
import phoenixdb
from phoenixdb.errors import *
enabled = True

except ImportError:
enabled = False

TYPES_MAPPING = {
'VARCHAR': TYPE_STRING,
'CHAR': TYPE_STRING,
'BINARY': TYPE_STRING,
'VARBINARY': TYPE_STRING,
'BOOLEAN': TYPE_BOOLEAN,
'TIME': TYPE_DATETIME,
'DATE': TYPE_DATETIME,
'TIMESTAMP': TYPE_DATETIME,
'UNSIGNED_TIME': TYPE_DATETIME,
'UNSIGNED_DATE': TYPE_DATETIME,
'UNSIGNED_TIMESTAMP': TYPE_DATETIME,
'INTEGER': TYPE_INTEGER,
'UNSIGNED_INT': TYPE_INTEGER,
'BIGINT': TYPE_INTEGER,
'UNSIGNED_LONG': TYPE_INTEGER,
'TINYINT': TYPE_INTEGER,
'UNSIGNED_TINYINT': TYPE_INTEGER,
'SMALLINT': TYPE_INTEGER,
'UNSIGNED_SMALLINT': TYPE_INTEGER,
'FLOAT': TYPE_FLOAT,
'UNSIGNED_FLOAT': TYPE_FLOAT,
'DOUBLE': TYPE_FLOAT,
'UNSIGNED_DOUBLE': TYPE_FLOAT,
'DECIMAL': TYPE_FLOAT
}

class Phoenix(BaseQueryRunner):
noop_query = 'select 1'

@classmethod
def configuration_schema(cls):
return {
'type': 'object',
'properties': {
'url': {
'type': 'string'
}
},
'required': ['url']
}

@classmethod
def enabled(cls):
return enabled

@classmethod
def type(cls):
return "phoenix"

def get_schema(self, get_stats=False):
schema = {}
query = """
SELECT TABLE_SCHEM, TABLE_NAME, COLUMN_NAME
FROM SYSTEM.CATALOG
WHERE TABLE_SCHEM IS NULL OR TABLE_SCHEM != 'SYSTEM' AND COLUMN_NAME IS NOT NULL
"""

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

if error is not None:
raise Exception("Failed getting schema.")

results = json_loads(results)

for row in results['rows']:
table_name = '{}.{}'.format(row['TABLE_SCHEM'], row['TABLE_NAME'])

if table_name not in schema:
schema[table_name] = {'name': table_name, 'columns': []}

schema[table_name]['columns'].append(row['COLUMN_NAME'])

return schema.values()

def run_query(self, query, user):
connection = phoenixdb.connect(
url=self.configuration.get('url', ''),
autocommit=True)

cursor = connection.cursor()

try:
cursor.execute(query)
column_tuples = [(i[0], TYPES_MAPPING.get(i[1], None)) for i in cursor.description]
columns = self.fetch_columns(column_tuples)
rows = [dict(zip(([c['name'] for c in columns]), r)) for i, r in enumerate(cursor.fetchall())]
data = {'columns': columns, 'rows': rows}
json_data = json_dumps(data)
error = None
cursor.close()
except Error as e:
json_data = None
error = 'code: {}, sql state:{}, message: {}'.format(e.code, e.sqlstate, e.message)
except (KeyboardInterrupt, InterruptException) as e:
error = "Query cancelled by user."
json_data = None
except Exception as ex:
json_data = None
error = unicode(ex)
finally:
if connection:
connection.close()

return json_data, error

register(Phoenix)
1 change: 1 addition & 0 deletions redash/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def all_settings():
'redash.query_runner.drill',
'redash.query_runner.uptycs',
'redash.query_runner.snowflake',
'redash.query_runner.phoenix'
]

enabled_query_runners = array_from_string(os.environ.get("REDASH_ENABLED_QUERY_RUNNERS", ",".join(default_query_runners)))
Expand Down
1 change: 1 addition & 0 deletions requirements_all_ds.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ ibm-db>=2.0.9
pydruid==0.4
requests_aws_sign==0.1.4
snowflake_connector_python==1.6.10
phoenixdb==0.7
# certifi is needed to support MongoDB and SSL:
certifi