diff --git a/src/common/backend/parser/gram.y b/src/common/backend/parser/gram.y index ac5ea6b18..b2aab8c1a 100644 --- a/src/common/backend/parser/gram.y +++ b/src/common/backend/parser/gram.y @@ -21307,6 +21307,41 @@ select_no_parens: yyscanner); $$ = $2; } + | with_clause select_clause siblings_clause + { + SelectStmt* stmt = (SelectStmt *) $2; + insertSelectOptions((SelectStmt *) $2, NIL, NIL, + NULL, NULL, $1, + yyscanner); + StartWithClause* swc = (StartWithClause*) stmt->startWithClause; + if (swc == NULL) { + ereport(errstate, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("order siblings by clause can only be used on start-with qualifed relations"), + parser_errposition(@2))); + } else { + swc->siblingsOrderBy = $3; + } + $$ = $2; + } + + | with_clause select_clause siblings_clause sort_clause + { + SelectStmt* stmt = (SelectStmt *) $2; + insertSelectOptions((SelectStmt *) $2, $4, NIL, + NULL, NULL, $1, + yyscanner); + StartWithClause* swc = (StartWithClause*) stmt->startWithClause; + if (swc == NULL) { + ereport(errstate, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("order siblings by clause can only be used on start-with qualifed relations"), + parser_errposition(@2))); + } else { + swc->siblingsOrderBy = $3; + } + $$ = $2; + } | with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit opt_into_clause { FilterStartWithUseCases((SelectStmt *) $2, $4, yyscanner, @4); diff --git a/src/test/regress/expected/sw_bugfix-2.out b/src/test/regress/expected/sw_bugfix-2.out index 4c75cb3b3..24d3ec6bd 100755 --- a/src/test/regress/expected/sw_bugfix-2.out +++ b/src/test/regress/expected/sw_bugfix-2.out @@ -580,6 +580,14 @@ select *, connect_by_iscycle from t1 start with c1=1 connect by nocycle prior c1 1 | | 1 | 0 (3 rows) +with cte1 as (select * from t1) select *, connect_by_iscycle from cte1 start with c1=1 connect by nocycle prior c1=c2 order siblings by 1,2 nulls last; + c1 | c2 | c3 | connect_by_iscycle +----+----+----+-------------------- + 1 | 1 | 1 | 0 + 1 | 1 | 1 | 0 + 1 | | 1 | 0 +(3 rows) + delete from t1 where c2 is null; select *, connect_by_iscycle from t1 start with c1<3 connect by nocycle prior c1