Search in sources :

Example 1 with PlanNodeIdAllocator

use of com.facebook.presto.sql.planner.PlanNodeIdAllocator 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 2 with PlanNodeIdAllocator

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

the class TestSimplifyExpressions method simplifyExpressions.

private static Expression simplifyExpressions(Expression expression) {
    PlanNodeIdAllocator planNodeIdAllocator = new PlanNodeIdAllocator();
    FilterNode filterNode = new FilterNode(planNodeIdAllocator.getNextId(), new ValuesNode(planNodeIdAllocator.getNextId(), emptyList(), emptyList()), expression);
    FilterNode simplifiedNode = (FilterNode) SIMPLIFIER.optimize(filterNode, TEST_SESSION, booleanSymbolTypeMapFor(expression), new SymbolAllocator(), planNodeIdAllocator);
    return simplifiedNode.getPredicate();
}
Also used : SymbolAllocator(com.facebook.presto.sql.planner.SymbolAllocator) ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) PlanNodeIdAllocator(com.facebook.presto.sql.planner.PlanNodeIdAllocator) FilterNode(com.facebook.presto.sql.planner.plan.FilterNode)

Example 3 with PlanNodeIdAllocator

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

the class QueryExplainer method getLogicalPlan.

private Plan getLogicalPlan(Session session, Statement statement, List<Expression> parameters) {
    // analyze statement
    Analysis analysis = analyze(session, statement, parameters);
    PlanNodeIdAllocator idAllocator = new PlanNodeIdAllocator();
    // plan statement
    LogicalPlanner logicalPlanner = new LogicalPlanner(session, planOptimizers, idAllocator, metadata, sqlParser);
    return logicalPlanner.plan(analysis);
}
Also used : LogicalPlanner(com.facebook.presto.sql.planner.LogicalPlanner) PlanNodeIdAllocator(com.facebook.presto.sql.planner.PlanNodeIdAllocator)

Example 4 with PlanNodeIdAllocator

use of com.facebook.presto.sql.planner.PlanNodeIdAllocator 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 5 with PlanNodeIdAllocator

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

the class LocalQueryRunner method createPlan.

public Plan createPlan(Session session, @Language("SQL") String sql, List<PlanOptimizer> optimizers, LogicalPlanner.Stage stage) {
    Statement wrapped = sqlParser.createStatement(sql);
    Statement statement = unwrapExecuteStatement(wrapped, sqlParser, session);
    List<Expression> parameters = emptyList();
    if (wrapped instanceof Execute) {
        parameters = ((Execute) wrapped).getParameters();
    }
    validateParameters(statement, parameters);
    assertFormattedSql(sqlParser, statement);
    PlanNodeIdAllocator idAllocator = new PlanNodeIdAllocator();
    QueryExplainer queryExplainer = new QueryExplainer(optimizers, metadata, accessControl, sqlParser, dataDefinitionTask);
    Analyzer analyzer = new Analyzer(session, metadata, sqlParser, accessControl, Optional.of(queryExplainer), parameters);
    LogicalPlanner logicalPlanner = new LogicalPlanner(session, optimizers, idAllocator, metadata, sqlParser);
    Analysis analysis = analyzer.analyze(statement);
    return logicalPlanner.plan(analysis, stage);
}
Also used : QueryExplainer(com.facebook.presto.sql.analyzer.QueryExplainer) LogicalPlanner(com.facebook.presto.sql.planner.LogicalPlanner) Execute(com.facebook.presto.sql.tree.Execute) Expression(com.facebook.presto.sql.tree.Expression) PlanNodeIdAllocator(com.facebook.presto.sql.planner.PlanNodeIdAllocator) SqlQueryManager.unwrapExecuteStatement(com.facebook.presto.execution.SqlQueryManager.unwrapExecuteStatement) Statement(com.facebook.presto.sql.tree.Statement) Analysis(com.facebook.presto.sql.analyzer.Analysis) Analyzer(com.facebook.presto.sql.analyzer.Analyzer)

Aggregations

PlanNodeIdAllocator (com.facebook.presto.sql.planner.PlanNodeIdAllocator)6 SymbolAllocator (com.facebook.presto.sql.planner.SymbolAllocator)4 Symbol (com.facebook.presto.sql.planner.Symbol)3 LogicalPlanner (com.facebook.presto.sql.planner.LogicalPlanner)2 AggregationNode (com.facebook.presto.sql.planner.plan.AggregationNode)2 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)2 ValuesNode (com.facebook.presto.sql.planner.plan.ValuesNode)2 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Session (com.facebook.presto.Session)1 SystemSessionProperties (com.facebook.presto.SystemSessionProperties)1 SqlQueryManager.unwrapExecuteStatement (com.facebook.presto.execution.SqlQueryManager.unwrapExecuteStatement)1 Signature (com.facebook.presto.metadata.Signature)1 PrestoException (com.facebook.presto.spi.PrestoException)1 OPTIMIZER_TIMEOUT (com.facebook.presto.spi.StandardErrorCode.OPTIMIZER_TIMEOUT)1 Type (com.facebook.presto.spi.type.Type)1