Search in sources :

Example 36 with AggregationNode

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

the class SimplifyCountOverConstant method apply.

@Override
public Result apply(AggregationNode parent, Captures captures, Context context) {
    ProjectNode child = captures.get(CHILD);
    boolean changed = false;
    Map<VariableReferenceExpression, AggregationNode.Aggregation> aggregations = new LinkedHashMap<>(parent.getAggregations());
    for (Entry<VariableReferenceExpression, AggregationNode.Aggregation> entry : parent.getAggregations().entrySet()) {
        VariableReferenceExpression variable = entry.getKey();
        AggregationNode.Aggregation aggregation = entry.getValue();
        if (isCountOverConstant(aggregation, child.getAssignments())) {
            changed = true;
            aggregations.put(variable, new AggregationNode.Aggregation(new CallExpression(aggregation.getCall().getSourceLocation(), "count", functionResolution.countFunction(), BIGINT, ImmutableList.of()), Optional.empty(), Optional.empty(), false, aggregation.getMask()));
        }
    }
    if (!changed) {
        return Result.empty();
    }
    return Result.ofPlanNode(new AggregationNode(parent.getSourceLocation(), parent.getId(), child, aggregations, parent.getGroupingSets(), ImmutableList.of(), parent.getStep(), parent.getHashVariable(), parent.getGroupIdVariable()));
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) CallExpression(com.facebook.presto.spi.relation.CallExpression) LinkedHashMap(java.util.LinkedHashMap)

Example 37 with AggregationNode

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

the class DruidPushdownUtils method handlePushDownSingleDistinctCount.

/**
 * Try to push down query like: `SELECT count(distinct $COLUMN) FROM myTable` to Druid 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).
 *
 * @return true if push down successfully otherwise false.
 */
private static boolean handlePushDownSingleDistinctCount(ImmutableList.Builder<DruidAggregationColumnNode> 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(), DRUID_COUNT_DISTINCT_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) AggregationFunctionColumnNode(com.facebook.presto.druid.DruidAggregationColumnNode.AggregationFunctionColumnNode) CallExpression(com.facebook.presto.spi.relation.CallExpression)

Example 38 with AggregationNode

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

the class LogicalPlanner method createAnalyzePlan.

private RelationPlan createAnalyzePlan(Analysis analysis, Analyze analyzeStatement) {
    TableHandle targetTable = analysis.getAnalyzeTarget().get();
    // Plan table scan
    Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, targetTable);
    ImmutableList.Builder<VariableReferenceExpression> tableScanOutputsBuilder = ImmutableList.builder();
    ImmutableMap.Builder<VariableReferenceExpression, ColumnHandle> variableToColumnHandle = ImmutableMap.builder();
    ImmutableMap.Builder<String, VariableReferenceExpression> columnNameToVariable = ImmutableMap.builder();
    TableMetadata tableMetadata = metadata.getTableMetadata(session, targetTable);
    for (ColumnMetadata column : tableMetadata.getColumns()) {
        VariableReferenceExpression variable = variableAllocator.newVariable(getSourceLocation(analyzeStatement), column.getName(), column.getType());
        tableScanOutputsBuilder.add(variable);
        variableToColumnHandle.put(variable, columnHandles.get(column.getName()));
        columnNameToVariable.put(column.getName(), variable);
    }
    List<VariableReferenceExpression> tableScanOutputs = tableScanOutputsBuilder.build();
    TableStatisticsMetadata tableStatisticsMetadata = metadata.getStatisticsCollectionMetadata(session, targetTable.getConnectorId().getCatalogName(), tableMetadata.getMetadata());
    TableStatisticAggregation tableStatisticAggregation = statisticsAggregationPlanner.createStatisticsAggregation(tableStatisticsMetadata, columnNameToVariable.build(), true);
    StatisticAggregations statisticAggregations = tableStatisticAggregation.getAggregations();
    PlanNode planNode = new StatisticsWriterNode(getSourceLocation(analyzeStatement), idAllocator.getNextId(), new AggregationNode(getSourceLocation(analyzeStatement), idAllocator.getNextId(), new TableScanNode(getSourceLocation(analyzeStatement), idAllocator.getNextId(), targetTable, tableScanOutputs, variableToColumnHandle.build(), TupleDomain.all(), TupleDomain.all()), statisticAggregations.getAggregations(), singleGroupingSet(statisticAggregations.getGroupingVariables()), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty()), targetTable, variableAllocator.newVariable(getSourceLocation(analyzeStatement), "rows", BIGINT), tableStatisticsMetadata.getTableStatistics().contains(ROW_COUNT), tableStatisticAggregation.getDescriptor());
    return new RelationPlan(planNode, analysis.getScope(analyzeStatement), planNode.getOutputVariables());
}
Also used : ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) TableMetadata(com.facebook.presto.metadata.TableMetadata) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TableStatisticsMetadata(com.facebook.presto.spi.statistics.TableStatisticsMetadata) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) TableStatisticAggregation(com.facebook.presto.sql.planner.StatisticsAggregationPlanner.TableStatisticAggregation) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) StatisticsWriterNode(com.facebook.presto.sql.planner.plan.StatisticsWriterNode) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) StatisticAggregations(com.facebook.presto.sql.planner.plan.StatisticAggregations) PlanNode(com.facebook.presto.spi.plan.PlanNode) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) TableHandle(com.facebook.presto.spi.TableHandle)

Example 39 with AggregationNode

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

the class TestEffectivePredicateExtractor method testGroupByEmpty.

@Test
public void testGroupByEmpty() {
    PlanNode node = new AggregationNode(Optional.empty(), newId(), filter(baseTableScan, FALSE_CONSTANT), ImmutableMap.of(), globalAggregation(), ImmutableList.of(), AggregationNode.Step.FINAL, Optional.empty(), Optional.empty());
    RowExpression effectivePredicate = effectivePredicateExtractor.extract(node);
    assertEquals(effectivePredicate, TRUE_CONSTANT);
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) RowExpression(com.facebook.presto.spi.relation.RowExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) Test(org.testng.annotations.Test)

Example 40 with AggregationNode

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

the class AggregationFunctionMatcher method getAssignedVariable.

@Override
public Optional<VariableReferenceExpression> getAssignedVariable(PlanNode node, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    Optional<VariableReferenceExpression> result = Optional.empty();
    if (!(node instanceof AggregationNode)) {
        return result;
    }
    AggregationNode aggregationNode = (AggregationNode) node;
    FunctionCall expectedCall = callMaker.getExpectedValue(symbolAliases);
    for (Map.Entry<VariableReferenceExpression, Aggregation> assignment : aggregationNode.getAggregations().entrySet()) {
        if (verifyAggregation(metadata.getFunctionAndTypeManager(), assignment.getValue(), expectedCall)) {
            checkState(!result.isPresent(), "Ambiguous function calls in %s", aggregationNode);
            result = Optional.of(assignment.getKey());
        }
    }
    return result;
}
Also used : Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Map(java.util.Map)

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