Search in sources :

Example 21 with ProjectNode

use of io.prestosql.spi.plan.ProjectNode in project hetu-core by openlookeng.

the class RelationPlanner method planCrossJoinUnnest.

private RelationPlan planCrossJoinUnnest(RelationPlan leftPlan, Join joinNode, Unnest node) {
    RelationType unnestOutputDescriptor = analysis.getOutputDescriptor(node);
    // Create symbols for the result of unnesting
    ImmutableList.Builder<Symbol> unnestedSymbolsBuilder = ImmutableList.builder();
    for (Field field : unnestOutputDescriptor.getVisibleFields()) {
        Symbol symbol = planSymbolAllocator.newSymbol(field);
        unnestedSymbolsBuilder.add(symbol);
    }
    ImmutableList<Symbol> unnestedSymbols = unnestedSymbolsBuilder.build();
    // Add a projection for all the unnest arguments
    PlanBuilder planBuilder = initializePlanBuilder(leftPlan);
    planBuilder = planBuilder.appendProjections(node.getExpressions(), planSymbolAllocator, idAllocator);
    TranslationMap translations = planBuilder.getTranslations();
    ProjectNode projectNode = (ProjectNode) planBuilder.getRoot();
    ImmutableMap.Builder<Symbol, List<Symbol>> unnestSymbols = ImmutableMap.builder();
    UnmodifiableIterator<Symbol> unnestedSymbolsIterator = unnestedSymbols.iterator();
    for (Expression expression : node.getExpressions()) {
        Type type = analysis.getType(expression);
        Symbol inputSymbol = translations.get(expression);
        if (type instanceof ArrayType) {
            Type elementType = ((ArrayType) type).getElementType();
            if (elementType instanceof RowType) {
                ImmutableList.Builder<Symbol> unnestSymbolBuilder = ImmutableList.builder();
                for (int i = 0; i < ((RowType) elementType).getFields().size(); i++) {
                    unnestSymbolBuilder.add(unnestedSymbolsIterator.next());
                }
                unnestSymbols.put(inputSymbol, unnestSymbolBuilder.build());
            } else {
                unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next()));
            }
        } else if (type instanceof MapType) {
            unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next(), unnestedSymbolsIterator.next()));
        } else {
            throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
        }
    }
    Optional<Symbol> ordinalitySymbol = node.isWithOrdinality() ? Optional.of(unnestedSymbolsIterator.next()) : Optional.empty();
    checkState(!unnestedSymbolsIterator.hasNext(), "Not all output symbols were matched with input symbols");
    UnnestNode unnestNode = new UnnestNode(idAllocator.getNextId(), projectNode, leftPlan.getFieldMappings(), unnestSymbols.build(), ordinalitySymbol);
    return new RelationPlan(unnestNode, analysis.getScope(joinNode), unnestNode.getOutputSymbols());
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Symbol(io.prestosql.spi.plan.Symbol) RowType(io.prestosql.spi.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap) MapType(io.prestosql.spi.type.MapType) ArrayType(io.prestosql.spi.type.ArrayType) Field(io.prestosql.sql.analyzer.Field) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) RelationType(io.prestosql.sql.analyzer.RelationType) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) UnnestNode(io.prestosql.sql.planner.plan.UnnestNode) RelationType(io.prestosql.sql.analyzer.RelationType) ProjectNode(io.prestosql.spi.plan.ProjectNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 22 with ProjectNode

use of io.prestosql.spi.plan.ProjectNode in project hetu-core by openlookeng.

the class RelationPlanner method addCoercions.

private RelationPlan addCoercions(RelationPlan plan, Type[] targetColumnTypes) {
    List<Symbol> oldSymbols = plan.getFieldMappings();
    RelationType oldDescriptor = plan.getDescriptor().withOnlyVisibleFields();
    verify(targetColumnTypes.length == oldSymbols.size());
    ImmutableList.Builder<Symbol> newSymbols = new ImmutableList.Builder<>();
    Field[] newFields = new Field[targetColumnTypes.length];
    Assignments.Builder assignments = Assignments.builder();
    for (int i = 0; i < targetColumnTypes.length; i++) {
        Symbol inputSymbol = oldSymbols.get(i);
        Type inputType = planSymbolAllocator.getTypes().get(inputSymbol);
        Type outputType = targetColumnTypes[i];
        if (!outputType.equals(inputType)) {
            Expression cast = new Cast(toSymbolReference(inputSymbol), outputType.getTypeSignature().toString());
            Symbol outputSymbol = planSymbolAllocator.newSymbol(cast, outputType);
            assignments.put(outputSymbol, castToRowExpression(cast));
            newSymbols.add(outputSymbol);
        } else {
            SymbolReference symbolReference = toSymbolReference(inputSymbol);
            Symbol outputSymbol = planSymbolAllocator.newSymbol(symbolReference, outputType);
            assignments.put(outputSymbol, castToRowExpression(symbolReference));
            newSymbols.add(outputSymbol);
        }
        Field oldField = oldDescriptor.getFieldByIndex(i);
        newFields[i] = new Field(oldField.getRelationAlias(), oldField.getName(), targetColumnTypes[i], oldField.isHidden(), oldField.getOriginTable(), oldField.getOriginColumnName(), oldField.isAliased());
    }
    ProjectNode projectNode = new ProjectNode(idAllocator.getNextId(), plan.getRoot(), assignments.build());
    return new RelationPlan(projectNode, Scope.builder().withRelationType(RelationId.anonymous(), new RelationType(newFields)).build(), newSymbols.build());
}
Also used : Cast(io.prestosql.sql.tree.Cast) Symbol(io.prestosql.spi.plan.Symbol) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) SymbolReference(io.prestosql.sql.tree.SymbolReference) Assignments(io.prestosql.spi.plan.Assignments) Field(io.prestosql.sql.analyzer.Field) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) RelationType(io.prestosql.sql.analyzer.RelationType) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) RelationType(io.prestosql.sql.analyzer.RelationType) ProjectNode(io.prestosql.spi.plan.ProjectNode)

Example 23 with ProjectNode

use of io.prestosql.spi.plan.ProjectNode in project hetu-core by openlookeng.

the class RelationPlanner method visitAliasedRelation.

@Override
protected RelationPlan visitAliasedRelation(AliasedRelation node, Void context) {
    RelationPlan subPlan = process(node.getRelation(), context);
    PlanNode root = subPlan.getRoot();
    List<Symbol> mappings = subPlan.getFieldMappings();
    if (node.getColumnNames() != null) {
        ImmutableList.Builder<Symbol> newMappings = ImmutableList.builder();
        Assignments.Builder assignments = Assignments.builder();
        // project only the visible columns from the underlying relation
        for (int i = 0; i < subPlan.getDescriptor().getAllFieldCount(); i++) {
            Field field = subPlan.getDescriptor().getFieldByIndex(i);
            if (!field.isHidden()) {
                Symbol aliasedColumn = planSymbolAllocator.newSymbol(field);
                assignments.put(aliasedColumn, castToRowExpression(toSymbolReference(subPlan.getFieldMappings().get(i))));
                newMappings.add(aliasedColumn);
            }
        }
        root = new ProjectNode(idAllocator.getNextId(), subPlan.getRoot(), assignments.build());
        mappings = newMappings.build();
    }
    return new RelationPlan(root, analysis.getScope(node), mappings);
}
Also used : Field(io.prestosql.sql.analyzer.Field) PlanNode(io.prestosql.spi.plan.PlanNode) Symbol(io.prestosql.spi.plan.Symbol) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Assignments(io.prestosql.spi.plan.Assignments) ProjectNode(io.prestosql.spi.plan.ProjectNode)

Example 24 with ProjectNode

use of io.prestosql.spi.plan.ProjectNode in project hetu-core by openlookeng.

the class SubqueryPlanner method appendExistSubqueryApplyNode.

/**
 * Exists is modeled as:
 * <pre>
 *     - Project($0 > 0)
 *       - Aggregation(COUNT(*))
 *         - Limit(1)
 *           -- subquery
 * </pre>
 */
private PlanBuilder appendExistSubqueryApplyNode(PlanBuilder subPlan, ExistsPredicate existsPredicate, boolean correlationAllowed) {
    if (subPlan.canTranslate(existsPredicate)) {
        // given subquery is already appended
        return subPlan;
    }
    PlanBuilder subqueryPlan = createPlanBuilder(existsPredicate.getSubquery());
    PlanNode subqueryPlanRoot = subqueryPlan.getRoot();
    if (isAggregationWithEmptyGroupBy(subqueryPlanRoot)) {
        subPlan.getTranslations().put(existsPredicate, TRUE_LITERAL);
        return subPlan;
    }
    // add an explicit projection that removes all columns
    PlanNode subqueryNode = new ProjectNode(idAllocator.getNextId(), subqueryPlan.getRoot(), Assignments.of());
    Symbol exists = planSymbolAllocator.newSymbol("exists", BOOLEAN);
    subPlan.getTranslations().put(existsPredicate, exists);
    ExistsPredicate rewrittenExistsPredicate = new ExistsPredicate(TRUE_LITERAL);
    return appendApplyNode(subPlan, existsPredicate.getSubquery(), subqueryNode, Assignments.of(exists, castToRowExpression(rewrittenExistsPredicate)), correlationAllowed);
}
Also used : PlanNode(io.prestosql.spi.plan.PlanNode) Symbol(io.prestosql.spi.plan.Symbol) ExistsPredicate(io.prestosql.sql.tree.ExistsPredicate) ProjectNode(io.prestosql.spi.plan.ProjectNode)

Example 25 with ProjectNode

use of io.prestosql.spi.plan.ProjectNode in project hetu-core by openlookeng.

the class ImplementFilteredAggregations method apply.

@Override
public Result apply(AggregationNode aggregationNode, Captures captures, Context context) {
    Assignments.Builder newAssignments = Assignments.builder();
    ImmutableMap.Builder<Symbol, Aggregation> aggregations = ImmutableMap.builder();
    ImmutableList.Builder<Expression> maskSymbols = ImmutableList.builder();
    boolean aggregateWithoutFilterPresent = false;
    for (Map.Entry<Symbol, Aggregation> entry : aggregationNode.getAggregations().entrySet()) {
        Symbol output = entry.getKey();
        // strip the filters
        Aggregation aggregation = entry.getValue();
        Optional<Symbol> mask = aggregation.getMask();
        if (aggregation.getFilter().isPresent()) {
            Symbol filter = aggregation.getFilter().get();
            Symbol symbol = context.getSymbolAllocator().newSymbol(filter.getName(), BOOLEAN);
            verify(!mask.isPresent(), "Expected aggregation without mask symbols, see Rule pattern");
            newAssignments.put(symbol, castToRowExpression(toSymbolReference(filter)));
            mask = Optional.of(symbol);
            maskSymbols.add(toSymbolReference(symbol));
        } else {
            aggregateWithoutFilterPresent = true;
        }
        aggregations.put(output, new Aggregation(aggregation.getFunctionCall(), aggregation.getArguments(), aggregation.isDistinct(), Optional.empty(), aggregation.getOrderingScheme(), mask));
    }
    Expression predicate = TRUE_LITERAL;
    if (!aggregationNode.hasNonEmptyGroupingSet() && !aggregateWithoutFilterPresent) {
        predicate = combineDisjunctsWithDefault(maskSymbols.build(), TRUE_LITERAL);
    }
    // identity projection for all existing inputs
    newAssignments.putAll(AssignmentUtils.identityAsSymbolReferences(aggregationNode.getSource().getOutputSymbols()));
    return Result.ofPlanNode(new AggregationNode(context.getIdAllocator().getNextId(), new FilterNode(context.getIdAllocator().getNextId(), new ProjectNode(context.getIdAllocator().getNextId(), aggregationNode.getSource(), newAssignments.build()), castToRowExpression(predicate)), aggregations.build(), aggregationNode.getGroupingSets(), ImmutableList.of(), aggregationNode.getStep(), aggregationNode.getHashSymbol(), aggregationNode.getGroupIdSymbol(), aggregationNode.getAggregationType(), aggregationNode.getFinalizeSymbol()));
}
Also used : Symbol(io.prestosql.spi.plan.Symbol) ImmutableList(com.google.common.collect.ImmutableList) FilterNode(io.prestosql.spi.plan.FilterNode) Assignments(io.prestosql.spi.plan.Assignments) AggregationNode(io.prestosql.spi.plan.AggregationNode) ImmutableMap(com.google.common.collect.ImmutableMap) Aggregation(io.prestosql.spi.plan.AggregationNode.Aggregation) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) Expression(io.prestosql.sql.tree.Expression) ProjectNode(io.prestosql.spi.plan.ProjectNode) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Aggregations

ProjectNode (io.prestosql.spi.plan.ProjectNode)79 Symbol (io.prestosql.spi.plan.Symbol)56 Assignments (io.prestosql.spi.plan.Assignments)47 PlanNode (io.prestosql.spi.plan.PlanNode)40 RowExpression (io.prestosql.spi.relation.RowExpression)30 Expression (io.prestosql.sql.tree.Expression)30 ImmutableList (com.google.common.collect.ImmutableList)29 OriginalExpressionUtils.castToRowExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression)27 ImmutableMap (com.google.common.collect.ImmutableMap)25 AggregationNode (io.prestosql.spi.plan.AggregationNode)25 Map (java.util.Map)24 FilterNode (io.prestosql.spi.plan.FilterNode)22 TableScanNode (io.prestosql.spi.plan.TableScanNode)21 Cast (io.prestosql.sql.tree.Cast)20 List (java.util.List)19 CallExpression (io.prestosql.spi.relation.CallExpression)18 HashMap (java.util.HashMap)18 Type (io.prestosql.spi.type.Type)17 Test (org.testng.annotations.Test)17 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)16