This is an automated email from the ASF dual-hosted git repository. englefly 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 8febe409e39 [opt](Nereids) translate "between" to "equalTo" if upper equals to lower (#43344) 8febe409e39 is described below commit 8febe409e393be265c87fe38ce915788ff6aded5 Author: minghong <zhoumingh...@selectdb.com> AuthorDate: Tue Nov 12 16:32:29 2024 +0800 [opt](Nereids) translate "between" to "equalTo" if upper equals to lower (#43344) ### What problem does this PR solve? predicate "A between 1 and 10" is converted to "A<=10 and A>=1" but if the lower value equals to upper value, between predicate can be converted to a euqalTo predicate. Example: "A between 1 and 1" is converted to "A=1" <!-- You need to clearly describe your PR in this part: 1. What problem was fixed (it's best to include specific error reporting information). How it was fixed. 2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be. 3. What features were added. Why this function was added. 4. Which codes were refactored and why this part of the code was refactored. 5. Which functions were optimized and what is the difference before and after the optimization. The description of the PR needs to enable reviewers to quickly and clearly understand the logic of the code modification. --> <!-- If there are related issues, please fill in the issue number. - If you want the issue to be closed after the PR is merged, please use "close #12345". Otherwise, use "ref #12345" --> Issue Number: close #xxx <!-- If this PR is followup a preivous PR, for example, fix the bug that introduced by a related PR, link the PR here --> Related PR: #xxx Problem Summary: ### Check List (For Committer) - Test <!-- At least one of them must be included. --> - [ ] Regression test - [x] Unit Test - [ ] Manual test (add detailed scripts or steps below) - [ ] No need to test or manual test. Explain why: - [ ] This is a refactor/code format and no logic has been changed. - [ ] Previous test can cover this change. - [ ] No colde files have been changed. - [ ] Other reason <!-- Add your reason? --> - Behavior changed: - [x] No. - [ ] Yes. <!-- Explain the behavior change --> - Does this need documentation? - [ ] No. - [ ] Yes. <!-- Add document PR link here. eg: https://github.com/apache/doris-website/pull/1214 --> - Release note <!-- bugfix, feat, behavior changed need a release note --> <!-- Add one line release note for this PR. --> None ### Check List (For Reviewer who merge this PR) - [ ] Confirm the release note - [ ] Confirm test cases - [ ] Confirm document - [ ] Add branch pick label <!-- Add branch pick label that this PR should merge into --> --- .../doris/nereids/parser/LogicalPlanBuilder.java | 14 ++- .../rules/expression/ExpressionOptimization.java | 4 +- .../rules/expression/rules/BetweenToEqual.java | 115 +++++++++++++++++++++ .../trees/expressions/TimestampArithmetic.java | 1 + .../apache/doris/nereids/parser/BetweenTest.java | 40 +++++++ .../rules/expression/SimplifyRangeTest.java | 1 + .../between_to_equal/betweenToEqual.groovy | 46 +++++++++ 7 files changed, 216 insertions(+), 5 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java index f8e59c36130..02cb23e4269 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java @@ -3508,10 +3508,16 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> { Expression outExpression; switch (ctx.kind.getType()) { case DorisParser.BETWEEN: - outExpression = new And( - new GreaterThanEqual(valueExpression, getExpression(ctx.lower)), - new LessThanEqual(valueExpression, getExpression(ctx.upper)) - ); + Expression lower = getExpression(ctx.lower); + Expression upper = getExpression(ctx.upper); + if (lower.equals(upper)) { + outExpression = new EqualTo(valueExpression, lower); + } else { + outExpression = new And( + new GreaterThanEqual(valueExpression, getExpression(ctx.lower)), + new LessThanEqual(valueExpression, getExpression(ctx.upper)) + ); + } break; case DorisParser.LIKE: outExpression = new Like( 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 0ae179489ac..5d45b2430c4 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 @@ -18,6 +18,7 @@ package org.apache.doris.nereids.rules.expression; import org.apache.doris.nereids.rules.expression.rules.ArrayContainToArrayOverlap; +import org.apache.doris.nereids.rules.expression.rules.BetweenToEqual; import org.apache.doris.nereids.rules.expression.rules.CaseWhenToIf; import org.apache.doris.nereids.rules.expression.rules.DateFunctionRewrite; import org.apache.doris.nereids.rules.expression.rules.DistinctPredicatesRule; @@ -53,7 +54,8 @@ public class ExpressionOptimization extends ExpressionRewrite { CaseWhenToIf.INSTANCE, TopnToMax.INSTANCE, NullSafeEqualToEqual.INSTANCE, - LikeToEqualRewrite.INSTANCE + LikeToEqualRewrite.INSTANCE, + BetweenToEqual.INSTANCE ) ); private static final ExpressionRuleExecutor EXECUTOR = new ExpressionRuleExecutor(OPTIMIZE_REWRITE_RULES); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/BetweenToEqual.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/BetweenToEqual.java new file mode 100644 index 00000000000..d1279fdfda5 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/BetweenToEqual.java @@ -0,0 +1,115 @@ +// 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.ExpressionPatternMatcher; +import org.apache.doris.nereids.rules.expression.ExpressionPatternRuleFactory; +import org.apache.doris.nereids.trees.expressions.And; +import org.apache.doris.nereids.trees.expressions.ComparisonPredicate; +import org.apache.doris.nereids.trees.expressions.EqualTo; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.GreaterThanEqual; +import org.apache.doris.nereids.trees.expressions.LessThanEqual; +import org.apache.doris.nereids.trees.expressions.literal.Literal; +import org.apache.doris.nereids.util.ExpressionUtils; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +import java.util.List; +import java.util.Map; + +/** + * f(A, B) between 1 and 1 => f(A, B) = 1 + * + */ +public class BetweenToEqual implements ExpressionPatternRuleFactory { + + public static BetweenToEqual INSTANCE = new BetweenToEqual(); + + @Override + public List<ExpressionPatternMatcher<? extends Expression>> buildRules() { + return ImmutableList.of( + matchesType(And.class).then(BetweenToEqual::rewriteBetweenToEqual) + ); + } + + private static Expression rewriteBetweenToEqual(And and) { + List<Expression> conjuncts = ExpressionUtils.extractConjunction(and); + Map<Expression, List<ComparisonPredicate>> betweenCandidate = Maps.newHashMap(); + for (Expression conj : conjuncts) { + if (isCandidate(conj)) { + conj = normalizeCandidate((ComparisonPredicate) conj); + Expression varPart = conj.child(0); + betweenCandidate.computeIfAbsent(varPart, k -> Lists.newArrayList()); + betweenCandidate.get(varPart).add((ComparisonPredicate) conj); + } + } + List<EqualTo> equals = Lists.newArrayList(); + List<Expression> equalsKey = Lists.newArrayList(); + for (Expression varPart : betweenCandidate.keySet()) { + List<ComparisonPredicate> candidates = betweenCandidate.get(varPart); + if (candidates.size() == 2 && greaterEqualAndLessEqual(candidates.get(0), candidates.get(1))) { + if (candidates.get(0).child(1).equals(candidates.get(1).child(1))) { + equals.add(new EqualTo(candidates.get(0).child(0), candidates.get(0).child(1))); + equalsKey.add(candidates.get(0).child(0)); + } + } + } + if (equals.isEmpty()) { + return null; + } else { + List<Expression> newConjuncts = Lists.newArrayList(equals); + for (Expression conj : conjuncts) { + if (isCandidate(conj)) { + conj = normalizeCandidate((ComparisonPredicate) conj); + if (equalsKey.contains(conj.child(0))) { + continue; + } + } + newConjuncts.add(conj); + } + return ExpressionUtils.and(newConjuncts); + } + } + + // A >= a + // A <= a + // A is expr, a is literal + private static boolean isCandidate(Expression expr) { + if (expr instanceof GreaterThanEqual || expr instanceof LessThanEqual) { + return expr.child(0) instanceof Literal && !(expr.child(1) instanceof Literal) + || expr.child(1) instanceof Literal && !(expr.child(0) instanceof Literal); + } + return false; + } + + private static Expression normalizeCandidate(ComparisonPredicate expr) { + if (expr.child(1) instanceof Literal) { + return expr; + } else { + return expr.withChildren(expr.child(1), expr.child(0)); + } + } + + private static boolean greaterEqualAndLessEqual(ComparisonPredicate cmp1, ComparisonPredicate cmp2) { + return cmp1 instanceof GreaterThanEqual && cmp2 instanceof LessThanEqual + || (cmp1 instanceof LessThanEqual && cmp2 instanceof GreaterThanEqual); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/TimestampArithmetic.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/TimestampArithmetic.java index 888a1a0869c..d3e326fa48a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/TimestampArithmetic.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/TimestampArithmetic.java @@ -160,6 +160,7 @@ public class TimestampArithmetic extends Expression implements BinaryExpression, } TimestampArithmetic other = (TimestampArithmetic) o; return Objects.equals(funcName, other.funcName) && Objects.equals(timeUnit, other.timeUnit) + && op.equals(other.op) && Objects.equals(left(), other.left()) && Objects.equals(right(), other.right()); } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/BetweenTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/BetweenTest.java new file mode 100644 index 00000000000..c9ef4fbee27 --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/BetweenTest.java @@ -0,0 +1,40 @@ +// 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.parser; + +import org.apache.doris.nereids.trees.expressions.And; +import org.apache.doris.nereids.trees.expressions.EqualTo; +import org.apache.doris.nereids.trees.expressions.Expression; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BetweenTest { + private static final NereidsParser PARSER = new NereidsParser(); + + @Test + public void testBetween() { + String expression = "A between 1 and 1"; // + Expression result = PARSER.parseExpression(expression); + Assertions.assertInstanceOf(EqualTo.class, result); + + expression = "A between 1 and 2"; + result = PARSER.parseExpression(expression); + Assertions.assertInstanceOf(And.class, result); + } +} diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/SimplifyRangeTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/SimplifyRangeTest.java index 1f622619dd6..329efee77a8 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/SimplifyRangeTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/SimplifyRangeTest.java @@ -359,6 +359,7 @@ public class SimplifyRangeTest extends ExpressionRewrite { "(CA is null and null) OR CB < timestamp '2024-01-05 00:50:00'"); } + @Test private void assertRewrite(String expression, String expected) { Map<String, Slot> mem = Maps.newHashMap(); Expression needRewriteExpression = replaceUnboundSlot(PARSER.parseExpression(expression), mem); diff --git a/regression-test/suites/nereids_rules_p0/between_to_equal/betweenToEqual.groovy b/regression-test/suites/nereids_rules_p0/between_to_equal/betweenToEqual.groovy new file mode 100644 index 00000000000..9ce93e3fac3 --- /dev/null +++ b/regression-test/suites/nereids_rules_p0/between_to_equal/betweenToEqual.groovy @@ -0,0 +1,46 @@ +// 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. + +suite("between_to_equal") { + sql """ + create table datebetween ( + guid int, + dt DATE, + first_visit_time varchar + )Engine=Olap + DUPLICATE KEY(guid) + distributed by hash(dt) buckets 3 + properties('replication_num' = '1'); + + insert into datebetween values (1, '2021-01-01', 'abc'); + """ + explain { + sql " select * from datebetween where dt between '2021-01-01' and '2021-01-01 11:11:11';" + contains("PREDICATES: (dt[#1] = '2021-01-01')"); + } + + explain { + sql " select * from datebetween where dt between '2021-01-01' and '2021-01-01 11:11:11' and dt < '2024-12-01';" + contains("PREDICATES: (dt[#1] = '2021-01-01')") + } + + explain { + sql "select * from datebetween where dt between '2021-01-01' and '2021-01-01 11:11:11' and dt < '2024-12-01' or guid =1;" + contains("PREDICATES: ((dt[#1] = '2021-01-01') OR (guid[#0] = 1))") + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org