io.trino.sql.planner.iterative.rule.RemoveEmptyMergeWriterRuleSet Maven / Gradle / Ivy
/*
* 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.ir.Constant;
import io.trino.sql.ir.Row;
import io.trino.sql.planner.iterative.Rule;
import io.trino.sql.planner.plan.TableFinishNode;
import io.trino.sql.planner.plan.ValuesNode;
import java.util.Set;
import static io.trino.spi.type.BigintType.BIGINT;
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.mergeWriter;
import static io.trino.sql.planner.plan.Patterns.project;
import static io.trino.sql.planner.plan.Patterns.source;
import static io.trino.sql.planner.plan.Patterns.tableFinish;
import static java.util.Objects.requireNonNull;
/**
* If the predicate for a mergeWriter is optimized to false, the target table scan
* of the mergeWriter will be replaced with an empty values node. This type of
* plan cannot be executed and is meaningless anyway, so we replace the
* entire thing with a values node.
*
* Transforms
*
* - TableFinish
* - Exchange (optional)
* - MergeWriter
* - Exchange
* - Project
* - empty Values
*
* into
*
* - Values (0)
*
*/
public final class RemoveEmptyMergeWriterRuleSet
{
private RemoveEmptyMergeWriterRuleSet() {}
public static Set> rules()
{
return ImmutableSet.of(
removeEmptyMergeWriterRule(),
removeEmptyMergeWriterWithExchangeRule());
}
static Rule removeEmptyMergeWriterRule()
{
return new RemoveEmptyMergeWriter(tableFinish()
.with(source().matching(mergeWriter()
.with(source().matching(exchange()
.with(source().matching(project()
.with(source().matching(emptyValues())))))))));
}
static Rule removeEmptyMergeWriterWithExchangeRule()
{
return new RemoveEmptyMergeWriter(tableFinish()
.with(source().matching(exchange()
.with(source().matching(mergeWriter()
.with(source().matching(exchange()
.with(source().matching(project()
.with(source().matching(emptyValues())))))))))));
}
private static final class RemoveEmptyMergeWriter
implements Rule
{
private final Pattern pattern;
private RemoveEmptyMergeWriter(Pattern pattern)
{
this.pattern = requireNonNull(pattern, "pattern is null");
}
@Override
public Pattern 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 Constant(BIGINT, 0L))))));
}
}
}