-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Optimize BETWEEN predicate to include both the sides. #22521
Merged
Merged
Changes from 1 commit
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 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 |
---|---|---|
|
@@ -22,14 +22,13 @@ | |
import io.trino.sql.ir.IrVisitor; | ||
import io.trino.sql.ir.Reference; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.collect.ImmutableSet.toImmutableSet; | ||
import static io.trino.sql.ir.Comparison.Operator.GREATER_THAN_OR_EQUAL; | ||
import static io.trino.sql.ir.Comparison.Operator.LESS_THAN_OR_EQUAL; | ||
import static java.util.Collections.singletonList; | ||
import static java.util.Comparator.comparing; | ||
import static java.util.function.Function.identity; | ||
|
@@ -68,6 +67,7 @@ public static Optional<SortExpressionContext> extractSortExpression(Set<Symbol> | |
.map(visitor::process) | ||
.filter(Optional::isPresent) | ||
.map(Optional::get) | ||
.flatMap(List::stream) | ||
.collect(toMap(SortExpressionContext::getSortExpression, identity(), SortExpressionExtractor::merge)) | ||
.values()); | ||
|
||
|
@@ -88,7 +88,7 @@ private static SortExpressionContext merge(SortExpressionContext left, SortExpre | |
} | ||
|
||
private static class SortExpressionVisitor | ||
extends IrVisitor<Optional<SortExpressionContext>, Void> | ||
extends IrVisitor<Optional<List<SortExpressionContext>>, Void> | ||
{ | ||
private final Set<Symbol> buildSymbols; | ||
|
||
|
@@ -98,13 +98,13 @@ public SortExpressionVisitor(Set<Symbol> buildSymbols) | |
} | ||
|
||
@Override | ||
protected Optional<SortExpressionContext> visitExpression(Expression expression, Void context) | ||
protected Optional<List<SortExpressionContext>> visitExpression(Expression expression, Void context) | ||
{ | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
protected Optional<SortExpressionContext> visitComparison(Comparison comparison, Void context) | ||
protected Optional<List<SortExpressionContext>> visitComparison(Comparison comparison, Void context) | ||
{ | ||
return switch (comparison.operator()) { | ||
case GREATER_THAN, GREATER_THAN_OR_EQUAL, LESS_THAN, LESS_THAN_OR_EQUAL -> { | ||
|
@@ -115,7 +115,7 @@ protected Optional<SortExpressionContext> visitComparison(Comparison comparison, | |
hasBuildReferencesOnOtherSide = hasBuildSymbolReference(buildSymbols, comparison.right()); | ||
} | ||
if (sortChannel.isPresent() && !hasBuildReferencesOnOtherSide) { | ||
yield sortChannel.map(symbolReference -> new SortExpressionContext(symbolReference, singletonList(comparison))); | ||
yield sortChannel.map(symbolReference -> singletonList(new SortExpressionContext(symbolReference, singletonList(comparison)))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
} | ||
yield Optional.empty(); | ||
} | ||
|
@@ -124,13 +124,20 @@ protected Optional<SortExpressionContext> visitComparison(Comparison comparison, | |
} | ||
|
||
@Override | ||
protected Optional<SortExpressionContext> visitBetween(Between node, Void context) | ||
protected Optional<List<SortExpressionContext>> visitBetween(Between node, Void context) | ||
{ | ||
Optional<SortExpressionContext> result = visitComparison(new Comparison(GREATER_THAN_OR_EQUAL, node.value(), node.min()), context); | ||
if (result.isPresent()) { | ||
return result; | ||
// Handle both side of BETWEEN as `GREATER_THAN_OR_EQUAL` expression and `LESS_THAN_OR_EQUAL` expression. | ||
sopel39 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Optional<List<SortExpressionContext>> betweenLeftResult = visitComparison(new Comparison(Comparison.Operator.GREATER_THAN_OR_EQUAL, node.value(), node.min()), context); | ||
Optional<List<SortExpressionContext>> betweenRightResult = visitComparison(new Comparison(Comparison.Operator.LESS_THAN_OR_EQUAL, node.value(), node.max()), context); | ||
if (betweenLeftResult.isPresent() && betweenRightResult.isPresent()) { | ||
return Optional.of(Arrays.asList(betweenLeftResult.get().getFirst(), betweenRightResult.get().getFirst())); | ||
} | ||
else if (betweenLeftResult.isPresent()) { | ||
return betweenLeftResult; | ||
} | ||
else { | ||
return betweenRightResult; | ||
} | ||
return visitComparison(new Comparison(LESS_THAN_OR_EQUAL, node.value(), node.max()), context); | ||
} | ||
} | ||
|
||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional
is no longer needed, justList<SortExpressionContext>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated