So, I was reviewing two recent bugs reports  19922/19923  [0] & [1]
and fix from [2]. And I learned how CTE with CYCLE clause is
implemented in PostgreSQL: it goes thru rewrite subsystem to make
classing CTE for cycle detection, just like doc says [3]. To better
understand how [2] and rewriteSearchAndCycle works I made this example
(from attached 0002 test):

create temp table cycle_window_edge (src int, dst int);
insert into cycle_window_edge values (1, 2), (2, 1), (2, 3), (3, 4);

with recursive walk(id, depth) as (

 values (1, 0::bigint)
 union all
 select e.dst, w.depth + 1
 from walk w join cycle_window_edge e on e.src = w.id

) cycle id set is_cycle using path
select depth, id, is_cycle from walk order by depth, id;

On master, rewriteSearchAndCycle will rewrite this to be efficiently
this (internally it will be AST not SQL, so SQL is just for
explanation)


WITH RECURSIVE walk(id, depth, n, is_cycle, path) AS (
    VALUES (1, 0, 0::bigint, false, ARRAY[1])

    UNION ALL

    SELECT c.id, c.depth, c.n, c.is_cycle, c.path
    FROM (
        SELECT e.dst AS id,
               w.depth + 1 AS depth,
               count(*) OVER () AS n,
               e.dst = ANY(w.path) AS is_cycle,
               w.path || e.dst AS path,
               w.is_cycle AS parent_is_cycle
        FROM walk w
        JOIN cycle_window_edge e ON e.src = w.id
    ) AS c
    WHERE c.parent_is_cycle <> true
)
SELECT depth, id, n, is_cycle
FROM walk
ORDER BY depth, id;

-- not correct result (last row n == 2, counted edge 1->2 and than
filtering out)

 depth | id | n | is_cycle
-------+----+---+----------
     0 |  1 | 0 | f
     1 |  2 | 1 | f
     2 |  1 | 2 | t
     2 |  3 | 2 | f
     3 |  4 | 2 | f
(5 rows)



So, our recursive step goes into subquery and we then do outer
filtering by parent_is_cycle var.
But turns out, this is not always correct rewriting, cause we first
collect all rows from subquery and only then filter.  If we use window
function in recursive step, parent_is_cycle=true rows will be counted
in the window function column for surviving rows.

So, dont make subquery?

After 0001, rewrite would be like ths

WITH RECURSIVE walk(id, depth, n, is_cycle, path) AS (
    VALUES (1, 0, 0::bigint, false, ARRAY[1])

    UNION ALL

    SELECT e.dst,
           w.depth + 1,
           count(*) OVER () AS n,
           e.dst = ANY(w.path) AS is_cycle,
           w.path || e.dst AS path
    FROM walk w
    JOIN cycle_window_edge e ON e.src = w.id
    WHERE w.is_cycle <> true
)
SELECT depth, id, n, is_cycle
FROM walk
ORDER BY depth, id;

-- correct result

 depth | id | n | is_cycle
-------+----+---+----------
     0 |  1 | 0 | f
     1 |  2 | 1 | f
     2 |  1 | 2 | t
     2 |  3 | 2 | f
     3 |  4 | 1 | f
(5 rows)


which will preserve window functions semantic


[0]https://www.postgresql.org/message-id/[email protected]
[1] https://www.postgresql.org/message-id/[email protected]
[2] 
https://github.com/postgres/postgres/commit/d27418375d64663c920d81b0814d389a33d621db
[3] https://www.postgresql.org/docs/current/queries-with.html

-- 
Best regards,
Kirill Reshke

Attachment: 0001-Fix-CYCLE-filtering-before-window-functions.patch
Description: Binary data

Attachment: 0002-Test-CYCLE-filtering-before-window-functions.patch
Description: Binary data

Reply via email to