Search in sources :

Example 6 with SymbolMapper

use of io.trino.sql.planner.optimizations.SymbolMapper in project trino by trinodb.

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.buildOrThrow());
    SymbolMapper symbolMapper = symbolMapper(mappings.buildOrThrow());
    return symbolMapper.map(writerNode, unionNode.getSources().get(source), context.getIdAllocator().getNextId());
}
Also used : SymbolMapper(io.trino.sql.planner.optimizations.SymbolMapper) Symbol(io.trino.sql.planner.Symbol) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap)

Example 7 with SymbolMapper

use of io.trino.sql.planner.optimizations.SymbolMapper in project trino by trinodb.

the class PushTopNThroughProject method apply.

@Override
public Result apply(TopNNode parent, Captures captures, Context context) {
    ProjectNode projectNode = captures.get(PROJECT_CHILD);
    // Do not push down if the projection is made up of symbol references and exclusive dereferences. This prevents
    // undoing of PushDownDereferencesThroughTopN. We still push topN in the case of overlapping dereferences since
    // it enables PushDownDereferencesThroughTopN rule to push optimal dereferences.
    Set<Expression> projections = ImmutableSet.copyOf(projectNode.getAssignments().getExpressions());
    if (!extractRowSubscripts(projections, false, context.getSession(), typeAnalyzer, context.getSymbolAllocator().getTypes()).isEmpty() && exclusiveDereferences(projections, context.getSession(), typeAnalyzer, context.getSymbolAllocator().getTypes())) {
        return Result.empty();
    }
    // 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.isEmpty()) {
        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.trino.sql.planner.plan.PlanNode) TableScanNode(io.trino.sql.planner.plan.TableScanNode) Expression(io.trino.sql.tree.Expression) SymbolMapper(io.trino.sql.planner.optimizations.SymbolMapper) FilterNode(io.trino.sql.planner.plan.FilterNode) ProjectNode(io.trino.sql.planner.plan.ProjectNode) TopNNode(io.trino.sql.planner.plan.TopNNode)

Aggregations

SymbolMapper (io.trino.sql.planner.optimizations.SymbolMapper)7 Symbol (io.trino.sql.planner.Symbol)6 Expression (io.trino.sql.tree.Expression)4 PlanNode (io.trino.sql.planner.plan.PlanNode)3 ProjectNode (io.trino.sql.planner.plan.ProjectNode)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 AggregationNode (io.trino.sql.planner.plan.AggregationNode)2 SymbolReference (io.trino.sql.tree.SymbolReference)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 Type (io.trino.spi.type.Type)1 TypeSignatureTranslator.toSqlType (io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType)1 PartitioningScheme (io.trino.sql.planner.PartitioningScheme)1 Aggregation (io.trino.sql.planner.plan.AggregationNode.Aggregation)1 AggregationNode.globalAggregation (io.trino.sql.planner.plan.AggregationNode.globalAggregation)1 Assignments (io.trino.sql.planner.plan.Assignments)1 ExchangeNode (io.trino.sql.planner.plan.ExchangeNode)1 FilterNode (io.trino.sql.planner.plan.FilterNode)1 LimitNode (io.trino.sql.planner.plan.LimitNode)1