L1S2 · 数据与检索S5 · 评测与观测Verified: 2026-11-17
Stage 2 · 语义指标层约束 Text-to-SQL 与只读沙箱校验模式
把极易出错的“让大模型裸写数百张宽表 JOIN”改造为“语义层检索指标定义 + Few-Shot SQL 骨架 + EXPLAIN 只读沙箱自愈 + 行级 RBAC 参数绑定”,把生产 BI 问答 SQL 可执行准确度从 ~60% 拉升至 >92%。
适用场景 (Best For)
包含复杂业务口径(如 ARR/NRR/活跃留存定义)、百张以上数仓表及多租户财务/运营自助 BI Copilot。
人机边界 (Human-AI Boundary)
核心财务报表与外部披露指标必须绑定经数据治理委员会签字的 Semantic Layer YAML 口径,大模型不得擅自推断分母过滤条件;每次展示结果附带可点击展开的 SQL 血缘审计视图。
常见反模式与避坑 (Anti-Patterns)
- Stuffing raw DDL of 200 tables into the prompt context without dynamic table pruning or semantic metric definitions
- Executing LLM-generated raw SQL directly against production transactional databases without read-only credentials, row-limit caps, or AST tenant WHERE-clause verification
延迟成本与评测门禁
Schema retrieval (<60ms) + SQL generation (~800ms) + sandbox dry-run (<250ms) · Retrieved DDL/Few-shot token footprint vs max 2 retry repair iterations on SQL syntax errors
- Execution Accuracy (EX) on Verified Business Slices: >= 92% result-set equivalence against analyst gold SQL (Compare normalized result tuples (ignoring column order aliases where equivalent) on domain golden sets)
- Schema-Linking Recall & Zero DDL Leakage: 100% required tables/join keys recalled; 0 DML/unscoped queries executed (Assert AST parser blocks non-SELECT statements and injects mandatory tenant_id predicate)
💰 商业 ROI 模型与证据等级
production_practice将业务团队等待数据分析师排期取数的 2–3 天 SLA 压缩至秒级自助查数,同时借助 Semantic Layer 单一真相源规避各部门自行写 SQL 口径不一致引发的决策返工。
🏗️ 端到端系统架构流水线
Step 1: Metric / Schema Hybrid Retrieval 与样本值索引
≤ 80msZero frontier tokens
基于问题混合检索 Top-K 相关表 DDL、指标 YAML 口径及高基数枚举值样例(如 exact region/status strings),避免 WHERE 字面量拼错。
Step 2: AST 安全解析、EXPLAIN 预检与有界自愈重试
≤ 1200msCapped at max 2 repair rounds
用 sqlglot 解析 AST 强制只读与租户隔离条件,先执行 EXPLAIN 与 COST 阈值校验;仅对列不存在等可执行报错触发最多 2 次诊断重写。
⚖️ 工程选型与架构权衡矩阵
Semantic Layer Metric-Spec Compilation + Ad-hoc Fallback
适用条件:高频营收/留存/财务标准指标占查询量 70% 以上的 BI 系统。
核心权衡:前期需建模指标语义层 YAML,但将核心 KPI 幻觉降至近乎 0。
Pure Free-Form Schema-RAG + SQL Self-Correction
适用条件:快速探索临时上传 CSV 或非标准化临时库场景。
核心权衡:零前期建模成本,但隐式 JOIN 与业务过滤口径仍需人工复核 SQL。
🚨 生产失效模式与可观测特征
- Filter literal mismatch ('USA' vs 'United States' vs 'us') causing empty result sets silently misread as zero revenue
- Cartesian product fan-out JOINs inflating SUM() metrics without grain-mismatch warnings
🎤 面试高频追问与架构答辩题
- Why is prompting an LLM with raw `CREATE TABLE` DDL insufficient for enterprise Text-to-SQL accuracy, and how do Semantic Layers + AST SQL guards solve both accuracy and multi-tenant security?
⚡
想检验本题的肌肉记忆?进入 3 阶闪卡主动回忆盲测
隐藏答案遮罩,自测:审题锚点 ➔ 数理公式机理 ➔ 工业标杆代码与连环追问。3 分钟攻破认知盲区!
上下游关联知识链路
站内交互工具与信源依据
- BIRD-Bench: Can LLM Already Serve as a Database Interface? (paper, 2026-11-17)
- dbt MetricFlow Semantic Layer Architecture (official_docs, 2026-11-17)