Search in sources :

Example 1 with StatisticAggregations

use of com.facebook.presto.sql.planner.plan.StatisticAggregations in project presto by prestodb.

the class RowExpressionRewriteRuleSet method translateStatisticAggregation.

private Optional<StatisticAggregations> translateStatisticAggregation(StatisticAggregations statisticAggregations, Rule.Context context) {
    ImmutableMap.Builder<VariableReferenceExpression, AggregationNode.Aggregation> rewrittenAggregation = builder();
    boolean changed = false;
    for (Map.Entry<VariableReferenceExpression, AggregationNode.Aggregation> entry : statisticAggregations.getAggregations().entrySet()) {
        AggregationNode.Aggregation rewritten = rewriteAggregation(entry.getValue(), context);
        rewrittenAggregation.put(entry.getKey(), rewritten);
        if (!rewritten.equals(entry.getValue())) {
            changed = true;
        }
    }
    if (changed) {
        return Optional.of(new StatisticAggregations(rewrittenAggregation.build(), statisticAggregations.getGroupingVariables()));
    }
    return Optional.empty();
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) StatisticAggregations(com.facebook.presto.sql.planner.plan.StatisticAggregations)

Example 2 with StatisticAggregations

use of com.facebook.presto.sql.planner.plan.StatisticAggregations 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 3 with StatisticAggregations

use of com.facebook.presto.sql.planner.plan.StatisticAggregations in project presto by prestodb.

the class StatisticsAggregationPlanner method createStatisticsAggregation.

public TableStatisticAggregation createStatisticsAggregation(TableStatisticsMetadata statisticsMetadata, Map<String, VariableReferenceExpression> columnToVariableMap, boolean useOriginalExpression) {
    StatisticAggregationsDescriptor.Builder<VariableReferenceExpression> descriptor = StatisticAggregationsDescriptor.builder();
    List<String> groupingColumns = statisticsMetadata.getGroupingColumns();
    List<VariableReferenceExpression> groupingVariables = groupingColumns.stream().map(columnToVariableMap::get).collect(toImmutableList());
    for (int i = 0; i < groupingVariables.size(); i++) {
        descriptor.addGrouping(groupingColumns.get(i), groupingVariables.get(i));
    }
    ImmutableMap.Builder<VariableReferenceExpression, AggregationNode.Aggregation> aggregations = ImmutableMap.builder();
    StandardFunctionResolution functionResolution = new FunctionResolution(metadata.getFunctionAndTypeManager());
    for (TableStatisticType type : statisticsMetadata.getTableStatistics()) {
        if (type != ROW_COUNT) {
            throw new PrestoException(NOT_SUPPORTED, "Table-wide statistic type not supported: " + type);
        }
        AggregationNode.Aggregation aggregation = new AggregationNode.Aggregation(new CallExpression("count", functionResolution.countFunction(), BIGINT, ImmutableList.of()), Optional.empty(), Optional.empty(), false, Optional.empty());
        VariableReferenceExpression variable = variableAllocator.newVariable("rowCount", BIGINT);
        aggregations.put(variable, aggregation);
        descriptor.addTableStatistic(ROW_COUNT, variable);
    }
    for (ColumnStatisticMetadata columnStatisticMetadata : statisticsMetadata.getColumnStatistics()) {
        String columnName = columnStatisticMetadata.getColumnName();
        ColumnStatisticType statisticType = columnStatisticMetadata.getStatisticType();
        VariableReferenceExpression inputVariable = columnToVariableMap.get(columnName);
        verify(inputVariable != null, "inputVariable is null");
        ColumnStatisticsAggregation aggregation = createColumnAggregation(statisticType, inputVariable, useOriginalExpression);
        VariableReferenceExpression variable = variableAllocator.newVariable(statisticType + ":" + columnName, aggregation.getOutputType());
        aggregations.put(variable, aggregation.getAggregation());
        descriptor.addColumnStatistic(columnStatisticMetadata, variable);
    }
    StatisticAggregations aggregation = new StatisticAggregations(aggregations.build(), groupingVariables);
    return new TableStatisticAggregation(aggregation, descriptor.build());
}
Also used : ColumnStatisticMetadata(com.facebook.presto.spi.statistics.ColumnStatisticMetadata) PrestoException(com.facebook.presto.spi.PrestoException) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) StandardFunctionResolution(com.facebook.presto.spi.function.StandardFunctionResolution) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) ImmutableMap(com.google.common.collect.ImmutableMap) StatisticAggregations(com.facebook.presto.sql.planner.plan.StatisticAggregations) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ColumnStatisticType(com.facebook.presto.spi.statistics.ColumnStatisticType) StatisticAggregationsDescriptor(com.facebook.presto.sql.planner.plan.StatisticAggregationsDescriptor) StandardFunctionResolution(com.facebook.presto.spi.function.StandardFunctionResolution) TableStatisticType(com.facebook.presto.spi.statistics.TableStatisticType) CallExpression(com.facebook.presto.spi.relation.CallExpression)

Aggregations

AggregationNode (com.facebook.presto.spi.plan.AggregationNode)3 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)3 StatisticAggregations (com.facebook.presto.sql.planner.plan.StatisticAggregations)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 TableMetadata (com.facebook.presto.metadata.TableMetadata)1 ColumnHandle (com.facebook.presto.spi.ColumnHandle)1 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)1 ConnectorTableMetadata (com.facebook.presto.spi.ConnectorTableMetadata)1 PrestoException (com.facebook.presto.spi.PrestoException)1 TableHandle (com.facebook.presto.spi.TableHandle)1 StandardFunctionResolution (com.facebook.presto.spi.function.StandardFunctionResolution)1 PlanNode (com.facebook.presto.spi.plan.PlanNode)1 TableScanNode (com.facebook.presto.spi.plan.TableScanNode)1 CallExpression (com.facebook.presto.spi.relation.CallExpression)1 ColumnStatisticMetadata (com.facebook.presto.spi.statistics.ColumnStatisticMetadata)1 ColumnStatisticType (com.facebook.presto.spi.statistics.ColumnStatisticType)1 TableStatisticType (com.facebook.presto.spi.statistics.TableStatisticType)1 TableStatisticsMetadata (com.facebook.presto.spi.statistics.TableStatisticsMetadata)1 TableStatisticAggregation (com.facebook.presto.sql.planner.StatisticsAggregationPlanner.TableStatisticAggregation)1 StatisticAggregationsDescriptor (com.facebook.presto.sql.planner.plan.StatisticAggregationsDescriptor)1