union all 支持子语句排序

This commit is contained in:
Julong-Li
2023-07-25 10:36:43 +08:00
parent f265b5d0ad
commit 1bb8db709f
9 changed files with 89 additions and 8 deletions

View File

@ -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|

View File

@ -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,

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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)

View File

@ -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 | | |

View File

@ -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

View File

@ -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
test: enable_expr_fusion_flatten

View File

@ -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);