12-04 更新 实时
首页/ 技术栈/ 正文

超越语义模型:智能体的可组合指标

技术栈 来源:Databricks Blog 2026-12-04 05:17 3 阅读 机器翻译
超越语义模型:智能体的可组合指标

这些能力结合在一起,使智能体能够使用集中治理的指标定义来回答新的业务问题,而不再需要单体式或相互重叠的语义模型。这两项功能目前均处于 Beta 阶段。

传统语义模型将回答一组业务问题所需的表、关系和指标汇集在一起。这种方法在 BI 场景中一直效果良好,因为构建模型时通常已明确分析范围。但在 AI 智能体的场景下,分析范围越来越多地是在查询时确定的。用户可以提出横跨销售、支持工单和产品使用情况的问题,即便相应的指标是由不同团队独立定义和治理的。若要在建模阶段就预判指标与维度的每一种组合,要么需要构建臃肿难用的语义模型,要么会导致冗余模型大量滋生。

在 Databricks,我们对语义建模采取了不同的思路。今年 4 月,我们发布了 Unity Catalog metric views,这是一种以 SQL 为先的方式来定义和治理业务指标。自那以后,许多客户已将 metric views 作为其语义层的基础。

今天,我们很高兴地宣布 Unity Catalog Semantics 中的两项新功能,它们让智能体能够在查询时直接用 SQL 组合独立治理的指标:

查询时连接。现在,metric views 可以在普通 SQL 中与其他 metric views、表和视图进行连接。SQL 引擎会保护度量值免受连接引发的重复影响,即便是在多对多连接的情况下也是如此,从而能够即时组合来自多个数据源的指标和维度。如需进一步了解查询时连接,请阅读产品文档。

关系约束。借助熟悉的 SQL 信息性约束,数据建模人员现在可以直接在 Unity Catalog 中定义指标视图之间的关系。智能体和 BI 工具可以发现这些关系,并利用它们生成跨独立治理的指标视图的连接。要了解有关关系约束连接的更多信息,请阅读产品文档。

这些功能共同将业务指标与关系的治理同单个分析的范围分离开来。数据建模人员在 Unity Catalog 中治理这些定义,而 AI 智能体(包括由 Genie 驱动的智能体)可以在查询时将它们组合起来,以回答新的业务问题。

在 SQL 中组合度量的挑战

商业分析中的一个常见问题是从不同数据源组合指标。假设你的公司将销售交易和支持工单存储在两个独立的表中,但你希望将它们整合起来,并按地区分别查看两者。用 SQL 编写这一查询的自然方式是:按地区连接两个表,然后计算销售总额并统计所有支持工单的数量:

遗憾的是,这个简单的查询可能会产生错误的结果。如果一个地区有多笔销售记录和多张工单,那么连接操作会使销售记录和工单记录重复,从而产生错误的聚合结果。这种意外行为通常被称为裂谷陷阱(chasm trap),常常让毫无防备的用户栽跟头。

传统语义模型通过控制查询的构建方式以及度量的计算方式来应对这一问题。这通常要求用户和工具通过特定于模型的接口或拖拽式 UI 进行查询。

Unity Catalog Semantics 采用了不同的方式:防范连接引发的重复这一保护机制被内置于 SQL 引擎本身。用户、智能体和 BI 工具可以构建丰富的 SQL 查询,同时依靠 Databricks 来保持受治理度量的正确聚合语义。

作为可组合构建块的指标视图

指标视图类似 SQL 视图或表,但它们可以包含业务指标。

它们有两类列:

字段是普通列:你可以对它们进行筛选、分组和连接。它们通常用于维度或事实。

度量定义业务指标。它们携带着聚合计算,以及其所聚合源行的标识,这样引擎就知道如何避免行重复。

度量可以按业务需要任意丰富,从 SUM(amount) 到其他度量的比率、累计合计以及半累加余额。度量在 SQL 中使用 MEASURE() 求值,这是一个聚合函数,可用于任何聚合函数可用的位置:SELECT 列表、HAVING、ORDER BY、子查询和 CTE:

与传统语义模型不同(其语义会根据所请求的度量和维度动态变化),指标视图保留了 SQL 视图的关系行为。这意味着:

它的字段可以用于普通 SQL,而且在查询中添加一个度量不会改变底层的行集合。

MEASURE() 对 WHERE 和 GROUP BY 的响应,与基于该度量定义的标准聚合完全一致,因此过滤和切片的表现可预期。

指标视图是可组合的:一个指标视图可以作为另一个指标视图的构建模块。

将受治理的度量与查询时联接组合使用

以前你可以在 SQL 中查询指标视图,但指标视图必须单独出现在 FROM 子句中。现在,你可以把指标视图与普通表或视图联接,甚至可以与其他指标视图联接。引擎保证联接不会让度量值膨胀。每个源行在每个分组中最多只贡献一次,无论它产生多少条联接后的行;这就是我们防止裂谷陷阱(chasm trap)和扇出陷阱(fan trap)的方式。

这解锁了强大的分析场景:面向用户和智能体的灵活、即时分析,以及面向建模人员的可复用复杂指标视图源定义。

在查询时丰富指标视图

假设一位同事创建了一个指标视图 sales_metric_view,其中包含 total_sales 度量和 region 维度。但你想把销售额上卷到国家层级。与其请同事把 country 加到他们的指标视图中,你可以在查询时联接一张地理表:

同样的模式也适用于关联财年日历、客户属性,以及在指标视图之外定义的其他业务维度。你(或你的智能体)可以用普通 SQL 来扩展分析,而无需改动受治理的指标视图。

组合独立治理的指标

你还可以关联两个指标视图,以分析各自独立定义的度量(这些度量可能由组织中不同部门负责)。我们来看两个基于以下表构建的指标视图:

对应的指标视图定义如下:

现在,你可以通过关联这两个指标视图,把销售额和支持工单放在一起报表:

关键特性在于,这种关联不会错误地重复度量。East 有两条销售记录和三条工单,产生六行关联结果。对这些行执行普通的 SUM(s.amount) 会返回 900,而不是 300。但 MEASURE(s.total_sales) 会正确返回 300,因为每笔原始销售只被计入一次。同样,MEASURE(t.ticket_count) 统计的是三条工单,而不是六行关联结果。这一特性在一对多、多对一和多对多关联中都成立。无论哪一侧发生行数膨胀,Databricks 引擎都能确保每个源数据行在每个分组中最多只对其度量贡献一次。

请注意,与普通 SQL 一样,North 和 South 会被内连接排除。MEASURE() 能够防止关联导致的数据重复,同时不改变关联本身的语义。

你还可以在表达式中组合来自不同指标视图的度量。例如,计算每笔销售对应的支持工单数:

东部每笔销售有 1.5 张工单,而西部有 1.0 张。

跨指标视图筛选

连接还允许你使用一个指标视图中的字段来筛选另一个指标视图中的度量。例如,按渠道报告销售额,但仅限于至少有一张低严重程度支持工单的区域:

东部和西部都有低严重程度工单,因此它们的销售额符合条件。然而,东部有两张匹配工单。对连接后的行执行常规的 SUM(s.amount) 会重复计算东部的销售额,使 web 返回 350,store 返回 450。使用 MEASURE() 时,引擎会保留原始度量语义,并为每个渠道正确返回 250。

同样的筛选语义也适用于 OUTER JOIN、对已求值度量使用 HAVING,以及跨指标视图的 EXISTS 和 IN 子查询。

你可以手动使用半连接(semi-join)或在连接前对匹配区域去重,从而获得相同结果。但这些改写要求查询作者对每个查询的连接基数和聚合进行仔细推理。借助 Unity Catalog Semantics,你可以在编写简单直接的查询时得到正确答案。

使用查询时连接定义多事实指标视图(即将推出)

如今,指标视图作者可以在有限场景下,通过在指标视图 YAML 中使用定义时的一对多连接来声明多事实度量。很快,指标视图源中支持指标视图之间的 SQL 连接,将能够在保留关系语义的同时实现更灵活的多事实定义。

指标视图之间的关系约束

原文较长,译文为节选;译文由英文原文机器翻译,术语按行业惯例统一,可在文末展开原文核对。

查看英文原文(Beyond the semantic model: composable metrics for AI agents)

Together, these capabilities enable agents to answer new business questions using centrally governed metric definitions, without requiring monolithic or overlapping semantic models. Both features are now in Beta.

Traditional semantic models bring together the tables, relationships, and metrics needed to answer a set of business questions. This approach has worked well for BI, where the scope of analysis is often known when a model is built. But with AI agents, the scope of analysis is increasingly determined at query time. Users can ask questions spanning sales, support tickets, and product usage, even when the corresponding metrics are defined and governed independently by different teams. Anticipating every combination of metrics and dimensions at modeling time would require either unwieldy semantic models or a proliferation of redundant ones.

At Databricks, we’re taking a different approach to semantic modeling. In April, we released Unity Catalog metric views, a SQL-first way to define and govern business metrics. Since then, many customers have adopted metric views as the foundation of their semantic layer.

Today, we're excited to announce two new features in Unity Catalog Semantics that enable agents to compose independently governed metrics directly in SQL at query time:

Query-time joins. Metric views can now be joined with other metric views, tables, and views in ordinary SQL. The SQL engine protects measures from join-induced duplication, even across many-to-many joins, allowing metrics and dimensions from multiple data sources to be combined on the fly. To learn more about query-time joins please read the product documentation.

Relationship constraints. Using familiar SQL informational constraints, data modelers can now define relationships between metric views directly in Unity Catalog. Agents and BI tools can discover these relationships and use them to generate joins across independently governed metric views. To learn more about relationship constraints joins please read the product documentation.

Together, these features separate the governance of business metrics and relationships from the scope of individual analyses. Data modelers govern these definitions in Unity Catalog, while AI agents, including those powered by Genie, can combine them at query time to answer new business questions.

The challenge of composing measures in SQL

A common problem in business analytics is combining metrics from different data sources. Suppose your company stores sales transactions and support tickets in two separate tables, but you want to consolidate them and view both broken down by region. A natural way to write this query in SQL would be to join the two tables on region, then compute the total sales amount and count all the support tickets:

Unfortunately, this simple query can produce wrong results. If a region has multiple sales and multiple tickets, then the join duplicates sales and tickets, producing incorrect aggregates. This unexpected behavior is commonly referred to as a chasm trap and often trips up an unsuspecting user.

Traditional semantic models address this problem by controlling how queries are constructed and how measures are evaluated. This typically requires users and tools to query through model-specific interfaces or drag-and-drop UI.

Unity Catalog Semantics takes a different approach: the protection against join-induced duplication is built into the SQL engine itself. Users, agents, and BI tools can construct rich SQL queries while relying on Databricks to preserve the correct aggregation semantics of governed measures.

Metric views as composable building blocks

Metric views are like SQL views or tables, but they can include business metrics.

They have two kinds of columns:

Fields are normal columns: you can filter on them, group by them, and join on them. They are typically used for dimensions or facts.

Measures define business metrics. They carry an aggregate calculation together with the identity of the source rows it aggregates, so that the engine knows how to avoid duplicating rows.

A measure can be as rich as the business requires, from SUM(amount) to ratios of other measures, running totals, and semi-additive balances. Measures are evaluated in SQL with MEASURE(), an aggregate function that goes anywhere an aggregate function goes: the SELECT list, HAVING, ORDER BY, subqueries, and CTEs:

Unlike traditional semantic models whose semantics change dynamically based on the requested measures and dimensions, a metric view preserves the relational behavior of a SQL view. This means:

Its fields can be used in ordinary SQL, and adding a measure to a query does not change the underlying set of rows.

MEASURE() responds to WHERE and GROUP BY just as a standard aggregate over the measure's definition would, so filtering and slicing behave predictably.

Metric views are composable: one can serve as a building block for another.

Composing governed metrics with query-time joins

Previously you could query metric views in SQL, but a metric view had to be by itself in the FROM clause. Now you can join metric views with normal tables or views, or even with other metric views. The engine guarantees that joins do not inflate measures. Each source row contributes at most once per group, regardless of how many joined rows it produces; this is how we prevent chasm and fan traps.

This unlocks powerful analytic scenarios: flexible, on-the-fly analysis for users and agents, and reusable complex metric view source definitions for modelers.

Enrich metric views at query time

Suppose a colleague has created a metric view, sales_metric_view, with a total_sales measure and a region dimension. But you want to roll up sales to the country level. Rather than having to ask your colleague to add country to their metric view, you can join a geography table at query time:

The same pattern applies to joining fiscal calendars, customer attributes, and other business dimensions defined outside the metric view. You (or your agent) can extend an analysis using ordinary SQL without changing the governed metric view.

Combine independently governed metrics

You can also join two metric views to analyze measures defined independently (perhaps owned by different parts of your organization). Let’s consider two metric views built on the following tables:

The corresponding metric view definitions are below:

You can now report sales and support tickets together by joining the two metric views:

The key feature is that this join does not incorrectly duplicate measures. East has two sales and three tickets, producing six joined rows. A regular SUM(s.amount) over those rows returns 900, rather than 300. But MEASURE(s.total_sales) correctly returns 300, because each original sale contributes only once. Similarly, MEASURE(t.ticket_count) counts three tickets, not six joined rows. This holds for one-to-many, many-to-one, and many-to-many joins. Whichever side fans out, the Databricks engine ensures each source row contributes to its measure at most once per group.

Note that North and South are excluded by the inner join, just as in ordinary SQL. MEASURE() protects against join-induced duplication without changing the semantics of the join itself.

You can also combine measures from different metric views in expressions. For example, to calculate support tickets per sale:

East has 1.5 tickets per sale, while West has 1.0.

Filter across metric views

Joins also let you use fields from one metric view to filter measures from another. For example, to report sales by channel, but only for regions with at least one low-severity support ticket:

East and West both have low-severity tickets, so their sales qualify. However, East has two matching tickets. A regular SUM(s.amount) across the joined rows would double-count East's sales, returning 350 for web and 450 for store. With MEASURE(), the engine preserves the original measure semantics and correctly returns 250 for each channel.

The same filtering semantics extend to OUTER JOINs, HAVING on evaluated measures, and EXISTS and IN subqueries across metric views.

You could achieve the same result manually using a semi-join or deduplicating the matching regions before joining. But those rewrites require query authors to carefully reason about join cardinality and aggregation for every query. With Unity Catalog Semantics, you get the right answer while writing simple, straightforward queries.

Define multi-fact metric views using query-time joins (Coming soon)

Today, metric view authors can declare multi-fact measures in limited scenarios using definition-time one-to-many joins in the metric view YAML. Soon, support for SQL joins between metric views in the metric view source will enable more flexible multi-fact definitions while preserving relational semantics.

Relationship constraints between metric views

Query-time joins allow agents to safely compose governed metrics in SQL. But agents also need to know how metric views relate to one another. Relationship constraints allow data modelers to capture and govern these relationships directly in Unity Catalog, without prescribing which joins must be performed for a particular analysis.

Foreign key constraints already provide a familiar SQL mechanism for describing relationships between tables. However, they are designed around many-to-one relationships based on key equality, rather than the broader range of relationships needed for semantic modeling.

Relationship constraints address these shortcomings. A relationship constraint is a named, directional link between metric views, with a declared cardinality (one-to-one, many-to-one, or even many-to-many) and a flexible join condition. Agents and BI tools can use these constraints to discover how metric views relate to each other.

Suppose sales_metric_view and tickets_metric_view both expose a customer identifier, and a third metric view, customers_metric_view, holds customer attributes such as segment. The two facts meet at the shared dimension:

A data modeler can add relationship constraints between these metric views to document how they semantically relate to each other:

These relationships will now appear in DESCRIBE TABLE EXTENDED on the metric views and in information_schema.table_constraints. A tool that reads these relationships can discover that sales and tickets connect through customers, and can write the join itself:

Note that no semantic model combining sales, tickets, and customers had to be defined in advance. The individual metric views and their relationships are governed in Unity Catalog, and the agent determines which metric views to join based on the business question. The SQL engine preserves the aggregation semantics of each measure across those joins.

We plan to augment these constraints with more instructions to better address cases such as role-playing dimensions and resolving path ambiguity.

Relationship constraints vs. definition-time joins

Relationships complement the joins a metric view can declare in its own definition.

A definition-time join in the metric view YAML fixes the join at definition time; the engine must use that join path. This is useful when there is exactly one way to relate two datasets, or when the modeler wants to lock in a cross-metric view measure definition using a specific path.

A relationship constraint describes possible joins but defers the choice to the agent or the user at query time. This is useful when there are, or may eventually be, multiple possible ways to relate two datasets, for example, role-playing dimensions (a shipping city and a billing city), a self-relationship (employees and their managers), and even cycles. In these cases, the consuming tool can decide which joins best fit the analytic question at query time.

A SQL foundation for agents and BI tools

Genie Ontology already treats metric views as the authoritative definitions of business metrics. With relationship constraints, Genie can also discover how these independently governed metric views relate to one another.

In the sales and support tickets example above, Genie can discover the relationships through the shared customer dimension and generate SQL to analyze sales and support tickets by customer segment. Query-time joins ensure that neither measure is inflated by the join.

As teams publish new metric views and relationships to Unity Catalog, they expand the range of business questions Genie can answer without having to rebuild existing semantic models.

Genie and AI/BI Dashboards

Genie One, Genie Agents, and Genie Code can leverage relationship constraints and query-time joins to answer questions across independently governed metric views. AI/BI Dashboards can also import relationships defined in Unity Catalog, eliminating the need to redefine them for each dashboard. Relationships authored within dashboards can be published back to Unity Catalog, making them reusable across other dashboards and Genie experiences.

Third-party BI integration

BI tools generate SQL for analytic operations such as Top N filters, nested aggregations, and cross-filtering. Integrating with traditional semantic layers often requires BI tools to translate these operations into a model-specific query interface or disable functionality that the interface cannot express. With query-time joins, BI tools can continue generating SQL for these operations while delegating the evaluation of governed measures to Databricks.

For example, the query below, generated by a BI tool, uses a subquery to identify the top two regions by number of sales, then filters the main query to report total sales revenue by channel across those regions. This type of analysis can be difficult or impossible to express through a restricted semantic query interface. With Unity Catalog Semantics, the query runs as written, and the Databricks engine preserves the aggregation semantics of the measures in both the subquery and the main query.

We believe this first-class SQL support makes UC Semantics a uniquely appealing integration target for BI tools. We have started working on integrations with some BI vendors and are looking forward to announcing these in the near future.

If you are a BI vendor, please reach out; we’d love to work with you on integrating UC Semantics into your tools.

What’s next for composable metrics?

Query-time joins and relationships build on what UC Semantics already offers with metric views:

window measures for analysis like running totals and period-over-period comparisons

level-of-detail measures for simple two-pass analysis

parameters that let a metric view be called like a table-valued function

composability via metric views serving as sources for others

materialization and pre-aggregation with smart query rewriting for performance and cost

Our next steps continue adding the power of data modeling to SQL. We are working toward declaring ad hoc measures directly in SQL, without requiring a YAML definition for quick explorations or small extensions to existing metric views. We are also working toward rich SQL extensions to control a measure's evaluation context, so that semi-additive logic and period comparisons can be expressed in SQL rather than configured around them.

Our direction is to make governed metrics and relationships reusable across the full range of questions that users and agents ask. By bringing semantic modeling into SQL and Unity Catalog, we can govern metrics independently of the analyses that consume them. Teams define their business metrics once, without having to anticipate every question that will be asked of them.

Continued commitment to openness

Last July, we open-sourced the core metric view and measure reference implementation in Apache Spark™ and Unity Catalog. We are now working to build on top of this foundation and add support for joining measures in SQL. In parallel, we’re excited to be a top contributor to industry standardization efforts such as Apache Ossie (Incubating) and its commitment to portable query semantics.

Query-time joins and relationship constraints are in Beta on Databricks Runtime 19 and above and on SQL warehouses. The documentation for joining metric views in SQL, relationships between metric views, and querying metric views walks through the examples in this post, and the TPC-H tutorial builds a complete metric view from sample data.

How is a metric view different from a semantic model?

A metric view is a Unity Catalog view that behaves like a regular SQL view, with the addition of governed measures. A traditional semantic model takes a different approach: it selects which tables to join based on the requested measures and dimensions, so the underlying set of rows can change from one question to the next. Different BI tools or query interfaces may interpret the same model differently and return different answers.

Metric views avoid this problem by governing the measure logic while preserving standard SQL semantics for everything else. Rather than asking every BI tool to understand a new semantic model, metric views build on a common interface that BI tools already understand: SQL. The metric logic stays consistent regardless of which tool asks the question.

Do I need a primary key for MEASURE() to work across joins?

No. Each measure carries the identity of its source rows, and MEASURE() aggregates over those rows without duplication, regardless of how the query joins them. Two source rows with identical values remain two rows; only the duplication introduced by the join is removed.

What is the difference between definition-time joins in YAML, relationships, and query-time joins?

A join in the metric view's YAML definition fixes the modeling path at definition time, which suits star and snowflake schemas. A relationship is metadata that records how two metric views relate without executing a join. A query-time join composes metric views in SQL for one question. The three work together: define a star via a definition-time join in YAML, describe the graph with relationships, and compose across it at query time.

Can BI tools use metric view joins today?

Yes. BI tools can integrate with metric views through different mechanisms, depending on their query capabilities.

Tools with a pass-through aggregate function can call the governed measure directly. In Tableau, for example, a calculated field that wraps MEASURE() in RAWSQLAGG_REAL sends the measure to Databricks, which evaluates it exactly as defined. The tool keeps generating its own SQL, so it can still leverage features like Top N filters, sets, and two-stage aggregations on top of metric views.

Power BI works through mirroring. Databricks publishes each metric view into a Power BI semantic model as a table with matching Power BI measures in DAX, so reports can join across metric views in that model.

Get the latest posts in your inbox

原文为英文,正文由自动抓取并保留原文。

本文由采集器自动同步

原文发布于 Databricks Blog(Beyond the semantic model: composable metrics for AI agents),标题、摘要与正文由机器翻译,术语按行业惯例统一,未经人工逐句校订。

阅读原文 ↗