From f04e6be8fd1c5894eafae1676b52395e973bc3f6 Mon Sep 17 00:00:00 2001 From: seawinde Date: Thu, 5 Dec 2024 11:16:58 +0800 Subject: [PATCH] opt --- .../expression/ExpressionOptimization.java | 2 - .../expression/rules/SimplifyDateTrunc.java | 80 ------------------- 2 files changed, 82 deletions(-) delete mode 100644 fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyDateTrunc.java diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java index 639e416f99fd320..5d45b2430c42b0f 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java @@ -27,7 +27,6 @@ import org.apache.doris.nereids.rules.expression.rules.NullSafeEqualToEqual; import org.apache.doris.nereids.rules.expression.rules.OrToIn; import org.apache.doris.nereids.rules.expression.rules.SimplifyComparisonPredicate; -import org.apache.doris.nereids.rules.expression.rules.SimplifyDateTrunc; import org.apache.doris.nereids.rules.expression.rules.SimplifyDecimalV3Comparison; import org.apache.doris.nereids.rules.expression.rules.SimplifyInPredicate; import org.apache.doris.nereids.rules.expression.rules.SimplifyRange; @@ -43,7 +42,6 @@ public class ExpressionOptimization extends ExpressionRewrite { public static final List OPTIMIZE_REWRITE_RULES = ImmutableList.of( bottomUp( - SimplifyDateTrunc.INSTANCE, ExtractCommonFactorRule.INSTANCE, DistinctPredicatesRule.INSTANCE, SimplifyComparisonPredicate.INSTANCE, diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyDateTrunc.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyDateTrunc.java deleted file mode 100644 index f65ec84e8cb2b8b..000000000000000 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyDateTrunc.java +++ /dev/null @@ -1,80 +0,0 @@ -// 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.rules; - -import org.apache.doris.nereids.rules.expression.AbstractExpressionRewriteRule; -import org.apache.doris.nereids.rules.expression.ExpressionMatchingContext; -import org.apache.doris.nereids.rules.expression.ExpressionPatternMatcher; -import org.apache.doris.nereids.rules.expression.ExpressionPatternRuleFactory; -import org.apache.doris.nereids.rules.expression.ExpressionRewriteContext; -import org.apache.doris.nereids.trees.expressions.ComparisonPredicate; -import org.apache.doris.nereids.trees.expressions.Expression; -import org.apache.doris.nereids.trees.expressions.Slot; -import org.apache.doris.nereids.trees.expressions.functions.scalar.DateTrunc; -import org.apache.doris.nereids.trees.expressions.literal.Literal; -import org.apache.doris.nereids.util.ExpressionUtils; - -import com.google.common.collect.ImmutableList; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * date_trunc(slot#0, 'day') > '2024-01-01' can be simplify to slot#0 > '2024-01-01' - * */ -public class SimplifyDateTrunc extends AbstractExpressionRewriteRule implements ExpressionPatternRuleFactory { - - public static SimplifyDateTrunc INSTANCE = new SimplifyDateTrunc(); - - @Override - public List> buildRules() { - return ImmutableList.of( - matchesType(ComparisonPredicate.class) - .when(predicate -> - (predicate.getArgument(0) instanceof DateTrunc - && predicate.getArgument(1) instanceof Literal) - || (predicate.getArgument(1) instanceof DateTrunc - && predicate.getArgument(0) instanceof Literal) - ) - .thenApply(SimplifyDateTrunc::rewriteWhenComparison) - ); - } - - private static Expression rewriteWhenComparison(ExpressionMatchingContext context) { - - Expression predicate = context.expr; - DateTrunc dateTrunc = predicate.getArgument(0) instanceof DateTrunc - ? (DateTrunc) predicate.getArgument(0) : (DateTrunc) predicate.getArgument(1); - Literal literal = predicate.getArgument(0) instanceof Literal - ? (Literal) predicate.getArgument(0) : (Literal) predicate.getArgument(1); - Expression argument = dateTrunc.getArgument(0); - if (!(argument instanceof Slot)) { - return predicate; - } - Map exprMapping = new HashMap<>(); - exprMapping.put(argument, literal); - Expression replacedExpr = ExpressionUtils.replace(argument, exprMapping); - Expression evaluatedExpr = FoldConstantRuleOnFE.evaluate(replacedExpr, - new ExpressionRewriteContext(context.cascadesContext)); - if (evaluatedExpr.equals(literal)) { - return predicate.withChildren(ImmutableList.of(argument, literal)); - } - return predicate; - } -}