Search in sources :

Example 1 with Symbol

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

the class AggregationFunctionMatcher method getAssignedSymbol.

@Override
public Optional<Symbol> getAssignedSymbol(PlanNode node, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    Optional<Symbol> result = Optional.empty();
    if (!(node instanceof AggregationNode)) {
        return result;
    }
    AggregationNode aggregationNode = (AggregationNode) node;
    FunctionCall expectedCall = callMaker.getExpectedValue(symbolAliases);
    for (Map.Entry<Symbol, FunctionCall> assignment : aggregationNode.getAggregations().entrySet()) {
        if (expectedCall.equals(assignment.getValue())) {
            checkState(!result.isPresent(), "Ambiguous function calls in %s", aggregationNode);
            result = Optional.of(assignment.getKey());
        }
    }
    return result;
}
Also used : Symbol(com.facebook.presto.sql.planner.Symbol) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Map(java.util.Map)

Example 2 with Symbol

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

the class GroupIdMatcher 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());
    GroupIdNode groudIdNode = (GroupIdNode) node;
    List<List<Symbol>> actualGroups = groudIdNode.getGroupingSets();
    Map<Symbol, Symbol> actualArgumentMappings = groudIdNode.getArgumentMappings();
    if (actualGroups.size() != groups.size()) {
        return NO_MATCH;
    }
    for (int i = 0; i < actualGroups.size(); i++) {
        if (!AggregationMatcher.matches(groups.get(i), actualGroups.get(i), symbolAliases)) {
            return NO_MATCH;
        }
    }
    if (!AggregationMatcher.matches(identityMappings.keySet(), actualArgumentMappings.keySet(), symbolAliases)) {
        return NO_MATCH;
    }
    return match(groupIdAlias, groudIdNode.getGroupIdSymbol().toSymbolReference());
}
Also used : GroupIdNode(com.facebook.presto.sql.planner.plan.GroupIdNode) Symbol(com.facebook.presto.sql.planner.Symbol) List(java.util.List)

Example 3 with Symbol

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

the class OutputMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    int i = 0;
    for (String alias : aliases) {
        Expression expression = symbolAliases.get(alias);
        boolean found = false;
        while (i < node.getOutputSymbols().size()) {
            Symbol outputSymbol = node.getOutputSymbols().get(i++);
            if (expression.equals(outputSymbol.toSymbolReference())) {
                found = true;
                break;
            }
        }
        if (!found) {
            return NO_MATCH;
        }
    }
    return match();
}
Also used : Expression(com.facebook.presto.sql.tree.Expression) Symbol(com.facebook.presto.sql.planner.Symbol)

Example 4 with Symbol

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

the class TestCountConstantOptimizer method testCountConstantOptimizer.

@Test
public void testCountConstantOptimizer() throws Exception {
    CountConstantOptimizer optimizer = new CountConstantOptimizer();
    PlanNodeIdAllocator planNodeIdAllocator = new PlanNodeIdAllocator();
    Symbol countAggregationSymbol = new Symbol("count");
    Signature countAggregationSignature = new Signature("count", FunctionKind.AGGREGATE, parseTypeSignature(StandardTypes.BIGINT), parseTypeSignature(StandardTypes.BIGINT));
    ImmutableMap<Symbol, FunctionCall> aggregations = ImmutableMap.of(countAggregationSymbol, new FunctionCall(QualifiedName.of("count"), ImmutableList.of(new SymbolReference("expr"))));
    ImmutableMap<Symbol, Signature> functions = ImmutableMap.of(countAggregationSymbol, countAggregationSignature);
    ValuesNode valuesNode = new ValuesNode(planNodeIdAllocator.getNextId(), ImmutableList.of(new Symbol("col")), ImmutableList.of(ImmutableList.of()));
    AggregationNode eligiblePlan = new AggregationNode(planNodeIdAllocator.getNextId(), new ProjectNode(planNodeIdAllocator.getNextId(), valuesNode, Assignments.of(new Symbol("expr"), new LongLiteral("42"))), aggregations, functions, ImmutableMap.of(), ImmutableList.of(ImmutableList.of()), AggregationNode.Step.INTERMEDIATE, Optional.empty(), Optional.empty());
    assertTrue(((AggregationNode) optimizer.optimize(eligiblePlan, TEST_SESSION, ImmutableMap.of(), new SymbolAllocator(), new PlanNodeIdAllocator())).getAggregations().get(countAggregationSymbol).getArguments().isEmpty());
    AggregationNode ineligiblePlan = new AggregationNode(planNodeIdAllocator.getNextId(), new ProjectNode(planNodeIdAllocator.getNextId(), valuesNode, Assignments.of(new Symbol("expr"), new FunctionCall(QualifiedName.of("function"), ImmutableList.of(new Identifier("x"))))), aggregations, functions, ImmutableMap.of(), ImmutableList.of(ImmutableList.of()), AggregationNode.Step.INTERMEDIATE, Optional.empty(), Optional.empty());
    assertFalse(((AggregationNode) optimizer.optimize(ineligiblePlan, TEST_SESSION, ImmutableMap.of(), new SymbolAllocator(), new PlanNodeIdAllocator())).getAggregations().get(countAggregationSymbol).getArguments().isEmpty());
}
Also used : SymbolAllocator(com.facebook.presto.sql.planner.SymbolAllocator) ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) Symbol(com.facebook.presto.sql.planner.Symbol) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) Identifier(com.facebook.presto.sql.tree.Identifier) PlanNodeIdAllocator(com.facebook.presto.sql.planner.PlanNodeIdAllocator) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 5 with Symbol

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

the class ExpressionEquivalence method areExpressionsEquivalent.

public boolean areExpressionsEquivalent(Session session, Expression leftExpression, Expression rightExpression, Map<Symbol, Type> types) {
    Map<Symbol, Integer> symbolInput = new HashMap<>();
    Map<Integer, Type> inputTypes = new HashMap<>();
    int inputId = 0;
    for (Entry<Symbol, Type> entry : types.entrySet()) {
        symbolInput.put(entry.getKey(), inputId);
        inputTypes.put(inputId, entry.getValue());
        inputId++;
    }
    RowExpression leftRowExpression = toRowExpression(session, leftExpression, symbolInput, inputTypes);
    RowExpression rightRowExpression = toRowExpression(session, rightExpression, symbolInput, inputTypes);
    RowExpression canonicalizedLeft = leftRowExpression.accept(CANONICALIZATION_VISITOR, null);
    RowExpression canonicalizedRight = rightRowExpression.accept(CANONICALIZATION_VISITOR, null);
    return canonicalizedLeft.equals(canonicalizedRight);
}
Also used : Type(com.facebook.presto.spi.type.Type) HashMap(java.util.HashMap) IdentityLinkedHashMap(com.facebook.presto.util.maps.IdentityLinkedHashMap) Symbol(com.facebook.presto.sql.planner.Symbol) RowExpression(com.facebook.presto.sql.relational.RowExpression)

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