-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Remove empty UPDATE
#13755
Merged
Merged
Remove empty UPDATE
#13755
Changes from all commits
Commits
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 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
72 changes: 72 additions & 0 deletions
72
core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/RemoveEmptyUpdate.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,72 @@ | ||
/* | ||
* 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 io.trino.matching.Captures; | ||
import io.trino.matching.Pattern; | ||
import io.trino.sql.planner.iterative.Rule; | ||
import io.trino.sql.planner.plan.TableFinishNode; | ||
import io.trino.sql.planner.plan.ValuesNode; | ||
import io.trino.sql.tree.GenericLiteral; | ||
import io.trino.sql.tree.Row; | ||
|
||
import static io.trino.sql.planner.plan.Patterns.emptyValues; | ||
import static io.trino.sql.planner.plan.Patterns.exchange; | ||
import static io.trino.sql.planner.plan.Patterns.source; | ||
import static io.trino.sql.planner.plan.Patterns.tableFinish; | ||
import static io.trino.sql.planner.plan.Patterns.update; | ||
|
||
/** | ||
* If the predicate for an update is optimized to false, the target table | ||
* scan of the update will be replaced with an empty values node. | ||
* This type of plan cannot be executed and is meaningless anyway, so we | ||
* replace the entire plan node with a `values` node. | ||
* <p> | ||
* Transforms | ||
* <pre> | ||
* - TableFinish | ||
* - Exchange | ||
* - Update | ||
* - empty Values | ||
* </pre> | ||
* into | ||
* <pre> | ||
* - Values (0) | ||
* </pre> | ||
*/ | ||
public class RemoveEmptyUpdate | ||
implements Rule<TableFinishNode> | ||
{ | ||
private static final Pattern<TableFinishNode> PATTERN = tableFinish() | ||
.with(source().matching(exchange() | ||
.with(source().matching(update() | ||
.with(source().matching(emptyValues())))))); | ||
|
||
@Override | ||
public Pattern<TableFinishNode> getPattern() | ||
{ | ||
return PATTERN; | ||
} | ||
|
||
@Override | ||
public Result apply(TableFinishNode node, Captures captures, Context context) | ||
{ | ||
return Result.ofPlanNode( | ||
new ValuesNode( | ||
node.getId(), | ||
node.getOutputSymbols(), | ||
ImmutableList.of(new Row(ImmutableList.of(new GenericLiteral("BIGINT", "0")))))); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
core/trino-main/src/test/java/io/trino/sql/planner/iterative/rule/TestRemoveEmptyUpdate.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,59 @@ | ||
/* | ||
* 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.TableHandle; | ||
import io.trino.plugin.tpch.TpchTableHandle; | ||
import io.trino.plugin.tpch.TpchTransactionHandle; | ||
import io.trino.spi.connector.SchemaTableName; | ||
import io.trino.spi.type.BigintType; | ||
import io.trino.sql.planner.assertions.PlanMatchPattern; | ||
import io.trino.sql.planner.iterative.rule.test.BaseRuleTest; | ||
import org.testng.annotations.Test; | ||
|
||
import static io.trino.testing.TestingHandles.TEST_CATALOG_HANDLE; | ||
|
||
public class TestRemoveEmptyUpdate | ||
extends BaseRuleTest | ||
{ | ||
@Test | ||
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. I'm inexperienced in the query planning area of Trino so I'd very much benefit of some hints regarding to improve/extend the tests for this fix. |
||
public void testRuleDoesNotFireOnTableScan() | ||
{ | ||
tester().assertThat(new RemoveEmptyUpdate()) | ||
.on(p -> p.tableUpdate( | ||
new SchemaTableName("sch", "tab"), | ||
p.tableScan( | ||
new TableHandle(TEST_CATALOG_HANDLE, new TpchTableHandle("sf1", "nation", 1.0), TpchTransactionHandle.INSTANCE), | ||
ImmutableList.of(), | ||
ImmutableMap.of()), | ||
p.symbol("a", BigintType.BIGINT), | ||
ImmutableList.of(p.symbol("a", BigintType.BIGINT)))) | ||
.doesNotFire(); | ||
} | ||
|
||
@Test | ||
public void testRuleFiresWhenAppliedOnEmptyValuesNode() | ||
{ | ||
tester().assertThat(new RemoveEmptyUpdate()) | ||
.on(p -> p.tableUpdate( | ||
new SchemaTableName("sch", "tab"), | ||
p.values(), | ||
p.symbol("a", BigintType.BIGINT), | ||
ImmutableList.of(p.symbol("a", BigintType.BIGINT)))) | ||
.matches( | ||
PlanMatchPattern.values(ImmutableMap.of("a", 0))); | ||
} | ||
} |
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
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.
for Delete we have a rule set (since we match empty delete with and without an exchange).
wonder whether this is some left over (or otherwise: why don't we need same for Update?)
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.
followed up with #13794