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;
}
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);
}
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`");
}
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)));
}
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));
}
Aggregations