From 3ffd5b8e9a96abdaf7587ac18cdb8dfb50bc5edc Mon Sep 17 00:00:00 2001 From: morrySnow <101034200+morrySnow@users.noreply.github.com> Date: Thu, 25 Jan 2024 15:58:19 +0800 Subject: [PATCH] [fix](Nereids) some special expression should not be constant (#30305) --- .../nereids/trees/expressions/Expression.java | 18 +++++++++++++++++- .../test_window_function.groovy | 4 ++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java index 048cae55f3..246cbbf896 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java @@ -18,18 +18,22 @@ package org.apache.doris.nereids.trees.expressions; import org.apache.doris.common.Config; +import org.apache.doris.nereids.analyzer.PlaceholderExpression; import org.apache.doris.nereids.analyzer.Unbound; +import org.apache.doris.nereids.analyzer.UnboundVariable; import org.apache.doris.nereids.exceptions.AnalysisException; import org.apache.doris.nereids.trees.AbstractTreeNode; import org.apache.doris.nereids.trees.expressions.functions.ExpressionTrait; import org.apache.doris.nereids.trees.expressions.functions.Nondeterministic; import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Lambda; import org.apache.doris.nereids.trees.expressions.literal.Literal; import org.apache.doris.nereids.trees.expressions.literal.NullLiteral; import org.apache.doris.nereids.trees.expressions.shape.LeafExpression; import org.apache.doris.nereids.trees.expressions.typecoercion.ExpectsInputTypes; import org.apache.doris.nereids.trees.expressions.typecoercion.TypeCheckResult; import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; +import org.apache.doris.nereids.trees.plans.commands.info.PartitionDefinition.MaxValue; import org.apache.doris.nereids.types.ArrayType; import org.apache.doris.nereids.types.DataType; import org.apache.doris.nereids.types.MapType; @@ -249,7 +253,19 @@ public abstract class Expression extends AbstractTreeNode implements * Whether the expression is a constant. */ public boolean isConstant() { - if (this instanceof AggregateFunction) { + if (this instanceof AssertNumRowsElement + || this instanceof AggregateFunction + || this instanceof Lambda + || this instanceof MaxValue + || this instanceof OrderExpression + || this instanceof PlaceholderExpression + || this instanceof Properties + || this instanceof SubqueryExpr + || this instanceof UnboundVariable + || this instanceof Variable + || this instanceof VariableDesc + || this instanceof WindowExpression + || this instanceof WindowFrame) { // agg_fun(literal) is not constant, the result depends on the group by keys return false; } 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 26b770e868..bcaaefe703 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 @@ -25,6 +25,10 @@ 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; """ + + // test constant folding by be on OrderExpression + sql """set enable_fold_constant_by_be = true""" + 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) */