Search in sources :

Example 6 with TopNNode

use of com.facebook.presto.spi.plan.TopNNode in project presto by prestodb.

the class SymbolMapper method map.

public TopNNode map(TopNNode node, PlanNode source, PlanNodeId newNodeId) {
    ImmutableList.Builder<VariableReferenceExpression> variables = ImmutableList.builder();
    ImmutableMap.Builder<VariableReferenceExpression, SortOrder> orderings = ImmutableMap.builder();
    Set<VariableReferenceExpression> seenCanonicals = new HashSet<>(node.getOrderingScheme().getOrderByVariables().size());
    for (VariableReferenceExpression variable : node.getOrderingScheme().getOrderByVariables()) {
        VariableReferenceExpression canonical = map(variable);
        if (seenCanonicals.add(canonical)) {
            seenCanonicals.add(canonical);
            variables.add(canonical);
            orderings.put(canonical, node.getOrderingScheme().getOrdering(variable));
        }
    }
    ImmutableMap<VariableReferenceExpression, SortOrder> orderingMap = orderings.build();
    return new TopNNode(node.getSourceLocation(), newNodeId, source, node.getCount(), new OrderingScheme(variables.build().stream().map(variable -> new Ordering(variable, orderingMap.get(variable))).collect(toImmutableList())), node.getStep());
}
Also used : ExpressionTreeRewriter(com.facebook.presto.sql.tree.ExpressionTreeRewriter) WarningCollector(com.facebook.presto.spi.WarningCollector) PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) RowExpressionRewriter(com.facebook.presto.expressions.RowExpressionRewriter) OriginalExpressionUtils.isExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.isExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) HashMap(java.util.HashMap) TableWriterNode(com.facebook.presto.sql.planner.plan.TableWriterNode) StatisticAggregationsDescriptor(com.facebook.presto.sql.planner.plan.StatisticAggregationsDescriptor) TableWriterMergeNode(com.facebook.presto.sql.planner.plan.TableWriterMergeNode) HashSet(java.util.HashSet) OriginalExpressionUtils.castToExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression) ImmutableList(com.google.common.collect.ImmutableList) Symbol(com.facebook.presto.sql.planner.Symbol) TypeProvider(com.facebook.presto.sql.planner.TypeProvider) StatisticAggregations(com.facebook.presto.sql.planner.plan.StatisticAggregations) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) PrestoWarning(com.facebook.presto.spi.PrestoWarning) PartitioningScheme(com.facebook.presto.sql.planner.PartitioningScheme) CallExpression(com.facebook.presto.spi.relation.CallExpression) OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) RowExpression(com.facebook.presto.spi.relation.RowExpression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) RowExpressionTreeRewriter(com.facebook.presto.expressions.RowExpressionTreeRewriter) ImmutableSet(com.google.common.collect.ImmutableSet) PlanNodeIdAllocator(com.facebook.presto.spi.plan.PlanNodeIdAllocator) SortOrder(com.facebook.presto.common.block.SortOrder) ImmutableMap(com.google.common.collect.ImmutableMap) Ordering(com.facebook.presto.spi.plan.Ordering) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) ExpressionTreeUtils.getNodeLocation(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.getNodeLocation) MULTIPLE_ORDER_BY(com.facebook.presto.spi.StandardWarningCode.MULTIPLE_ORDER_BY) ExpressionRewriter(com.facebook.presto.sql.tree.ExpressionRewriter) PlanNode(com.facebook.presto.spi.plan.PlanNode) List(java.util.List) AggregationNode.groupingSets(com.facebook.presto.spi.plan.AggregationNode.groupingSets) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Expression(com.facebook.presto.sql.tree.Expression) TopNNode(com.facebook.presto.spi.plan.TopNNode) Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) Entry(java.util.Map.Entry) TableFinishNode(com.facebook.presto.sql.planner.plan.TableFinishNode) StatisticsWriterNode(com.facebook.presto.sql.planner.plan.StatisticsWriterNode) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) SortOrder(com.facebook.presto.common.block.SortOrder) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Ordering(com.facebook.presto.spi.plan.Ordering) TopNNode(com.facebook.presto.spi.plan.TopNNode) HashSet(java.util.HashSet)

Example 7 with TopNNode

use of com.facebook.presto.spi.plan.TopNNode in project presto by prestodb.

the class TestEffectivePredicateExtractor method testTopN.

@Test
public void testTopN() {
    PlanNode node = new TopNNode(Optional.empty(), newId(), filter(baseTableScan, and(equals(AV, BV), equals(BV, CV), lessThan(CV, bigintLiteral(10)))), 1, new OrderingScheme(ImmutableList.of(new Ordering(AV, SortOrder.ASC_NULLS_FIRST))), TopNNode.Step.PARTIAL);
    RowExpression effectivePredicate = effectivePredicateExtractor.extract(node);
    // Pass through
    assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjuncts(equals(AV, BV), equals(BV, CV), lessThan(CV, bigintLiteral(10))));
}
Also used : OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) PlanNode(com.facebook.presto.spi.plan.PlanNode) Ordering(com.facebook.presto.spi.plan.Ordering) RowExpression(com.facebook.presto.spi.relation.RowExpression) TopNNode(com.facebook.presto.spi.plan.TopNNode) Test(org.testng.annotations.Test)

Example 8 with TopNNode

use of com.facebook.presto.spi.plan.TopNNode in project presto by prestodb.

the class TestPinotQueryGenerator method testAggregationWithOrderByPushDownInTopN.

@Test(expectedExceptions = NoSuchElementException.class)
public void testAggregationWithOrderByPushDownInTopN() {
    PlanBuilder planBuilder = createPlanBuilder(defaultSessionHolder);
    TableScanNode tableScanNode = tableScan(planBuilder, pinotTable, city, fare);
    AggregationNode agg = planBuilder.aggregation(aggBuilder -> aggBuilder.source(tableScanNode).singleGroupingSet(variable("city")).addAggregation(planBuilder.variable("agg"), getRowExpression("sum(fare)", defaultSessionHolder)));
    TopNNode topN = new TopNNode(Optional.empty(), planBuilder.getIdAllocator().getNextId(), agg, 50L, new OrderingScheme(ImmutableList.of(new Ordering(variable("city"), SortOrder.DESC_NULLS_FIRST))), TopNNode.Step.FINAL);
    testPinotQuery(pinotConfig, topN, "", defaultSessionHolder, ImmutableMap.of());
}
Also used : OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) Ordering(com.facebook.presto.spi.plan.Ordering) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) TopNNode(com.facebook.presto.spi.plan.TopNNode) Test(org.testng.annotations.Test)

Example 9 with TopNNode

use of com.facebook.presto.spi.plan.TopNNode in project presto by prestodb.

the class TestPinotQueryGenerator method testSimpleSelectWithTopN.

@Test
public void testSimpleSelectWithTopN() {
    pinotConfig.setPushdownTopNBrokerQueries(true);
    SessionHolder sessionHolder = new SessionHolder(pinotConfig);
    PlanBuilder planBuilder = createPlanBuilder(new SessionHolder(pinotConfig));
    TableScanNode tableScanNode = tableScan(planBuilder, pinotTable, regionId, city, fare);
    TopNNode topNFare = topN(planBuilder, 50L, ImmutableList.of("fare"), ImmutableList.of(false), tableScanNode);
    testPinotQuery(pinotConfig, topNFare, "SELECT regionId, city, fare FROM realtimeOnly ORDER BY fare DESC LIMIT 50", sessionHolder, ImmutableMap.of());
    TopNNode topnFareAndCity = topN(planBuilder, 50L, ImmutableList.of("fare", "city"), ImmutableList.of(true, false), tableScanNode);
    testPinotQuery(pinotConfig, topnFareAndCity, "SELECT regionId, city, fare FROM realtimeOnly ORDER BY fare, city DESC LIMIT 50", sessionHolder, ImmutableMap.of());
    ProjectNode projectNode = project(planBuilder, topnFareAndCity, ImmutableList.of("regionid", "city"));
    testPinotQuery(pinotConfig, projectNode, "SELECT regionId, city FROM realtimeOnly ORDER BY fare, city DESC LIMIT 50", sessionHolder, ImmutableMap.of());
}
Also used : TableScanNode(com.facebook.presto.spi.plan.TableScanNode) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) TopNNode(com.facebook.presto.spi.plan.TopNNode) Test(org.testng.annotations.Test)

Aggregations

TopNNode (com.facebook.presto.spi.plan.TopNNode)9 Test (org.testng.annotations.Test)7 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)5 Ordering (com.facebook.presto.spi.plan.Ordering)5 OrderingScheme (com.facebook.presto.spi.plan.OrderingScheme)5 TableScanNode (com.facebook.presto.spi.plan.TableScanNode)5 PlanBuilder (com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)5 PlanNode (com.facebook.presto.spi.plan.PlanNode)3 RowExpression (com.facebook.presto.spi.relation.RowExpression)2 SortOrder (com.facebook.presto.common.block.SortOrder)1 RowExpressionRewriter (com.facebook.presto.expressions.RowExpressionRewriter)1 RowExpressionTreeRewriter (com.facebook.presto.expressions.RowExpressionTreeRewriter)1 PrestoWarning (com.facebook.presto.spi.PrestoWarning)1 MULTIPLE_ORDER_BY (com.facebook.presto.spi.StandardWarningCode.MULTIPLE_ORDER_BY)1 WarningCollector (com.facebook.presto.spi.WarningCollector)1 Aggregation (com.facebook.presto.spi.plan.AggregationNode.Aggregation)1 AggregationNode.groupingSets (com.facebook.presto.spi.plan.AggregationNode.groupingSets)1 PlanNodeId (com.facebook.presto.spi.plan.PlanNodeId)1 PlanNodeIdAllocator (com.facebook.presto.spi.plan.PlanNodeIdAllocator)1 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)1