use of com.facebook.presto.spi.plan.Assignments in project presto by prestodb.
the class PushProjectionThroughUnion method apply.
@Override
public Result apply(ProjectNode parent, Captures captures, Context context) {
UnionNode source = captures.get(CHILD);
// OutputLayout of the resultant Union, will be same as the layout of the Project
List<VariableReferenceExpression> outputLayout = parent.getOutputVariables();
// Mapping from the output symbol to ordered list of symbols from each of the sources
ImmutableListMultimap.Builder<VariableReferenceExpression, VariableReferenceExpression> mappings = ImmutableListMultimap.builder();
// sources for the resultant UnionNode
ImmutableList.Builder<PlanNode> outputSources = ImmutableList.builder();
for (int i = 0; i < source.getSources().size(); i++) {
// assignments for the new ProjectNode
Assignments.Builder assignments = Assignments.builder();
// mapping from current ProjectNode to new ProjectNode, used to identify the output layout
Map<VariableReferenceExpression, VariableReferenceExpression> projectVariableMapping = new HashMap<>();
// Translate the assignments in the ProjectNode using symbols of the source of the UnionNode
for (Map.Entry<VariableReferenceExpression, RowExpression> entry : parent.getAssignments().entrySet()) {
RowExpression translatedExpression;
VariableReferenceExpression variable;
translatedExpression = RowExpressionVariableInliner.inlineVariables(source.sourceVariableMap(i), entry.getValue());
variable = context.getVariableAllocator().newVariable(translatedExpression);
assignments.put(variable, translatedExpression);
projectVariableMapping.put(entry.getKey(), variable);
}
outputSources.add(new ProjectNode(source.getSourceLocation(), context.getIdAllocator().getNextId(), source.getSources().get(i), assignments.build(), parent.getLocality()));
outputLayout.forEach(variable -> mappings.put(variable, projectVariableMapping.get(variable)));
}
ListMultimap<VariableReferenceExpression, VariableReferenceExpression> outputsToInputs = mappings.build();
return Result.ofPlanNode(new UnionNode(source.getSourceLocation(), parent.getId(), outputSources.build(), ImmutableList.copyOf(outputsToInputs.keySet()), fromListMultimap(outputsToInputs)));
}
use of com.facebook.presto.spi.plan.Assignments in project presto by prestodb.
the class TestVerifyNoOriginalExpression method validateJoin.
private void validateJoin(RowExpression rowExpressionPredicate, Expression expressionPredicate, boolean ifRowExpression) {
ImmutableMap<VariableReferenceExpression, RowExpression> map = ImmutableMap.of(VARIABLE_REFERENCE_EXPRESSION, VARIABLE_REFERENCE_EXPRESSION);
ProjectNode projectNode = builder.project(new Assignments(map), valuesNode);
JoinNode joinNode;
if (ifRowExpression) {
joinNode = builder.join(JoinNode.Type.INNER, projectNode, projectNode, rowExpressionPredicate);
} else {
joinNode = builder.join(JoinNode.Type.INNER, projectNode, projectNode, castToRowExpression(expressionPredicate));
}
testValidation(joinNode);
}
use of com.facebook.presto.spi.plan.Assignments 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.spi.plan.Assignments in project presto by prestodb.
the class RewriteFilterWithExternalFunctionToProject method apply.
@Override
public Result apply(FilterNode node, Captures captures, Context context) {
if (!node.getPredicate().accept(new ExternalCallExpressionChecker(functionAndTypeManager), null)) {
// No remote function in predicate
return Result.empty();
}
VariableReferenceExpression predicateVariable = context.getVariableAllocator().newVariable(node.getPredicate());
Assignments.Builder assignments = Assignments.builder();
node.getOutputVariables().forEach(variable -> assignments.put(variable, variable));
Assignments identityAssignments = assignments.build();
assignments.put(predicateVariable, node.getPredicate());
return Result.ofPlanNode(new ProjectNode(node.getSourceLocation(), context.getIdAllocator().getNextId(), new FilterNode(node.getSourceLocation(), context.getIdAllocator().getNextId(), new ProjectNode(context.getIdAllocator().getNextId(), node.getSource(), assignments.build()), predicateVariable), identityAssignments, LOCAL));
}
use of com.facebook.presto.spi.plan.Assignments in project presto by prestodb.
the class TransformExistsApplyToLateralNode method rewriteToNonDefaultAggregation.
private Optional<PlanNode> rewriteToNonDefaultAggregation(ApplyNode applyNode, Context context) {
checkState(applyNode.getSubquery().getOutputVariables().isEmpty(), "Expected subquery output variables to be pruned");
VariableReferenceExpression exists = getOnlyElement(applyNode.getSubqueryAssignments().getVariables());
VariableReferenceExpression subqueryTrue = context.getVariableAllocator().newVariable(exists.getSourceLocation(), "subqueryTrue", BOOLEAN);
Assignments.Builder assignments = Assignments.builder();
assignments.putAll(identitiesAsSymbolReferences(applyNode.getInput().getOutputVariables()));
assignments.put(exists, castToRowExpression(new CoalesceExpression(ImmutableList.of(createSymbolReference(subqueryTrue), BooleanLiteral.FALSE_LITERAL))));
PlanNode subquery = new ProjectNode(context.getIdAllocator().getNextId(), new LimitNode(applyNode.getSourceLocation(), context.getIdAllocator().getNextId(), applyNode.getSubquery(), 1L, FINAL), Assignments.of(subqueryTrue, castToRowExpression(TRUE_LITERAL)));
PlanNodeDecorrelator decorrelator = new PlanNodeDecorrelator(context.getIdAllocator(), context.getVariableAllocator(), context.getLookup());
if (!decorrelator.decorrelateFilters(subquery, applyNode.getCorrelation()).isPresent()) {
return Optional.empty();
}
return Optional.of(new ProjectNode(context.getIdAllocator().getNextId(), new LateralJoinNode(applyNode.getSourceLocation(), applyNode.getId(), applyNode.getInput(), subquery, applyNode.getCorrelation(), LEFT, applyNode.getOriginSubqueryError()), assignments.build()));
}
Aggregations