Snowflake 中的递归 CTE 循环
Recursive CTE cyles in Snowflake
我发现这个例子可以处理递归 CTE 中的循环:
Recursive CTE stop condition for loops
https://dbfiddle.uk/?rdbms=postgres_13&fiddle=dfe8858352afad6411609d157d3fe85e
我想在雪花中做同样的事情,我该怎么做?我试图“移植”这个例子,但我不清楚数组部分应该如何转换为雪花?
我有下面的内容,returns 与您在 Postgres 中的示例的结果相同:
WITH RECURSIVE paths AS (
-- For simplicity assume node 1 is the start
-- we'll have two starting nodes for data = 1 and 2
SELECT DISTINCT
src as node
, data as data
, 0 as depth
, src::text as path
, false as is_cycle
, ARRAY_CONSTRUCT(src) as path_array
FROM edges
WHERE src IN ( 1,2)
UNION ALL
SELECT
edges.dst
, edges.data
, depth + 1
, paths.path || '->' || edges.dst::text
, ARRAY_CONTAINS(dst::variant, path_array)
, ARRAY_APPEND(path_array, dst)
FROM paths
JOIN edges
ON edges.src = paths.node
AND edges.data = paths.data
AND NOT is_cycle
)
SELECT * FROM paths;
但是,我必须删除递归部分中的 DISTINCT,因为它在 Snowflake 中是不允许的:
SQL compilation error: DISTINCT is not allowed in a CTEs recursive term.
我发现这个例子可以处理递归 CTE 中的循环: Recursive CTE stop condition for loops
https://dbfiddle.uk/?rdbms=postgres_13&fiddle=dfe8858352afad6411609d157d3fe85e
我想在雪花中做同样的事情,我该怎么做?我试图“移植”这个例子,但我不清楚数组部分应该如何转换为雪花?
我有下面的内容,returns 与您在 Postgres 中的示例的结果相同:
WITH RECURSIVE paths AS (
-- For simplicity assume node 1 is the start
-- we'll have two starting nodes for data = 1 and 2
SELECT DISTINCT
src as node
, data as data
, 0 as depth
, src::text as path
, false as is_cycle
, ARRAY_CONSTRUCT(src) as path_array
FROM edges
WHERE src IN ( 1,2)
UNION ALL
SELECT
edges.dst
, edges.data
, depth + 1
, paths.path || '->' || edges.dst::text
, ARRAY_CONTAINS(dst::variant, path_array)
, ARRAY_APPEND(path_array, dst)
FROM paths
JOIN edges
ON edges.src = paths.node
AND edges.data = paths.data
AND NOT is_cycle
)
SELECT * FROM paths;
但是,我必须删除递归部分中的 DISTINCT,因为它在 Snowflake 中是不允许的:
SQL compilation error: DISTINCT is not allowed in a CTEs recursive term.