Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.WindowExpression;
import org.apache.doris.nereids.trees.expressions.functions.BoundFunction;
import org.apache.doris.nereids.trees.expressions.functions.agg.Count;
import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
import org.apache.doris.nereids.trees.plans.logical.LogicalWindow;
import org.apache.doris.nereids.util.TypeCoercionUtils;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -87,11 +89,13 @@ private Plan simplify(MatchingContext<LogicalWindow<Plan>> ctx) {
if (function instanceof BoundFunction) {
BoundFunction boundFunction = (BoundFunction) function;
String name = ((BoundFunction) function).getName();
if ((name.equals(COUNT) && boundFunction.child(0).notNullable())
if ((name.equals(COUNT) && checkCount((Count) boundFunction))
|| REWRRITE_TO_CONST_WINDOW_FUNCTIONS.contains(name)) {
projectionsBuilder.add(new Alias(alias.getExprId(), new TinyIntLiteral((byte) 1), alias.getName()));
} else if (REWRRITE_TO_SLOT_WINDOW_FUNCTIONS.contains(name)) {
projectionsBuilder.add(new Alias(alias.getExprId(), boundFunction.child(0), alias.getName()));
projectionsBuilder.add(new Alias(alias.getExprId(),
TypeCoercionUtils.castIfNotSameType(boundFunction.child(0), boundFunction.getDataType()),
alias.getName()));
} else {
remainWindowExpression.add(expr);
}
Expand Down Expand Up @@ -120,4 +124,8 @@ private Plan simplify(MatchingContext<LogicalWindow<Plan>> ctx) {
window.child(0)));
}
}

private boolean checkCount(Count count) {
return count.isCountStar() || count.child(0).notNullable();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,28 @@
-- !select_avg --
\N \N \N
\N \N \N
1 1 1
1 1 1
2 2 2
3 3 3
3 3 3
4 4 4
5 5 5
5 5 5
7 7 7
1 1.0 1.0
1 1.0 1.0
2 2.0 2.0
3 3.0 3.0
3 3.0 3.0
4 4.0 4.0
5 5.0 5.0
5 5.0 5.0
7 7.0 7.0

-- !more_than_pk --
\N \N \N
\N \N \N
1 1 1
1 1 1
2 2 2
3 3 3
3 3 3
4 4 4
5 5 5
5 5 5
7 7 7
1 1.0 1.0
1 1.0 1.0
2 2.0 2.0
3 3.0 3.0
3 3.0 3.0
4 4.0 4.0
5 5.0 5.0
5 5.0 5.0
7 7.0 7.0

-- !select_last_value_shape --
PhysicalResultSink
Expand All @@ -163,18 +163,31 @@ PhysicalResultSink
------filter((mal_test_simplify_window.__DORIS_DELETE_SIGN__ = 0))
--------PhysicalOlapScan[mal_test_simplify_window]

-- !select_count_star_col1 --
\N 1 1
1 1 1
1 1 1
2 1 1
2 1 1
2 1 1
3 1 1
3 1 1
4 1 1
6 1 1
6 1 1

-- !select_upper_plan_use_all_rewrite --
\N \N
\N \N
1 1
1 1
2 2
3 3
3 3
4 4
5 5
5 5
7 7
1 1.0
1 1.0
2 2.0
3 3.0
3 3.0
4 4.0
5 5.0
5 5.0
7 7.0

-- !select_upper_plan_use_rewrite_and_not_rewrite --
\N \N \N
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ suite("simplify_window_expression") {
explain shape plan
select b, avg(b) over (partition by a,b,c) c1, avg(b) over (partition by a,b,c order by b) c2
from mal_test_simplify_window"""
qt_select_count_star_col1 """
select a,count() over (partition by a,b) c1, count() over (partition by a,b order by a) c2
from mal_test_simplify_window order by 1,2,3;"""

qt_select_upper_plan_use_all_rewrite """
select b, c1 from (select b,avg(b) over (partition by a,b) c1
Expand Down