use of com.facebook.presto.spi.plan.ProjectNode in project presto by prestodb.
the class TestTypeValidator method testValidTypeOnlyCoercion.
@Test
public void testValidTypeOnlyCoercion() {
Expression expression = new Cast(new SymbolReference(variableB.getName()), StandardTypes.BIGINT);
Assignments assignments = Assignments.builder().put(variableAllocator.newVariable(expression, BIGINT), castToRowExpression(expression)).put(variableAllocator.newVariable(new SymbolReference(variableE.getName()), VARCHAR), // implicit coercion from varchar(3) to varchar
castToRowExpression(new SymbolReference(variableE.getName()))).build();
PlanNode node = new ProjectNode(newId(), baseTableScan, assignments);
assertTypesValid(node);
}
use of com.facebook.presto.spi.plan.ProjectNode in project presto by prestodb.
the class QueryPlanner method explicitCoercionFields.
private PlanBuilder explicitCoercionFields(PlanBuilder subPlan, Iterable<Expression> alreadyCoerced, Iterable<? extends Expression> uncoerced) {
TranslationMap translations = new TranslationMap(subPlan.getRelationPlan(), analysis, lambdaDeclarationToVariableMap);
Assignments.Builder projections = Assignments.builder();
projections.putAll(coerce(uncoerced, subPlan, translations));
for (Expression expression : alreadyCoerced) {
if (expression instanceof SymbolReference) {
// If this is an identity projection, no need to rewrite it
// This is needed because certain synthetic identity expressions such as "group id" introduced when planning GROUPING
// don't have a corresponding analysis, so the code below doesn't work for them
projections.put(variableAllocator.toVariableReference(expression), castToRowExpression(expression));
continue;
}
VariableReferenceExpression variable = variableAllocator.newVariable(expression, analysis.getType(expression));
Expression rewritten = subPlan.rewrite(expression);
projections.put(variable, castToRowExpression(rewritten));
translations.put(expression, variable);
}
return new PlanBuilder(translations, new ProjectNode(subPlan.getRoot().getSourceLocation(), idAllocator.getNextId(), subPlan.getRoot(), projections.build(), LOCAL));
}
use of com.facebook.presto.spi.plan.ProjectNode in project presto by prestodb.
the class QueryPlanner method project.
private PlanBuilder project(PlanBuilder subPlan, Iterable<Expression> expressions) {
TranslationMap outputTranslations = new TranslationMap(subPlan.getRelationPlan(), analysis, lambdaDeclarationToVariableMap);
Assignments.Builder projections = Assignments.builder();
for (Expression expression : expressions) {
if (expression instanceof SymbolReference) {
VariableReferenceExpression variable = variableAllocator.toVariableReference(expression);
projections.put(variable, castToRowExpression(expression));
outputTranslations.put(expression, variable);
continue;
}
VariableReferenceExpression variable = variableAllocator.newVariable(expression, analysis.getTypeWithCoercions(expression));
projections.put(variable, castToRowExpression(subPlan.rewrite(expression)));
outputTranslations.put(expression, variable);
}
return new PlanBuilder(outputTranslations, new ProjectNode(idAllocator.getNextId(), subPlan.getRoot(), projections.build()));
}
use of com.facebook.presto.spi.plan.ProjectNode in project presto by prestodb.
the class RelationPlanner method planJoinUsing.
private RelationPlan planJoinUsing(Join node, RelationPlan left, RelationPlan right) {
/* Given: l JOIN r USING (k1, ..., kn)
produces:
- project
coalesce(l.k1, r.k1)
...,
coalesce(l.kn, r.kn)
l.v1,
...,
l.vn,
r.v1,
...,
r.vn
- join (l.k1 = r.k1 and ... l.kn = r.kn)
- project
cast(l.k1 as commonType(l.k1, r.k1))
...
- project
cast(rl.k1 as commonType(l.k1, r.k1))
If casts are redundant (due to column type and common type being equal),
they will be removed by optimization passes.
*/
List<Identifier> joinColumns = ((JoinUsing) node.getCriteria().get()).getColumns();
Analysis.JoinUsingAnalysis joinAnalysis = analysis.getJoinUsing(node);
ImmutableList.Builder<JoinNode.EquiJoinClause> clauses = ImmutableList.builder();
Map<Identifier, VariableReferenceExpression> leftJoinColumns = new HashMap<>();
Map<Identifier, VariableReferenceExpression> rightJoinColumns = new HashMap<>();
Assignments.Builder leftCoercions = Assignments.builder();
Assignments.Builder rightCoercions = Assignments.builder();
leftCoercions.putAll(identitiesAsSymbolReferences(left.getRoot().getOutputVariables()));
rightCoercions.putAll(identitiesAsSymbolReferences(right.getRoot().getOutputVariables()));
for (int i = 0; i < joinColumns.size(); i++) {
Identifier identifier = joinColumns.get(i);
Type type = analysis.getType(identifier);
// compute the coercion for the field on the left to the common supertype of left & right
VariableReferenceExpression leftOutput = variableAllocator.newVariable(identifier, type);
int leftField = joinAnalysis.getLeftJoinFields().get(i);
leftCoercions.put(leftOutput, castToRowExpression(new Cast(identifier.getLocation(), createSymbolReference(left.getVariable(leftField)), type.getTypeSignature().toString(), false, metadata.getFunctionAndTypeManager().isTypeOnlyCoercion(left.getDescriptor().getFieldByIndex(leftField).getType(), type))));
leftJoinColumns.put(identifier, leftOutput);
// compute the coercion for the field on the right to the common supertype of left & right
VariableReferenceExpression rightOutput = variableAllocator.newVariable(identifier, type);
int rightField = joinAnalysis.getRightJoinFields().get(i);
rightCoercions.put(rightOutput, castToRowExpression(new Cast(identifier.getLocation(), createSymbolReference(right.getVariable(rightField)), type.getTypeSignature().toString(), false, metadata.getFunctionAndTypeManager().isTypeOnlyCoercion(right.getDescriptor().getFieldByIndex(rightField).getType(), type))));
rightJoinColumns.put(identifier, rightOutput);
clauses.add(new JoinNode.EquiJoinClause(leftOutput, rightOutput));
}
ProjectNode leftCoercion = new ProjectNode(idAllocator.getNextId(), left.getRoot(), leftCoercions.build());
ProjectNode rightCoercion = new ProjectNode(idAllocator.getNextId(), right.getRoot(), rightCoercions.build());
JoinNode join = new JoinNode(getSourceLocation(node), idAllocator.getNextId(), JoinNodeUtils.typeConvert(node.getType()), leftCoercion, rightCoercion, clauses.build(), ImmutableList.<VariableReferenceExpression>builder().addAll(leftCoercion.getOutputVariables()).addAll(rightCoercion.getOutputVariables()).build(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableMap.of());
// Add a projection to produce the outputs of the columns in the USING clause,
// which are defined as coalesce(l.k, r.k)
Assignments.Builder assignments = Assignments.builder();
ImmutableList.Builder<VariableReferenceExpression> outputs = ImmutableList.builder();
for (Identifier column : joinColumns) {
VariableReferenceExpression output = variableAllocator.newVariable(column, analysis.getType(column));
outputs.add(output);
assignments.put(output, castToRowExpression(new CoalesceExpression(column.getLocation(), createSymbolReference(leftJoinColumns.get(column)), createSymbolReference(rightJoinColumns.get(column)))));
}
for (int field : joinAnalysis.getOtherLeftFields()) {
VariableReferenceExpression variable = left.getFieldMappings().get(field);
outputs.add(variable);
assignments.put(variable, castToRowExpression(createSymbolReference(variable)));
}
for (int field : joinAnalysis.getOtherRightFields()) {
VariableReferenceExpression variable = right.getFieldMappings().get(field);
outputs.add(variable);
assignments.put(variable, castToRowExpression(createSymbolReference(variable)));
}
return new RelationPlan(new ProjectNode(idAllocator.getNextId(), join, assignments.build()), analysis.getScope(node), outputs.build());
}
use of com.facebook.presto.spi.plan.ProjectNode in project presto by prestodb.
the class AddIntermediateAggregations method recurseToPartial.
/**
* Recurse through a series of preceding ExchangeNodes and ProjectNodes to find the preceding PARTIAL aggregation
*/
private Optional<PlanNode> recurseToPartial(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, TypeProvider types) {
if (node instanceof AggregationNode && ((AggregationNode) node).getStep() == PARTIAL) {
return Optional.of(addGatheringIntermediate((AggregationNode) node, idAllocator, types));
}
if (!(node instanceof ExchangeNode) && !(node instanceof ProjectNode)) {
return Optional.empty();
}
ImmutableList.Builder<PlanNode> builder = ImmutableList.builder();
for (PlanNode source : node.getSources()) {
Optional<PlanNode> planNode = recurseToPartial(lookup.resolve(source), lookup, idAllocator, types);
if (!planNode.isPresent()) {
return Optional.empty();
}
builder.add(planNode.get());
}
return Optional.of(node.replaceChildren(builder.build()));
}
Aggregations