Snowflake中FIRST_VALUE+IGNORE NULLS在不同窗口帧下的异常行为
Snowflake中FIRST_VALUE结合IGNORE NULLS的窗口帧行为疑问
在测试Snowflake窗口函数时,发现FIRST_VALUE结合IGNORE NULLS在不同窗口帧定义下的行为不符合预期,具体情况如下:
测试数据集构建
create or replace temporary table test_data as select * from values (null, 1, 1), (2, 1, 2), (3, 1, 3), (4, 1, 4), (5, 1, 5) as t(id, partition_col, order_col);
测试查询
Query 1(有限窗口帧)
select order_col, id, first_value(id ignore nulls) over (partition by partition_col order by order_col rows between 1 preceding and current row) as fv_limited from test_data;
Query 2(累积窗口帧)
select order_col, id, first_value(id ignore nulls) over (partition by partition_col order by order_col rows between unbounded preceding and current row) as fv_unbounded from test_data;
不符合预期的结果
- Query 1的首行
fv_limited为null(符合预期,因为窗口帧仅包含首行的null值) - Query 2的首行
fv_unbounded却为2,但按预期此时窗口帧仅包含首行的null值,不应获取到后续行的非null值
疑问
- 该行为是否为设计逻辑:当窗口帧为
UNBOUNDED PRECEDING时,IGNORE NULLS会重新解读有序集合? - 或是Snowflake的实现特性:对累积帧采用更"持久"的处理,将首个非null值反向传递?
希望明确该行为的正式语义,以及能否在生产逻辑中可靠依赖此行为。
内容的提问来源于stack exchange,提问作者Arkadiusz




