Snowflake table 和生成器函数未给出预期结果

Snowflake table and generator functions does not give expected result

我试图创建一个简单的 SQL 来跟踪 query_history 使用情况,但是在使用 tablegenerator 函数(CTE下面命名为 x)。

使用我的时隙限制 query_history 时我没有得到任何结果,所以过了一会儿我硬编码了一个 SQL 以给出相同的结果(下面名为 y 的 CTE ) 这很好用。

为什么 x 不起作用?据我所知 xy 产生相同的结果?

首先测试示例运行 代码原样,这不会产生任何结果。 然后注释行 x as timeslots 并取消注释行 y as timeslots,这将给出所需的结果。

with 
x as (
    select
        dateadd('min',seq4()*10,dateadd('min',-60,current_timestamp())) f,
        dateadd('min',(seq4()+1)*10,dateadd('min',-60,current_timestamp())) t
    from table(generator(rowcount => 6))
), 
y as (
    select
        dateadd('min',n*10,dateadd('min',-60,current_timestamp())) f,
        dateadd('min',(n+1)*10,dateadd('min',-60,current_timestamp())) t
    from (select 0 n union all select 1 n union all select 2 union all select 3 
          union all select 4 union all select 5)
)

--select * from x;
--select * from y;

select distinct 
    user_name,
    timeslots.f
from snowflake.account_usage.query_history, 
   x as timeslots
   --y as timeslots
where start_time >= timeslots.f
and start_time < timeslots.t
order by timeslots.f desc;

(我知道代码不是最优的,这只是为了说明问题)

SEQ:

Returns a sequence of monotonically increasing integers, with wrap-around. Wrap-around occurs after the largest representable integer of the integer width (1, 2, 4, or 8 byte).

If a fully ordered, gap-free sequence is required, consider using the ROW_NUMBER window function.

对于:

with x as (
    select
        dateadd('min',seq4()*10,dateadd('min',-60,current_timestamp())) f,
        dateadd('min',(seq4()+1)*10,dateadd('min',-60,current_timestamp())) t
    from table(generator(rowcount => 6))
)
SELECT * FROM x;

应该是:

with x as (
    select
        (ROW_NUMBER() OVER(ORDER BY seq4())) - 1 AS n,
        dateadd('min',n*10,dateadd('min',-60,current_timestamp())) f,
        dateadd('min',(n+1)*10,dateadd('min',-60,current_timestamp())) t
    from table(generator(rowcount => 6))
)
SELECT * FROM x;