924060929 commented on code in PR #12151:
URL: https://github.com/apache/doris/pull/12151#discussion_r959230265


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/rules/FoldConstantRule.java:
##########
@@ -0,0 +1,440 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.rules.expression.rewrite.rules;
+
+import org.apache.doris.analysis.BetweenPredicate;
+import org.apache.doris.analysis.CastExpr;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.ExprId;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.PrimitiveType;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.common.IdGenerator;
+import org.apache.doris.common.LoadException;
+import org.apache.doris.common.util.TimeUtils;
+import org.apache.doris.common.util.VectorizedUtil;
+import org.apache.doris.nereids.glue.translator.ExpressionTranslator;
+import 
org.apache.doris.nereids.rules.expression.rewrite.AbstractExpressionRewriteRule;
+import 
org.apache.doris.nereids.rules.expression.rewrite.ExpressionRewriteContext;
+import org.apache.doris.nereids.trees.expressions.And;
+import org.apache.doris.nereids.trees.expressions.BinaryArithmetic;
+import org.apache.doris.nereids.trees.expressions.CaseWhen;
+import org.apache.doris.nereids.trees.expressions.Cast;
+import org.apache.doris.nereids.trees.expressions.ComparisonPredicate;
+import org.apache.doris.nereids.trees.expressions.CompoundPredicate;
+import org.apache.doris.nereids.trees.expressions.EqualTo;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.ExpressionEvaluator;
+import org.apache.doris.nereids.trees.expressions.GreaterThan;
+import org.apache.doris.nereids.trees.expressions.GreaterThanEqual;
+import org.apache.doris.nereids.trees.expressions.InPredicate;
+import org.apache.doris.nereids.trees.expressions.IsNull;
+import org.apache.doris.nereids.trees.expressions.LessThan;
+import org.apache.doris.nereids.trees.expressions.LessThanEqual;
+import org.apache.doris.nereids.trees.expressions.Like;
+import org.apache.doris.nereids.trees.expressions.Not;
+import org.apache.doris.nereids.trees.expressions.NullSafeEqual;
+import org.apache.doris.nereids.trees.expressions.Or;
+import org.apache.doris.nereids.trees.expressions.TimestampArithmetic;
+import org.apache.doris.nereids.trees.expressions.WhenClause;
+import org.apache.doris.nereids.trees.expressions.functions.BoundFunction;
+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.literal.NullLiteral;
+import org.apache.doris.nereids.types.DataType;
+import org.apache.doris.proto.InternalService;
+import org.apache.doris.proto.InternalService.PConstantExprResult;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.rpc.BackendServiceProxy;
+import org.apache.doris.system.Backend;
+import org.apache.doris.thrift.TExpr;
+import org.apache.doris.thrift.TFoldConstantParams;
+import org.apache.doris.thrift.TNetworkAddress;
+import org.apache.doris.thrift.TPrimitiveType;
+import org.apache.doris.thrift.TQueryGlobals;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+/**
+ * Constant evaluation of an expression.
+ */
+public class FoldConstantRule extends AbstractExpressionRewriteRule {
+    public static final FoldConstantRule INSTANCE = new FoldConstantRule();
+    private static final Logger LOG = 
LogManager.getLogger(FoldConstantRule.class);
+    private static final DateFormat DATE_FORMAT = new 
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+    private final IdGenerator<ExprId> idGenerator = ExprId.createGenerator();
+
+    private static boolean isAllLiteral(Expression... children) {
+        return Arrays.stream(children).allMatch(c -> c instanceof Literal);
+    }
+
+    private static boolean isAllLiteral(List<Expression> children) {
+        return children.stream().allMatch(c -> c instanceof Literal);
+    }
+
+    private static boolean hasNull(List<Expression> children) {
+        return children.stream().anyMatch(c -> c instanceof NullLiteral);
+    }
+
+    private static boolean hasNull(Expression... children) {
+        return Arrays.stream(children).anyMatch(c -> c instanceof NullLiteral);
+    }
+
+    @Override
+    public Expression rewrite(Expression expr, ExpressionRewriteContext ctx) {
+        Expression expression = expr.accept(this, ctx);
+        if (ctx.connectContext != null && 
ctx.connectContext.getSessionVariable().isEnableFoldConstantByBe()) {
+            return foldByBe(expression, ctx);
+        }
+        return expression;
+    }
+
+    @Override
+    public Expression visitComparisonPredicate(ComparisonPredicate cp, 
ExpressionRewriteContext context) {
+        Expression left = rewrite(cp.left(), context);
+        Expression right = rewrite(cp.right(), context);
+
+        if (!(cp instanceof NullSafeEqual) && hasNull(left, right)) {
+            return Literal.of(null);
+        }
+
+        if (isAllLiteral(left, right)) {
+            Literal l = (Literal) left;
+            Literal r = (Literal) right;
+            if (cp instanceof EqualTo) {
+                return BooleanLiteral.of(l.compareTo(r) == 0);
+            }
+            if (cp instanceof GreaterThan) {
+                return BooleanLiteral.of(l.compareTo(r) > 0);
+            }
+            if (cp instanceof GreaterThanEqual) {
+                return BooleanLiteral.of(l.compareTo(r) >= 0);
+            }
+            if (cp instanceof LessThan) {
+                return BooleanLiteral.of(l.compareTo(r) < 0);
+            }
+            if (cp instanceof LessThanEqual) {
+                return BooleanLiteral.of(l.compareTo(r) <= 0);
+            }
+            if (cp instanceof NullSafeEqual) {
+                if (l.isNull() && r.isNull()) {
+                    return BooleanLiteral.TRUE;
+                } else if (!l.isNull() && !r.isNull()) {
+                    return BooleanLiteral.of(l.compareTo(r) == 0);
+                } else {
+                    return BooleanLiteral.FALSE;
+                }
+            }
+        }
+        return cp.withChildren(left, right);
+    }
+
+    @Override
+    public Expression visitNot(Not not, ExpressionRewriteContext context) {
+        Expression child = rewrite(not.child(), context);
+        if (child instanceof NullLiteral) {
+            return Literal.of(null);
+        }

Review Comment:
   this part look like a common logic, you can move to the common visit method, 
like this:
   ```java
   public Expression visit(Expression expr, ExpressionRewriteContext context) {
     List<Expression> children = expr.children()
            .stream()
            .map(child -> child.visit(this, context)
            .collect(Collectors.toList());
       boolean hasNullLiteralChild = ExpresstionUtils.hasNullLiteral(children);
   
       // isNullHandling: 
Env.getCurrentEnv().isNullResultWithOneNullParamFunction()
       if (expr.isNullHandling() && hasNullLiteralChild) {
         return Literal.of(null);
       }
   
       if (!ExpressionUtils.isAllLiteral(children) {
         return expr.withChildren(children);
       }
       // this position every child is Literal, and forward to visitXxx method
       return expr.withChildren(children).accept(this, context);
   }
   ```
   
   so every visitXxx method can simple fold constant because every child must 
be Literal 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to