如何增加雪花中的最大迭代次数?

how to increase max iteration count in snowflake?

当我在 snowflake 中编写下面的代码时,出现错误。

set start_date = '2020-08-01'::TIMESTAMP_NTZ(9);
set end_date = '2020-11-30'::TIMESTAMP_NTZ(9);

WITH DateTable AS (
    SELECT $start_date AS MyDate
    UNION ALL
    SELECT DATEADD(DAY, 1, MyDate) FROM DateTable WHERE MyDate < $end_date
)
SELECT * FROM DateTable; 

报错信息如下...

Recursion exceeded max iteration count (100)

我好像没有增加最大迭代次数的选项。
如何增加交互次数的限制?

最大迭代次数似乎不容易修改。 documentation says:

In theory, constructing a recursive CTE incorrectly can cause an infinite loop. In practice, Snowflake prevents this by limiting the number of iterations that the recursive clause will perform in a single query. The MAX_RECURSIONS parameter limits the number of iterations.

To change MAX_RECURSIONS for your account, please contact Snowflake Support.

对于您的特定用例,您可以通过更改查询逻辑来解决该限制。你真的不需要递归来实现你的目标;相反,您可以使用 window 函数和任何 table 作为足够的行数 - 例如 bigtable(col):

select dateadd(day, x.rn - 1, $start_date) as mydate
from (select row_number() over (order by col) rn from bigtable) x
where dateadd(day, x.rn - 1, $start_date) <= $end_date

使用generator的非递归版本:

SET (start_date,end_date)=('2020-08-01'::TIMESTAMP_NTZ(9),'2020-11-30'::TIMESTAMP_NTZ(9));
SET c = (SELECT DATEDIFF(DAY, $start_date, $end_date));

SELECT DATEADD(DAY, c.n, $start_date) AS calc_date
FROM(SELECT ROW_NUMBER() OVER (ORDER BY 1) - 1 FROM TABLE(generator(rowcount=>$c))) c(n);

解释:

变量 c 保存 start/end 日期之间的天数,并且是生成器的输入。生成器 returns 指定的行数。 ROW_NUMBER() OVER() - 1窗口函数returns从0开始的连续数,加到start_date.