Search in sources :

Example 46 with ProjectNode

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);
}
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 47 with ProjectNode

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));
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) Expression(com.facebook.presto.sql.tree.Expression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) OriginalExpressionUtils.asSymbolReference(com.facebook.presto.sql.relational.OriginalExpressionUtils.asSymbolReference) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Assignments(com.facebook.presto.spi.plan.Assignments) ProjectNode(com.facebook.presto.spi.plan.ProjectNode)

Example 48 with ProjectNode

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()));
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) Expression(com.facebook.presto.sql.tree.Expression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) OriginalExpressionUtils.asSymbolReference(com.facebook.presto.sql.relational.OriginalExpressionUtils.asSymbolReference) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Assignments(com.facebook.presto.spi.plan.Assignments) ProjectNode(com.facebook.presto.spi.plan.ProjectNode)

Example 49 with ProjectNode

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

Example 50 with ProjectNode

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()));
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) ExchangeNode(com.facebook.presto.sql.planner.plan.ExchangeNode) ImmutableList(com.google.common.collect.ImmutableList) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) ProjectNode(com.facebook.presto.spi.plan.ProjectNode)

Aggregations

ProjectNode (com.facebook.presto.spi.plan.ProjectNode)53 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)41 Assignments (com.facebook.presto.spi.plan.Assignments)33 PlanNode (com.facebook.presto.spi.plan.PlanNode)23 RowExpression (com.facebook.presto.spi.relation.RowExpression)20 OriginalExpressionUtils.castToRowExpression (com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression)19 CallExpression (com.facebook.presto.spi.relation.CallExpression)16 Expression (com.facebook.presto.sql.tree.Expression)16 ImmutableList (com.google.common.collect.ImmutableList)15 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)14 Map (java.util.Map)12 Test (org.testng.annotations.Test)12 FilterNode (com.facebook.presto.spi.plan.FilterNode)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 Cast (com.facebook.presto.sql.tree.Cast)10 SymbolReference (com.facebook.presto.sql.tree.SymbolReference)10 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)8 List (java.util.List)8 Type (com.facebook.presto.common.type.Type)7 Set (java.util.Set)7