Search in sources :

Example 1 with PlanOptimizer

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

the class TestIterativeOptimizer method optimizerTimeoutsOnNonConvergingPlan.

@Test(timeOut = 1000)
public void optimizerTimeoutsOnNonConvergingPlan() {
    PlanOptimizer optimizer = new IterativeOptimizer(new StatsRecorder(), ImmutableSet.of(new NonConvergingRule()));
    try {
        queryRunner.inTransaction(transactionSession -> {
            queryRunner.createPlan(transactionSession, "SELECT * FROM nation", ImmutableList.of(optimizer));
            fail("The optimizer should not converge");
            return null;
        });
    } catch (PrestoException ex) {
        assertEquals(ex.getErrorCode(), OPTIMIZER_TIMEOUT.toErrorCode());
    }
}
Also used : StatsRecorder(com.facebook.presto.sql.planner.StatsRecorder) PlanOptimizer(com.facebook.presto.sql.planner.optimizations.PlanOptimizer) PrestoException(com.facebook.presto.spi.PrestoException) Test(org.testng.annotations.Test)

Example 2 with PlanOptimizer

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

the class LogicalPlanner method plan.

public Plan plan(Analysis analysis, Stage stage) {
    PlanNode root = planStatement(analysis, analysis.getStatement());
    if (stage.ordinal() >= Stage.OPTIMIZED.ordinal()) {
        for (PlanOptimizer optimizer : planOptimizers) {
            root = optimizer.optimize(root, session, symbolAllocator.getTypes(), symbolAllocator, idAllocator);
            requireNonNull(root, format("%s returned a null plan", optimizer.getClass().getName()));
        }
    }
    if (stage.ordinal() >= Stage.OPTIMIZED_AND_VALIDATED.ordinal()) {
        // make sure we produce a valid plan after optimizations run. This is mainly to catch programming errors
        PlanSanityChecker.validate(root, session, metadata, sqlParser, symbolAllocator.getTypes());
    }
    return new Plan(root, symbolAllocator);
}
Also used : PlanOptimizer(com.facebook.presto.sql.planner.optimizations.PlanOptimizer) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode)

Example 3 with PlanOptimizer

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

the class IterativeOptimizer method optimize.

@Override
public PlanNode optimize(PlanNode plan, Session session, Map<Symbol, Type> types, SymbolAllocator symbolAllocator, PlanNodeIdAllocator idAllocator) {
    if (!SystemSessionProperties.isNewOptimizerEnabled(session)) {
        for (PlanOptimizer optimizer : legacyRules) {
            plan = optimizer.optimize(plan, session, symbolAllocator.getTypes(), symbolAllocator, idAllocator);
        }
        return plan;
    }
    Memo memo = new Memo(idAllocator, plan);
    Lookup lookup = node -> {
        if (node instanceof GroupReference) {
            return memo.getNode(((GroupReference) node).getGroupId());
        }
        return node;
    };
    Duration timeout = SystemSessionProperties.getOptimizerTimeout(session);
    exploreGroup(memo.getRootGroup(), new Context(memo, lookup, idAllocator, symbolAllocator, System.nanoTime(), timeout.toMillis()));
    return memo.extract();
}
Also used : StatsRecorder(com.facebook.presto.sql.planner.StatsRecorder) PlanNodeIdAllocator(com.facebook.presto.sql.planner.PlanNodeIdAllocator) PlanOptimizer(com.facebook.presto.sql.planner.optimizations.PlanOptimizer) ImmutableSet(com.google.common.collect.ImmutableSet) Session(com.facebook.presto.Session) Set(java.util.Set) PrestoException(com.facebook.presto.spi.PrestoException) String.format(java.lang.String.format) Duration(io.airlift.units.Duration) Preconditions.checkState(com.google.common.base.Preconditions.checkState) OPTIMIZER_TIMEOUT(com.facebook.presto.spi.StandardErrorCode.OPTIMIZER_TIMEOUT) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) Symbol(com.facebook.presto.sql.planner.Symbol) Type(com.facebook.presto.spi.type.Type) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Map(java.util.Map) Optional(java.util.Optional) SymbolAllocator(com.facebook.presto.sql.planner.SymbolAllocator) SystemSessionProperties(com.facebook.presto.SystemSessionProperties) PlanOptimizer(com.facebook.presto.sql.planner.optimizations.PlanOptimizer) Duration(io.airlift.units.Duration)

Example 4 with PlanOptimizer

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

the class BasePlanTest method assertMinimallyOptimizedPlan.

protected void assertMinimallyOptimizedPlan(@Language("SQL") String sql, PlanMatchPattern pattern) {
    LocalQueryRunner queryRunner = getQueryRunner();
    List<PlanOptimizer> optimizers = ImmutableList.of(new UnaliasSymbolReferences(), new PruneUnreferencedOutputs(), new PruneIdentityProjections());
    queryRunner.inTransaction(transactionSession -> {
        Plan actualPlan = queryRunner.createPlan(transactionSession, sql, optimizers, LogicalPlanner.Stage.OPTIMIZED);
        PlanAssert.assertPlan(transactionSession, queryRunner.getMetadata(), actualPlan, pattern);
        return null;
    });
}
Also used : PlanOptimizer(com.facebook.presto.sql.planner.optimizations.PlanOptimizer) PruneUnreferencedOutputs(com.facebook.presto.sql.planner.optimizations.PruneUnreferencedOutputs) PruneIdentityProjections(com.facebook.presto.sql.planner.optimizations.PruneIdentityProjections) Plan(com.facebook.presto.sql.planner.Plan) LocalQueryRunner(com.facebook.presto.testing.LocalQueryRunner) UnaliasSymbolReferences(com.facebook.presto.sql.planner.optimizations.UnaliasSymbolReferences)

Example 5 with PlanOptimizer

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

the class AbstractTestQueryFramework method getQueryExplainer.

private QueryExplainer getQueryExplainer() {
    Metadata metadata = queryRunner.getMetadata();
    FeaturesConfig featuresConfig = new FeaturesConfig().setOptimizeHashGeneration(true);
    boolean forceSingleNode = queryRunner.getNodeCount() == 1;
    List<PlanOptimizer> optimizers = new PlanOptimizers(metadata, sqlParser, featuresConfig, forceSingleNode, new MBeanExporter(new TestingMBeanServer())).get();
    return new QueryExplainer(optimizers, metadata, queryRunner.getAccessControl(), sqlParser, ImmutableMap.of());
}
Also used : PlanOptimizer(com.facebook.presto.sql.planner.optimizations.PlanOptimizer) TestingMBeanServer(org.weakref.jmx.testing.TestingMBeanServer) QueryExplainer(com.facebook.presto.sql.analyzer.QueryExplainer) FeaturesConfig(com.facebook.presto.sql.analyzer.FeaturesConfig) MBeanExporter(org.weakref.jmx.MBeanExporter) Metadata(com.facebook.presto.metadata.Metadata) PlanOptimizers(com.facebook.presto.sql.planner.PlanOptimizers)

Aggregations

PlanOptimizer (com.facebook.presto.sql.planner.optimizations.PlanOptimizer)5 PrestoException (com.facebook.presto.spi.PrestoException)2 StatsRecorder (com.facebook.presto.sql.planner.StatsRecorder)2 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)2 Session (com.facebook.presto.Session)1 SystemSessionProperties (com.facebook.presto.SystemSessionProperties)1 Metadata (com.facebook.presto.metadata.Metadata)1 OPTIMIZER_TIMEOUT (com.facebook.presto.spi.StandardErrorCode.OPTIMIZER_TIMEOUT)1 Type (com.facebook.presto.spi.type.Type)1 FeaturesConfig (com.facebook.presto.sql.analyzer.FeaturesConfig)1 QueryExplainer (com.facebook.presto.sql.analyzer.QueryExplainer)1 Plan (com.facebook.presto.sql.planner.Plan)1 PlanNodeIdAllocator (com.facebook.presto.sql.planner.PlanNodeIdAllocator)1 PlanOptimizers (com.facebook.presto.sql.planner.PlanOptimizers)1 Symbol (com.facebook.presto.sql.planner.Symbol)1 SymbolAllocator (com.facebook.presto.sql.planner.SymbolAllocator)1 PruneIdentityProjections (com.facebook.presto.sql.planner.optimizations.PruneIdentityProjections)1 PruneUnreferencedOutputs (com.facebook.presto.sql.planner.optimizations.PruneUnreferencedOutputs)1 UnaliasSymbolReferences (com.facebook.presto.sql.planner.optimizations.UnaliasSymbolReferences)1 LocalQueryRunner (com.facebook.presto.testing.LocalQueryRunner)1