Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](nereids)remove literal partition by and order by expression in window function #27214

Merged
merged 1 commit into from
Nov 18, 2023
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 @@ -19,6 +19,7 @@

import org.apache.doris.nereids.rules.Rule;
import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.rules.rewrite.NormalizeToSlot.NormalizeToSlotContext;
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
Expand All @@ -34,6 +35,7 @@

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand All @@ -44,7 +46,22 @@ public class ExtractAndNormalizeWindowExpression extends OneRewriteRuleFactory i
@Override
public Rule build() {
return logicalProject().when(project -> containsWindowExpression(project.getProjects())).then(project -> {
List<NamedExpression> outputs = project.getProjects();
List<NamedExpression> outputs =
ExpressionUtils.rewriteDownShortCircuit(project.getProjects(), output -> {
if (output instanceof WindowExpression) {
// remove literal partition by and order by keys
WindowExpression windowExpression = (WindowExpression) output;
return windowExpression.withPartitionKeysOrderKeys(
windowExpression.getPartitionKeys().stream()
.filter(expression -> !expression.isConstant())
.collect(Collectors.toList()),
windowExpression.getOrderKeys().stream()
.filter(orderExpression -> !orderExpression
.getOrderKey().getExpr().isConstant())
.collect(Collectors.toList()));
}
return output;
});

// 1. handle bottom projects
Set<Alias> existedAlias = ExpressionUtils.collect(outputs, Alias.class::isInstance);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql --
1
2
3
4
5
6
7

-- !sql --
JDR 2014-10-02T00:00 12.86 12.875
JDR 2014-10-03T00:00 12.89 12.896666667
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ suite("test_window_function") {

sql """ INSERT INTO ${windowFunctionTable1} VALUES ('JDR',12.86,'2014-10-02 00:00:00','2014-10-02 00:00:00.111111','2014-10-02 00:00:00.111111','2014-10-02 00:00:00.111111'),('JDR',12.89,'2014-10-03 00:00:00','2014-10-03 00:00:00.111111','2014-10-03 00:00:00.111111','2014-10-03 00:00:00.111111'),('JDR',12.94,'2014-10-04 00:00:00','2014-10-04 00:00:00.111111','2014-10-04 00:00:00.111111','2014-10-04 00:00:00.111111'),('JDR',12.55,'2014-10-05 00:00:00','2014-10-05 00:00:00.111111','2014-10-05 00:00:00.111111','2014-10-05 00:00:00.111111'),('JDR',14.03,'2014-10-06 00:00:00','2014-10-06 00:00:00.111111','2014-10-06 00:00:00.111111','2014-10-06 00:00:00.111111'),('JDR',14.75,'2014-10-07 00:00:00','2014-10-07 00:00:00.111111','2014-10-07 00:00:00.111111','2014-10-07 00:00:00.111111'),('JDR',13.98,'2014-10-08 00:00:00','2014-10-08 00:00:00.111111','2014-10-08 00:00:00.111111','2014-10-08 00:00:00.111111') """

qt_sql """SELECT row_number() OVER (partition by 1 order by 2) from ${windowFunctionTable1} order by 1; """
// Nereids does't support window function
// qt_sql """
// SELECT /*+SET_VAR(parallel_fragment_exec_instance_num=1) */
Expand Down