Skip to content
Merged
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
@@ -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 io.trino.spi.connector.CatalogHandle;
import io.trino.spi.connector.SchemaTableName;
import io.trino.sql.planner.Partitioning;
import io.trino.sql.planner.PartitioningScheme;
import io.trino.sql.planner.Symbol;
import io.trino.sql.planner.iterative.Rule;
import io.trino.sql.planner.iterative.rule.test.BaseRuleTest;
import io.trino.sql.planner.iterative.rule.test.PlanBuilder;
import io.trino.sql.planner.plan.Assignments;
import io.trino.sql.planner.plan.ExchangeNode;
import io.trino.sql.planner.plan.PlanNode;
import io.trino.sql.planner.plan.TableFinishNode;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.List;

import static io.trino.sql.planner.SystemPartitioningHandle.SINGLE_DISTRIBUTION;
import static io.trino.sql.planner.assertions.PlanMatchPattern.values;
import static io.trino.sql.planner.iterative.rule.RemoveEmptyMergeWriterRuleSet.removeEmptyMergeWriterRule;
import static io.trino.sql.planner.iterative.rule.RemoveEmptyMergeWriterRuleSet.removeEmptyMergeWriterWithExchangeRule;

public class TestRemoveEmptyMergeWriterRuleSet
extends BaseRuleTest
{
private CatalogHandle catalogHandle;
private SchemaTableName schemaTableName;

@BeforeClass
public void setup()
{
catalogHandle = tester().getCurrentCatalogHandle();
schemaTableName = new SchemaTableName("schema", "table");
}

@Test
public void testRemoveEmptyMergeRewrite()
{
testRemoveEmptyMergeRewrite(removeEmptyMergeWriterRule(), false);
}

@Test
public void testRemoveEmptyMergeRewriteWithExchange()
{
testRemoveEmptyMergeRewrite(removeEmptyMergeWriterWithExchangeRule(), true);
}

private void testRemoveEmptyMergeRewrite(Rule<TableFinishNode> rule, boolean planWithExchange)
{
tester().assertThat(rule)
.on(p -> {
Symbol mergeRow = p.symbol("merge_row");
Symbol rowId = p.symbol("row_id");
Symbol rowCount = p.symbol("row_count");

PlanNode merge = p.merge(
schemaTableName,
p.exchange(e -> e
.addSource(
p.project(
Assignments.builder()
.putIdentity(mergeRow)
.putIdentity(rowId)
.putIdentity(rowCount)
.build(),
p.values(mergeRow, rowId, rowCount)))
.addInputsSet(mergeRow, rowId, rowCount)
.partitioningScheme(
new PartitioningScheme(
Partitioning.create(SINGLE_DISTRIBUTION, List.of()),
List.of(mergeRow, rowId, rowCount)))),
mergeRow,
rowId,
List.of(rowCount));
return p.tableFinish(
planWithExchange ? withExchange(p, merge, rowCount) : merge,
p.createTarget(catalogHandle, schemaTableName, true, true),
rowCount);
})
.matches(values("A"));
}

private ExchangeNode withExchange(PlanBuilder planBuilder, PlanNode source, Symbol symbol)
{
return planBuilder.exchange(e -> e
.addSource(source)
.addInputsSet(symbol)
.partitioningScheme(new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, List.of()), List.of(symbol))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -685,10 +685,20 @@ public TableScanNode build()
}
}

public TableFinishNode tableWithExchangeCreate(WriterTarget target, PlanNode source, Symbol rowCountSymbol, PartitioningScheme partitioningScheme)
public TableFinishNode tableFinish(PlanNode source, WriterTarget target, Symbol rowCountSymbol)
{
return new TableFinishNode(
idAllocator.getNextId(),
source,
target,
rowCountSymbol,
Optional.empty(),
Optional.empty());
}

public TableFinishNode tableWithExchangeCreate(WriterTarget target, PlanNode source, Symbol rowCountSymbol)
{
return tableFinish(
exchange(e -> e
.addSource(tableWriter(
ImmutableList.of(rowCountSymbol),
Expand All @@ -699,11 +709,9 @@ public TableFinishNode tableWithExchangeCreate(WriterTarget target, PlanNode sou
source,
rowCountSymbol))
.addInputsSet(rowCountSymbol)
.partitioningScheme(partitioningScheme)),
.partitioningScheme(new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()), ImmutableList.of(rowCountSymbol)))),
target,
rowCountSymbol,
Optional.empty(),
Optional.empty());
rowCountSymbol);
}

public CreateTarget createTarget(CatalogHandle catalogHandle, SchemaTableName schemaTableName, boolean reportingWrittenBytesSupported, boolean multipleWritersPerPartitionSupported, OptionalInt maxWriterTasks)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ public void testScaledWritersUsedAndTargetSupportsIt(PartitioningHandle scaledWr
.source(planBuilder.tableWithExchangeCreate(
planBuilder.createTarget(catalogSupportingScaledWriters, schemaTableName, true, true),
tableWriterSource,
symbol,
new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()), ImmutableList.of(symbol)))));
symbol)));
validatePlan(root);
}

Expand All @@ -146,8 +145,7 @@ public void testScaledWritersUsedAndTargetDoesNotSupportReportingWrittenBytes(Pa
.source(planBuilder.tableWithExchangeCreate(
planBuilder.createTarget(catalogNotSupportingScaledWriters, schemaTableName, false, true),
tableWriterSource,
symbol,
new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()), ImmutableList.of(symbol)))));
symbol)));
assertThatThrownBy(() -> validatePlan(root))
.isInstanceOf(IllegalStateException.class)
.hasMessage("The scaled writer partitioning scheme is set but writer target no_bytes_written_reported:INSTANCE doesn't support reporting physical written bytes");
Expand Down Expand Up @@ -176,8 +174,7 @@ public void testScaledWritersWithMultipleSourceExchangesAndTargetDoesNotSupportR
.source(planBuilder.tableWithExchangeCreate(
planBuilder.createTarget(catalogNotSupportingScaledWriters, schemaTableName, false, true),
tableWriterSource,
symbol,
new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()), ImmutableList.of(symbol)))));
symbol)));
assertThatThrownBy(() -> validatePlan(root))
.isInstanceOf(IllegalStateException.class)
.hasMessage("The scaled writer partitioning scheme is set but writer target no_bytes_written_reported:INSTANCE doesn't support reporting physical written bytes");
Expand Down Expand Up @@ -206,8 +203,7 @@ public void testScaledWritersWithMultipleSourceExchangesAndTargetSupportIt(Parti
.source(planBuilder.tableWithExchangeCreate(
planBuilder.createTarget(catalogSupportingScaledWriters, schemaTableName, true, true),
tableWriterSource,
symbol,
new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()), ImmutableList.of(symbol)))));
symbol)));
validatePlan(root);
}

Expand All @@ -228,8 +224,7 @@ public void testScaledWritersUsedAboveTableWriterInThePlanTree(PartitioningHandl
.source(planBuilder.tableWithExchangeCreate(
planBuilder.createTarget(catalogNotSupportingScaledWriters, schemaTableName, false, true),
tableWriterSource,
symbol,
new PartitioningScheme(Partitioning.create(scaledWriterPartitionHandle, ImmutableList.of()), ImmutableList.of(symbol)))));
symbol)));
validatePlan(root);
}

Expand Down Expand Up @@ -257,8 +252,7 @@ public void testScaledWritersTwoTableWritersNodes(PartitioningHandle scaledWrite
.source(planBuilder.tableWithExchangeCreate(
planBuilder.createTarget(catalogNotSupportingScaledWriters, schemaTableName, false, true),
tableWriterSource,
symbol,
new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()), ImmutableList.of(symbol)))));
symbol)));
assertThatThrownBy(() -> validatePlan(root))
.isInstanceOf(IllegalStateException.class)
.hasMessage("The scaled writer partitioning scheme is set but writer target no_bytes_written_reported:INSTANCE doesn't support reporting physical written bytes");
Expand All @@ -281,8 +275,7 @@ public void testScaledWriterUsedAndTargetDoesNotSupportMultipleWritersPerPartiti
.source(planBuilder.tableWithExchangeCreate(
planBuilder.createTarget(catalogNotSupportingScaledWriters, schemaTableName, true, false),
tableWriterSource,
symbol,
new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()), ImmutableList.of(symbol)))));
symbol)));

if (scaledWriterPartitionHandle == SCALED_WRITER_ROUND_ROBIN_DISTRIBUTION) {
validatePlan(root);
Expand Down Expand Up @@ -317,8 +310,7 @@ public void testScaledWriterWithMultipleSourceExchangesAndTargetDoesNotSupportMu
.source(planBuilder.tableWithExchangeCreate(
planBuilder.createTarget(catalogNotSupportingScaledWriters, schemaTableName, true, false),
tableWriterSource,
symbol,
new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()), ImmutableList.of(symbol)))));
symbol)));

if (scaledWriterPartitionHandle == SCALED_WRITER_ROUND_ROBIN_DISTRIBUTION) {
validatePlan(root);
Expand Down