support order siblings by in cte base table

This commit is contained in:
Zongtian Hou
2023-01-31 12:06:22 +08:00
parent 6d4cfc8074
commit db19171daf
3 changed files with 44 additions and 0 deletions

View File

@ -21307,6 +21307,41 @@ select_no_parens:
yyscanner); yyscanner);
$$ = $2; $$ = $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 | with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit opt_into_clause
{ {
FilterStartWithUseCases((SelectStmt *) $2, $4, yyscanner, @4); FilterStartWithUseCases((SelectStmt *) $2, $4, yyscanner, @4);

View File

@ -580,6 +580,14 @@ select *, connect_by_iscycle from t1 start with c1=1 connect by nocycle prior c1
1 | | 1 | 0 1 | | 1 | 0
(3 rows) (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; delete from t1 where c2 is null;
select *, connect_by_iscycle from t1 start with c1<3 connect by nocycle prior c1<c2 order siblings by NLSSORT (c1, ' NLS_SORT = generic_m_ci '); select *, connect_by_iscycle from t1 start with c1<3 connect by nocycle prior c1<c2 order siblings by NLSSORT (c1, ' NLS_SORT = generic_m_ci ');
c1 | c2 | c3 | connect_by_iscycle c1 | c2 | c3 | connect_by_iscycle

View File

@ -228,6 +228,7 @@ select *, connect_by_iscycle from t1 start with c1=1 connect by nocycle prior c1
insert into t1 values(1,NULL,1); insert into t1 values(1,NULL,1);
select *, connect_by_iscycle from t1 start with c1=1 connect by nocycle prior c1=c2 order siblings by 1,2 nulls first; select *, connect_by_iscycle from t1 start with c1=1 connect by nocycle prior c1=c2 order siblings by 1,2 nulls first;
select *, connect_by_iscycle from t1 start with c1=1 connect by nocycle prior c1=c2 order siblings by 1,2 nulls last; select *, connect_by_iscycle from t1 start with c1=1 connect by nocycle prior c1=c2 order siblings by 1,2 nulls last;
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;
delete from t1 where c2 is null; delete from t1 where c2 is null;
select *, connect_by_iscycle from t1 start with c1<3 connect by nocycle prior c1<c2 order siblings by NLSSORT (c1, ' NLS_SORT = generic_m_ci '); select *, connect_by_iscycle from t1 start with c1<3 connect by nocycle prior c1<c2 order siblings by NLSSORT (c1, ' NLS_SORT = generic_m_ci ');