Search in sources :

Example 1 with PlanOptimizer

use of io.prestosql.sql.planner.optimizations.PlanOptimizer in project hetu-core by openlookeng.

the class BasePlanTest method assertMinimallyOptimizedPlan.

protected void assertMinimallyOptimizedPlan(@Language("SQL") String sql, PlanMatchPattern pattern) {
    List<PlanOptimizer> optimizers = ImmutableList.of(new UnaliasSymbolReferences(queryRunner.getMetadata()), new PruneUnreferencedOutputs(), new IterativeOptimizer(new RuleStatsRecorder(), queryRunner.getStatsCalculator(), queryRunner.getCostCalculator(), ImmutableSet.of(new RemoveRedundantIdentityProjections())));
    assertPlan(sql, LogicalPlanner.Stage.OPTIMIZED, pattern, optimizers);
}
Also used : PlanOptimizer(io.prestosql.sql.planner.optimizations.PlanOptimizer) RuleStatsRecorder(io.prestosql.sql.planner.RuleStatsRecorder) RemoveRedundantIdentityProjections(io.prestosql.sql.planner.iterative.rule.RemoveRedundantIdentityProjections) PruneUnreferencedOutputs(io.prestosql.sql.planner.optimizations.PruneUnreferencedOutputs) IterativeOptimizer(io.prestosql.sql.planner.iterative.IterativeOptimizer) UnaliasSymbolReferences(io.prestosql.sql.planner.optimizations.UnaliasSymbolReferences)

Example 2 with PlanOptimizer

use of io.prestosql.sql.planner.optimizations.PlanOptimizer in project hetu-core by openlookeng.

the class HetuLogicalPlanner method plan.

@Override
public Plan plan(Analysis analysis, boolean skipStatsWithPlan, Stage stage) {
    PlanNode root = planStatement(analysis, analysis.getStatement());
    PlanNode.SkipOptRuleLevel optimizationLevel = APPLY_ALL_RULES;
    planSanityChecker.validateIntermediatePlan(root, session, metadata, typeAnalyzer, planSymbolAllocator.getTypes(), warningCollector);
    if (stage.ordinal() >= Stage.OPTIMIZED.ordinal()) {
        for (PlanOptimizer optimizer : planOptimizers) {
            if (OptimizerUtils.isEnabledLegacy(optimizer, session, root)) {
                // We run it separately in the SqlQueryExecution
                if (optimizer instanceof BeginTableWrite) {
                    continue;
                }
                if (OptimizerUtils.canApplyOptimizer(optimizer, optimizationLevel)) {
                    root = optimizer.optimize(root, session, planSymbolAllocator.getTypes(), planSymbolAllocator, idAllocator, warningCollector);
                    requireNonNull(root, format("%s returned a null plan", optimizer.getClass().getName()));
                    optimizationLevel = optimizationLevel == APPLY_ALL_RULES ? root.getSkipOptRuleLevel() : optimizationLevel;
                }
            }
        }
    }
    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.validateFinalPlan(root, session, metadata, typeAnalyzer, planSymbolAllocator.getTypes(), warningCollector);
    }
    TypeProvider types = planSymbolAllocator.getTypes();
    // we calculate stats here only if need to show as part of EXPLAIN, otherwise not needed.
    if (skipStatsWithPlan && isSkipAttachingStatsWithPlan(session)) {
        return new Plan(root, types, StatsAndCosts.empty());
    } else {
        StatsProvider statsProvider = new CachingStatsProvider(statsCalculator, session, types);
        CostProvider costProvider = new CachingCostProvider(costCalculator, statsProvider, Optional.empty(), session, types);
        return new Plan(root, types, StatsAndCosts.create(root, statsProvider, costProvider));
    }
}
Also used : PlanOptimizer(io.prestosql.sql.planner.optimizations.PlanOptimizer) CachingStatsProvider(io.prestosql.cost.CachingStatsProvider) PlanNode(io.prestosql.spi.plan.PlanNode) BeginTableWrite(io.prestosql.sql.planner.optimizations.BeginTableWrite) CachingStatsProvider(io.prestosql.cost.CachingStatsProvider) StatsProvider(io.prestosql.cost.StatsProvider) CachingCostProvider(io.prestosql.cost.CachingCostProvider) CostProvider(io.prestosql.cost.CostProvider) CachingCostProvider(io.prestosql.cost.CachingCostProvider) TypeProvider(io.prestosql.sql.planner.TypeProvider) Plan(io.prestosql.sql.planner.Plan) SystemSessionProperties.isSkipAttachingStatsWithPlan(io.prestosql.SystemSessionProperties.isSkipAttachingStatsWithPlan)

Example 3 with PlanOptimizer

use of io.prestosql.sql.planner.optimizations.PlanOptimizer in project hetu-core by openlookeng.

the class TestIterativeOptimizer method optimizerTimeoutsOnNonConvergingPlan.

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

Example 4 with PlanOptimizer

use of io.prestosql.sql.planner.optimizations.PlanOptimizer in project hetu-core by openlookeng.

the class LogicalPlanner method plan.

public Plan plan(Analysis analysis, boolean skipStatsWithPlan, Stage stage) {
    PlanNode root = planStatement(analysis, analysis.getStatement());
    PlanNode.SkipOptRuleLevel optimizationLevel = APPLY_ALL_RULES;
    planSanityChecker.validateIntermediatePlan(root, session, metadata, typeAnalyzer, planSymbolAllocator.getTypes(), warningCollector);
    if (stage.ordinal() >= Stage.OPTIMIZED.ordinal()) {
        for (PlanOptimizer optimizer : planOptimizers) {
            if (OptimizerUtils.isEnabledLegacy(optimizer, session, root) && OptimizerUtils.canApplyOptimizer(optimizer, optimizationLevel)) {
                root = optimizer.optimize(root, session, planSymbolAllocator.getTypes(), planSymbolAllocator, idAllocator, warningCollector);
                requireNonNull(root, format("%s returned a null plan", optimizer.getClass().getName()));
                optimizationLevel = optimizationLevel == APPLY_ALL_RULES ? root.getSkipOptRuleLevel() : optimizationLevel;
            }
        }
    }
    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.validateFinalPlan(root, session, metadata, typeAnalyzer, planSymbolAllocator.getTypes(), warningCollector);
    }
    TypeProvider types = planSymbolAllocator.getTypes();
    // we calculate stats here only if need to show as part of EXPLAIN, otherwise not needed.
    if (skipStatsWithPlan && isSkipAttachingStatsWithPlan(session)) {
        return new Plan(root, types, StatsAndCosts.empty());
    } else {
        StatsProvider statsProvider = new CachingStatsProvider(statsCalculator, session, types);
        CostProvider costProvider = new CachingCostProvider(costCalculator, statsProvider, Optional.empty(), session, types);
        return new Plan(root, types, StatsAndCosts.create(root, statsProvider, costProvider));
    }
}
Also used : PlanOptimizer(io.prestosql.sql.planner.optimizations.PlanOptimizer) CachingStatsProvider(io.prestosql.cost.CachingStatsProvider) PlanNode(io.prestosql.spi.plan.PlanNode) StatsProvider(io.prestosql.cost.StatsProvider) CachingStatsProvider(io.prestosql.cost.CachingStatsProvider) CachingCostProvider(io.prestosql.cost.CachingCostProvider) CostProvider(io.prestosql.cost.CostProvider) CachingCostProvider(io.prestosql.cost.CachingCostProvider) SystemSessionProperties.isSkipAttachingStatsWithPlan(io.prestosql.SystemSessionProperties.isSkipAttachingStatsWithPlan)

Example 5 with PlanOptimizer

use of io.prestosql.sql.planner.optimizations.PlanOptimizer in project hetu-core by openlookeng.

the class IterativeOptimizer method optimize.

@Override
public PlanNode optimize(PlanNode plan, Session session, TypeProvider types, PlanSymbolAllocator planSymbolAllocator, PlanNodeIdAllocator idAllocator, WarningCollector warningCollector) {
    PlanNode tmpPlan = plan;
    // only disable new rules if we have legacy rules to fall back to
    if (!SystemSessionProperties.isNewOptimizerEnabled(session) && !legacyRules.isEmpty()) {
        for (PlanOptimizer optimizer : legacyRules) {
            if (OptimizerUtils.isEnabledLegacy(optimizer, session, tmpPlan)) {
                tmpPlan = optimizer.optimize(tmpPlan, session, planSymbolAllocator.getTypes(), planSymbolAllocator, idAllocator, warningCollector);
            }
        }
        return plan;
    }
    Memo memo = new Memo(idAllocator, plan);
    Lookup lookup = Lookup.from(planNode -> Stream.of(memo.resolve(planNode)));
    Duration timeout = SystemSessionProperties.getOptimizerTimeout(session);
    Context context = new Context(memo, lookup, idAllocator, planSymbolAllocator, System.nanoTime(), timeout.toMillis(), session, warningCollector);
    exploreGroup(memo.getRootGroup(), context);
    return memo.extract();
}
Also used : PlanOptimizer(io.prestosql.sql.planner.optimizations.PlanOptimizer) PlanNode(io.prestosql.spi.plan.PlanNode) Duration(io.airlift.units.Duration)

Aggregations

PlanOptimizer (io.prestosql.sql.planner.optimizations.PlanOptimizer)8 PlanNode (io.prestosql.spi.plan.PlanNode)4 Plan (io.prestosql.sql.planner.Plan)3 SystemSessionProperties.isSkipAttachingStatsWithPlan (io.prestosql.SystemSessionProperties.isSkipAttachingStatsWithPlan)2 CachingCostProvider (io.prestosql.cost.CachingCostProvider)2 CachingStatsProvider (io.prestosql.cost.CachingStatsProvider)2 CostCalculator (io.prestosql.cost.CostCalculator)2 CostProvider (io.prestosql.cost.CostProvider)2 StatsProvider (io.prestosql.cost.StatsProvider)2 CubeManager (io.prestosql.cube.CubeManager)2 HeuristicIndexerManager (io.prestosql.heuristicindex.HeuristicIndexerManager)2 Metadata (io.prestosql.metadata.Metadata)2 PrestoException (io.prestosql.spi.PrestoException)2 QueryExplainer (io.prestosql.sql.analyzer.QueryExplainer)2 PlanFragmenter (io.prestosql.sql.planner.PlanFragmenter)2 TypeAnalyzer (io.prestosql.sql.planner.TypeAnalyzer)2 Cache (com.google.common.cache.Cache)1 Duration (io.airlift.units.Duration)1 Session (io.prestosql.Session)1 SystemSessionProperties (io.prestosql.SystemSessionProperties)1