diff --git a/src/bin/gs_guc/cluster_guc.conf b/src/bin/gs_guc/cluster_guc.conf index 986e134d3..c59516547 100755 --- a/src/bin/gs_guc/cluster_guc.conf +++ b/src/bin/gs_guc/cluster_guc.conf @@ -115,6 +115,7 @@ commit_siblings|int|0,1000|NULL|NULL| config_file|string|0,0|NULL|NULL| connection_alarm_rate|real|0,1|NULL|NULL| constraint_exclusion|enum|partition,on,off,true,false,yes,no,1,0|NULL|NULL| +enable_union_all_subquery_orderby|bool|0,0|NULL|NULL| convert_string_to_digit|bool|0,0|NULL|Please don't modify this parameter which will change the type conversion rule and may lead to unpredictable behavior!| cost_param|int|0,2147483647|NULL|NULL| cpu_collect_timer|int|1,2147483647|NULL|NULL| diff --git a/src/common/backend/utils/misc/guc/guc_sql.cpp b/src/common/backend/utils/misc/guc/guc_sql.cpp index eea75db5a..a4d460d8d 100755 --- a/src/common/backend/utils/misc/guc/guc_sql.cpp +++ b/src/common/backend/utils/misc/guc/guc_sql.cpp @@ -473,6 +473,17 @@ static void InitSqlConfigureNamesBool() NULL, NULL, NULL}, + {{"enable_union_all_subquery_orderby", + PGC_USERSET, + NODE_SINGLENODE, + QUERY_TUNING_METHOD, + gettext_noop("Enable union all to order subquery."), + NULL}, + &u_sess->attr.attr_sql.enable_union_all_subquery_orderby, + false, + NULL, + NULL, + NULL}, {{"enable_global_stats", PGC_SUSET, NODE_ALL, diff --git a/src/gausskernel/optimizer/prep/prepnonjointree.cpp b/src/gausskernel/optimizer/prep/prepnonjointree.cpp index 505a4e7ba..328cdfe51 100755 --- a/src/gausskernel/optimizer/prep/prepnonjointree.cpp +++ b/src/gausskernel/optimizer/prep/prepnonjointree.cpp @@ -2178,9 +2178,12 @@ static void reduce_orderby_recurse(Query* query, Node* jtnode, bool reduce) } } else if (IsA(jtnode, SetOperationStmt)) { SetOperationStmt* op = (SetOperationStmt*)jtnode; - - reduce_orderby_recurse(query, op->larg, true); - reduce_orderby_recurse(query, op->rarg, true); + bool need_reduce = true; + if (op->op == SETOP_UNION && op->all && u_sess->attr.attr_sql.enable_union_all_subquery_orderby) { + need_reduce = reduce; + } + reduce_orderby_recurse(query, op->larg, need_reduce); + reduce_orderby_recurse(query, op->rarg, need_reduce); } return; @@ -2243,8 +2246,13 @@ void reduce_orderby(Query* query, bool reduce) /* If there has setop, it should optimize orderby clause. */ if (query->setOperations) { - reduce_orderby_recurse(query, ((SetOperationStmt*)query->setOperations)->larg, true); - reduce_orderby_recurse(query, ((SetOperationStmt*)query->setOperations)->rarg, true); + bool need_reduce = true; + SetOperationStmt *setop_stmt = (SetOperationStmt *)query->setOperations; + if (setop_stmt->op == SETOP_UNION && setop_stmt->all && u_sess->attr.attr_sql.enable_union_all_subquery_orderby) { + need_reduce = reduce; + } + reduce_orderby_recurse(query, ((SetOperationStmt*)query->setOperations)->larg, need_reduce); + reduce_orderby_recurse(query, ((SetOperationStmt*)query->setOperations)->rarg, need_reduce); } } diff --git a/src/include/knl/knl_guc/knl_session_attr_sql.h b/src/include/knl/knl_guc/knl_session_attr_sql.h index c5cf08952..74e69d666 100644 --- a/src/include/knl/knl_guc/knl_session_attr_sql.h +++ b/src/include/knl/knl_guc/knl_session_attr_sql.h @@ -63,6 +63,7 @@ typedef struct knl_session_attr_sql { bool enable_indexonlyscan; bool enable_bitmapscan; bool force_bitmapand; + bool enable_union_all_subquery_orderby; bool enable_parallel_ddl; bool enable_tidscan; bool enable_sort; diff --git a/src/test/regress/expected/test_union_all_orderby.out b/src/test/regress/expected/test_union_all_orderby.out new file mode 100644 index 000000000..a0da822e9 --- /dev/null +++ b/src/test/regress/expected/test_union_all_orderby.out @@ -0,0 +1,43 @@ +set enable_union_all_subquery_orderby=on; +create table tb1(a int); +insert into tb1 values(1); +insert into tb1 values(3); +insert into tb1 values(2); +create table tb2(a int); +insert into tb2 values(5); +insert into tb2 values(4); +create table tb3(a int); +insert into tb3 values(7); +insert into tb3 values(6); +(select * from tb1 order by a) union all (select * from tb2 order by a); + a +--- + 1 + 2 + 3 + 4 + 5 +(5 rows) + +(select * from tb1 order by a) union all (select * from tb2 order by a desc); + a +--- + 1 + 2 + 3 + 5 + 4 +(5 rows) + +(select * from tb1 order by a) union all (select * from tb2 order by a) union all (select * from tb3 order by a); + a +--- + 1 + 2 + 3 + 4 + 5 + 6 + 7 +(7 rows) + diff --git a/src/test/regress/output/recovery_2pc_tools.source b/src/test/regress/output/recovery_2pc_tools.source index c491ca40a..63bf34624 100644 --- a/src/test/regress/output/recovery_2pc_tools.source +++ b/src/test/regress/output/recovery_2pc_tools.source @@ -331,6 +331,7 @@ select name,vartype,unit,min_val,max_val from pg_settings where name <> 'qunit_c enable_tde | bool | | | enable_thread_pool | bool | | | enable_tidscan | bool | | | + enable_union_all_subquery_orderby | bool | | | enable_upgrade_merge_lock_mode | bool | | | enable_user_metric_persistent | bool | | | enable_ustore | bool | | | diff --git a/src/test/regress/parallel_schedule0 b/src/test/regress/parallel_schedule0 index f7f9846b4..885c81222 100644 --- a/src/test/regress/parallel_schedule0 +++ b/src/test/regress/parallel_schedule0 @@ -905,7 +905,7 @@ test: vec_sonic_hashjoin_number_nospill test: timeout test: dml test: hashfilter hashfilter_1 -test: reduce_orderby +test: reduce_orderby test_union_all_orderby #test: backtrace_log #test: bulkload_start test: bulkload_parallel_test_2 bulkload_parallel_test_3 @@ -1098,3 +1098,4 @@ test: user_host_test test: enable_expr_fusion_flatten # test for on update timestamp and generated column test: on_update_session1 on_update_session2 + diff --git a/src/test/regress/parallel_schedule0C b/src/test/regress/parallel_schedule0C index 3bcbb5177..a7ef5ba08 100644 --- a/src/test/regress/parallel_schedule0C +++ b/src/test/regress/parallel_schedule0C @@ -130,7 +130,7 @@ test: mysql_function_clearup cost_model test: base_update test: seqscan_fusion -test: union_null_01 fulljoin_rewrite +test: union_null_01 fulljoin_rewrite test_union_all_orderby # var selectivity test: var_eq_const_selectivity @@ -183,4 +183,4 @@ test: slow_sql test: user_host_test # test for new_expr_by_flatten -test: enable_expr_fusion_flatten \ No newline at end of file +test: enable_expr_fusion_flatten diff --git a/src/test/regress/sql/test_union_all_orderby.sql b/src/test/regress/sql/test_union_all_orderby.sql new file mode 100644 index 000000000..d239a68d2 --- /dev/null +++ b/src/test/regress/sql/test_union_all_orderby.sql @@ -0,0 +1,15 @@ +set enable_union_all_subquery_orderby=on; +create table tb1(a int); +insert into tb1 values(1); +insert into tb1 values(3); +insert into tb1 values(2); +create table tb2(a int); +insert into tb2 values(5); +insert into tb2 values(4); +create table tb3(a int); +insert into tb3 values(7); +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