Why a metric should never need a comment saying 'requires GROUP BY'

2026-10-18

A metric definition from an open semantic-layer example:

name: brand_rank_in_store
expression: RANK() OVER (PARTITION BY store.s_store_sk
            ORDER BY SUM(store_sales.ss_ext_sales_price) DESC)
description: Rank of each brand by sales within a store
             (requires grouping by store.s_store_sk and item.i_brand)

The last line is the problem. The metric is only correct in a query that groups by two specific columns, and the specification says so in a comment. A human can read it. A tool that splices the expression into SQL cannot. An LLM will usually, but not always, get it right.

The problem

SQL makes the meaning of a number depend on its surrounding query. The same SUM means something different under a different GROUP BY, and a join that silently multiplies rows changes it again. A metric definition that is only a SQL snippet is therefore incomplete. The missing part, the required grain, lives in the head of whoever writes the query.

Text-to-SQL inherits the gap. The model must rediscover the grouping each time, and a wrong answer looks exactly like a right one: valid SQL, plausible numbers.

Grammar comparison

KQL 28 rules

Move the decision out of the query

In KQL the author states intent only:

FIND customers c, c orders o
FILTER count(o) > 10 AND o.order_date BETWEEN "2023-01-01" AND "2023-12-31"
FETCH c.company_name, count(o) DESC

There is no GROUP BY, no HAVING, and no join condition. Before any SQL exists, the query becomes an intermediate model (IQL), and a fixed set of rules makes the structural decisions explicit:

  • GroupRule: if the output contains an aggregate, every non-aggregate output becomes a grouping expression. c.company_name is grouped because count(o) is aggregated.
  • HavingRule: a filter containing an aggregate cannot live in WHERE, so it moves to HAVING, while the date filter stays in WHERE.
  • PushLogicalExpressionRule: a filter on an optional (outer-joined) table moves into the ON clause. In WHERE it would silently turn the LEFT JOIN into an INNER JOIN and drop the customers with no orders.
  • IdentityRule: count(o) becomes a count of a key column. count_distinct(od) on a composite key must count the whole key. Counting only the first column gave 830 instead of 2155 on the Northwind order lines, until the rule learned the difference.

Each rule is small and documented with a correct and an incorrect SQL example. The SQL is then rendered for the target dialect.

Why this is the right place

The compiler does not guess the grouping. It derives it from the shape of the query, the same way every time. When the result is wrong you can point at a rule, which you cannot do with a model's output. When the compiler cannot decide, it refuses instead of emitting plausible SQL.

The model's job shrinks to choosing entities, filters and aggregates in a language short enough to audit by eye.

Limits

A missing foreign key in the model means a refused join, not a guessed one. A ranking like the one above needs a query block (a CTE) to be expressed. I think a visible refusal is the right trade for an invisible error.