From 4d0c1c3c2f406655d0c10dcc8ad45bbb848b34e8 Mon Sep 17 00:00:00 2001 From: laishenghao Date: Wed, 26 Mar 2025 17:11:19 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3union=20all=E5=87=BA=E7=8E=B0?= =?UTF-8?q?core=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gausskernel/optimizer/plan/createplan.cpp | 5 ++ .../expected/test_union_all_orderby.out | 51 +++++++++++++++++++ .../regress/sql/test_union_all_orderby.sql | 24 ++++++++- 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/gausskernel/optimizer/plan/createplan.cpp b/src/gausskernel/optimizer/plan/createplan.cpp index c326b70cd9..d4b079f077 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 a0da822e95..05eb1fd43a 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 d239a68d29..4902347762 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; -- Gitee