Search in sources :

Example 1 with StatisticAggregations

use of io.prestosql.sql.planner.plan.StatisticAggregations in project hetu-core by openlookeng.

the class TestExternalFunctionPushDownChecker method testTableWriterNodeWithExternalCall.

@Test(expectedExceptions = ExternalFunctionPushDownChecker.IllegalExternalFunctionUsageException.class, expectedExceptionsMessageRegExp = "The external function jdbc.v1.foo does not support to push down to data source for this query.")
public void testTableWriterNodeWithExternalCall() {
    TableWriterNode.DeleteTarget deleteTarget = new TableWriterNode.DeleteTarget(tableHandle, new SchemaTableName("sch", "tab"));
    PlanNode node = new TableWriterNode(idAllocator.getNextId(), builder.values(), deleteTarget, columnA, columnB, ImmutableList.of(columnA, columnB), ImmutableList.of("a", "b"), Optional.empty(), Optional.of(new StatisticAggregations(ImmutableMap.of(columnA, new AggregationNode.Aggregation(externalFooCall1, externalFooCall1.getArguments(), false, Optional.empty(), Optional.empty(), Optional.empty())), ImmutableList.of(columnA))), Optional.of(new StatisticAggregationsDescriptor<>(ImmutableMap.of(), ImmutableMap.of(), ImmutableMap.of())));
    validatePlan(node);
}
Also used : PlanNode(io.prestosql.spi.plan.PlanNode) StatisticAggregationsDescriptor(io.prestosql.sql.planner.plan.StatisticAggregationsDescriptor) TableWriterNode(io.prestosql.sql.planner.plan.TableWriterNode) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) StatisticAggregations(io.prestosql.sql.planner.plan.StatisticAggregations) Test(org.testng.annotations.Test) BasePlanTest(io.prestosql.sql.planner.assertions.BasePlanTest)

Example 2 with StatisticAggregations

use of io.prestosql.sql.planner.plan.StatisticAggregations in project hetu-core by openlookeng.

the class TestExternalFunctionPushDownChecker method testTableWriterNode.

@Test
public void testTableWriterNode() {
    TableWriterNode.DeleteTarget deleteTarget = new TableWriterNode.DeleteTarget(tableHandle, new SchemaTableName("sch", "tab"));
    PlanNode node = new TableWriterNode(idAllocator.getNextId(), builder.values(), deleteTarget, columnA, columnB, ImmutableList.of(columnA, columnB), ImmutableList.of("a", "b"), Optional.empty(), Optional.of(new StatisticAggregations(ImmutableMap.of(columnA, new AggregationNode.Aggregation(sumCall, sumCall.getArguments(), false, Optional.empty(), Optional.empty(), Optional.empty())), ImmutableList.of(columnA))), Optional.of(new StatisticAggregationsDescriptor<>(ImmutableMap.of(), ImmutableMap.of(), ImmutableMap.of())));
    validatePlan(node);
}
Also used : PlanNode(io.prestosql.spi.plan.PlanNode) StatisticAggregationsDescriptor(io.prestosql.sql.planner.plan.StatisticAggregationsDescriptor) TableWriterNode(io.prestosql.sql.planner.plan.TableWriterNode) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) StatisticAggregations(io.prestosql.sql.planner.plan.StatisticAggregations) Test(org.testng.annotations.Test) BasePlanTest(io.prestosql.sql.planner.assertions.BasePlanTest)

Example 3 with StatisticAggregations

use of io.prestosql.sql.planner.plan.StatisticAggregations in project hetu-core by openlookeng.

the class TestExternalFunctionPushDownChecker method testVacuumTableNode.

@Test
public void testVacuumTableNode() {
    TableWriterNode.DeleteTarget deleteTarget = new TableWriterNode.DeleteTarget(tableHandle, new SchemaTableName("sch", "tab"));
    PlanNode node = new VacuumTableNode(idAllocator.getNextId(), tableHandle, deleteTarget, columnA, columnB, "p1", false, ImmutableList.of(), Optional.of(new StatisticAggregations(ImmutableMap.of(columnA, new AggregationNode.Aggregation(sumCall, sumCall.getArguments(), false, Optional.empty(), Optional.empty(), Optional.empty())), ImmutableList.of(columnA))), Optional.of(new StatisticAggregationsDescriptor<>(ImmutableMap.of(), ImmutableMap.of(), ImmutableMap.of())));
    validatePlan(node);
}
Also used : PlanNode(io.prestosql.spi.plan.PlanNode) VacuumTableNode(io.prestosql.sql.planner.plan.VacuumTableNode) StatisticAggregationsDescriptor(io.prestosql.sql.planner.plan.StatisticAggregationsDescriptor) TableWriterNode(io.prestosql.sql.planner.plan.TableWriterNode) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) StatisticAggregations(io.prestosql.sql.planner.plan.StatisticAggregations) Test(org.testng.annotations.Test) BasePlanTest(io.prestosql.sql.planner.assertions.BasePlanTest)

Example 4 with StatisticAggregations

use of io.prestosql.sql.planner.plan.StatisticAggregations in project hetu-core by openlookeng.

the class LogicalPlanner method createTableWriterPlan.

private RelationPlan createTableWriterPlan(Analysis analysis, RelationPlan plan, WriterTarget target, List<String> columnNames, Optional<NewTableLayout> writeTableLayout, TableStatisticsMetadata statisticsMetadata) {
    PlanNode source = plan.getRoot();
    if (!analysis.isCreateTableAsSelectWithData()) {
        source = new LimitNode(idAllocator.getNextId(), source, 0L, false);
    }
    // todo this should be checked in analysis
    writeTableLayout.ifPresent(layout -> {
        if (!ImmutableSet.copyOf(columnNames).containsAll(layout.getPartitionColumns())) {
            throw new PrestoException(NOT_SUPPORTED, "INSERT must write all distribution columns: " + layout.getPartitionColumns());
        }
    });
    List<Symbol> symbols = plan.getFieldMappings();
    Optional<PartitioningScheme> partitioningScheme = Optional.empty();
    if (writeTableLayout.isPresent()) {
        List<Symbol> partitionFunctionArguments = new ArrayList<>();
        writeTableLayout.get().getPartitionColumns().stream().mapToInt(columnNames::indexOf).mapToObj(symbols::get).forEach(partitionFunctionArguments::add);
        List<Symbol> outputLayout = new ArrayList<>(symbols);
        PartitioningHandle partitioningHandle = writeTableLayout.get().getPartitioning().orElse(FIXED_HASH_DISTRIBUTION);
        partitioningScheme = Optional.of(new PartitioningScheme(Partitioning.create(partitioningHandle, partitionFunctionArguments), outputLayout));
    }
    if (!statisticsMetadata.isEmpty()) {
        verify(columnNames.size() == symbols.size(), "columnNames.size() != symbols.size(): %s and %s", columnNames, symbols);
        Map<String, Symbol> columnToSymbolMap = zip(columnNames.stream(), symbols.stream(), SimpleImmutableEntry::new).collect(toImmutableMap(Entry::getKey, Entry::getValue));
        TableStatisticAggregation result = statisticsAggregationPlanner.createStatisticsAggregation(statisticsMetadata, columnToSymbolMap);
        StatisticAggregations.Parts aggregations = result.getAggregations().createPartialAggregations(planSymbolAllocator, metadata);
        // partial aggregation is run within the TableWriteOperator to calculate the statistics for
        // the data consumed by the TableWriteOperator
        // final aggregation is run within the TableFinishOperator to summarize collected statistics
        // by the partial aggregation from all of the writer nodes
        StatisticAggregations partialAggregation = aggregations.getPartialAggregation();
        PlanNode writerNode = new TableWriterNode(idAllocator.getNextId(), source, target, planSymbolAllocator.newSymbol("partialrows", BIGINT), planSymbolAllocator.newSymbol("fragment", VARBINARY), symbols, columnNames, partitioningScheme, Optional.of(partialAggregation), Optional.of(result.getDescriptor().map(aggregations.getMappings()::get)));
        TableFinishNode commitNode = new TableFinishNode(idAllocator.getNextId(), writerNode, target, planSymbolAllocator.newSymbol("rows", BIGINT), Optional.of(aggregations.getFinalAggregation()), Optional.of(result.getDescriptor()));
        return new RelationPlan(commitNode, analysis.getRootScope(), commitNode.getOutputSymbols());
    }
    TableFinishNode commitNode = new TableFinishNode(idAllocator.getNextId(), new TableWriterNode(idAllocator.getNextId(), source, target, planSymbolAllocator.newSymbol("partialrows", BIGINT), planSymbolAllocator.newSymbol("fragment", VARBINARY), symbols, columnNames, partitioningScheme, Optional.empty(), Optional.empty()), target, planSymbolAllocator.newSymbol("rows", BIGINT), Optional.empty(), Optional.empty());
    return new RelationPlan(commitNode, analysis.getRootScope(), commitNode.getOutputSymbols());
}
Also used : TableStatisticAggregation(io.prestosql.sql.planner.StatisticsAggregationPlanner.TableStatisticAggregation) Symbol(io.prestosql.spi.plan.Symbol) ArrayList(java.util.ArrayList) PrestoException(io.prestosql.spi.PrestoException) TableFinishNode(io.prestosql.sql.planner.plan.TableFinishNode) StatisticAggregations(io.prestosql.sql.planner.plan.StatisticAggregations) PlanNode(io.prestosql.spi.plan.PlanNode) LimitNode(io.prestosql.spi.plan.LimitNode) TableWriterNode(io.prestosql.sql.planner.plan.TableWriterNode)

Example 5 with StatisticAggregations

use of io.prestosql.sql.planner.plan.StatisticAggregations in project hetu-core by openlookeng.

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();
    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()), ImmutableList.of(), false, Optional.empty(), Optional.empty(), Optional.empty());
        Symbol symbol = planSymbolAllocator.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);
        verify(inputSymbol != null, "inputSymbol is null");
        Type inputType = planSymbolAllocator.getTypes().get(inputSymbol);
        verify(inputType != null, "inputType is null for symbol: %s", inputSymbol);
        ColumnStatisticsAggregation aggregation = createColumnAggregation(statisticType, inputSymbol, inputType);
        Symbol symbol = planSymbolAllocator.newSymbol(statisticType + ":" + columnName, aggregation.getOutputType());
        aggregations.put(symbol, aggregation.getAggregation());
        descriptor.addColumnStatistic(columnStatisticMetadata, symbol);
    }
    StatisticAggregations aggregation = new StatisticAggregations(aggregations.build(), groupingSymbols);
    return new TableStatisticAggregation(aggregation, descriptor.build());
}
Also used : ColumnStatisticMetadata(io.prestosql.spi.statistics.ColumnStatisticMetadata) Symbol(io.prestosql.spi.plan.Symbol) PrestoException(io.prestosql.spi.PrestoException) AggregationNode(io.prestosql.spi.plan.AggregationNode) StandardFunctionResolution(io.prestosql.spi.function.StandardFunctionResolution) FunctionResolution(io.prestosql.sql.relational.FunctionResolution) ImmutableMap(com.google.common.collect.ImmutableMap) StatisticAggregations(io.prestosql.sql.planner.plan.StatisticAggregations) ColumnStatisticType(io.prestosql.spi.statistics.ColumnStatisticType) TableStatisticType(io.prestosql.spi.statistics.TableStatisticType) Type(io.prestosql.spi.type.Type) ColumnStatisticType(io.prestosql.spi.statistics.ColumnStatisticType) StatisticAggregationsDescriptor(io.prestosql.sql.planner.plan.StatisticAggregationsDescriptor) StandardFunctionResolution(io.prestosql.spi.function.StandardFunctionResolution) TableStatisticType(io.prestosql.spi.statistics.TableStatisticType) CallExpression(io.prestosql.spi.relation.CallExpression)

Aggregations

StatisticAggregations (io.prestosql.sql.planner.plan.StatisticAggregations)11 PlanNode (io.prestosql.spi.plan.PlanNode)8 StatisticAggregationsDescriptor (io.prestosql.sql.planner.plan.StatisticAggregationsDescriptor)8 TableWriterNode (io.prestosql.sql.planner.plan.TableWriterNode)7 SchemaTableName (io.prestosql.spi.connector.SchemaTableName)6 BasePlanTest (io.prestosql.sql.planner.assertions.BasePlanTest)6 Test (org.testng.annotations.Test)6 Symbol (io.prestosql.spi.plan.Symbol)5 TableFinishNode (io.prestosql.sql.planner.plan.TableFinishNode)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 AggregationNode (io.prestosql.spi.plan.AggregationNode)3 TableStatisticAggregation (io.prestosql.sql.planner.StatisticsAggregationPlanner.TableStatisticAggregation)3 VacuumTableNode (io.prestosql.sql.planner.plan.VacuumTableNode)3 PrestoException (io.prestosql.spi.PrestoException)2 Type (io.prestosql.spi.type.Type)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 TableMetadata (io.prestosql.metadata.TableMetadata)1 ColumnHandle (io.prestosql.spi.connector.ColumnHandle)1