Expand description
Distributed SQL plan groundwork (Stage 3J, spec section 12.10).
This module is the planning and coordination seam for tablet-distributed
queries. It deliberately depends on small local metadata traits
(TabletLocator, ClusterMetadata) instead of mongreldb-cluster, so
the query crate stays free of the cluster dependency; the cluster wave
adapts mongreldb_cluster::tablet routing metadata onto these traits.
§What lives here
DistributedPlan— the plan structure from spec section 12.10: pinnedQueryId+MetadataVersion,PlanFragments with tablet assignments, andExchangeDescriptoredges between them.distribute— the planner. It lowers aLogicalPlanLitetree (the simplified input IR) onto tablets: colocated joins when both sides share partitioning and layout, broadcast joins when the small side fits under a byte threshold, repartition joins otherwise. Every fragment carries estimated rows/bytes and a maximum spill allowance (spec section 12.10).- Distributed top-k —
merge_top_k/exact_top_kare pure functions implementing the deterministic coordinator merge (final score descending, tablet id ascending,RowIdascending) with adaptive refill when a tablet’s unseen-score bound shows it could still contribute winners. - Execution skeleton —
FragmentExecutor,FragmentTransport, and theCoordinatorruntime: per-fragment resource reservation, cancellation fan-out wired to the existingSqlQueryRegistry, worker lease expiry that cleans abandoned fragments, and real in-memory merge operators (k-wayMergeSort,FinalAggregatecombine, distributed top-k) used until the remote Arrow IPC transport lands.
§Integration point with DataFusion
LogicalPlanLite is intentionally minimal. The DataFusion integration wave
lowers datafusion::logical_expr::LogicalPlan (produced by
MongrelSession after catalog/schema resolution)
onto LogicalPlanLite — scans of MongrelScanExec-backed tables become
LogicalPlanLite::Scan, aggregates/joins/sorts/limits map one-to-one —
and hands the tree to distribute. Full DataFusion plan conversion is
explicitly out of scope for this wave.
Structs§
- Aggregate
Expr - One aggregate expression.
- Batch
Frame - One Arrow-ish record-batch frame on a fragment stream.
- Coordinator
- The query coordinator (spec section 12.10): registers the query with the
existing
SqlQueryRegistry(soregistry.cancel(...)reaches every fragment through theExecutionControlhierarchy), reserves resources per fragment, fans out cancellation to every fragment, sweeps expired worker leases to clean abandoned fragments, and merges producer streams per the exchange descriptors with the real in-memory operators. - Distributed
Plan - A distributed query plan pinned to one control-plane metadata version (spec section 12.10).
- Exchange
Descriptor - One exchange edge between two fragments (spec section 12.10).
- Fragment
Control - Cooperative per-fragment execution control: cancellation/deadline shared
with the query’s
ExecutionControlhierarchy plus the fragment’s spill allowance. - InMemory
Fragment Executor - Reference
FragmentExecutorover anInMemoryTableStore. Interprets scans, projections, partial aggregates, local merge sorts, bounded local top-ks (with exact tie information), and limits for real; exchange sources drain their input streams; join operators reject (their execution binding lands with the tablet wave — plan shape is fully tested). - InMemory
Table Store - In-memory per-
(table, tablet)record-batch store backingInMemoryFragmentExecutor. - InMemory
Transport - In-memory
FragmentTransport: routes fragments to per-tablet executors, records starts/cancellations/refills for test introspection, and keeps each fragment’sExecutionControlso cancellations are observable. - JoinKey
- One join-key equality pair (
left.col = right.col). - Lease
Ledger - Worker lease ledger (spec section 12.10: “worker lease expiry cleans abandoned fragments”). Workers are keyed by the tablet whose data they serve this wave; the node-level binding lands with the transport wave.
- Plan
Description - Everything
distributeneeds to build aDistributedPlan. - Plan
Fragment - One executable unit of a
DistributedPlan. - Planner
Options - Planner tuning knobs.
- Resource
Ledger - Per-query fragment resource ledger. Reservations are RAII: dropping a
ResourcePermitreleases its share. - Resource
Permit - A held resource reservation; released on drop.
- SortKey
- One sort key.
- Table
Stats - Planner statistics for one table.
- Tablet
TopK - One tablet’s bounded local top-k plus tie information (spec section 12.10: “each tablet returns a bounded local top-k plus tie information”).
- TopK
Candidate - One ranked row in the top-k model: the score plus the exact tie-break
identity (spec section 12.10: final score descending, tablet id ascending,
RowIdascending). - TopK
Merge - The outcome of one deterministic coordinator merge step.
- TopK
Refill - The next batch of a tablet’s local top-k (adaptive refill, spec section 12.10).
Enums§
- Aggregate
Function - Supported aggregate functions.
- Build
Side - Which join input is the broadcast (build) side.
- Distributed
Error - Errors raised by distributed planning and coordination.
- Exchange
Kind - How rows move across one exchange edge (spec section 12.10).
- Fragment
Assignment - Where one fragment executes.
- Fragment
Operator - One physical operator inside a fragment (spec section 12.10).
- Logical
Plan Lite - The simplified logical IR
distributelowers onto tablets. - Partition
Spec - How one table’s rows map onto tablets — the planner’s dependency-free
mirror of
mongreldb_cluster::tablet::Partitioning(spec section 12.2). The cluster wave adapts the cluster type onto this enum one-to-one. - Score
Bound - Tie information for scored (top-k) streams, carried on a producer’s terminal frame (spec section 12.10: “bounded local top-k plus tie information”).
Constants§
- DEFAULT_
BROADCAST_ THRESHOLD_ BYTES - Small-side byte threshold below which a join is planned as a broadcast join (spec section 12.10: “broadcast small side”).
- DEFAULT_
MAX_ SPILL_ BYTES_ PER_ FRAGMENT - Default per-fragment maximum spill allowance (spec section 12.10: “every
plan includes … a maximum spill allowance”). Bound to the core
mongreldb_core::SpillManagerviaFragmentControl::begin_spill. - TOPK_
ROWID_ COLUMN - Name of the unsigned row-id column every scored (top-k) stream carries. Tablet scans feeding a distributed top-k must project this column; it is stripped from the coordinator’s final top-k output.
Traits§
- Cluster
Metadata - Control-plane metadata the planner pins against (spec section 12.10: “coordinator plans using metadata version”).
- Fragment
Executor - Executes one fragment on its worker. The real binding runs tablet
fragments against a
ClusterReplicacore in a later wave; this wave shipsInMemoryFragmentExecutoras the reference implementation. - Fragment
Transport - Moves fragments and cancellation between the coordinator and workers (spec section 12.10). The Arrow IPC transport with backpressure is the remote-transport wave’s job; this wave ships the in-memory implementation.
- Tablet
Locator - Resolves which tablets serve one table. Defined here (not in
mongreldb-cluster) so the query crate stays free of the cluster dependency; the cluster wave bindsTabletLayout/routing metadata onto this trait.
Functions§
- distribute
- Lowers a
LogicalPlanLitetree onto tablets (spec section 12.10). - exact_
top_ k - Drives
merge_top_kwith adaptive refill until the result is provably exact (spec section 12.10: “for exact global top-k, use adaptive refill when local bounds show unseen rows could still win”). - merge_
top_ k - Deterministically merges bounded local top-ks (spec section 12.10: “coordinator merges deterministically”).
- topk_
cmp - The deterministic winner order: score descending, then tablet id
ascending, then
RowIdascending. ReturnsOrdering::Lesswhenaranks strictly better thanb.
Type Aliases§
- Distributed
Result - Result alias for distributed planning and coordination.
- Exchange
Id - Plan-local exchange identifier (index into
DistributedPlan::exchanges). - Fragment
Id - Plan-local fragment identifier (index into
DistributedPlan::fragments). - Fragment
Stream - A fragment’s output stream.