From cd70f1984e091833ab14c24cc48b3680f20c17ce Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 27 Nov 2024 14:15:08 +0800 Subject: [PATCH] branch-3.0: [fix](nereids) fix ptopN push down under multi winexprs with partial forbidden type #44617 (#44649) Cherry-picked from #44617 Co-authored-by: xzj7019 --- .../trees/plans/logical/LogicalWindow.java | 10 ++--- .../nereids_syntax_p0/window_function.out | 6 +++ ...sh_down_multi_filter_through_window.groovy | 20 ++++++++++ .../nereids_syntax_p0/window_function.groovy | 39 +++++++++++++++++++ 4 files changed, 70 insertions(+), 5 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalWindow.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalWindow.java index 6cd8389349a007..5ee70afbf5ff86 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalWindow.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalWindow.java @@ -218,7 +218,7 @@ && child(0).child(0) instanceof LogicalPartitionTopN)) { for (NamedExpression windowExpr : windowExpressions) { if (windowExpr == null || windowExpr.children().size() != 1 || !(windowExpr.child(0) instanceof WindowExpression)) { - continue; + return null; } WindowExpression windowFunc = (WindowExpression) windowExpr.child(0); @@ -226,12 +226,12 @@ && child(0).child(0) instanceof LogicalPartitionTopN)) { if (!(windowFunc.getFunction() instanceof RowNumber || windowFunc.getFunction() instanceof Rank || windowFunc.getFunction() instanceof DenseRank)) { - continue; + return null; } // Check the partition key and order key. if (windowFunc.getPartitionKeys().isEmpty() && windowFunc.getOrderKeys().isEmpty()) { - continue; + return null; } // Check the window type and window frame. @@ -240,10 +240,10 @@ && child(0).child(0) instanceof LogicalPartitionTopN)) { WindowFrame frame = windowFrame.get(); if (!(frame.getLeftBoundary().getFrameBoundType() == WindowFrame.FrameBoundType.UNBOUNDED_PRECEDING && frame.getRightBoundary().getFrameBoundType() == WindowFrame.FrameBoundType.CURRENT_ROW)) { - continue; + return null; } } else { - continue; + return null; } // Check filter conditions. diff --git a/regression-test/data/nereids_syntax_p0/window_function.out b/regression-test/data/nereids_syntax_p0/window_function.out index 4ec92fc61ad46c..38eba68274e18f 100644 --- a/regression-test/data/nereids_syntax_p0/window_function.out +++ b/regression-test/data/nereids_syntax_p0/window_function.out @@ -561,3 +561,9 @@ \N \N +-- !multi_winf1 -- +1 c + +-- !multi_winf2 -- +1 35 + diff --git a/regression-test/suites/nereids_rules_p0/push_down_filter_through_window/push_down_multi_filter_through_window.groovy b/regression-test/suites/nereids_rules_p0/push_down_filter_through_window/push_down_multi_filter_through_window.groovy index d808d30f8ebdfe..015a6e7fae7d47 100644 --- a/regression-test/suites/nereids_rules_p0/push_down_filter_through_window/push_down_multi_filter_through_window.groovy +++ b/regression-test/suites/nereids_rules_p0/push_down_filter_through_window/push_down_multi_filter_through_window.groovy @@ -157,4 +157,24 @@ suite("push_down_multi_filter_through_window") { sql ("select * from (select row_number() over(partition by c1, c2 order by c3) as rn, rank() over(partition by c1 order by c3) as rk from push_down_multi_predicate_through_window_t) t where rn <= 1 or rk <= 1;") notContains "VPartitionTopN" } + + explain { + sql ("select * from (select row_number() over(partition by c1, c2 order by c3) as rn, sum(c2) over(order by c2 range between unbounded preceding and unbounded following) as sw from push_down_multi_predicate_through_window_t) t where rn <= 1 and sw <= 1;") + notContains "VPartitionTopN" + } + + explain { + sql ("select * from (select sum(c2) over(order by c2 range between unbounded preceding and unbounded following) as sw, row_number() over(partition by c1, c2 order by c3) as rn from push_down_multi_predicate_through_window_t) t where rn <= 1 and sw <= 1;") + notContains "VPartitionTopN" + } + + explain { + sql ("select * from (select row_number() over(partition by c1, c2 order by c3 rows between unbounded preceding and current row) as rn, sum(c2) over(order by c2) as sw from push_down_multi_predicate_through_window_t) t where rn <= 1 and sw <= 1;") + notContains "VPartitionTopN" + } + + explain { + sql ("select * from (select sum(c2) over(order by c2) as sw, row_number() over(partition by c1, c2 order by c3 rows between unbounded preceding and current row) as rn from push_down_multi_predicate_through_window_t) t where rn <= 1 and sw <= 1;") + notContains "VPartitionTopN" + } } diff --git a/regression-test/suites/nereids_syntax_p0/window_function.groovy b/regression-test/suites/nereids_syntax_p0/window_function.groovy index 9a427d1019887d..31cb425fcf7a44 100644 --- a/regression-test/suites/nereids_syntax_p0/window_function.groovy +++ b/regression-test/suites/nereids_syntax_p0/window_function.groovy @@ -240,4 +240,43 @@ suite("window_function") { """ qt_sql """ select LAST_VALUE(col_tinyint_undef_signed_not_null) over (partition by col_double_undef_signed_not_null, col_int_undef_signed, (col_float_undef_signed_not_null - col_int_undef_signed), round_bankers(col_int_undef_signed) order by pk rows between unbounded preceding and 4 preceding) AS col_alias56089 from table_200_undef_partitions2_keys3_properties4_distributed_by53 order by col_alias56089; """ + + order_qt_multi_winf1 """ + select * + from ( + select + row_number() over(partition by c1 order by c2) rn, + lead(c2, 2, '') over(partition by c1 order by c2) + from ( + select 1 as c1, 'a' as c2 + union all + select 1 as c1, 'b' as c2 + union all + select 1 as c1, 'c' as c2 + union all + select 1 as c1, 'd' as c2 + union all + select 1 as c1, 'e' as c2 + )t + )a where rn=1 + """ + order_qt_multi_winf2 """ + select * + from ( + select + row_number() over(partition by c1 order by c2) rn, + sum(c2) over(order by c2 range between unbounded preceding and unbounded following) + from ( + select 1 as c1, 5 as c2 + union all + select 1 as c1, 6 as c2 + union all + select 1 as c1, 7 as c2 + union all + select 1 as c1, 8 as c2 + union all + select 1 as c1, 9 as c2 + )t + )a where rn=1 + """ }