Merged
fix word root check #745
Fimon12138Add on 1 day ago
fix word root check #745
Merged
Fimon12138Add on 1 day ago
Total: 7 files changed+37-8
@@ -3,6 +3,7 @@
3- 当收到用户的输入时,你需要仔细分析输入中的关键词,主动做一些联想,使用 `write_todos` 进行任务计划。3- 当收到用户的输入时,你需要仔细分析输入中的关键词,主动做一些联想,使用 `write_todos` 进行任务计划。
4- 如果是检查特征是否已开发/是否已存在,读取 `check-feature-developed` skill 的 SKILL.md,并按其工作流执行。4- 如果是检查特征是否已开发/是否已存在,读取 `check-feature-developed` skill 的 SKILL.md,并按其工作流执行。
5- 如果是对特征命名,读取 `sql-rules` skill,按其中的 ddl_rules 执行,结果以合法的 HiveSQL DDL 保存到 `create_<tablename>.sql`,并修复 write_file 和 edit_file 的全部失败报错。5- 如果是对特征命名,读取 `sql-rules` skill,按其中的 ddl_rules 执行,结果以合法的 HiveSQL DDL 保存到 `create_<tablename>.sql`,并修复 write_file 和 edit_file 的全部失败报错。
6+- 用户单独要求校验表名或字段名词根时,读取 `sql-rules` skill 中的词根校验规则并调用 `validate_word_roots`;不要生成 DDL。
6- 如果是对已有表加列(新增字段),读取 `add-columns` skill 的 SKILL.md,并按其工作流执行。7- 如果是对已有表加列(新增字段),读取 `add-columns` skill 的 SKILL.md,并按其工作流执行。
7- 否则按特征开发任务处理,读取 `feature-engineering` skill 的 SKILL.md,并按其工作流执行。8- 否则按特征开发任务处理,读取 `feature-engineering` skill 的 SKILL.md,并按其工作流执行。
8- 打印输出你的计划内容。9- 打印输出你的计划内容。
@@ -141,6 +141,7 @@ description: |
141 141 
142- **字符串类型空值判断 → `bicoredata.IsEmpty(field)`**(保留侧 `!bicoredata.IsEmpty(field)`):仅针对字符串存储;禁止对 STRING 用 `IS NULL` / `IS NOT NULL` / `= ''` / `<> ''` 替代。**语义非字符串但按字符串存储**(时间/次数/金额等存 STRING)仍必须用 `bicoredata.IsEmpty`;**非字符串类型存储**(元数据为数值/时间戳等)不适用 `bicoredata.IsEmpty`,按该类型判空。142- **字符串类型空值判断 → `bicoredata.IsEmpty(field)`**(保留侧 `!bicoredata.IsEmpty(field)`):仅针对字符串存储;禁止对 STRING 用 `IS NULL` / `IS NOT NULL` / `= ''` / `<> ''` 替代。**语义非字符串但按字符串存储**(时间/次数/金额等存 STRING)仍必须用 `bicoredata.IsEmpty`;**非字符串类型存储**(元数据为数值/时间戳等)不适用 `bicoredata.IsEmpty`,按该类型判空。
143- **禁止方言不支持的类型**:DDL 列类型与 `CAST` 目标类型必须属于 Hive/Spark 白名单;禁止 `REAL`/`NUMERIC`/`TEXT` 等跨方言类型(过程浮点用 `DOUBLE`,落表用 `DECIMAL`)。143- **禁止方言不支持的类型**:DDL 列类型与 `CAST` 目标类型必须属于 Hive/Spark 白名单;禁止 `REAL`/`NUMERIC`/`TEXT` 等跨方言类型(过程浮点用 `DOUBLE`,落表用 `DECIMAL`)。
144+- **比较条件须符合列类型和业务语义**:STRING 列按数值语义比较时先转换为数值类型,并明确空值和非法值的处理方式(细则见 `sql-rules/references/dml_rules.md`)。
144- **小数连算禁止中间 CAST 截断**:先算完整表达式再对最终值 CAST;禁止 `A * CAST(B/C AS DECIMAL(...))` 这类对中间结果提前 CAST,应写 `CAST(A * B / C AS DECIMAL(...))`(细则见 `dml_rules.md`)。145- **小数连算禁止中间 CAST 截断**:先算完整表达式再对最终值 CAST;禁止 `A * CAST(B/C AS DECIMAL(...))` 这类对中间结果提前 CAST,应写 `CAST(A * B / C AS DECIMAL(...))`(细则见 `dml_rules.md`)。
145 146 
146### 分表写入(Split-Table)规则147### 分表写入(Split-Table)规则
@@ -9,7 +9,7 @@ description: |
9 9 
10## 文档内容10## 文档内容
11 11 
12-- `references/ddl_rules.md`:DDL 的建表语法规则。12+- `references/ddl_rules.md`:DDL 的建表语法规则和词根校验规则。
13- `references/dml_rules.md`:DML 的 INSERT 语法规则和通用规则(含空值判断、CAST 方言类型约束等)。13- `references/dml_rules.md`:DML 的 INSERT 语法规则和通用规则(含空值判断、CAST 方言类型约束等)。
14 14 
15## 读取原则15## 读取原则
@@ -61,4 +61,6 @@
61- 没有合适候选时,可调用 `validate_word_roots` 工具,对未命中词根提供中文描述或增大 `k` 以获取更多候选词根;仍无法确定时,向用户说明未解决的词根并请求确认,不要把未命中的词根描述为已通过校验。61- 没有合适候选时,可调用 `validate_word_roots` 工具,对未命中词根提供中文描述或增大 `k` 以获取更多候选词根;仍无法确定时,向用户说明未解决的词根并请求确认,不要把未命中的词根描述为已通过校验。
62- 解析失败时先修正 DDL;字典缺失、服务不可用或响应异常时,不要反复写入同一文件重试,保留候选名称继续任务,并在最终交付中说明未完成词根校验。62- 解析失败时先修正 DDL;字典缺失、服务不可用或响应异常时,不要反复写入同一文件重试,保留候选名称继续任务,并在最终交付中说明未完成词根校验。
63- 已有生产表和已有列不得自动重命名;修改用户明确指定的名称前须先与用户确认。63- 已有生产表和已有列不得自动重命名;修改用户明确指定的名称前须先与用户确认。
64-- 用户单独要求校验名称时,先将表名或列名按下划线拆分为词根(不含库名),再调用 `validate_word_roots`,无需生成 DDL 文件。该工具直接校验词根,不会自动拆分完整名称。64+- 用户单独要求校验名称时,先将表名或列名按下划线拆分为词根(不含库名),结合业务语义为各词根提供中文描述,再调用 `validate_word_roots`,无需生成 DDL 文件。该工具直接校验词根,不会自动拆分完整名称;传入含下划线的名称会报错。
65+- 每次调用最多传入 20 个词根项;超过 20 个时分批调用,并为每个词根保留对应的中文描述。
66+- 词根 CSV 是校验工具的内部数据;禁止用 `read_file`、`grep` 或 shell 等方式直接读取或搜索 CSV,候选词根只通过 `validate_word_roots` 获取。
@@ -201,7 +201,7 @@
201- 选用聚合函数 AVG / MIN / MAX / SUM 时应该结合字段的名称和描述,例如平均值字段使用AVG,总值字段使用SUM,最大值字段使用MAX,最小值字段使用MIN。当对于已采样的样本进行聚合时,要考虑原始表列语义和聚合函数的搭配使用,如已有列语义已经是最大最小值,且目标计算结果仍然是最大最小值,那么通常来说应该要对各样本的最大值取最大值,各样本的最小值取最小值。201- 选用聚合函数 AVG / MIN / MAX / SUM 时应该结合字段的名称和描述,例如平均值字段使用AVG,总值字段使用SUM,最大值字段使用MAX,最小值字段使用MIN。当对于已采样的样本进行聚合时,要考虑原始表列语义和聚合函数的搭配使用,如已有列语义已经是最大最小值,且目标计算结果仍然是最大最小值,那么通常来说应该要对各样本的最大值取最大值,各样本的最小值取最小值。
202- 过滤时间时优先使用分区时间字段,除非用户显式地要求使用其他字段过滤。202- 过滤时间时优先使用分区时间字段,除非用户显式地要求使用其他字段过滤。
203- 用户没有明确要求时不要使用ROUND,保留小数应该使用 `CAST (x as DECIMAL(n,d))`。特征为比率时默认使用 `CAST (x as DECIMAL(38,6))` 来保留6位小数,百分数也默认保留6位小数,除非用户有其他显式要求。用户没有明确要求时,除了比率外的特征不要用 CAST / ROUND 修改精度。203- 用户没有明确要求时不要使用ROUND,保留小数应该使用 `CAST (x as DECIMAL(n,d))`。特征为比率时默认使用 `CAST (x as DECIMAL(38,6))` 来保留6位小数,百分数也默认保留6位小数,除非用户有其他显式要求。用户没有明确要求时,除了比率外的特征不要用 CAST / ROUND 修改精度。
204-- 进行数值比较时,如果列是字符串类型,先转换成数值类型再比较。204+- **比较时按列类型和业务语义处理**:数值列使用数值字面量;STRING 列按数值语义比较时,先转换为数值类型。空串、非法值和 NULL 按明确的业务口径处理,不能用字符串比较代替数值比较。
205- 禁止 `INSERT INTO`、`INSERT OR REPLACE`、SQLite `PRIMARY KEY` 语法。205- 禁止 `INSERT INTO`、`INSERT OR REPLACE`、SQLite `PRIMARY KEY` 语法。
206- 输出:INSERT 草案。206- 输出:INSERT 草案。
207- JOIN 类型:明确写出 JOIN 的类型,如 LEFT JOIN , FULL OUTER JOIN 。事实表关联维表必须使用 LEFT JOIN,任何情况下(包括过滤空值或源表已聚合时)都不能使用 INNER JOIN,这是为了避免丢弃事实表中未匹配的记录。注意:该 LEFT JOIN 规则仅适用于事实表关联维表(维表是主键超集);多时间窗口/多分区范围的事实数据拼接不属于此场景,必须先按规则 22 判断各侧集合包含关系再选 JOIN 类型,禁止直接套用“事实表一律作左表”。207- JOIN 类型:明确写出 JOIN 的类型,如 LEFT JOIN , FULL OUTER JOIN 。事实表关联维表必须使用 LEFT JOIN,任何情况下(包括过滤空值或源表已聚合时)都不能使用 INNER JOIN,这是为了避免丢弃事实表中未匹配的记录。注意:该 LEFT JOIN 规则仅适用于事实表关联维表(维表是主键超集);多时间窗口/多分区范围的事实数据拼接不属于此场景,必须先按规则 22 判断各侧集合包含关系再选 JOIN 类型,禁止直接套用“事实表一律作左表”。
@@ -1,29 +1,47 @@
1"""Expose the original batch validator for manual root checks and candidate lookup."""1"""Expose the original batch validator for manual root checks and candidate lookup."""
2 2 
3from langchain.tools import ToolRuntime3from langchain.tools import ToolRuntime
4+from pydantic import BaseModel, Field
4from utils.word_root_validator import WordRootContext5from utils.word_root_validator import WordRootContext
5from utils.word_root_validator import validate_word_roots as _validate_word_roots6from utils.word_root_validator import validate_word_roots as _validate_word_roots
6 7 
7from dataagent import AgentContext8from dataagent import AgentContext
8 9 
9 10 
11+class WordRootItem(BaseModel):
12+ """One atomic word root and its optional business description."""
13+ 
14+ english_name: str = Field(
15+ min_length=1,
16+ description="Required single word root without underscores.",
17+ )
18+ chinese_description: str | None = Field(
19+ default=None,
20+ description="Optional Chinese business meaning of this word root.",
21+ )
22+ 
23+ 
10async def validate_word_roots(24async def validate_word_roots(
11- items: list[dict[str, str | None]],25+ items: list[WordRootItem],
12 runtime: ToolRuntime[AgentContext],26 runtime: ToolRuntime[AgentContext],
13 k: int = 5,27 k: int = 5,
14 llm_batch_size: int = 5,28 llm_batch_size: int = 5,
15) -> dict:29) -> dict:
16 """Validate up to 20 unique roots and return exact/regex matches or candidates.30 """Validate up to 20 unique roots and return exact/regex matches or candidates.
17 31 
18- Each item requires english_name and optionally chinese_description. Split32+ Each item is one atomic word root with an optional chinese_description.
19- full table/column names on underscores before calling; omit database names.33+ Underscores are rejected; split full table/column names and provide a
34+ description for each root before calling. Omit database names.
20 k limits candidates per root (1-20); llm_batch_size limits unresolved roots35 k limits candidates per root (1-20); llm_batch_size limits unresolved roots
21 per model request (1-20). Missing CSV returns validation_status=skipped,36 per model request (1-20). Missing CSV returns validation_status=skipped,
22 which is not evidence of a match. DDL file hooks run checks automatically.37 which is not evidence of a match. DDL file hooks run checks automatically.
23 """38 """
24 try:39 try:
40+ normalized_items = [
41+ item.model_dump() if isinstance(item, WordRootItem) else item for item in items
42+ ]
25 return await _validate_word_roots(43 return await _validate_word_roots(
26- items, k, llm_batch_size, _tool_context=WordRootContext(runtime.context)44+ normalized_items, k, llm_batch_size, _tool_context=WordRootContext(runtime.context)
27 )45 )
28 except Exception as exc:46 except Exception as exc:
29 return {"validation_status": "error", "error_msg": str(exc), "results": []}47 return {"validation_status": "error", "error_msg": str(exc), "results": []}
@@ -174,7 +174,9 @@ async def validate_word_roots(
174) -> dict[str, Any]:174) -> dict[str, Any]:
175 """Validate a batch of proposed word roots against the configured table.175 """Validate a batch of proposed word roots against the configured table.
176 176 
177- Each input item requires ``english_name`` and may contain ``chinese_description``.177+ Each input item requires one atomic ``english_name`` word root and may contain
178+ ``chinese_description``. Underscores are rejected: callers must split complete
179+ identifiers and provide a description for each root.
178 Exact English-name, abbreviation, and special-regex checks are local.180 Exact English-name, abbreviation, and special-regex checks are local.
179 Unresolved items are sent to the model in groups, with the full table181 Unresolved items are sent to the model in groups, with the full table
180 included once per request. Duplicate items are processed once and their182 included once per request. Duplicate items are processed once and their
@@ -216,6 +218,11 @@ async def validate_word_roots(
216 chinese_description = item.get("chinese_description")218 chinese_description = item.get("chinese_description")
217 if not isinstance(english_name, str) or not english_name.strip():219 if not isinstance(english_name, str) or not english_name.strip():
218 raise ValueError(f"items[{index}].english_name must be a non-empty string")220 raise ValueError(f"items[{index}].english_name must be a non-empty string")
221+ if "_" in english_name:
222+ raise ValueError(
223+ f"items[{index}].english_name must be one word root without underscores; "
224+ "split the identifier into roots and provide a description for each root"
225+ )
219 if chinese_description is not None and not isinstance(chinese_description, str):226 if chinese_description is not None and not isinstance(chinese_description, str):
220 raise ValueError(f"items[{index}].chinese_description must be a string when provided")227 raise ValueError(f"items[{index}].chinese_description must be a string when provided")
221 normalized_item = {228 normalized_item = {