ddn.df.optimizer

The bounded query optimizer of ddn.df (ADR-2026-10-24-16, amended 2026-10-29): proved-safe rewrites over the plan tree - predicate pushdown, the two fusion rules (adjacent filters compose under the totality rule; a withColumn inlines into the one projection expression referencing it) and projection pruning - applied before the executor walks the tree, plus the shared-subplan prediction of the executor's per-collection memo (structurally identical deterministic subtrees evaluate once per collection). Every rewrite preserves the oracle contract bitwise: the optimized collect equals both the unoptimized collect and the eager chain in values, schema, row order, validity and error behaviour.

The eligibility contract (binding):

  • Barriers - a predicate never crosses a limit, an aggregation, a join, a window expression or an unclassified node.
  • Totality - a predicate crosses a computed projection or a withColumn value only when the crossed expressions are provably total (direct column references and literals), because a projection evaluated on fewer rows could lose the error a dropped row would have raised. Identity column references and literals are safe: they produce the same values (or the same absence) on any row subset. The same rule admits filter fusion: a total outer predicate may evaluate on all rows, not just the survivors, because it raises no error either way.
  • Shadowing - a predicate that references a projected or replaced output name moves only when that output is the identity reference of the same column (the values below are identical).
  • Sort commutation - a predicate crosses a sort freely: the stable sort of an order-preserved subset equals filtering after the sort.
  • Fusion composes adjacent filters only when the outer predicate is total and both predicates resolve BOOLEAN where they evaluate, and inlines a withColumn only into the single projection expression referencing it (never through over keys, aggregate or window holders); the substitution is structural, never a reassociation, and alias-wrapping preserves the output names.
  • Pruning removes only what no downstream node references - dead projection entries, dead withColumn nodes and unreferenced source columns - and only across provably total expressions, so no evaluation and no error disappears.
  • Float reductions and expression evaluation order never change; aggregation and join inputs prune to exactly their referenced columns; nothing below a join is pruned this tier.
  • Shared-subplan reuse is the executor's per-collection memo over structural subtree identity; the optimizer only predicts its count (the counters' sharedSubplans).

Types 2

The rewrite counters one optimization pass reports (also the explain annotations). sharedSubplans is not a rewrite but the executor's predicted work reduction: the number of subtree results the per-collection memo will serve instead of re-evaluating.

Fields
size_t pushedFilters
size_t prunedProjections
size_t droppedColumns
size_t droppedNodes
size_t fusedFilters
size_t fusedProjections
size_t sharedSubplans
size_t scanProjections
Methods
bool fired() const @safe pure nothrowWhether any rewrite rule fired.
structOptResult

The result of one optimization pass: the rewritten plan root and the rule counters.

Fields
OptCounters counters

Functions 9

fnOptResult optimizePlan(PlanNode root) @safeOptimizes a plan: predicate pushdown, then the two proved-safe fusion rules, then projection pruning, and finally the shared-subplan prediction of the executor's per-collection memo. Every rule pre...
private fnPlanNode pushFilters(PlanNode node, ref OptCounters counters) @safe
private fnbool canCross(Expr pred, PlanNode node) @safe
private fnPlanNode rebuildCrossed(PlanNode node, PlanNode newChild) @safe
private fnPlanNode fuse(PlanNode node, ref OptCounters counters) @safe
private fnPlanNode fuseFilters(PlanNode node, ref OptCounters counters) @safe
private fnPlanNode fuseProjection(PlanNode node, ref OptCounters counters) @safe
private fnPlanNode prune(PlanNode node, string[] needed, ref OptCounters counters) @safe
private fnstring[] mergeNames(string[] into, string[] from) @safe