Skip to content

Commit 6c975b6

Browse files
committed
feat: support AWS Redshift datasource
1 parent 79f7630 commit 6c975b6

File tree

9 files changed

+84
-6
lines changed

9 files changed

+84
-6
lines changed

backend/apps/datasource/crud/datasource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def preview(session: SessionDep, current_user: CurrentUser, id: int, data: Table
275275
sql = f"""SELECT TOP 100 [{"], [".join(fields)}] FROM [{conf.dbSchema}].[{data.table.table_name}]
276276
{where}
277277
"""
278-
elif ds.type == "pg" or ds.type == "excel":
278+
elif ds.type == "pg" or ds.type == "excel" or ds.type == "redshift":
279279
sql = f"""SELECT "{'", "'.join(fields)}" FROM "{conf.dbSchema}"."{data.table.table_name}"
280280
{where}
281281
LIMIT 100"""

backend/apps/db/constant.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class DB(Enum):
2121
ck = ('ck', '"', '"', ConnectType.sqlalchemy)
2222
dm = ('dm', '"', '"', ConnectType.py_driver)
2323
doris = ('doris', '`', '`', ConnectType.py_driver)
24+
redshift = ('redshift', '"', '"', ConnectType.py_driver)
2425

2526
def __init__(self, type, prefix, suffix, connect_type: ConnectType):
2627
self.type = type

backend/apps/db/db.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
if platform.system() != "Darwin":
1010
import dmPython
1111
import pymysql
12+
import redshift_connector
1213
from sqlalchemy import create_engine, text, Engine
1314
from sqlalchemy.orm import sessionmaker
1415

@@ -139,6 +140,19 @@ def check_connection(trans: Trans, ds: CoreDatasource, is_raise: bool = False):
139140
if is_raise:
140141
raise HTTPException(status_code=500, detail=trans('i18n_ds_invalid') + f': {e.args}')
141142
return False
143+
elif ds.type == 'redshift':
144+
with redshift_connector.connect(host=conf.host, port=conf.port, database=conf.database, user=conf.username,
145+
password=conf.password,
146+
timeout=10) as conn, conn.cursor() as cursor:
147+
try:
148+
cursor.execute('select 1')
149+
SQLBotLogUtil.info("success")
150+
return True
151+
except Exception as e:
152+
SQLBotLogUtil.error(f"Datasource {ds.id} connection failed: {e}")
153+
if is_raise:
154+
raise HTTPException(status_code=500, detail=trans('i18n_ds_invalid') + f': {e.args}')
155+
return False
142156

143157

144158
def get_version(ds: CoreDatasource):
@@ -165,6 +179,8 @@ def get_version(ds: CoreDatasource):
165179
cursor.execute(sql)
166180
res = cursor.fetchall()
167181
return res[0][0]
182+
elif ds.type == 'redshift':
183+
return ''
168184
except Exception as e:
169185
print(e)
170186
return ''
@@ -194,6 +210,14 @@ def get_schema(ds: CoreDatasource):
194210
res = cursor.fetchall()
195211
res_list = [item[0] for item in res]
196212
return res_list
213+
elif ds.type == 'redshift':
214+
with redshift_connector.connect(host=conf.host, port=conf.port, database=conf.database, user=conf.username,
215+
password=conf.password,
216+
timeout=conf.timeout) as conn, conn.cursor() as cursor:
217+
cursor.execute(f"""SELECT nspname FROM pg_namespace""")
218+
res = cursor.fetchall()
219+
res_list = [item[0] for item in res]
220+
return res_list
197221

198222

199223
def get_tables(ds: CoreDatasource):
@@ -222,6 +246,14 @@ def get_tables(ds: CoreDatasource):
222246
res = cursor.fetchall()
223247
res_list = [TableSchema(*item) for item in res]
224248
return res_list
249+
elif ds.type == 'redshift':
250+
with redshift_connector.connect(host=conf.host, port=conf.port, database=conf.database, user=conf.username,
251+
password=conf.password,
252+
timeout=conf.timeout) as conn, conn.cursor() as cursor:
253+
cursor.execute(sql)
254+
res = cursor.fetchall()
255+
res_list = [TableSchema(*item) for item in res]
256+
return res_list
225257

226258

227259
def get_fields(ds: CoreDatasource, table_name: str = None):
@@ -250,6 +282,14 @@ def get_fields(ds: CoreDatasource, table_name: str = None):
250282
res = cursor.fetchall()
251283
res_list = [ColumnSchema(*item) for item in res]
252284
return res_list
285+
elif ds.type == 'redshift':
286+
with redshift_connector.connect(host=conf.host, port=conf.port, database=conf.database, user=conf.username,
287+
password=conf.password,
288+
timeout=conf.timeout) as conn, conn.cursor() as cursor:
289+
cursor.execute(sql)
290+
res = cursor.fetchall()
291+
res_list = [ColumnSchema(*item) for item in res]
292+
return res_list
253293

254294

255295
def exec_sql(ds: CoreDatasource | AssistantOutDsSchema, sql: str, origin_column=False):
@@ -311,3 +351,22 @@ def exec_sql(ds: CoreDatasource | AssistantOutDsSchema, sql: str, origin_column=
311351
"sql": bytes.decode(base64.b64encode(bytes(sql, 'utf-8')))}
312352
except Exception as ex:
313353
raise ex
354+
elif ds.type == 'redshift':
355+
with redshift_connector.connect(host=conf.host, port=conf.port, database=conf.database, user=conf.username,
356+
password=conf.password,
357+
timeout=conf.timeout) as conn, conn.cursor() as cursor:
358+
try:
359+
cursor.execute(sql)
360+
res = cursor.fetchall()
361+
columns = [field[0] for field in cursor.description] if origin_column else [field[0].lower() for
362+
field in
363+
cursor.description]
364+
result_list = [
365+
{str(columns[i]): float(value) if isinstance(value, Decimal) else value for i, value in
366+
enumerate(tuple_item)}
367+
for tuple_item in res
368+
]
369+
return {"fields": columns, "data": result_list,
370+
"sql": bytes.decode(base64.b64encode(bytes(sql, 'utf-8')))}
371+
except Exception as ex:
372+
raise ex

backend/apps/db/db_sql.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def get_version_sql(ds: CoreDatasource, conf: DatasourceConf):
2828
return f"""
2929
SELECT * FROM v$version
3030
"""
31+
elif ds.type == 'redshift':
32+
return ''
3133

3234

3335
def get_table_sql(ds: CoreDatasource, conf: DatasourceConf):
@@ -107,6 +109,17 @@ def get_table_sql(ds: CoreDatasource, conf: DatasourceConf):
107109
where owner='{conf.dbSchema}'
108110
AND (table_type = 'TABLE' or table_type = 'VIEW')
109111
"""
112+
elif ds.type == 'redshift':
113+
return f"""
114+
SELECT
115+
relname AS TableName,
116+
obj_description(relfilenode::regclass, 'pg_class') AS TableDescription
117+
FROM
118+
pg_class
119+
WHERE
120+
relkind in ('r','p', 'f')
121+
AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = '{conf.dbSchema}')
122+
"""
110123

111124

112125
def get_field_sql(ds: CoreDatasource, conf: DatasourceConf, table_name: str = None):
@@ -141,7 +154,7 @@ def get_field_sql(ds: CoreDatasource, conf: DatasourceConf, table_name: str = No
141154
"""
142155
sql2 = f" AND C.TABLE_NAME = '{table_name}'" if table_name is not None and table_name != "" else ""
143156
return sql1 + sql2
144-
elif ds.type == "pg" or ds.type == "excel":
157+
elif ds.type == "pg" or ds.type == "excel" or ds.type == "redshift":
145158
sql1 = f"""
146159
SELECT a.attname AS COLUMN_NAME,
147160
pg_catalog.format_type(a.atttypid, a.atttypmod) AS DATA_TYPE,

backend/apps/db/type.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ def db_type_relation() -> Dict:
1212
"oracle": "Oracle",
1313
"ck": "ClickHouse",
1414
"dm": "达梦",
15-
"doris": "Apache Doris"
15+
"doris": "Apache Doris",
16+
"redshift": "AWS Redshift"
1617
}

backend/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ dependencies = [
4949
"clickhouse-sqlalchemy>=0.3.2",
5050
"dicttoxml>=1.7.16",
5151
"dmpython>=2.5.22; platform_system != 'Darwin'",
52+
"redshift-connector>=2.1.8",
5253
]
5354

5455
[project.optional-dependencies]

backend/template.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ template:
3939
- SQL查询的字段若是函数字段,如 COUNT(),CAST() 等,必须加上别名
4040
- 计算占比,百分比类型字段,保留两位小数,以%结尾。
4141
- 生成SQL时,必须避免关键字冲突。
42-
- 如数据库引擎是 PostgreSQL、Oracle、ClickHouse、达梦(DM),则在schema、表名、字段名、别名外层加双引号;
42+
- 如数据库引擎是 PostgreSQL、Oracle、ClickHouse、达梦(DM)、AWS Redshift,则在schema、表名、字段名、别名外层加双引号;
4343
- 如数据库引擎是 MySQL、Doris,则在表名、字段名、别名外层加反引号;
4444
- 如数据库引擎是 Microsoft SQL Server,则在schema、表名、字段名、别名外层加方括号。
4545
- 以PostgreSQL为例,查询Schema为TEST表TABLE下所有数据,则生成的SQL为:
@@ -224,7 +224,7 @@ template:
224224
- 如果存在冗余的过滤条件则进行去重后再生成新SQL。
225225
- 给过滤条件中的字段前加上表别名(如果没有表别名则加表名),如:table.field。
226226
- 生成SQL时,必须避免关键字冲突:
227-
- 如数据库引擎是 PostgreSQL、Oracle、ClickHouse、达梦(DM),则在schema、表名、字段名、别名外层加双引号;
227+
- 如数据库引擎是 PostgreSQL、Oracle、ClickHouse、达梦(DM)、AWS Redshift,则在schema、表名、字段名、别名外层加双引号;
228228
- 如数据库引擎是 MySQL、Doris,则在表名、字段名、别名外层加反引号;
229229
- 如数据库引擎是 Microsoft SQL Server,则在schema、表名、字段名、别名外层加方括号。
230230
- 生成的SQL使用JSON格式返回:
486 Bytes
Loading

frontend/src/views/ds/js/ds-type.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import sqlServer from '@/assets/datasource/icon_SQL_Server.png'
66
import ck from '@/assets/datasource/icon_ck.png'
77
import dm from '@/assets/datasource/icon_dm.png'
88
import doris from '@/assets/datasource/icon_doris.png'
9+
import redshift from '@/assets/datasource/icon_redshift.png'
910
import { i18n } from '@/i18n'
1011

1112
const t = i18n.global.t
@@ -18,6 +19,7 @@ export const dsType = [
1819
{ label: 'ClickHouse', value: 'ck' },
1920
{ label: '达梦', value: 'dm' },
2021
{ label: 'Apache Doris', value: 'doris' },
22+
{ label: 'AWS Redshift', value: 'redshift' },
2123
]
2224

2325
export const dsTypeWithImg = [
@@ -29,6 +31,7 @@ export const dsTypeWithImg = [
2931
{ name: 'ClickHouse', type: 'ck', img: ck },
3032
{ name: '达梦', type: 'dm', img: dm },
3133
{ name: 'Apache Doris', type: 'doris', img: doris },
34+
{ name: 'AWS Redshift', type: 'redshift', img: redshift },
3235
]
3336

34-
export const haveSchema = ['sqlServer', 'pg', 'oracle', 'dm']
37+
export const haveSchema = ['sqlServer', 'pg', 'oracle', 'dm', 'redshift']

0 commit comments

Comments
 (0)