Merged
chore(archive): 归档 develop_oss@c8a258d3f(DuckDB 跨 schema 解析 + dwcli 错误分类) #57
LiuliuliuAdd on 25 days ago
chore(archive): 归档 develop_oss@c8a258d3f(DuckDB 跨 schema 解析 + dwcli 错误分类) #57
Merged
Merged fromqq_22408539/DataSwarm_3182MRintoopenJiuwen/DataSwarmdevelop
Total: 6 files changed+400-68
| @@ -97,11 +97,14 @@ def _dwcli(*args: str, timeout: int | None = None) -> dict: | |||
| 97 | time.sleep(1.0 * (attempt + 1)) | 97 | time.sleep(1.0 * (attempt + 1)) |
| 98 | continue | 98 | continue |
| 99 | break | 99 | break |
| 100 | - if payload.get("status") == "error" or payload.get("error_code") in ( | 100 | + # 只把工作空间类错误包成 WorkspaceContextError;其余错误必须走 _err_msg |
| 101 | + # 带出 detail——否则技能侧只能看到 "not_found" / "internal_error" 这类光秃秃 | ||
| 102 | + # 的错误名,hint 指向「查数据库日志」,真实原因全丢。 | ||
| 103 | + code = payload.get("error_code") or payload.get("error") | ||
| 104 | + if code in ( | ||
| 101 | "WORKSPACE_CONTEXT_REQUIRED", "WORKSPACE_CONTEXT_CONFLICT", | 105 | "WORKSPACE_CONTEXT_REQUIRED", "WORKSPACE_CONTEXT_CONFLICT", |
| 102 | "DB_REF_NOT_ALLOWED", "DUPLICATE_METADATA_DB_REF"): | 106 | "DB_REF_NOT_ALLOWED", "DUPLICATE_METADATA_DB_REF"): |
| 103 | - code = payload.get("error_code") or "WORKSPACE_CONTEXT_ERROR" | 107 | + raise WorkspaceContextError(_err_msg(payload), code) |
| 104 | - raise WorkspaceContextError(payload.get("error") or "dwcli 调用失败", code) | ||
| 105 | if payload.get("status") == "error": | 108 | if payload.get("status") == "error": |
| 106 | raise RuntimeError(_err_msg(payload)) | 109 | raise RuntimeError(_err_msg(payload)) |
| 107 | if payload.get("http_code", 0) >= 400: | 110 | if payload.get("http_code", 0) >= 400: |
| @@ -316,12 +319,18 @@ def tables(db_ref: str, role: str = "business") -> dict: | |||
| 316 | 319 | ||
| 317 | 320 | ||
| 318 | def schema(db_ref: str, table: str, role: str = "business") -> dict: | 321 | def schema(db_ref: str, table: str, role: str = "business") -> dict: |
| 319 | - """表结构。经 dwcli databases table。""" | 322 | + """表结构。经 dwcli databases table。 |
| 323 | + | ||
| 324 | + ``resolved_table`` 是服务端目录里的规范表名:带 schema 的目录(DuckDB | ||
| 325 | + 原生库 / SQL Server)接受裸表名后返回全限定名,调用方据此拼后续 SQL。 | ||
| 326 | + """ | ||
| 320 | spec = parse_db_ref(db_ref) | 327 | spec = parse_db_ref(db_ref) |
| 321 | db_name = spec.get("database") or "" | 328 | db_name = spec.get("database") or "" |
| 322 | payload = _dwcli("databases", "table", "--db", db_name, "--table", table, | 329 | payload = _dwcli("databases", "table", "--db", db_name, "--table", table, |
| 323 | "--db-ref", db_ref) | 330 | "--db-ref", db_ref) |
| 324 | - return {"table": table, "columns": payload.get("columns", payload.get("fields", [])), | 331 | + return {"table": table, |
| 332 | + "resolved_table": payload.get("name") or table, | ||
| 333 | + "columns": payload.get("columns", payload.get("fields", [])), | ||
| 325 | "db": {"type": spec.get("type"), "ref": db_ref}} | 334 | "db": {"type": spec.get("type"), "ref": db_ref}} |
| 326 | 335 | ||
| 327 | 336 | ||
| @@ -22,6 +22,8 @@ _TABLE_NOT_FOUND_RE = re.compile( | |||
| 22 | r"Catalog Error: Table with name .+ not found" | 22 | r"Catalog Error: Table with name .+ not found" |
| 23 | ) | 23 | ) |
| 24 | _COLUMN_NOT_FOUND_RE = re.compile(r"Catalog Error: Column with name .+ does not exist") | 24 | _COLUMN_NOT_FOUND_RE = re.compile(r"Catalog Error: Column with name .+ does not exist") |
| 25 | +# 裸表名在多个 schema 下同名:不是拼写错误,也不是服务故障——改写全限定名即可。 | ||
| 26 | +_AMBIGUOUS_TABLE_RE = re.compile(r"is ambiguous across schemas", re.IGNORECASE) | ||
| 25 | _SYNTAX_RE = re.compile( | 27 | _SYNTAX_RE = re.compile( |
| 26 | r"errCode\s*=\s*2|mismatched input|Parse error|Syntax error|syntax error" | 28 | r"errCode\s*=\s*2|mismatched input|Parse error|Syntax error|syntax error" |
| 27 | r"|\(1064,|\(1105,|ParserException|Binder Error|Catalog Error", | 29 | r"|\(1064,|\(1105,|ParserException|Binder Error|Catalog Error", |
| @@ -46,6 +48,8 @@ def classify_error(exc: Exception) -> tuple[str, str, bool]: | |||
| 46 | return "PERMISSION_DENIED", msg, False | 48 | return "PERMISSION_DENIED", msg, False |
| 47 | if _has_code(3024, 20101) or "Query timeout" in msg: | 49 | if _has_code(3024, 20101) or "Query timeout" in msg: |
| 48 | return "TIMEOUT", msg, True | 50 | return "TIMEOUT", msg, True |
| 51 | + if _AMBIGUOUS_TABLE_RE.search(msg): | ||
| 52 | + return "AMBIGUOUS_TABLE", msg, True | ||
| 49 | if _TABLE_NOT_FOUND_RE.search(msg): | 53 | if _TABLE_NOT_FOUND_RE.search(msg): |
| 50 | return "TABLE_NOT_FOUND", msg, False | 54 | return "TABLE_NOT_FOUND", msg, False |
| 51 | if _COLUMN_NOT_FOUND_RE.search(msg): | 55 | if _COLUMN_NOT_FOUND_RE.search(msg): |
| @@ -61,6 +65,7 @@ def classify_error(exc: Exception) -> tuple[str, str, bool]: | |||
| 61 | SUGGESTIONS: dict[str, str] = { | 65 | SUGGESTIONS: dict[str, str] = { |
| 62 | "PERMISSION_DENIED": "联系 DBA 申请该表的 select 权限", | 66 | "PERMISSION_DENIED": "联系 DBA 申请该表的 select 权限", |
| 63 | "TABLE_NOT_FOUND": "确认表名拼写或检查 information_schema.tables", | 67 | "TABLE_NOT_FOUND": "确认表名拼写或检查 information_schema.tables", |
| 68 | + "AMBIGUOUS_TABLE": '表名在多个 schema 下同名,改用全限定名(如 "schema"."table")', | ||
| 64 | "COLUMN_NOT_FOUND": "确认列名拼写,用 get_schema.py 重新拉取表结构", | 69 | "COLUMN_NOT_FOUND": "确认列名拼写,用 get_schema.py 重新拉取表结构", |
| 65 | "SYNTAX_ERROR": "检查 SQL 语法,参考 SQL 方言注意事项", | 70 | "SYNTAX_ERROR": "检查 SQL 语法,参考 SQL 方言注意事项", |
| 66 | "TIMEOUT": "添加 LIMIT 或收紧 WHERE 条件后重试", | 71 | "TIMEOUT": "添加 LIMIT 或收紧 WHERE 条件后重试", |
| @@ -20,7 +20,9 @@ def _schema_duckdb(table: str) -> dict: | |||
| 20 | emit_error("TABLE_NOT_FOUND", f"{table} not found or has no columns") | 20 | emit_error("TABLE_NOT_FOUND", f"{table} not found or has no columns") |
| 21 | return { | 21 | return { |
| 22 | "database": database_name(), | 22 | "database": database_name(), |
| 23 | - "table": table, | 23 | + # 原生库按 schema 暴露("schema"."table"):裸表名解析后回填规范名, |
| 24 | + # 下游写 SQL 直接用它,避免默认 search_path 只有 main 时查不到。 | ||
| 25 | + "table": result.get("resolved_table") or table, | ||
| 24 | "columns": [ | 26 | "columns": [ |
| 25 | {"name": c["name"], "type": c["type"], "comment": c.get("comment") or "", | 27 | {"name": c["name"], "type": c["type"], "comment": c.get("comment") or "", |
| 26 | "nullable": c.get("nullable", c.get("null", True)), "ordinal": i + 1} | 28 | "nullable": c.get("nullable", c.get("null", True)), "ordinal": i + 1} |
| @@ -31,6 +31,9 @@ def _sample_dim_values_duckdb(table: str, columns: list[str]) -> dict: | |||
| 31 | except Exception as exc: | 31 | except Exception as exc: |
| 32 | et, msg, _ = classify_error(exc) | 32 | et, msg, _ = classify_error(exc) |
| 33 | emit_error(et, msg) | 33 | emit_error(et, msg) |
| 34 | + # 裸表名解析后的规范名(原生库 "schema"."table"):采样 SQL 必须用它, | ||
| 35 | + # 否则 main 之外的 schema 在默认 search_path 下查不到。 | ||
| 36 | + table = result.get("resolved_table") or table | ||
| 34 | dim_cols = [ | 37 | dim_cols = [ |
| 35 | (c["name"], c["type"], c.get("comment") or "") | 38 | (c["name"], c["type"], c.get("comment") or "") |
| 36 | for c in result.get("columns", []) | 39 | for c in result.get("columns", []) |