-
Notifications
You must be signed in to change notification settings - Fork 684
Expand file tree
/
Copy pathdatasource.py
More file actions
543 lines (450 loc) · 22 KB
/
datasource.py
File metadata and controls
543 lines (450 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import datetime
import json
from typing import List, Optional
from fastapi import HTTPException
from sqlalchemy import and_, text
from sqlbot_xpack.permissions.models.ds_rules import DsRules
from sqlmodel import select
from apps.datasource.crud.permission import get_column_permission_fields, get_row_permission_filters, is_normal_user
from apps.datasource.embedding.table_embedding import calc_table_embedding
from apps.datasource.utils.utils import aes_decrypt
from apps.db.constant import DB
from apps.db.db import get_tables, get_fields, exec_sql, check_connection
from apps.db.engine import get_engine_config, get_engine_conn
from apps.system.schemas.auth import CacheName, CacheNamespace
from common.core.config import settings
from common.core.deps import SessionDep, CurrentUser, Trans
from common.utils.embedding_threads import run_save_table_embeddings, run_save_ds_embeddings
from common.utils.utils import SQLBotLogUtil, deepcopy_ignore_extra
from common.core.sqlbot_cache import cache, clear_cache
from .table import get_tables_by_ds_id
from ..crud.field import delete_field_by_ds_id, update_field
from ..crud.table import delete_table_by_ds_id, update_table
from ..models.datasource import CoreDatasource, CreateDatasource, CoreTable, CoreField, ColumnSchema, TableObj, \
DatasourceConf, TableAndFields
def get_datasource_list(session: SessionDep, user: CurrentUser, oid: Optional[int] = None) -> List[CoreDatasource]:
current_oid = user.oid if user.oid is not None else 1
if user.isAdmin and oid:
current_oid = oid
return session.exec(
select(CoreDatasource).where(CoreDatasource.oid == current_oid).order_by(CoreDatasource.name)).all()
def get_ds(session: SessionDep, id: int):
statement = select(CoreDatasource).where(CoreDatasource.id == id)
datasource = session.exec(statement).first()
return datasource
def check_status_by_id(session: SessionDep, trans: Trans, ds_id: int, is_raise: bool = False):
ds = session.get(CoreDatasource, ds_id)
if ds is None:
if is_raise:
raise HTTPException(status_code=500, detail=trans('i18n_ds_invalid'))
return False
return check_status(session, trans, ds, is_raise)
def check_status(session: SessionDep, trans: Trans, ds: CoreDatasource, is_raise: bool = False):
return check_connection(trans, ds, is_raise)
def check_name(session: SessionDep, trans: Trans, user: CurrentUser, ds: CoreDatasource):
if ds.id is not None:
ds_list = session.query(CoreDatasource).filter(
and_(CoreDatasource.name == ds.name, CoreDatasource.id != ds.id, CoreDatasource.oid == user.oid)).all()
if ds_list is not None and len(ds_list) > 0:
raise HTTPException(status_code=500, detail=trans('i18n_ds_name_exist'))
else:
ds_list = session.query(CoreDatasource).filter(
and_(CoreDatasource.name == ds.name, CoreDatasource.oid == user.oid)).all()
if ds_list is not None and len(ds_list) > 0:
raise HTTPException(status_code=500, detail=trans('i18n_ds_name_exist'))
@clear_cache(namespace=CacheNamespace.AUTH_INFO, cacheName=CacheName.DS_ID_LIST, keyExpression="user.oid")
async def create_ds(session: SessionDep, trans: Trans, user: CurrentUser, create_ds: CreateDatasource):
ds = CoreDatasource()
deepcopy_ignore_extra(create_ds, ds)
check_name(session, trans, user, ds)
ds.create_time = datetime.datetime.now()
# status = check_status(session, ds)
ds.create_by = user.id
ds.oid = user.oid if user.oid is not None else 1
ds.status = "Success"
ds.type_name = DB.get_db(ds.type).db_name
record = CoreDatasource(**ds.model_dump())
session.add(record)
session.flush()
session.refresh(record)
ds.id = record.id
session.commit()
# save tables and fields
sync_table(session, ds, create_ds.tables)
updateNum(session, ds)
return ds
def chooseTables(session: SessionDep, trans: Trans, id: int, tables: List[CoreTable]):
ds = session.query(CoreDatasource).filter(CoreDatasource.id == id).first()
check_status(session, trans, ds, True)
sync_table(session, ds, tables)
updateNum(session, ds)
def update_ds(session: SessionDep, trans: Trans, user: CurrentUser, ds: CoreDatasource):
ds.id = int(ds.id)
check_name(session, trans, user, ds)
# status = check_status(session, trans, ds)
ds.status = "Success"
record = session.exec(select(CoreDatasource).where(CoreDatasource.id == ds.id)).first()
update_data = ds.model_dump(exclude_unset=True)
for field, value in update_data.items():
setattr(record, field, value)
session.add(record)
session.commit()
run_save_ds_embeddings([ds.id])
return ds
def update_ds_recommended_config(session: SessionDep, datasource_id: int, recommended_config: int):
record = session.exec(select(CoreDatasource).where(CoreDatasource.id == datasource_id)).first()
record.recommended_config = recommended_config
session.add(record)
session.commit()
async def delete_ds(session: SessionDep, id: int):
term = session.exec(select(CoreDatasource).where(CoreDatasource.id == id)).first()
if term.type == "excel":
# drop all tables for current datasource
engine = get_engine_conn()
conf = DatasourceConf(**json.loads(aes_decrypt(term.configuration)))
with engine.connect() as conn:
for sheet in conf.sheets:
conn.execute(text(f'DROP TABLE IF EXISTS "{sheet["tableName"]}"'))
conn.commit()
session.delete(term)
session.commit()
delete_table_by_ds_id(session, id)
delete_field_by_ds_id(session, id)
if term:
await clear_ws_ds_cache(term.oid)
return {
"message": f"Datasource with ID {id} deleted successfully."
}
def getTables(session: SessionDep, id: int):
ds = session.exec(select(CoreDatasource).where(CoreDatasource.id == id)).first()
tables = get_tables(ds)
return tables
def getTablesByDs(session: SessionDep, ds: CoreDatasource):
# check_status(session, ds, True)
tables = get_tables(ds)
return tables
def getFields(session: SessionDep, id: int, table_name: str):
ds = session.exec(select(CoreDatasource).where(CoreDatasource.id == id)).first()
fields = get_fields(ds, table_name)
return fields
def getFieldsByDs(session: SessionDep, ds: CoreDatasource, table_name: str):
fields = get_fields(ds, table_name)
return fields
def execSql(session: SessionDep, id: int, sql: str):
ds = session.exec(select(CoreDatasource).where(CoreDatasource.id == id)).first()
return exec_sql(ds, sql, True)
def sync_single_fields(session: SessionDep, trans: Trans, id: int):
table = session.query(CoreTable).filter(CoreTable.id == id).first()
ds = session.query(CoreDatasource).filter(CoreDatasource.id == table.ds_id).first()
tables = getTablesByDs(session, ds)
t_name = []
for _t in tables:
t_name.append(_t.tableName)
if not table.table_name in t_name:
raise HTTPException(status_code=500, detail=trans('i18n_table_not_exist'))
# sync field
fields = getFieldsByDs(session, ds, table.table_name)
sync_fields(session, ds, table, fields)
# do table embedding
run_save_table_embeddings([table.id])
run_save_ds_embeddings([ds.id])
def sync_table(session: SessionDep, ds: CoreDatasource, tables: List[CoreTable]):
id_list = []
for item in tables:
statement = select(CoreTable).where(and_(CoreTable.ds_id == ds.id, CoreTable.table_name == item.table_name))
record = session.exec(statement).first()
# update exist table, only update table_comment
if record is not None:
item.id = record.id
id_list.append(record.id)
record.table_comment = item.table_comment
session.add(record)
session.commit()
else:
# save new table
table = CoreTable(ds_id=ds.id, checked=True, table_name=item.table_name, table_comment=item.table_comment,
custom_comment=item.table_comment)
session.add(table)
session.flush()
session.refresh(table)
item.id = table.id
id_list.append(table.id)
session.commit()
# sync field
fields = getFieldsByDs(session, ds, item.table_name)
sync_fields(session, ds, item, fields)
if len(id_list) > 0:
session.query(CoreTable).filter(and_(CoreTable.ds_id == ds.id, CoreTable.id.not_in(id_list))).delete(
synchronize_session=False)
session.query(CoreField).filter(and_(CoreField.ds_id == ds.id, CoreField.table_id.not_in(id_list))).delete(
synchronize_session=False)
session.commit()
else: # delete all tables and fields in this ds
session.query(CoreTable).filter(CoreTable.ds_id == ds.id).delete(synchronize_session=False)
session.query(CoreField).filter(CoreField.ds_id == ds.id).delete(synchronize_session=False)
session.commit()
# do table embedding
run_save_table_embeddings(id_list)
run_save_ds_embeddings([ds.id])
def sync_fields(session: SessionDep, ds: CoreDatasource, table: CoreTable, fields: List[ColumnSchema]):
id_list = []
for index, item in enumerate(fields):
statement = select(CoreField).where(
and_(CoreField.table_id == table.id, CoreField.field_name == item.fieldName))
record = session.exec(statement).first()
if record is not None:
item.id = record.id
id_list.append(record.id)
record.field_comment = item.fieldComment
record.field_index = index
record.field_type = item.fieldType
session.add(record)
session.commit()
else:
field = CoreField(ds_id=ds.id, table_id=table.id, checked=True, field_name=item.fieldName,
field_type=item.fieldType, field_comment=item.fieldComment,
custom_comment=item.fieldComment, field_index=index)
session.add(field)
session.flush()
session.refresh(field)
item.id = field.id
id_list.append(field.id)
session.commit()
if len(id_list) > 0:
session.query(CoreField).filter(and_(CoreField.table_id == table.id, CoreField.id.not_in(id_list))).delete(
synchronize_session=False)
session.commit()
def update_table_and_fields(session: SessionDep, data: TableObj):
update_table(session, data.table)
for field in data.fields:
update_field(session, field)
# do table embedding
run_save_table_embeddings([data.table.id])
run_save_ds_embeddings([data.table.ds_id])
def updateTable(session: SessionDep, table: CoreTable):
update_table(session, table)
# do table embedding
run_save_table_embeddings([table.id])
run_save_ds_embeddings([table.ds_id])
def updateField(session: SessionDep, field: CoreField):
update_field(session, field)
# do table embedding
run_save_table_embeddings([field.table_id])
run_save_ds_embeddings([field.ds_id])
def preview(session: SessionDep, current_user: CurrentUser, id: int, data: TableObj):
ds = session.query(CoreDatasource).filter(CoreDatasource.id == id).first()
# check_status(session, ds, True)
# ignore data's fields param, query fields from database
if not data.table.id:
return {"fields": [], "data": [], "sql": ''}
fields = session.query(CoreField).filter(CoreField.table_id == data.table.id).order_by(
CoreField.field_index.asc()).all()
if fields is None or len(fields) == 0:
return {"fields": [], "data": [], "sql": ''}
where = ''
f_list = [f for f in fields if f.checked]
if is_normal_user(current_user):
# column is checked, and, column permission for data.fields
contain_rules = session.query(DsRules).all()
f_list = get_column_permission_fields(session=session, current_user=current_user, table=data.table,
fields=f_list, contain_rules=contain_rules)
# row permission tree
where_str = ''
filter_mapping = get_row_permission_filters(session=session, current_user=current_user, ds=ds, tables=None,
single_table=data.table)
if filter_mapping:
mapping_dict = filter_mapping[0]
where_str = mapping_dict.get('filter')
where = (' where ' + where_str) if where_str is not None and where_str != '' else ''
fields = [f.field_name for f in f_list]
if fields is None or len(fields) == 0:
return {"fields": [], "data": [], "sql": ''}
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if ds.type != "excel" else get_engine_config()
sql: str = ""
if ds.type == "mysql" or ds.type == "doris" or ds.type == "starrocks":
sql = f"""SELECT `{"`, `".join(fields)}` FROM `{data.table.table_name}`
{where}
LIMIT 100"""
elif ds.type == "sqlServer":
sql = f"""SELECT TOP 100 [{"], [".join(fields)}] FROM [{conf.dbSchema}].[{data.table.table_name}]
{where}
"""
elif ds.type == "pg" or ds.type == "excel" or ds.type == "redshift" or ds.type == "kingbase":
sql = f"""SELECT "{'", "'.join(fields)}" FROM "{conf.dbSchema}"."{data.table.table_name}"
{where}
LIMIT 100"""
elif ds.type == "oracle":
# sql = f"""SELECT "{'", "'.join(fields)}" FROM "{conf.dbSchema}"."{data.table.table_name}"
# {where}
# ORDER BY "{fields[0]}"
# OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY"""
sql = f"""SELECT * FROM
(SELECT "{'", "'.join(fields)}" FROM "{conf.dbSchema}"."{data.table.table_name}"
{where}
ORDER BY "{fields[0]}")
WHERE ROWNUM <= 100
"""
elif ds.type == "ck":
sql = f"""SELECT "{'", "'.join(fields)}" FROM "{data.table.table_name}"
{where}
LIMIT 100"""
elif ds.type == "dm":
sql = f"""SELECT "{'", "'.join(fields)}" FROM "{conf.dbSchema}"."{data.table.table_name}"
{where}
LIMIT 100"""
elif ds.type == "es":
sql = f"""SELECT "{'", "'.join(fields)}" FROM "{data.table.table_name}"
{where}
LIMIT 100"""
return exec_sql(ds, sql, True)
def fieldEnum(session: SessionDep, id: int):
field = session.query(CoreField).filter(CoreField.id == id).first()
if field is None:
return []
table = session.query(CoreTable).filter(CoreTable.id == field.table_id).first()
if table is None:
return []
ds = session.query(CoreDatasource).filter(CoreDatasource.id == table.ds_id).first()
if ds is None:
return []
db = DB.get_db(ds.type)
sql = f"""SELECT DISTINCT {db.prefix}{field.field_name}{db.suffix} FROM {db.prefix}{table.table_name}{db.suffix}"""
res = exec_sql(ds, sql, True)
return [item.get(res.get('fields')[0]) for item in res.get('data')]
def updateNum(session: SessionDep, ds: CoreDatasource):
all_tables = get_tables(ds) if ds.type != 'excel' else json.loads(aes_decrypt(ds.configuration)).get('sheets')
selected_tables = get_tables_by_ds_id(session, ds.id)
num = f'{len(selected_tables)}/{len(all_tables)}'
record = session.exec(select(CoreDatasource).where(CoreDatasource.id == ds.id)).first()
update_data = ds.model_dump(exclude_unset=True)
for field, value in update_data.items():
setattr(record, field, value)
record.num = num
session.add(record)
session.commit()
def get_table_obj_by_ds(session: SessionDep, current_user: CurrentUser, ds: CoreDatasource) -> List[TableAndFields]:
_list: List = []
tables = session.query(CoreTable).filter(
and_(CoreTable.ds_id == ds.id, CoreTable.checked == True)
).all()
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if ds.type != "excel" else get_engine_config()
schema = conf.dbSchema if conf.dbSchema is not None and conf.dbSchema != "" else conf.database
# get all field
table_ids = [table.id for table in tables]
all_fields = session.query(CoreField).filter(
and_(CoreField.table_id.in_(table_ids), CoreField.checked == True)).all()
# build dict
fields_dict = {}
for field in all_fields:
if fields_dict.get(field.table_id):
fields_dict.get(field.table_id).append(field)
else:
fields_dict[field.table_id] = [field]
contain_rules = session.query(DsRules).all()
for table in tables:
# fields = session.query(CoreField).filter(and_(CoreField.table_id == table.id, CoreField.checked == True)).all()
fields = fields_dict.get(table.id)
# do column permissions, filter fields
fields = get_column_permission_fields(session=session, current_user=current_user, table=table, fields=fields,
contain_rules=contain_rules)
_list.append(TableAndFields(schema=schema, table=table, fields=fields))
return _list
def get_table_schema(session: SessionDep, current_user: CurrentUser, ds: CoreDatasource, question: str,
embedding: bool = True, table_list: list[str] = None) -> str:
schema_str = ""
table_objs = get_table_obj_by_ds(session=session, current_user=current_user, ds=ds)
if len(table_objs) == 0:
return schema_str
db_name = table_objs[0].schema
schema_str += f"【DB_ID】 {db_name}\n【Schema】\n"
tables = []
all_tables = [] # temp save all tables
for obj in table_objs:
# 如果传入了table_list,则只处理在列表中的表
if table_list is not None and obj.table.table_name not in table_list:
continue
schema_table = ''
schema_table += f"# Table: {db_name}.{obj.table.table_name}" if ds.type != "mysql" and ds.type != "es" else f"# Table: {obj.table.table_name}"
table_comment = ''
if obj.table.custom_comment:
table_comment = obj.table.custom_comment.strip()
if table_comment == '':
schema_table += '\n[\n'
else:
schema_table += f", {table_comment}\n[\n"
if obj.fields:
field_list = []
for field in obj.fields:
field_comment = ''
if field.custom_comment:
field_comment = field.custom_comment.strip()
if field_comment == '':
field_list.append(f"({field.field_name}:{field.field_type})")
else:
field_list.append(f"({field.field_name}:{field.field_type}, {field_comment})")
schema_table += ",\n".join(field_list)
schema_table += '\n]\n'
t_obj = {"id": obj.table.id, "schema_table": schema_table, "embedding": obj.table.embedding}
tables.append(t_obj)
all_tables.append(t_obj)
# 如果没有符合过滤条件的表,直接返回
if not tables:
return schema_str
# do table embedding
if embedding and tables and settings.TABLE_EMBEDDING_ENABLED:
tables = calc_table_embedding(tables, question)
# splice schema
if tables:
for s in tables:
schema_str += s.get('schema_table')
# field relation
if tables and ds.table_relation:
relations = list(filter(lambda x: x.get('shape') == 'edge', ds.table_relation))
if relations:
# Complete the missing table
# get tables in relation, remove irrelevant relation
embedding_table_ids = [s.get('id') for s in tables]
all_relations = list(
filter(lambda x: x.get('source').get('cell') in embedding_table_ids or x.get('target').get(
'cell') in embedding_table_ids, relations))
# get relation table ids, sub embedding table ids
relation_table_ids = []
for r in all_relations:
relation_table_ids.append(r.get('source').get('cell'))
relation_table_ids.append(r.get('target').get('cell'))
relation_table_ids = list(set(relation_table_ids))
# get table dict
table_records = session.query(CoreTable).filter(CoreTable.id.in_(list(map(int, relation_table_ids)))).all()
table_dict = {}
for ele in table_records:
table_dict[ele.id] = ele.table_name
# get lost table ids
lost_table_ids = list(set(relation_table_ids) - set(embedding_table_ids))
# get lost table schema and splice it
lost_tables = list(filter(lambda x: x.get('id') in lost_table_ids, all_tables))
if lost_tables:
for s in lost_tables:
schema_str += s.get('schema_table')
# get field dict
relation_field_ids = []
for relation in all_relations:
relation_field_ids.append(relation.get('source').get('port'))
relation_field_ids.append(relation.get('target').get('port'))
relation_field_ids = list(set(relation_field_ids))
field_records = session.query(CoreField).filter(CoreField.id.in_(list(map(int, relation_field_ids)))).all()
field_dict = {}
for ele in field_records:
field_dict[ele.id] = ele.field_name
if all_relations:
schema_str += '【Foreign keys】\n'
for ele in all_relations:
schema_str += f"{table_dict.get(int(ele.get('source').get('cell')))}.{field_dict.get(int(ele.get('source').get('port')))}={table_dict.get(int(ele.get('target').get('cell')))}.{field_dict.get(int(ele.get('target').get('port')))}\n"
return schema_str
@cache(namespace=CacheNamespace.AUTH_INFO, cacheName=CacheName.DS_ID_LIST, keyExpression="oid")
async def get_ws_ds(session, oid) -> list:
stmt = select(CoreDatasource.id).distinct().where(CoreDatasource.oid == oid)
db_list = session.exec(stmt).all()
return db_list
@clear_cache(namespace=CacheNamespace.AUTH_INFO, cacheName=CacheName.DS_ID_LIST, keyExpression="oid")
async def clear_ws_ds_cache(oid):
SQLBotLogUtil.info(f"ds cache for ws [{oid}] has been cleaned")