Skip to content

Commit

Permalink
[fix](nereids) fix ptopN push down under multi winexprs with partial …
Browse files Browse the repository at this point in the history
…forbidden type (#44617)

Related PR: #38393

Problem Summary: In the previous pr which supporting multi win expr
ptopN pushdown, it handled partial forbidden type unexpectly and will
lead some case to push down the pTopN wrongly.
plan before fixing:

explain shape plan 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;

+------------------------------------------------------------------------------------+
| Explain String(Nereids Planner)                                                    |
+------------------------------------------------------------------------------------+
| PhysicalResultSink                                                                 |
| --PhysicalProject                                                                  |
| ----filter((rn <= 1) and (sw <= 1))                                                |
| ------PhysicalWindow                                                               |
| --------PhysicalQuickSort[MERGE_SORT]                                              |
| ----------PhysicalDistribute[DistributionSpecGather]                               |
| ------------PhysicalQuickSort[LOCAL_SORT]                                          |
| --------------PhysicalWindow                                                       |
| ----------------PhysicalQuickSort[LOCAL_SORT]                                      |
| ------------------PhysicalDistribute[DistributionSpecHash]                         |
| --------------------PhysicalPartitionTopN                                          |
| ----------------------PhysicalOlapScan[push_down_multi_predicate_through_window_t] |
+------------------------------------------------------------------------------------+

plan after fixing: 

explain shape plan 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;

+----------------------------------------------------------------------------------+
| Explain String(Nereids Planner)                                                  |
+----------------------------------------------------------------------------------+
| PhysicalResultSink                                                               |
| --PhysicalProject                                                                |
| ----filter((rn <= 1) and (sw <= 1))                                              |
| ------PhysicalWindow                                                             |
| --------PhysicalQuickSort[MERGE_SORT]                                            |
| ----------PhysicalDistribute[DistributionSpecGather]                             |
| ------------PhysicalQuickSort[LOCAL_SORT]                                        |
| --------------PhysicalWindow                                                     |
| ----------------PhysicalQuickSort[LOCAL_SORT]                                    |
| ------------------PhysicalDistribute[DistributionSpecHash]                       |
| --------------------PhysicalOlapScan[push_down_multi_predicate_through_window_t] |
+----------------------------------------------------------------------------------+
  • Loading branch information
xzj7019 authored and Your Name committed Nov 27, 2024
1 parent 4994841 commit 4697f59
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,20 @@ && 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);

// Check the window function name.
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.
Expand All @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions regression-test/data/nereids_syntax_p0/window_function.out
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,9 @@
\N
\N

-- !multi_winf1 --
1 c

-- !multi_winf2 --
1 35

Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
39 changes: 39 additions & 0 deletions regression-test/suites/nereids_syntax_p0/window_function.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
}

0 comments on commit 4697f59

Please sign in to comment.