Search in sources :

Example 21 with Symbol

use of com.facebook.presto.sql.planner.Symbol in project presto by prestodb.

the class PruneValuesColumns method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof ProjectNode)) {
        return Optional.empty();
    }
    ProjectNode parent = (ProjectNode) node;
    PlanNode child = lookup.resolve(parent.getSource());
    if (!(child instanceof ValuesNode)) {
        return Optional.empty();
    }
    ValuesNode values = (ValuesNode) child;
    Optional<List<Symbol>> dependencies = pruneInputs(child.getOutputSymbols(), parent.getAssignments().getExpressions());
    if (!dependencies.isPresent()) {
        return Optional.empty();
    }
    List<Symbol> newOutputs = dependencies.get();
    // for each output of project, the corresponding column in the values node
    int[] mapping = new int[newOutputs.size()];
    for (int i = 0; i < mapping.length; i++) {
        mapping[i] = values.getOutputSymbols().indexOf(newOutputs.get(i));
    }
    ImmutableList.Builder<List<Expression>> rowsBuilder = ImmutableList.builder();
    for (List<Expression> row : values.getRows()) {
        rowsBuilder.add(Arrays.stream(mapping).mapToObj(row::get).collect(Collectors.toList()));
    }
    return Optional.of(new ProjectNode(parent.getId(), new ValuesNode(values.getId(), newOutputs, rowsBuilder.build()), parent.getAssignments()));
}
Also used : ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) Symbol(com.facebook.presto.sql.planner.Symbol) ImmutableList(com.google.common.collect.ImmutableList) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Expression(com.facebook.presto.sql.tree.Expression) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 22 with Symbol

use of com.facebook.presto.sql.planner.Symbol in project presto by prestodb.

the class ProjectNode method isIdentity.

public boolean isIdentity() {
    for (Map.Entry<Symbol, Expression> entry : assignments.entrySet()) {
        Expression expression = entry.getValue();
        Symbol symbol = entry.getKey();
        if (!(expression instanceof SymbolReference && ((SymbolReference) expression).getName().equals(symbol.getName()))) {
            return false;
        }
    }
    return true;
}
Also used : Expression(com.facebook.presto.sql.tree.Expression) Symbol(com.facebook.presto.sql.planner.Symbol) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) Map(java.util.Map)

Example 23 with Symbol

use of com.facebook.presto.sql.planner.Symbol in project presto by prestodb.

the class StreamPreferredProperties method translateSymbols.

private static Optional<List<Symbol>> translateSymbols(Iterable<Symbol> partitioning, Function<Symbol, Optional<Symbol>> translator) {
    ImmutableList.Builder<Symbol> newPartitioningColumns = ImmutableList.builder();
    for (Symbol partitioningColumn : partitioning) {
        Optional<Symbol> translated = translator.apply(partitioningColumn);
        if (!translated.isPresent()) {
            return Optional.empty();
        }
        newPartitioningColumns.add(translated.get());
    }
    return Optional.of(newPartitioningColumns.build());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Symbol(com.facebook.presto.sql.planner.Symbol)

Example 24 with Symbol

use of com.facebook.presto.sql.planner.Symbol in project presto by prestodb.

the class SimplifyCountOverConstant method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof AggregationNode)) {
        return Optional.empty();
    }
    AggregationNode parent = (AggregationNode) node;
    PlanNode input = lookup.resolve(parent.getSource());
    if (!(input instanceof ProjectNode)) {
        return Optional.empty();
    }
    ProjectNode child = (ProjectNode) input;
    boolean changed = false;
    Map<Symbol, AggregationNode.Aggregation> assignments = new LinkedHashMap<>(parent.getAssignments());
    for (Entry<Symbol, AggregationNode.Aggregation> entry : parent.getAssignments().entrySet()) {
        Symbol symbol = entry.getKey();
        AggregationNode.Aggregation aggregation = entry.getValue();
        if (isCountOverConstant(aggregation, child.getAssignments())) {
            changed = true;
            assignments.put(symbol, new AggregationNode.Aggregation(new FunctionCall(QualifiedName.of("count"), ImmutableList.of()), new Signature("count", AGGREGATE, parseTypeSignature(StandardTypes.BIGINT))));
        }
    }
    if (!changed) {
        return Optional.empty();
    }
    return Optional.of(new AggregationNode(node.getId(), child, assignments, parent.getGroupingSets(), parent.getStep(), parent.getHashSymbol(), parent.getGroupIdSymbol()));
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Symbol(com.facebook.presto.sql.planner.Symbol) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) LinkedHashMap(java.util.LinkedHashMap)

Example 25 with Symbol

use of com.facebook.presto.sql.planner.Symbol in project presto by prestodb.

the class AggregationMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    AggregationNode aggregationNode = (AggregationNode) node;
    if (groupId.isPresent() != aggregationNode.getGroupIdSymbol().isPresent()) {
        return NO_MATCH;
    }
    if (groupingSets.size() != aggregationNode.getGroupingSets().size()) {
        return NO_MATCH;
    }
    List<Symbol> aggregationsWithMask = aggregationNode.getAggregations().entrySet().stream().filter(entry -> entry.getValue().isDistinct()).map(entry -> entry.getKey()).collect(Collectors.toList());
    if (aggregationsWithMask.size() != masks.keySet().size()) {
        return NO_MATCH;
    }
    for (Symbol symbol : aggregationsWithMask) {
        if (!masks.keySet().contains(symbol)) {
            return NO_MATCH;
        }
    }
    for (int i = 0; i < groupingSets.size(); i++) {
        if (!matches(groupingSets.get(i), aggregationNode.getGroupingSets().get(i), symbolAliases)) {
            return NO_MATCH;
        }
    }
    return match();
}
Also used : Session(com.facebook.presto.Session) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) Symbol(com.facebook.presto.sql.planner.Symbol) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Map(java.util.Map) MatchResult.match(com.facebook.presto.sql.planner.assertions.MatchResult.match) Optional(java.util.Optional) NO_MATCH(com.facebook.presto.sql.planner.assertions.MatchResult.NO_MATCH) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) Metadata(com.facebook.presto.metadata.Metadata) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) Symbol(com.facebook.presto.sql.planner.Symbol) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode)

Aggregations

Symbol (com.facebook.presto.sql.planner.Symbol)38 List (java.util.List)13 Map (java.util.Map)13 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)12 Optional (java.util.Optional)12 Session (com.facebook.presto.Session)9 ImmutableList (com.google.common.collect.ImmutableList)9 Metadata (com.facebook.presto.metadata.Metadata)8 Expression (com.facebook.presto.sql.tree.Expression)8 Preconditions.checkState (com.google.common.base.Preconditions.checkState)8 AggregationNode (com.facebook.presto.sql.planner.plan.AggregationNode)7 PlanNode (com.facebook.presto.spi.plan.PlanNode)6 NO_MATCH (com.facebook.presto.sql.planner.assertions.MatchResult.NO_MATCH)6 MatchResult.match (com.facebook.presto.sql.planner.assertions.MatchResult.match)6 ProjectNode (com.facebook.presto.sql.planner.plan.ProjectNode)6 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)6 StatsProvider (com.facebook.presto.cost.StatsProvider)5 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)5 WindowNode (com.facebook.presto.sql.planner.plan.WindowNode)5 MoreObjects.toStringHelper (com.google.common.base.MoreObjects.toStringHelper)5