Commit 79dfd6cd5629fb06d20b10097d61b2631a0d78e0

Authored by zichun
1 parent a3391c96

Add viw_kg_edge_ref: ERP entity-reference (FK) graph (L3)

Parse gdsconfigformslave.sRelation (comma-separated field&table&pk&display
hops) into FK edges: src_table.field -> tgt_table.pk, with the set of
display columns each FK surfaces aggregated per edge. 1597 distinct FK
edges / 368 source tables / 109 target tables (mostly ele*/sis* master
data). Provides the join dictionary for NL2SQL grounding.
Showing 1 changed file with 53 additions and 0 deletions
sql/viw_kg_edge_ref.sql 0 → 100644
  1 +-- ============================================================
  2 +-- 视图:viw_kg_edge_ref —— ERP 知识图谱·实体引用边 (ER/FK graph, L3, NL2SQL 接地)
  3 +-- 一条边 = 源表.外键字段 --引用--> 目标表.主键 (显示 显示字段)
  4 +-- 例:salsalesordermaster.sCustomerId --> elecustomer.sId (显示 sCustomerName)
  5 +--
  6 +-- 边的来源:字段控件上的 gdsconfigformslave.sRelation
  7 +-- 格式 = 逗号分隔的多跳查找,每跳 `字段&关联表&主键&显示字段`
  8 +-- 单跳:sDepartId&sisdepart&sId&sName
  9 +-- 多跳(链式带出):sEmployeeId&eleemployee&sId&sDepartId,sDepartId&sisdepart&sId&sName
  10 +-- —— 本视图取【第一跳】作为主外键边(源 = 该表单自身的数据表)。
  11 +--
  12 +-- 源表 = 字段所属主表单的 sTbName (gdsconfigformslave.sParentId → gdsconfigformmaster.sTbName)。
  13 +-- 目标表多为主数据/字典表:ele*(员工/产品/客户/物料 等)、sis*(各类分类字典)。
  14 +-- bTgtMaster=1 标记「引用到主数据/字典」,是 NL2SQL join 的关键信号。
  15 +-- iFormCount = 有多少张表单声明了这条外键 —— 边权重/显著度,可用于排序召回。
  16 +--
  17 +-- 边粒度 = 一条外键 (源表.字段→目标表.主键)。同一外键在不同控件可带出多个显示字段,
  18 +-- 聚合进 sDisplayFields(逗号去重列表),不拆成多条边。
  19 +-- 表名统一小写作稳定节点id(MySQL 表名大小写不敏感;与 sDataSource 对齐)。
  20 +-- 校验(本地库 xlyweberp_saas):1536 条去重外键边 / 368 源表 / 109 目标表。
  21 +-- ============================================================
  22 +CREATE OR REPLACE VIEW viw_kg_edge_ref AS
  23 +SELECT
  24 + LOWER(h.src_table) AS sSrcTable, -- 源表(小写节点id)
  25 + h.src_field AS sSrcField, -- 外键字段
  26 + LOWER(h.tgt_table) AS sTgtTable, -- 目标表(小写节点id)
  27 + h.tgt_key AS sTgtKey, -- 目标主键
  28 + GROUP_CONCAT(DISTINCT NULLIF(h.display_field,'') ORDER BY h.display_field SEPARATOR ',') AS sDisplayFields, -- 可带出的显示字段集
  29 + COUNT(DISTINCT h.sParentId) AS iFormCount, -- 声明此外键的表单数(边权重)
  30 + CAST(LOWER(LEFT(h.tgt_table,3)) IN ('ele','sis') AS UNSIGNED) AS bTgtMaster -- 1=引用主数据/字典表
  31 +FROM (
  32 + SELECT
  33 + m.sTbName AS src_table,
  34 + SUBSTRING_INDEX(hop1, '&', 1) AS src_field,
  35 + SUBSTRING_INDEX(SUBSTRING_INDEX(hop1, '&', 2), '&', -1) AS tgt_table,
  36 + SUBSTRING_INDEX(SUBSTRING_INDEX(hop1, '&', 3), '&', -1) AS tgt_key,
  37 + CASE WHEN (LENGTH(hop1) - LENGTH(REPLACE(hop1, '&', ''))) >= 3
  38 + THEN SUBSTRING_INDEX(SUBSTRING_INDEX(hop1, '&', 4), '&', -1)
  39 + ELSE '' END AS display_field,
  40 + s.sParentId
  41 + FROM (
  42 + SELECT s0.sParentId, s0.sRelation,
  43 + SUBSTRING_INDEX(s0.sRelation, ',', 1) AS hop1 -- 只取第一跳
  44 + FROM gdsconfigformslave s0
  45 + WHERE s0.sRelation LIKE '%&%&%' -- 至少 字段&表&主键
  46 + ) s
  47 + JOIN gdsconfigformmaster m ON s.sParentId = m.sId
  48 + WHERE IFNULL(m.sTbName, '') <> ''
  49 + AND (LENGTH(hop1) - LENGTH(REPLACE(hop1, '&', ''))) >= 2 -- 第一跳须 >=3 段
  50 + AND SUBSTRING_INDEX(hop1, '&', 1) <> ''
  51 + AND SUBSTRING_INDEX(SUBSTRING_INDEX(hop1, '&', 2), '&', -1) <> ''
  52 +) h
  53 +GROUP BY LOWER(h.src_table), h.src_field, LOWER(h.tgt_table), h.tgt_key;
... ...