spg-engine 7.9.17

Execution engine for SPG: glues spg-sql parsing to spg-storage. Foreign keys, joins, vectors, cold tier.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// pedantic doc_markdown flags the embedded algorithm-spec block;
// allowing at the module level keeps the spec readable.
#![allow(clippy::doc_markdown)]

//! v6.2.3 — JOIN reorder planner pass.
//!
//! Runs after parse + clock rewrite + ORDER BY position
//! resolution. For SelectStatements with multiple INNER-joined
//! tables, picks an ordering that minimises the cumulative
//! nested-loop work — leveraging v6.2.0's `Statistics` for
//! per-edge selectivity estimates.
//!
//! Algorithm:
//!
//!   * Identify tables: `from.primary` + `from.joins[*]`.
//!   * Identify edges: each `INNER JOIN ... ON <expr>` adds one
//!     edge whose endpoints are the table names referenced by
//!     the ON expression (extracted by walking `ColumnName`
//!     nodes). LEFT / CROSS joins disable reorder — they have
//!     semantics-preserving order constraints we don't unpack
//!     in v6.2.3.
//!   * Enumerate orderings:
//!       - For `n ≤ 4` tables: brute force all `n!` orderings.
//!       - For `n > 4`: greedy — pick the smallest table first,
//!         then at each step pick the next table that gives the
//!         smallest expected output size given the edges already
//!         applicable.
//!   * Cost an ordering: walk left-to-right tracking the running
//!     output size. Every edge becomes applicable as soon as both
//!     its endpoint tables are in the prefix; multiplying the
//!     running size by `selectivity::equal(stats, …) / n_distinct`
//!     for that edge updates the size. Step cost = `running_size ×
//!     new_table_size`. Total cost = sum of step costs.
//!   * Pick the minimum-cost ordering and rewrite `from.primary` +
//!     `from.joins` in that order. ON predicates travel with their
//!     edges — they re-attach to whichever join introduces both
//!     endpoint tables.
//!
//! All of v6.2.3 ships pure-AST rewriting. The executor at
//! `exec_joined_select` doesn't change shape — it consumes the
//! newly-ordered FROM clause unchanged.

use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;

use spg_sql::ast::{ColumnName, Expr, FromClause, FromJoin, JoinKind, SelectStatement, TableRef};

use crate::selectivity;
use crate::statistics::Statistics;
use spg_storage::Catalog;

/// v6.2.3 — full-enumeration cap. v6.2.x can re-tune; the value
/// determines `n!` plan-space size (4! = 24, 5! = 120, 6! = 720
/// — 6 is on the verge of "noticeable" in micro-bench terms).
pub const FULL_ENUM_MAX: usize = 4;

/// v6.2.3 — entry point. Rewrites `stmt.from` (when present) so
/// the join chain is in cost-minimising order. No-op when:
///   - `stmt.from` is `None` or has no joins
///   - any join is LEFT / CROSS (semantics-sensitive)
///   - any ON predicate can't be resolved to a pair of endpoint
///     tables (the conservative fallback keeps the user's order)
/// v6.2.3 test-only — computes the chosen order WITHOUT mutating
/// the statement. Returns `None` when the pass would no-op.
pub fn choose_order_for_test(
    stmt: &SelectStatement,
    catalog: &Catalog,
    stats: &Statistics,
) -> Option<Vec<usize>> {
    let mut clone = stmt.clone();
    choose_order_inner(&mut clone, catalog, stats)
}

fn choose_order_inner(
    stmt: &mut SelectStatement,
    catalog: &Catalog,
    stats: &Statistics,
) -> Option<Vec<usize>> {
    let from = stmt.from.as_mut()?;
    if from.joins.is_empty() {
        return None;
    }
    if from
        .joins
        .iter()
        .any(|j| !matches!(j.kind, JoinKind::Inner))
    {
        return None;
    }
    let mut tables: Vec<TableRef> = Vec::with_capacity(1 + from.joins.len());
    tables.push(from.primary.clone());
    for j in &from.joins {
        tables.push(j.table.clone());
    }
    let n = tables.len();
    let mut alias_to_idx: BTreeMap<String, usize> = BTreeMap::new();
    for (i, t) in tables.iter().enumerate() {
        let key = t.alias.clone().unwrap_or_else(|| t.name.clone());
        alias_to_idx.insert(key, i);
        if t.alias.is_some() {
            alias_to_idx.entry(t.name.clone()).or_insert(i);
        }
    }
    let mut edges: Vec<Edge> = Vec::new();
    for j in &from.joins {
        let on = j.on.as_ref()?;
        for sub in split_and_conjunctions(on) {
            let mut endpoint_set: Vec<usize> = Vec::new();
            if !collect_referenced_tables(sub, &alias_to_idx, &mut endpoint_set) {
                return None;
            }
            endpoint_set.sort_unstable();
            endpoint_set.dedup();
            edges.push(Edge {
                endpoints: endpoint_set,
                predicate: sub.clone(),
                selectivity: estimate_edge_selectivity(sub, &tables, catalog, stats),
            });
        }
    }
    let mut sizes: Vec<u64> = Vec::with_capacity(n);
    for t in &tables {
        let table = catalog.get(&t.name)?;
        sizes.push(table.rows().len() as u64);
    }
    Some(if n <= FULL_ENUM_MAX {
        best_order_brute(n, &sizes, &edges)
    } else {
        best_order_greedy(n, &sizes, &edges)
    })
}

pub fn reorder_joins(stmt: &mut SelectStatement, catalog: &Catalog, stats: &Statistics) {
    let Some(from) = stmt.from.as_mut() else {
        return;
    };
    if from.joins.is_empty() {
        return;
    }
    if from
        .joins
        .iter()
        .any(|j| !matches!(j.kind, JoinKind::Inner))
    {
        return;
    }
    // v6.2.3 — reorder is gated on having statistics. PG's
    // policy: ANALYZE drives the optimizer. Without stats the
    // pass would be guessing from row counts only, which (as
    // the v6.2.3 perf gate shows) can produce the same plan as
    // source order and hide regressions in the executor. The
    // user-facing contract: "run ANALYZE to opt into JOIN
    // reorder."
    if stats.is_empty() {
        return;
    }
    // Build the table list (primary first; each join contributes
    // one table). The reorder only swaps indices; ON predicates
    // travel with their edges.
    let mut tables: Vec<TableRef> = Vec::with_capacity(1 + from.joins.len());
    tables.push(from.primary.clone());
    for j in &from.joins {
        tables.push(j.table.clone());
    }
    let n = tables.len();
    // Build (table_name → alias) and (alias → table_name) so we
    // can resolve column references to either.
    let mut alias_to_idx: BTreeMap<String, usize> = BTreeMap::new();
    for (i, t) in tables.iter().enumerate() {
        let key = t.alias.clone().unwrap_or_else(|| t.name.clone());
        alias_to_idx.insert(key, i);
        // Also register the bare name when the alias differs, so
        // an ON-expression that uses the unaliased name still
        // resolves.
        if t.alias.is_some() {
            alias_to_idx.entry(t.name.clone()).or_insert(i);
        }
    }
    // Extract edges from each join's ON predicate. Every join in
    // `from.joins` carries one ON; predicate's referenced
    // table-set determines which tables are endpoints.
    let mut edges: Vec<Edge> = Vec::new();
    for j in &from.joins {
        let Some(on) = j.on.as_ref() else {
            // INNER without ON is a CROSS in v4.x parser — bail
            // (we'd lose the user's intent).
            return;
        };
        for sub in split_and_conjunctions(on) {
            let mut endpoint_set: Vec<usize> = Vec::new();
            if !collect_referenced_tables(sub, &alias_to_idx, &mut endpoint_set) {
                return;
            }
            endpoint_set.sort_unstable();
            endpoint_set.dedup();
            edges.push(Edge {
                endpoints: endpoint_set,
                predicate: sub.clone(),
                selectivity: estimate_edge_selectivity(sub, &tables, catalog, stats),
            });
        }
    }
    // Per-table row counts from the catalog. Tables that aren't
    // in the catalog yet (which can happen for a CTE — covered by
    // a different code path — but we double-check) bail.
    let mut sizes: Vec<u64> = Vec::with_capacity(n);
    for t in &tables {
        let Some(table) = catalog.get(&t.name) else {
            return;
        };
        sizes.push(table.rows().len() as u64);
    }
    // Pick an ordering.
    let order: Vec<usize> = if n <= FULL_ENUM_MAX {
        best_order_brute(n, &sizes, &edges)
    } else {
        best_order_greedy(n, &sizes, &edges)
    };
    // No-op when the chosen order matches the input.
    if order.iter().enumerate().all(|(i, &j)| i == j) {
        return;
    }
    rewrite_from(from, &tables, &edges, &order);
}

struct Edge {
    /// Sorted unique table indices the ON predicate references.
    endpoints: Vec<usize>,
    /// The original ON expression. Re-attached to whichever join
    /// in the reordered chain introduces both endpoints.
    predicate: Expr,
    /// 0..1 — fraction of rows expected to satisfy the predicate.
    /// Cached so plan evaluation doesn't repeatedly walk the
    /// histogram.
    selectivity: f64,
}

/// v6.2.3 — split a conjunction predicate `p1 AND p2 AND …` into
/// its leaf clauses. Each leaf becomes its own [`Edge`] so the
/// optimizer can pull tight predicates earlier in the plan tree.
/// Non-AND expressions return a single-element vec.
fn split_and_conjunctions(expr: &Expr) -> Vec<&Expr> {
    use spg_sql::ast::BinOp;
    let mut out: Vec<&Expr> = Vec::new();
    let mut stack: Vec<&Expr> = alloc::vec![expr];
    while let Some(e) = stack.pop() {
        if let Expr::Binary {
            op: BinOp::And,
            lhs,
            rhs,
        } = e
        {
            stack.push(rhs);
            stack.push(lhs);
        } else {
            out.push(e);
        }
    }
    out
}

fn collect_referenced_tables(
    expr: &Expr,
    alias_to_idx: &BTreeMap<String, usize>,
    out: &mut Vec<usize>,
) -> bool {
    match expr {
        Expr::Column(ColumnName {
            qualifier: Some(q),
            ..
        }) => {
            if let Some(&i) = alias_to_idx.get(q) {
                out.push(i);
                true
            } else {
                false
            }
        }
        Expr::Column(_) => {
            // Unqualified column — can't resolve without column-set
            // knowledge. Conservative bail.
            false
        }
        Expr::Literal(_) | Expr::Placeholder(_) => true,
        Expr::Binary { lhs, rhs, .. } => {
            collect_referenced_tables(lhs, alias_to_idx, out)
                && collect_referenced_tables(rhs, alias_to_idx, out)
        }
        Expr::Unary { expr, .. } => collect_referenced_tables(expr, alias_to_idx, out),
        Expr::FunctionCall { args, .. } => args
            .iter()
            .all(|a| collect_referenced_tables(a, alias_to_idx, out)),
        Expr::Cast { expr, .. } | Expr::IsNull { expr, .. } => {
            collect_referenced_tables(expr, alias_to_idx, out)
        }
        Expr::Like {
            expr: e, pattern, ..
        } => {
            collect_referenced_tables(e, alias_to_idx, out)
                && collect_referenced_tables(pattern, alias_to_idx, out)
        }
        // Subqueries / windows / EXTRACT etc. — bail conservatively.
        _ => false,
    }
}

/// Estimate selectivity for an ON-predicate. v6.2.3 MVP recognises
/// `t1.col1 = t2.col2` and uses `1 / max(n_distinct_left,
/// n_distinct_right, 1)` as an FK-join estimate (PG's heuristic).
/// Everything else: PG's `DEFAULT_RANGE = 0.333`.
fn estimate_edge_selectivity(
    on: &Expr,
    tables: &[TableRef],
    catalog: &Catalog,
    stats: &Statistics,
) -> f64 {
    use spg_sql::ast::BinOp;
    let Expr::Binary {
        op: BinOp::Eq,
        lhs,
        rhs,
    } = on
    else {
        return selectivity::DEFAULT_RANGE;
    };
    let lhs_col = column_ref(lhs);
    let rhs_col = column_ref(rhs);
    let (Some(lhs_col), Some(rhs_col)) = (lhs_col, rhs_col) else {
        return selectivity::DEFAULT_RANGE;
    };
    let lhs_distinct = column_n_distinct(&lhs_col, tables, catalog, stats);
    let rhs_distinct = column_n_distinct(&rhs_col, tables, catalog, stats);
    let max_distinct = lhs_distinct.max(rhs_distinct).max(1);
    1.0 / max_distinct as f64
}

fn column_ref(expr: &Expr) -> Option<(Option<String>, String)> {
    if let Expr::Column(ColumnName { qualifier, name }) = expr {
        Some((qualifier.clone(), name.clone()))
    } else {
        None
    }
}

fn column_n_distinct(
    col: &(Option<String>, String),
    tables: &[TableRef],
    catalog: &Catalog,
    stats: &Statistics,
) -> u64 {
    let Some(alias) = col.0.as_ref() else {
        return 0;
    };
    let Some(table_name) = tables
        .iter()
        .find(|t| t.alias.as_deref() == Some(alias.as_str()) || t.name == *alias)
        .map(|t| t.name.clone())
    else {
        return 0;
    };
    if let Some(s) = stats.get(&table_name, &col.1) {
        return s.n_distinct.max(1);
    }
    catalog
        .get(&table_name)
        .map_or(1, |t| (t.rows().len() as u64).max(1))
}

fn best_order_brute(n: usize, sizes: &[u64], edges: &[Edge]) -> Vec<usize> {
    let mut indices: Vec<usize> = (0..n).collect();
    let mut best_cost = f64::INFINITY;
    let mut best_order = indices.clone();
    permute(&mut indices, 0, &mut |perm| {
        let c = plan_cost(perm, sizes, edges);
        if c < best_cost {
            best_cost = c;
            best_order = perm.to_vec();
        }
    });
    best_order
}

fn permute<F: FnMut(&[usize])>(arr: &mut Vec<usize>, k: usize, visit: &mut F) {
    if k >= arr.len() {
        visit(arr);
        return;
    }
    for i in k..arr.len() {
        arr.swap(i, k);
        permute(arr, k + 1, visit);
        arr.swap(i, k);
    }
}

fn best_order_greedy(n: usize, sizes: &[u64], edges: &[Edge]) -> Vec<usize> {
    // Seed: smallest table.
    let mut chosen: Vec<usize> = Vec::with_capacity(n);
    let mut remaining: Vec<usize> = (0..n).collect();
    let &first = remaining
        .iter()
        .min_by_key(|&&i| sizes[i])
        .expect("n > 0");
    chosen.push(first);
    remaining.retain(|&x| x != first);
    while !remaining.is_empty() {
        // Pick the candidate whose addition produces the smallest
        // intermediate output size. plan_cost over the current
        // prefix + candidate works.
        let mut best_cand = remaining[0];
        let mut best_cost = f64::INFINITY;
        for &cand in &remaining {
            let mut probe = chosen.clone();
            probe.push(cand);
            let c = plan_cost(&probe, sizes, edges);
            if c < best_cost {
                best_cost = c;
                best_cand = cand;
            }
        }
        chosen.push(best_cand);
        remaining.retain(|&x| x != best_cand);
    }
    chosen
}

/// Cost an ordering by simulating the cumulative nested-loop work
/// + cross-applying every edge whose endpoints are now in the
/// prefix.
fn plan_cost(order: &[usize], sizes: &[u64], edges: &[Edge]) -> f64 {
    // Track output size at each step. Step 0: just the first table.
    let mut running = sizes[order[0]] as f64;
    let mut cost = 0.0_f64;
    let mut in_prefix: Vec<bool> = alloc::vec![false; sizes.len()];
    in_prefix[order[0]] = true;
    for &table_idx in &order[1..] {
        let right = sizes[table_idx] as f64;
        // Step cost: produce `running × right` candidate rows
        // before filtering.
        cost += running * right;
        in_prefix[table_idx] = true;
        let mut step_output = running * right;
        // Apply every edge whose endpoints just became fully
        // covered by the prefix.
        for edge in edges {
            if edge
                .endpoints
                .iter()
                .all(|&e| in_prefix[e])
            {
                // Apply selectivity only if at least one endpoint
                // is this step's `table_idx` — otherwise the
                // selectivity was already applied at an earlier
                // step.
                if edge.endpoints.contains(&table_idx) {
                    step_output *= edge.selectivity;
                }
            }
        }
        running = step_output.max(1.0);
    }
    cost
}

fn rewrite_from(
    from: &mut FromClause,
    tables: &[TableRef],
    edges: &[Edge],
    order: &[usize],
) {
    from.primary = tables[order[0]].clone();
    from.joins.clear();
    let mut in_prefix: Vec<bool> = alloc::vec![false; tables.len()];
    in_prefix[order[0]] = true;
    let mut edges_used: Vec<bool> = alloc::vec![false; edges.len()];
    for &table_idx in &order[1..] {
        in_prefix[table_idx] = true;
        // Pick the edge that joins `table_idx` to the prefix.
        // There may be multiple; AND them all so we don't lose any
        // predicate.
        let mut combined: Option<Expr> = None;
        for (ei, edge) in edges.iter().enumerate() {
            if edges_used[ei] {
                continue;
            }
            if edge.endpoints.contains(&table_idx)
                && edge.endpoints.iter().all(|&e| in_prefix[e])
            {
                edges_used[ei] = true;
                combined = Some(match combined {
                    None => edge.predicate.clone(),
                    Some(prev) => Expr::Binary {
                        op: spg_sql::ast::BinOp::And,
                        lhs: alloc::boxed::Box::new(prev),
                        rhs: alloc::boxed::Box::new(edge.predicate.clone()),
                    },
                });
            }
        }
        // Fallback: if no edge applies, build a `TRUE` predicate
        // so the join shape stays valid. (Shouldn't happen on a
        // connected join graph; the reorder pass only takes
        // connected graphs anyway.)
        let on = combined.unwrap_or_else(|| {
            Expr::Literal(spg_sql::ast::Literal::Bool(true))
        });
        from.joins.push(FromJoin {
            kind: JoinKind::Inner,
            table: tables[table_idx].clone(),
            on: Some(on),
        });
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use spg_sql::parser;

    #[test]
    fn no_joins_is_noop() {
        let mut stmt = match parser::parse_statement("SELECT * FROM users").unwrap() {
            spg_sql::ast::Statement::Select(s) => s,
            _ => panic!(),
        };
        let cat = Catalog::new();
        let stats = Statistics::new();
        let snap = stmt.clone();
        reorder_joins(&mut stmt, &cat, &stats);
        assert_eq!(stmt, snap);
    }

    #[test]
    fn five_table_star_picks_fact_first() {
        // 4 big tables joined to a small fact table via fact.k_i =
        // big_i.k. Reorder must pick fact first so each
        // intermediate stays at fact-table cardinality.
        let mut e = crate::Engine::new();
        e.execute("CREATE TABLE fact (id INT NOT NULL, k1 INT NOT NULL, k2 INT NOT NULL, k3 INT NOT NULL, k4 INT NOT NULL)").unwrap();
        for tag in ["big1", "big2", "big3", "big4"] {
            e.execute(&alloc::format!("CREATE TABLE {tag} (k INT NOT NULL)"))
                .unwrap();
        }
        for i in 0..3 {
            e.execute(&alloc::format!(
                "INSERT INTO fact VALUES ({i}, {i}, {i}, {i}, {i})"
            ))
            .unwrap();
        }
        for tag in ["big1", "big2", "big3", "big4"] {
            for i in 0..40 {
                e.execute(&alloc::format!("INSERT INTO {tag} VALUES ({i})"))
                    .unwrap();
            }
        }
        e.execute("ANALYZE").unwrap();
        let stmt = e.prepare(
            "SELECT fact.id FROM big1 \
             INNER JOIN big2 ON 1 = 1 \
             INNER JOIN big3 ON 1 = 1 \
             INNER JOIN big4 ON 1 = 1 \
             INNER JOIN fact ON fact.k1 = big1.k AND fact.k2 = big2.k AND fact.k3 = big3.k AND fact.k4 = big4.k",
        )
        .unwrap();
        let spg_sql::ast::Statement::Select(sel) = stmt else { panic!() };
        let from = sel.from.unwrap();
        assert_eq!(
            from.primary.name, "fact",
            "reorder must put fact first; got primary={:?}",
            from.primary.name
        );
    }

    #[test]
    fn left_join_is_skipped() {
        // LEFT JOIN has semantics-preserving order; v6.2.3 bails.
        let mut stmt = match parser::parse_statement(
            "SELECT * FROM a LEFT JOIN b ON a.id = b.id LEFT JOIN c ON b.id = c.id",
        )
        .unwrap()
        {
            spg_sql::ast::Statement::Select(s) => s,
            _ => panic!(),
        };
        let cat = Catalog::new();
        let stats = Statistics::new();
        let snap = stmt.clone();
        reorder_joins(&mut stmt, &cat, &stats);
        assert_eq!(stmt, snap);
    }
}