diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ExtractAndNormalizeWindowExpression.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ExtractAndNormalizeWindowExpression.java index 10e256a132c4be..3f13f8bd3f363d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ExtractAndNormalizeWindowExpression.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ExtractAndNormalizeWindowExpression.java @@ -39,6 +39,7 @@ import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import java.util.stream.Stream; /** @@ -52,14 +53,24 @@ public Rule build() { List outputs = ExpressionUtils.rewriteDownShortCircuit(project.getProjects(), output -> { if (output instanceof WindowExpression) { + WindowExpression windowExpression = (WindowExpression) output; Expression expression = ((WindowExpression) output).getFunction(); if (expression instanceof Sum || expression instanceof Max || expression instanceof Min || expression instanceof Avg) { // sum, max, min and avg in window function should be always nullable - return ((WindowExpression) output) + windowExpression = ((WindowExpression) output) .withFunction(((NullableAggregateFunction) expression) .withAlwaysNullable(true)); } + // remove literal partition by and order by keys + return windowExpression.withPartitionKeysOrderKeys( + windowExpression.getPartitionKeys().stream() + .filter(partitionExpr -> !partitionExpr.isConstant()) + .collect(Collectors.toList()), + windowExpression.getOrderKeys().stream() + .filter(orderExpression -> !orderExpression + .getOrderKey().getExpr().isConstant()) + .collect(Collectors.toList())); } return output; }); diff --git a/regression-test/data/nereids_p0/sql_functions/window_functions/test_window_function.out b/regression-test/data/nereids_p0/sql_functions/window_functions/test_window_function.out index 44dbf7c998d1fd..fe0ee1764e2409 100644 --- a/regression-test/data/nereids_p0/sql_functions/window_functions/test_window_function.out +++ b/regression-test/data/nereids_p0/sql_functions/window_functions/test_window_function.out @@ -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 @@ -609,3 +618,5 @@ USA Pete Hello 32767 6 12 32767 32767 6 6 32767 + + diff --git a/regression-test/suites/nereids_p0/sql_functions/window_functions/test_window_function.groovy b/regression-test/suites/nereids_p0/sql_functions/window_functions/test_window_function.groovy index d17dd8350e5234..26b770e8687ed1 100644 --- a/regression-test/suites/nereids_p0/sql_functions/window_functions/test_window_function.groovy +++ b/regression-test/suites/nereids_p0/sql_functions/window_functions/test_window_function.groovy @@ -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) */