Search in sources :

Example 1 with TopNNode

use of io.prestosql.spi.plan.TopNNode in project hetu-core by openlookeng.

the class TopNMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    TopNNode topNNode = (TopNNode) node;
    if (topNNode.getCount() != count) {
        return NO_MATCH;
    }
    if (!orderingSchemeMatches(orderBy, topNNode.getOrderingScheme(), symbolAliases)) {
        return NO_MATCH;
    }
    if (topNNode.getStep() != step) {
        return NO_MATCH;
    }
    return match();
}
Also used : TopNNode(io.prestosql.spi.plan.TopNNode)

Example 2 with TopNNode

use of io.prestosql.spi.plan.TopNNode in project hetu-core by openlookeng.

the class TestEffectivePredicateExtractor method testTopN.

@Test
public void testTopN() {
    PlanNode node = new TopNNode(newId(), filter(baseTableScan, and(equals(AE, BE), equals(BE, CE), lessThan(CE, bigintLiteral(10)))), 1, new OrderingScheme(ImmutableList.of(A), ImmutableMap.of(A, SortOrder.ASC_NULLS_LAST)), TopNNode.Step.PARTIAL);
    Expression effectivePredicate = effectivePredicateExtractor.extract(SESSION, node, TypeProvider.empty(), typeAnalyzer);
    // Pass through
    assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjunctsSet(equals(AE, BE), equals(BE, CE), lessThan(CE, bigintLiteral(10))));
}
Also used : OrderingScheme(io.prestosql.spi.plan.OrderingScheme) PlanNode(io.prestosql.spi.plan.PlanNode) CallExpression(io.prestosql.spi.relation.CallExpression) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) InListExpression(io.prestosql.sql.tree.InListExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Expression(io.prestosql.sql.tree.Expression) TopNNode(io.prestosql.spi.plan.TopNNode) Test(org.testng.annotations.Test)

Example 3 with TopNNode

use of io.prestosql.spi.plan.TopNNode in project hetu-core by openlookeng.

the class TopNStatsRule method doCalculate.

@Override
protected Optional<PlanNodeStatsEstimate> doCalculate(TopNNode node, StatsProvider statsProvider, Lookup lookup, Session session, TypeProvider types) {
    PlanNodeStatsEstimate sourceStats = statsProvider.getStats(node.getSource());
    double rowCount = sourceStats.getOutputRowCount();
    if (rowCount <= node.getCount()) {
        return Optional.of(sourceStats);
    }
    long limitCount = node.getCount();
    PlanNodeStatsEstimate resultStats = PlanNodeStatsEstimate.buildFrom(sourceStats).setOutputRowCount(limitCount).build();
    if (limitCount == 0) {
        return Optional.of(resultStats);
    }
    // augment null fraction estimation for first ORDER BY symbol
    // Assuming not empty list
    Symbol firstOrderSymbol = node.getOrderingScheme().getOrderBy().get(0);
    SortOrder sortOrder = node.getOrderingScheme().getOrdering(firstOrderSymbol);
    resultStats = resultStats.mapSymbolColumnStatistics(firstOrderSymbol, symbolStats -> {
        SymbolStatsEstimate.Builder newStats = SymbolStatsEstimate.buildFrom(symbolStats);
        double nullCount = rowCount * symbolStats.getNullsFraction();
        if (sortOrder.isNullsFirst()) {
            if (nullCount > limitCount) {
                newStats.setNullsFraction(1.0);
            } else {
                newStats.setNullsFraction(nullCount / limitCount);
            }
        } else {
            double nonNullCount = (rowCount - nullCount);
            if (nonNullCount > limitCount) {
                newStats.setNullsFraction(0.0);
            } else {
                newStats.setNullsFraction((limitCount - nonNullCount) / limitCount);
            }
        }
        return newStats.build();
    });
    // TopN actually limits (or when there was no row count estimated for source)
    return Optional.of(resultStats);
}
Also used : Symbol(io.prestosql.spi.plan.Symbol) Patterns.topN(io.prestosql.sql.planner.plan.Patterns.topN) Lookup(io.prestosql.sql.planner.iterative.Lookup) TopNNode(io.prestosql.spi.plan.TopNNode) Session(io.prestosql.Session) TypeProvider(io.prestosql.sql.planner.TypeProvider) Optional(java.util.Optional) Pattern(io.prestosql.matching.Pattern) SortOrder(io.prestosql.spi.block.SortOrder) Symbol(io.prestosql.spi.plan.Symbol) SortOrder(io.prestosql.spi.block.SortOrder)

Example 4 with TopNNode

use of io.prestosql.spi.plan.TopNNode in project hetu-core by openlookeng.

the class PushTopNThroughProject method apply.

@Override
public Result apply(TopNNode parent, Captures captures, Context context) {
    ProjectNode projectNode = captures.get(PROJECT_CHILD);
    // do not push topN between projection and filter(table scan) so that they can be merged into a PageProcessor
    PlanNode projectSource = context.getLookup().resolve(projectNode.getSource());
    if (projectSource instanceof FilterNode) {
        PlanNode filterSource = context.getLookup().resolve(((FilterNode) projectSource).getSource());
        if (filterSource instanceof TableScanNode) {
            return Result.empty();
        }
    }
    Optional<SymbolMapper> symbolMapper = symbolMapper(parent.getOrderingScheme().getOrderBy(), projectNode.getAssignments());
    if (!symbolMapper.isPresent()) {
        return Result.empty();
    }
    TopNNode mappedTopN = symbolMapper.get().map(parent, projectNode.getSource(), context.getIdAllocator().getNextId());
    return Result.ofPlanNode(projectNode.replaceChildren(ImmutableList.of(mappedTopN)));
}
Also used : PlanNode(io.prestosql.spi.plan.PlanNode) TableScanNode(io.prestosql.spi.plan.TableScanNode) SymbolMapper(io.prestosql.sql.planner.optimizations.SymbolMapper) FilterNode(io.prestosql.spi.plan.FilterNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) TopNNode(io.prestosql.spi.plan.TopNNode)

Example 5 with TopNNode

use of io.prestosql.spi.plan.TopNNode in project hetu-core by openlookeng.

the class MergeLimitOverProjectWithSort method apply.

@Override
public Result apply(LimitNode parent, Captures captures, Context context) {
    ProjectNode project = captures.get(PROJECT);
    SortNode sort = captures.get(SORT);
    return Result.ofPlanNode(project.replaceChildren(ImmutableList.of(new TopNNode(parent.getId(), sort.getSource(), parent.getCount(), sort.getOrderingScheme(), parent.isPartial() ? PARTIAL : SINGLE))));
}
Also used : SortNode(io.prestosql.sql.planner.plan.SortNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) TopNNode(io.prestosql.spi.plan.TopNNode)

Aggregations

TopNNode (io.prestosql.spi.plan.TopNNode)6 PlanNode (io.prestosql.spi.plan.PlanNode)3 ProjectNode (io.prestosql.spi.plan.ProjectNode)2 Test (org.testng.annotations.Test)2 Iterables (com.google.common.collect.Iterables)1 Session (io.prestosql.Session)1 Pattern (io.prestosql.matching.Pattern)1 SortOrder (io.prestosql.spi.block.SortOrder)1 AggregationNode (io.prestosql.spi.plan.AggregationNode)1 FilterNode (io.prestosql.spi.plan.FilterNode)1 JoinNode (io.prestosql.spi.plan.JoinNode)1 OrderingScheme (io.prestosql.spi.plan.OrderingScheme)1 Symbol (io.prestosql.spi.plan.Symbol)1 TableScanNode (io.prestosql.spi.plan.TableScanNode)1 CallExpression (io.prestosql.spi.relation.CallExpression)1 RowExpression (io.prestosql.spi.relation.RowExpression)1 LogicalPlanner (io.prestosql.sql.planner.LogicalPlanner)1 Plan (io.prestosql.sql.planner.Plan)1 TypeProvider (io.prestosql.sql.planner.TypeProvider)1 BasePlanTest (io.prestosql.sql.planner.assertions.BasePlanTest)1