-
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
[Datasources] Add: Add query runner for Yandex ClickHouse #1409
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import json | ||
import logging | ||
from redash.query_runner import * | ||
from redash.utils import JSONEncoder | ||
logger = logging.getLogger(__name__) | ||
|
||
try: | ||
import requests | ||
enabled = True | ||
except ImportError as e: | ||
logger.info(str(e)) | ||
enabled = False | ||
|
||
|
||
class ClickHouse(BaseSQLQueryRunner): | ||
noop_query = "SELECT 1" | ||
|
||
@classmethod | ||
def configuration_schema(cls): | ||
return { | ||
"type": "object", | ||
"properties": { | ||
"user": { | ||
"type": "string", | ||
"default": "default" | ||
}, | ||
"password": { | ||
"type": "string" | ||
}, | ||
"host": { | ||
"type": "string", | ||
"default": "127.0.0.1" | ||
}, | ||
"port": { | ||
"type": "number", | ||
"default": 8123 | ||
}, | ||
"dbname": { | ||
"type": "string", | ||
"title": "Database Name" | ||
} | ||
}, | ||
"required": ["dbname"], | ||
"secret": ["password"] | ||
} | ||
|
||
@classmethod | ||
def type(cls): | ||
return "clickhouse" | ||
|
||
def __init__(self, configuration): | ||
super(ClickHouse, self).__init__(configuration) | ||
|
||
def _get_tables(self, schema): | ||
query = "SELECT database, table, name FROM system.columns WHERE database NOT IN ('system')" | ||
|
||
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['database'], row['table']) | ||
|
||
if table_name not in schema: | ||
schema[table_name] = {'name': table_name, 'columns': []} | ||
|
||
schema[table_name]['columns'].append(row['name']) | ||
|
||
return schema.values() | ||
|
||
def _send_query(self, data, stream=False): | ||
url = 'http://{host}:{port}'.format(host=self.configuration['host'], port=self.configuration['port']) | ||
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. http and not https? why not let the user specify the full url instead of host/port pair? |
||
r = requests.post(url, data=data, stream=stream, params={ | ||
'user': self.configuration['user'], 'password': self.configuration['password'], | ||
'database': self.configuration['dbname'] | ||
}) | ||
if r.status_code != 200: | ||
raise Exception(r.text) | ||
return r.json() | ||
|
||
@staticmethod | ||
def _define_column_type(column): | ||
c = column.lower() | ||
if 'int' in c: | ||
return TYPE_INTEGER | ||
elif 'float' in c: | ||
return TYPE_FLOAT | ||
elif 'datetime' == c: | ||
return TYPE_DATETIME | ||
elif 'date' == c: | ||
return TYPE_DATE | ||
else: | ||
return TYPE_STRING | ||
|
||
def _clickhouse_query(self, query): | ||
query += ' FORMAT JSON' | ||
result = self._send_query(query) | ||
columns = [{'name': r['name'], 'friendly_name': r['name'], | ||
'type': self._define_column_type(r['type'])} for r in result['meta']] | ||
return {'columns': columns, 'rows': result['data']} | ||
|
||
def run_query(self, query, user): | ||
logger.info("Clickhouse is about to execute query: %s", query) | ||
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. Let's change this to |
||
if query == "": | ||
json_data = None | ||
error = "Query is empty" | ||
return json_data, error | ||
try: | ||
q = self._clickhouse_query(query) | ||
data = json.dumps(q, cls=JSONEncoder) | ||
error = None | ||
except Exception as exc: | ||
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. If we just return the exception as the |
||
data = None | ||
error = str(exc) | ||
return data, error | ||
|
||
register(ClickHouse) |
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.
No need to wrap this in
try
/except
.requests
is a direct dependency of the project.