Search in sources :

Example 1 with SymbolMapper

use of io.prestosql.sql.planner.optimizations.SymbolMapper in project hetu-core by openlookeng.

the class PushLimitThroughProject method apply.

@Override
public Result apply(LimitNode parent, Captures captures, Context context) {
    ProjectNode projectNode = captures.get(CHILD);
    // for a LimitNode without ties, simply reorder the nodes
    if (!parent.isWithTies()) {
        return Result.ofPlanNode(transpose(parent, projectNode));
    }
    // for a LimitNode with ties, the tiesResolvingScheme must be rewritten in terms of symbols before projection
    SymbolMapper.Builder symbolMapper = SymbolMapper.builder();
    for (Symbol symbol : parent.getTiesResolvingScheme().get().getOrderBy()) {
        Expression expression = castToExpression(projectNode.getAssignments().get(symbol));
        // if a symbol results from some computation, the translation fails
        if (!(expression instanceof SymbolReference)) {
            return Result.empty();
        }
        symbolMapper.put(symbol, SymbolUtils.from(expression));
    }
    LimitNode mappedLimitNode = symbolMapper.build().map(parent, projectNode.getSource());
    return Result.ofPlanNode(projectNode.replaceChildren(ImmutableList.of(mappedLimitNode)));
}
Also used : SymbolMapper(io.prestosql.sql.planner.optimizations.SymbolMapper) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) Expression(io.prestosql.sql.tree.Expression) LimitNode(io.prestosql.spi.plan.LimitNode) Symbol(io.prestosql.spi.plan.Symbol) SymbolReference(io.prestosql.sql.tree.SymbolReference) ProjectNode(io.prestosql.spi.plan.ProjectNode)

Example 2 with SymbolMapper

use of io.prestosql.sql.planner.optimizations.SymbolMapper in project hetu-core by openlookeng.

the class PushTopNThroughProject method apply.

@Override
public Result apply(TopNNode parent, Captures captures, Context context) {
    ProjectNode projectNode = captures.get(PROJECT_CHILD);
    // do not push topN between projection and filter(table scan) so that they can be merged into a PageProcessor
    PlanNode projectSource = context.getLookup().resolve(projectNode.getSource());
    if (projectSource instanceof FilterNode) {
        PlanNode filterSource = context.getLookup().resolve(((FilterNode) projectSource).getSource());
        if (filterSource instanceof TableScanNode) {
            return Result.empty();
        }
    }
    Optional<SymbolMapper> symbolMapper = symbolMapper(parent.getOrderingScheme().getOrderBy(), projectNode.getAssignments());
    if (!symbolMapper.isPresent()) {
        return Result.empty();
    }
    TopNNode mappedTopN = symbolMapper.get().map(parent, projectNode.getSource(), context.getIdAllocator().getNextId());
    return Result.ofPlanNode(projectNode.replaceChildren(ImmutableList.of(mappedTopN)));
}
Also used : PlanNode(io.prestosql.spi.plan.PlanNode) TableScanNode(io.prestosql.spi.plan.TableScanNode) SymbolMapper(io.prestosql.sql.planner.optimizations.SymbolMapper) FilterNode(io.prestosql.spi.plan.FilterNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) TopNNode(io.prestosql.spi.plan.TopNNode)

Example 3 with SymbolMapper

use of io.prestosql.sql.planner.optimizations.SymbolMapper in project hetu-core by openlookeng.

the class PushPartialAggregationThroughExchange method pushPartial.

private PlanNode pushPartial(AggregationNode aggregation, ExchangeNode exchange, Context context) {
    List<PlanNode> partials = new ArrayList<>();
    for (int i = 0; i < exchange.getSources().size(); i++) {
        PlanNode source = exchange.getSources().get(i);
        SymbolMapper.Builder mappingsBuilder = SymbolMapper.builder();
        for (int outputIndex = 0; outputIndex < exchange.getOutputSymbols().size(); outputIndex++) {
            Symbol output = exchange.getOutputSymbols().get(outputIndex);
            Symbol input = exchange.getInputs().get(i).get(outputIndex);
            if (!output.equals(input)) {
                mappingsBuilder.put(output, input);
            }
        }
        SymbolMapper symbolMapper = mappingsBuilder.build();
        if (symbolMapper.getTypes() == null) {
            symbolMapper.setTypes(context.getSymbolAllocator().getTypes());
        }
        AggregationNode mappedPartial = symbolMapper.map(aggregation, source, context.getIdAllocator());
        Assignments.Builder assignments = Assignments.builder();
        for (Symbol output : aggregation.getOutputSymbols()) {
            Symbol input = symbolMapper.map(output);
            assignments.put(output, VariableReferenceSymbolConverter.toVariableReference(input, context.getSymbolAllocator().getTypes()));
        }
        partials.add(new ProjectNode(context.getIdAllocator().getNextId(), mappedPartial, assignments.build()));
    }
    for (PlanNode node : partials) {
        verify(aggregation.getOutputSymbols().equals(node.getOutputSymbols()));
    }
    // Since this exchange source is now guaranteed to have the same symbols as the inputs to the the partial
    // aggregation, we don't need to rewrite symbols in the partitioning function
    PartitioningScheme partitioning = new PartitioningScheme(exchange.getPartitioningScheme().getPartitioning(), aggregation.getOutputSymbols(), exchange.getPartitioningScheme().getHashColumn(), exchange.getPartitioningScheme().isReplicateNullsAndAny(), exchange.getPartitioningScheme().getBucketToPartition());
    return new ExchangeNode(context.getIdAllocator().getNextId(), exchange.getType(), exchange.getScope(), partitioning, partials, ImmutableList.copyOf(Collections.nCopies(partials.size(), aggregation.getOutputSymbols())), Optional.empty(), aggregation.getAggregationType());
}
Also used : SymbolMapper(io.prestosql.sql.planner.optimizations.SymbolMapper) ExchangeNode(io.prestosql.sql.planner.plan.ExchangeNode) Symbol(io.prestosql.spi.plan.Symbol) PartitioningScheme(io.prestosql.sql.planner.PartitioningScheme) ArrayList(java.util.ArrayList) Assignments(io.prestosql.spi.plan.Assignments) AggregationNode(io.prestosql.spi.plan.AggregationNode) PlanNode(io.prestosql.spi.plan.PlanNode) ProjectNode(io.prestosql.spi.plan.ProjectNode)

Example 4 with SymbolMapper

use of io.prestosql.sql.planner.optimizations.SymbolMapper in project hetu-core by openlookeng.

the class PushTableWriteThroughUnion method rewriteSource.

private static TableWriterNode rewriteSource(TableWriterNode writerNode, UnionNode unionNode, int source, List<Map<Symbol, Symbol>> sourceMappings, Context context) {
    Map<Symbol, Symbol> inputMappings = getInputSymbolMapping(unionNode, source);
    ImmutableMap.Builder<Symbol, Symbol> mappings = ImmutableMap.builder();
    mappings.putAll(inputMappings);
    ImmutableMap.Builder<Symbol, Symbol> outputMappings = ImmutableMap.builder();
    for (Symbol outputSymbol : writerNode.getOutputSymbols()) {
        if (inputMappings.containsKey(outputSymbol)) {
            outputMappings.put(outputSymbol, inputMappings.get(outputSymbol));
        } else {
            Symbol newSymbol = context.getSymbolAllocator().newSymbol(outputSymbol);
            outputMappings.put(outputSymbol, newSymbol);
            mappings.put(outputSymbol, newSymbol);
        }
    }
    sourceMappings.add(outputMappings.build());
    ImmutableMap.Builder<String, String> stringMapping = ImmutableMap.builder();
    mappings.build().forEach((symbol1, symbol2) -> stringMapping.put(symbol1.getName(), symbol2.getName()));
    SymbolMapper symbolMapper = new SymbolMapper(stringMapping.build(), context.getSymbolAllocator().getTypes());
    return symbolMapper.map(writerNode, unionNode.getSources().get(source), context.getIdAllocator().getNextId());
}
Also used : SymbolMapper(io.prestosql.sql.planner.optimizations.SymbolMapper) Symbol(io.prestosql.spi.plan.Symbol) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap)

Example 5 with SymbolMapper

use of io.prestosql.sql.planner.optimizations.SymbolMapper in project hetu-core by openlookeng.

the class PushTopNThroughProject method symbolMapper.

private Optional<SymbolMapper> symbolMapper(List<Symbol> symbols, Assignments assignments) {
    SymbolMapper.Builder mapper = SymbolMapper.builder();
    for (Symbol symbol : symbols) {
        if (isExpression(assignments.get(symbol))) {
            Expression expression = castToExpression(assignments.get(symbol));
            if (!(expression instanceof SymbolReference)) {
                return Optional.empty();
            }
            mapper.put(symbol, SymbolUtils.from(expression));
        } else {
            RowExpression expression = assignments.get(symbol);
            if (!(expression instanceof VariableReferenceExpression)) {
                return Optional.empty();
            }
            mapper.put(symbol, new Symbol(((VariableReferenceExpression) expression).getName()));
        }
    }
    return Optional.of(mapper.build());
}
Also used : SymbolMapper(io.prestosql.sql.planner.optimizations.SymbolMapper) OriginalExpressionUtils.isExpression(io.prestosql.sql.relational.OriginalExpressionUtils.isExpression) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Expression(io.prestosql.sql.tree.Expression) Symbol(io.prestosql.spi.plan.Symbol) SymbolReference(io.prestosql.sql.tree.SymbolReference) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) RowExpression(io.prestosql.spi.relation.RowExpression)

Aggregations

SymbolMapper (io.prestosql.sql.planner.optimizations.SymbolMapper)6 Symbol (io.prestosql.spi.plan.Symbol)5 PlanNode (io.prestosql.spi.plan.PlanNode)3 ProjectNode (io.prestosql.spi.plan.ProjectNode)3 OriginalExpressionUtils.castToExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression)2 Expression (io.prestosql.sql.tree.Expression)2 SymbolReference (io.prestosql.sql.tree.SymbolReference)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 AggregationNode (io.prestosql.spi.plan.AggregationNode)1 Assignments (io.prestosql.spi.plan.Assignments)1 FilterNode (io.prestosql.spi.plan.FilterNode)1 LimitNode (io.prestosql.spi.plan.LimitNode)1 TableScanNode (io.prestosql.spi.plan.TableScanNode)1 TopNNode (io.prestosql.spi.plan.TopNNode)1 UnionNode (io.prestosql.spi.plan.UnionNode)1 RowExpression (io.prestosql.spi.relation.RowExpression)1 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)1 PartitioningScheme (io.prestosql.sql.planner.PartitioningScheme)1