-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[opt](Nereids) translate "between" to "equalTo" if upper equals to lower #43344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
115 changes: 115 additions & 0 deletions
115
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/BetweenToEqual.java
This file contains hidden or 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,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); | ||
} | ||
} |
This file contains hidden or 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
40 changes: 40 additions & 0 deletions
40
fe/fe-core/src/test/java/org/apache/doris/nereids/parser/BetweenTest.java
This file contains hidden or 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,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); | ||
} | ||
} |
This file contains hidden or 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
46 changes: 46 additions & 0 deletions
46
regression-test/suites/nereids_rules_p0/between_to_equal/betweenToEqual.groovy
This file contains hidden or 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,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))") | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.