Search in sources :

Example 31 with JoinNode

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

the class ScalarAggregationToJoinRewriter method rewriteScalarAggregation.

private PlanNode rewriteScalarAggregation(LateralJoinNode lateralJoinNode, AggregationNode scalarAggregation, PlanNode scalarAggregationSource, Optional<Expression> joinExpression, VariableReferenceExpression nonNull) {
    AssignUniqueId inputWithUniqueColumns = new AssignUniqueId(lateralJoinNode.getSourceLocation(), idAllocator.getNextId(), lateralJoinNode.getInput(), variableAllocator.newVariable(nonNull.getSourceLocation(), "unique", BIGINT));
    JoinNode leftOuterJoin = new JoinNode(scalarAggregation.getSourceLocation(), idAllocator.getNextId(), JoinNode.Type.LEFT, inputWithUniqueColumns, scalarAggregationSource, ImmutableList.of(), ImmutableList.<VariableReferenceExpression>builder().addAll(inputWithUniqueColumns.getOutputVariables()).addAll(scalarAggregationSource.getOutputVariables()).build(), joinExpression.map(OriginalExpressionUtils::castToRowExpression), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableMap.of());
    Optional<AggregationNode> aggregationNode = createAggregationNode(scalarAggregation, leftOuterJoin, nonNull);
    if (!aggregationNode.isPresent()) {
        return lateralJoinNode;
    }
    Optional<ProjectNode> subqueryProjection = searchFrom(lateralJoinNode.getSubquery(), lookup).where(ProjectNode.class::isInstance).recurseOnlyWhen(EnforceSingleRowNode.class::isInstance).findFirst();
    List<VariableReferenceExpression> aggregationOutputVariables = getTruncatedAggregationVariables(lateralJoinNode, aggregationNode.get());
    if (subqueryProjection.isPresent()) {
        Assignments assignments = Assignments.builder().putAll(identitiesAsSymbolReferences(aggregationOutputVariables)).putAll(subqueryProjection.get().getAssignments()).build();
        return new ProjectNode(idAllocator.getNextId(), aggregationNode.get(), assignments);
    } else {
        return new ProjectNode(idAllocator.getNextId(), aggregationNode.get(), identityAssignmentsAsSymbolReferences(aggregationOutputVariables));
    }
}
Also used : AssignUniqueId(com.facebook.presto.sql.planner.plan.AssignUniqueId) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Assignments(com.facebook.presto.spi.plan.Assignments) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) ProjectNode(com.facebook.presto.spi.plan.ProjectNode)

Example 32 with JoinNode

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

the class TransformCorrelatedLateralJoinToJoin method apply.

@Override
public Result apply(LateralJoinNode lateralJoinNode, Captures captures, Context context) {
    PlanNode subquery = lateralJoinNode.getSubquery();
    PlanNodeDecorrelator planNodeDecorrelator = new PlanNodeDecorrelator(context.getIdAllocator(), context.getVariableAllocator(), context.getLookup());
    Optional<DecorrelatedNode> decorrelatedNodeOptional = planNodeDecorrelator.decorrelateFilters(subquery, lateralJoinNode.getCorrelation());
    return decorrelatedNodeOptional.map(decorrelatedNode -> Result.ofPlanNode(new JoinNode(lateralJoinNode.getSourceLocation(), context.getIdAllocator().getNextId(), lateralJoinNode.getType().toJoinNodeType(), lateralJoinNode.getInput(), decorrelatedNode.getNode(), ImmutableList.of(), lateralJoinNode.getOutputVariables(), decorrelatedNode.getCorrelatedPredicates().map(OriginalExpressionUtils::castToRowExpression), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableMap.of()))).orElseGet(Result::empty);
}
Also used : JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) PlanNodeDecorrelator(com.facebook.presto.sql.planner.optimizations.PlanNodeDecorrelator) DecorrelatedNode(com.facebook.presto.sql.planner.optimizations.PlanNodeDecorrelator.DecorrelatedNode) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) ImmutableMap(com.google.common.collect.ImmutableMap) OriginalExpressionUtils(com.facebook.presto.sql.relational.OriginalExpressionUtils) Rule(com.facebook.presto.sql.planner.iterative.Rule) Captures(com.facebook.presto.matching.Captures) Pattern.nonEmpty(com.facebook.presto.matching.Pattern.nonEmpty) Patterns.lateralJoin(com.facebook.presto.sql.planner.plan.Patterns.lateralJoin) LateralJoin.correlation(com.facebook.presto.sql.planner.plan.Patterns.LateralJoin.correlation) Pattern(com.facebook.presto.matching.Pattern) PlanNode(com.facebook.presto.spi.plan.PlanNode) ImmutableList(com.google.common.collect.ImmutableList) Optional(java.util.Optional) PlanNodeDecorrelator(com.facebook.presto.sql.planner.optimizations.PlanNodeDecorrelator) PlanNode(com.facebook.presto.spi.plan.PlanNode) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) DecorrelatedNode(com.facebook.presto.sql.planner.optimizations.PlanNodeDecorrelator.DecorrelatedNode) OriginalExpressionUtils(com.facebook.presto.sql.relational.OriginalExpressionUtils)

Example 33 with JoinNode

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

the class TestLocalDynamicFilter method testCreateMultipleCriteria.

@Test
public void testCreateMultipleCriteria() throws ExecutionException, InterruptedException {
    SubPlan subplan = subplan("SELECT count() FROM lineitem, partsupp " + "WHERE lineitem.partkey = partsupp.partkey AND lineitem.suppkey = partsupp.suppkey " + "AND partsupp.availqty < 10", LogicalPlanner.Stage.OPTIMIZED_AND_VALIDATED, false);
    JoinNode joinNode = searchJoins(subplan.getChildren().get(0).getFragment()).findOnlyElement();
    LocalDynamicFilter filter = LocalDynamicFilter.create(joinNode, 1).orElseThrow(NoSuchElementException::new);
    List<String> filterIds = filter.getBuildChannels().entrySet().stream().sorted(Map.Entry.comparingByValue()).map(Map.Entry::getKey).collect(toImmutableList());
    filter.getTupleDomainConsumer().accept(TupleDomain.withColumnDomains(ImmutableMap.of(filterIds.get(0), Domain.singleValue(BIGINT, 4L), filterIds.get(1), Domain.singleValue(BIGINT, 5L))));
    TupleDomain<VariableReferenceExpression> expected = TupleDomain.withColumnDomains(ImmutableMap.of(new VariableReferenceExpression(Optional.empty(), "partkey", BIGINT), Domain.singleValue(BIGINT, 4L), new VariableReferenceExpression(Optional.empty(), "suppkey", BIGINT), Domain.singleValue(BIGINT, 5L)));
    assertEquals(filter.getResultFuture().get(), expected);
}
Also used : JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) NoSuchElementException(java.util.NoSuchElementException) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) Test(org.testng.annotations.Test)

Example 34 with JoinNode

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

the class TestLocalDynamicFilter method testCreateMultipleJoins.

@Test
public void testCreateMultipleJoins() throws ExecutionException, InterruptedException {
    SubPlan subplan = subplan("SELECT count() FROM lineitem, orders, part " + "WHERE lineitem.orderkey = orders.orderkey AND lineitem.partkey = part.partkey " + "AND orders.custkey < 10 AND part.name = 'abc'", LogicalPlanner.Stage.OPTIMIZED_AND_VALIDATED, false);
    List<JoinNode> joinNodes = searchJoins(subplan.getChildren().get(0).getFragment()).findAll();
    assertEquals(joinNodes.size(), 2);
    for (JoinNode joinNode : joinNodes) {
        LocalDynamicFilter filter = LocalDynamicFilter.create(joinNode, 1).orElseThrow(NoSuchElementException::new);
        String filterId = Iterables.getOnlyElement(filter.getBuildChannels().keySet());
        VariableReferenceExpression probeVariable = Iterables.getOnlyElement(joinNode.getCriteria()).getLeft();
        filter.getTupleDomainConsumer().accept(TupleDomain.withColumnDomains(ImmutableMap.of(filterId, Domain.singleValue(BIGINT, 6L))));
        assertEquals(filter.getResultFuture().get(), TupleDomain.withColumnDomains(ImmutableMap.of(probeVariable, Domain.singleValue(BIGINT, 6L))));
    }
}
Also used : JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) NoSuchElementException(java.util.NoSuchElementException) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) Test(org.testng.annotations.Test)

Example 35 with JoinNode

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

the class TestLocalDynamicFilter method testCreateProbeSideUnion.

@Test
public void testCreateProbeSideUnion() throws ExecutionException, InterruptedException {
    SubPlan subplan = subplan("WITH union_table(key) AS " + "((SELECT partkey FROM part) UNION (SELECT suppkey FROM supplier)) " + "SELECT count() FROM union_table, nation WHERE union_table.key = nation.nationkey " + "AND nation.comment = 'abc'", LogicalPlanner.Stage.OPTIMIZED_AND_VALIDATED, true);
    JoinNode joinNode = searchJoins(subplan.getFragment()).findOnlyElement();
    LocalDynamicFilter filter = LocalDynamicFilter.create(joinNode, 1).orElseThrow(NoSuchElementException::new);
    String filterId = Iterables.getOnlyElement(filter.getBuildChannels().keySet());
    filter.getTupleDomainConsumer().accept(TupleDomain.withColumnDomains(ImmutableMap.of(filterId, Domain.singleValue(BIGINT, 7L))));
    TupleDomain<VariableReferenceExpression> expected = TupleDomain.withColumnDomains(ImmutableMap.of(new VariableReferenceExpression(Optional.empty(), "partkey", BIGINT), Domain.singleValue(BIGINT, 7L), new VariableReferenceExpression(Optional.empty(), "suppkey", BIGINT), Domain.singleValue(BIGINT, 7L)));
    assertEquals(filter.getResultFuture().get(), expected);
}
Also used : JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) NoSuchElementException(java.util.NoSuchElementException) BasePlanTest(com.facebook.presto.sql.planner.assertions.BasePlanTest) Test(org.testng.annotations.Test)

Aggregations

JoinNode (com.facebook.presto.sql.planner.plan.JoinNode)49 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)36 Test (org.testng.annotations.Test)24 PlanNode (com.facebook.presto.spi.plan.PlanNode)23 TableScanNode (com.facebook.presto.spi.plan.TableScanNode)15 RowExpression (com.facebook.presto.spi.relation.RowExpression)15 ImmutableList (com.google.common.collect.ImmutableList)13 FilterNode (com.facebook.presto.spi.plan.FilterNode)11 ColumnHandle (com.facebook.presto.spi.ColumnHandle)9 PlanNodeId (com.facebook.presto.spi.plan.PlanNodeId)9 SemiJoinNode (com.facebook.presto.sql.planner.plan.SemiJoinNode)9 Type (com.facebook.presto.common.type.Type)8 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)8 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)8 Session (com.facebook.presto.Session)6 Metadata (com.facebook.presto.metadata.Metadata)6 Assignments (com.facebook.presto.spi.plan.Assignments)6 BasePlanTest (com.facebook.presto.sql.planner.assertions.BasePlanTest)6 MultiJoinNode (com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode)6 MultiJoinNode.toMultiJoinNode (com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode.toMultiJoinNode)6