Search in sources :

Example 21 with LogicalPlan

use of io.crate.planner.operators.LogicalPlan in project crate by crate.

the class Optimizer method tryApplyRules.

private LogicalPlan tryApplyRules(List<Rule<?>> rules, LogicalPlan plan, TableStats tableStats, TransactionContext txnCtx) {
    final boolean isTraceEnabled = LOGGER.isTraceEnabled();
    LogicalPlan node = plan;
    // Some rules may only become applicable after another rule triggered, so we keep
    // trying to re-apply the rules as long as at least one plan was transformed.
    boolean done = false;
    int numIterations = 0;
    while (!done && numIterations < 10_000) {
        done = true;
        Version minVersion = minNodeVersionInCluster.get();
        for (Rule rule : rules) {
            if (minVersion.before(rule.requiredVersion())) {
                continue;
            }
            Match<?> match = rule.pattern().accept(node, Captures.empty());
            if (match.isPresent()) {
                if (isTraceEnabled) {
                    LOGGER.trace("Rule '" + rule.getClass().getSimpleName() + "' matched");
                }
                @SuppressWarnings("unchecked") LogicalPlan transformedPlan = rule.apply(match.value(), match.captures(), tableStats, txnCtx, nodeCtx);
                if (transformedPlan != null) {
                    if (isTraceEnabled) {
                        LOGGER.trace("Rule '" + rule.getClass().getSimpleName() + "' transformed the logical plan");
                    }
                    node = transformedPlan;
                    done = false;
                }
            }
        }
        numIterations++;
    }
    assert numIterations < 10_000 : "Optimizer reached 10_000 iterations safety guard. This is an indication of a broken rule that matches again and again";
    return node;
}
Also used : Version(org.elasticsearch.Version) LogicalPlan(io.crate.planner.operators.LogicalPlan)

Example 22 with LogicalPlan

use of io.crate.planner.operators.LogicalPlan in project crate by crate.

the class Optimizer method optimize.

public LogicalPlan optimize(LogicalPlan plan, TableStats tableStats, TransactionContext txnCtx) {
    var applicableRules = removeExcludedRules(rules, txnCtx.sessionSettings().excludedOptimizerRules());
    LogicalPlan optimizedRoot = tryApplyRules(applicableRules, plan, tableStats, txnCtx);
    var optimizedSources = Lists2.mapIfChange(optimizedRoot.sources(), x -> optimize(x, tableStats, txnCtx));
    return tryApplyRules(applicableRules, optimizedSources == optimizedRoot.sources() ? optimizedRoot : optimizedRoot.replaceSources(optimizedSources), tableStats, txnCtx);
}
Also used : LogicalPlan(io.crate.planner.operators.LogicalPlan)

Example 23 with LogicalPlan

use of io.crate.planner.operators.LogicalPlan in project crate by crate.

the class PlannerTest method test_invalid_any_param_leads_to_clear_error_message.

@Test
public void test_invalid_any_param_leads_to_clear_error_message() throws Exception {
    LogicalPlan plan = e.logicalPlan("select name = ANY(?) from sys.cluster");
    Asserts.assertThrowsMatches(() -> {
        LogicalPlanner.getNodeOperationTree(plan, mock(DependencyCarrier.class), e.getPlannerContext(clusterService.state()), new Row1("foo"), SubQueryResults.EMPTY);
    }, ConversionException.class, "Cannot cast value `foo` to type `text_array`");
}
Also used : Row1(io.crate.data.Row1) LogicalPlan(io.crate.planner.operators.LogicalPlan) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 24 with LogicalPlan

use of io.crate.planner.operators.LogicalPlan in project crate by crate.

the class SingleRowSubselectPlannerTest method testSingleRowSubSelectInSelectList.

@Test
public void testSingleRowSubSelectInSelectList() throws Exception {
    LogicalPlan plan = e.logicalPlan("select (select b from t2 limit 1) from t1");
    assertThat(plan.dependencies().keySet(), contains(instanceOf(RootRelationBoundary.class)));
}
Also used : LogicalPlan(io.crate.planner.operators.LogicalPlan) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 25 with LogicalPlan

use of io.crate.planner.operators.LogicalPlan in project crate by crate.

the class UpdatePlannerTest method testMultiValueSubQueryWithinSingleValueSubQueryDoesNotInheritSoftLimit.

@Test
public void testMultiValueSubQueryWithinSingleValueSubQueryDoesNotInheritSoftLimit() {
    MultiPhasePlan plan = e.plan("update users set ints = (" + "   select count(id) from users where id in (select unnest([1, 2, 3, 4])))");
    assertThat(plan.rootPlan, instanceOf(UpdatePlanner.Update.class));
    Map<LogicalPlan, SelectSymbol> rootPlanDependencies = plan.dependencies;
    LogicalPlan outerSubSelectPlan = rootPlanDependencies.keySet().iterator().next();
    SelectSymbol outerSubSelectSymbol = rootPlanDependencies.values().iterator().next();
    assertThat(outerSubSelectSymbol.getResultType(), is(SINGLE_COLUMN_SINGLE_VALUE));
    assertThat(outerSubSelectPlan.numExpectedRows(), is(2L));
    LogicalPlan innerSubSelectPlan = outerSubSelectPlan.dependencies().keySet().iterator().next();
    SelectSymbol innerSubSelectSymbol = outerSubSelectPlan.dependencies().values().iterator().next();
    assertThat(innerSubSelectSymbol.getResultType(), is(SINGLE_COLUMN_MULTIPLE_VALUES));
    assertThat(innerSubSelectPlan.numExpectedRows(), is(-1L));
}
Also used : SelectSymbol(io.crate.expression.symbol.SelectSymbol) LogicalPlan(io.crate.planner.operators.LogicalPlan) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Aggregations

LogicalPlan (io.crate.planner.operators.LogicalPlan)28 Symbol (io.crate.expression.symbol.Symbol)12 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)11 Test (org.junit.Test)11 Filter (io.crate.planner.operators.Filter)9 TableStats (io.crate.statistics.TableStats)7 WindowFunction (io.crate.expression.symbol.WindowFunction)6 WindowAgg (io.crate.planner.operators.WindowAgg)6 ArrayList (java.util.ArrayList)5 SelectSymbol (io.crate.expression.symbol.SelectSymbol)4 List (java.util.List)4 Reference (io.crate.metadata.Reference)3 ExecutionPlan (io.crate.planner.ExecutionPlan)3 DocTableRelation (io.crate.analyze.relations.DocTableRelation)2 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)2 Row (io.crate.data.Row)2 Row1 (io.crate.data.Row1)2 RowConsumer (io.crate.data.RowConsumer)2 UnsupportedFeatureException (io.crate.exceptions.UnsupportedFeatureException)2 NodeOperationTree (io.crate.execution.dsl.phases.NodeOperationTree)2