Skip to content
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 also when not wrapped with exchange #13794

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
import io.trino.sql.planner.iterative.rule.RemoveEmptyGlobalAggregation;
import io.trino.sql.planner.iterative.rule.RemoveEmptyTableExecute;
import io.trino.sql.planner.iterative.rule.RemoveEmptyUnionBranches;
import io.trino.sql.planner.iterative.rule.RemoveEmptyUpdate;
import io.trino.sql.planner.iterative.rule.RemoveEmptyUpdateRuleSet;
import io.trino.sql.planner.iterative.rule.RemoveFullSample;
import io.trino.sql.planner.iterative.rule.RemoveRedundantDistinctLimit;
import io.trino.sql.planner.iterative.rule.RemoveRedundantEnforceSingleRowNode;
Expand Down Expand Up @@ -865,9 +865,9 @@ public PlanOptimizers(
statsCalculator,
costCalculator,
ImmutableSet.<Rule<?>>builder()
// Run RemoveEmptyDeleteRuleSet, RemoveEmptyUpdate and RemoveEmptyTableExecute after table scan is removed by PickTableLayout/AddExchanges
// Run RemoveEmptyDeleteRuleSet, RemoveEmptyUpdateRuleSet and RemoveEmptyTableExecute after table scan is removed by PickTableLayout/AddExchanges
.addAll(RemoveEmptyDeleteRuleSet.rules())
.add(new RemoveEmptyUpdate())
.addAll(RemoveEmptyUpdateRuleSet.rules())
.add(new RemoveEmptyTableExecute())
.build()));

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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.ImmutableSet;
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 java.util.Set;

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;
import static java.util.Objects.requireNonNull;

/**
* 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 the
* entire node is being replaced with a values node.
* <p>
* Transforms
* <pre>
* - TableFinish
* - Exchange (optional)
* - Delete
* - empty Values
* </pre>
* into
* <pre>
* - Values (0)
* </pre>
*/
public final class RemoveEmptyUpdateRuleSet
{
private RemoveEmptyUpdateRuleSet() {}

public static Set<Rule<?>> rules()
{
return ImmutableSet.of(
remoteEmptyUpdateRule(),
removeEmptyUpdateWithExchangeRule());
}

static Rule<TableFinishNode> remoteEmptyUpdateRule()
{
return new RemoveEmptyUpdate(tableFinish()
.with(source().matching(update()
.with(source().matching(emptyValues())))));
}

static Rule<TableFinishNode> removeEmptyUpdateWithExchangeRule()
{
return new RemoveEmptyUpdate(tableFinish()
.with(source().matching(exchange()
.with(source().matching(update()
.with(source().matching(emptyValues())))))));
}

private static final class RemoveEmptyUpdate
implements Rule<TableFinishNode>
{
private final Pattern<TableFinishNode> pattern;

private RemoveEmptyUpdate(Pattern<TableFinishNode> pattern)
{
this.pattern = requireNonNull(pattern, "pattern is null");
}

@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"))))));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,23 @@
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;
import io.trino.sql.planner.iterative.rule.test.BaseRuleTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static io.trino.sql.planner.iterative.rule.RemoveEmptyUpdateRuleSet.remoteEmptyUpdateRule;
import static io.trino.sql.planner.iterative.rule.RemoveEmptyUpdateRuleSet.removeEmptyUpdateWithExchangeRule;
import static io.trino.testing.DataProviders.toDataProvider;
import static io.trino.testing.TestingHandles.TEST_CATALOG_HANDLE;

public class TestRemoveEmptyUpdate
public class TestRemoveEmptyUpdateRuleSet
extends BaseRuleTest
{
@Test
public void testRuleDoesNotFireOnTableScan()
@Test(dataProvider = "rules")
public void testRuleDoesNotFireOnTableScan(Rule<?> rule)
{
tester().assertThat(new RemoveEmptyUpdate())
tester().assertThat(rule)
.on(p -> p.tableUpdate(
new SchemaTableName("sch", "tab"),
p.tableScan(
Expand All @@ -42,12 +47,22 @@ public void testRuleDoesNotFireOnTableScan()
p.symbol("a", BigintType.BIGINT),
ImmutableList.of(p.symbol("a", BigintType.BIGINT))))
.doesNotFire();
tester().assertThat(rule)
.on(p -> p.tableWithExchangeUpdate(
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())
tester().assertThat(remoteEmptyUpdateRule())
.on(p -> p.tableUpdate(
new SchemaTableName("sch", "tab"),
p.values(),
Expand All @@ -56,4 +71,24 @@ public void testRuleFiresWhenAppliedOnEmptyValuesNode()
.matches(
PlanMatchPattern.values(ImmutableMap.of("a", 0)));
}

@Test
public void testExchangeRuleFiresWhenAppliedOnEmptyValuesNode()
{
tester().assertThat(removeEmptyUpdateWithExchangeRule())
.on(p -> p.tableWithExchangeUpdate(
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)));
}

@DataProvider
public static Object[][] rules()
{
return RemoveEmptyUpdateRuleSet.rules().stream()
.collect(toDataProvider());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,34 @@ public TableFinishNode tableWithExchangeDelete(SchemaTableName schemaTableName,
Optional.empty());
}

public TableFinishNode tableWithExchangeUpdate(SchemaTableName schemaTableName, PlanNode updateSource, Symbol updateRowId, List<Symbol> columnsToBeUpdated)
{
UpdateTarget updateTarget = updateTarget(
schemaTableName,
columnsToBeUpdated.stream()
.map(Symbol::getName)
.collect(toImmutableList()));
return new TableFinishNode(
idAllocator.getNextId(),
exchange(e -> e
.addSource(new UpdateNode(
idAllocator.getNextId(),
updateSource,
updateTarget,
updateRowId,
ImmutableList.<Symbol>builder()
.addAll(columnsToBeUpdated)
.add(updateRowId)
.build(),
ImmutableList.of(updateRowId)))
.addInputsSet(updateRowId)
.singleDistributionPartitioningScheme(updateRowId)),
updateTarget,
updateRowId,
Optional.empty(),
Optional.empty());
}

public TableFinishNode tableWithExchangeCreate(WriterTarget target, PlanNode source, Symbol rowCountSymbol, PartitioningScheme partitioningScheme)
{
return new TableFinishNode(
Expand Down Expand Up @@ -781,19 +809,16 @@ public TableFinishNode tableUpdate(SchemaTableName schemaTableName, PlanNode upd
.collect(toImmutableList()));
return new TableFinishNode(
idAllocator.getNextId(),
exchange(e -> e
.addSource(new UpdateNode(
idAllocator.getNextId(),
updateSource,
updateTarget,
updateRowId,
ImmutableList.<Symbol>builder()
.addAll(columnsToBeUpdated)
.add(updateRowId)
.build(),
ImmutableList.of(updateRowId)))
.addInputsSet(updateRowId)
.singleDistributionPartitioningScheme(updateRowId)),
new UpdateNode(
idAllocator.getNextId(),
updateSource,
updateTarget,
updateRowId,
ImmutableList.<Symbol>builder()
.addAll(columnsToBeUpdated)
.add(updateRowId)
.build(),
ImmutableList.of(updateRowId)),
updateTarget,
updateRowId,
Optional.empty(),
Expand Down