forked from trinodb/trino
-
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.
Normalize OR expressions referencing the same symbol as IN
- Loading branch information
1 parent
1f98270
commit 7448c39
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
...main/src/main/java/io/trino/sql/planner/iterative/rule/NormalizeOrExpressionRewriter.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,93 @@ | ||
/* | ||
* Licensed 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 io.trino.sql.planner.iterative.rule; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import io.trino.sql.tree.ComparisonExpression; | ||
import io.trino.sql.tree.Expression; | ||
import io.trino.sql.tree.ExpressionRewriter; | ||
import io.trino.sql.tree.ExpressionTreeRewriter; | ||
import io.trino.sql.tree.InListExpression; | ||
import io.trino.sql.tree.InPredicate; | ||
import io.trino.sql.tree.LogicalExpression; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.google.common.collect.ImmutableList.toImmutableList; | ||
import static com.google.common.collect.ImmutableSet.toImmutableSet; | ||
import static io.trino.sql.ExpressionUtils.and; | ||
import static io.trino.sql.ExpressionUtils.or; | ||
import static io.trino.sql.tree.ComparisonExpression.Operator.EQUAL; | ||
import static io.trino.sql.tree.LogicalExpression.Operator.AND; | ||
import static java.util.stream.Collectors.groupingBy; | ||
import static java.util.stream.Collectors.mapping; | ||
|
||
public final class NormalizeOrExpressionRewriter | ||
{ | ||
public static Expression normalizeOrExpression(Expression expression) | ||
{ | ||
return ExpressionTreeRewriter.rewriteWith(new Visitor(), expression); | ||
} | ||
|
||
private NormalizeOrExpressionRewriter() {} | ||
|
||
private static class Visitor | ||
extends ExpressionRewriter<Void> | ||
{ | ||
@Override | ||
public Expression rewriteLogicalExpression(LogicalExpression node, Void context, ExpressionTreeRewriter<Void> treeRewriter) | ||
{ | ||
List<Expression> terms = node.getTerms().stream() | ||
.map(expression -> treeRewriter.rewrite(expression, context)) | ||
.collect(toImmutableList()); | ||
|
||
if (node.getOperator() == AND) { | ||
return and(terms); | ||
} | ||
|
||
List<InPredicate> comparisons = terms.stream() | ||
.filter(NormalizeOrExpressionRewriter::isEqualityComparisonExpression) | ||
.map(ComparisonExpression.class::cast) | ||
.collect(groupingBy( | ||
ComparisonExpression::getLeft, | ||
LinkedHashMap::new, | ||
mapping(ComparisonExpression::getRight, Collectors.toList()))) | ||
.entrySet().stream() | ||
.filter(entry -> entry.getValue().size() > 1) | ||
.map(entry -> new InPredicate(entry.getKey(), new InListExpression(entry.getValue()))) | ||
.collect(Collectors.toList()); | ||
|
||
Set<Expression> expressionToSkip = comparisons.stream() | ||
.map(InPredicate::getValue) | ||
.collect(toImmutableSet()); | ||
|
||
List<Expression> others = terms.stream() | ||
.filter(expression -> !isEqualityComparisonExpression(expression) || !expressionToSkip.contains(((ComparisonExpression) expression).getLeft())) | ||
.collect(Collectors.toList()); | ||
|
||
return or(ImmutableList.<Expression>builder() | ||
.addAll(others) | ||
.addAll(comparisons) | ||
.build()); | ||
} | ||
} | ||
|
||
private static boolean isEqualityComparisonExpression(Expression expression) | ||
{ | ||
return expression instanceof ComparisonExpression && ((ComparisonExpression) expression).getOperator() == EQUAL; | ||
} | ||
} |
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