Search in sources :

Example 21 with Assignments

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()));
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Assignments(com.facebook.presto.spi.plan.Assignments) RowExpression(com.facebook.presto.spi.relation.RowExpression) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 22 with Assignments

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);
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) Expression(com.facebook.presto.sql.tree.Expression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) Assignments(com.facebook.presto.spi.plan.Assignments) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) Test(org.testng.annotations.Test)

Example 23 with Assignments

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);
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) Expression(com.facebook.presto.sql.tree.Expression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) Assignments(com.facebook.presto.spi.plan.Assignments) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) Test(org.testng.annotations.Test)

Example 24 with Assignments

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);
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) Expression(com.facebook.presto.sql.tree.Expression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) Assignments(com.facebook.presto.spi.plan.Assignments) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) Test(org.testng.annotations.Test)

Example 25 with Assignments

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());
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) Assignments(com.facebook.presto.spi.plan.Assignments) JoinUsing(com.facebook.presto.sql.tree.JoinUsing) TypeUtils.isEnumType(com.facebook.presto.common.type.TypeUtils.isEnumType) ArrayType(com.facebook.presto.common.type.ArrayType) RowType(com.facebook.presto.common.type.RowType) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) RelationType(com.facebook.presto.sql.analyzer.RelationType) Identifier(com.facebook.presto.sql.tree.Identifier) Analysis(com.facebook.presto.sql.analyzer.Analysis) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression)

Aggregations

Assignments (com.facebook.presto.spi.plan.Assignments)25 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)23 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)22 OriginalExpressionUtils.castToRowExpression (com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression)12 PlanNode (com.facebook.presto.spi.plan.PlanNode)11 RowExpression (com.facebook.presto.spi.relation.RowExpression)11 Expression (com.facebook.presto.sql.tree.Expression)10 CallExpression (com.facebook.presto.spi.relation.CallExpression)8 ImmutableList (com.google.common.collect.ImmutableList)8 Cast (com.facebook.presto.sql.tree.Cast)7 SymbolReference (com.facebook.presto.sql.tree.SymbolReference)7 Map (java.util.Map)6 Type (com.facebook.presto.common.type.Type)5 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)5 FilterNode (com.facebook.presto.spi.plan.FilterNode)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 ImmutableMap (com.google.common.collect.ImmutableMap)5 Session (com.facebook.presto.Session)4 Field (com.facebook.presto.sql.analyzer.Field)4 TupleDomain (com.facebook.presto.common.predicate.TupleDomain)3