-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add support for FETCH clause and FETCH FIRST WITH TIES clause #14871
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
975f7e9
Add support for FETCH syntax
fgwang7w 9373d10
revision for FETCH implementation
fgwang7w 7ae4dfa
Add support for FETCH FIRST clause WITH TIES(allowing syntax)
fgwang7w be32619
Add optimizer rule to eanble LimitNode having 'With Ties' property
fgwang7w a37d692
revision for FETCH
fgwang7w 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
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
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 |
|---|---|---|
|
|
@@ -51,6 +51,7 @@ | |
| import com.facebook.presto.sql.tree.Cast; | ||
| import com.facebook.presto.sql.tree.Delete; | ||
| import com.facebook.presto.sql.tree.Expression; | ||
| import com.facebook.presto.sql.tree.FetchFirst; | ||
| import com.facebook.presto.sql.tree.FieldReference; | ||
| import com.facebook.presto.sql.tree.FrameBound; | ||
| import com.facebook.presto.sql.tree.FunctionCall; | ||
|
|
@@ -146,9 +147,10 @@ public RelationPlan plan(Query query) | |
| List<Expression> outputs = analysis.getOutputExpressions(query); | ||
| builder = handleSubqueries(builder, query, outputs); | ||
| builder = project(builder, Iterables.concat(orderBy, outputs)); | ||
| builder = sort(builder, query); | ||
| Optional<OrderingScheme> orderingScheme = orderingScheme(builder, query.getOrderBy(), analysis.getOrderByExpressions(query)); | ||
| builder = sort(builder, orderingScheme); | ||
| builder = offset(builder, query.getOffset()); | ||
| builder = limit(builder, query); | ||
| builder = limit(builder, query.getLimit(), orderingScheme); | ||
| builder = project(builder, analysis.getOutputExpressions(query)); | ||
|
|
||
| return new RelationPlan(builder.getRoot(), analysis.getScope(query), computeOutputs(builder, analysis.getOutputExpressions(query))); | ||
|
|
@@ -194,9 +196,10 @@ public RelationPlan plan(QuerySpecification node) | |
| builder = project(builder, Iterables.concat(orderBy, outputs)); | ||
|
|
||
| builder = distinct(builder, node); | ||
| builder = sort(builder, node); | ||
| Optional<OrderingScheme> orderingScheme = orderingScheme(builder, node.getOrderBy(), analysis.getOrderByExpressions(node)); | ||
| builder = sort(builder, orderingScheme); | ||
| builder = offset(builder, node.getOffset()); | ||
| builder = limit(builder, node); | ||
| builder = limit(builder, node.getLimit(), orderingScheme); | ||
| builder = project(builder, outputs); | ||
|
|
||
| return new RelationPlan(builder.getRoot(), analysis.getScope(node), computeOutputs(builder, outputs)); | ||
|
|
@@ -887,29 +890,17 @@ private PlanBuilder distinct(PlanBuilder subPlan, QuerySpecification node) | |
| return subPlan; | ||
| } | ||
|
|
||
| private PlanBuilder sort(PlanBuilder subPlan, Query node) | ||
| { | ||
| return sort(subPlan, node.getOrderBy(), analysis.getOrderByExpressions(node)); | ||
| } | ||
|
|
||
| private PlanBuilder sort(PlanBuilder subPlan, QuerySpecification node) | ||
| { | ||
| return sort(subPlan, node.getOrderBy(), analysis.getOrderByExpressions(node)); | ||
| } | ||
|
|
||
| private PlanBuilder sort(PlanBuilder subPlan, Optional<OrderBy> orderBy, List<Expression> orderByExpressions) | ||
| private Optional<OrderingScheme> orderingScheme(PlanBuilder subPlan, Optional<OrderBy> orderBy, List<Expression> orderByExpressions) | ||
| { | ||
| if (!orderBy.isPresent() || (isSkipRedundantSort(session)) && analysis.isOrderByRedundant(orderBy.get())) { | ||
| return subPlan; | ||
| return Optional.empty(); | ||
|
Contributor
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. Why do we need this change? |
||
| } | ||
|
|
||
| PlanNode planNode; | ||
| OrderingScheme orderingScheme = toOrderingScheme( | ||
| orderByExpressions.stream().map(subPlan::translate).collect(toImmutableList()), | ||
| orderBy.get().getSortItems().stream().map(PlannerUtils::toSortOrder).collect(toImmutableList())); | ||
| planNode = new SortNode(idAllocator.getNextId(), subPlan.getRoot(), orderingScheme, false); | ||
|
|
||
| return subPlan.withNewRoot(planNode); | ||
| return Optional.of(orderingScheme); | ||
| } | ||
|
|
||
| private PlanBuilder offset(PlanBuilder subPlan, Optional<Offset> offset) | ||
|
|
@@ -925,25 +916,33 @@ private PlanBuilder offset(PlanBuilder subPlan, Optional<Offset> offset) | |
| analysis.getOffset(offset.get()))); | ||
| } | ||
|
|
||
| private PlanBuilder limit(PlanBuilder subPlan, Query node) | ||
| { | ||
| return limit(subPlan, node.getLimit()); | ||
| } | ||
|
|
||
| private PlanBuilder limit(PlanBuilder subPlan, QuerySpecification node) | ||
| { | ||
| return limit(subPlan, node.getLimit()); | ||
| } | ||
|
|
||
| private PlanBuilder limit(PlanBuilder subPlan, Optional<String> limit) | ||
| private PlanBuilder sort(PlanBuilder subPlan, Optional<OrderingScheme> orderingScheme) | ||
| { | ||
| if (!limit.isPresent()) { | ||
| if (!orderingScheme.isPresent()) { | ||
| return subPlan; | ||
| } | ||
| return subPlan.withNewRoot( | ||
| new SortNode( | ||
| idAllocator.getNextId(), | ||
| subPlan.getRoot(), | ||
| orderingScheme.get(), | ||
| false)); | ||
| } | ||
|
|
||
| if (!limit.get().equalsIgnoreCase("all")) { | ||
| long limitValue = Long.parseLong(limit.get()); | ||
| subPlan = subPlan.withNewRoot(new LimitNode(idAllocator.getNextId(), subPlan.getRoot(), limitValue, FINAL)); | ||
| private PlanBuilder limit(PlanBuilder subPlan, Optional<Node> limit, Optional<OrderingScheme> orderingScheme) | ||
| { | ||
| if (limit.isPresent() && analysis.getLimit(limit.get()).isPresent()) { | ||
| Optional<OrderingScheme> tiesResolvingScheme = Optional.empty(); | ||
| if (limit.get() instanceof FetchFirst && ((FetchFirst) limit.get()).isWithTies()) { | ||
| tiesResolvingScheme = orderingScheme; | ||
| } | ||
| return subPlan.withNewRoot( | ||
| new LimitNode( | ||
| idAllocator.getNextId(), | ||
| subPlan.getRoot(), | ||
| analysis.getLimit(limit.get()).getAsLong(), | ||
| tiesResolvingScheme, | ||
| FINAL)); | ||
| } | ||
|
|
||
| return subPlan; | ||
|
|
||
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.
We need this to be controlled by a session param/feature config. In fact, I would like the whole feature to be optional.