use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.
the class PlanMatchingVisitor method matchSources.
/*
* This is a little counter-intuitive. Calling matchSources calls
* source.accept, which (eventually) ends up calling into visitPlan
* recursively. Assuming the plan and pattern currently being matched
* actually match each other, eventually you hit the leaf nodes. At that
* point, visitPlan starts by returning the match result for the leaf nodes
* containing the symbol aliases needed by further up.
*
* For the non-leaf nodes, an invocation of matchSources returns a match
* result for a successful match containing the union of all of the symbol
* aliases added by the sources of the node currently being visited.
*
* Visiting that node proceeds by trying to apply the current pattern's
* detailMatches() method to the node being visited. When a match is found,
* visitPlan returns a match result containing the aliases for all of the
* current node's sources, and the aliases for the current node.
*/
private MatchResult matchSources(PlanNode node, PlanMatchingState state) {
List<PlanMatchPattern> sourcePatterns = state.getPatterns();
checkState(node.getSources().size() == sourcePatterns.size(), "Matchers count does not match count of sources");
int i = 0;
SymbolAliases.Builder allSourceAliases = SymbolAliases.builder();
for (PlanNode source : node.getSources()) {
// Match sources to patterns 1:1
MatchResult matchResult = source.accept(this, sourcePatterns.get(i++));
if (!matchResult.isMatch()) {
return NO_MATCH;
}
// Add the per-source aliases to the per-state aliases.
allSourceAliases.putAll(matchResult.getAliases());
}
return match(allSourceAliases.build());
}
use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.
the class TestMemo method testReplaceSubtree.
/*
From: X -> Y -> Z
To: X -> Y' -> Z'
*/
@Test
public void testReplaceSubtree() throws Exception {
PlanNode plan = node(node(node()));
Memo memo = new Memo(idAllocator, plan);
assertEquals(memo.getGroupCount(), 3);
// replace child of root node with subtree
PlanNode transformed = node(node());
memo.replace(getChildGroup(memo, memo.getRootGroup()), transformed, "rule");
assertEquals(memo.getGroupCount(), 3);
assertMatchesStructure(memo.extract(), node(plan.getId(), transformed));
}
use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.
the class TestTypeValidator method testInvalidWindowFunctionSignature.
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "type of symbol 'sum(_[0-9]+)?' is expected to be double, but the actual type is bigint")
public void testInvalidWindowFunctionSignature() throws Exception {
Symbol windowSymbol = symbolAllocator.newSymbol("sum", DOUBLE);
Signature signature = new Signature("sum", FunctionKind.WINDOW, ImmutableList.of(), ImmutableList.of(), // should be DOUBLE
BIGINT.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature()), false);
FunctionCall functionCall = new FunctionCall(QualifiedName.of("sum"), ImmutableList.of(columnC.toSymbolReference()));
WindowNode.Frame frame = new WindowNode.Frame(WindowFrame.Type.RANGE, FrameBound.Type.UNBOUNDED_PRECEDING, Optional.empty(), FrameBound.Type.UNBOUNDED_FOLLOWING, Optional.empty());
WindowNode.Function function = new WindowNode.Function(functionCall, signature, frame);
WindowNode.Specification specification = new WindowNode.Specification(ImmutableList.of(), ImmutableList.of(), ImmutableMap.of());
PlanNode node = new WindowNode(newId(), baseTableScan, specification, ImmutableMap.of(windowSymbol, function), Optional.empty(), ImmutableSet.of(), 0);
assertTypesValid(node);
}
use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.
the class TestTypeValidator method testInvalidAggregationFunctionCall.
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "type of symbol 'sum(_[0-9]+)?' is expected to be double, but the actual type is bigint")
public void testInvalidAggregationFunctionCall() throws Exception {
Symbol aggregationSymbol = symbolAllocator.newSymbol("sum", DOUBLE);
Map<Symbol, Signature> functions = ImmutableMap.of(aggregationSymbol, new Signature("sum", FunctionKind.AGGREGATE, ImmutableList.of(), ImmutableList.of(), DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature()), false));
// should be columnC
Map<Symbol, FunctionCall> aggregations = ImmutableMap.of(aggregationSymbol, new FunctionCall(QualifiedName.of("sum"), ImmutableList.of(columnA.toSymbolReference())));
PlanNode node = new AggregationNode(newId(), baseTableScan, aggregations, functions, ImmutableMap.of(), ImmutableList.of(ImmutableList.of(columnA, columnB)), SINGLE, Optional.empty(), Optional.empty());
assertTypesValid(node);
}
use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.
the class TestTypeValidator method testValidAggregation.
@Test
public void testValidAggregation() throws Exception {
Symbol aggregationSymbol = symbolAllocator.newSymbol("sum", DOUBLE);
Map<Symbol, Signature> functions = ImmutableMap.of(aggregationSymbol, new Signature("sum", FunctionKind.AGGREGATE, ImmutableList.of(), ImmutableList.of(), DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature()), false));
Map<Symbol, FunctionCall> aggregations = ImmutableMap.of(aggregationSymbol, new FunctionCall(QualifiedName.of("sum"), ImmutableList.of(columnC.toSymbolReference())));
PlanNode node = new AggregationNode(newId(), baseTableScan, aggregations, functions, ImmutableMap.of(), ImmutableList.of(ImmutableList.of(columnA, columnB)), SINGLE, Optional.empty(), Optional.empty());
assertTypesValid(node);
}
Aggregations