forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[draft](mtmv) support rewrite when mv has date_trunc but query doesn'…
…t have
- Loading branch information
Showing
7 changed files
with
151 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyDateTrunc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// 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<ExpressionPatternMatcher<? extends Expression>> 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<? extends Expression> 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<Expression, Expression> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters