Search in sources :

Example 11 with ProjectNode

use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.

the class PushLimitThroughProject method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof LimitNode)) {
        return Optional.empty();
    }
    LimitNode parent = (LimitNode) node;
    PlanNode child = lookup.resolve(parent.getSource());
    if (!(child instanceof ProjectNode)) {
        return Optional.empty();
    }
    return Optional.of(transpose(parent, child));
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) LimitNode(com.facebook.presto.sql.planner.plan.LimitNode) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode)

Example 12 with ProjectNode

use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.

the class QueryPlanner method project.

private PlanBuilder project(PlanBuilder subPlan, Iterable<Expression> expressions) {
    TranslationMap outputTranslations = new TranslationMap(subPlan.getRelationPlan(), analysis, lambdaDeclarationToSymbolMap);
    Assignments.Builder projections = Assignments.builder();
    for (Expression expression : expressions) {
        Expression rewritten = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(analysis.getParameters(), analysis), expression);
        Symbol symbol = symbolAllocator.newSymbol(rewritten, analysis.getTypeWithCoercions(expression));
        projections.put(symbol, subPlan.rewrite(rewritten));
        outputTranslations.addIntermediateMapping(expression, rewritten);
        outputTranslations.put(rewritten, symbol);
    }
    return new PlanBuilder(outputTranslations, new ProjectNode(idAllocator.getNextId(), subPlan.getRoot(), projections.build()), analysis.getParameters());
}
Also used : Expression(com.facebook.presto.sql.tree.Expression) Assignments(com.facebook.presto.sql.planner.plan.Assignments) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode)

Example 13 with ProjectNode

use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.

the class PlanBuilder method appendProjections.

public PlanBuilder appendProjections(Iterable<Expression> expressions, SymbolAllocator symbolAllocator, PlanNodeIdAllocator idAllocator) {
    TranslationMap translations = copyTranslations();
    Assignments.Builder projections = Assignments.builder();
    // add an identity projection for underlying plan
    for (Symbol symbol : getRoot().getOutputSymbols()) {
        projections.put(symbol, symbol.toSymbolReference());
    }
    ImmutableMap.Builder<Symbol, Expression> newTranslations = ImmutableMap.builder();
    ParameterRewriter parameterRewriter = new ParameterRewriter(parameters, getAnalysis());
    for (Expression expression : expressions) {
        Expression rewritten = ExpressionTreeRewriter.rewriteWith(parameterRewriter, expression);
        translations.addIntermediateMapping(expression, rewritten);
        Symbol symbol = symbolAllocator.newSymbol(rewritten, getAnalysis().getTypeWithCoercions(expression));
        projections.put(symbol, translations.rewrite(rewritten));
        newTranslations.put(symbol, rewritten);
    }
    // Now append the new translations into the TranslationMap
    for (Map.Entry<Symbol, Expression> entry : newTranslations.build().entrySet()) {
        translations.put(entry.getValue(), entry.getKey());
    }
    return new PlanBuilder(translations, new ProjectNode(idAllocator.getNextId(), getRoot(), projections.build()), parameters);
}
Also used : Expression(com.facebook.presto.sql.tree.Expression) Assignments(com.facebook.presto.sql.planner.plan.Assignments) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 14 with ProjectNode

use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.

the class QueryPlanner method explicitCoercionFields.

private PlanBuilder explicitCoercionFields(PlanBuilder subPlan, Iterable<Expression> alreadyCoerced, Iterable<? extends Expression> uncoerced) {
    TranslationMap translations = new TranslationMap(subPlan.getRelationPlan(), analysis, lambdaDeclarationToSymbolMap);
    Assignments.Builder projections = Assignments.builder();
    projections.putAll(coerce(uncoerced, subPlan, translations));
    for (Expression expression : alreadyCoerced) {
        Symbol symbol = symbolAllocator.newSymbol(expression, analysis.getType(expression));
        Expression parametersReplaced = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(analysis.getParameters(), analysis), expression);
        translations.addIntermediateMapping(expression, parametersReplaced);
        Expression rewritten = subPlan.rewrite(expression);
        projections.put(symbol, rewritten);
        translations.put(parametersReplaced, symbol);
    }
    return new PlanBuilder(translations, new ProjectNode(idAllocator.getNextId(), subPlan.getRoot(), projections.build()), analysis.getParameters());
}
Also used : Expression(com.facebook.presto.sql.tree.Expression) Assignments(com.facebook.presto.sql.planner.plan.Assignments) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode)

Example 15 with ProjectNode

use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.

the class PruneTableScanColumns method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof ProjectNode)) {
        return Optional.empty();
    }
    ProjectNode parent = (ProjectNode) node;
    PlanNode source = lookup.resolve(parent.getSource());
    if (!(source instanceof TableScanNode)) {
        return Optional.empty();
    }
    TableScanNode child = (TableScanNode) source;
    Optional<List<Symbol>> dependencies = pruneInputs(child.getOutputSymbols(), parent.getAssignments().getExpressions());
    if (!dependencies.isPresent()) {
        return Optional.empty();
    }
    List<Symbol> newOutputs = dependencies.get();
    return Optional.of(new ProjectNode(parent.getId(), new TableScanNode(child.getId(), child.getTable(), newOutputs, newOutputs.stream().collect(Collectors.toMap(Function.identity(), e -> child.getAssignments().get(e))), child.getLayout(), child.getCurrentConstraint(), child.getOriginalConstraint()), parent.getAssignments()));
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) TableScanNode(com.facebook.presto.sql.planner.plan.TableScanNode) Symbol(com.facebook.presto.sql.planner.Symbol) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) List(java.util.List)

Aggregations

ProjectNode (com.facebook.presto.sql.planner.plan.ProjectNode)21 Expression (com.facebook.presto.sql.tree.Expression)14 Assignments (com.facebook.presto.sql.planner.plan.Assignments)11 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)11 Symbol (com.facebook.presto.sql.planner.Symbol)7 Cast (com.facebook.presto.sql.tree.Cast)7 Test (org.testng.annotations.Test)7 AggregationNode (com.facebook.presto.sql.planner.plan.AggregationNode)5 ValuesNode (com.facebook.presto.sql.planner.plan.ValuesNode)5 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)4 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Map (java.util.Map)4 Signature (com.facebook.presto.metadata.Signature)3 Type (com.facebook.presto.spi.type.Type)3 Field (com.facebook.presto.sql.analyzer.Field)3 RelationType (com.facebook.presto.sql.analyzer.RelationType)3 LimitNode (com.facebook.presto.sql.planner.plan.LimitNode)3