diff --git a/src/gausskernel/optimizer/plan/createplan.cpp b/src/gausskernel/optimizer/plan/createplan.cpp index c326b70cd9fca77a9ddf0dfce4783c510d368728..d4b079f07781799efe284c9b67aef5945c285595 100755 --- a/src/gausskernel/optimizer/plan/createplan.cpp +++ b/src/gausskernel/optimizer/plan/createplan.cpp @@ -2699,6 +2699,11 @@ static Scan* create_indexscan_plan( qpqual = lappend(qpqual, rinfo); } + if (u_sess->opt_cxt.is_under_append_plan && tlist == NIL) { + Const *c = makeConst(INT4OID, -1, InvalidOid, -2, (Datum)0, true, false); + tlist = lappend(tlist, makeTargetEntry((Expr*)c, 1, NULL, true)); + } + /* Sort clauses into best execution order */ qpqual = order_qual_clauses(root, qpqual); diff --git a/src/test/regress/expected/test_union_all_orderby.out b/src/test/regress/expected/test_union_all_orderby.out index a0da822e95f211fd54bf7c13e5cc2ccffc58f734..05eb1fd43a2d0f2654fd38a971b5edce0d2388a0 100644 --- a/src/test/regress/expected/test_union_all_orderby.out +++ b/src/test/regress/expected/test_union_all_orderby.out @@ -41,3 +41,54 @@ insert into tb3 values(6); 7 (7 rows) +reset enable_union_all_subquery_orderby; +-- test union all with none targetlist +drop table if exists union_pseudo_tlist cascade; +NOTICE: table "union_pseudo_tlist" does not exist, skipping +create table union_pseudo_tlist(id int); +insert into union_pseudo_tlist values(1); +create index on union_pseudo_tlist(id); +set enable_seqscan=off; +select count(*) from union_pseudo_tlist where now() is not null and id is not null; + count +------- + 1 +(1 row) + +explain(costs off) select count(*) from (select * from union_pseudo_tlist union all select * from union_pseudo_tlist) where now() is not null; + QUERY PLAN +----------------------------------------------------------------------------------------------- + Aggregate + -> Result + One-Time Filter: (now() IS NOT NULL) + -> Append + -> Result + One-Time Filter: (now() IS NOT NULL) + -> Index Only Scan using union_pseudo_tlist_id_idx on union_pseudo_tlist + -> Result + One-Time Filter: (now() IS NOT NULL) + -> Index Only Scan using union_pseudo_tlist_id_idx on union_pseudo_tlist +(10 rows) + +select count(*) from (select * from union_pseudo_tlist union all select * from union_pseudo_tlist) where now() is not null; + count +------- + 2 +(1 row) + +select count(*) from (select * from union_pseudo_tlist union all select * from union_pseudo_tlist) where now() is not null and id is not null; + count +------- + 2 +(1 row) + +-- use pbe +prepare test_index_pseudo as select count(*) from (select * from union_pseudo_tlist union all select * from union_pseudo_tlist) where id in (1) and id=$1; +execute test_index_pseudo(1); + count +------- + 2 +(1 row) + +drop table if exists union_pseudo_tlist cascade; +reset enable_seqscan; diff --git a/src/test/regress/sql/test_union_all_orderby.sql b/src/test/regress/sql/test_union_all_orderby.sql index d239a68d290d43bf6bcb77c55f0c2942fef39377..490234776226eb0190005179185cd7b9e4d47ff5 100644 --- a/src/test/regress/sql/test_union_all_orderby.sql +++ b/src/test/regress/sql/test_union_all_orderby.sql @@ -12,4 +12,26 @@ insert into tb3 values(6); (select * from tb1 order by a) union all (select * from tb2 order by a); (select * from tb1 order by a) union all (select * from tb2 order by a desc); -(select * from tb1 order by a) union all (select * from tb2 order by a) union all (select * from tb3 order by a); \ No newline at end of file +(select * from tb1 order by a) union all (select * from tb2 order by a) union all (select * from tb3 order by a); + +reset enable_union_all_subquery_orderby; + +-- test union all with none targetlist +drop table if exists union_pseudo_tlist cascade; +create table union_pseudo_tlist(id int); +insert into union_pseudo_tlist values(1); +create index on union_pseudo_tlist(id); +set enable_seqscan=off; + +select count(*) from union_pseudo_tlist where now() is not null and id is not null; + +explain(costs off) select count(*) from (select * from union_pseudo_tlist union all select * from union_pseudo_tlist) where now() is not null; +select count(*) from (select * from union_pseudo_tlist union all select * from union_pseudo_tlist) where now() is not null; +select count(*) from (select * from union_pseudo_tlist union all select * from union_pseudo_tlist) where now() is not null and id is not null; + +-- use pbe +prepare test_index_pseudo as select count(*) from (select * from union_pseudo_tlist union all select * from union_pseudo_tlist) where id in (1) and id=$1; +execute test_index_pseudo(1); + +drop table if exists union_pseudo_tlist cascade; +reset enable_seqscan;