Search in sources :

Example 11 with ValuesNode

use of com.facebook.presto.spi.plan.ValuesNode in project presto by prestodb.

the class PushAggregationThroughOuterJoin method createAggregationOverNull.

private Optional<MappedAggregationInfo> createAggregationOverNull(AggregationNode referenceAggregation, PlanVariableAllocator variableAllocator, PlanNodeIdAllocator idAllocator, Lookup lookup) {
    // Create a values node that consists of a single row of nulls.
    // Map the output symbols from the referenceAggregation's source
    // to symbol references for the new values node.
    ImmutableList.Builder<VariableReferenceExpression> nullVariables = ImmutableList.builder();
    ImmutableList.Builder<RowExpression> nullLiterals = ImmutableList.builder();
    ImmutableMap.Builder<VariableReferenceExpression, VariableReferenceExpression> sourcesVariableMappingBuilder = ImmutableMap.builder();
    for (VariableReferenceExpression sourceVariable : referenceAggregation.getSource().getOutputVariables()) {
        RowExpression nullLiteral = constantNull(sourceVariable.getSourceLocation(), sourceVariable.getType());
        nullLiterals.add(nullLiteral);
        VariableReferenceExpression nullVariable = variableAllocator.newVariable(nullLiteral);
        nullVariables.add(nullVariable);
        // TODO The type should be from sourceVariable.getType
        sourcesVariableMappingBuilder.put(sourceVariable, nullVariable);
    }
    ValuesNode nullRow = new ValuesNode(referenceAggregation.getSourceLocation(), idAllocator.getNextId(), nullVariables.build(), ImmutableList.of(nullLiterals.build()));
    Map<VariableReferenceExpression, VariableReferenceExpression> sourcesVariableMapping = sourcesVariableMappingBuilder.build();
    // For each aggregation function in the reference node, create a corresponding aggregation function
    // that points to the nullRow. Map the symbols from the aggregations in referenceAggregation to the
    // symbols in these new aggregations.
    ImmutableMap.Builder<VariableReferenceExpression, VariableReferenceExpression> aggregationsVariableMappingBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<VariableReferenceExpression, AggregationNode.Aggregation> aggregationsOverNullBuilder = ImmutableMap.builder();
    for (Map.Entry<VariableReferenceExpression, AggregationNode.Aggregation> entry : referenceAggregation.getAggregations().entrySet()) {
        VariableReferenceExpression aggregationVariable = entry.getKey();
        AggregationNode.Aggregation aggregation = entry.getValue();
        if (!isUsingVariables(aggregation, sourcesVariableMapping.keySet())) {
            return Optional.empty();
        }
        AggregationNode.Aggregation overNullAggregation = new AggregationNode.Aggregation(new CallExpression(aggregation.getCall().getSourceLocation(), aggregation.getCall().getDisplayName(), aggregation.getCall().getFunctionHandle(), aggregation.getCall().getType(), aggregation.getArguments().stream().map(argument -> inlineVariables(sourcesVariableMapping, argument)).collect(toImmutableList())), aggregation.getFilter().map(filter -> inlineVariables(sourcesVariableMapping, filter)), aggregation.getOrderBy().map(orderBy -> inlineOrderByVariables(sourcesVariableMapping, orderBy)), aggregation.isDistinct(), aggregation.getMask().map(x -> new VariableReferenceExpression(sourcesVariableMapping.get(x).getSourceLocation(), sourcesVariableMapping.get(x).getName(), x.getType())));
        QualifiedObjectName functionName = functionAndTypeManager.getFunctionMetadata(overNullAggregation.getFunctionHandle()).getName();
        VariableReferenceExpression overNull = variableAllocator.newVariable(aggregation.getCall().getSourceLocation(), functionName.getObjectName(), aggregationVariable.getType());
        aggregationsOverNullBuilder.put(overNull, overNullAggregation);
        aggregationsVariableMappingBuilder.put(aggregationVariable, overNull);
    }
    Map<VariableReferenceExpression, VariableReferenceExpression> aggregationsSymbolMapping = aggregationsVariableMappingBuilder.build();
    // create an aggregation node whose source is the null row.
    AggregationNode aggregationOverNullRow = new AggregationNode(referenceAggregation.getSourceLocation(), idAllocator.getNextId(), nullRow, aggregationsOverNullBuilder.build(), globalAggregation(), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty());
    return Optional.of(new MappedAggregationInfo(aggregationOverNullRow, aggregationsSymbolMapping));
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) Captures(com.facebook.presto.matching.Captures) Assignments(com.facebook.presto.spi.plan.Assignments) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Patterns.join(com.facebook.presto.sql.planner.plan.Patterns.join) Pattern(com.facebook.presto.matching.Pattern) ValuesNode(com.facebook.presto.spi.plan.ValuesNode) HashSet(java.util.HashSet) Capture(com.facebook.presto.matching.Capture) ImmutableList(com.google.common.collect.ImmutableList) DistinctOutputQueryUtil.isDistinct(com.facebook.presto.sql.planner.optimizations.DistinctOutputQueryUtil.isDistinct) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) AggregationNode.singleGroupingSet(com.facebook.presto.spi.plan.AggregationNode.singleGroupingSet) CallExpression(com.facebook.presto.spi.relation.CallExpression) OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) Patterns.aggregation(com.facebook.presto.sql.planner.plan.Patterns.aggregation) PlanNodeIdAllocator(com.facebook.presto.spi.plan.PlanNodeIdAllocator) AggregationNode.globalAggregation(com.facebook.presto.spi.plan.AggregationNode.globalAggregation) SortOrder(com.facebook.presto.common.block.SortOrder) ImmutableMap(com.google.common.collect.ImmutableMap) Session(com.facebook.presto.Session) Ordering(com.facebook.presto.spi.plan.Ordering) Rule(com.facebook.presto.sql.planner.iterative.Rule) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) RowExpressionVariableInliner.inlineVariables(com.facebook.presto.sql.planner.RowExpressionVariableInliner.inlineVariables) Expressions.constantNull(com.facebook.presto.sql.relational.Expressions.constantNull) Lookup(com.facebook.presto.sql.planner.iterative.Lookup) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Patterns.source(com.facebook.presto.sql.planner.plan.Patterns.source) PlanNode(com.facebook.presto.spi.plan.PlanNode) List(java.util.List) SystemSessionProperties.shouldPushAggregationThroughJoin(com.facebook.presto.SystemSessionProperties.shouldPushAggregationThroughJoin) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) Capture.newCapture(com.facebook.presto.matching.Capture.newCapture) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator) Optional(java.util.Optional) ValuesNode(com.facebook.presto.spi.plan.ValuesNode) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) RowExpression(com.facebook.presto.spi.relation.RowExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) ImmutableMap(com.google.common.collect.ImmutableMap) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) AggregationNode.globalAggregation(com.facebook.presto.spi.plan.AggregationNode.globalAggregation) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) CallExpression(com.facebook.presto.spi.relation.CallExpression)

Example 12 with ValuesNode

use of com.facebook.presto.spi.plan.ValuesNode in project presto by prestodb.

the class LogicalPlanner method planStatement.

public PlanNode planStatement(Analysis analysis, Statement statement) {
    if (statement instanceof CreateTableAsSelect && analysis.isCreateTableAsSelectNoOp()) {
        checkState(analysis.getCreateTableDestination().isPresent(), "Table destination is missing");
        VariableReferenceExpression variable = variableAllocator.newVariable(getSourceLocation(statement), "rows", BIGINT);
        PlanNode source = new ValuesNode(getSourceLocation(statement), idAllocator.getNextId(), ImmutableList.of(variable), ImmutableList.of(ImmutableList.of(constant(0L, BIGINT))));
        return new OutputNode(source.getSourceLocation(), idAllocator.getNextId(), source, ImmutableList.of("rows"), ImmutableList.of(variable));
    }
    return createOutputPlan(planStatementWithoutOutput(analysis, statement), analysis);
}
Also used : ValuesNode(com.facebook.presto.spi.plan.ValuesNode) OutputNode(com.facebook.presto.sql.planner.plan.OutputNode) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect)

Example 13 with ValuesNode

use of com.facebook.presto.spi.plan.ValuesNode in project presto by prestodb.

the class TestJoinNodeFlattener method testRetainsOutputSymbols.

@Test
public void testRetainsOutputSymbols() {
    PlanBuilder p = planBuilder();
    VariableReferenceExpression a1 = p.variable("A1");
    VariableReferenceExpression b1 = p.variable("B1");
    VariableReferenceExpression b2 = p.variable("B2");
    VariableReferenceExpression c1 = p.variable("C1");
    VariableReferenceExpression c2 = p.variable("C2");
    ValuesNode valuesA = p.values(a1);
    ValuesNode valuesB = p.values(b1, b2);
    ValuesNode valuesC = p.values(c1, c2);
    JoinNode joinNode = p.join(INNER, valuesA, p.join(INNER, valuesB, valuesC, ImmutableList.of(equiJoinClause(b1, c1)), ImmutableList.of(b1, b2, c1, c2), Optional.empty()), ImmutableList.of(equiJoinClause(a1, b1)), ImmutableList.of(a1, b1), Optional.empty());
    MultiJoinNode expected = MultiJoinNode.builder().setSources(valuesA, valuesB, valuesC).setFilter(and(createEqualsExpression(b1, c1), createEqualsExpression(a1, b1))).setOutputVariables(a1, b1).build();
    assertEquals(toMultiJoinNode(joinNode, noLookup(), DEFAULT_JOIN_LIMIT, functionResolution, determinismEvaluator), expected);
}
Also used : ValuesNode(com.facebook.presto.spi.plan.ValuesNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) MultiJoinNode.toMultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode.toMultiJoinNode) MultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) MultiJoinNode.toMultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode.toMultiJoinNode) MultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode) Test(org.testng.annotations.Test)

Example 14 with ValuesNode

use of com.facebook.presto.spi.plan.ValuesNode in project presto by prestodb.

the class TestJoinNodeFlattener method testDoesNotConvertNestedOuterJoins.

@Test
public void testDoesNotConvertNestedOuterJoins() {
    PlanBuilder p = planBuilder();
    VariableReferenceExpression a1 = p.variable("A1");
    VariableReferenceExpression b1 = p.variable("B1");
    VariableReferenceExpression c1 = p.variable("C1");
    JoinNode leftJoin = p.join(LEFT, p.values(a1), p.values(b1), ImmutableList.of(equiJoinClause(a1, b1)), ImmutableList.of(a1, b1), Optional.empty());
    ValuesNode valuesC = p.values(c1);
    JoinNode joinNode = p.join(INNER, leftJoin, valuesC, ImmutableList.of(equiJoinClause(a1, c1)), ImmutableList.of(a1, b1, c1), Optional.empty());
    MultiJoinNode expected = MultiJoinNode.builder().setSources(leftJoin, valuesC).setFilter(createEqualsExpression(a1, c1)).setOutputVariables(a1, b1, c1).build();
    assertEquals(toMultiJoinNode(joinNode, noLookup(), DEFAULT_JOIN_LIMIT, functionResolution, determinismEvaluator), expected);
}
Also used : ValuesNode(com.facebook.presto.spi.plan.ValuesNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) MultiJoinNode.toMultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode.toMultiJoinNode) MultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) MultiJoinNode.toMultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode.toMultiJoinNode) MultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode) Test(org.testng.annotations.Test)

Example 15 with ValuesNode

use of com.facebook.presto.spi.plan.ValuesNode in project presto by prestodb.

the class TestJoinNodeFlattener method testMoreThanJoinLimit.

@Test
public void testMoreThanJoinLimit() {
    PlanBuilder p = planBuilder();
    VariableReferenceExpression a1 = p.variable("A1");
    VariableReferenceExpression b1 = p.variable("B1");
    VariableReferenceExpression c1 = p.variable("C1");
    VariableReferenceExpression d1 = p.variable("D1");
    VariableReferenceExpression d2 = p.variable("D2");
    VariableReferenceExpression e1 = p.variable("E1");
    VariableReferenceExpression e2 = p.variable("E2");
    ValuesNode valuesA = p.values(a1);
    ValuesNode valuesB = p.values(b1);
    ValuesNode valuesC = p.values(c1);
    ValuesNode valuesD = p.values(d1, d2);
    ValuesNode valuesE = p.values(e1, e2);
    JoinNode join1 = p.join(INNER, valuesA, valuesB, ImmutableList.of(equiJoinClause(a1, b1)), ImmutableList.of(a1, b1), Optional.empty());
    JoinNode join2 = p.join(INNER, valuesD, valuesE, ImmutableList.of(equiJoinClause(d1, e1), equiJoinClause(d2, e2)), ImmutableList.of(d1, d2, e1, e2), Optional.empty());
    JoinNode joinNode = p.join(INNER, p.join(INNER, join1, valuesC, ImmutableList.of(equiJoinClause(a1, c1)), ImmutableList.of(a1, b1, c1), Optional.empty()), join2, ImmutableList.of(equiJoinClause(b1, e1)), ImmutableList.of(a1, b1, c1, d1, d2, e1, e2), Optional.empty());
    MultiJoinNode expected = MultiJoinNode.builder().setSources(join1, join2, valuesC).setFilter(and(createEqualsExpression(a1, c1), createEqualsExpression(b1, e1))).setOutputVariables(a1, b1, c1, d1, d2, e1, e2).build();
    assertEquals(toMultiJoinNode(joinNode, noLookup(), 2, functionResolution, determinismEvaluator), expected);
}
Also used : ValuesNode(com.facebook.presto.spi.plan.ValuesNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) MultiJoinNode.toMultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode.toMultiJoinNode) MultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) MultiJoinNode.toMultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode.toMultiJoinNode) MultiJoinNode(com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode) Test(org.testng.annotations.Test)

Aggregations

ValuesNode (com.facebook.presto.spi.plan.ValuesNode)18 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)13 Test (org.testng.annotations.Test)8 PlanNode (com.facebook.presto.spi.plan.PlanNode)7 RowExpression (com.facebook.presto.spi.relation.RowExpression)6 JoinNode (com.facebook.presto.sql.planner.plan.JoinNode)6 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)5 MultiJoinNode (com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode)5 MultiJoinNode.toMultiJoinNode (com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode.toMultiJoinNode)5 PlanBuilder (com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)5 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 Map (java.util.Map)4 Session (com.facebook.presto.Session)3 OutputNode (com.facebook.presto.sql.planner.plan.OutputNode)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Capture (com.facebook.presto.matching.Capture)2 Capture.newCapture (com.facebook.presto.matching.Capture.newCapture)2 Captures (com.facebook.presto.matching.Captures)2