Skip to content

Commit 8487876

Browse files
authored
feat: New support databend for redash (#5902)
* feat: New support databend for redash * fix
1 parent c08ef9b commit 8487876

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Redash supports more than 35 SQL and NoSQL [data sources](https://redash.io/help
5252
- Exasol
5353
- Microsoft Excel
5454
- Firebolt
55+
- Databend
5556
- Google Analytics
5657
- Google BigQuery
5758
- Google Spreadsheets

redash/query_runner/databend.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
try:
2+
from databend_sqlalchemy import connector
3+
import re
4+
5+
enabled = True
6+
except ImportError:
7+
enabled = False
8+
9+
from redash.query_runner import BaseQueryRunner, register
10+
from redash.query_runner import TYPE_STRING, TYPE_INTEGER, TYPE_BOOLEAN, TYPE_FLOAT, TYPE_DATETIME, TYPE_DATE
11+
from redash.utils import json_dumps, json_loads
12+
13+
14+
class Databend(BaseQueryRunner):
15+
noop_query = "SELECT 1"
16+
17+
@classmethod
18+
def configuration_schema(cls):
19+
return {
20+
"type": "object",
21+
"properties": {
22+
"host": {"type": "string", "default": "localhost"},
23+
"port": {"type": "int", "default": 8000},
24+
"username": {"type": "string"},
25+
"password": {"type": "string", "default": ""},
26+
"database": {"type": "string"},
27+
"secure": {"type": "string", "default": False},
28+
},
29+
"order": ["username", "password", "host", "port", "database"],
30+
"required": ["username", "database"],
31+
"secret": ["password"],
32+
}
33+
34+
@classmethod
35+
def name(cls):
36+
return "Databend"
37+
38+
@classmethod
39+
def type(cls):
40+
return "Databend"
41+
42+
@classmethod
43+
def enabled(cls):
44+
return enabled
45+
46+
@staticmethod
47+
def _define_column_type(column_type):
48+
c = column_type.lower()
49+
f = re.search(r"^nullable\((.*)\)$", c)
50+
if f is not None:
51+
c = f.group(1)
52+
if c.startswith("int") or c.startswith("uint"):
53+
return TYPE_INTEGER
54+
elif c.startswith("float"):
55+
return TYPE_FLOAT
56+
elif c == "datetime":
57+
return TYPE_DATETIME
58+
elif c == "date":
59+
return TYPE_DATE
60+
else:
61+
return TYPE_STRING
62+
63+
def run_query(self, query, user):
64+
host = (self.configuration.get("host") or "localhost"),
65+
port = (self.configuration.get("port") or 8000),
66+
username = (self.configuration.get("username") or None),
67+
password = (self.configuration.get("password") or None),
68+
database = (self.configuration.get("database") or None),
69+
secure = (self.configuration.get("secure") or False),
70+
connection = connector.connect(f"databend://{username}:{password}@{host}:{port}/{database}?secure={secure}")
71+
cursor = connection.cursor()
72+
73+
try:
74+
cursor.execute(query)
75+
columns = self.fetch_columns(
76+
[(i[0], self._define_column_type(i[1])) for i in cursor.description]
77+
)
78+
rows = [
79+
dict(zip((column["name"] for column in columns), row)) for row in cursor
80+
]
81+
82+
data = {"columns": columns, "rows": rows}
83+
error = None
84+
json_data = json_dumps(data)
85+
finally:
86+
connection.close()
87+
88+
return json_data, error
89+
90+
def _get_tables(self):
91+
query = """
92+
SELECT TABLE_SCHEMA,
93+
TABLE_NAME,
94+
COLUMN_NAME
95+
FROM INFORMATION_SCHEMA.COLUMNS
96+
WHERE TABLE_SCHEMA NOT IN ('information_schema', 'system')
97+
"""
98+
99+
results, error = self.run_query(query, None)
100+
101+
if error is not None:
102+
self._handle_run_query_error(error)
103+
104+
schema = {}
105+
results = json_loads(results)
106+
107+
for row in results["rows"]:
108+
table_name = "{}.{}".format(row["table_schema"], row["table_name"])
109+
110+
if table_name not in schema:
111+
schema[table_name] = {"name": table_name, "columns": []}
112+
113+
schema[table_name]["columns"].append(row["column_name"])
114+
115+
return list(schema.values())
116+
117+
118+
register(Databend)

redash/settings/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ def email_server_is_configured():
390390
"redash.query_runner.excel",
391391
"redash.query_runner.csv",
392392
"redash.query_runner.firebolt",
393+
"redash.query_runner.databend",
393394
"redash.query_runner.nz",
394395
"redash.query_runner.arango"
395396
]

requirements_all_ds.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ cmem-cmempy==21.2.3
4141
xlrd==2.0.1
4242
openpyxl==3.0.7
4343
firebolt-sdk
44+
databend-sqlalchemy
4445
pandas==1.3.4
4546
nzpy>=1.15
4647
nzalchemy

0 commit comments

Comments
 (0)