-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Rule to prune unreferenced source columns for TableExecuteNode
Replicate the logic from deprecated PruneUnreferencedOutputs which will be removed soon.
- Loading branch information
Showing
7 changed files
with
280 additions
and
0 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
54 changes: 54 additions & 0 deletions
54
...ain/src/main/java/io/trino/sql/planner/iterative/rule/PruneTableExecuteSourceColumns.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,54 @@ | ||
/* | ||
* 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.ImmutableSet; | ||
import io.trino.matching.Captures; | ||
import io.trino.matching.Pattern; | ||
import io.trino.sql.planner.PartitioningScheme; | ||
import io.trino.sql.planner.Symbol; | ||
import io.trino.sql.planner.iterative.Rule; | ||
import io.trino.sql.planner.plan.TableExecuteNode; | ||
|
||
import static io.trino.sql.planner.iterative.rule.Util.restrictChildOutputs; | ||
import static io.trino.sql.planner.plan.Patterns.tableExecute; | ||
|
||
public class PruneTableExecuteSourceColumns | ||
implements Rule<TableExecuteNode> | ||
{ | ||
private static final Pattern<TableExecuteNode> PATTERN = tableExecute(); | ||
|
||
@Override | ||
public Pattern<TableExecuteNode> getPattern() | ||
{ | ||
return PATTERN; | ||
} | ||
|
||
@Override | ||
public Result apply(TableExecuteNode tableExecuteNode, Captures captures, Context context) | ||
{ | ||
ImmutableSet.Builder<Symbol> requiredInputs = ImmutableSet.<Symbol>builder() | ||
.addAll(tableExecuteNode.getColumns()); | ||
|
||
if (tableExecuteNode.getPartitioningScheme().isPresent()) { | ||
PartitioningScheme partitioningScheme = tableExecuteNode.getPartitioningScheme().get(); | ||
partitioningScheme.getPartitioning().getColumns().forEach(requiredInputs::add); | ||
partitioningScheme.getHashColumn().ifPresent(requiredInputs::add); | ||
} | ||
|
||
return restrictChildOutputs(context.getIdAllocator(), tableExecuteNode, requiredInputs.build()) | ||
.map(Result::ofPlanNode) | ||
.orElse(Result.empty()); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
core/trino-main/src/main/java/io/trino/testing/TestingTableExecuteHandle.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,19 @@ | ||
/* | ||
* 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.testing; | ||
|
||
import io.trino.spi.connector.ConnectorTableExecuteHandle; | ||
|
||
public class TestingTableExecuteHandle | ||
implements ConnectorTableExecuteHandle {} |
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
78 changes: 78 additions & 0 deletions
78
core/trino-main/src/test/java/io/trino/sql/planner/assertions/TableExecuteMatcher.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,78 @@ | ||
/* | ||
* 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.assertions; | ||
|
||
import io.trino.Session; | ||
import io.trino.cost.StatsProvider; | ||
import io.trino.metadata.Metadata; | ||
import io.trino.sql.planner.Symbol; | ||
import io.trino.sql.planner.plan.PlanNode; | ||
import io.trino.sql.planner.plan.TableExecuteNode; | ||
|
||
import java.util.List; | ||
|
||
import static com.google.common.base.MoreObjects.toStringHelper; | ||
import static com.google.common.base.Preconditions.checkState; | ||
import static com.google.common.collect.ImmutableList.toImmutableList; | ||
import static io.trino.sql.planner.assertions.MatchResult.NO_MATCH; | ||
import static io.trino.sql.planner.assertions.MatchResult.match; | ||
|
||
public class TableExecuteMatcher | ||
implements Matcher | ||
{ | ||
private final List<String> columns; | ||
private final List<String> columnNames; | ||
|
||
public TableExecuteMatcher(List<String> columns, List<String> columnNames) | ||
{ | ||
this.columns = columns; | ||
this.columnNames = columnNames; | ||
} | ||
|
||
@Override | ||
public boolean shapeMatches(PlanNode node) | ||
{ | ||
return node instanceof TableExecuteNode; | ||
} | ||
|
||
@Override | ||
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) | ||
{ | ||
checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName()); | ||
|
||
TableExecuteNode tableExecuteNode = (TableExecuteNode) node; | ||
if (!tableExecuteNode.getColumnNames().equals(columnNames)) { | ||
return NO_MATCH; | ||
} | ||
|
||
if (!columns.stream() | ||
.map(symbol -> Symbol.from(symbolAliases.get(symbol))) | ||
.collect(toImmutableList()) | ||
.equals(tableExecuteNode.getColumns())) { | ||
return NO_MATCH; | ||
} | ||
|
||
return match(); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return toStringHelper(this) | ||
.add("columns", columns) | ||
.add("columnNames", columnNames) | ||
.toString(); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...src/test/java/io/trino/sql/planner/iterative/rule/TestPruneTableExecuteSourceColumns.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,88 @@ | ||
/* | ||
* 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.sql.planner.Symbol; | ||
import io.trino.sql.planner.iterative.rule.test.BaseRuleTest; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Optional; | ||
|
||
import static io.trino.sql.planner.assertions.PlanMatchPattern.expression; | ||
import static io.trino.sql.planner.assertions.PlanMatchPattern.strictProject; | ||
import static io.trino.sql.planner.assertions.PlanMatchPattern.tableExecute; | ||
import static io.trino.sql.planner.assertions.PlanMatchPattern.values; | ||
|
||
public class TestPruneTableExecuteSourceColumns | ||
extends BaseRuleTest | ||
{ | ||
@Test | ||
public void testNotAllInputsReferenced() | ||
{ | ||
tester().assertThat(new PruneTableExecuteSourceColumns()) | ||
.on(p -> { | ||
Symbol a = p.symbol("a"); | ||
Symbol b = p.symbol("b"); | ||
return p.tableExecute( | ||
ImmutableList.of(a), | ||
ImmutableList.of("column_a"), | ||
p.values(a, b)); | ||
}) | ||
.matches( | ||
tableExecute( | ||
ImmutableList.of("a"), | ||
ImmutableList.of("column_a"), | ||
strictProject( | ||
ImmutableMap.of("a", expression("a")), | ||
values("a", "b")))); | ||
} | ||
|
||
@Test | ||
public void testAllInputsReferenced() | ||
{ | ||
tester().assertThat(new PruneTableExecuteSourceColumns()) | ||
.on(p -> { | ||
Symbol a = p.symbol("a"); | ||
Symbol b = p.symbol("b"); | ||
return p.tableExecute( | ||
ImmutableList.of(a, b), | ||
ImmutableList.of("column_a", "column_b"), | ||
p.values(a, b)); | ||
}) | ||
.doesNotFire(); | ||
} | ||
|
||
@Test | ||
public void testDoNotPrunePartitioningSchemeSymbols() | ||
{ | ||
tester().assertThat(new PruneTableExecuteSourceColumns()) | ||
.on(p -> { | ||
Symbol a = p.symbol("a"); | ||
Symbol partition = p.symbol("partition"); | ||
Symbol hash = p.symbol("hash"); | ||
return p.tableExecute( | ||
ImmutableList.of(a), | ||
ImmutableList.of("column_a"), | ||
Optional.of(p.partitioningScheme( | ||
ImmutableList.of(partition, hash), | ||
ImmutableList.of(partition), | ||
hash)), | ||
Optional.empty(), | ||
p.values(a, partition, hash)); | ||
}) | ||
.doesNotFire(); | ||
} | ||
} |
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