Search in sources :

Example 1 with ColumnStatistics

use of com.facebook.presto.spi.statistics.ColumnStatistics in project presto by prestodb.

the class TestTpchMetadata method testColumnStats.

private void testColumnStats(String schema, TpchTable<?> table, TpchColumn<?> column, Constraint<ColumnHandle> constraint, ColumnStatistics expected) {
    TpchTableHandle tableHandle = tpchMetadata.getTableHandle(session, new SchemaTableName(schema, table.getTableName()));
    List<ColumnHandle> columnHandles = ImmutableList.copyOf(tpchMetadata.getColumnHandles(session, tableHandle).values());
    TableStatistics tableStatistics = tpchMetadata.getTableStatistics(session, tableHandle, Optional.empty(), columnHandles, constraint);
    ColumnHandle columnHandle = tpchMetadata.getColumnHandles(session, tableHandle).get(column.getSimplifiedColumnName());
    ColumnStatistics actual = tableStatistics.getColumnStatistics().get(columnHandle);
    EstimateAssertion estimateAssertion = new EstimateAssertion(TOLERANCE);
    estimateAssertion.assertClose(actual.getDistinctValuesCount(), expected.getDistinctValuesCount(), "distinctValuesCount");
    estimateAssertion.assertClose(actual.getDataSize(), expected.getDataSize(), "dataSize");
    estimateAssertion.assertClose(actual.getNullsFraction(), expected.getNullsFraction(), "nullsFraction");
    estimateAssertion.assertClose(actual.getRange(), expected.getRange(), "range");
}
Also used : ColumnStatistics(com.facebook.presto.spi.statistics.ColumnStatistics) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TableStatistics(com.facebook.presto.spi.statistics.TableStatistics) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 2 with ColumnStatistics

use of com.facebook.presto.spi.statistics.ColumnStatistics in project presto by prestodb.

the class MetastoreHiveStatisticsProvider method createZeroStatistics.

private TableStatistics createZeroStatistics(Map<String, ColumnHandle> columns, Map<String, Type> columnTypes) {
    TableStatistics.Builder result = TableStatistics.builder();
    result.setRowCount(Estimate.of(0));
    result.setTotalSize(Estimate.of(0));
    columns.forEach((columnName, columnHandle) -> {
        Type columnType = columnTypes.get(columnName);
        verify(columnType != null, "columnType is missing for column: %s", columnName);
        ColumnStatistics.Builder columnStatistics = ColumnStatistics.builder();
        columnStatistics.setNullsFraction(Estimate.of(0));
        columnStatistics.setDistinctValuesCount(Estimate.of(0));
        if (hasDataSize(columnType)) {
            columnStatistics.setDataSize(Estimate.of(0));
        }
        result.setColumnStatistics(columnHandle, columnStatistics.build());
    });
    return result.build();
}
Also used : ColumnStatistics(com.facebook.presto.spi.statistics.ColumnStatistics) HiveColumnStatistics(com.facebook.presto.hive.metastore.HiveColumnStatistics) Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) Type(com.facebook.presto.common.type.Type) TableStatistics(com.facebook.presto.spi.statistics.TableStatistics)

Example 3 with ColumnStatistics

use of com.facebook.presto.spi.statistics.ColumnStatistics in project presto by prestodb.

the class MetastoreHiveStatisticsProvider method getTableStatistics.

private static TableStatistics getTableStatistics(Map<String, ColumnHandle> columns, Map<String, Type> columnTypes, List<HivePartition> partitions, Map<String, PartitionStatistics> statistics) {
    if (statistics.isEmpty()) {
        return TableStatistics.empty();
    }
    checkArgument(!partitions.isEmpty(), "partitions is empty");
    OptionalDouble optionalAverageRowsPerPartition = calculateAverageRowsPerPartition(statistics.values());
    if (!optionalAverageRowsPerPartition.isPresent()) {
        return TableStatistics.empty();
    }
    double averageRowsPerPartition = optionalAverageRowsPerPartition.getAsDouble();
    verify(averageRowsPerPartition >= 0, "averageRowsPerPartition must be greater than or equal to zero");
    int queriedPartitionsCount = partitions.size();
    double rowCount = averageRowsPerPartition * queriedPartitionsCount;
    TableStatistics.Builder result = TableStatistics.builder();
    result.setRowCount(Estimate.of(rowCount));
    OptionalDouble optionalAverageSizePerPartition = calculateAverageSizePerPartition(statistics.values());
    if (optionalAverageSizePerPartition.isPresent()) {
        double averageSizePerPartition = optionalAverageSizePerPartition.getAsDouble();
        verify(averageSizePerPartition >= 0, "averageSizePerPartition must be greater than or equal to zero: %s", averageSizePerPartition);
        double totalSize = averageSizePerPartition * queriedPartitionsCount;
        result.setTotalSize(Estimate.of(totalSize));
    }
    for (Map.Entry<String, ColumnHandle> column : columns.entrySet()) {
        String columnName = column.getKey();
        HiveColumnHandle columnHandle = (HiveColumnHandle) column.getValue();
        Type columnType = columnTypes.get(columnName);
        ColumnStatistics columnStatistics;
        if (columnHandle.isPartitionKey()) {
            columnStatistics = createPartitionColumnStatistics(columnHandle, columnType, partitions, statistics, averageRowsPerPartition, rowCount);
        } else {
            columnStatistics = createDataColumnStatistics(columnName, columnType, rowCount, statistics.values());
        }
        result.setColumnStatistics(columnHandle, columnStatistics);
    }
    return result.build();
}
Also used : ColumnStatistics(com.facebook.presto.spi.statistics.ColumnStatistics) HiveColumnStatistics(com.facebook.presto.hive.metastore.HiveColumnStatistics) HiveColumnHandle(com.facebook.presto.hive.HiveColumnHandle) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) Type(com.facebook.presto.common.type.Type) TableStatistics(com.facebook.presto.spi.statistics.TableStatistics) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) OptionalDouble(java.util.OptionalDouble) HiveColumnHandle(com.facebook.presto.hive.HiveColumnHandle)

Example 4 with ColumnStatistics

use of com.facebook.presto.spi.statistics.ColumnStatistics in project presto by prestodb.

the class TableScanStatsRule method doCalculate.

@Override
protected Optional<PlanNodeStatsEstimate> doCalculate(TableScanNode node, StatsProvider sourceStats, Lookup lookup, Session session, TypeProvider types) {
    // TODO Construct predicate like AddExchanges's LayoutConstraintEvaluator
    Constraint<ColumnHandle> constraint = new Constraint<>(node.getCurrentConstraint());
    TableStatistics tableStatistics = metadata.getTableStatistics(session, node.getTable(), ImmutableList.copyOf(node.getAssignments().values()), constraint);
    Map<VariableReferenceExpression, VariableStatsEstimate> outputVariableStats = new HashMap<>();
    for (Map.Entry<VariableReferenceExpression, ColumnHandle> entry : node.getAssignments().entrySet()) {
        Optional<ColumnStatistics> columnStatistics = Optional.ofNullable(tableStatistics.getColumnStatistics().get(entry.getValue()));
        outputVariableStats.put(entry.getKey(), columnStatistics.map(statistics -> StatsUtil.toVariableStatsEstimate(tableStatistics, statistics)).orElse(VariableStatsEstimate.unknown()));
    }
    return Optional.of(PlanNodeStatsEstimate.builder().setOutputRowCount(tableStatistics.getRowCount().getValue()).setTotalSize(tableStatistics.getTotalSize().getValue()).setConfident(true).addVariableStatistics(outputVariableStats).build());
}
Also used : ColumnStatistics(com.facebook.presto.spi.statistics.ColumnStatistics) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Constraint(com.facebook.presto.spi.Constraint) HashMap(java.util.HashMap) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) TableStatistics(com.facebook.presto.spi.statistics.TableStatistics) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with ColumnStatistics

use of com.facebook.presto.spi.statistics.ColumnStatistics in project presto by prestodb.

the class ConnectorFilterStatsCalculatorService method toPlanNodeStats.

private static PlanNodeStatsEstimate toPlanNodeStats(TableStatistics tableStatistics, Map<ColumnHandle, String> columnNames, Map<String, Type> columnTypes) {
    PlanNodeStatsEstimate.Builder builder = PlanNodeStatsEstimate.builder().setOutputRowCount(tableStatistics.getRowCount().getValue());
    for (Map.Entry<ColumnHandle, ColumnStatistics> entry : tableStatistics.getColumnStatistics().entrySet()) {
        String columnName = columnNames.get(entry.getKey());
        VariableReferenceExpression variable = new VariableReferenceExpression(Optional.empty(), columnName, columnTypes.get(columnName));
        builder.addVariableStatistics(variable, toVariableStatsEstimate(tableStatistics, entry.getValue()));
    }
    return builder.build();
}
Also used : ColumnStatistics(com.facebook.presto.spi.statistics.ColumnStatistics) ColumnHandle(com.facebook.presto.spi.ColumnHandle) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Map(java.util.Map)

Aggregations

ColumnStatistics (com.facebook.presto.spi.statistics.ColumnStatistics)10 TableStatistics (com.facebook.presto.spi.statistics.TableStatistics)9 ColumnHandle (com.facebook.presto.spi.ColumnHandle)8 Map (java.util.Map)6 Chars.isCharType (com.facebook.presto.common.type.Chars.isCharType)5 Type (com.facebook.presto.common.type.Type)5 Varchars.isVarcharType (com.facebook.presto.common.type.Varchars.isVarcharType)5 HiveColumnStatistics (com.facebook.presto.hive.metastore.HiveColumnStatistics)5 SchemaTableName (com.facebook.presto.spi.SchemaTableName)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 OptionalDouble (java.util.OptionalDouble)4 Logger (com.facebook.airlift.log.Logger)3 NullableValue (com.facebook.presto.common.predicate.NullableValue)3 BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)3 DATE (com.facebook.presto.common.type.DateType.DATE)3 DOUBLE (com.facebook.presto.common.type.DoubleType.DOUBLE)3 INTEGER (com.facebook.presto.common.type.IntegerType.INTEGER)3 REAL (com.facebook.presto.common.type.RealType.REAL)3 SMALLINT (com.facebook.presto.common.type.SmallintType.SMALLINT)3 TINYINT (com.facebook.presto.common.type.TinyintType.TINYINT)3