Skip to main content

optimize_with

Function optimize_with 

Source
pub fn optimize_with(plan: &mut Plan, context: &Context) -> Result<()>
Expand description

Rewrites a bound plan into the plan that runs, skipping the passes the context turned off.

Every pass preserves the plan invariant, which is what Plan::validate checks, so this checks it once at the end rather than each pass checking itself. In a release build it does not, because a pass that breaks the invariant breaks it the same way in both builds and the debug build is where that gets found.

It also checks that the plan still returns as many columns as it did on the way in. A malformed plan is found by whatever runs next, but a rewrite that quietly changes what a query returns is the one failure that running the query afterwards would not notice, and column pruning in particular is a pass whose only way of being wrong is exactly that.

It also checks, in a debug build, that running the whole sequence a second time changes nothing. That is the property that makes a fixed sequence the right shape: a pass that keeps finding work on a plan it has already rewritten is a pass whose output depends on how many times it happened to run, and in a fixed sequence it runs once, so the plan that reaches the executor is whatever the first pass left behind. Each pass has its own test for this and the assertion is here anyway, because the pair that is not idempotent together is usually a pair that is idempotent apart.

ยงErrors

Whatever a pass reported, and then, in a debug build, if a pass left the plan malformed, narrowed what it returns or did not settle, all three of which are a bug in the pass and not in the query.