-
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
226 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
80 changes: 80 additions & 0 deletions
80
...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,80 @@ | ||
/* | ||
* 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.Maps; | ||
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 java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
|
||
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) | ||
{ | ||
Predicate<WindowNode.Function> isOrderInsensitive = windowFunction -> windowFunction.getResolvedFunction().functionKind() == FunctionKind.AGGREGATE && | ||
!metadata.getAggregationFunctionMetadata(context.getSession(), windowFunction.getResolvedFunction()).isOrderSensitive(); | ||
|
||
if (node.getWindowFunctions().values().stream().noneMatch(isOrderInsensitive)) { | ||
return Result.empty(); | ||
} | ||
|
||
Map<Symbol, WindowNode.Function> prunedFunctions = Maps.transformValues(node.getWindowFunctions(), windowFunction -> { | ||
if (isOrderInsensitive.test(windowFunction)) { | ||
return new WindowNode.Function( | ||
windowFunction.getResolvedFunction(), | ||
windowFunction.getArguments(), | ||
Optional.empty(), // prune | ||
windowFunction.getFrame(), | ||
windowFunction.isIgnoreNulls()); | ||
} | ||
return windowFunction; | ||
}); | ||
|
||
return Result.ofPlanNode(new WindowNode( | ||
node.getId(), | ||
node.getSource(), | ||
node.getSpecification(), | ||
prunedFunctions, | ||
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
104 changes: 104 additions & 0 deletions
104
...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,104 @@ | ||
/* | ||
* 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; | ||
import static io.trino.type.UnknownType.UNKNOWN; | ||
|
||
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(UNKNOWN, "input")), | ||
ImmutableMap.of(new Symbol(UNKNOWN, "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(UNKNOWN, "input")), | ||
ImmutableMap.of(new Symbol(UNKNOWN, "input"), SortOrder.ASC_NULLS_LAST))), | ||
DEFAULT_FRAME, | ||
false)), | ||
planBuilder.values(sourceSymbols, ImmutableList.of())); | ||
} | ||
} |