This is an automated email from the ASF dual-hosted git repository.

kxiao pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new 25ad181de01 [fix](nereids)remove literal partition by and order by 
expression in window function #26899 (#27214)
25ad181de01 is described below

commit 25ad181de018d3bdfef86a200eae9142dd77bb18
Author: starocean999 <[email protected]>
AuthorDate: Sat Nov 18 10:21:32 2023 +0800

    [fix](nereids)remove literal partition by and order by expression in window 
function #26899 (#27214)
---
 .../rewrite/ExtractAndNormalizeWindowExpression.java  | 19 ++++++++++++++++++-
 .../window_functions/test_window_function.out         |  9 +++++++++
 .../window_functions/test_window_function.groovy      |  1 +
 3 files changed, 28 insertions(+), 1 deletion(-)

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 5181c978edc..f45f7a287e8 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
@@ -19,6 +19,7 @@ package org.apache.doris.nereids.rules.rewrite;
 
 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;
@@ -34,6 +35,7 @@ import com.google.common.collect.Sets;
 
 import java.util.List;
 import java.util.Set;
+import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
 /**
@@ -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);
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 44dbf7c998d..e06d5a30ddc 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
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 d17dd8350e5..26b770e8687 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','201 [...]
 
+    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) */


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to