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));
}
}
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);
}
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);
}
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))));
}
}
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);
}
Aggregations