Search in sources :

Example 6 with ProjectNode

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

the class RelationPlanner method planCrossJoinUnnest.

private RelationPlan planCrossJoinUnnest(RelationPlan leftPlan, Join joinNode, Unnest node) {
    RelationType unnestOutputDescriptor = analysis.getOutputDescriptor(node);
    // Create symbols for the result of unnesting
    ImmutableList.Builder<Symbol> unnestedSymbolsBuilder = ImmutableList.builder();
    for (Field field : unnestOutputDescriptor.getVisibleFields()) {
        Symbol symbol = symbolAllocator.newSymbol(field);
        unnestedSymbolsBuilder.add(symbol);
    }
    ImmutableList<Symbol> unnestedSymbols = unnestedSymbolsBuilder.build();
    // Add a projection for all the unnest arguments
    PlanBuilder planBuilder = initializePlanBuilder(leftPlan);
    planBuilder = planBuilder.appendProjections(node.getExpressions(), symbolAllocator, idAllocator);
    TranslationMap translations = planBuilder.getTranslations();
    ProjectNode projectNode = (ProjectNode) planBuilder.getRoot();
    ImmutableMap.Builder<Symbol, List<Symbol>> unnestSymbols = ImmutableMap.builder();
    UnmodifiableIterator<Symbol> unnestedSymbolsIterator = unnestedSymbols.iterator();
    for (Expression expression : node.getExpressions()) {
        Type type = analysis.getType(expression);
        Symbol inputSymbol = translations.get(expression);
        if (type instanceof ArrayType) {
            unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next()));
        } else if (type instanceof MapType) {
            unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next(), unnestedSymbolsIterator.next()));
        } else {
            throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
        }
    }
    Optional<Symbol> ordinalitySymbol = node.isWithOrdinality() ? Optional.of(unnestedSymbolsIterator.next()) : Optional.empty();
    checkState(!unnestedSymbolsIterator.hasNext(), "Not all output symbols were matched with input symbols");
    UnnestNode unnestNode = new UnnestNode(idAllocator.getNextId(), projectNode, leftPlan.getFieldMappings(), unnestSymbols.build(), ordinalitySymbol);
    return new RelationPlan(unnestNode, analysis.getScope(joinNode), unnestNode.getOutputSymbols());
}
Also used : ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) MapType(com.facebook.presto.type.MapType) ArrayType(com.facebook.presto.type.ArrayType) Field(com.facebook.presto.sql.analyzer.Field) ComparisonExpressionType(com.facebook.presto.sql.tree.ComparisonExpressionType) ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) RelationType(com.facebook.presto.sql.analyzer.RelationType) ExpressionInterpreter.evaluateConstantExpression(com.facebook.presto.sql.planner.ExpressionInterpreter.evaluateConstantExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) UnnestNode(com.facebook.presto.sql.planner.plan.UnnestNode) RelationType(com.facebook.presto.sql.analyzer.RelationType) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList)

Example 7 with ProjectNode

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

the class ImplementFilteredAggregations method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof AggregationNode)) {
        return Optional.empty();
    }
    AggregationNode aggregation = (AggregationNode) node;
    boolean hasFilters = aggregation.getAggregations().entrySet().stream().anyMatch(e -> e.getValue().getFilter().isPresent() && // can't handle filtered aggregations with DISTINCT (conservatively, if they have a mask)
    !aggregation.getMasks().containsKey(e.getKey()));
    if (!hasFilters) {
        return Optional.empty();
    }
    Assignments.Builder newAssignments = Assignments.builder();
    ImmutableMap.Builder<Symbol, Symbol> masks = ImmutableMap.<Symbol, Symbol>builder().putAll(aggregation.getMasks());
    ImmutableMap.Builder<Symbol, FunctionCall> calls = ImmutableMap.builder();
    for (Map.Entry<Symbol, FunctionCall> entry : aggregation.getAggregations().entrySet()) {
        Symbol output = entry.getKey();
        // strip the filters
        FunctionCall call = entry.getValue();
        calls.put(output, new FunctionCall(call.getName(), call.getWindow(), Optional.empty(), call.isDistinct(), call.getArguments()));
        if (call.getFilter().isPresent()) {
            Expression filter = entry.getValue().getFilter().get();
            Symbol symbol = symbolAllocator.newSymbol(filter, BOOLEAN);
            newAssignments.put(symbol, filter);
            masks.put(output, symbol);
        }
    }
    // identity projection for all existing inputs
    newAssignments.putIdentities(aggregation.getSource().getOutputSymbols());
    return Optional.of(new AggregationNode(idAllocator.getNextId(), new ProjectNode(idAllocator.getNextId(), aggregation.getSource(), newAssignments.build()), calls.build(), aggregation.getFunctions(), masks.build(), aggregation.getGroupingSets(), aggregation.getStep(), aggregation.getHashSymbol(), aggregation.getGroupIdSymbol()));
}
Also used : Symbol(com.facebook.presto.sql.planner.Symbol) Assignments(com.facebook.presto.sql.planner.plan.Assignments) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) ImmutableMap(com.google.common.collect.ImmutableMap) Expression(com.facebook.presto.sql.tree.Expression) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 8 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 9 with ProjectNode

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

the class TestEffectivePredicateExtractor method testProject.

@Test
public void testProject() throws Exception {
    PlanNode node = new ProjectNode(newId(), filter(baseTableScan, and(equals(AE, BE), equals(BE, CE), lessThan(CE, bigintLiteral(10)))), Assignments.of(D, AE, E, CE));
    Expression effectivePredicate = EffectivePredicateExtractor.extract(node, TYPES);
    // Rewrite in terms of project output symbols
    assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjuncts(lessThan(DE, bigintLiteral(10)), equals(DE, EE)));
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) Test(org.testng.annotations.Test)

Example 10 with ProjectNode

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

the class TestCountConstantOptimizer method testCountConstantOptimizer.

@Test
public void testCountConstantOptimizer() throws Exception {
    CountConstantOptimizer optimizer = new CountConstantOptimizer();
    PlanNodeIdAllocator planNodeIdAllocator = new PlanNodeIdAllocator();
    Symbol countAggregationSymbol = new Symbol("count");
    Signature countAggregationSignature = new Signature("count", FunctionKind.AGGREGATE, parseTypeSignature(StandardTypes.BIGINT), parseTypeSignature(StandardTypes.BIGINT));
    ImmutableMap<Symbol, FunctionCall> aggregations = ImmutableMap.of(countAggregationSymbol, new FunctionCall(QualifiedName.of("count"), ImmutableList.of(new SymbolReference("expr"))));
    ImmutableMap<Symbol, Signature> functions = ImmutableMap.of(countAggregationSymbol, countAggregationSignature);
    ValuesNode valuesNode = new ValuesNode(planNodeIdAllocator.getNextId(), ImmutableList.of(new Symbol("col")), ImmutableList.of(ImmutableList.of()));
    AggregationNode eligiblePlan = new AggregationNode(planNodeIdAllocator.getNextId(), new ProjectNode(planNodeIdAllocator.getNextId(), valuesNode, Assignments.of(new Symbol("expr"), new LongLiteral("42"))), aggregations, functions, ImmutableMap.of(), ImmutableList.of(ImmutableList.of()), AggregationNode.Step.INTERMEDIATE, Optional.empty(), Optional.empty());
    assertTrue(((AggregationNode) optimizer.optimize(eligiblePlan, TEST_SESSION, ImmutableMap.of(), new SymbolAllocator(), new PlanNodeIdAllocator())).getAggregations().get(countAggregationSymbol).getArguments().isEmpty());
    AggregationNode ineligiblePlan = new AggregationNode(planNodeIdAllocator.getNextId(), new ProjectNode(planNodeIdAllocator.getNextId(), valuesNode, Assignments.of(new Symbol("expr"), new FunctionCall(QualifiedName.of("function"), ImmutableList.of(new Identifier("x"))))), aggregations, functions, ImmutableMap.of(), ImmutableList.of(ImmutableList.of()), AggregationNode.Step.INTERMEDIATE, Optional.empty(), Optional.empty());
    assertFalse(((AggregationNode) optimizer.optimize(ineligiblePlan, TEST_SESSION, ImmutableMap.of(), new SymbolAllocator(), new PlanNodeIdAllocator())).getAggregations().get(countAggregationSymbol).getArguments().isEmpty());
}
Also used : SymbolAllocator(com.facebook.presto.sql.planner.SymbolAllocator) ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) Symbol(com.facebook.presto.sql.planner.Symbol) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) Identifier(com.facebook.presto.sql.tree.Identifier) PlanNodeIdAllocator(com.facebook.presto.sql.planner.PlanNodeIdAllocator) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

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