0

我发现这个例子可以处理递归 CTE 中的循环:循环的 递归 CTE 停止条件

https://dbfiddle.uk/?rdbms=postgres_13&fiddle=dfe8858352afad6411609d157d3fe85e

我想在雪花中做同样的事情,我该怎么做?我试图“移植”这个例子,但我不清楚数组部分应该如何转换为雪花?

4

1 回答 1

0

我有下面,它返回与 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,因为雪花中不允许这样做:

SQL compilation error: DISTINCT is not allowed in a CTEs recursive term.
于 2021-09-07T22:42:46.963 回答