FIRST_VALUE() 中带有 IGNORE NULLS 的意外行为 (Vertica)
Unexpected behavior in FIRST_VALUE() with IGNORE NULLS (Vertica)
我在带有 IGNORE NULLS 参数的 Vertica FIRST_VALUE() 分析函数中看到意外行为。它似乎 return NULL,但它不应该。
问题出现在这个非常小的 table:
drop table if exists temp;
create table temp (time_ timestamp(6), name varchar(10));
insert into temp (time_) values ('2016-03-18 20:32:16.144');
insert into temp (time_, name) values ('2016-03-18 20:52:09.062', 'abc');
以下是 table 的内容(select * 来自 temp):
time_ | name
------------------------+--------
2016-03-18 20:32:16.144 | <null>
2016-03-18 20:52:09.062 | abc
这是我的查询 运行:
select time_,
first_value(name ignore nulls) over (order by time_) first_name
from temp;
以下是此查询的结果 returns:
time_ | first_name
------------------------+------------
2016-03-18 20:32:16.144 | <null>
2016-03-18 20:52:09.062 | abc
以下是我希望(和希望)从此查询得到的结果:
time_ | first_name
------------------------+------------
2016-03-18 20:32:16.144 | abc
2016-03-18 20:52:09.062 | abc
上面的查询是否有一个非常基本的语法错误?此问题出现在 Vertica Community Edition 7.1.1 上。
这是我思考的方向
select time_
,first_value(name) over (order by case when name is null then 1 else 0 end,time_) FirstName
from temp A
order by time_
Returns
time_ FirstName
20:32:16.1440000 abc
20:52:09.0620000 abc
函数按预期工作。
over (order by time_)
是 over (order by time_ range unbounded preceding)
的快捷方式,over (order by time_ range unbounded preceding)
是 over (order by time_ range between unbounded preceding and current row)
的快捷方式,这意味着每一行只能看到它前面的行,包括它自己。
第一行只能看到它自己,因此在其范围内没有非 NULL 值。
如果你想要整个作用域的第一个非NULL值,你必须指定整个作用域:
first_value(name ignore nulls) over
(order by time_ range between unbounded preceding and unbounded following) first_name
不,这绝对不是错误。
您可能一直在使用 sum(x) over (order by y)
之类的语法来表示 运行 总数,而默认的 window RANGE UNBOUNDED PRECEDING 对您来说似乎很自然。
由于您没有为 FIRST_VALUE 函数定义明确的 window,因此您一直在使用相同的默认值 window。
这是另一个测试用例:
ts val
-- ----
1 NULL
2 X
3 NULL
4 Y
5 NULL
您希望从以下功能中得到什么?
last_value (val) order (by ts)
您希望从以下功能中得到什么?
last_value (val ignore nulls) order (by ts)
我在带有 IGNORE NULLS 参数的 Vertica FIRST_VALUE() 分析函数中看到意外行为。它似乎 return NULL,但它不应该。
问题出现在这个非常小的 table:
drop table if exists temp;
create table temp (time_ timestamp(6), name varchar(10));
insert into temp (time_) values ('2016-03-18 20:32:16.144');
insert into temp (time_, name) values ('2016-03-18 20:52:09.062', 'abc');
以下是 table 的内容(select * 来自 temp):
time_ | name
------------------------+--------
2016-03-18 20:32:16.144 | <null>
2016-03-18 20:52:09.062 | abc
这是我的查询 运行:
select time_,
first_value(name ignore nulls) over (order by time_) first_name
from temp;
以下是此查询的结果 returns:
time_ | first_name
------------------------+------------
2016-03-18 20:32:16.144 | <null>
2016-03-18 20:52:09.062 | abc
以下是我希望(和希望)从此查询得到的结果:
time_ | first_name
------------------------+------------
2016-03-18 20:32:16.144 | abc
2016-03-18 20:52:09.062 | abc
上面的查询是否有一个非常基本的语法错误?此问题出现在 Vertica Community Edition 7.1.1 上。
这是我思考的方向
select time_
,first_value(name) over (order by case when name is null then 1 else 0 end,time_) FirstName
from temp A
order by time_
Returns
time_ FirstName
20:32:16.1440000 abc
20:52:09.0620000 abc
函数按预期工作。
over (order by time_)
是 over (order by time_ range unbounded preceding)
的快捷方式,over (order by time_ range unbounded preceding)
是 over (order by time_ range between unbounded preceding and current row)
的快捷方式,这意味着每一行只能看到它前面的行,包括它自己。
第一行只能看到它自己,因此在其范围内没有非 NULL 值。
如果你想要整个作用域的第一个非NULL值,你必须指定整个作用域:
first_value(name ignore nulls) over
(order by time_ range between unbounded preceding and unbounded following) first_name
不,这绝对不是错误。
您可能一直在使用 sum(x) over (order by y)
之类的语法来表示 运行 总数,而默认的 window RANGE UNBOUNDED PRECEDING 对您来说似乎很自然。
由于您没有为 FIRST_VALUE 函数定义明确的 window,因此您一直在使用相同的默认值 window。
这是另一个测试用例:
ts val
-- ----
1 NULL
2 X
3 NULL
4 Y
5 NULL
您希望从以下功能中得到什么?
last_value (val) order (by ts)
您希望从以下功能中得到什么?
last_value (val ignore nulls) order (by ts)