-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
+123
−0
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from redash.query_runner import * | ||
from redash.utils import json_dumps, json_loads | ||
|
||
import logging | ||
logger = logging.getLogger(__name__) | ||
|
||
try: | ||
import phoenixdb | ||
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 (KeyboardInterrupt, InterruptException) as e: | ||
error = "Query cancelled by user." | ||
json_data = None | ||
except Exception as ex: | ||
json_data = None | ||
error = 'error.{}.{}'.format(query, ex) | ||
if not isinstance(error, basestring): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why error want be a |
||
error = unicode(error) | ||
finally: | ||
if connection: | ||
connection.close() | ||
|
||
return json_data, error | ||
|
||
register(Phoenix) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message is shown to the user and should be (to some degree) user friendly. Not sure this qualifies :)
Maybe returning just the exception message is enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error handling is sufficient. Users can identify errors with code and message. Please refer to https://python-phoenixdb.readthedocs.io/en/latest/api.html#phoenixdb.Error.