-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prune ORDER BY in window aggregation functions
- Loading branch information
Showing
7 changed files
with
229 additions
and
6 deletions.
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
83 changes: 83 additions & 0 deletions
83
...in/src/main/java/io/trino/sql/planner/iterative/rule/PruneOrderByInWindowAggregation.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,83 @@ | ||
/* | ||
* 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.ImmutableMap; | ||
import io.trino.matching.Captures; | ||
import io.trino.matching.Pattern; | ||
import io.trino.metadata.Metadata; | ||
import io.trino.spi.function.FunctionKind; | ||
import io.trino.sql.planner.Symbol; | ||
import io.trino.sql.planner.iterative.Rule; | ||
import io.trino.sql.planner.plan.WindowNode; | ||
import io.trino.sql.planner.plan.WindowNode.Function; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static io.trino.sql.planner.plan.Patterns.window; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class PruneOrderByInWindowAggregation | ||
implements Rule<WindowNode> | ||
{ | ||
private static final Pattern<WindowNode> PATTERN = window(); | ||
private final Metadata metadata; | ||
|
||
public PruneOrderByInWindowAggregation(Metadata metadata) | ||
{ | ||
this.metadata = requireNonNull(metadata, "metadata is null"); | ||
} | ||
|
||
@Override | ||
public Pattern<WindowNode> getPattern() | ||
{ | ||
return PATTERN; | ||
} | ||
|
||
@Override | ||
public Result apply(WindowNode node, Captures captures, Context context) | ||
{ | ||
boolean anyRewritten = false; | ||
ImmutableMap.Builder<Symbol, Function> rewritten = ImmutableMap.builder(); | ||
for (Map.Entry<Symbol, Function> entry : node.getWindowFunctions().entrySet()) { | ||
Function function = entry.getValue(); | ||
// getAggregateFunctionImplementation can be expensive, so check it last. | ||
if (function.getOrderingScheme().isPresent() && | ||
function.getResolvedFunction().functionKind() == FunctionKind.AGGREGATE && | ||
!metadata.getAggregationFunctionMetadata(context.getSession(), function.getResolvedFunction()).isOrderSensitive()) { | ||
function = new Function( | ||
function.getResolvedFunction(), | ||
function.getArguments(), | ||
Optional.empty(), // prune | ||
function.getFrame(), | ||
function.isIgnoreNulls()); | ||
anyRewritten = true; | ||
} | ||
rewritten.put(entry.getKey(), function); | ||
} | ||
|
||
if (!anyRewritten) { | ||
return Result.empty(); | ||
} | ||
return Result.ofPlanNode(new WindowNode( | ||
node.getId(), | ||
node.getSource(), | ||
node.getSpecification(), | ||
rewritten.buildOrThrow(), | ||
node.getHashSymbol(), | ||
node.getPrePartitionedInputs(), | ||
node.getPreSortedOrderPrefix())); | ||
} | ||
} |
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
103 changes: 103 additions & 0 deletions
103
...rc/test/java/io/trino/sql/planner/iterative/rule/TestPruneOrderByInWindowAggregation.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,103 @@ | ||
/* | ||
* 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 com.google.common.collect.ImmutableMap; | ||
import io.trino.metadata.Metadata; | ||
import io.trino.metadata.ResolvedFunction; | ||
import io.trino.spi.connector.SortOrder; | ||
import io.trino.sql.ir.Reference; | ||
import io.trino.sql.planner.OrderingScheme; | ||
import io.trino.sql.planner.Symbol; | ||
import io.trino.sql.planner.iterative.rule.test.BaseRuleTest; | ||
import io.trino.sql.planner.iterative.rule.test.PlanBuilder; | ||
import io.trino.sql.planner.plan.DataOrganizationSpecification; | ||
import io.trino.sql.planner.plan.WindowNode; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static io.trino.metadata.TestMetadataManager.createTestMetadataManager; | ||
import static io.trino.spi.type.BigintType.BIGINT; | ||
import static io.trino.sql.analyzer.TypeSignatureProvider.fromTypes; | ||
import static io.trino.sql.planner.assertions.PlanMatchPattern.sort; | ||
import static io.trino.sql.planner.assertions.PlanMatchPattern.specification; | ||
import static io.trino.sql.planner.assertions.PlanMatchPattern.values; | ||
import static io.trino.sql.planner.assertions.PlanMatchPattern.window; | ||
import static io.trino.sql.planner.assertions.PlanMatchPattern.windowFunction; | ||
import static io.trino.sql.planner.plan.WindowNode.Frame.DEFAULT_FRAME; | ||
import static io.trino.sql.tree.SortItem.NullOrdering.LAST; | ||
import static io.trino.sql.tree.SortItem.Ordering.ASCENDING; | ||
|
||
public class TestPruneOrderByInWindowAggregation | ||
extends BaseRuleTest | ||
{ | ||
private static final Metadata METADATA = createTestMetadataManager(); | ||
|
||
@Test | ||
public void testBasics() | ||
{ | ||
tester().assertThat(new PruneOrderByInWindowAggregation(METADATA)) | ||
.on(this::buildWindowNode) | ||
.matches( | ||
window( | ||
windowMatcherBuilder -> windowMatcherBuilder | ||
.specification(specification( | ||
ImmutableList.of("key"), | ||
ImmutableList.of(), | ||
ImmutableMap.of())) | ||
.addFunction( | ||
"avg", | ||
windowFunction("avg", ImmutableList.of("input"), DEFAULT_FRAME)) | ||
.addFunction( | ||
"array_agg", | ||
windowFunction("array_agg", ImmutableList.of("input"), DEFAULT_FRAME, ImmutableList.of(sort("input", ASCENDING, LAST)))), | ||
values("input", "key", "keyHash", "mask"))); | ||
} | ||
|
||
private WindowNode buildWindowNode(PlanBuilder planBuilder) | ||
{ | ||
Symbol avg = planBuilder.symbol("avg"); | ||
Symbol arrayAgg = planBuilder.symbol("araray_agg"); | ||
Symbol input = planBuilder.symbol("input"); | ||
Symbol key = planBuilder.symbol("key"); | ||
Symbol keyHash = planBuilder.symbol("keyHash"); | ||
Symbol mask = planBuilder.symbol("mask"); | ||
List<Symbol> sourceSymbols = ImmutableList.of(input, key, keyHash, mask); | ||
|
||
ResolvedFunction avgFunction = createTestMetadataManager().resolveBuiltinFunction("avg", fromTypes(BIGINT)); | ||
ResolvedFunction arrayAggFunction = createTestMetadataManager().resolveBuiltinFunction("array_agg", fromTypes(BIGINT)); | ||
|
||
return planBuilder.window( | ||
new DataOrganizationSpecification(ImmutableList.of(planBuilder.symbol("key", BIGINT)), Optional.empty()), | ||
ImmutableMap.of( | ||
avg, new WindowNode.Function(avgFunction, | ||
ImmutableList.of(new Reference(BIGINT, "input")), | ||
Optional.of(new OrderingScheme( | ||
ImmutableList.of(new Symbol(BIGINT, "input")), | ||
ImmutableMap.of(new Symbol(BIGINT, "input"), SortOrder.ASC_NULLS_LAST))), | ||
DEFAULT_FRAME, | ||
false), | ||
arrayAgg, new WindowNode.Function(arrayAggFunction, | ||
ImmutableList.of(new Reference(BIGINT, "input")), | ||
Optional.of(new OrderingScheme( | ||
ImmutableList.of(new Symbol(BIGINT, "input")), | ||
ImmutableMap.of(new Symbol(BIGINT, "input"), SortOrder.ASC_NULLS_LAST))), | ||
DEFAULT_FRAME, | ||
false)), | ||
planBuilder.values(sourceSymbols, ImmutableList.of())); | ||
} | ||
} |