feat:LLM 分析文件并生成结构化建议
This commit is contained in:
@@ -3,9 +3,18 @@
|
||||
列名、别名一律通过 quote_identifier 引用,请求里的字符串不会拼进 SQL 的其他位置
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from app.config import settings
|
||||
from app.core.exceptions import BizException
|
||||
from app.datafiles.query import quote_identifier
|
||||
from app.schemas.data_file import AggregateRequest, DataColumn, GroupBy, Metric
|
||||
from app.datafiles.query import quote_identifier, run_query
|
||||
from app.schemas.data_file import (
|
||||
AggregateRequest,
|
||||
AggregateResult,
|
||||
DataColumn,
|
||||
GroupBy,
|
||||
Metric,
|
||||
)
|
||||
|
||||
# 只能用于数值列的聚合;其余聚合对列类型没有要求
|
||||
NUMBER_ONLY = {"sum", "avg"}
|
||||
@@ -20,7 +29,28 @@ TEMPLATES = {
|
||||
TIME_TYPES = {"date", "datetime"}
|
||||
|
||||
|
||||
def build_aggregate_sql(
|
||||
def run_aggregate(
|
||||
path: Path, request: AggregateRequest, columns: list[DataColumn]
|
||||
) -> AggregateResult:
|
||||
"""对一张表的 Parquet 执行汇总;接口和上传后的分析共用
|
||||
|
||||
请求不合法时抛 422;执行失败(包括文件已被删除)抛 QueryError
|
||||
"""
|
||||
sql, names = _build_sql(request, columns)
|
||||
result = run_query(
|
||||
sql,
|
||||
{"t": path},
|
||||
timeout=settings.data_query_timeout,
|
||||
max_rows=request.limit,
|
||||
)
|
||||
return AggregateResult(
|
||||
columns=names,
|
||||
rows=[dict(zip(names, row)) for row in result.rows],
|
||||
truncated=result.truncated,
|
||||
)
|
||||
|
||||
|
||||
def _build_sql(
|
||||
request: AggregateRequest, columns: list[DataColumn]
|
||||
) -> tuple[str, list[str]]:
|
||||
"""返回 SQL(表名固定为 t)和结果列名;请求不合法时抛 422
|
||||
|
||||
Reference in New Issue
Block a user