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). - Remote execution —
RemoteFragmentTransportandRemoteFragmentEndpointuse a versioned, bounded Arrow IPC pull protocol. One batch per pull provides backpressure; query-scoped cancellation and adaptive top-k refill cross the same authenticated node-internal RPC carrier.
§Integration point with DataFusion
DataFusionDistributedPlanner::lower converts a real
datafusion::logical_expr::LogicalPlan (produced by
MongrelSession after catalog/schema resolution)
onto LogicalPlanLite and hands the tree to distribute. Supported
operators: TableScan, Projection (column pushdown), Filter (pushdown onto
scans as typed predicate text for worker evaluation), Aggregate, Sort,
Limit, equi-Join, and Union. Every other DataFusion node is rejected with
DistributedError::Unsupported.
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. - Core
Fragment Table Source - Core MVCC-backed tablet source used by remote SQL workers.
- Data
Fusion Distributed Planner - Lowers a DataFusion
datafusion::logical_expr::LogicalPlanonto tablets. - 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. - Fragment
Lifecycle Metrics - Lifecycle counters for remote fragment workers (P0.4-T6).
- Fragment
Lifecycle Snapshot - Point-in-time fragment lifecycle view (P0.4-T6).
- 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 source 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.
- Loopback
Fragment RpcClient - In-process carrier for protocol and endpoint tests.
- Plan
Description - Everything
distributeneeds to build aDistributedPlan. - Plan
Fragment - One executable unit of a
DistributedPlan. - Planner
Options - Planner tuning knobs.
- Planning
Table Catalog - Arrow schemas for tables that DataFusion must resolve while planning SQL for distributed placement. Schemas are planning-only (no row data).
- Remote
Fragment Endpoint - Worker-side endpoint for the internal fragment protocol.
- Remote
Fragment Transport - Coordinator-side Arrow IPC transport for tablet fragments.
- 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. - DEFAULT_
REMOTE_ FRAGMENT_ EXECUTIONS - Default maximum number of fragment streams held by one worker.
- DEFAULT_
REMOTE_ FRAGMENT_ MESSAGE_ BYTES - Default maximum encoded request or response body for one fragment RPC.
- MAX_
FRAGMENT_ AUTHORIZATION_ CONTEXT_ BYTES - Maximum server-issued authorization envelope forwarded to a worker.
- REMOTE_
FRAGMENT_ PROTOCOL_ VERSION - Version of the private cluster fragment protocol.
- REMOTE_
FRAGMENT_ SERVICE_ ID - Stable service discriminator inside the cluster’s authenticated internal RPC multiplexer.
- 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
Authorization Resolver - Validates the forwarded server-issued authorization envelope.
- Fragment
Database Provider - Resolves the local storage owner for one hosted tablet.
- Fragment
Executor - Executes one fragment on its worker. Server/engine bindings install an
implementation behind
RemoteFragmentEndpoint;InMemoryFragmentExecutorremains the deterministic reference executor. - Fragment
RpcClient - One authenticated request/response carrier for remote fragment bodies.
- Fragment
Table Source - Batch source consumed by the reference fragment operator engine.
- Fragment
Transport - Moves fragments, refill, and cancellation between the coordinator and
workers (spec section 12.10). Implementations include the deterministic
InMemoryTransportand the bounded Arrow IPCRemoteFragmentTransport. - 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”).
- plan_
logical_ distributed - Lowers an already-parsed DataFusion logical plan onto tablets.
- plan_
logical_ distributed_ with_ id - Same as
plan_logical_distributedbut stamps a caller-chosen query id. - plan_
sql_ distributed - Public gateway entry: parse
sqlwith DataFusion againstcatalog, then lower viaDataFusionDistributedPlannerontolocator/metadata. - plan_
sql_ distributed_ with_ id - Same as
plan_sql_distributedbut stamps a caller-chosen query id. - 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.