---
title: "聚合只在实体集合上折叠"
---

> Documentation Index
> Fetch the complete documentation index at: https://koine.systems/llms.txt
> Use this file to discover all available pages before exploring further.

# 聚合只在实体集合上折叠

## 背景

K-016 定义了六个聚合函数，但没有回答：

- 聚合遍历谁：实体、关系行、值、枚举？
- `sum` 能用于 Duration / quantity 吗？
- `min` / `max` 空集怎么办？
- 嵌套聚合和 binder 遮蔽怎么处理？
- 聚合能不能出现在 transition 右值或 state/init？

关系是集合，不是 bag；如果允许按关系行聚合，就要引入重复、分组、key 去重等一整套语义。Koine 的实体已经显式声明、有限、完整，更适合作为聚合域。

## 选项

- 聚合遍历实体、关系行、标量值、枚举的任意组合。
- 只遍历实体；关系通过过滤条件参与。
- `sum` 只支持 Int/Decimal；或跟随 `+` 支持 Duration/quantity。
- `min` / `max` 空集返回 0、返回空值、或诊断。

## 决定

### 1. 聚合域只有 entity

```koine
sum(amount_in(line, order_currency(order)) for line: Line where line_in(line, order))
count(line: Line where line_in(line, order))
min(amount_in(line, USD) for line: Line where line_in(line, order))
```

- binder 只能绑定 entity 类型。
- 遍历当前世界里该类型的全部实体；实体是集合，不重复。
- 不遍历关系行、标量值、枚举值、产品值。
- binder 名必须是新名字；不允许遮蔽 define 参数或外层 binder。

### 2. 语法

```text
sum(expr for x: EntityType where condition)
count(x: EntityType where condition)
min(expr for x: EntityType where condition)
max(expr for x: EntityType where condition)
any(expr for x: EntityType where condition)
all(expr for x: EntityType where condition)
```

- `where` 可选；省略表示全部实体。
- `count` 保持不带 `for` 的形式；这是语法上的例外，语义清楚。

### 3. 类型与空集

| 聚合 | 元素/过滤类型 | 结果 | 空集 |
| --- | --- | --- | --- |
| `sum` | Int | Int | 0 |
| `sum` | Decimal | Decimal | 0 |
| `sum` | Duration | Duration | 0 Duration |
| `sum` | Quantity，dimension D | Quantity，dimension D | 0（dimension D） |
| `count` | 过滤 Bool | Int | 0 |
| `min` / `max` | 有全序的 T | T | 诊断 |
| `any` | Bool | Bool | false |
| `all` | Bool | Bool | true |

- `min` / `max` 的 T 只能是 Int、Decimal、Date、Time、Timestamp、Duration、同 dimension Quantity；Text、entity、产品值、枚举、relation 没有全序。
- `min` / `max` 空集报 `koine.type.aggregate-empty`；不返回 0，也不返回“无”。
- 元素表达式或过滤失败时，诊断向上传播；不能跳过该元素或把失败当成 false。
- `any` / `all` 可以短路；语义等价。

### 4. 嵌套与调用图

- 聚合可以嵌套，但不鼓励；深层/相关嵌套走 lint，不改变语义。
- 聚合表达式/过滤里的 define、derived relation 调用进入无环调用图。
- 聚合不能递归；不能通过 define 间接递归。

### 5. 上下文

允许：define 体、aggregate 表达式/过滤、require、axiom、transition 右值、derived relation 公式。

禁止：state / init、value 精化、quantity / unit 声明、view。

### 6. 诊断

- `koine.type.aggregate-binder`：binder 不是 entity 类型。
- `koine.type.aggregate-element`：元素类型不支持该聚合。
- `koine.type.aggregate-filter`：过滤不是 Bool。
- `koine.type.aggregate-empty`：`min` / `max` 空集。
- `koine.type.aggregate-shadow`：binder 重名/遮蔽。

## 理由

- **实体是显式、有限、完整的集合**；关系也是集合，但按行聚合需要处理 key、重复、分组，语义表面积大。
- **sum 跟随 `+`**：Int/Decimal/Duration/Quantity 都有加法和零元；语言一致，不需要为每种类型发明新聚合。
- **min/max 跟随全序**：K-009 已规定哪些类型有全序；聚合复用，不另行定义。
- **空集显式**：sum/count/any/all 有自然零元；min/max 没有，必须诊断。
- **主流实践**：SQL 的 `SUM` / `MIN` / `MAX` 也是按类型和空集分别定义；Koine 把域限制到实体，避免 grouping/bag 复杂度。

## 后果

- K-016 从 `draft` 升为 `specified`。
- `cheapest_line` 在空行集合上会报 `koine.type.aggregate-empty`；调用方必须保证有行，或先处理空集。
- `order_total` 在空行集合上 `sum` 返回 0，合法。
- 实现需要按实体类型枚举当前世界实体，并实现空集/失败传播。
- 如果以后需要按关系行聚合，另开特性和 ADR。

## 相关

- [定义规范](https://koine.systems/reference/spec/definitions/)
- [形式语义](https://koine.systems/reference/spec/semantics/)
- [运算符](https://koine.systems/reference/spec/operators/)
- [量与单位](https://koine.systems/reference/spec/units/)
- [ADR-0023：封闭运算符表与显式 Decimal 除法](https://koine.systems/notes/decisions/0023-operator-table/)

Source: https://koine.systems/notes/decisions/0025-aggregate-entity-sets/index.mdx
