For contributors — how XLOG’s optimizer works internally. This page is dense on
purpose; it explains the planning stack for people who work on XLOG’s internals,
not for people writing queries.
- lowering-time atom ordering and RIR (compiled plan tree) construction;
- generic predicate pushdown;
- statistics-backed rewrites for recognized triangle and 4-cycle shapes;
- multiway promotion into WCOJ / Free Join candidates;
- runtime dispatch gates and counters.
Lowering-Time Planning
xlog-logic::lower::Lowerer converts frontend rules into RIR (the compiled plan
tree). It owns the first join-tree shape for ordinary positive atoms. When it
builds scan and join nodes, it uses greedy ordering and planning helpers.
This layer does three more things. Predicate names become relation IDs. Schemas
are inferred. And the stratum order from the dependency analyzer — the required
run order for recursive rule groups — becomes executable SCC order.
Predicate Pushdown
Predicate pushdown moves a filter as close to the data scan as possible, so rows are dropped early.xlog_logic::optimizer::Optimizer currently applies this as
its one generic transformation. A filter is moved closer to a scan when the
predicate can be evaluated on one side of a join, or safely through a projection.
The optimizer does carry a cost type, a default transfer multiplier, and a
dp_threshold configuration field. But broad dynamic-programming join ordering
is not the active generic planner. XLOG’s generic planner is predicate
pushdown, not a universal SQL-style join optimizer.
Selectivity Rewrites
A selectivity rewrite reorders the joins inside one recognized query shape when statistics say a cheaper order exists. Selectivity is how sharply a filter or join cuts the row count. Theselectivity_pass is shape-specific. It recognizes canonical lowered
triangle and 4-cycle bodies. For those, it estimates candidate inner pairings
with StatsManager::estimate_join_cardinality. It rewrites only when statistics
make a valid lower-cost pairing available.
Safety floors keep the pass conservative:
- unrecognized shapes are left unchanged;
- missing or zero cardinality entries leave the body unchanged;
- ties keep the existing order;
- recursive SCC bodies (mutually recursive rule groups) stay on the safe default order.
Multiway Promotion
Multiway promotion marks a join body as a candidate for a multiway route rather than a chain of binary joins.promote_multiway identifies eligible bodies and
emits RirNode::MultiWayJoin for runtime dispatch. It coordinates with
statistics, variable-order settings, and shape rules. This lets the runtime later
choose between:
- dedicated WCOJ (worst-case-optimal join) kernels;
- Free Join routes;
- ordinary binary-join fallback.
Runtime Planning
The executor adds runtime information the compiler cannot know:- actual relation buffers and row counts;
- relation generations and cache state;
- available device budget;
- CUDA provider capabilities;
- kill switches (runtime flags that force a route off);
- route-specific counters and error-decline counts.
wcoj_cost_model also plans Free Join order. It applies a
factorized-loss veto: a route can decline itself when the known workload would
lose the intended benefit.
Statistics Layer
xlog-stats::StatsManager stores:
- relation cardinality and byte-size estimates;
- column statistics where available;
- join selectivity observations;
- heat used by adaptive indexing decisions.
Reference: Naming
These are the precise names for each optimizer stage, so counters, logs, and code line up:- “predicate pushdown” — the generic optimizer transformation;
- “shape-specific selectivity rewrite” — triangle and 4-cycle pairing;
- “multiway promotion” — RIR conversion into dispatch candidates;
- “runtime dispatch” — actual WCOJ, Free Join, nested-loop, or hash-join execution.
dp_threshold field exists, but
dynamic-programming join ordering is not implemented as the active planner.