diff --git a/src/common/backend/parser/parse_startwith.cpp b/src/common/backend/parser/parse_startwith.cpp index 551321563..37cdb30ad 100644 --- a/src/common/backend/parser/parse_startwith.cpp +++ b/src/common/backend/parser/parse_startwith.cpp @@ -331,6 +331,11 @@ void AddStartWithTargetRelInfo(ParseState* pstate, Node* relNode, startInfo->aliasname = sub->alias->aliasname; } + SelectStmt *substmt = (SelectStmt*)sub->subquery; + if (substmt->withClause == NULL) { + substmt->withClause = (WithClause*)copyObject(pstate->origin_with); + } + startInfo->rte = rte; startInfo->rtr = rtr; startInfo->rtekind = rte->rtekind; diff --git a/src/test/regress/expected/sw_bugfix-2.out b/src/test/regress/expected/sw_bugfix-2.out index fc9981c42..7960395d6 100755 --- a/src/test/regress/expected/sw_bugfix-2.out +++ b/src/test/regress/expected/sw_bugfix-2.out @@ -1415,3 +1415,13 @@ LIMIT 169; ---- (0 rows) +create table t123(id int, lid int, name text); +insert into t123 values(1,null,'A'),(2,1,'B'),(3,2,'C'); +with t2 as (select * from t123 where id!=10) select level,t.* from (select * from t2 where id!=10 order by id) t start with t.id=2 connect by prior t.id=t.lid; + level | id | lid | name +-------+----+-----+------ + 1 | 2 | 1 | B + 2 | 3 | 2 | C +(2 rows) + +drop table t123; diff --git a/src/test/regress/sql/sw_bugfix-2.sql b/src/test/regress/sql/sw_bugfix-2.sql index c23ed8ae9..3bf19d872 100644 --- a/src/test/regress/sql/sw_bugfix-2.sql +++ b/src/test/regress/sql/sw_bugfix-2.sql @@ -501,3 +501,8 @@ WHERE true CONNECT BY EXISTS ( test_hcb_ptb as ref_7 ) LIMIT 169; + +create table t123(id int, lid int, name text); +insert into t123 values(1,null,'A'),(2,1,'B'),(3,2,'C'); +with t2 as (select * from t123 where id!=10) select level,t.* from (select * from t2 where id!=10 order by id) t start with t.id=2 connect by prior t.id=t.lid; +drop table t123;