From 363f311f55c5645a94dc0ce38a4f1f3843941a46 Mon Sep 17 00:00:00 2001
From: root <root@localhost>
Date: Wed, 25 Nov 2026 03:07:20 +0000
Subject: [PATCH 2/2] Test CYCLE filtering before window functions

---
 src/test/regress/expected/with.out | 20 ++++++++++++++++++++
 src/test/regress/sql/with.sql      | 14 ++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/src/test/regress/expected/with.out b/src/test/regress/expected/with.out
index e11303f14e6..1359fe145eb 100644
--- a/src/test/regress/expected/with.out
+++ b/src/test/regress/expected/with.out
@@ -1554,6 +1554,26 @@ select * from t;
  1 | t | {(1),(1)}
 (2 rows)
 
+-- cycle parents must not affect windows in the recursive term
+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, n) as (
+	values (1, 0, 0::bigint)
+	union all
+	select e.dst, w.depth + 1, count(*) over ()
+	from walk w join cycle_window_edge e on e.src = w.id
+) cycle id set is_cycle using path
+select depth, id, n, is_cycle from walk order by depth, id;
+ 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)
+
+drop table cycle_window_edge;
 -- various syntax errors
 with recursive search_graph(f, t, label) as (
 	select * from graph g
diff --git a/src/test/regress/sql/with.sql b/src/test/regress/sql/with.sql
index 0bcd9fd4f1c..33823d64e01 100644
--- a/src/test/regress/sql/with.sql
+++ b/src/test/regress/sql/with.sql
@@ -746,6 +746,20 @@ with recursive t(x) as (
 ) cycle x set m using p
 select * from t;
 
+-- cycle parents must not affect windows in the recursive term
+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, n) as (
+	values (1, 0, 0::bigint)
+	union all
+	select e.dst, w.depth + 1, count(*) over ()
+	from walk w join cycle_window_edge e on e.src = w.id
+) cycle id set is_cycle using path
+select depth, id, n, is_cycle from walk order by depth, id;
+
+drop table cycle_window_edge;
+
 -- various syntax errors
 with recursive search_graph(f, t, label) as (
 	select * from graph g
-- 
2.43.0

