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.
- Loading branch information
Showing
32 changed files
with
1,532 additions
and
1 deletion.
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
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
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
91 changes: 91 additions & 0 deletions
91
...-main/src/main/java/io/prestosql/sql/planner/iterative/rule/ImplementOffsetOverOther.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,91 @@ | ||
/* | ||
* 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.prestosql.sql.planner.iterative.rule; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import io.prestosql.matching.Captures; | ||
import io.prestosql.matching.Pattern; | ||
import io.prestosql.sql.planner.Symbol; | ||
import io.prestosql.sql.planner.iterative.Rule; | ||
import io.prestosql.sql.planner.plan.Assignments; | ||
import io.prestosql.sql.planner.plan.FilterNode; | ||
import io.prestosql.sql.planner.plan.OffsetNode; | ||
import io.prestosql.sql.planner.plan.ProjectNode; | ||
import io.prestosql.sql.planner.plan.RowNumberNode; | ||
import io.prestosql.sql.planner.plan.SortNode; | ||
import io.prestosql.sql.planner.plan.TopNNode; | ||
import io.prestosql.sql.tree.ComparisonExpression; | ||
import io.prestosql.sql.tree.GenericLiteral; | ||
|
||
import java.util.Optional; | ||
|
||
import static io.prestosql.spi.type.BigintType.BIGINT; | ||
import static io.prestosql.sql.planner.plan.Patterns.offset; | ||
import static io.prestosql.sql.planner.plan.Patterns.source; | ||
|
||
/** | ||
* Transforms: | ||
* <pre> | ||
* - Offset (row count = x) | ||
* - Source (other than Sort, TopN) | ||
* </pre> | ||
* Into: | ||
* <pre> | ||
* - Project (prune rowNumber symbol) | ||
* - Filter (rowNumber > x) | ||
* - RowNumber | ||
* - Source | ||
* </pre> | ||
*/ | ||
public class ImplementOffsetOverOther | ||
implements Rule<OffsetNode> | ||
{ | ||
private static final Pattern<OffsetNode> PATTERN = offset() | ||
.with(source().matching(node -> !(node instanceof TopNNode || node instanceof SortNode))); | ||
|
||
@Override | ||
public Pattern<OffsetNode> getPattern() | ||
{ | ||
return PATTERN; | ||
} | ||
|
||
@Override | ||
public Result apply(OffsetNode parent, Captures captures, Context context) | ||
{ | ||
Symbol rowNumberSymbol = context.getSymbolAllocator().newSymbol("row_number", BIGINT); | ||
|
||
RowNumberNode rowNumberNode = new RowNumberNode( | ||
context.getIdAllocator().getNextId(), | ||
parent.getSource(), | ||
ImmutableList.of(), | ||
rowNumberSymbol, | ||
Optional.empty(), | ||
Optional.empty()); | ||
|
||
FilterNode filterNode = new FilterNode( | ||
context.getIdAllocator().getNextId(), | ||
rowNumberNode, | ||
new ComparisonExpression( | ||
ComparisonExpression.Operator.GREATER_THAN, | ||
rowNumberSymbol.toSymbolReference(), | ||
new GenericLiteral("BIGINT", Long.toString(parent.getCount())))); | ||
|
||
ProjectNode projectNode = new ProjectNode( | ||
context.getIdAllocator().getNextId(), | ||
filterNode, | ||
Assignments.identity(parent.getOutputSymbols())); | ||
|
||
return Result.ofPlanNode(projectNode); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
...src/main/java/io/prestosql/sql/planner/iterative/rule/ImplementOffsetOverProjectSort.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,109 @@ | ||
/* | ||
* 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.prestosql.sql.planner.iterative.rule; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import io.prestosql.matching.Capture; | ||
import io.prestosql.matching.Captures; | ||
import io.prestosql.matching.Pattern; | ||
import io.prestosql.spi.block.SortOrder; | ||
import io.prestosql.sql.planner.OrderingScheme; | ||
import io.prestosql.sql.planner.Symbol; | ||
import io.prestosql.sql.planner.iterative.Rule; | ||
import io.prestosql.sql.planner.plan.FilterNode; | ||
import io.prestosql.sql.planner.plan.OffsetNode; | ||
import io.prestosql.sql.planner.plan.ProjectNode; | ||
import io.prestosql.sql.planner.plan.RowNumberNode; | ||
import io.prestosql.sql.planner.plan.SortNode; | ||
import io.prestosql.sql.tree.ComparisonExpression; | ||
import io.prestosql.sql.tree.GenericLiteral; | ||
|
||
import java.util.Optional; | ||
|
||
import static io.prestosql.matching.Capture.newCapture; | ||
import static io.prestosql.spi.type.BigintType.BIGINT; | ||
import static io.prestosql.sql.planner.plan.Patterns.offset; | ||
import static io.prestosql.sql.planner.plan.Patterns.project; | ||
import static io.prestosql.sql.planner.plan.Patterns.sort; | ||
import static io.prestosql.sql.planner.plan.Patterns.source; | ||
|
||
/** | ||
* Transforms: | ||
* <pre> | ||
* - Offset (row count = x) | ||
* - Project (prunes sort symbols no longer useful) | ||
* - Sort (order by a, b, c) | ||
* </pre> | ||
* Into: | ||
* <pre> | ||
* - Project (prunes rowNumber symbol and sort symbols no longer useful) | ||
* - Sort (order by rowNumber) | ||
* - Filter (rowNumber > x) | ||
* - RowNumber | ||
* - Sort (order by a, b, c) | ||
* </pre> | ||
*/ | ||
public class ImplementOffsetOverProjectSort | ||
implements Rule<OffsetNode> | ||
{ | ||
private static final Capture<ProjectNode> PROJECT = newCapture(); | ||
private static final Capture<SortNode> SORT = newCapture(); | ||
|
||
private static final Pattern<OffsetNode> PATTERN = offset() | ||
.with(source().matching( | ||
project().capturedAs(PROJECT).matching(ProjectNode::isIdentity) | ||
.with(source().matching( | ||
sort().capturedAs(SORT))))); | ||
|
||
@Override | ||
public Pattern<OffsetNode> getPattern() | ||
{ | ||
return PATTERN; | ||
} | ||
|
||
@Override | ||
public Result apply(OffsetNode parent, Captures captures, Context context) | ||
{ | ||
ProjectNode project = captures.get(PROJECT); | ||
SortNode sort = captures.get(SORT); | ||
|
||
Symbol rowNumberSymbol = context.getSymbolAllocator().newSymbol("row_number", BIGINT); | ||
|
||
RowNumberNode rowNumberNode = new RowNumberNode( | ||
context.getIdAllocator().getNextId(), | ||
sort, | ||
ImmutableList.of(), | ||
rowNumberSymbol, | ||
Optional.empty(), | ||
Optional.empty()); | ||
|
||
FilterNode filterNode = new FilterNode( | ||
context.getIdAllocator().getNextId(), | ||
rowNumberNode, | ||
new ComparisonExpression( | ||
ComparisonExpression.Operator.GREATER_THAN, | ||
rowNumberSymbol.toSymbolReference(), | ||
new GenericLiteral("BIGINT", Long.toString(parent.getCount())))); | ||
|
||
SortNode sortNode = new SortNode( | ||
context.getIdAllocator().getNextId(), | ||
filterNode, | ||
new OrderingScheme(ImmutableList.of(rowNumberSymbol), ImmutableMap.of(rowNumberSymbol, SortOrder.ASC_NULLS_FIRST))); | ||
|
||
ProjectNode projectNode = (ProjectNode) project.replaceChildren(ImmutableList.of(sortNode)); | ||
|
||
return Result.ofPlanNode(projectNode); | ||
} | ||
} |
Oops, something went wrong.