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

morrysnow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 8ad6b283403 [fix](nereids) not rewrite first_value when first_value 
ignore nulls (#45065)
8ad6b283403 is described below

commit 8ad6b283403fd8b3407dc91af25c0f7906109b81
Author: feiniaofeiafei <moail...@selectdb.com>
AuthorDate: Tue Dec 10 16:21:59 2024 +0800

    [fix](nereids) not rewrite first_value when first_value ignore nulls 
(#45065)
    
    this pr fix 2 things:
    1.when first_value/last_value second parameter is true, which means
    ignore nulls, fe should not do some rewrite.
    2.adding check that first_value/last_value second parameter must be true
    or false.
    
    Related PR: #44996 #27623
---
 .../rules/analysis/WindowFunctionChecker.java      | 11 +++++++
 .../functions/window/FirstOrLastValue.java         | 15 ++++++++++
 .../nereids_syntax_p0/window_function.groovy       | 35 ++++++++++++++++++++++
 3 files changed, 61 insertions(+)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/WindowFunctionChecker.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/WindowFunctionChecker.java
index 036b6be8b20..d6904ae074c 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/WindowFunctionChecker.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/WindowFunctionChecker.java
@@ -37,6 +37,7 @@ import 
org.apache.doris.nereids.trees.expressions.functions.window.Ntile;
 import org.apache.doris.nereids.trees.expressions.functions.window.PercentRank;
 import org.apache.doris.nereids.trees.expressions.functions.window.Rank;
 import org.apache.doris.nereids.trees.expressions.functions.window.RowNumber;
+import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
 import org.apache.doris.nereids.trees.expressions.literal.Literal;
 import 
org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionVisitor;
 import org.apache.doris.nereids.util.TypeCoercionUtils;
@@ -315,6 +316,10 @@ public class WindowFunctionChecker extends 
DefaultExpressionVisitor<Expression,
      */
     @Override
     public FirstOrLastValue visitFirstValue(FirstValue firstValue, Void ctx) {
+        FirstOrLastValue.checkSecondParameter(firstValue);
+        if (2 == firstValue.arity() && 
firstValue.child(1).equals(BooleanLiteral.TRUE)) {
+            return firstValue;
+        }
         Optional<WindowFrame> windowFrame = windowExpression.getWindowFrame();
         if (windowFrame.isPresent()) {
             WindowFrame wf = windowFrame.get();
@@ -339,6 +344,12 @@ public class WindowFunctionChecker extends 
DefaultExpressionVisitor<Expression,
         return firstValue;
     }
 
+    @Override
+    public FirstOrLastValue visitLastValue(LastValue lastValue, Void ctx) {
+        FirstOrLastValue.checkSecondParameter(lastValue);
+        return lastValue;
+    }
+
     /**
      * required WindowFrame: (RANGE, UNBOUNDED PRECEDING, CURRENT ROW)
      */
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/window/FirstOrLastValue.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/window/FirstOrLastValue.java
index bb0a68eef6a..c21c420f703 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/window/FirstOrLastValue.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/window/FirstOrLastValue.java
@@ -18,9 +18,11 @@
 package org.apache.doris.nereids.trees.expressions.functions.window;
 
 import org.apache.doris.catalog.FunctionSignature;
+import org.apache.doris.nereids.exceptions.AnalysisException;
 import org.apache.doris.nereids.trees.expressions.Expression;
 import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
 import 
org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
+import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
 import org.apache.doris.nereids.types.BooleanType;
 import org.apache.doris.nereids.types.coercion.AnyDataType;
 
@@ -63,4 +65,17 @@ public abstract class FirstOrLastValue extends WindowFunction
     public List<FunctionSignature> getSignatures() {
         return SIGNATURES;
     }
+
+    /**check the second parameter must be true or false*/
+    public static void checkSecondParameter(FirstOrLastValue firstOrLastValue) 
{
+        if (1 == firstOrLastValue.arity()) {
+            return;
+        }
+        if (!BooleanLiteral.TRUE.equals(firstOrLastValue.child(1))
+                && !BooleanLiteral.FALSE.equals(firstOrLastValue.child(1))) {
+            throw new AnalysisException("The second parameter of " + 
firstOrLastValue.getName()
+                    + " must be a constant or a constant expression, and the 
result of "
+                    + "the calculated constant or constant expression must be 
true or false.");
+        }
+    }
 }
diff --git a/regression-test/suites/nereids_syntax_p0/window_function.groovy 
b/regression-test/suites/nereids_syntax_p0/window_function.groovy
index 31cb425fcf7..f2ce708f4c3 100644
--- a/regression-test/suites/nereids_syntax_p0/window_function.groovy
+++ b/regression-test/suites/nereids_syntax_p0/window_function.groovy
@@ -279,4 +279,39 @@ suite("window_function") {
                 )t
             )a where rn=1
     """
+
+    // test first value second param is not constant
+    test {
+        sql "select first_value(c1,c1) over() from window_test"
+        exception "The second parameter of first_value must be a constant or a 
constant expression, and the result of the calculated constant or constant 
expression must be true or false."
+    }
+
+    test {
+        sql "select last_value(c1,c1) over() from window_test"
+        exception "The second parameter of last_value must be a constant or a 
constant expression, and the result of the calculated constant or constant 
expression must be true or false."
+    }
+
+    test {
+        sql "select first_value(c1,cast('abc' as boolean)) over() from 
window_test"
+        exception "The second parameter of first_value must be a constant or a 
constant expression, and the result of the calculated constant or constant 
expression must be true or false."
+    }
+    test {
+        sql "select first_value(c1,'') over() from window_test"
+        exception "The second parameter of first_value must be a constant or a 
constant expression, and the result of the calculated constant or constant 
expression must be true or false."
+    }
+    test {
+        sql "select last_value(c1,'345_a') over() from window_test"
+        exception "The second parameter of last_value must be a constant or a 
constant expression, and the result of the calculated constant or constant 
expression must be true or false."
+    }
+    sql "select last_value(c1,cast('67' as boolean)) over() from window_test"
+    sql "select first_value(c1,cast(56 as boolean)) over() from window_test"
+    sql "select last_value(c1,cast(56 as boolean)) over() from window_test"
+    sql "select first_value(c1,cast('true' as boolean)) over() from 
window_test"
+    sql "select last_value(c1,cast('false' as boolean)) over() from 
window_test"
+    sql "select first_value(c1,cast('1' as boolean)) over() from window_test"
+    sql "select last_value(c1,cast('0' as boolean)) over() from window_test"
+    sql "select first_value(c1,true) over() from window_test"
+    sql "select last_value(c1,false) over() from window_test"
+    sql "select first_value(c1,1) over() from window_test"
+    sql "select last_value(c1,0) over() from window_test"
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to