为什么框架的默认 OVER () 行为?

Why this default OVER () behavior for the frame?

我从以下两个 window 函数中注意到:

WITH sales AS (
    select 2020 as year, 100 as revenue UNION
    select 2021 as year, 200 as revenue UNION
    select 2022 as year, 300 as revenue UNION
    select 2023 as year, 100 as revenue
)
SELECT JSON_ARRAYAGG(revenue) OVER (order by year) FROM sales LIMIT 1;

# revenue_list
[100]

还有这个:

WITH sales AS (
    select 2020 as year, 100 as revenue UNION
    select 2021 as year, 200 as revenue UNION
    select 2022 as year, 300 as revenue UNION
    select 2023 as year, 100 as revenue
)
SELECT JSON_ARRAYAGG(revenue) OVER () revenue_list FROM sales LIMIT 1;
# revenue_list
[100, 200, 300, 100]

框架的默认行为似乎是:

这是对默认框架行为的正确理解吗?如果是这样,为什么会做出这样的选择? (例如,除非用户指定,否则为什么不让它始终不受限制?)

第一个查询中的 LIMIT 1 子句误导了您。考虑没有任何 LIMIT 子句的完整查询:

SELECT JSON_ARRAYAGG(revenue) OVER (ORDER BY year) FROM sales;

由此产生的结果集为:

返回 [100] 的原因...完全是任意的。您的 LIMIT 查询没有 ORDER BY 子句,因此我们在结果集中看到的“第一条”记录恰好返回(但理论上可能是 四个记录中的任意一个).

OVER () 版本只会将整个 table 聚合到 JSON 数组中,但会为每条记录显示该值。

SELECT JSON_ARRAYAGG(revenue) OVER () FROM sales;  -- [100, 200, 300, 100] each record
SELECT JSON_ARRAYAGG(revenue) FROM sales;          -- [100, 200, 300, 100] one record