【发布时间】:2021-07-18 08:49:55
【问题描述】:
我在雪花中有这个视图:
create or replace view foo as
select $1 as id_foo, $2 as sales
from (values (1,100),(2,200),(3,300));
还有这个user-defined table function:
CREATE OR REPLACE FUNCTION build_aux_table ( restriction number )
RETURNS TABLE ( aux_id number )
AS 'select $1 as aux_id from (VALUES (1),(2),(3)) where aux_id = restriction';
以下查询有效,并返回 3 行:
select id_foo, baz.aux_id
from foo
cross join table(build_aux_table(foo.id_foo)) baz;
但是,我没想到查询会编译,因为我们要加入的 UDTF 生成的表依赖于第一个表中的列。我的理解是,这种表内依赖需要LATERAL 连接,如下所示(也可以):
select id_foo, baz.aux_id
from foo
cross join lateral build_aux_table(foo.id_foo) baz;
为什么没有 LATERAL 的查询会起作用?
【问题讨论】:
-
好地方。我希望有同样的行为:Table Functions
"Table functions can also be applied to a set of rows using the LATERAL construct." -
@LukaszSzozda 奇怪的是,FLATTEN 关键字被描述为一个表函数,但它确实需要 FLATTEN。
标签: sql snowflake-cloud-data-platform lateral-join