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