use of com.facebook.presto.spi.plan.Assignments in project presto by prestodb.
the class CanonicalPlanGenerator method visitProject.
@Override
public Optional<PlanNode> visitProject(ProjectNode node, Map<VariableReferenceExpression, VariableReferenceExpression> context) {
Optional<PlanNode> source = node.getSource().accept(this, context);
if (!source.isPresent()) {
return Optional.empty();
}
List<RowExpressionReference> rowExpressionReferences = node.getAssignments().entrySet().stream().map(entry -> new RowExpressionReference(inlineVariables(context, entry.getValue()), entry.getKey())).sorted(comparing(rowExpressionReference -> rowExpressionReference.getRowExpression().toString())).collect(toImmutableList());
ImmutableMap.Builder<VariableReferenceExpression, RowExpression> assignments = ImmutableMap.builder();
for (RowExpressionReference rowExpressionReference : rowExpressionReferences) {
VariableReferenceExpression reference = variableAllocator.newVariable(rowExpressionReference.getRowExpression());
context.put(rowExpressionReference.getVariableReferenceExpression(), reference);
assignments.put(reference, rowExpressionReference.getRowExpression());
}
return Optional.of(new ProjectNode(node.getSourceLocation(), planNodeidAllocator.getNextId(), source.get(), new Assignments(assignments.build()), node.getLocality()));
}
use of com.facebook.presto.spi.plan.Assignments in project presto by prestodb.
the class TestTypeValidator method testValidProject.
@Test
public void testValidProject() {
Expression expression1 = new Cast(new SymbolReference(variableB.getName()), StandardTypes.BIGINT);
Expression expression2 = new Cast(new SymbolReference(variableC.getName()), StandardTypes.BIGINT);
Assignments assignments = Assignments.builder().put(variableAllocator.newVariable(expression1, BIGINT), castToRowExpression(expression1)).put(variableAllocator.newVariable(expression2, BIGINT), castToRowExpression(expression2)).build();
PlanNode node = new ProjectNode(newId(), baseTableScan, assignments);
assertTypesValid(node);
}
use of com.facebook.presto.spi.plan.Assignments in project presto by prestodb.
the class TestTypeValidator method testInvalidProject.
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "type of variable 'expr(_[0-9]+)?' is expected to be bigint, but the actual type is integer")
public void testInvalidProject() {
Expression expression1 = new Cast(new SymbolReference(variableB.getName()), StandardTypes.INTEGER);
Expression expression2 = new Cast(new SymbolReference(variableA.getName()), StandardTypes.INTEGER);
Assignments assignments = Assignments.builder().put(variableAllocator.newVariable(expression1, BIGINT), // should be INTEGER
castToRowExpression(expression1)).put(variableAllocator.newVariable(expression1, INTEGER), castToRowExpression(expression2)).build();
PlanNode node = new ProjectNode(newId(), baseTableScan, assignments);
assertTypesValid(node);
}
use of com.facebook.presto.spi.plan.Assignments 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.Assignments 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());
}
Aggregations