Search in sources :

Example 6 with RowNumberNode

use of io.trino.sql.planner.plan.RowNumberNode in project trino by trinodb.

the class PushdownFilterIntoRowNumber method apply.

@Override
public Result apply(FilterNode node, Captures captures, Context context) {
    Session session = context.getSession();
    TypeProvider types = context.getSymbolAllocator().getTypes();
    DomainTranslator.ExtractionResult extractionResult = DomainTranslator.getExtractionResult(plannerContext, session, node.getPredicate(), types);
    TupleDomain<Symbol> tupleDomain = extractionResult.getTupleDomain();
    RowNumberNode source = captures.get(CHILD);
    Symbol rowNumberSymbol = source.getRowNumberSymbol();
    OptionalInt upperBound = extractUpperBound(tupleDomain, rowNumberSymbol);
    if (upperBound.isEmpty()) {
        return Result.empty();
    }
    if (upperBound.getAsInt() <= 0) {
        return Result.ofPlanNode(new ValuesNode(node.getId(), node.getOutputSymbols(), ImmutableList.of()));
    }
    RowNumberNode merged = mergeLimit(source, upperBound.getAsInt());
    boolean needRewriteSource = !merged.getMaxRowCountPerPartition().equals(source.getMaxRowCountPerPartition());
    if (needRewriteSource) {
        source = merged;
    }
    if (!allRowNumberValuesInDomain(tupleDomain, rowNumberSymbol, source.getMaxRowCountPerPartition().get())) {
        if (needRewriteSource) {
            return Result.ofPlanNode(new FilterNode(node.getId(), source, node.getPredicate()));
        } else {
            return Result.empty();
        }
    }
    TupleDomain<Symbol> newTupleDomain = tupleDomain.filter((symbol, domain) -> !symbol.equals(rowNumberSymbol));
    Expression newPredicate = ExpressionUtils.combineConjuncts(plannerContext.getMetadata(), extractionResult.getRemainingExpression(), new DomainTranslator(plannerContext).toPredicate(session, newTupleDomain));
    if (newPredicate.equals(BooleanLiteral.TRUE_LITERAL)) {
        return Result.ofPlanNode(source);
    }
    if (!newPredicate.equals(node.getPredicate())) {
        return Result.ofPlanNode(new FilterNode(node.getId(), source, newPredicate));
    }
    return Result.empty();
}
Also used : ValuesNode(io.trino.sql.planner.plan.ValuesNode) Expression(io.trino.sql.tree.Expression) Symbol(io.trino.sql.planner.Symbol) FilterNode(io.trino.sql.planner.plan.FilterNode) TypeProvider(io.trino.sql.planner.TypeProvider) DomainTranslator(io.trino.sql.planner.DomainTranslator) RowNumberNode(io.trino.sql.planner.plan.RowNumberNode) OptionalInt(java.util.OptionalInt) Session(io.trino.Session)

Aggregations

RowNumberNode (io.trino.sql.planner.plan.RowNumberNode)6 Symbol (io.trino.sql.planner.Symbol)4 FilterNode (io.trino.sql.planner.plan.FilterNode)3 ProjectNode (io.trino.sql.planner.plan.ProjectNode)3 Expression (io.trino.sql.tree.Expression)3 Session (io.trino.Session)2 DomainTranslator (io.trino.sql.planner.DomainTranslator)2 ValuesNode (io.trino.sql.planner.plan.ValuesNode)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 OptionalInt (java.util.OptionalInt)2 MoreObjects.toStringHelper (com.google.common.base.MoreObjects.toStringHelper)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 HashBiMap (com.google.common.collect.HashBiMap)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 StatsProvider (io.trino.cost.StatsProvider)1 Capture (io.trino.matching.Capture)1 Capture.newCapture (io.trino.matching.Capture.newCapture)1