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

[Datasources] Add: Add query runner for Yandex ClickHouse #1409

Merged
merged 5 commits into from
Nov 24, 2016
Merged
Changes from 1 commit
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
30 changes: 10 additions & 20 deletions redash/query_runner/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@
import logging
from redash.query_runner import *
from redash.utils import JSONEncoder
import requests
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"
Expand All @@ -20,21 +14,17 @@ def configuration_schema(cls):
return {
"type": "object",
"properties": {
"url": {
"type": "string",
"default": "http://127.0.0.1:8123"
},
"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"
Expand Down Expand Up @@ -72,8 +62,7 @@ def _get_tables(self, schema):
return schema.values()

def _send_query(self, data, stream=False):
url = 'http://{host}:{port}'.format(host=self.configuration['host'], port=self.configuration['port'])
r = requests.post(url, data=data, stream=stream, params={
r = requests.post(self.configuration['url'], data=data, stream=stream, params={
'user': self.configuration['user'], 'password': self.configuration['password'],
'database': self.configuration['dbname']
})
Expand Down Expand Up @@ -103,7 +92,7 @@ def _clickhouse_query(self, query):
return {'columns': columns, 'rows': result['data']}

def run_query(self, query, user):
logger.info("Clickhouse is about to execute query: %s", query)
logger.debug("Clickhouse is about to execute query: %s", query)
if query == "":
json_data = None
error = "Query is empty"
Expand All @@ -112,9 +101,10 @@ def run_query(self, query, user):
q = self._clickhouse_query(query)
data = json.dumps(q, cls=JSONEncoder)
error = None
except Exception as exc:
except Exception as e:
data = None
error = str(exc)
logging.exception(e)
error = str(e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, it's possible the exception might have some unicode characters, no? (like fragments of the query) So maybe better to use unicode here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course. changed.

return data, error

register(ClickHouse)