Search in sources :

Example 1 with TableStatisticType

use of io.trino.spi.statistics.TableStatisticType in project trino by trinodb.

the class StatisticsAggregationPlanner method createStatisticsAggregation.

public TableStatisticAggregation createStatisticsAggregation(TableStatisticsMetadata statisticsMetadata, Map<String, Symbol> columnToSymbolMap) {
    StatisticAggregationsDescriptor.Builder<Symbol> descriptor = StatisticAggregationsDescriptor.builder();
    List<String> groupingColumns = statisticsMetadata.getGroupingColumns();
    List<Symbol> groupingSymbols = groupingColumns.stream().map(columnToSymbolMap::get).collect(toImmutableList());
    for (int i = 0; i < groupingSymbols.size(); i++) {
        descriptor.addGrouping(groupingColumns.get(i), groupingSymbols.get(i));
    }
    ImmutableMap.Builder<Symbol, AggregationNode.Aggregation> aggregations = ImmutableMap.builder();
    for (TableStatisticType type : statisticsMetadata.getTableStatistics()) {
        if (type != ROW_COUNT) {
            throw new TrinoException(NOT_SUPPORTED, "Table-wide statistic type not supported: " + type);
        }
        AggregationNode.Aggregation aggregation = new AggregationNode.Aggregation(metadata.resolveFunction(session, QualifiedName.of("count"), ImmutableList.of()), ImmutableList.of(), false, Optional.empty(), Optional.empty(), Optional.empty());
        Symbol symbol = symbolAllocator.newSymbol("rowCount", BIGINT);
        aggregations.put(symbol, aggregation);
        descriptor.addTableStatistic(ROW_COUNT, symbol);
    }
    for (ColumnStatisticMetadata columnStatisticMetadata : statisticsMetadata.getColumnStatistics()) {
        String columnName = columnStatisticMetadata.getColumnName();
        ColumnStatisticType statisticType = columnStatisticMetadata.getStatisticType();
        Symbol inputSymbol = columnToSymbolMap.get(columnName);
        verifyNotNull(inputSymbol, "inputSymbol is null");
        Type inputType = symbolAllocator.getTypes().get(inputSymbol);
        verifyNotNull(inputType, "inputType is null for symbol: %s", inputSymbol);
        ColumnStatisticsAggregation aggregation = createColumnAggregation(statisticType, inputSymbol, inputType);
        Symbol symbol = symbolAllocator.newSymbol(statisticType + ":" + columnName, aggregation.getOutputType());
        aggregations.put(symbol, aggregation.getAggregation());
        descriptor.addColumnStatistic(columnStatisticMetadata, symbol);
    }
    StatisticAggregations aggregation = new StatisticAggregations(aggregations.buildOrThrow(), groupingSymbols);
    return new TableStatisticAggregation(aggregation, descriptor.build());
}
Also used : ColumnStatisticMetadata(io.trino.spi.statistics.ColumnStatisticMetadata) AggregationNode(io.trino.sql.planner.plan.AggregationNode) ImmutableMap(com.google.common.collect.ImmutableMap) StatisticAggregations(io.trino.sql.planner.plan.StatisticAggregations) Type(io.trino.spi.type.Type) ColumnStatisticType(io.trino.spi.statistics.ColumnStatisticType) TableStatisticType(io.trino.spi.statistics.TableStatisticType) ColumnStatisticType(io.trino.spi.statistics.ColumnStatisticType) TrinoException(io.trino.spi.TrinoException) StatisticAggregationsDescriptor(io.trino.sql.planner.plan.StatisticAggregationsDescriptor) TableStatisticType(io.trino.spi.statistics.TableStatisticType)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 TrinoException (io.trino.spi.TrinoException)1 ColumnStatisticMetadata (io.trino.spi.statistics.ColumnStatisticMetadata)1 ColumnStatisticType (io.trino.spi.statistics.ColumnStatisticType)1 TableStatisticType (io.trino.spi.statistics.TableStatisticType)1 Type (io.trino.spi.type.Type)1 AggregationNode (io.trino.sql.planner.plan.AggregationNode)1 StatisticAggregations (io.trino.sql.planner.plan.StatisticAggregations)1 StatisticAggregationsDescriptor (io.trino.sql.planner.plan.StatisticAggregationsDescriptor)1