为什么这个Snowflake查询可以在不需要横向关键字的情况下工作? [英] Why does this Snowflake query work without requiring the LATERAL keyword?

查看:19
本文介绍了为什么这个Snowflake查询可以在不需要横向关键字的情况下工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Snowflake中有此视图:

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; 

没有横向的查询为什么工作?

推荐答案

代码:

select id_foo, baz.aux_id
from foo
cross join table(build_aux_table(foo.id_foo)) baz;

基本相同于:

select id_foo, baz.aux_id
from foo
,table(build_aux_table(foo.id_foo)) baz;

文档介绍:

Calling JavaScript UDTFs in Queries

这个简单的示例说明如何调用UDTF。此示例传递文字值。

SELECT * FROM TABLE(js_udtf(10.0::FLOAT, 20.0::FLOAT));

此示例调用UDTF并将值从另一个表传递给它。在此示例中,名为js_udtf的UDTF针对名为tab1的表中的每一行调用一次。每次调用该函数时,都会从当前行的列c1和c2传递值。

SELECT * FROM tab1, TABLE(js_udtf(tab1.c1, tab1.c2)) ;

Sample UDTFs - Examples with joins

在与其他表的联接中使用UDTF;请注意,表中的联接列作为参数传递给函数。

select * 
from favorite_years y join table(favorite_colors(y.year)) c;

这篇关于为什么这个Snowflake查询可以在不需要横向关键字的情况下工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆