Search in sources :

Example 6 with Symbol

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

the class SingleMarkDistinctToGroupBy 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 source = lookup.resolve(parent.getSource());
    if (!(source instanceof MarkDistinctNode)) {
        return Optional.empty();
    }
    MarkDistinctNode child = (MarkDistinctNode) source;
    boolean hasFilters = parent.getAggregations().values().stream().map(FunctionCall::getFilter).anyMatch(Optional::isPresent);
    if (hasFilters) {
        return Optional.empty();
    }
    // optimize if and only if
    // all aggregation functions have a single common distinct mask symbol
    // AND all aggregation functions have mask
    Set<Symbol> masks = ImmutableSet.copyOf(parent.getMasks().values());
    if (masks.size() != 1 || parent.getMasks().size() != parent.getAggregations().size()) {
        return Optional.empty();
    }
    Symbol mask = Iterables.getOnlyElement(masks);
    if (!child.getMarkerSymbol().equals(mask)) {
        return Optional.empty();
    }
    return Optional.of(new AggregationNode(idAllocator.getNextId(), new AggregationNode(idAllocator.getNextId(), child.getSource(), Collections.emptyMap(), ImmutableList.of(child.getDistinctSymbols()), SINGLE, child.getHashSymbol(), Optional.empty()), // remove DISTINCT flag from function calls
    parent.getAssignments().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> removeDistinct(e.getValue()))), parent.getGroupingSets(), parent.getStep(), parent.getHashSymbol(), parent.getGroupIdSymbol()));
}
Also used : PlanNodeIdAllocator(com.facebook.presto.sql.planner.PlanNodeIdAllocator) Iterables(com.google.common.collect.Iterables) ImmutableSet(com.google.common.collect.ImmutableSet) Rule(com.facebook.presto.sql.planner.iterative.Rule) Set(java.util.Set) Collectors(java.util.stream.Collectors) Lookup(com.facebook.presto.sql.planner.iterative.Lookup) SINGLE(com.facebook.presto.sql.planner.plan.AggregationNode.Step.SINGLE) MarkDistinctNode(com.facebook.presto.sql.planner.plan.MarkDistinctNode) ImmutableList(com.google.common.collect.ImmutableList) Symbol(com.facebook.presto.sql.planner.Symbol) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Map(java.util.Map) Optional(java.util.Optional) SymbolAllocator(com.facebook.presto.sql.planner.SymbolAllocator) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) Collections(java.util.Collections) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) MarkDistinctNode(com.facebook.presto.sql.planner.plan.MarkDistinctNode) Optional(java.util.Optional) Symbol(com.facebook.presto.sql.planner.Symbol) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) Map(java.util.Map)

Example 7 with Symbol

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

the class TransformExistsApplyToScalarApply method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof ApplyNode)) {
        return Optional.empty();
    }
    ApplyNode parent = (ApplyNode) node;
    if (parent.getSubqueryAssignments().size() != 1) {
        return Optional.empty();
    }
    Expression expression = getOnlyElement(parent.getSubqueryAssignments().getExpressions());
    if (!(expression instanceof ExistsPredicate)) {
        return Optional.empty();
    }
    Symbol count = symbolAllocator.newSymbol(COUNT.toString(), BIGINT);
    Symbol exists = getOnlyElement(parent.getSubqueryAssignments().getSymbols());
    return Optional.of(new ApplyNode(node.getId(), parent.getInput(), new ProjectNode(idAllocator.getNextId(), new AggregationNode(idAllocator.getNextId(), new LimitNode(idAllocator.getNextId(), parent.getSubquery(), 1, false), ImmutableMap.of(count, COUNT_CALL), ImmutableMap.of(count, countSignature), ImmutableMap.of(), ImmutableList.of(ImmutableList.of()), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty()), Assignments.of(exists, new ComparisonExpression(GREATER_THAN, count.toSymbolReference(), new Cast(new LongLiteral("0"), BIGINT.toString())))), Assignments.of(exists, exists.toSymbolReference()), parent.getCorrelation()));
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) LimitNode(com.facebook.presto.sql.planner.plan.LimitNode) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) Symbol(com.facebook.presto.sql.planner.Symbol) ApplyNode(com.facebook.presto.sql.planner.plan.ApplyNode) ExistsPredicate(com.facebook.presto.sql.tree.ExistsPredicate) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode)

Example 8 with Symbol

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

the class ImplementFilteredAggregations method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof AggregationNode)) {
        return Optional.empty();
    }
    AggregationNode aggregation = (AggregationNode) node;
    boolean hasFilters = aggregation.getAggregations().entrySet().stream().anyMatch(e -> e.getValue().getFilter().isPresent() && // can't handle filtered aggregations with DISTINCT (conservatively, if they have a mask)
    !aggregation.getMasks().containsKey(e.getKey()));
    if (!hasFilters) {
        return Optional.empty();
    }
    Assignments.Builder newAssignments = Assignments.builder();
    ImmutableMap.Builder<Symbol, Symbol> masks = ImmutableMap.<Symbol, Symbol>builder().putAll(aggregation.getMasks());
    ImmutableMap.Builder<Symbol, FunctionCall> calls = ImmutableMap.builder();
    for (Map.Entry<Symbol, FunctionCall> entry : aggregation.getAggregations().entrySet()) {
        Symbol output = entry.getKey();
        // strip the filters
        FunctionCall call = entry.getValue();
        calls.put(output, new FunctionCall(call.getName(), call.getWindow(), Optional.empty(), call.isDistinct(), call.getArguments()));
        if (call.getFilter().isPresent()) {
            Expression filter = entry.getValue().getFilter().get();
            Symbol symbol = symbolAllocator.newSymbol(filter, BOOLEAN);
            newAssignments.put(symbol, filter);
            masks.put(output, symbol);
        }
    }
    // identity projection for all existing inputs
    newAssignments.putIdentities(aggregation.getSource().getOutputSymbols());
    return Optional.of(new AggregationNode(idAllocator.getNextId(), new ProjectNode(idAllocator.getNextId(), aggregation.getSource(), newAssignments.build()), calls.build(), aggregation.getFunctions(), masks.build(), aggregation.getGroupingSets(), aggregation.getStep(), aggregation.getHashSymbol(), aggregation.getGroupIdSymbol()));
}
Also used : Symbol(com.facebook.presto.sql.planner.Symbol) Assignments(com.facebook.presto.sql.planner.plan.Assignments) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) ImmutableMap(com.google.common.collect.ImmutableMap) Expression(com.facebook.presto.sql.tree.Expression) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 9 with Symbol

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

the class AggregationNode method makeAssignments.

private static Map<Symbol, Aggregation> makeAssignments(Map<Symbol, FunctionCall> aggregations, Map<Symbol, Signature> functions, Map<Symbol, Symbol> masks) {
    ImmutableMap.Builder<Symbol, Aggregation> builder = ImmutableMap.builder();
    for (Map.Entry<Symbol, FunctionCall> entry : aggregations.entrySet()) {
        Symbol output = entry.getKey();
        builder.put(output, new Aggregation(entry.getValue(), functions.get(output), Optional.ofNullable(masks.get(output))));
    }
    return builder.build();
}
Also used : Symbol(com.facebook.presto.sql.planner.Symbol) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 10 with Symbol

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

the class MockRemoteTaskFactory method createTableScanTask.

public MockRemoteTask createTableScanTask(TaskId taskId, Node newNode, List<Split> splits, PartitionedSplitCountTracker partitionedSplitCountTracker) {
    Symbol symbol = new Symbol("column");
    PlanNodeId sourceId = new PlanNodeId("sourceId");
    PlanFragment testFragment = new PlanFragment(new PlanFragmentId("test"), new TableScanNode(sourceId, new TableHandle(new ConnectorId("test"), new TestingTableHandle()), ImmutableList.of(symbol), ImmutableMap.of(symbol, new TestingColumnHandle("column")), Optional.empty(), TupleDomain.all(), null), ImmutableMap.of(symbol, VARCHAR), SOURCE_DISTRIBUTION, ImmutableList.of(sourceId), new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()), ImmutableList.of(symbol)));
    ImmutableMultimap.Builder<PlanNodeId, Split> initialSplits = ImmutableMultimap.builder();
    for (Split sourceSplit : splits) {
        initialSplits.put(sourceId, sourceSplit);
    }
    return createRemoteTask(TEST_SESSION, taskId, newNode, testFragment, initialSplits.build(), createInitialEmptyOutputBuffers(BROADCAST), partitionedSplitCountTracker, true);
}
Also used : Symbol(com.facebook.presto.sql.planner.Symbol) TestingTableHandle(com.facebook.presto.sql.planner.TestingTableHandle) PartitioningScheme(com.facebook.presto.sql.planner.PartitioningScheme) PlanFragment(com.facebook.presto.sql.planner.PlanFragment) PlanNodeId(com.facebook.presto.sql.planner.plan.PlanNodeId) TestingColumnHandle(com.facebook.presto.sql.planner.TestingColumnHandle) TableScanNode(com.facebook.presto.sql.planner.plan.TableScanNode) TestingTableHandle(com.facebook.presto.sql.planner.TestingTableHandle) TableHandle(com.facebook.presto.metadata.TableHandle) PlanFragmentId(com.facebook.presto.sql.planner.plan.PlanFragmentId) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Split(com.facebook.presto.metadata.Split) ConnectorId(com.facebook.presto.connector.ConnectorId)

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