diff --git a/src/gausskernel/optimizer/plan/planstartwith.cpp b/src/gausskernel/optimizer/plan/planstartwith.cpp index 274e999ad..0788d3f14 100644 --- a/src/gausskernel/optimizer/plan/planstartwith.cpp +++ b/src/gausskernel/optimizer/plan/planstartwith.cpp @@ -555,6 +555,16 @@ static char *CheckAndFixSiblingsColName(PlannerInfo *root, Plan *basePlan, errmsg("expression with none-var in order siblings is not supported"))); } + foreach (lc, vars) { + Var* var = (Var *)lfirst(lc); + RangeTblEntry *rte = root->simple_rte_array[var->varno]; + char *raw_cte_alias = (char *)strVal(list_nth(rte->eref->colnames, var->varattno - 1)); + if (raw_cte_alias != NULL && IsPseudoReturnColumn(raw_cte_alias)) { + ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("Not support refer startwith Pseudo column in order siblings by."))); + } + } + /* do not support multi-column refs specified as order sibling's sort entry */ if (list_length(vars) > 1) { ereport(WARNING, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), @@ -565,7 +575,12 @@ static char *CheckAndFixSiblingsColName(PlannerInfo *root, Plan *basePlan, RangeTblEntry *rte = root->simple_rte_array[var->varno]; char *raw_cte_alias = (char *)strVal(list_nth(rte->eref->colnames, var->varattno - 1)); resultColName = strrchr(raw_cte_alias, '@'); - resultColName += 1; /* fix '@' offset */ + if (resultColName != NULL) { + resultColName += 1; /* fix '@' offset */ + } else { + ereport(ERROR, (errmodule(MOD_OPT_PLANNER), + errmsg("Invalid column name %s in order siblings is found.", raw_cte_alias))); + } return resultColName; } diff --git a/src/test/regress/expected/sw_bugfix-2.out b/src/test/regress/expected/sw_bugfix-2.out index 99cd9f242..76c218f8b 100755 --- a/src/test/regress/expected/sw_bugfix-2.out +++ b/src/test/regress/expected/sw_bugfix-2.out @@ -2342,3 +2342,20 @@ explain select * from sw_tb_3 where exists (select * from sw_tb_1, sw_tb_2 where drop table sw_tb_1; drop table sw_tb_2; drop table sw_tb_3; +-- test ORDER SIBLINGS BY clause which contains an expression or alias with a pseudocolumn +drop table if exists hier; +create table hier(parent varchar(30),child varchar(30)); +insert into hier values(null,'Asia'); +insert into hier values('Asia','China'); +insert into hier values('Asia','Japan'); +insert into hier values('Asia','ENGLAND'); +insert into hier values('Asia','HONGKONG'); +insert into hier values('China','BEIJING'); +insert into hier values('China','SHANGHAI'); +insert into hier values('China','AK47'); +insert into hier values('China','天津'); +select child, level, lpad(' ', level*3, ' ')||child c1 from hier start with parent is null connect by prior child = parent ORDER SIBLINGS BY c1; +ERROR: Not support refer startwith Pseudo column in order siblings by. +select child, level, lpad(' ', level*3, ' ')||child c1, level c2 from hier start with parent is null connect by prior child = parent ORDER SIBLINGS BY c2; +ERROR: Not support refer startwith Pseudo column in order siblings by. +drop table hier; \ No newline at end of file diff --git a/src/test/regress/sql/sw_bugfix-2.sql b/src/test/regress/sql/sw_bugfix-2.sql index d98ff7900..ec2127801 100644 --- a/src/test/regress/sql/sw_bugfix-2.sql +++ b/src/test/regress/sql/sw_bugfix-2.sql @@ -865,4 +865,20 @@ explain select * from sw_tb_1,sw_tb_2 where (sw_tb_1.a+sw_tb_1.b=sw_tb_2.b or sw explain select * from sw_tb_3 where exists (select * from sw_tb_1, sw_tb_2 where sw_tb_1.a + sw_tb_2.a = sw_tb_3.a connect by level < 2); drop table sw_tb_1; drop table sw_tb_2; -drop table sw_tb_3; \ No newline at end of file +drop table sw_tb_3; + +-- test ORDER SIBLINGS BY clause which contains an expression or alias with a pseudocolumn +drop table if exists hier; +create table hier(parent varchar(30),child varchar(30)); +insert into hier values(null,'Asia'); +insert into hier values('Asia','China'); +insert into hier values('Asia','Japan'); +insert into hier values('Asia','ENGLAND'); +insert into hier values('Asia','HONGKONG'); +insert into hier values('China','BEIJING'); +insert into hier values('China','SHANGHAI'); +insert into hier values('China','AK47'); +insert into hier values('China','天津'); +select child, level, lpad(' ', level*3, ' ')||child c1 from hier start with parent is null connect by prior child = parent ORDER SIBLINGS BY c1; +select child, level, lpad(' ', level*3, ' ')||child c1, level c2 from hier start with parent is null connect by prior child = parent ORDER SIBLINGS BY c2; +drop table hier; \ No newline at end of file