Search in sources :

Example 46 with AggregationNode

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

the class TestUnion method assertAtMostOneAggregationBetweenRemoteExchanges.

private static void assertAtMostOneAggregationBetweenRemoteExchanges(Plan plan) {
    List<PlanNode> fragments = searchFrom(plan.getRoot()).where(TestUnion::isRemoteExchange).findAll().stream().flatMap(exchangeNode -> exchangeNode.getSources().stream()).collect(toList());
    for (PlanNode fragment : fragments) {
        List<PlanNode> aggregations = searchFrom(fragment).where(AggregationNode.class::isInstance).recurseOnlyWhen(TestUnion::isNotRemoteExchange).findAll();
        assertFalse(aggregations.size() > 1, "More than a single AggregationNode between remote exchanges");
    }
}
Also used : JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) Iterables(com.google.common.collect.Iterables) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) GATHER(com.facebook.presto.sql.planner.plan.ExchangeNode.Type.GATHER) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) PlanNode(com.facebook.presto.spi.plan.PlanNode) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) REPARTITION(com.facebook.presto.sql.planner.plan.ExchangeNode.Type.REPARTITION) TopNNode(com.facebook.presto.spi.plan.TopNNode) Map(java.util.Map) Assert.assertTrue(org.testng.Assert.assertTrue) Plan(com.facebook.presto.sql.planner.Plan) Assert.assertFalse(org.testng.Assert.assertFalse) LogicalPlanner(com.facebook.presto.sql.planner.LogicalPlanner) PlanNodeSearcher.searchFrom(com.facebook.presto.sql.planner.optimizations.PlanNodeSearcher.searchFrom) ExchangeNode(com.facebook.presto.sql.planner.plan.ExchangeNode) PlanNode(com.facebook.presto.spi.plan.PlanNode) AggregationNode(com.facebook.presto.spi.plan.AggregationNode)

Example 47 with AggregationNode

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

the class PinotPushdownUtils method handlePushDownSingleDistinctCount.

/**
 * Try to push down query like: `SELECT count(distinct $COLUMN) FROM myTable` to Pinot as `SELECT distinctCount($COLUMN) FROM myTable`.
 * This function only handles the case of an AggregationNode (COUNT on $COLUMN) on top of an AggregationNode(of non-aggregate on $COLUMN).
 *
 * @param nodeBuilder
 * @param aggregationNode
 * @param outputColumn
 * @param aggregation
 * @return true if push down successfully otherwise false.
 */
private static boolean handlePushDownSingleDistinctCount(ImmutableList.Builder<AggregationColumnNode> nodeBuilder, AggregationNode aggregationNode, VariableReferenceExpression outputColumn, AggregationNode.Aggregation aggregation) {
    if (!aggregation.getCall().getDisplayName().equalsIgnoreCase(COUNT_FUNCTION_NAME)) {
        return false;
    }
    List<RowExpression> arguments = aggregation.getCall().getArguments();
    if (arguments.size() != 1) {
        return false;
    }
    RowExpression aggregationArgument = arguments.get(0);
    // Handle the case of Count Aggregation on top of a Non-Agg GroupBy Aggregation.
    if (!(aggregationNode.getSource() instanceof AggregationNode)) {
        return false;
    }
    AggregationNode sourceAggregationNode = (AggregationNode) aggregationNode.getSource();
    Set<String> sourceAggregationGroupSet = getGroupKeys(sourceAggregationNode.getGroupingKeys());
    Set<String> aggregationGroupSet = getGroupKeys(aggregationNode.getGroupingKeys());
    aggregationGroupSet.add(aggregationArgument.toString());
    if (!sourceAggregationGroupSet.containsAll(aggregationGroupSet) && aggregationGroupSet.containsAll(sourceAggregationGroupSet)) {
        return false;
    }
    nodeBuilder.add(new AggregationFunctionColumnNode(outputColumn, new CallExpression(aggregation.getCall().getSourceLocation(), PINOT_DISTINCT_COUNT_FUNCTION_NAME, aggregation.getFunctionHandle(), aggregation.getCall().getType(), ImmutableList.of(aggregationArgument))));
    return true;
}
Also used : RowExpression(com.facebook.presto.spi.relation.RowExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) CallExpression(com.facebook.presto.spi.relation.CallExpression)

Example 48 with AggregationNode

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

the class TestPinotSplitManager method testBrokerTopNLarge.

private void testBrokerTopNLarge(PinotTableHandle table, int sessionTopNLarge, int configTopNLarge, boolean useSql) {
    String topNLimitKeyword = useSql ? "LIMIT " : "TOP ";
    PinotConfig pinotConfig = new PinotConfig().setUsePinotSqlForBrokerQueries(useSql).setTopNLarge(configTopNLarge);
    SessionHolder sessionHolder = new SessionHolder(pinotConfig);
    ConnectorSession session = createSessionWithTopNLarge(sessionTopNLarge, pinotConfig);
    PlanBuilder planBuilder = createPlanBuilder(sessionHolder);
    PlanNode tableScanNode = tableScan(planBuilder, table, regionId, city, fare, secondsSinceEpoch);
    AggregationNode aggregationNode = planBuilder.aggregation(aggregationNodeBuilder -> aggregationNodeBuilder.source(tableScanNode).singleGroupingSet(variable("city"), variable("regionid")).addAggregation(planBuilder.variable("sum_fare"), getRowExpression("sum(fare)", sessionHolder)).addAggregation(planBuilder.variable("count_regionid"), getRowExpression("count(regionid)", sessionHolder)));
    PinotQueryGenerator.PinotQueryGeneratorResult pinotQueryGeneratorResult = new PinotQueryGenerator(pinotConfig, functionAndTypeManager, functionAndTypeManager, standardFunctionResolution).generate(aggregationNode, session).get();
    String[] limits = pinotQueryGeneratorResult.getGeneratedPinotQuery().getQuery().split(topNLimitKeyword);
    assertEquals(Integer.parseInt(limits[1]), sessionTopNLarge);
    aggregationNode = planBuilder.aggregation(aggregationNodeBuilder -> aggregationNodeBuilder.source(tableScanNode).singleGroupingSet(variable("city"), variable("regionid")).addAggregation(planBuilder.variable("sum_fare"), getRowExpression("sum(fare)", sessionHolder)).addAggregation(planBuilder.variable("count_regionid"), getRowExpression("count(regionid)", sessionHolder)));
    pinotQueryGeneratorResult = new PinotQueryGenerator(pinotConfig, functionAndTypeManager, functionAndTypeManager, standardFunctionResolution).generate(aggregationNode, sessionHolder.getConnectorSession()).get();
    limits = pinotQueryGeneratorResult.getGeneratedPinotQuery().getQuery().split(topNLimitKeyword);
    assertEquals(Integer.parseInt(limits[1]), configTopNLarge);
}
Also used : AggregationNode(com.facebook.presto.spi.plan.AggregationNode) ConnectorSplitSource(com.facebook.presto.spi.ConnectorSplitSource) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) PinotQueryGenerator(com.facebook.presto.pinot.query.PinotQueryGenerator) MoreFutures.getFutureValue(com.facebook.airlift.concurrent.MoreFutures.getFutureValue) ArrayList(java.util.ArrayList) NOT_PARTITIONED(com.facebook.presto.spi.connector.NotPartitionedPartitionHandle.NOT_PARTITIONED) ImmutableList(com.google.common.collect.ImmutableList) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) Splitter(com.google.common.base.Splitter) ENGLISH(java.util.Locale.ENGLISH) Assert.assertFalse(org.testng.Assert.assertFalse) SEGMENT(com.facebook.presto.pinot.PinotSplit.SplitType.SEGMENT) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) UTC_KEY(com.facebook.presto.common.type.TimeZoneKey.UTC_KEY) Executors(java.util.concurrent.Executors) ConnectorSession(com.facebook.presto.spi.ConnectorSession) FeaturesConfig(com.facebook.presto.sql.analyzer.FeaturesConfig) PlanNode(com.facebook.presto.spi.plan.PlanNode) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Optional(java.util.Optional) Assert.assertTrue(org.testng.Assert.assertTrue) BROKER(com.facebook.presto.pinot.PinotSplit.SplitType.BROKER) PlanNode(com.facebook.presto.spi.plan.PlanNode) PinotQueryGenerator(com.facebook.presto.pinot.query.PinotQueryGenerator) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) ConnectorSession(com.facebook.presto.spi.ConnectorSession) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)

Example 49 with AggregationNode

use of com.facebook.presto.spi.plan.AggregationNode 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 50 with AggregationNode

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

the class TestPinotQueryGeneratorSql method testAggregationWithGroupBy.

@Test
public void testAggregationWithGroupBy() {
    PlanBuilder planBuilder = createPlanBuilder(defaultSessionHolder);
    TableScanNode tableScanNode = tableScan(planBuilder, pinotTable, regionId, city, fare);
    AggregationNode aggregationNode = planBuilder.aggregation(aggregationNodeBuilder -> aggregationNodeBuilder.source(tableScanNode).singleGroupingSet(variable("city"), variable("regionid")).addAggregation(planBuilder.variable("sum_fare"), getRowExpression("sum(fare)", defaultSessionHolder)).addAggregation(planBuilder.variable("count_regionid"), getRowExpression("count(regionid)", defaultSessionHolder)));
    testPinotQuery(pinotConfig, aggregationNode, ImmutableList.of("SELECT city, regionId, sum(fare), count(regionId) FROM realtimeOnly GROUP BY city, regionId LIMIT 10000", "SELECT city, regionId, count(regionId), sum(fare) FROM realtimeOnly GROUP BY city, regionId LIMIT 10000"), defaultSessionHolder, ImmutableMap.of());
    ProjectNode project = planBuilder.project(Assignments.builder().put(variable("count_regionid"), variable("count_regionid")).put(variable("city"), variable("city")).put(variable("regionid"), variable("regionid")).put(variable("sum_fare"), variable("sum_fare")).build(), aggregationNode);
    testPinotQuery(pinotConfig, project, "SELECT city, regionId, count(regionId), sum(fare) FROM realtimeOnly GROUP BY city, regionId LIMIT 10000", defaultSessionHolder, ImmutableMap.of());
    project = planBuilder.project(Assignments.builder().put(variable("count_regionid"), variable("count_regionid")).put(variable("regionid"), variable("regionid")).put(variable("sum_fare"), variable("sum_fare")).build(), aggregationNode);
    testPinotQuery(pinotConfig, project, "SELECT city, regionId, count(regionId), sum(fare) FROM realtimeOnly GROUP BY city, regionId LIMIT 10000", defaultSessionHolder, ImmutableMap.of());
    project = planBuilder.project(Assignments.builder().put(variable("count_regionid"), variable("count_regionid")).put(variable("city"), variable("city")).put(variable("sum_fare"), variable("sum_fare")).build(), aggregationNode);
    testPinotQuery(pinotConfig, project, "SELECT city, regionId, count(regionId), sum(fare) FROM realtimeOnly GROUP BY city, regionId LIMIT 10000", defaultSessionHolder, ImmutableMap.of());
    project = planBuilder.project(Assignments.builder().put(variable("sum_fare"), variable("sum_fare")).put(variable("count_regionid"), variable("count_regionid")).build(), aggregationNode);
    testPinotQuery(pinotConfig, project, "SELECT city, regionId, sum(fare), count(regionId) FROM realtimeOnly GROUP BY city, regionId LIMIT 10000", defaultSessionHolder, ImmutableMap.of());
}
Also used : TableScanNode(com.facebook.presto.spi.plan.TableScanNode) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) Test(org.testng.annotations.Test)

Aggregations

AggregationNode (com.facebook.presto.spi.plan.AggregationNode)51 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)33 PlanNode (com.facebook.presto.spi.plan.PlanNode)23 CallExpression (com.facebook.presto.spi.relation.CallExpression)18 Test (org.testng.annotations.Test)18 Aggregation (com.facebook.presto.spi.plan.AggregationNode.Aggregation)15 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)14 ImmutableList (com.google.common.collect.ImmutableList)14 Map (java.util.Map)14 ImmutableMap (com.google.common.collect.ImmutableMap)13 RowExpression (com.facebook.presto.spi.relation.RowExpression)12 TableScanNode (com.facebook.presto.spi.plan.TableScanNode)9 Optional (java.util.Optional)9 Assignments (com.facebook.presto.spi.plan.Assignments)8 Ordering (com.facebook.presto.spi.plan.Ordering)7 OrderingScheme (com.facebook.presto.spi.plan.OrderingScheme)7 PlanBuilder (com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)7 OriginalExpressionUtils.castToRowExpression (com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression)6 List (java.util.List)6 Assert.assertFalse (org.testng.Assert.assertFalse)6