Skip to main content

spg_engine/
select.rs

1//! SELECT execution — the window / meta-view / CTE variants and the
2//! subquery-resolution pre-pass. Lifted out of `lib.rs` (v7.32 engine
3//! modularisation). These `impl Engine` methods are dispatched from the
4//! bare-SELECT entry points and drive the non-trivial SELECT shapes.
5
6use alloc::borrow::Cow;
7use alloc::string::{String, ToString};
8use alloc::vec::Vec;
9
10use spg_sql::ast::{
11    ColumnName, Expr, FromClause, SelectItem, SelectStatement, Statement, TableRef, UnionKind,
12};
13use spg_storage::{
14    Catalog, ColumnSchema, DataType, Row, StorageError, TableSchema, Value, VecEncoding,
15};
16
17use crate::describe;
18use crate::eval::{EvalContext, EvalError};
19use crate::join::RowRef;
20use crate::system_catalog::collect_view_refs;
21use crate::{
22    ByteBudget, CancelToken, Engine, EngineError, OrderKey, QueryResult, aggregate,
23    apply_offset_and_limit, apply_offset_and_limit_tagged, approx_row_bytes, build_order_keys,
24    collect_meta_view_names, collect_qualified_refs, collect_scalar_subqueries,
25    collect_window_nodes, compute_window_partition, eval, expr_tree_has_subquery,
26    materialise_in_order, materialise_meta_view, memoize, order_by_value_cmp_in, partition_key_cmp,
27    rewrite_window_to_columns, select_has_window, select_references_meta_view, select_refers_to,
28    sort_by_keys, synth_info_key_column_usage, synth_info_referential_constraints,
29    synth_info_routines, synth_info_statistics, synth_information_schema_columns,
30    synth_information_schema_tables, synth_mysql_db, synth_mysql_user, synth_pg_attribute,
31    synth_pg_class, synth_pg_constraint, synth_pg_database, synth_pg_extension, synth_pg_index_raw,
32    synth_pg_indexes, synth_pg_namespace, synth_pg_operator, synth_pg_proc, synth_pg_roles,
33    synth_pg_sequence, synth_pg_settings, synth_pg_timezone_abbrevs, synth_pg_timezone_names,
34    synth_pg_trigger, synth_pg_type, synth_pg_views, topk_trim, try_gin_jsonb_seek, try_gin_seek,
35    try_index_seek, try_nsw_knn, try_pk_walk_top_n, try_trgm_seek, value_is_bigint,
36    value_is_integer, value_to_i64,
37};
38
39/// v7.39 (round 618) — a recursive term that can be run over the working set
40/// directly, instead of through a whole query execution per round.
41///
42/// PG plans the recursive term ONCE and re-scans a worktable each iteration.
43/// SPG emptied and refilled a real table and then called `exec_select_cancel`
44/// — FROM resolution, schema build, predicate compilation, projection build
45/// and result materialisation — for every round. Measured with the counting
46/// allocator on `WITH RECURSIVE r(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM r
47/// WHERE n < N)`: about 40 allocations and 99 kB PER ROUND while the working
48/// set is one row, or 1.98 GB at N = 20000.
49///
50/// This is the shape that covers the ordinary recursive term: read the CTE,
51/// filter it, project it. Anything else — a join, an aggregate, a window, a
52/// subquery, DISTINCT, GROUP BY, ORDER BY, LIMIT, a locking clause, a
53/// non-table source — returns `None` and keeps the general path, so the
54/// answers it gives are the ones that path gave.
55struct RecursiveTermPlan<'t> {
56    items: Vec<&'t Expr>,
57    where_: Option<&'t Expr>,
58    alias: String,
59}
60
61fn plan_recursive_term<'t>(
62    t: &'t SelectStatement,
63    cte_name: &str,
64    ncols: usize,
65) -> Option<RecursiveTermPlan<'t>> {
66    if !t.unions.is_empty()
67        || !t.ctes.is_empty()
68        || t.distinct
69        || !t.distinct_on.is_empty()
70        || t.group_by.is_some()
71        || t.group_by_all
72        || t.having.is_some()
73        || !t.order_by.is_empty()
74        || t.limit.is_some()
75        || t.offset.is_some()
76        || t.limit_with_ties
77        || t.locking.is_some()
78    {
79        return None;
80    }
81    let from = t.from.as_ref()?;
82    if !from.joins.is_empty() {
83        return None;
84    }
85    let p = &from.primary;
86    if !p.name.eq_ignore_ascii_case(cte_name)
87        || p.as_of_segment.is_some()
88        || p.unnest_expr.is_some()
89        || !p.unnest_column_aliases.is_empty()
90        || p.with_ordinality
91        || p.generate_series_args.is_some()
92        || p.lateral_subquery.is_some()
93        || p.jsonb_each_text_arg.is_some()
94        || p.table_fn_call.is_some()
95    {
96        return None;
97    }
98    let unsupported = |e: &Expr| {
99        crate::aggregate::contains_aggregate(e)
100            || crate::subquery::expr_has_subquery(e)
101            || crate::window::expr_has_window_pub(e)
102    };
103    let mut items: Vec<&Expr> = Vec::with_capacity(t.items.len());
104    for it in &t.items {
105        match it {
106            SelectItem::Expr { expr, .. } => {
107                if unsupported(expr) {
108                    return None;
109                }
110                items.push(expr);
111            }
112            // `*` would have to be expanded against the CTE's own schema;
113            // the general path already does that, so leave it there.
114            _ => return None,
115        }
116    }
117    if items.len() != ncols {
118        return None;
119    }
120    if let Some(w) = &t.where_
121        && unsupported(w)
122    {
123        return None;
124    }
125    Some(RecursiveTermPlan {
126        items,
127        where_: t.where_.as_ref(),
128        alias: p.alias.clone().unwrap_or_else(|| p.name.clone()),
129    })
130}
131
132impl Engine {
133    /// v4.12 window executor. Implements `ROW_NUMBER` / `RANK` /
134    /// `DENSE_RANK` and the partition-aware aggregates `SUM` /
135    /// `AVG` / `COUNT` / `MIN` / `MAX`. The plan is:
136    /// 1. Apply the WHERE filter.
137    /// 2. For each unique `WindowFunction` node in the projection,
138    ///    partition + sort, compute the per-row value.
139    /// 3. Append the window values as synthetic columns (`__win_N`)
140    ///    to the row schema.
141    /// 4. Rewrite the projection to read those columns.
142    /// 5. Hand off to the regular project / ORDER BY / LIMIT pipe.
143    #[allow(
144        clippy::too_many_lines,
145        clippy::type_complexity,
146        clippy::needless_range_loop
147    )] // window-eval is one cohesive pipe; splitting fragments
148    pub(crate) fn exec_select_with_window(
149        &self,
150        stmt: &SelectStatement,
151        cancel: CancelToken<'_>,
152    ) -> Result<QueryResult, EngineError> {
153        let from = stmt.from.as_ref().ok_or_else(|| {
154            EngineError::Unsupported("window functions require a FROM clause".into())
155        })?;
156        // v7.17.0 Phase 3.P0-43 — JOIN + window functions. Phase
157        // 3.6 rejected this combination outright ("queued for
158        // v5.x"); P0-43 materialises the join + WHERE through the
159        // existing nested-loop helper and runs the window pipeline
160        // on the joined row set with the combined `alias.col`
161        // schema. The window expressions resolve through the
162        // qualifier-aware column resolver same as the aggregate /
163        // projection paths on JOIN.
164        let (schema_cols_owned, alias_opt): (Vec<ColumnSchema>, Option<&str>);
165        // v7.39 (round 976) — rows this walk OWNS. A derived FROM item and
166        // a JOIN both produce rows that exist nowhere else, so they land
167        // here; a plain stored table does not, and borrows instead.
168        //
169        // It used to clone every row out of the table, on the reasoning
170        // that "the clone is cheap relative to the window computation that
171        // follows". Measured on 400k rows, `row_number() OVER ()` cost
172        // 31.881 ms against 46.520 with a 200-byte column added — so the
173        // clone tracks row width at about 36 ns per row per 200 bytes, and
174        // the window computation it was being compared against is a
175        // counter increment per row. Nothing downstream needs the rows
176        // owned: the very next statement used to be
177        // `filtered.iter().collect()` into the `&Row` slice the window
178        // pipeline actually reads.
179        let mut owned_rows: Vec<Row<'static>> = Vec::new();
180        // What the pipeline reads. Borrows `owned_rows` or the table.
181        let mut filtered: Vec<&Row<'static>> = Vec::new();
182        // Set by the branches that fill `owned_rows`, because "empty" is
183        // an answer a query can legitimately have and so cannot be the
184        // signal for which of the two holds the rows.
185        let mut rows_are_owned = false;
186        if from.joins.is_empty() {
187            let primary = &from.primary;
188            // v7.37 D.13 — window functions over a derived table (subquery /
189            // VALUES / unnest / generate_series). The catalog-by-name lookup
190            // below only finds real tables, so a derived primary threw
191            // TableNotFound. Materialise the derived rows + schema through the
192            // same helper the non-window FROM-primary path uses, then WHERE-
193            // filter and feed the identical window pipeline.
194            let is_derived = primary.lateral_subquery.is_some()
195                || primary.unnest_expr.is_some()
196                || primary.generate_series_args.is_some()
197                || primary.jsonb_each_text_arg.is_some()
198                || primary.table_fn_call.is_some();
199            if is_derived {
200                let (drows, dcols) = self.materialise_table_ref(primary)?;
201                schema_cols_owned = dcols;
202                alias_opt = primary.alias.as_deref();
203                let ctx = self.ev_ctx(&schema_cols_owned, alias_opt);
204                let mut owned: Vec<Row<'static>> = Vec::new();
205                for (i, row) in drows.into_iter().enumerate() {
206                    if i.is_multiple_of(256) {
207                        cancel.check()?;
208                    }
209                    if let Some(w) = &stmt.where_ {
210                        let cond = eval::eval_expr(w, &row, &ctx)?;
211                        if !crate::eval::predicate_is_true(&cond, "WHERE", ctx.mysql_dialect)? {
212                            continue;
213                        }
214                    }
215                    owned.push(row);
216                }
217                owned_rows = owned;
218                rows_are_owned = true;
219            } else {
220                let table = self.active_catalog().get(&primary.name).ok_or_else(|| {
221                    StorageError::TableNotFound {
222                        name: primary.name.clone(),
223                    }
224                })?;
225                let alias = primary.alias.as_deref().unwrap_or(primary.name.as_str());
226                schema_cols_owned = table.schema().columns.clone();
227                alias_opt = Some(alias);
228                let ctx = self.ev_ctx(&schema_cols_owned, alias_opt);
229                // The WHERE test, in ONE place, for all four ways a row can
230                // reach this walk. It deliberately does not touch the row
231                // collections: a closure that pushed into them would tie
232                // its argument to the closure body and no borrowed row
233                // could escape it, which is what forced the clone-shaped
234                // version of this loop in the first place.
235                let passes = |row: &Row<'static>| -> Result<bool, EngineError> {
236                    if let Some(w) = &stmt.where_ {
237                        let cond = eval::eval_expr(w, row, &ctx)?;
238                        if !crate::eval::predicate_is_true(&cond, "WHERE", ctx.mysql_dialect)? {
239                            return Ok(false);
240                        }
241                    }
242                    Ok(true)
243                };
244                // v7.37.15 Phase B — scan_visible filters rows by the
245                // engine's current snapshot. Phase B's `current_snapshot()`
246                // returns `Snapshot::unbounded()` so every row is visible,
247                // matching pre-v7.37.15 byte-for-byte. Phase C will wire
248                // real per-tx snapshots through this same callsite — no
249                // code change needed here when that lands.
250                let snap = self.current_snapshot();
251                if table.has_cold_rows_fast() {
252                    // v7.36 (cold-tier coverage) — a cold segment's rows
253                    // are produced on demand and live in a temporary this
254                    // walk cannot borrow from, so a table carrying any owns
255                    // its rows. Hot iter then cold iter, both through the
256                    // same WHERE, as before.
257                    let mut owned: Vec<Row<'static>> = Vec::new();
258                    for (i, row) in table.scan_visible(&snap) {
259                        if i.is_multiple_of(256) {
260                            cancel.check()?;
261                        }
262                        if passes(row)? {
263                            owned.push(row.clone());
264                        }
265                    }
266                    let hot_len = table.row_count();
267                    for (offset, row) in self.iter_cold_rows_of_table(table).iter().enumerate() {
268                        let i = hot_len + offset;
269                        if i.is_multiple_of(256) {
270                            cancel.check()?;
271                        }
272                        if passes(row)? {
273                            owned.push(row.clone());
274                        }
275                    }
276                    owned_rows = owned;
277                    rows_are_owned = true;
278                } else {
279                    // v7.39 (round 975) — ask the indices first, the way
280                    // the streaming walk has since round 970. This walk had
281                    // the same hole and it is reached by any statement
282                    // carrying a window function, so a WHERE that names an
283                    // indexed column read the whole table: measured on 400k
284                    // rows, `row_number() OVER () … WHERE id = 500` — a
285                    // ONE-row answer on a primary key — took 13.762 ms
286                    // against PG18.4's 0.151, while the same predicate
287                    // without the window took 0.091. The cost was
288                    // independent of how many rows survived (999 survivors
289                    // cost 13.312 ms) and of row width (13.312 narrow vs
290                    // 13.327 wide), which is what a full table walk looks
291                    // like and what a result-shaped cost does not.
292                    //
293                    // The seek only NARROWS — `passes` still applies the
294                    // whole WHERE — so no answer can change. Positions
295                    // arrive visibility-filtered by the same predicate the
296                    // scan applies and capped at a quarter of the table,
297                    // and `None` walks the table exactly as before.
298                    let seek_positions: Option<Vec<usize>> = stmt.where_.as_ref().and_then(|w| {
299                        crate::index_access::try_index_seek_positions(
300                            w,
301                            &schema_cols_owned,
302                            table,
303                            alias,
304                            &snap,
305                            self.speaks_mysql,
306                        )
307                    });
308                    match seek_positions {
309                        Some(mut positions) => {
310                            // Table order, which is the order the scan
311                            // would have produced.
312                            positions.sort_unstable();
313                            for (n, pos) in positions.into_iter().enumerate() {
314                                if n.is_multiple_of(256) {
315                                    cancel.check()?;
316                                }
317                                let Some(row) = table.rows().get(pos) else {
318                                    continue;
319                                };
320                                if passes(row)? {
321                                    filtered.push(row);
322                                }
323                            }
324                        }
325                        None => {
326                            for (i, row) in table.scan_visible(&snap) {
327                                if i.is_multiple_of(256) {
328                                    cancel.check()?;
329                                }
330                                if passes(row)? {
331                                    filtered.push(row);
332                                }
333                            }
334                        }
335                    }
336                }
337            }
338        } else {
339            let deferred = self.build_joined_filtered_rows(
340                from,
341                stmt.where_.as_ref(),
342                cancel,
343                None,
344                &mut ByteBudget::new(self.max_query_bytes),
345            )?;
346            // A join's survivors are row-index tuples over its sources, so
347            // there is no single row to borrow — this branch owns them.
348            owned_rows = deferred.materialise();
349            rows_are_owned = true;
350            schema_cols_owned = deferred.combined_schema;
351            alias_opt = None;
352        }
353        if rows_are_owned {
354            filtered = owned_rows.iter().collect();
355        }
356        let schema_cols = &schema_cols_owned;
357        let ctx = self.ev_ctx(schema_cols, alias_opt);
358        let alias = alias_opt.unwrap_or("");
359        let n_rows = filtered.len();
360        // The window pipeline reads `&[&Row<'static>]`, and `filtered`
361        // already is one whichever branch produced it — the separate
362        // `filtered_refs` this used to build was the collect that made
363        // owning the rows look necessary.
364
365        // 2) Collect unique window function nodes from projection.
366        let mut window_nodes: Vec<Expr> = Vec::new();
367        for item in &stmt.items {
368            if let SelectItem::Expr { expr, .. } = item {
369                collect_window_nodes(expr, &mut window_nodes);
370            }
371        }
372        // v7.39 (round 592) — and from ORDER BY, which may name a window the
373        // select list never mentions. The order-key builder below rewrites
374        // window calls to `__win_N` columns, and a call that was never
375        // collected has no column to become.
376        for o in &stmt.order_by {
377            collect_window_nodes(&o.expr, &mut window_nodes);
378        }
379
380        // 3) For each window, compute per-row value.
381        // Index: same order as window_nodes; for row i, win_vals[w][i].
382        let mut win_vals: Vec<Vec<Value<'static>>> = Vec::with_capacity(window_nodes.len());
383        for wnode in &window_nodes {
384            let Expr::WindowFunction {
385                name,
386                args,
387                partition_by,
388                order_by,
389                frame,
390                null_treatment,
391                filter,
392            } = wnode
393            else {
394                unreachable!("collect_window_nodes pushes only WindowFunction");
395            };
396            // Compute (partition_key, order_key, original_index) for each row.
397            // v7.39 (round 593) — a key that is a plain column sits at the same
398            // position in every row, but was resolved BY NAME for each one. A
399            // per-library profile of `lag(id) OVER (ORDER BY id)` put
400            // `resolve_column` at 5.8% of the query on its own, with
401            // `rehydrate_cell` and the `eval_expr` dispatch behind it. Resolve
402            // once; anything that is not a plain column keeps the resolver.
403            let p_bound: Vec<Option<usize>> = partition_by
404                .iter()
405                .map(|e| crate::orderby::bound_column_position(e, schema_cols, alias_opt))
406                .collect();
407            let o_bound: Vec<Option<usize>> = order_by
408                .iter()
409                .map(|(e, _, _)| crate::orderby::bound_column_position(e, schema_cols, alias_opt))
410                .collect();
411            let arg_bound = args
412                .first()
413                .and_then(|a| crate::orderby::bound_column_position(a, schema_cols, alias_opt));
414            // v7.39 (round 690) — a window's ORDER BY over a column that
415            // declares a collation sorts by it, the same as a top-level
416            // ORDER BY. Resolved from the bound position, so only a bare
417            // column gets one; an expression produces a new value and the
418            // derivation that would give IT a collation is unbuilt.
419            let o_colls: Vec<Option<alloc::string::String>> = o_bound
420                .iter()
421                .map(|p| {
422                    p.and_then(|pos| schema_cols.get(pos))
423                        .and_then(|sc| sc.collation_name.clone())
424                        .filter(|n| crate::collate::is_supported(n))
425                })
426                .collect();
427            let mut indexed: Vec<(Vec<Value<'static>>, Vec<(Value, bool, Option<bool>)>, usize)> =
428                Vec::with_capacity(n_rows);
429            // v7.39 (round 731) — single bound INT partition key, no window
430            // ORDER BY: group on the i64 directly. The generic build paid
431            // two heap Vecs per row (pkey + empty okey) plus a canonical
432            // string encode per row just to bucket 500k rows into 100
433            // groups; the whole per-row key apparatus disappears here.
434            // Neither key Vec is read downstream on this path: the hash
435            // grouping replaces partition_key_cmp, and okey is empty by
436            // construction.
437            let int_pkey_fast = order_by.is_empty()
438                && partition_by.len() == 1
439                && p_bound[0].is_some_and(|pos| {
440                    matches!(
441                        schema_cols.get(pos).map(|c| c.ty),
442                        Some(
443                            spg_storage::DataType::Int
444                                | spg_storage::DataType::BigInt
445                                | spg_storage::DataType::SmallInt
446                        )
447                    )
448                });
449            // v7.39 (round 979) — the same idea for a single bound INT
450            // window ORDER BY: sort on the i64 instead of on a heap vector
451            // per row.
452            //
453            // Measured at 400k rows (round 978, ablation, answer checked
454            // byte-for-byte against the general path on a key column that
455            // is a permutation): `row_number() OVER (ORDER BY k)` went
456            // 157.057-157.868 ms to 31.253-31.679, which is 79.8% and puts
457            // it on top of the `OVER ()` baseline — the sort essentially
458            // disappears. Round 977 had already shown the cost was
459            // key-shaped rather than row-shaped: the sort's share was
460            // 132.0 ms on a three-integer table and 132.5 with a 200-byte
461            // column added, and a per-row COPY does scale with width
462            // (round 976 measured that at +36 ns/row/200 bytes).
463            //
464            // Gated to ROW_NUMBER, which is the one function that reads
465            // neither key vector — it numbers the order it is handed.
466            // `rank` and `dense_rank` compare adjacent entries' order keys
467            // in `compute_window_partition`, so leaving those vectors
468            // empty would silently give every row rank 1. A wider version
469            // would carry the i64 in the entry and teach those two to use
470            // it; this one is the part that can be shown correct by
471            // construction.
472            let int_okey_fast = partition_by.is_empty()
473                && order_by.len() == 1
474                && frame.is_none()
475                && filter.is_none()
476                && matches!(null_treatment, spg_sql::ast::NullTreatment::Respect)
477                && name.eq_ignore_ascii_case("row_number")
478                && o_bound[0].is_some_and(|pos| {
479                    matches!(
480                        schema_cols.get(pos).map(|c| c.ty),
481                        Some(
482                            spg_storage::DataType::Int
483                                | spg_storage::DataType::BigInt
484                                | spg_storage::DataType::SmallInt
485                        )
486                    )
487                });
488            // Set when a cell in that column turns out not to be an
489            // integer after all. The declared type says it should be, but
490            // "should" is not a thing to sort 400k rows on, so the general
491            // path takes over and this build is discarded.
492            let mut int_okey_bailed = false;
493            if int_okey_fast {
494                let pos = o_bound[0].expect("gated bound");
495                let desc = order_by[0].1;
496                // PG orders NULLs last ascending and first descending
497                // unless the query says otherwise.
498                let nulls_first = order_by[0].2.unwrap_or(desc);
499                let mut keyed: Vec<(bool, i64, usize)> = Vec::with_capacity(n_rows);
500                for (i, row) in filtered.iter().enumerate() {
501                    match row.values.get(pos) {
502                        Some(Value::Int(n)) => keyed.push((false, i64::from(*n), i)),
503                        Some(Value::BigInt(n)) => keyed.push((false, *n, i)),
504                        Some(Value::SmallInt(n)) => keyed.push((false, i64::from(*n), i)),
505                        Some(Value::Null) | None => keyed.push((true, 0, i)),
506                        Some(_) => {
507                            int_okey_bailed = true;
508                            break;
509                        }
510                    }
511                }
512                if !int_okey_bailed {
513                    // `null_rank` puts NULLs on the side the query asked
514                    // for; the row's original index breaks every tie, so
515                    // equal keys keep the order the scan produced — what
516                    // the stable sort below would have given them.
517                    let null_rank = |is_null: bool| -> u8 { u8::from(is_null != nulls_first) };
518                    keyed.sort_unstable_by(|a, b| {
519                        null_rank(a.0)
520                            .cmp(&null_rank(b.0))
521                            .then_with(|| {
522                                if a.0 {
523                                    core::cmp::Ordering::Equal
524                                } else if desc {
525                                    b.1.cmp(&a.1)
526                                } else {
527                                    a.1.cmp(&b.1)
528                                }
529                            })
530                            .then_with(|| a.2.cmp(&b.2))
531                    });
532                    for (_, _, i) in keyed {
533                        indexed.push((Vec::new(), Vec::new(), i));
534                    }
535                } else {
536                    indexed.clear();
537                }
538            }
539            if int_okey_fast && !int_okey_bailed {
540                // Ordered above; nothing else to build.
541            } else if int_pkey_fast {
542                let pos = p_bound[0].expect("gated bound");
543                let mut slot: hashbrown::HashMap<Option<i64>, usize> = hashbrown::HashMap::new();
544                let mut groups: Vec<Vec<usize>> = Vec::new();
545                for (i, row) in filtered.iter().enumerate() {
546                    let k: Option<i64> = match row.values.get(pos) {
547                        Some(Value::BigInt(n)) => Some(*n),
548                        Some(Value::Int(n)) => Some(i64::from(*n)),
549                        Some(Value::SmallInt(n)) => Some(i64::from(*n)),
550                        _ => None,
551                    };
552                    match slot.get(&k) {
553                        Some(&gi) => groups[gi].push(i),
554                        None => {
555                            slot.insert(k, groups.len());
556                            groups.push(alloc::vec![i]);
557                        }
558                    }
559                }
560                // The downstream partition-boundary scan compares pkeys
561                // of ADJACENT entries, so the key must ride along — one
562                // single-element Vec per row (half the generic build's
563                // allocations, no string encode).
564                for g in groups {
565                    for i in g {
566                        let k: Value<'static> = match filtered[i].values.get(pos) {
567                            Some(v) => v.clone(),
568                            None => Value::Null,
569                        };
570                        indexed.push((alloc::vec![k], Vec::new(), i));
571                    }
572                }
573            } else {
574                for (i, row) in filtered.iter().enumerate() {
575                    let pkey: Vec<Value<'static>> = partition_by
576                        .iter()
577                        .enumerate()
578                        .map(
579                            |(k, p)| match p_bound[k].and_then(|pos| row.values.get(pos)) {
580                                Some(v) => Ok(v.clone()),
581                                None => eval::eval_expr(p, row, &ctx),
582                            },
583                        )
584                        .collect::<Result<_, _>>()?;
585                    // v7.39 (read01 round 54) — a window's ORDER BY over an enum
586                    // column must sort by MEMBER order (enumsortorder), not the
587                    // label's text. Enum values are Text at runtime, so the raw
588                    // value key sorted alphabetically — `row_number() OVER (ORDER
589                    // BY mood)` numbered the rows happy,ok,sad. Substitute the
590                    // member ordinal, the same key the top-level ORDER BY uses.
591                    // (Closes the enum-order knife's recorded window residual.)
592                    let okey: Vec<(Value, bool, Option<bool>)> = order_by
593                        .iter()
594                        .enumerate()
595                        .map(|(k, (e, desc, nf))| -> Result<_, EngineError> {
596                            let v = match o_bound[k].and_then(|pos| row.values.get(pos)) {
597                                Some(v) => v.clone(),
598                                None => eval::eval_expr(e, row, &ctx)?,
599                            };
600                            let v = match crate::orderby::enum_order_ordinal(e, &v, &ctx) {
601                                Some(ord) => Value::Float(ord),
602                                None => v,
603                            };
604                            Ok((v, *desc, *nf))
605                        })
606                        .collect::<Result<_, _>>()?;
607                    indexed.push((pkey, okey, i));
608                }
609            }
610            // Sort by (partition_key, order_key). Partition key uses
611            // a stable encoded form; order key respects ASC/DESC.
612            // v7.39 (round 731) — with NO window ORDER BY the sort's only
613            // job was putting same-partition rows next to each other, and a
614            // 500k-row comparison sort is a spectacular way to hash-group:
615            // the panel's `sum(id) OVER (PARTITION BY g)` spent ~100 ms
616            // here. Group by encoded key instead, preserving row order
617            // inside each group — exactly what the stable sort preserved,
618            // so every function (row_number included) answers the same.
619            if int_okey_fast && !int_okey_bailed {
620                // Already ordered by the i64 key above.
621            } else if int_pkey_fast {
622                // Already grouped above; same-partition rows are adjacent
623                // in original row order.
624            } else if order_by.is_empty() && !partition_by.is_empty() {
625                let mut slot: hashbrown::HashMap<String, usize> = hashbrown::HashMap::new();
626                let mut groups: Vec<
627                    Vec<(Vec<Value<'static>>, Vec<(Value, bool, Option<bool>)>, usize)>,
628                > = Vec::new();
629                let mut keybuf = String::new();
630                for entry in indexed.drain(..) {
631                    keybuf.clear();
632                    for v in &entry.0 {
633                        crate::aggregate::push_canonical_key(&mut keybuf, v);
634                    }
635                    match slot.get(keybuf.as_str()) {
636                        Some(&gi) => groups[gi].push(entry),
637                        None => {
638                            slot.insert(keybuf.clone(), groups.len());
639                            groups.push(alloc::vec![entry]);
640                        }
641                    }
642                }
643                for g in groups {
644                    indexed.extend(g);
645                }
646            } else {
647                indexed.sort_by(|a, b| {
648                    let p_cmp = partition_key_cmp(&a.0, &b.0);
649                    if p_cmp != core::cmp::Ordering::Equal {
650                        return p_cmp;
651                    }
652                    crate::window::order_key_cmp_in(&a.1, &b.1, &o_colls)
653                });
654            }
655            // Per-partition compute.
656            let mut out_vals: Vec<Value<'static>> = alloc::vec![Value::Null; n_rows];
657            let mut p_start = 0;
658            while p_start < indexed.len() {
659                let mut p_end = p_start + 1;
660                while p_end < indexed.len()
661                    && partition_key_cmp(&indexed[p_start].0, &indexed[p_end].0)
662                        == core::cmp::Ordering::Equal
663                {
664                    p_end += 1;
665                }
666                // Compute the function within this partition slice.
667                compute_window_partition(
668                    name,
669                    args,
670                    arg_bound,
671                    !order_by.is_empty(),
672                    frame.as_ref(),
673                    *null_treatment,
674                    filter.as_deref(),
675                    &indexed[p_start..p_end],
676                    &filtered,
677                    &ctx,
678                    &mut out_vals,
679                )?;
680                p_start = p_end;
681            }
682            win_vals.push(out_vals);
683        }
684
685        // 4) Build extended schema: original columns + synthetic.
686        let mut ext_cols = schema_cols.clone();
687        for (i, wnode) in window_nodes.iter().enumerate() {
688            // v7.39.12 — the synthetic column carries the window call's
689            // TYPE.
690            //
691            // The comment here said "type doesn't matter for projection
692            // eval", and for the eval it does not — the values are
693            // already computed. It is the type that travels in the
694            // RowDescription, and psql aligns a column by that: on
695            // `SELECT count(*) AS plaincnt, count(*) OVER () AS wincnt`
696            // PostgreSQL right-aligns both and SPG left-aligned the
697            // second, because the first was bigint and the second was
698            // this `Text`. `\gdesc` — which asks the extended
699            // protocol's Describe — reported the right type for both,
700            // so the two descriptions of one column disagreed.
701            //
702            // Reported by sentori against 7.39.11, found by the
703            // alignment. Text stays as the fallback for a call whose
704            // type this build cannot name, which is what it was.
705            let ty =
706                crate::describe::describe_expr_type(wnode, schema_cols).unwrap_or(DataType::Text);
707            ext_cols.push(ColumnSchema::new(alloc::format!("__win_{i}"), ty, true));
708        }
709        // 6) Rewrite the projection: WindowFunction nodes → Column(__win_N).
710        let mut rewritten_items: Vec<SelectItem> = Vec::with_capacity(stmt.items.len());
711        for item in &stmt.items {
712            let new_item = match item {
713                SelectItem::Wildcard => SelectItem::Wildcard,
714                SelectItem::QualifiedWildcard(q) => SelectItem::QualifiedWildcard(q.clone()),
715                SelectItem::Expr { expr, alias } => {
716                    let mut e = expr.clone();
717                    rewrite_window_to_columns(&mut e, &window_nodes);
718                    // The rewrite swaps the window call for a synthetic
719                    // `__win_N` column, and the projection then reported
720                    // THAT as the column name — `SELECT count(*) OVER ()`
721                    // answered `__win_0`, an internal name, where PG18
722                    // answers `count`. Pin the name while the call the
723                    // column is named for is still in hand.
724                    let alias = if alias.is_none() && e != *expr {
725                        Some(default_output_name(expr, self.speaks_mysql))
726                    } else {
727                        alias.clone()
728                    };
729                    SelectItem::Expr { expr: e, alias }
730                }
731            };
732            rewritten_items.push(new_item);
733        }
734
735        // 7) Project into final rows. JOIN case uses None so the
736        // qualifier check in `resolve_column` falls through to the
737        // composite `alias.col` schema lookup; single-table case
738        // keeps the bare alias so `bare_col` resolution still
739        // works for the projection's per-row column references.
740        // v7.39 (read01 round 54) — build through `ev_ctx`, the canonical
741        // constructor: it threads the catalog (plus render style / tz / GUCs)
742        // that a bare `EvalContext::new` drops. Without the catalog the OUTER
743        // `ORDER BY <enum col>` of a windowed query sorted by TEXT — the
744        // window values were right, the row order silently was not.
745        let ext_ctx = self.ev_ctx(&ext_cols, alias_opt);
746        let projection = build_projection_hiding_tail(
747            &rewritten_items,
748            &ext_cols,
749            alias,
750            self.speaks_mysql,
751            window_nodes.len(),
752            Some(self.active_catalog()),
753        )?;
754        let mut tagged: Vec<(Vec<OrderKey>, Row)> = Vec::with_capacity(n_rows);
755        // v7.39 (round 592) — the extended row (input columns plus the window
756        // values) used to be materialised for EVERY input row and kept until
757        // the projection had run: the input values cloned into a fresh Vec,
758        // then grown once to take the window columns. A counting allocator put
759        // the window path at 4 allocations a row where a plain derived table
760        // takes 1, and named all four — the input row, the clone, the growth,
761        // and the projected row. Only the last has to exist afterwards, so the
762        // extended row is one buffer refilled per row.
763        let mut ext_row: Row<'static> =
764            Row::new(Vec::with_capacity(schema_cols.len() + window_nodes.len()));
765        for i in 0..n_rows {
766            if i.is_multiple_of(256) {
767                cancel.check()?;
768            }
769            ext_row.values.clear();
770            ext_row.values.extend(filtered[i].values.iter().cloned());
771            for w in 0..window_nodes.len() {
772                ext_row.values.push(win_vals[w][i].clone());
773            }
774            let row = &ext_row;
775            let mut values = Vec::with_capacity(projection.len());
776            for p in &projection {
777                values.push(eval::eval_expr(&p.expr, row, &ext_ctx)?);
778            }
779            let order_keys = if stmt.order_by.is_empty() {
780                Vec::new()
781            } else {
782                let mut keys = Vec::with_capacity(stmt.order_by.len());
783                for o in &stmt.order_by {
784                    let mut e = o.expr.clone();
785                    rewrite_window_to_columns(&mut e, &window_nodes);
786                    let key = eval::eval_expr(&e, row, &ext_ctx)?;
787                    // v7.39 (read01 round 54) — this path builds its order keys
788                    // itself instead of going through `build_order_keys`, so it
789                    // skipped the enum-ordinal substitution: the OUTER
790                    // `ORDER BY <enum col>` of a windowed query sorted by the
791                    // label's TEXT, not by member order. The window values were
792                    // right and only the row order was wrong — silently.
793                    match crate::orderby::enum_order_ordinal(&e, &key, &ext_ctx) {
794                        Some(ord) => keys.push(value_to_order_key(&Value::Float(ord))?),
795                        None => keys.push(value_to_order_key(&key)?),
796                    }
797                }
798                keys
799            };
800            tagged.push((order_keys, Row::new(values)));
801        }
802        // ORDER BY + LIMIT/OFFSET on the projected rows.
803        if !stmt.order_by.is_empty() {
804            let descs: Vec<bool> = stmt.order_by.iter().map(|o| o.desc).collect();
805            // v7.39.11 — and the collation, which this path was sorting
806            // without.
807            //
808            // Reported by sentori against 7.39.10: `SELECT t, count(*)
809            // OVER () FROM t ORDER BY t` answered `A B a b` on a
810            // database collating `en_US.utf8` where the same query
811            // without the window function answers `a A b B`. No row is
812            // wrong and nothing raises; only the order changes.
813            //
814            // Same cause as the enum-ordinal defect the comment above
815            // records: this branch builds its order keys itself instead
816            // of going through `build_order_keys`, so anything that
817            // path resolves has to be resolved again here, and the
818            // collation was not. `order_by_collations` is the one place
819            // that answers it — explicit `COLLATE` first, then the
820            // column's declaration, then the database's — so calling it
821            // here cannot disagree with the ungrouped path.
822            let colls = crate::orderby::order_by_collations(&stmt.order_by, &ctx)?;
823            crate::orderby::sort_by_keys_in(&mut tagged, &descs, &colls);
824        }
825        let mut out_rows: Vec<Row<'static>> = tagged.into_iter().map(|(_, r)| r).collect();
826        // v7.37 D.41 — `SELECT DISTINCT` over a window projection: the window
827        // pipeline builds one output row per input row, so DISTINCT must dedup the
828        // projected rows (PG evaluates window functions before DISTINCT). Applied
829        // after ORDER BY (duplicate rows share sort keys, so order is preserved)
830        // and before LIMIT.
831        if stmt.distinct {
832            // v7.38.14 — see the synthetic-source sites below: the mask was
833            // always available here, from the same projection this function
834            // already built.
835            out_rows = dedup_rows(
836                out_rows,
837                FoldSpec::of_masks(
838                    self.speaks_mysql,
839                    &fold_mask(&projection),
840                    &pad_mask(&projection),
841                ),
842            );
843        }
844        apply_offset_and_limit(&mut out_rows, stmt.offset_literal(), stmt.limit_literal());
845        let final_cols: Vec<ColumnSchema> = projection
846            .into_iter()
847            .map(|p| p.to_column_schema())
848            .collect();
849        Ok(QueryResult::Rows {
850            columns: final_cols,
851            rows: out_rows,
852        })
853    }
854
855    /// v4.11: materialise each CTE into a temp table inside a
856    /// cloned catalog, then run the body SELECT against a fresh
857    /// engine instance that owns the enriched catalog. The clone
858    /// is moderately expensive — only paid by CTE-bearing queries.
859    /// Subqueries inside CTE bodies / the main body resolve as
860    /// usual; `clock_fn` is propagated so `NOW()` lines up.
861    /// v7.16.2 — mailrs round-10 A.3. Materialise the
862    /// `information_schema.*` / `pg_catalog.*` virtual views
863    /// the SELECT references, then re-execute the SELECT
864    /// against an enriched catalog where those views are real
865    /// tables. Same pattern as `exec_with_ctes`. The temp
866    /// engine carries `meta_views_materialised = true` so its
867    /// own meta-dispatch short-circuits — without that we'd
868    /// infinite-recurse since the temp catalog's view name
869    /// still starts with `__spg_info_` and re-triggers the
870    /// check.
871    pub(crate) fn exec_select_with_meta_views(
872        &self,
873        stmt: &SelectStatement,
874        cancel: CancelToken<'_>,
875    ) -> Result<QueryResult, EngineError> {
876        let catalog = self.meta_view_catalog(stmt)?;
877        let mut temp = Engine::restore(catalog);
878        if let Some(c) = self.clock {
879            temp = temp.with_clock(c);
880        }
881        if let Some(f) = self.salt_fn {
882            temp = temp.with_salt_fn(f);
883        }
884        // v7.39 (round 522) — the temp engine holds the materialised
885        // catalog and, until now, nothing of the SESSION. So every
886        // session-scoped answer changed the moment a system view
887        // appeared in the FROM clause: `SELECT current_user` said
888        // `unmei` and `SELECT current_user FROM pg_class` said `admin`;
889        // `current_setting('work_mem')` fell back to the boot default
890        // after a SET; `application_name` read empty. A privilege check
891        // written against a catalog join was reading a different
892        // identity than the same check written without one.
893        //
894        // Carry what a session can be observed through — its parameters
895        // (which is also where the session user lives), the role store
896        // the privilege builtins read, the dialect, and the rendering
897        // settings a timestamp is spelled with.
898        temp.session_params.clone_from(&self.session_params);
899        temp.users.clone_from(&self.users);
900        temp.backslash_escapes = self.backslash_escapes;
901        temp.speaks_mysql = self.speaks_mysql;
902        temp.mysql_strict = self.mysql_strict;
903        temp.render_style = self.render_style;
904        temp.tz_offset_fn = self.tz_offset_fn;
905        temp.tz_localize_fn = self.tz_localize_fn;
906        temp.tz_abbrev_fn = self.tz_abbrev_fn;
907        temp.meta_views_materialised = true;
908        temp.exec_select_cancel(stmt, cancel)
909    }
910
911    /// v7.39 (round 462) — the catalog a meta-view SELECT resolves
912    /// against: this engine's catalog with every `__spg_*` view the
913    /// statement references materialised into it.
914    ///
915    /// Split out of `exec_select_with_meta_views` so Describe can reach
916    /// the same shapes execution reaches. Describe used to look the FROM
917    /// relation up in the plain catalog, where a system view does not
918    /// exist, and reported "no columns" for every one of them — so an
919    /// extended-protocol client reading `pg_stat_user_tables` got rows
920    /// with no column metadata. Sharing the materialisation means a
921    /// view added here is described correctly the day it is added.
922    pub(crate) fn meta_view_catalog(&self, stmt: &SelectStatement) -> Result<Catalog, EngineError> {
923        let mut needed: alloc::collections::BTreeSet<String> = alloc::collections::BTreeSet::new();
924        collect_meta_view_names(stmt, &mut needed);
925        let mut catalog = self.active_catalog().clone();
926        for view in &needed {
927            if catalog.get(view).is_some() {
928                continue;
929            }
930            match view.as_str() {
931                "__spg_info_columns" => {
932                    let (schema, rows) = synth_information_schema_columns(
933                        self.active_catalog(),
934                        self.speaks_mysql,
935                        &self.mysql_schema_name(),
936                    );
937                    materialise_meta_view(&mut catalog, view, schema, rows)?;
938                }
939                "__spg_info_tables" => {
940                    let (schema, rows) = synth_information_schema_tables(
941                        self.active_catalog(),
942                        self.speaks_mysql,
943                        &self.mysql_schema_name(),
944                    );
945                    materialise_meta_view(&mut catalog, view, schema, rows)?;
946                }
947                "__spg_pg_class" => {
948                    let (schema, rows) = synth_pg_class(
949                        self.active_catalog(),
950                        i64::try_from(self.vacuum_oldest_active()).unwrap_or(i64::MAX),
951                    );
952                    materialise_meta_view(&mut catalog, view, schema, rows)?;
953                }
954                "__spg_pg_attribute" => {
955                    let (schema, rows) = synth_pg_attribute(self.active_catalog());
956                    materialise_meta_view(&mut catalog, view, schema, rows)?;
957                }
958                // v7.17.0 Phase 3.P0-50 — pg_catalog.pg_type for
959                // sqlx / SQLAlchemy / Diesel / pgAdmin lookups.
960                "__spg_pg_type" => {
961                    let (schema, rows) = synth_pg_type(self.active_catalog());
962                    materialise_meta_view(&mut catalog, view, schema, rows)?;
963                }
964                // v7.39 (round 621) — pg_catalog.pg_operator, which did not
965                // exist at all.
966                "__spg_pg_operator" => {
967                    let (schema, rows) = synth_pg_operator(self.active_catalog());
968                    materialise_meta_view(&mut catalog, view, schema, rows)?;
969                }
970                // v7.17.0 Phase 3.P0-51 — pg_catalog.pg_proc for
971                // function-name introspection (ORM / pgAdmin).
972                "__spg_pg_proc" => {
973                    let (schema, rows) = synth_pg_proc(self.active_catalog());
974                    materialise_meta_view(&mut catalog, view, schema, rows)?;
975                }
976                // v7.24 (round-16 D) — pg_catalog.pg_trigger. The
977                // round-16 "why doesn't prod fire the trigger"
978                // question was unanswerable because triggers had NO
979                // introspection surface; tgname/tgenabled plus the
980                // pragmatic relname/timing/events/function columns
981                // make "is it registered and enabled" a one-liner.
982                "__spg_pg_trigger" => {
983                    let (schema, rows) = synth_pg_trigger(self.active_catalog());
984                    materialise_meta_view(&mut catalog, view, schema, rows)?;
985                }
986                // v7.17.0 Phase 3.P0-52 — pg_catalog.pg_namespace
987                // (schema list for admin tools' tree views).
988                "__spg_pg_namespace" => {
989                    let (schema, rows) = synth_pg_namespace(self.active_catalog());
990                    materialise_meta_view(&mut catalog, view, schema, rows)?;
991                }
992                // v7.39 — pg_tables convenience view (was a pgwire
993                // canned response that ignored projections).
994                "__spg_pg_tables" => {
995                    let (schema, rows) =
996                        crate::system_catalog::synth_pg_tables(self.active_catalog());
997                    materialise_meta_view(&mut catalog, view, schema, rows)?;
998                }
999                // v7.37.24 (24.1) — pg_catalog.pg_enum (label list
1000                // for ENUM types; sqlx / ORM enum codecs read this).
1001                "__spg_pg_enum" => {
1002                    let (schema, rows) =
1003                        crate::system_catalog::synth_pg_enum(self.active_catalog());
1004                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1005                }
1006                // v7.37.21 (21.13) — pg_catalog.pg_replication_slots
1007                // (shape-stable empty until 21.12 persists slot state).
1008                // v7.39 (round 277) — session-scoped prepared statements.
1009                "__spg_pg_prepared_statements" => {
1010                    let (schema, rows) = crate::system_catalog::synth_pg_prepared_statements(
1011                        &self.prepared_statements,
1012                    );
1013                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1014                }
1015                "__spg_pg_replication_slots" => {
1016                    let (schema, rows) =
1017                        crate::system_catalog::synth_pg_replication_slots(self.active_catalog());
1018                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1019                }
1020                // v7.37.21 (21.13-b) — pg_catalog.pg_publication
1021                // (one row per CREATE PUBLICATION).
1022                "__spg_pg_publication" => {
1023                    let (schema, rows) = crate::system_catalog::synth_pg_publication(self);
1024                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1025                }
1026                // v7.37.21 (21.13-c) — pg_catalog.pg_subscription
1027                // (one row per CREATE SUBSCRIPTION; subconninfo
1028                // redacted so dashboards can't leak credentials).
1029                "__spg_pg_subscription" => {
1030                    let (schema, rows) = crate::system_catalog::synth_pg_subscription(self);
1031                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1032                }
1033                // v7.37.22 (22.x-stat-db) — pg_catalog.pg_stat_database
1034                // (one row for SPG's single database; counters are
1035                // shape-stable 0 until wiring lands).
1036                "__spg_pg_stat_database" => {
1037                    let (schema, rows) = crate::system_catalog::synth_pg_stat_database(
1038                        self,
1039                        self.stat_tup_inserted,
1040                        self.stat_tup_updated,
1041                        self.stat_tup_deleted,
1042                    );
1043                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1044                }
1045                // v7.37.22 (22.14) — pg_catalog.pg_stat_user_tables
1046                // (per-table churn counters; live_tup = row count).
1047                "__spg_pg_stat_user_tables" => {
1048                    // r192 — DML counters come from the engine-side
1049                    // non-transactional map, not the (tx-shadowed)
1050                    // catalog tables.
1051                    let (schema, rows) = crate::system_catalog::synth_pg_stat_user_tables(
1052                        self.active_catalog(),
1053                        &self.table_write_stats,
1054                    );
1055                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1056                }
1057                // v7.37.22 (22.15) — pg_catalog.pg_stat_user_indexes
1058                // (per-index usage counters; flag unused indexes).
1059                "__spg_pg_stat_user_indexes" => {
1060                    let (schema, rows) =
1061                        crate::system_catalog::synth_pg_stat_user_indexes(self.active_catalog());
1062                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1063                }
1064                // v7.37.22 (22.16) — pg_catalog.pg_stat_bgwriter.
1065                "__spg_pg_stat_bgwriter" => {
1066                    let (schema, rows) =
1067                        crate::system_catalog::synth_pg_stat_bgwriter(self.active_catalog());
1068                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1069                }
1070                // v7.38 (read01 P3.14) — pg_catalog.pg_stat_checkpointer /
1071                // pg_stat_wal shell views (shape-stable, counters pending).
1072                "__spg_pg_stat_checkpointer" => {
1073                    let (schema, rows) =
1074                        crate::system_catalog::synth_pg_stat_checkpointer(self.active_catalog());
1075                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1076                }
1077                "__spg_pg_stat_wal" => {
1078                    let (schema, rows) =
1079                        crate::system_catalog::synth_pg_stat_wal(self.active_catalog());
1080                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1081                }
1082                // v7.38 (read01 P3.15) — pg_catalog.pg_stat_slru /
1083                // pg_stat_subscription_stats shell views.
1084                "__spg_pg_stat_slru" => {
1085                    let (schema, rows) =
1086                        crate::system_catalog::synth_pg_stat_slru(self.active_catalog());
1087                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1088                }
1089                "__spg_pg_stat_subscription_stats" => {
1090                    let (schema, rows) = crate::system_catalog::synth_pg_stat_subscription_stats(
1091                        self.active_catalog(),
1092                    );
1093                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1094                }
1095                // v7.37.22 (22.17) — pg_catalog.pg_stat_archiver.
1096                "__spg_pg_stat_archiver" => {
1097                    let (schema, rows) =
1098                        crate::system_catalog::synth_pg_stat_archiver(self.active_catalog());
1099                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1100                }
1101                // v7.37.21 (21.13-d) — pg_catalog.pg_stat_replication.
1102                "__spg_pg_stat_replication" => {
1103                    let (schema, rows) =
1104                        crate::system_catalog::synth_pg_stat_replication(self.active_catalog());
1105                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1106                }
1107                // v7.37.24 (24.13) — pg_catalog.pg_am.
1108                "__spg_pg_am" => {
1109                    let (schema, rows) = crate::system_catalog::synth_pg_am(self.active_catalog());
1110                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1111                }
1112                // v7.37.22 (22.18) — pg_catalog.pg_stat_io (PG 16+).
1113                "__spg_pg_stat_io" => {
1114                    let (schema, rows) =
1115                        crate::system_catalog::synth_pg_stat_io(self.active_catalog());
1116                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1117                }
1118                // v7.37.22 (22.19) — pg_catalog.pg_stat_user_functions.
1119                "__spg_pg_stat_user_functions" => {
1120                    let (schema, rows) =
1121                        crate::system_catalog::synth_pg_stat_user_functions(self.active_catalog());
1122                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1123                }
1124                // v7.39 (round 287) — pg_catalog.pg_largeobject{,_metadata}.
1125                "__spg_pg_largeobject" => {
1126                    let (schema, rows) =
1127                        crate::system_catalog::synth_pg_largeobject(self.active_catalog());
1128                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1129                }
1130                "__spg_pg_largeobject_metadata" => {
1131                    let (schema, rows) =
1132                        crate::system_catalog::synth_pg_largeobject_metadata(self.active_catalog());
1133                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1134                }
1135                // v7.37.23 (23.7-a) — pg_catalog.pg_statistic_ext.
1136                "__spg_pg_statistic_ext" => {
1137                    let (schema, rows) =
1138                        crate::system_catalog::synth_pg_statistic_ext(self.active_catalog());
1139                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1140                }
1141                // v7.38.18 — pg_catalog.pg_stats, the readable view.
1142                "__spg_pg_stats" => {
1143                    let (schema, rows) = crate::system_catalog::synth_pg_stats(
1144                        self.active_catalog(),
1145                        &self.statistics,
1146                    );
1147                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1148                }
1149                // v7.37.24 (24.15) — pg_catalog.pg_statistic.
1150                "__spg_pg_statistic" => {
1151                    let (schema, rows) = crate::system_catalog::synth_pg_statistic(
1152                        self.active_catalog(),
1153                        &self.statistics,
1154                    );
1155                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1156                }
1157                // v7.37.22 (22.20) — pg_catalog.pg_stat_progress_vacuum.
1158                "__spg_pg_stat_progress_vacuum" => {
1159                    let (schema, rows) =
1160                        crate::system_catalog::synth_pg_stat_progress_vacuum(self.active_catalog());
1161                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1162                }
1163                // v7.37.22 (22.21) — pg_catalog.pg_stat_progress_create_index.
1164                "__spg_pg_stat_progress_create_index" => {
1165                    let (schema, rows) = crate::system_catalog::synth_pg_stat_progress_create_index(
1166                        self.active_catalog(),
1167                    );
1168                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1169                }
1170                // v7.37.22 (22.22) — pg_catalog.pg_stat_progress_analyze.
1171                "__spg_pg_stat_progress_analyze" => {
1172                    let (schema, rows) = crate::system_catalog::synth_pg_stat_progress_analyze(
1173                        self.active_catalog(),
1174                    );
1175                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1176                }
1177                // v7.37.24 (24.16) — pg_catalog.pg_inherits
1178                // (partition parent → child OID mapping).
1179                "__spg_pg_inherits" => {
1180                    let (schema, rows) =
1181                        crate::system_catalog::synth_pg_inherits(self.active_catalog());
1182                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1183                }
1184                // v7.39 (round 650) — the text-search catalogs, filled
1185                // with what SPG actually has rather than PG's thirty.
1186                "__spg_pg_ts_config_map" => {
1187                    let (schema, rows) =
1188                        crate::system_catalog::synth_pg_ts_config_map(self.active_catalog());
1189                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1190                }
1191                "__spg_pg_ts_config" => {
1192                    let (schema, rows) =
1193                        crate::system_catalog::synth_pg_ts_config(self.active_catalog());
1194                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1195                }
1196                "__spg_pg_ts_dict" => {
1197                    let (schema, rows) =
1198                        crate::system_catalog::synth_pg_ts_dict(self.active_catalog());
1199                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1200                }
1201                "__spg_pg_ts_parser" => {
1202                    let (schema, rows) =
1203                        crate::system_catalog::synth_pg_ts_parser(self.active_catalog());
1204                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1205                }
1206                "__spg_pg_ts_template" => {
1207                    let (schema, rows) =
1208                        crate::system_catalog::synth_pg_ts_template(self.active_catalog());
1209                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1210                }
1211                // v7.37.24 (24.17) — pg_catalog.pg_depend
1212                // (dependency graph; shape-stable empty since
1213                // SPG's drop enforcement is per-kind, not per-object).
1214                "__spg_pg_depend" => {
1215                    let (schema, rows) =
1216                        crate::system_catalog::synth_pg_depend(self.active_catalog());
1217                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1218                }
1219                // 7.38.1 S5.1 — pg_catalog.pg_opclass (pg_dump wall #1).
1220                "__spg_pg_opclass" => {
1221                    let (schema, rows) =
1222                        crate::system_catalog::synth_pg_opclass(self.active_catalog());
1223                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1224                }
1225                "__spg_pg_opfamily" => {
1226                    let (schema, rows) =
1227                        crate::system_catalog::synth_pg_opfamily(self.active_catalog());
1228                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1229                }
1230                "__spg_pg_amop" => {
1231                    let (schema, rows) =
1232                        crate::system_catalog::synth_pg_amop(self.active_catalog());
1233                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1234                }
1235                "__spg_pg_amproc" => {
1236                    let (schema, rows) =
1237                        crate::system_catalog::synth_pg_amproc(self.active_catalog());
1238                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1239                }
1240                // v7.38 (read01) — pg_catalog.pg_attrdef (column defaults;
1241                // ORM reflection + pg_dump read the deparsed default text).
1242                "__spg_pg_attrdef" => {
1243                    let (schema, rows) =
1244                        crate::system_catalog::synth_pg_attrdef(self.active_catalog());
1245                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1246                }
1247                // v7.39 (RLS) — pg_catalog.pg_policy (raw) + pg_policies (view).
1248                "__spg_pg_policy" => {
1249                    let (schema, rows) =
1250                        crate::system_catalog::synth_pg_policy(self.active_catalog());
1251                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1252                }
1253                "__spg_pg_policies" => {
1254                    let (schema, rows) =
1255                        crate::system_catalog::synth_pg_policies(self.active_catalog());
1256                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1257                }
1258                // v7.37.24 (24.14) — pg_catalog.pg_collation.
1259                "__spg_pg_collation" => {
1260                    let (schema, rows) =
1261                        crate::system_catalog::synth_pg_collation(self.active_catalog());
1262                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1263                }
1264                // v7.37.23 (23.6-b) — pg_catalog.pg_tablespace.
1265                "__spg_pg_tablespace" => {
1266                    let (schema, rows) =
1267                        crate::system_catalog::synth_pg_tablespace(self.active_catalog());
1268                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1269                }
1270                // v7.17.0 Phase 3.P0-53 — pg_catalog.pg_indexes view
1271                // for pgAdmin / DataGrip "indexes per table" listings.
1272                "__spg_pg_indexes" => {
1273                    let (schema, rows) = synth_pg_indexes(self.active_catalog());
1274                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1275                }
1276                // v7.39 (read01 round 50) — pg_catalog.pg_description, backing
1277                // psql's \d+ comment column and pg_dump's COMMENT ON emission.
1278                "__spg_pg_description" => {
1279                    let (schema, rows) =
1280                        crate::system_catalog::synth_pg_description(self.active_catalog());
1281                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1282                }
1283                // v7.17.0 Phase 3.P0-53 — pg_catalog.pg_index (raw)
1284                // for index introspection by ORM compilers.
1285                "__spg_pg_index" => {
1286                    let (schema, rows) = synth_pg_index_raw(self.active_catalog());
1287                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1288                }
1289                // v7.17.0 Phase 3.P0-54 — pg_catalog.pg_constraint
1290                // for FK / UNIQUE / PK / CHECK introspection.
1291                "__spg_pg_constraint" => {
1292                    let (schema, rows) = synth_pg_constraint(self.active_catalog());
1293                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1294                }
1295                // v7.37 U11 — pg_catalog.pg_sequence, one row per CREATE
1296                // SEQUENCE (psql \d <seq> + ORM sequence introspection).
1297                "__spg_pg_sequence" => {
1298                    let (schema, rows) = synth_pg_sequence(self.active_catalog());
1299                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1300                }
1301                // v7.17.0 Phase 3.P0-55 — pg_catalog.pg_database /
1302                // pg_roles / pg_user. SPG is single-database so
1303                // pg_database surfaces just `postgres`; pg_roles
1304                // / pg_user walk the engine's UserStore.
1305                "__spg_pg_database" => {
1306                    let (schema, rows) = synth_pg_database(self);
1307                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1308                }
1309                "__spg_pg_roles" => {
1310                    let (schema, rows) = synth_pg_roles(self);
1311                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1312                }
1313                // v7.39 (round 542) — pg_user is a DIFFERENT view over the
1314                // same roles, with PG's own `use*` column names. It used to
1315                // publish pg_roles' columns under this name.
1316                "__spg_pg_user" => {
1317                    let (schema, rows) = crate::system_catalog::synth_pg_user(self);
1318                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1319                }
1320                // v7.39 (read01 round 58) — role membership.
1321                "__spg_pg_auth_members" => {
1322                    let (schema, rows) = crate::system_catalog::synth_pg_auth_members(self);
1323                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1324                }
1325                // v7.17.0 Phase 3.P0-56 — pg_catalog.pg_views. PG's
1326                // pg_views surfaces every CREATE VIEW result; SPG
1327                // ships one row per declared view from the catalog.
1328                "__spg_pg_views" => {
1329                    let (schema, rows) = synth_pg_views(self.active_catalog());
1330                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1331                }
1332                // v7.39 (round 143) — pg_catalog.pg_rules: one row per
1333                // catalogued query-rewrite RULE.
1334                "__spg_pg_rules" => {
1335                    let (schema, rows) =
1336                        crate::system_catalog::synth_pg_rules(self.active_catalog());
1337                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1338                }
1339                // v7.39 (round 312) — pg_catalog.pg_rewrite: the rule
1340                // catalogue `pg_get_ruledef(oid)` resolves against.
1341                "__spg_pg_rewrite" => {
1342                    let (schema, rows) =
1343                        crate::system_catalog::synth_pg_rewrite(self.active_catalog());
1344                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1345                }
1346                // v7.39 (round 542) — pg_catalog.pg_matviews, with rows
1347                // and PG's own column names.
1348                "__spg_pg_matviews" => {
1349                    let (schema, rows) =
1350                        crate::system_catalog::synth_pg_matviews(self.active_catalog());
1351                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1352                }
1353                // pg_catalog.pg_extension — native capability list
1354                // (mailrs embed round-12).
1355                // v7.39 (round 546) — the catalogs SPG has real content
1356                // for, from the facts it already holds.
1357                "__spg_pg_db_role_setting" => {
1358                    let (schema, rows) = crate::system_catalog::synth_pg_db_role_setting(self);
1359                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1360                }
1361                "__spg_pg_language" => {
1362                    let (schema, rows) = crate::system_catalog::synth_pg_language();
1363                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1364                }
1365                "__spg_pg_sequences" => {
1366                    let (schema, rows) =
1367                        crate::system_catalog::synth_pg_sequences(self.active_catalog());
1368                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1369                }
1370                "__spg_pg_range" => {
1371                    let (schema, rows) = crate::system_catalog::synth_pg_range();
1372                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1373                }
1374                "__spg_pg_partitioned_table" => {
1375                    let (schema, rows) =
1376                        crate::system_catalog::synth_pg_partitioned_table(self.active_catalog());
1377                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1378                }
1379                "__spg_pg_authid" => {
1380                    let (schema, rows) = crate::system_catalog::synth_pg_authid(self);
1381                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1382                }
1383                "__spg_pg_group" => {
1384                    let (schema, rows) = crate::system_catalog::synth_pg_group(self);
1385                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1386                }
1387                "__spg_pg_shadow" => {
1388                    let (schema, rows) = crate::system_catalog::synth_pg_shadow(self);
1389                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1390                }
1391                // v7.39 (round 544) — pg_cast, probed from the real
1392                // cast implementation.
1393                "__spg_pg_cast" => {
1394                    let (schema, rows) = crate::system_catalog::synth_pg_cast();
1395                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1396                }
1397                // v7.39 (round 541) — an empty catalog that exists.
1398                "__spg_pg_foreign_table" => {
1399                    let (schema, rows) = crate::system_catalog::synth_pg_foreign_table();
1400                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1401                }
1402                "__spg_pg_extension" => {
1403                    let (schema, rows) = synth_pg_extension();
1404                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1405                }
1406                // v7.39 (round 502) — the timezone catalogues.
1407                "__spg_pg_timezone_names" => {
1408                    let (schema, rows) = synth_pg_timezone_names(self);
1409                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1410                }
1411                "__spg_pg_timezone_abbrevs" => {
1412                    let (schema, rows) = synth_pg_timezone_abbrevs(self);
1413                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1414                }
1415                // v7.17.0 Phase 3.P0-57 — pg_catalog.pg_settings.
1416                "__spg_pg_settings" => {
1417                    let (schema, rows) = synth_pg_settings(self);
1418                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1419                }
1420                // v7.17.0 Phase 3.P0-63 — information_schema.KEY_COLUMN_USAGE.
1421                // v7.39 (read01 round 51) — information_schema.role_table_grants
1422                // and .table_privileges. Both report the owner's seven implicit
1423                // table privileges; SPG's single role owns everything.
1424                // v7.39 (read01 round 59) — information_schema.column_privileges.
1425                "__spg_info_column_privileges" => {
1426                    let (schema, rows) =
1427                        crate::system_catalog::synth_info_column_privileges(self.active_catalog());
1428                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1429                }
1430                "__spg_info_role_table_grants" | "__spg_info_table_privileges" => {
1431                    let grantee = self.current_role().to_string();
1432                    let (schema, rows) = crate::system_catalog::synth_info_role_table_grants(
1433                        self.active_catalog(),
1434                        &grantee,
1435                    );
1436                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1437                }
1438                "__spg_info_key_column_usage" => {
1439                    // v7.39.11 — the session's dialect decides the
1440                    // column list; see the synthesiser.
1441                    let mysql = self.in_mysql_dialect();
1442                    let (schema, rows) = synth_info_key_column_usage(self.active_catalog(), mysql);
1443                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1444                }
1445                // v7.17.0 Phase 3.P0-64 — information_schema.REFERENTIAL_CONSTRAINTS.
1446                "__spg_info_referential_constraints" => {
1447                    let (schema, rows) = synth_info_referential_constraints(self.active_catalog());
1448                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1449                }
1450                // v7.17.0 Phase 3.P0-64 — information_schema.STATISTICS.
1451                "__spg_info_statistics" => {
1452                    let (schema, rows) = synth_info_statistics(self.active_catalog());
1453                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1454                }
1455                // v7.17.0 Phase 3.P0-64 — information_schema.ROUTINES.
1456                "__spg_info_routines" => {
1457                    let (schema, rows) = synth_info_routines();
1458                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1459                }
1460                // v7.37.24 (24.3) — information_schema.attributes.
1461                "__spg_info_attributes" => {
1462                    let (schema, rows) = crate::system_catalog::synth_information_schema_attributes(
1463                        self.active_catalog(),
1464                    );
1465                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1466                }
1467                // v7.37.24 (24.2) — information_schema.domains.
1468                "__spg_info_domains" => {
1469                    let (schema, rows) = crate::system_catalog::synth_information_schema_domains(
1470                        self.active_catalog(),
1471                    );
1472                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1473                }
1474                // v7.37.24 (24.9) — information_schema.schemata.
1475                "__spg_info_schemata" => {
1476                    let (schema, rows) = crate::system_catalog::synth_information_schema_schemata(
1477                        self.active_catalog(),
1478                        self.speaks_mysql,
1479                        &self.listed_database_names(),
1480                    );
1481                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1482                }
1483                // v7.37.24 (24.9) — information_schema.views.
1484                "__spg_info_views" => {
1485                    let (schema, rows) = crate::system_catalog::synth_information_schema_views(
1486                        self.active_catalog(),
1487                    );
1488                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1489                }
1490                // v7.37.24 (24.9) — information_schema.table_constraints.
1491                "__spg_info_table_constraints" => {
1492                    let (schema, rows) =
1493                        crate::system_catalog::synth_information_schema_table_constraints(
1494                            self.active_catalog(),
1495                            self.speaks_mysql,
1496                        );
1497                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1498                }
1499                // v7.37.17 — information_schema.constraint_column_usage.
1500                "__spg_info_constraint_column_usage" => {
1501                    let (schema, rows) = crate::system_catalog::synth_info_constraint_column_usage(
1502                        self.active_catalog(),
1503                    );
1504                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1505                }
1506                // v7.37.17 — information_schema.triggers.
1507                "__spg_info_triggers" => {
1508                    let (schema, rows) =
1509                        crate::system_catalog::synth_info_triggers(self.active_catalog());
1510                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1511                }
1512                // v7.37.17 — information_schema.check_constraints.
1513                "__spg_info_check_constraints" => {
1514                    let (schema, rows) =
1515                        crate::system_catalog::synth_info_check_constraints(self.active_catalog());
1516                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1517                }
1518                // v7.37.17 — information_schema.sequences.
1519                "__spg_info_sequences" => {
1520                    let (schema, rows) =
1521                        crate::system_catalog::synth_info_sequences(self.active_catalog());
1522                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1523                }
1524                // v7.17.0 Phase 3.P0-65 — mysql.user / mysql.db.
1525                "__spg_mysql_user" => {
1526                    let (schema, rows) = synth_mysql_user(self);
1527                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1528                }
1529                "__spg_mysql_db" => {
1530                    let (schema, rows) = synth_mysql_db();
1531                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1532                }
1533                // v7.39 (round 541) — the catalogs PG has that SPG is
1534                // genuinely empty of. Table-driven; see EMPTY_PG_CATALOGS.
1535                other if crate::system_catalog::synth_empty_pg_catalog(other).is_some() => {
1536                    let (schema, rows) =
1537                        crate::system_catalog::synth_empty_pg_catalog(other).expect("just checked");
1538                    materialise_meta_view(&mut catalog, view, schema, rows)?;
1539                }
1540                _ => {
1541                    return Err(EngineError::Unsupported(alloc::format!(
1542                        "meta view {view:?} is not yet materialisable; \
1543                         v7.16.2 covers information_schema.columns / .tables \
1544                         and pg_catalog.pg_class / pg_attribute; \
1545                         v7.17.0 P0-50..P0-57 add pg_type / pg_proc / pg_namespace / \
1546                         pg_indexes / pg_index / pg_constraint / pg_database / pg_roles / \
1547                         pg_user / pg_views / pg_matviews / pg_settings"
1548                    )));
1549                }
1550            }
1551        }
1552        Ok(catalog)
1553    }
1554
1555    pub(crate) fn exec_with_ctes(
1556        &self,
1557        stmt: &SelectStatement,
1558        cancel: CancelToken<'_>,
1559    ) -> Result<QueryResult, EngineError> {
1560        cancel.check()?;
1561        // v7.37.43-T4.4 — `&self` SELECT path: only read-only CTE
1562        // bodies are supported here. Writable CTEs on a SELECT
1563        // outer require `&mut self` and route through the
1564        // top-level `exec_select_cancel_mut` entry; sentori
1565        // 0065's WITH-INSERT-INSERT shape comes in as a top-level
1566        // INSERT, not a SELECT, so this restriction is harmless
1567        // in practice.
1568        if stmt.ctes.iter().any(|c| c.body.is_modifying()) {
1569            // v7.39 (read01 round 81) — PG's wording. A data-modifying CTE
1570            // (`WITH d AS (DELETE … RETURNING …) …`) is only legal at the top
1571            // of a statement, not nested inside a subquery; this path is
1572            // reached exactly when one is nested. The old text described SPG's
1573            // own executor plumbing ("the top-level mutable entry"), which
1574            // means nothing to a client.
1575            return Err(EngineError::Unsupported(
1576                "WITH clause containing a data-modifying statement must be at the top level".into(),
1577            ));
1578        }
1579        let catalog = self.materialise_ctes_readonly(&stmt.ctes, cancel)?;
1580        // Strip CTEs from the body before running on the temp engine
1581        // so we don't recurse forever.
1582        let mut body = stmt.clone();
1583        body.ctes = Vec::new();
1584        let mut temp = Engine::restore(catalog);
1585        if let Some(c) = self.clock {
1586            temp = temp.with_clock(c);
1587        }
1588        if let Some(f) = self.salt_fn {
1589            temp = temp.with_salt_fn(f);
1590        }
1591        temp.exec_select_cancel(&body, cancel)
1592    }
1593
1594    /// v7.37.43-T4.4 — read-only CTE materialiser used by the
1595    /// `&self` SELECT path. Caller guarantees no modifying CTE
1596    /// bodies are present.
1597    pub(crate) fn materialise_ctes_readonly(
1598        &self,
1599        ctes: &[spg_sql::ast::Cte],
1600        cancel: CancelToken<'_>,
1601    ) -> Result<crate::Catalog, EngineError> {
1602        cancel.check()?;
1603        let mut catalog = self.active_catalog().clone();
1604        for cte in ctes {
1605            let body_select = cte.body.as_select().ok_or_else(|| {
1606                EngineError::Unsupported(alloc::format!(
1607                    "data-modifying CTE not supported on this SELECT entry"
1608                ))
1609            })?;
1610            // v7.39 (round 156) — a CTE may SHADOW a same-named real table
1611            // (PG scoping: the WITH name wins for the outer query and later
1612            // CTEs, while THIS body still sees the real table — a
1613            // non-recursive body's self-name is the table, probe P2). This
1614            // materialiser works on a CLONE, so the shadow is simply: run
1615            // the body against the untouched clone, then drop the real
1616            // table from the clone before installing the CTE's temp. A
1617            // RECURSIVE self-reference is the CTE itself (P6), so there the
1618            // drop happens before the iterating materialiser runs.
1619            let (columns, rows) = if cte.recursive && select_refers_to(body_select, &cte.name) {
1620                let synthetic = spg_sql::ast::Cte {
1621                    name: cte.name.clone(),
1622                    body: spg_sql::ast::CteBody::Select(body_select.clone()),
1623                    recursive: true,
1624                    column_overrides: cte.column_overrides.clone(),
1625                    search: None,
1626                    cycle: None,
1627                };
1628                if catalog.get(&cte.name).is_some() {
1629                    let _ = catalog.drop_table(&cte.name);
1630                }
1631                self.materialise_recursive_cte(&synthetic, &catalog, cancel)?
1632            } else {
1633                let mut cte_engine = Engine::restore(catalog.clone());
1634                if let Some(c) = self.clock {
1635                    cte_engine = cte_engine.with_clock(c);
1636                }
1637                if let Some(f) = self.salt_fn {
1638                    cte_engine = cte_engine.with_salt_fn(f);
1639                }
1640                let body_result = cte_engine.exec_select_cancel(body_select, cancel)?;
1641                let QueryResult::Rows { columns, rows } = body_result else {
1642                    return Err(EngineError::Unsupported(alloc::format!(
1643                        "CTE {:?} body did not return rows",
1644                        cte.name
1645                    )));
1646                };
1647                (columns, rows)
1648            };
1649            let inferred = infer_column_types(&columns, &rows);
1650            let mut columns = inferred;
1651            if !cte.column_overrides.is_empty() {
1652                if cte.column_overrides.len() != columns.len() {
1653                    return Err(EngineError::Unsupported(alloc::format!(
1654                        "CTE {:?} column list has {} names but body returns {} columns",
1655                        cte.name,
1656                        cte.column_overrides.len(),
1657                        columns.len()
1658                    )));
1659                }
1660                for (col, name) in columns.iter_mut().zip(cte.column_overrides.iter()) {
1661                    col.name.clone_from(name);
1662                }
1663            }
1664            let schema = TableSchema::new(cte.name.clone(), columns);
1665            // v7.39 (round 156) — the body ran against the untouched clone;
1666            // from here on the CTE name resolves to the temp (PG scoping).
1667            if catalog.get(&cte.name).is_some() {
1668                let _ = catalog.drop_table(&cte.name);
1669            }
1670            catalog.create_table(schema).map_err(EngineError::Storage)?;
1671            let table = catalog
1672                .get_mut(&cte.name)
1673                .expect("just-created CTE table must exist");
1674            for row in rows {
1675                table.insert(row).map_err(EngineError::Storage)?;
1676            }
1677        }
1678        Ok(catalog)
1679    }
1680
1681    /// v7.37.43-T4.4 — shared CTE materialiser (mutable variant).
1682    /// Retained for non-DML callers; the DML path (writable CTE on
1683    /// INSERT/UPDATE/DELETE outer) uses `run_with_cte_temps` in
1684    /// `dml.rs` which installs the CTE temps directly on the
1685    /// active catalog so the outer statement's writes hit real
1686    /// tables.
1687    #[allow(dead_code)]
1688    pub(crate) fn materialise_ctes(
1689        &mut self,
1690        ctes: &[spg_sql::ast::Cte],
1691        cancel: CancelToken<'_>,
1692    ) -> Result<crate::Catalog, EngineError> {
1693        cancel.check()?;
1694        // v7.37.43-T4.4 — modifying CTEs need to write through the
1695        // SAME catalog as the outer statement, not a clone (PG's
1696        // writable CTE puts all modifications in one transaction).
1697        // For the read-only case the original logic cloned, but
1698        // since the outer statement also goes through the cloned
1699        // engine and ALL writes must converge, we now drive the
1700        // accumulator off `self.active_catalog().clone()` and
1701        // commit the modifying writes directly to `self`'s active
1702        // catalog so the surface is consistent.
1703        let mut catalog = self.active_catalog().clone();
1704        // v7.39 (round 149) — a modifying CTE body's target must be a
1705        // real relation, never a sibling CTE (PG: relation does not
1706        // exist); checked before any alias lands in the accumulator.
1707        for cte in ctes {
1708            let body_target = match &cte.body {
1709                spg_sql::ast::CteBody::Select(_) => None,
1710                spg_sql::ast::CteBody::Insert(i) => Some(i.table.as_str()),
1711                spg_sql::ast::CteBody::Update(u) => Some(u.table.as_str()),
1712                spg_sql::ast::CteBody::Delete(d) => Some(d.table.as_str()),
1713                spg_sql::ast::CteBody::Merge(m) => Some(m.target.as_str()),
1714            };
1715            if let Some(t) = body_target
1716                && ctes.iter().any(|c| c.name.eq_ignore_ascii_case(t))
1717                && catalog.get(t).is_none()
1718            {
1719                return Err(EngineError::Storage(
1720                    spg_storage::StorageError::TableNotFound { name: t.into() },
1721                ));
1722            }
1723        }
1724        for cte in ctes {
1725            if catalog.get(&cte.name).is_some() {
1726                return Err(EngineError::Unsupported(alloc::format!(
1727                    "CTE name {:?} shadows an existing table; rename the CTE",
1728                    cte.name
1729                )));
1730            }
1731            let (columns, rows) = match &cte.body {
1732                // v7.39 (round 145) — see the sibling site: only a body that
1733                // truly self-references takes the iterating materialiser.
1734                spg_sql::ast::CteBody::Select(body)
1735                    if cte.recursive && select_refers_to(body, &cte.name) =>
1736                {
1737                    // Recursive CTE — the existing helper takes a
1738                    // SELECT body and the snapshot catalog.
1739                    let synthetic = spg_sql::ast::Cte {
1740                        name: cte.name.clone(),
1741                        body: spg_sql::ast::CteBody::Select(body.clone()),
1742                        recursive: true,
1743                        column_overrides: cte.column_overrides.clone(),
1744                        search: None,
1745                        cycle: None,
1746                    };
1747                    self.materialise_recursive_cte(&synthetic, &catalog, cancel)?
1748                }
1749                spg_sql::ast::CteBody::Select(body) => {
1750                    // v7.25 (round-17) — run against the accumulated
1751                    // catalog so later CTEs can reference earlier
1752                    // ones in the same WITH clause.
1753                    let mut cte_engine = Engine::restore(catalog.clone());
1754                    if let Some(c) = self.clock {
1755                        cte_engine = cte_engine.with_clock(c);
1756                    }
1757                    if let Some(f) = self.salt_fn {
1758                        cte_engine = cte_engine.with_salt_fn(f);
1759                    }
1760                    let body_result = cte_engine.exec_select_cancel(body, cancel)?;
1761                    let QueryResult::Rows { columns, rows } = body_result else {
1762                        return Err(EngineError::Unsupported(alloc::format!(
1763                            "CTE {:?} body did not return rows",
1764                            cte.name
1765                        )));
1766                    };
1767                    (columns, rows)
1768                }
1769                spg_sql::ast::CteBody::Insert(body) => {
1770                    self.exec_modifying_cte_insert(&cte.name, body, cancel)?
1771                }
1772                spg_sql::ast::CteBody::Update(body) => {
1773                    self.exec_modifying_cte_update(&cte.name, body, cancel)?
1774                }
1775                spg_sql::ast::CteBody::Delete(body) => {
1776                    self.exec_modifying_cte_delete(&cte.name, body, cancel)?
1777                }
1778                spg_sql::ast::CteBody::Merge(body) => {
1779                    self.exec_modifying_cte_merge(&cte.name, body, cancel)?
1780                }
1781            };
1782            // v4.22: the projection builder labels any non-column
1783            // expression as Text — including literal SELECT 1.
1784            // Promote each column's type to whatever the rows
1785            // actually carry so the CTE storage table accepts them.
1786            let inferred = infer_column_types(&columns, &rows);
1787            let mut columns = inferred;
1788            if !cte.column_overrides.is_empty() {
1789                if cte.column_overrides.len() != columns.len() {
1790                    return Err(EngineError::Unsupported(alloc::format!(
1791                        "CTE {:?} column list has {} names but body returns {} columns",
1792                        cte.name,
1793                        cte.column_overrides.len(),
1794                        columns.len()
1795                    )));
1796                }
1797                for (col, name) in columns.iter_mut().zip(cte.column_overrides.iter()) {
1798                    col.name.clone_from(name);
1799                }
1800            }
1801            let schema = TableSchema::new(cte.name.clone(), columns);
1802            catalog.create_table(schema).map_err(EngineError::Storage)?;
1803            let table = catalog
1804                .get_mut(&cte.name)
1805                .expect("just-created CTE table must exist");
1806            for row in rows {
1807                table.insert(row).map_err(EngineError::Storage)?;
1808            }
1809        }
1810        Ok(catalog)
1811    }
1812
1813    /// v7.37.43-T4.4 — execute an INSERT CTE body. Runs the INSERT
1814    /// against `self` (so the mutation lands in the active catalog
1815    /// inside the current transaction) and captures the RETURNING
1816    /// projection — column schema + rows — to materialise as the
1817    /// CTE alias's table. An INSERT without RETURNING produces a
1818    /// 0-row table with a synthetic single-column placeholder
1819    /// (matches PG: the CTE alias is still defined, but referencing
1820    /// it from the outer query without RETURNING raises a
1821    /// column-resolution error at scan time).
1822    fn exec_modifying_cte_insert(
1823        &mut self,
1824        cte_name: &str,
1825        body: &spg_sql::ast::InsertStatement,
1826        _cancel: CancelToken<'_>,
1827    ) -> Result<
1828        (
1829            Vec<spg_storage::ColumnSchema>,
1830            Vec<spg_storage::Row<'static>>,
1831        ),
1832        EngineError,
1833    > {
1834        // round 151 — a WITH-headed body keeps its own ctes; the body
1835        // statement routes through its writable-CTE entry (outer CTEs
1836        // are never copied into bodies, so no recursion risk).
1837        let body = body.clone();
1838        let result = self.exec_insert(body)?;
1839        match result {
1840            QueryResult::Rows { columns, rows } => Ok((columns, rows)),
1841            QueryResult::CommandOk { .. } => {
1842                // No RETURNING — emit a sentinel single-column
1843                // schema with zero rows so the alias is defined.
1844                let placeholder = spg_storage::ColumnSchema::new(
1845                    alloc::format!("{cte_name}_returning_absent"),
1846                    spg_storage::DataType::Text,
1847                    true,
1848                );
1849                Ok((alloc::vec![placeholder], Vec::new()))
1850            }
1851        }
1852    }
1853
1854    /// v7.37.43-T4.4 — execute an UPDATE CTE body, same semantics
1855    /// as INSERT above.
1856    fn exec_modifying_cte_update(
1857        &mut self,
1858        cte_name: &str,
1859        body: &spg_sql::ast::UpdateStatement,
1860        cancel: CancelToken<'_>,
1861    ) -> Result<
1862        (
1863            Vec<spg_storage::ColumnSchema>,
1864            Vec<spg_storage::Row<'static>>,
1865        ),
1866        EngineError,
1867    > {
1868        let body = body.clone();
1869        let result = self.exec_update_cancel(&body, cancel)?;
1870        match result {
1871            QueryResult::Rows { columns, rows } => Ok((columns, rows)),
1872            QueryResult::CommandOk { .. } => {
1873                let placeholder = spg_storage::ColumnSchema::new(
1874                    alloc::format!("{cte_name}_returning_absent"),
1875                    spg_storage::DataType::Text,
1876                    true,
1877                );
1878                Ok((alloc::vec![placeholder], Vec::new()))
1879            }
1880        }
1881    }
1882
1883    /// v7.37.43-T4.4 — execute a DELETE CTE body.
1884    fn exec_modifying_cte_delete(
1885        &mut self,
1886        cte_name: &str,
1887        body: &spg_sql::ast::DeleteStatement,
1888        cancel: CancelToken<'_>,
1889    ) -> Result<
1890        (
1891            Vec<spg_storage::ColumnSchema>,
1892            Vec<spg_storage::Row<'static>>,
1893        ),
1894        EngineError,
1895    > {
1896        let body = body.clone();
1897        let result = self.exec_delete_cancel(&body, cancel)?;
1898        match result {
1899            QueryResult::Rows { columns, rows } => Ok((columns, rows)),
1900            QueryResult::CommandOk { .. } => {
1901                let placeholder = spg_storage::ColumnSchema::new(
1902                    alloc::format!("{cte_name}_returning_absent"),
1903                    spg_storage::DataType::Text,
1904                    true,
1905                );
1906                Ok((alloc::vec![placeholder], Vec::new()))
1907            }
1908        }
1909    }
1910
1911    /// v7.39 (round 149) — execute a MERGE CTE body (PG 17).
1912    fn exec_modifying_cte_merge(
1913        &mut self,
1914        cte_name: &str,
1915        body: &spg_sql::ast::MergeStatement,
1916        cancel: CancelToken<'_>,
1917    ) -> Result<
1918        (
1919            Vec<spg_storage::ColumnSchema>,
1920            Vec<spg_storage::Row<'static>>,
1921        ),
1922        EngineError,
1923    > {
1924        let body = body.clone();
1925        let result = self.exec_merge_cancel(&body, cancel)?;
1926        match result {
1927            QueryResult::Rows { columns, rows } => Ok((columns, rows)),
1928            QueryResult::CommandOk { .. } => {
1929                let placeholder = spg_storage::ColumnSchema::new(
1930                    alloc::format!("{cte_name}_returning_absent"),
1931                    spg_storage::DataType::Text,
1932                    true,
1933                );
1934                Ok((alloc::vec![placeholder], Vec::new()))
1935            }
1936        }
1937    }
1938
1939    /// v4.22: materialise a WITH RECURSIVE CTE. The body must be a
1940    /// UNION (or UNION ALL) of an anchor that does not reference
1941    /// the CTE name, and one or more recursive terms that do. The
1942    /// anchor runs first; each subsequent iteration runs the
1943    /// recursive term against a temp catalog where the CTE name is
1944    /// bound to the *previous* iteration's output. Iteration stops
1945    /// when the recursive term yields no rows; UNION (DISTINCT)
1946    /// deduplicates against the accumulated result, UNION ALL does
1947    /// not. A hard cap on total rows prevents runaway queries.
1948    #[allow(clippy::too_many_lines)]
1949    pub(crate) fn materialise_recursive_cte(
1950        &self,
1951        cte: &spg_sql::ast::Cte,
1952        base_catalog: &Catalog,
1953        cancel: CancelToken<'_>,
1954    ) -> Result<(Vec<ColumnSchema>, Vec<Row<'static>>), EngineError> {
1955        const MAX_TOTAL_ROWS: usize = 1_000_000;
1956        const MAX_ITERATIONS: usize = 100_000;
1957        cancel.check()?;
1958        // v7.37.43-T4.4 — RECURSIVE only supports SELECT bodies;
1959        // a modifying recursive CTE is parser-rejectable but we
1960        // guard here defensively.
1961        let body_select = cte.body.as_select().ok_or_else(|| {
1962            EngineError::Unsupported(alloc::format!(
1963                "WITH RECURSIVE {:?} body must be a SELECT, not a data-modifying statement",
1964                cte.name
1965            ))
1966        })?;
1967        if body_select.unions.is_empty() {
1968            return Err(EngineError::Unsupported(alloc::format!(
1969                "WITH RECURSIVE {:?} body must be a UNION of an anchor and a recursive term",
1970                cte.name
1971            )));
1972        }
1973        // Anchor: the body's leading SELECT, with unions stripped.
1974        let mut anchor = body_select.clone();
1975        let all_union_terms = core::mem::take(&mut anchor.unions);
1976        anchor.ctes = Vec::new();
1977        // v7.37 D.42 — split the UNION members: those that do NOT reference the
1978        // CTE are additional ANCHOR terms, only the ones that do recurse. A
1979        // multi-row VALUES seed lowers to `SELECT r1 UNION ALL SELECT r2 UNION
1980        // ALL <recursive>`, so the leading SELECT alone is not the whole anchor —
1981        // treating the non-recursive `SELECT r2` as a recursive term made it
1982        // re-emit its constant row every iteration → runaway loop.
1983        let (anchor_terms, union_terms): (Vec<_>, Vec<_>) = all_union_terms
1984            .into_iter()
1985            .partition(|(_, t)| !select_refers_to(t, &cte.name));
1986        let anchor_result = self.exec_select_cancel(&anchor, cancel)?;
1987        let QueryResult::Rows {
1988            columns: anchor_cols,
1989            rows: mut anchor_rows,
1990        } = anchor_result
1991        else {
1992            return Err(EngineError::Unsupported(alloc::format!(
1993                "WITH RECURSIVE {:?}: anchor did not return rows",
1994                cte.name
1995            )));
1996        };
1997        // Append every non-recursive UNION member's rows to the anchor set.
1998        for (_, term) in &anchor_terms {
1999            let mut term = term.clone();
2000            term.ctes = Vec::new();
2001            if let QueryResult::Rows { rows, .. } = self.exec_select_cancel(&term, cancel)? {
2002                anchor_rows.extend(rows);
2003            }
2004        }
2005        // The projection builder labels non-column expressions Text;
2006        // refine column types from the anchor's actual values so the
2007        // intermediate iter-catalog tables accept them.
2008        let mut columns = infer_column_types(&anchor_cols, &anchor_rows);
2009        if !cte.column_overrides.is_empty() {
2010            if cte.column_overrides.len() != columns.len() {
2011                return Err(EngineError::Unsupported(alloc::format!(
2012                    "CTE {:?} column list has {} names but anchor returns {} columns",
2013                    cte.name,
2014                    cte.column_overrides.len(),
2015                    columns.len()
2016                )));
2017            }
2018            for (col, name) in columns.iter_mut().zip(cte.column_overrides.iter()) {
2019                col.name.clone_from(name);
2020            }
2021        }
2022        let mut all_rows: Vec<Row<'static>> = anchor_rows.clone();
2023        let mut working_set: Vec<Row<'static>> = anchor_rows;
2024        let mut seen: alloc::collections::BTreeSet<Vec<u8>> = alloc::collections::BTreeSet::new();
2025        // Track at least one "all UNION ALL" flag — if every union
2026        // kind is ALL we skip the dedup step (faster + matches PG).
2027        let all_union_all = union_terms.iter().all(|(k, _)| matches!(k, UnionKind::All));
2028        if !all_union_all {
2029            for r in &all_rows {
2030                seen.insert(encode_row_key(r));
2031            }
2032        }
2033        // v7.39 (round 598) — the engine and its catalog are built ONCE.
2034        // Each iteration used to clone the catalog, create the CTE table,
2035        // and construct a whole `Engine` — which initialises 82 fields — to
2036        // hold that round's working set. A counting allocator put the loop
2037        // at 63 allocations and 104 kB per iteration, or 1 GB for a
2038        // 10,000-row recursive CTE, and none of it varied with how much
2039        // else was in the catalog: the per-round rebuild WAS the cost. The
2040        // table is emptied and refilled instead.
2041        let mut iter_catalog = base_catalog.clone();
2042        let schema = TableSchema::new(cte.name.clone(), columns.clone());
2043        iter_catalog
2044            .create_table(schema)
2045            .map_err(EngineError::Storage)?;
2046        let mut iter_engine = Engine::restore(iter_catalog);
2047        if let Some(c) = self.clock {
2048            iter_engine = iter_engine.with_clock(c);
2049        }
2050        if let Some(f) = self.salt_fn {
2051            iter_engine = iter_engine.with_salt_fn(f);
2052        }
2053        // The recursive terms are cloned once too — the clone stripped the
2054        // CTE list off each of them, per term per iteration.
2055        let recursive_terms: Vec<SelectStatement> = union_terms
2056            .iter()
2057            .map(|(_, t)| {
2058                let mut t = t.clone();
2059                t.ctes = Vec::new();
2060                t
2061            })
2062            .collect();
2063        // v7.39 (round 618) — plan every recursive term once. Taken only if
2064        // ALL of them plan, so a query never runs half on each path.
2065        let term_plans: Option<Vec<RecursiveTermPlan<'_>>> = recursive_terms
2066            .iter()
2067            .map(|t| plan_recursive_term(t, &cte.name, columns.len()))
2068            .collect();
2069        let fast_ctx = term_plans.as_ref().map(|plans| {
2070            let alias = plans[0].alias.clone();
2071            (alias, ())
2072        });
2073        for iter in 0..MAX_ITERATIONS {
2074            cancel.check()?;
2075            if working_set.is_empty() {
2076                break;
2077            }
2078            if let (Some(plans), Some((_, ()))) = (term_plans.as_ref(), fast_ctx.as_ref()) {
2079                // The worktable IS the working set: no table to empty and
2080                // refill, and no query execution per round.
2081                let mut next_set: Vec<Row<'static>> = Vec::new();
2082                for plan in plans {
2083                    let ctx = self.ev_ctx(&columns, Some(&plan.alias));
2084                    for row in &working_set {
2085                        cancel.check()?;
2086                        if let Some(w) = plan.where_ {
2087                            let v = eval::eval_expr(w, row, &ctx).map_err(EngineError::Eval)?;
2088                            if !matches!(v, Value::Bool(true)) {
2089                                continue;
2090                            }
2091                        }
2092                        let mut vals: Vec<Value<'static>> = Vec::with_capacity(plan.items.len());
2093                        for it in &plan.items {
2094                            vals.push(eval::eval_expr(it, row, &ctx).map_err(EngineError::Eval)?);
2095                        }
2096                        let out = Row::new(vals);
2097                        if !all_union_all {
2098                            let key = encode_row_key(&out);
2099                            if !seen.insert(key) {
2100                                continue;
2101                            }
2102                        }
2103                        next_set.push(out);
2104                    }
2105                }
2106                if next_set.is_empty() {
2107                    break;
2108                }
2109                all_rows.extend(next_set.iter().cloned());
2110                working_set = next_set;
2111                if all_rows.len() > MAX_TOTAL_ROWS {
2112                    return Err(EngineError::Unsupported(alloc::format!(
2113                        "WITH RECURSIVE {:?}: produced more than {MAX_TOTAL_ROWS} rows — likely runaway recursion",
2114                        cte.name
2115                    )));
2116                }
2117                if iter + 1 == MAX_ITERATIONS {
2118                    return Err(EngineError::Unsupported(alloc::format!(
2119                        "WITH RECURSIVE {:?}: exceeded {MAX_ITERATIONS} iterations",
2120                        cte.name
2121                    )));
2122                }
2123                continue;
2124            }
2125            {
2126                // Truncated rather than dropped and recreated: the table's
2127                // own structure is what dropping it throws away, and it is
2128                // identical every round.
2129                let cat = iter_engine.base_catalog_mut();
2130                let table = cat.get_mut(&cte.name).expect("created above");
2131                table.truncate();
2132                for row in &working_set {
2133                    table.insert(row.clone()).map_err(EngineError::Storage)?;
2134                }
2135            }
2136            // Run each recursive term in sequence and collect new rows.
2137            let mut next_set: Vec<Row<'static>> = Vec::new();
2138            for term in &recursive_terms {
2139                let r = iter_engine.exec_select_cancel(term, cancel)?;
2140                let QueryResult::Rows {
2141                    columns: rc,
2142                    rows: rs,
2143                } = r
2144                else {
2145                    return Err(EngineError::Unsupported(alloc::format!(
2146                        "WITH RECURSIVE {:?}: recursive term did not return rows",
2147                        cte.name
2148                    )));
2149                };
2150                if rc.len() != columns.len() {
2151                    return Err(EngineError::Unsupported(alloc::format!(
2152                        "WITH RECURSIVE {:?}: column count of recursive term ({}) does not match anchor ({})",
2153                        cte.name,
2154                        rc.len(),
2155                        columns.len()
2156                    )));
2157                }
2158                for row in rs {
2159                    if !all_union_all {
2160                        let key = encode_row_key(&row);
2161                        if !seen.insert(key) {
2162                            continue;
2163                        }
2164                    }
2165                    next_set.push(row);
2166                }
2167            }
2168            if next_set.is_empty() {
2169                break;
2170            }
2171            all_rows.extend(next_set.iter().cloned());
2172            working_set = next_set;
2173            if all_rows.len() > MAX_TOTAL_ROWS {
2174                return Err(EngineError::Unsupported(alloc::format!(
2175                    "WITH RECURSIVE {:?}: produced more than {MAX_TOTAL_ROWS} rows — likely runaway recursion",
2176                    cte.name
2177                )));
2178            }
2179            if iter + 1 == MAX_ITERATIONS {
2180                return Err(EngineError::Unsupported(alloc::format!(
2181                    "WITH RECURSIVE {:?}: exceeded {MAX_ITERATIONS} iterations",
2182                    cte.name
2183                )));
2184            }
2185        }
2186        Ok((columns, all_rows))
2187    }
2188
2189    pub(crate) fn resolve_select_subqueries(
2190        &self,
2191        stmt: &mut SelectStatement,
2192        cancel: CancelToken<'_>,
2193    ) -> Result<(), EngineError> {
2194        for item in &mut stmt.items {
2195            if let SelectItem::Expr { expr, alias } = item {
2196                // An UNCORRELATED subquery is replaced by its value right
2197                // here, and the shape the column was named for goes with
2198                // it: by projection time `SELECT EXISTS(SELECT 1)` is a
2199                // boolean literal, so SPG answered `?column?` where PG18
2200                // answers `exists`. Only a subquery at the TOP of the item
2201                // loses its name this way — one nested inside a call still
2202                // reports the call.
2203                if alias.is_none()
2204                    && matches!(
2205                        expr,
2206                        Expr::ScalarSubquery(_)
2207                            | Expr::Exists { .. }
2208                            | Expr::InSubquery { .. }
2209                            | Expr::RowInSubquery { .. }
2210                            | Expr::RowCmpSubquery { .. }
2211                    )
2212                {
2213                    *alias = Some(default_output_name(expr, self.speaks_mysql));
2214                }
2215                self.resolve_expr_subqueries(expr, cancel)?;
2216            }
2217        }
2218        if let Some(w) = &mut stmt.where_ {
2219            self.resolve_expr_subqueries(w, cancel)?;
2220        }
2221        // v7.24.1 — JOIN ON conditions can carry subqueries too;
2222        // they were never walked, so even an UNCORRELATED subquery
2223        // in ON hit "subquery reached row eval".
2224        if let Some(from) = &mut stmt.from {
2225            for j in &mut from.joins {
2226                if let Some(on) = &mut j.on {
2227                    self.resolve_expr_subqueries(on, cancel)?;
2228                }
2229            }
2230        }
2231        if let Some(gs) = &mut stmt.group_by {
2232            for g in gs {
2233                self.resolve_expr_subqueries(g, cancel)?;
2234            }
2235        }
2236        if let Some(h) = &mut stmt.having {
2237            self.resolve_expr_subqueries(h, cancel)?;
2238        }
2239        for o in &mut stmt.order_by {
2240            self.resolve_expr_subqueries(&mut o.expr, cancel)?;
2241        }
2242        for (_, peer) in &mut stmt.unions {
2243            self.resolve_select_subqueries(peer, cancel)?;
2244        }
2245        Ok(())
2246    }
2247
2248    #[allow(clippy::only_used_in_recursion)] // engine handle reads aren't really pure
2249    pub(crate) fn resolve_expr_subqueries(
2250        &self,
2251        e: &mut Expr,
2252        cancel: CancelToken<'_>,
2253    ) -> Result<(), EngineError> {
2254        // Replace-on-this-node cases first.
2255        if let Some(replacement) = self.subquery_replacement(e, cancel)? {
2256            *e = replacement;
2257            return Ok(());
2258        }
2259        match e {
2260            Expr::Collate { expr, .. } | Expr::NamedArg { expr, .. } => {
2261                self.resolve_expr_subqueries(expr, cancel)?
2262            }
2263            Expr::Variadic(expr) => self.resolve_expr_subqueries(expr, cancel)?,
2264            Expr::AggregateOrdered { call, order_by, .. } => {
2265                self.resolve_expr_subqueries(call, cancel)?;
2266                for o in order_by.iter_mut() {
2267                    self.resolve_expr_subqueries(&mut o.expr, cancel)?;
2268                }
2269            }
2270            Expr::Binary { lhs, rhs, .. } => {
2271                self.resolve_expr_subqueries(lhs, cancel)?;
2272                self.resolve_expr_subqueries(rhs, cancel)?;
2273            }
2274            Expr::Unary { expr, .. }
2275            | Expr::Cast { expr, .. }
2276            | Expr::IsNull { expr, .. }
2277            | Expr::BoolTest { expr, .. }
2278            | Expr::FieldAccess { base: expr, .. } => {
2279                self.resolve_expr_subqueries(expr, cancel)?;
2280            }
2281            Expr::FunctionCall { args, .. } => {
2282                for a in args {
2283                    self.resolve_expr_subqueries(a, cancel)?;
2284                }
2285            }
2286            Expr::Like { expr, pattern, .. } => {
2287                self.resolve_expr_subqueries(expr, cancel)?;
2288                self.resolve_expr_subqueries(pattern, cancel)?;
2289            }
2290            Expr::Extract { source, .. } => self.resolve_expr_subqueries(source, cancel)?,
2291            // v4.12 window functions — recurse into args + ORDER BY
2292            // + PARTITION BY in case they carry inner subqueries.
2293            Expr::WindowFunction {
2294                args,
2295                partition_by,
2296                order_by,
2297                ..
2298            } => {
2299                for a in args {
2300                    self.resolve_expr_subqueries(a, cancel)?;
2301                }
2302                for p in partition_by {
2303                    self.resolve_expr_subqueries(p, cancel)?;
2304                }
2305                for (e, _, _) in order_by {
2306                    self.resolve_expr_subqueries(e, cancel)?;
2307                }
2308            }
2309            // Subquery nodes are handled in subquery_replacement
2310            // (which returned None — defensive no-op); Literal /
2311            // Column are leaves.
2312            Expr::ScalarSubquery(_)
2313            | Expr::Exists { .. }
2314            | Expr::InSubquery { .. }
2315            | Expr::RowInSubquery { .. }
2316            | Expr::RowCmpSubquery { .. }
2317            | Expr::Literal(_)
2318            | Expr::Placeholder(_)
2319            | Expr::Column(_) => {}
2320            // v7.30.2 — list elements can carry scalar subqueries
2321            // (`x IN (1, (SELECT …))`).
2322            Expr::InList { expr, list, .. } => {
2323                self.resolve_expr_subqueries(expr, cancel)?;
2324                for item in list {
2325                    self.resolve_expr_subqueries(item, cancel)?;
2326                }
2327            }
2328            // v7.10.10 — recurse children.
2329            Expr::Array(items) => {
2330                for elem in items {
2331                    self.resolve_expr_subqueries(elem, cancel)?;
2332                }
2333            }
2334            Expr::ArraySubscript { target, index } => {
2335                self.resolve_expr_subqueries(target, cancel)?;
2336                self.resolve_expr_subqueries(index, cancel)?;
2337            }
2338            Expr::ArraySlice { target, lo, hi } => {
2339                self.resolve_expr_subqueries(target, cancel)?;
2340                if let Some(l) = lo {
2341                    self.resolve_expr_subqueries(l, cancel)?;
2342                }
2343                if let Some(h) = hi {
2344                    self.resolve_expr_subqueries(h, cancel)?;
2345                }
2346            }
2347            Expr::AnyAll { expr, array, .. } => {
2348                self.resolve_expr_subqueries(expr, cancel)?;
2349                // Quantified subquery — an uncorrelated one
2350                // materialises up front; a correlated one stays for
2351                // the per-row resolver.
2352                if let Expr::ScalarSubquery(inner) = array.as_mut() {
2353                    if !crate::subquery::select_is_correlated(inner) {
2354                        let s = (**inner).clone();
2355                        **array = self.materialize_quantified_rows(&s, cancel)?;
2356                    }
2357                } else {
2358                    self.resolve_expr_subqueries(array, cancel)?;
2359                }
2360            }
2361            Expr::Case {
2362                operand,
2363                branches,
2364                else_branch,
2365            } => {
2366                if let Some(o) = operand {
2367                    self.resolve_expr_subqueries(o, cancel)?;
2368                }
2369                for (w, t) in branches {
2370                    self.resolve_expr_subqueries(w, cancel)?;
2371                    self.resolve_expr_subqueries(t, cancel)?;
2372                }
2373                if let Some(e) = else_branch {
2374                    self.resolve_expr_subqueries(e, cancel)?;
2375                }
2376            }
2377        }
2378        Ok(())
2379    }
2380}
2381
2382impl Engine {
2383    /// v6.10.2 — projection for AS OF SEGMENT. Resolves
2384    /// `SelectItem::Wildcard` to all schema columns and
2385    /// `SelectItem::Expr` via the regular eval path.
2386    pub(crate) fn project_row_simple(
2387        &self,
2388        row: &Row<'static>,
2389        items: &[SelectItem],
2390        schema_cols: &[ColumnSchema],
2391        alias: &str,
2392    ) -> Result<Row<'static>, EngineError> {
2393        let ctx = self.ev_ctx(schema_cols, Some(alias));
2394        let cancel = CancelToken::none();
2395        let mut out_vals = Vec::new();
2396        for item in items {
2397            match item {
2398                // In a single-table projection (AS OF SEGMENT / RETURNING) a
2399                // qualified `t.*` covers exactly the same columns as a bare `*`.
2400                SelectItem::Wildcard | SelectItem::QualifiedWildcard(_) => {
2401                    out_vals.extend(row.values.iter().cloned());
2402                }
2403                SelectItem::Expr { expr, .. } => {
2404                    let v = self.eval_expr_with_correlated(expr, row, &ctx, cancel, None)?;
2405                    out_vals.push(v);
2406                }
2407            }
2408        }
2409        Ok(Row::new(out_vals))
2410    }
2411
2412    /// v6.10.2 — derive the output `ColumnSchema` list for an
2413    /// AS OF SEGMENT projection. Wildcards take the full schema;
2414    /// expressions take the alias if present or a synthetic
2415    /// `?column?` (PG convention) otherwise.
2416    pub(crate) fn derive_output_columns(
2417        &self,
2418        items: &[SelectItem],
2419        schema_cols: &[ColumnSchema],
2420        table_alias: &str,
2421    ) -> Vec<ColumnSchema> {
2422        let mut out = Vec::new();
2423        for item in items {
2424            match item {
2425                // `t.*` / `OLD.*` / `NEW.*` all mirror the full table schema in
2426                // a single-table projection.
2427                SelectItem::Wildcard | SelectItem::QualifiedWildcard(_) => {
2428                    out.extend(schema_cols.iter().cloned());
2429                }
2430                SelectItem::Expr { expr, alias } => {
2431                    // Bare column references inherit the schema
2432                    // column's name + type — PG names `RETURNING id`
2433                    // "id" and types it BIGINT, and the sqlx embed
2434                    // path type-checks RowDescription against the
2435                    // Rust target (mailrs embed round-12).
2436                    if let Expr::Column(col) = expr
2437                        && let Some(sc) = schema_cols.iter().find(|c| c.name == col.name)
2438                    {
2439                        let name = alias.clone().unwrap_or_else(|| sc.name.clone());
2440                        let mut c = ColumnSchema::new(name, sc.ty, sc.nullable);
2441                        // v7.39 (read01 round 54) — carry the enum identity:
2442                        // it lives outside the DataType lattice, so a derived
2443                        // table built from this schema otherwise forgets it and
2444                        // the OUTER `ORDER BY <enum col>` silently sorts by the
2445                        // label's TEXT instead of member order.
2446                        c.user_enum_type = sc.user_enum_type.clone();
2447                        out.push(c);
2448                        continue;
2449                    }
2450                    let name = alias.clone().unwrap_or_else(|| "?column?".to_string());
2451                    // v7.30.4 (mailrs round-27, P0) — type the
2452                    // expression with the same inference the SELECT
2453                    // list uses (INT−INT=INT, BIGINT+INT=BIGINT…).
2454                    // The old Text default broke every typed decode
2455                    // of `RETURNING uidnext - 1 AS uid`: four days
2456                    // of inbound mail indexed nowhere. Inference
2457                    // failure keeps the old Text fallback rather
2458                    // than inventing new error paths here.
2459                    // v7.39 (round 258) — take the enum identity from the
2460                    // same projection build, not just the type: a constant
2461                    // SELECT (`SELECT 'ok'::mood AS x`, which is what a
2462                    // VALUES row lowers to) is an EXPRESSION, so it landed
2463                    // here and the derived table forgot the enum.
2464                    let (ty, nullable) = build_projection(
2465                        core::slice::from_ref(item),
2466                        schema_cols,
2467                        table_alias,
2468                        self.speaks_mysql,
2469                        Some(self.active_catalog()),
2470                    )
2471                    .ok()
2472                    .and_then(|p| p.into_iter().next())
2473                    .map_or((DataType::Text, true), |p| (p.ty, p.nullable));
2474                    out.push(ColumnSchema::new(name, ty, nullable));
2475                }
2476            }
2477        }
2478        out
2479    }
2480
2481    /// v4.5: SELECT with cooperative cancellation. The token is
2482    /// honoured between UNION peers and inside the bare-SELECT row
2483    /// loop; HNSW kNN graph walks and the aggregate executor don't
2484    /// honour it yet (deferred — those paths bound their work
2485    /// internally by `LIMIT k` and `GROUP BY` cardinality).
2486    /// v7.38 (read01 P3.NEW3) — materialise a `spg_*` / `pg_*` meta-view by
2487    /// its (lowercased) name, or None if the name isn't a virtual view.
2488    /// Callers decide whether to return it directly (`SELECT *`) or stage
2489    /// it as a temp table for the full query pipeline.
2490    fn meta_view_result(&self, name: &str) -> Option<QueryResult> {
2491        Some(match name {
2492            "spg_statistic" => self.exec_spg_statistic(),
2493            "spg_stat_replication" => self.exec_spg_stat_replication(),
2494            "spg_stat_segment" => self.exec_spg_stat_segment(),
2495            "spg_memory_stats" => self.exec_spg_memory_stats(),
2496            "spg_stat_query" => self.exec_spg_stat_query(),
2497            "pg_stat_statements" => self.exec_pg_stat_statements(),
2498            "spg_stat_activity" => self.exec_spg_stat_activity(),
2499            "pg_stat_activity" => self.exec_pg_stat_activity(),
2500            "pg_locks" => self.exec_pg_locks(),
2501            "pg_statio_user_tables" => self.exec_pg_statio_user_tables(),
2502            "spg_stat_mvcc" => self.exec_spg_stat_mvcc(),
2503            "spg_partition_health" => self.exec_spg_partition_health(),
2504            "spg_audit_chain" => self.exec_spg_audit_chain(),
2505            "spg_audit_verify" => self.exec_spg_audit_verify(),
2506            "spg_table_ddl" => self.exec_spg_table_ddl(),
2507            "spg_role_ddl" => self.exec_spg_role_ddl(),
2508            "spg_database_ddl" => self.exec_spg_database_ddl(),
2509            _ => return None,
2510        })
2511    }
2512
2513    /// v7.39 (round 462) — the catalog an admin / stat view SELECT
2514    /// describes against: this engine's catalog with the view staged as a
2515    /// table, exactly as `exec_select_cancel_as` stages it for a
2516    /// non-bare query.
2517    ///
2518    /// These views never reach the catalog — each is a fixed row set built
2519    /// inside its own `exec_*` — so Describe reported no columns for all
2520    /// seventeen of them. Rows are deliberately not inserted: Describe
2521    /// only needs the shape, and `infer_column_types` reads the rows we
2522    /// already have in hand.
2523    pub(crate) fn admin_view_catalog(&self, stmt: &SelectStatement) -> Option<Catalog> {
2524        let from = stmt.from.as_ref()?;
2525        if !from.joins.is_empty() || self.active_catalog().get(&from.primary.name).is_some() {
2526            return None;
2527        }
2528        let lower = from.primary.name.to_ascii_lowercase();
2529        let QueryResult::Rows { columns, rows } = self.meta_view_result(&lower)? else {
2530            return None;
2531        };
2532        let mut catalog = self.active_catalog().clone();
2533        let cols = infer_column_types(&columns, &rows);
2534        catalog
2535            .create_table(TableSchema::new(from.primary.name.clone(), cols))
2536            .ok()?;
2537        Some(catalog)
2538    }
2539
2540    pub(crate) fn exec_select_cancel(
2541        &self,
2542        stmt: &SelectStatement,
2543        cancel: CancelToken<'_>,
2544    ) -> Result<QueryResult, EngineError> {
2545        self.exec_select_cancel_as(stmt, cancel, None)
2546    }
2547
2548    /// v7.39 (round 334, V55) — the same read core, authorised as
2549    /// `as_role`. A `SECURITY DEFINER` function's body runs as the
2550    /// function's OWNER: that is the entire point of the form, and without
2551    /// it every definer function failed with "permission denied" on the
2552    /// very table it exists to expose.
2553    /// v7.39 (round 559) — see the call site. `None` for anything but
2554    /// the bare shape, so every other query keeps its old path.
2555    fn try_bare_count_star(
2556        &self,
2557        stmt: &SelectStatement,
2558        as_role: Option<&str>,
2559    ) -> Result<Option<QueryResult>, EngineError> {
2560        use spg_sql::ast::SelectItem;
2561        if as_role.is_some()
2562            || !stmt.ctes.is_empty()
2563            || !stmt.unions.is_empty()
2564            || stmt.where_.is_some()
2565            || stmt.group_by.is_some()
2566            || stmt.having.is_some()
2567            || stmt.distinct
2568            || !stmt.order_by.is_empty()
2569            || stmt.limit.is_some()
2570            || stmt.offset.is_some()
2571            || stmt.items.len() != 1
2572        {
2573            return Ok(None);
2574        }
2575        let Some(from) = &stmt.from else {
2576            return Ok(None);
2577        };
2578        if !from.joins.is_empty()
2579            || stmt.locking.is_some()
2580            || from.primary.lateral_subquery.is_some()
2581            || from.primary.unnest_expr.is_some()
2582            || from.primary.generate_series_args.is_some()
2583            || from.primary.name.is_empty()
2584            || from.primary.name.starts_with("__spg_")
2585        {
2586            return Ok(None);
2587        }
2588        // A partition PARENT holds no rows of its own — they live in the
2589        // children — so its header count is 0 and the ordinary path has
2590        // to fan out. Caught by the partition conformance cases.
2591        //
2592        // v7.39 (round 645) — and an INHERITANCE parent holds only SOME
2593        // of them, which is worse: its header count is a real number,
2594        // just not the answer. `SELECT count(*) FROM par` returned 1
2595        // where PG returns 2, because this shortcut fired before the
2596        // fan-out could. The question is "does anything descend from
2597        // this", not "was it declared a partition parent".
2598        if crate::partition::has_children(self.active_catalog(), &from.primary.name) {
2599            return Ok(None);
2600        }
2601        let SelectItem::Expr { expr, alias } = &stmt.items[0] else {
2602            return Ok(None);
2603        };
2604        let spg_sql::ast::Expr::FunctionCall { name, args } = expr else {
2605            return Ok(None);
2606        };
2607        if !name.eq_ignore_ascii_case("count_star") || !args.is_empty() {
2608            return Ok(None);
2609        }
2610        // A row-security policy filters rows, so the header count is not
2611        // the answer; the ordinary path applies the policy.
2612        let Some(table) = self.active_catalog().get(&from.primary.name) else {
2613            return Ok(None);
2614        };
2615        if table.schema().row_security {
2616            return Ok(None);
2617        }
2618        // Rows frozen to the cold tier are not in `headers`, so the
2619        // header count would miss them. Caught by the cold-tier e2e.
2620        if table.has_cold_rows_fast() {
2621            return Ok(None);
2622        }
2623        let n = table.count_visible(&self.current_snapshot());
2624        let col = alias.clone().unwrap_or_else(|| String::from("count"));
2625        Ok(Some(QueryResult::Rows {
2626            columns: alloc::vec![ColumnSchema::new(col, DataType::BigInt, false)],
2627            rows: alloc::vec![Row::new(alloc::vec![Value::BigInt(
2628                i64::try_from(n).unwrap_or(i64::MAX)
2629            )])],
2630        }))
2631    }
2632
2633    /// v7.39 (round 560) — `SELECT <indexed col> FROM t WHERE <range on
2634    /// that col>` served from the index, never reading a row.
2635    ///
2636    /// Measured over pgwire on a 500k table, a 100k-row range: PG18's
2637    /// Index Only Scan 3.6 ms against SPG's 30 ms, widening with the row
2638    /// count (2x at 1k). PG needs its visibility map for this — a heap
2639    /// tuple carries its own visibility, so an index entry alone cannot
2640    /// say whether the row is live, and PG reads the heap for any page
2641    /// the map does not mark all-visible. SPG keeps a header array
2642    /// beside the rows, so the locator answers it directly and there is
2643    /// no map to be stale.
2644    /// v7.39 (round 564) — the shape test, once, for both the
2645    /// materialising scan and the streaming one.
2646    ///
2647    /// Two callers asking the same question in two places is how a fact
2648    /// starts drifting; the answer here is the single copy. Returns the
2649    /// table, the alias the predicate is written against, the projected
2650    /// column's position, and the name the single output column takes.
2651    pub(crate) fn index_only_shape<'s>(
2652        &'s self,
2653        stmt: &'s SelectStatement,
2654    ) -> Option<(&'s spg_storage::Table, &'s str, usize, String)> {
2655        use spg_sql::ast::SelectItem;
2656        if !stmt.ctes.is_empty()
2657            || !stmt.unions.is_empty()
2658            || stmt.group_by.is_some()
2659            || stmt.having.is_some()
2660            || stmt.distinct
2661            || stmt.locking.is_some()
2662            || !stmt.order_by.is_empty()
2663            || stmt.limit.is_some()
2664            || stmt.offset.is_some()
2665            || stmt.items.len() != 1
2666        {
2667            return None;
2668        }
2669        let (Some(from), Some(_)) = (&stmt.from, &stmt.where_) else {
2670            return None;
2671        };
2672        if !from.joins.is_empty()
2673            || from.primary.lateral_subquery.is_some()
2674            || from.primary.unnest_expr.is_some()
2675            || from.primary.generate_series_args.is_some()
2676            || from.primary.name.is_empty()
2677            || from.primary.name.starts_with("__spg_")
2678        {
2679            return None;
2680        }
2681        // v7.39 (round 645) — see the note on the sibling shortcut above:
2682        // an inheritance parent's own header count is not the answer.
2683        if crate::partition::has_children(self.active_catalog(), &from.primary.name) {
2684            return None;
2685        }
2686        let SelectItem::Expr { expr, alias } = &stmt.items[0] else {
2687            return None;
2688        };
2689        let spg_sql::ast::Expr::Column(c) = expr else {
2690            return None;
2691        };
2692        let alias_name = from.primary.alias.as_deref().unwrap_or(&from.primary.name);
2693        if let Some(q) = c.qualifier.as_deref()
2694            && !q.eq_ignore_ascii_case(alias_name)
2695        {
2696            return None;
2697        }
2698        let table = self.active_catalog().get(&from.primary.name)?;
2699        if table.schema().row_security {
2700            return None;
2701        }
2702        let cols = &table.schema().columns;
2703        let pos = cols
2704            .iter()
2705            .position(|s| s.name.eq_ignore_ascii_case(&c.name))?;
2706        let out = alias.clone().unwrap_or_else(|| cols[pos].name.clone());
2707        Some((table, alias_name, pos, out))
2708    }
2709
2710    /// v7.39 (round 565) — would this statement be answered out of the
2711    /// index alone?
2712    ///
2713    /// EXPLAIN has to name the node the executor will actually run, and
2714    /// the only honest way to know is to ask the same two questions the
2715    /// executor asks: the statement's shape, and everything decidable
2716    /// about the scan before it walks. Neither is re-stated here.
2717    pub(crate) fn stmt_takes_index_only_scan(&self, stmt: &SelectStatement) -> bool {
2718        let Some((table, alias_name, pos, _)) = self.index_only_shape(stmt) else {
2719            return false;
2720        };
2721        let Some(where_) = stmt.where_.as_ref() else {
2722            return false;
2723        };
2724        crate::index_access::index_only_precheck(
2725            where_,
2726            &table.schema().columns,
2727            table,
2728            alias_name,
2729            pos,
2730            self.speaks_mysql,
2731        )
2732        .is_some()
2733    }
2734
2735    fn try_index_only_scan(
2736        &self,
2737        stmt: &SelectStatement,
2738    ) -> Result<Option<QueryResult>, EngineError> {
2739        let Some((table, alias_name, pos, out_name)) = self.index_only_shape(stmt) else {
2740            return Ok(None);
2741        };
2742        // r1058 — same declines as `try_exec_joined_streaming`: CTEs
2743        // are not materialised here, and a partition parent's own
2744        // heap/indexes are empty (its rows live in the children).
2745        if !stmt.ctes.is_empty() {
2746            return Ok(None);
2747        }
2748        if let Some(from) = &stmt.from
2749            && crate::partition::has_children(self.active_catalog(), &from.primary.name)
2750        {
2751            return Ok(None);
2752        }
2753        let where_ = stmt.where_.as_ref().expect("shape checked it");
2754        let cols = &table.schema().columns;
2755        let Some(values) = crate::index_access::try_index_only_range(
2756            where_,
2757            cols,
2758            table,
2759            alias_name,
2760            &self.current_snapshot(),
2761            pos,
2762            self.speaks_mysql,
2763        ) else {
2764            return Ok(None);
2765        };
2766        let schema = alloc::vec![ColumnSchema::new(
2767            out_name,
2768            cols[pos].ty,
2769            cols[pos].nullable
2770        )];
2771        Ok(Some(QueryResult::Rows {
2772            columns: schema,
2773            rows: values
2774                .into_iter()
2775                .map(|v| Row::new(alloc::vec![v]))
2776                .collect(),
2777        }))
2778    }
2779
2780    /// v7.39 (round 564) — the same scan, emitting each value instead of
2781    /// building a `Vec<Row>` for the encoder to walk once and drop.
2782    ///
2783    /// A profile of the server serving a 50k-row range put 10.2% of the
2784    /// connection thread's CPU on BUILDING that vector and another 9.7%
2785    /// on dropping it — a fifth of the query, spent allocating and
2786    /// freeing one single-element `Vec` per output row so that the wire
2787    /// encoder could borrow each value for a few nanoseconds. The
2788    /// streaming interface it then hands them to takes `&[Value]`
2789    /// already.
2790    ///
2791    /// Returns `None` when the shape does not apply, so the caller falls
2792    /// back before anything has been emitted.
2793    pub(crate) fn try_index_only_stream<F>(
2794        &self,
2795        stmt: &SelectStatement,
2796        emit: &mut F,
2797    ) -> Result<Option<usize>, EngineError>
2798    where
2799        F: FnMut(crate::StreamItem<'_>) -> Result<(), EngineError>,
2800    {
2801        let Some((table, alias_name, pos, out_name)) = self.index_only_shape(stmt) else {
2802            return Ok(None);
2803        };
2804        // r1058 — same declines as `try_exec_joined_streaming`: CTEs
2805        // are not materialised here, and a partition parent's own
2806        // heap/indexes are empty (its rows live in the children).
2807        if !stmt.ctes.is_empty() {
2808            return Ok(None);
2809        }
2810        if let Some(from) = &stmt.from
2811            && crate::partition::has_children(self.active_catalog(), &from.primary.name)
2812        {
2813            return Ok(None);
2814        }
2815        let where_ = stmt.where_.as_ref().expect("shape checked it");
2816        let cols = &table.schema().columns;
2817        let schema = alloc::vec![ColumnSchema::new(
2818            out_name,
2819            cols[pos].ty,
2820            cols[pos].nullable
2821        )];
2822        let snapshot = self.current_snapshot();
2823        // The header goes out only once the walk has agreed to run — a
2824        // shape rejection after it would leave the client with a
2825        // RowDescription for a result that never comes.
2826        let mut wrote_header = false;
2827        let counted = crate::index_access::index_only_range_each(
2828            where_,
2829            cols,
2830            table,
2831            alias_name,
2832            &snapshot,
2833            pos,
2834            self.speaks_mysql,
2835            &mut |v: spg_storage::Value<'_>| {
2836                if !wrote_header {
2837                    emit(crate::StreamItem::Header(&schema))?;
2838                    wrote_header = true;
2839                }
2840                emit(crate::StreamItem::Row(crate::RowCells::Refs(&[&v])))
2841            },
2842        );
2843        match counted {
2844            None => Ok(None),
2845            Some(Err(e)) => Err(e),
2846            Some(Ok(n)) => {
2847                if !wrote_header {
2848                    emit(crate::StreamItem::Header(&schema))?;
2849                }
2850                Ok(Some(n))
2851            }
2852        }
2853    }
2854
2855    /// `DISTINCT ON`'s de-duplication, which runs after the inner
2856    /// SELECT has produced its rows.
2857    ///
2858    /// `#[inline(never)]` and out of `exec_select_cancel_as` for the
2859    /// reason round 848 established: a debug build gives every branch's
2860    /// locals a slot in the frame whichever branch runs, and this one is
2861    /// eighty lines of hashing, key slicing and survivor sorting that a
2862    /// statement without `DISTINCT ON` never touches. Round 867
2863    /// measured `exec_select_cancel_as` holding ~46 KB on a path that
2864    /// reaches none of it — the segment that had been blamed on
2865    /// `exec_bare_select_cancel`, which turned out to hold 2 KB.
2866    #[inline(never)]
2867    fn apply_distinct_on(
2868        &self,
2869        result: QueryResult,
2870        don_hidden: usize,
2871        don_limit: &(
2872            Option<spg_sql::ast::LimitExpr>,
2873            Option<spg_sql::ast::LimitExpr>,
2874        ),
2875        don_top1: usize,
2876        orig_order_by: &[spg_sql::ast::OrderBy],
2877    ) -> Result<QueryResult, EngineError> {
2878        let QueryResult::Rows { columns, rows } = result else {
2879            return Ok(result);
2880        };
2881        // The keys are the hidden trailing columns appended above.
2882        // v7.39 (round 729) — top-1 mode: the trailing columns are the
2883        // DON keys plus the ORDER tail; keep each group's best in one
2884        // hash pass, then sort the SURVIVORS with the original spec.
2885        let mut kept: alloc::vec::Vec<Row<'static>>;
2886        let key_start;
2887        if don_top1 > 0 {
2888            let tail = don_top1 - 1;
2889            key_start = columns.len().saturating_sub(don_hidden + tail);
2890            let ord_start = key_start + don_hidden;
2891            let tail_dirs: alloc::vec::Vec<(bool, Option<bool>)> = orig_order_by[don_hidden..]
2892                .iter()
2893                .map(|o| (o.desc, o.nulls_first))
2894                .collect();
2895            let mysql = self.speaks_mysql;
2896            let better = |a: &Row<'static>, b: &Row<'static>| -> bool {
2897                for (k, (desc, nf)) in tail_dirs.iter().enumerate() {
2898                    let av = a.values.get(ord_start + k).unwrap_or(&Value::Null);
2899                    let bv = b.values.get(ord_start + k).unwrap_or(&Value::Null);
2900                    match crate::order_by_value_cmp_in(*desc, *nf, av, bv, mysql) {
2901                        core::cmp::Ordering::Less => return true,
2902                        core::cmp::Ordering::Greater => return false,
2903                        core::cmp::Ordering::Equal => {}
2904                    }
2905                }
2906                false
2907            };
2908            let mut slot: hashbrown::HashMap<String, usize> = hashbrown::HashMap::new();
2909            let mut best: alloc::vec::Vec<Row<'static>> = alloc::vec::Vec::new();
2910            let mut keybuf = String::new();
2911            for row in rows {
2912                keybuf.clear();
2913                for v in row.values.get(key_start..ord_start).unwrap_or(&[]) {
2914                    aggregate::push_canonical_key(&mut keybuf, v);
2915                }
2916                match slot.get(keybuf.as_str()) {
2917                    Some(&i) => {
2918                        if better(&row, &best[i]) {
2919                            best[i] = row;
2920                        }
2921                    }
2922                    None => {
2923                        slot.insert(keybuf.clone(), best.len());
2924                        best.push(row);
2925                    }
2926                }
2927            }
2928            // Survivors sort with the FULL original spec (keys are still
2929            // aboard as hidden columns).
2930            let full_dirs: alloc::vec::Vec<(bool, Option<bool>)> = orig_order_by
2931                .iter()
2932                .map(|o| (o.desc, o.nulls_first))
2933                .collect();
2934            best.sort_by(|a, b| {
2935                for (k, (desc, nf)) in full_dirs.iter().enumerate() {
2936                    let av = a.values.get(key_start + k).unwrap_or(&Value::Null);
2937                    let bv = b.values.get(key_start + k).unwrap_or(&Value::Null);
2938                    match crate::order_by_value_cmp_in(*desc, *nf, av, bv, mysql) {
2939                        core::cmp::Ordering::Equal => {}
2940                        o => return o,
2941                    }
2942                }
2943                core::cmp::Ordering::Equal
2944            });
2945            for r in &mut best {
2946                r.values.truncate(key_start);
2947            }
2948            kept = best;
2949        } else {
2950            key_start = columns.len().saturating_sub(don_hidden);
2951            let mut seen: alloc::vec::Vec<alloc::vec::Vec<Value<'static>>> = alloc::vec::Vec::new();
2952            kept = alloc::vec::Vec::new();
2953            for mut row in rows {
2954                let key: alloc::vec::Vec<Value<'static>> =
2955                    row.values.get(key_start..).unwrap_or(&[]).to_vec();
2956                if seen.iter().any(|k| k == &key) {
2957                    continue;
2958                }
2959                seen.push(key);
2960                row.values.truncate(key_start);
2961                kept.push(row);
2962            }
2963        }
2964        let mut columns = columns;
2965        columns.truncate(key_start);
2966        // PG limits what DISTINCT ON left, not what fed it.
2967        let kept = apply_deferred_limit(kept, don_limit);
2968        Ok(QueryResult::Rows {
2969            columns,
2970            rows: kept,
2971        })
2972    }
2973
2974    pub(crate) fn exec_select_cancel_as(
2975        &self,
2976        stmt: &SelectStatement,
2977        cancel: CancelToken<'_>,
2978        as_role: Option<&str>,
2979    ) -> Result<QueryResult, EngineError> {
2980        // v7.39 (round 763, F31-C1) — `SELECT *, count(*) … GROUP BY
2981        // <all columns>` is legal PG (the wildcard expands to grouped
2982        // columns); SPG refused the whole shape. Expand the wildcard
2983        // into explicit column refs up front — the aggregate layer's
2984        // existing "must appear in the GROUP BY clause" validation
2985        // then answers PG's sentence for any non-grouped column.
2986        if let Some(expanded) = self.expand_aggregate_wildcard(stmt) {
2987            return self.exec_select_cancel_as(&expanded, cancel, as_role);
2988        }
2989        // v7.39 (round 559) — `SELECT count(*) FROM t` without touching
2990        // a row.
2991        //
2992        // The aggregate layer already short-circuits this to
2993        // `rows.len()`, so the O(1) part was never the problem — the
2994        // cost is UPSTREAM, materialising every visible row so that
2995        // layer can take its length. Measured over pgwire on 500k rows:
2996        // PG18 8.2 ms with two parallel workers, 10.3 ms with
2997        // parallelism off, SPG 16.5 ms — 1.6x slower than a
2998        // single-threaded PG on the commonest aggregate there is, and no
2999        // ledger entry recorded it.
3000        //
3001        // Counting visible HEADERS needs no row at all. PG cannot do
3002        // this: its visibility lives in the heap tuples themselves, so
3003        // it has to read them (that is why its own count(*) is a full
3004        // scan, parallel or not).
3005        // v7.39 (read01 round 57) — the table-privilege gate on the common
3006        // read core. A superuser session returns from it immediately.
3007        // v7.39 (round 529) — resolve an ORDER BY that names an output
3008        // ALIAS. The statement-level pass never reached a SELECT nested in
3009        // a FROM clause, a CTE or a scalar subquery, so the same query
3010        // worked on its own and failed the moment anything wrapped it —
3011        // which is what generated SQL does constantly.
3012        let aliased;
3013        let stmt = if crate::orderby::order_by_names_an_alias(stmt) {
3014            let mut s = stmt.clone();
3015            crate::orderby::resolve_order_by_position(&mut s);
3016            aliased = s;
3017            &aliased
3018        } else {
3019            stmt
3020        };
3021        // v7.39 (round 529) — DISTINCT ON needs two things it did not have.
3022        //
3023        // Its keys were evaluated against the PROJECTED row, so a key that
3024        // is not in the select list — `SELECT DISTINCT ON (g) v FROM t
3025        // ORDER BY g, v DESC`, the canonical "latest row per group" — could
3026        // not be read at all and the query failed. PG evaluates them on the
3027        // input. They are projected as hidden columns here and stripped
3028        // again below, the same way the grouping-set ordering columns
3029        // already travel.
3030        //
3031        // And the dedup ran AFTER the inner statement's LIMIT, so
3032        // `… DISTINCT ON (g) … LIMIT 2` on four rows answered ONE row where
3033        // PG answers two: the limit had already taken two rows of the same
3034        // group before anything deduplicated them. A paginated DISTINCT ON
3035        // returned short pages, with no error. The limit is deferred to
3036        // after the dedup, which is PG's order.
3037        let don_stmt;
3038        // v7.39 (round 729) — the top-1 consumer needs the ORIGINAL
3039        // order spec (the rewritten stmt's is emptied).
3040        let orig_order_by = stmt.order_by.clone();
3041        let (stmt, don_hidden, don_limit, don_top1) = if stmt.distinct_on.is_empty() {
3042            (stmt, 0, (None, None), 0usize)
3043        } else {
3044            let mut s = stmt.clone();
3045            let hidden = s.distinct_on.len();
3046            for (i, e) in stmt.distinct_on.iter().enumerate() {
3047                s.items.push(SelectItem::Expr {
3048                    expr: e.clone(),
3049                    alias: Some(alloc::format!("__distinct_on_{i}")),
3050                });
3051            }
3052            // v7.39 (round 729) — group-top-1 short circuit. When the
3053            // DISTINCT ON keys are exactly the ORDER BY's leading keys,
3054            // the answer is "per group, the row that wins the remaining
3055            // order" — a single O(n) hash pass. The old path sorted the
3056            // ENTIRE input first (500k rows, ~180 ms on the panel cell)
3057            // to keep 100. The inner query runs UNSORTED with every
3058            // order key appended as a hidden column; the dedup below
3059            // keeps each group's best, then sorts the SURVIVORS.
3060            // Declared-collation order keys stay on the sorting path
3061            // (the value comparator here is collation-blind).
3062            let prefix_matches = s.order_by.len() >= hidden
3063                && stmt
3064                    .distinct_on
3065                    .iter()
3066                    .zip(s.order_by.iter())
3067                    .all(|(d, o)| *d == o.expr && !o.desc && o.nulls_first.is_none());
3068            let colls_plain =
3069                crate::orderby::order_by_collations(&s.order_by, &self.ev_ctx(&[], None))
3070                    .map(|cs| cs.iter().all(Option::is_none))
3071                    .unwrap_or(false);
3072            let top1_tail = if prefix_matches && colls_plain && s.group_by.is_none() {
3073                let tail = s.order_by.len() - hidden;
3074                for (j, o) in s.order_by[hidden..].iter().enumerate() {
3075                    s.items.push(SelectItem::Expr {
3076                        expr: o.expr.clone(),
3077                        alias: Some(alloc::format!("__don_ord_{j}")),
3078                    });
3079                }
3080                // Carry the tail's direction flags through the aliases'
3081                // ORDER; the survivors re-sort below with the full spec.
3082                s.order_by = Vec::new();
3083                tail + 1 // sentinel: 1 + number of tail keys (0 tail is still active)
3084            } else {
3085                0
3086            };
3087            // Only a folded literal is deferred; a placeholder or an
3088            // expression keeps the path it has today rather than being
3089            // resolved a second way here.
3090            let deferrable = matches!(
3091                (&s.limit, &s.offset),
3092                (
3093                    None | Some(spg_sql::ast::LimitExpr::Literal(_)),
3094                    None | Some(spg_sql::ast::LimitExpr::Literal(_))
3095                )
3096            );
3097            let deferred = if deferrable {
3098                (s.limit.take(), s.offset.take())
3099            } else {
3100                (None, None)
3101            };
3102            don_stmt = s;
3103            (&don_stmt, hidden, deferred, top1_tail)
3104        };
3105        self.acl_check_select_as(stmt, as_role)?;
3106        validate_aggregate_placement(stmt)?;
3107        // BEFORE the fast paths below, not after: a name that resolves to
3108        // nothing is not a question the count fast path or the index-only
3109        // scan should get to answer first. Placed after them at first,
3110        // and the two of them swallowed `WHERE` and `ORDER BY` while
3111        // `GROUP BY` and `HAVING`, which cannot take those routes, raised
3112        // — the same statement answering two ways depending on the plan.
3113        self.validate_clause_columns(stmt)?;
3114        self.validate_function_arity(stmt)?;
3115        // v7.39 (round 559) — the bare `count(*)` fast path, AFTER the
3116        // privilege gate above. Placed before it at first, and the
3117        // security-definer e2e caught it immediately: a SECURITY INVOKER
3118        // function whose body is `SELECT count(*) FROM t` answered
3119        // instead of being refused, because the fast path never reached
3120        // the check.
3121        if let Some(r) = self.try_bare_count_star(stmt, as_role)? {
3122            return Ok(r);
3123        }
3124        // v7.39 (round 560) — an index-only range scan. Same placement
3125        // reasoning as the count above: after the privilege gate.
3126        if let Some(r) = self.try_index_only_scan(stmt)? {
3127            return Ok(r);
3128        }
3129        validate_locking_clause(stmt)?;
3130        let result = self.exec_select_cancel_inner(stmt, cancel)?;
3131        // v7.39 (round 135) — drop the synthetic `__grp_ord_*` ordering columns
3132        // the parser injects for GROUPING() in ORDER BY on a grouping-set query.
3133        // They carry the per-branch mask through the UNION-ALL sort and must not
3134        // appear in the output. Stripped per SELECT level (grouping-set queries
3135        // are often wrapped in a derived subquery), before DISTINCT ON.
3136        let result = strip_synthetic_order_cols(result);
3137        // v7.37.17 (17.6 siblings) — `SELECT DISTINCT ON (exprs)`:
3138        // rows arrive here already ORDER BY'd; keep the FIRST row of
3139        // each group the expressions define (PG semantics). The
3140        // expressions evaluate against the projected schema — an
3141        // expression that isn't in the select list errors honestly.
3142        if stmt.distinct_on.is_empty() {
3143            return Ok(result);
3144        }
3145        self.apply_distinct_on(result, don_hidden, &don_limit, don_top1, &orig_order_by)
3146    }
3147
3148    /// The UNION chain: execute the head as a bare block, then fold each
3149    /// peer in with left-associative dedup.
3150    ///
3151    /// `#[inline(never)]` and out of `exec_select_cancel_inner` for the
3152    /// reason round 848 established. A statement with no unions returns
3153    /// one line above the call — and every nested subquery on a deep
3154    /// path is such a statement, so each level of the recursion carried
3155    /// 170 lines of locals it could not reach. Round 867 measured that
3156    /// frame at 34,800 bytes, the largest single one on the descent,
3157    /// after two earlier attributions had blamed its caller and then its
3158    /// callee: the gap between two marks is the frame of everything
3159    /// BETWEEN them, and this function had no mark of its own.
3160    #[inline(never)]
3161    fn exec_union_chain(
3162        &self,
3163        stmt_ref: &SelectStatement,
3164        stmt: &SelectStatement,
3165        cancel: CancelToken<'_>,
3166    ) -> Result<QueryResult, EngineError> {
3167        // UNION path: clone-strip the head into a bare block (its own
3168        // DISTINCT and any inner ORDER BY are dropped by parser rule —
3169        // the wrapper SelectStatement carries them), execute, then chain
3170        // peers with left-associative dedup semantics.
3171        // v7.39 (round 232) — the wrapper's ORDER BY addresses the head's
3172        // output columns; a position past their count is PG's 42P10.
3173        crate::orderby::check_order_by_positions(stmt_ref)?;
3174        let mut head_unknown = branch_unknown_mask(stmt_ref);
3175        let head_regcast = branch_regcast_mask(stmt_ref);
3176        let mut head = stmt_ref.clone();
3177        head.unions = Vec::new();
3178        head.order_by = Vec::new();
3179        head.limit = None;
3180        let QueryResult::Rows {
3181            mut columns,
3182            mut rows,
3183        } = self.exec_bare_select_cancel(&head, cancel)?
3184        else {
3185            unreachable!("bare SELECT cannot return CommandOk")
3186        };
3187        for (kind, peer) in &stmt_ref.unions {
3188            // v7.37.17 (17.6 siblings) — a peer carrying its own
3189            // unions is a nested INTERSECT group (the parser's
3190            // precedence regrouping); recurse through the
3191            // union-aware wrapper for it.
3192            let peer_result = if peer.unions.is_empty() {
3193                self.exec_bare_select_cancel(peer, cancel)?
3194            } else {
3195                self.exec_select_cancel(peer, cancel)?
3196            };
3197            let QueryResult::Rows {
3198                columns: peer_cols,
3199                rows: mut peer_rows,
3200            } = peer_result
3201            else {
3202                unreachable!("bare SELECT cannot return CommandOk")
3203            };
3204            if peer_cols.len() != columns.len() {
3205                // v7.39 (round 232) — PG's wording, which clients match on.
3206                return Err(EngineError::Unsupported(alloc::format!(
3207                    "each {} query must have the same number of columns",
3208                    set_op_name(*kind)
3209                )));
3210            }
3211            // v7.39 (round 232+233) — PG resolves each result column to one
3212            // type before it merges anything, and refuses the query when the
3213            // two branches have no common type. SPG's unifier
3214            // (`unify_union_columns`) is value-driven and deliberately
3215            // conservative — "a column where any cell fails to coerce is left
3216            // exactly as it was" — so a mismatch produced a column holding
3217            // BOTH types (`SELECT a, b FROM t UNION SELECT b, a FROM t` came
3218            // back with integers and text interleaved) instead of an error.
3219            //
3220            // The check has to read the branch ASTs, not just their schemas:
3221            // SPG has no `Unknown` DataType, so a bare `'a'` literal describes
3222            // as TEXT and is indistinguishable from a real text column by
3223            // schema alone — yet PG treats the two completely differently
3224            // (`SELECT 1 UNION SELECT 'a'` is an input-syntax error on the
3225            // literal, `SELECT 1 UNION SELECT 'a'::text` is a type mismatch).
3226            let peer_unknown = branch_unknown_mask(peer);
3227            let peer_regcast = branch_regcast_mask(peer);
3228            for i in 0..columns.len() {
3229                let hu = head_unknown.get(i).copied().unwrap_or(false);
3230                let pu = peer_unknown.get(i).copied().unwrap_or(false);
3231                let (ht, pt) = (columns[i].ty, peer_cols[i].ty);
3232                let reg_dual = peer_regcast.get(i).copied().unwrap_or(false)
3233                    || head_regcast.get(i).copied().unwrap_or(false);
3234                match (hu, pu) {
3235                    // Both sides carry a real type: they must share a category.
3236                    (false, false) => {
3237                        if !reg_dual && !crate::conversions::types_unify(ht, pt) {
3238                            return Err(EngineError::Unsupported(alloc::format!(
3239                                "{} types {} and {} cannot be matched",
3240                                set_op_name(*kind),
3241                                crate::conversions::pg_type_name_for_error(ht),
3242                                crate::conversions::pg_type_name_for_error(pt),
3243                            )));
3244                        }
3245                    }
3246                    // One side is an untyped literal: it takes the other's
3247                    // type, and failing to convert is the error PG reports.
3248                    (true, false) => {
3249                        coerce_branch_column(&mut rows, i, pt, &columns[i].name)?;
3250                        columns[i].ty = pt;
3251                        head_unknown[i] = false;
3252                    }
3253                    (false, true) => {
3254                        coerce_branch_column(&mut peer_rows, i, ht, &columns[i].name)?;
3255                    }
3256                    // Both untyped — nothing to resolve against yet.
3257                    (true, true) => {}
3258                }
3259            }
3260            // v7.37 D.26 — a UNION result column is nullable when ANY branch is
3261            // nullable (PG semantics). Previously the result kept only the head's
3262            // nullability, so `VALUES (1),(NULL)` (a UNION-ALL chain seeded by the
3263            // non-null `1`) wrongly reported the column NOT NULL, which let
3264            // `count(col)`'s NOT-NULL fast-path count the NULL row.
3265            for (i, pc) in peer_cols.iter().enumerate() {
3266                if pc.nullable {
3267                    columns[i].nullable = true;
3268                }
3269            }
3270            // v7.39 (round 410) — under MySQL, set-op dedup / matching folds
3271            // text by the session collation (CI + accent + PAD SPACE), like
3272            // GROUP BY. PG stays byte-exact.
3273            let mysql = self.speaks_mysql;
3274            // v7.38.14 — the mask, which 7.38.13 recorded as impossible here
3275            // and was wrong about. `columns` and `peer_cols` are both in
3276            // scope; what was actually missing is that the branches' output
3277            // schemas did not CARRY the collation, so a mask built from them
3278            // would have marked every column byte-wise. Unifying the
3279            // projection-to-schema conversion fixed the supply side, and the
3280            // mask is now buildable from what was always there.
3281            //
3282            // Either side byte-wise keeps the position byte-wise, mirroring
3283            // `eval::resolve::mysql_text_fold_applies`: a set operation
3284            // between a folding column and a declared-binary one must not
3285            // quietly fold the binary one's values away.
3286            let set_mask: alloc::vec::Vec<bool> = columns
3287                .iter()
3288                .zip(peer_cols.iter())
3289                .map(|(l, r)| {
3290                    matches!(l.collation, spg_storage::Collation::Binary)
3291                        || matches!(r.collation, spg_storage::Collation::Binary)
3292                })
3293                .collect();
3294            let fold = FoldSpec::of(mysql, &set_mask);
3295            match kind {
3296                UnionKind::All => rows.extend(peer_rows),
3297                UnionKind::Distinct => {
3298                    rows.extend(peer_rows);
3299                    rows = dedup_rows(rows, fold);
3300                }
3301                // v7.37.17 (17.6 siblings) — PG set semantics.
3302                // v7.39 (round 591) — all four ask the same question of the
3303                // right side, and all four used to answer it by scanning it
3304                // once per left row. `PeerIndex` buckets it by the hash
3305                // DISTINCT already uses, so the answer is a lookup.
3306                // INTERSECT: distinct rows present on both sides.
3307                UnionKind::Intersect => {
3308                    let idx = PeerIndex::build(&peer_rows, fold);
3309                    rows = dedup_rows(rows, fold)
3310                        .into_iter()
3311                        .filter(|r| idx.contains(r))
3312                        .collect();
3313                }
3314                // INTERSECT ALL: multiset intersection — each row
3315                // keeps min(left count, right count) occurrences.
3316                UnionKind::IntersectAll => {
3317                    let mut idx = PeerIndex::build(&peer_rows, fold);
3318                    let mut kept: Vec<Row<'static>> = Vec::new();
3319                    for r in rows {
3320                        if idx.take_one(&r) {
3321                            kept.push(r);
3322                        }
3323                    }
3324                    rows = kept;
3325                }
3326                // EXCEPT: distinct left rows absent from the right.
3327                UnionKind::Except => {
3328                    let idx = PeerIndex::build(&peer_rows, fold);
3329                    rows = dedup_rows(rows, fold)
3330                        .into_iter()
3331                        .filter(|r| !idx.contains(r))
3332                        .collect();
3333                }
3334                // EXCEPT ALL: multiset subtraction — each right
3335                // occurrence cancels one left occurrence.
3336                UnionKind::ExceptAll => {
3337                    let mut idx = PeerIndex::build(&peer_rows, fold);
3338                    let mut kept: Vec<Row<'static>> = Vec::new();
3339                    for r in rows {
3340                        if !idx.take_one(&r) {
3341                            kept.push(r);
3342                        }
3343                    }
3344                    rows = kept;
3345                }
3346            }
3347        }
3348        // PG resolves a UNION / VALUES result column to one common type
3349        // and casts every branch to it (`SELECT '2020-01-01'::date UNION
3350        // ALL SELECT '2020-01-02'` → both DATE, not DATE + TEXT). SPG
3351        // built each branch independently, leaving mixed-type columns
3352        // that broke ORDER BY, comparisons, and value-based window
3353        // frames. Unify + coerce before the combined ORDER BY sees them.
3354        unify_union_columns(&mut columns, &mut rows);
3355        // ORDER BY at the top of a UNION applies to the combined result.
3356        // Eval against the projected schema (NOT the source table).
3357        if !stmt.order_by.is_empty() {
3358            // v7.39 (read01 round 54) — the combined-result ctx must carry the
3359            // catalog, and the projected columns must keep their enum identity
3360            // (`user_enum_type`), or `ORDER BY <enum col>` over a UNION sorts
3361            // by TEXT instead of member order — silently wrong rows, not an
3362            // error. (Same shape as the enum-order knife's GROUP BY fix.)
3363            let synth_ctx = EvalContext::new(&columns, None).with_catalog(self.active_catalog());
3364            // v7.37.17 (17.6 siblings) — positional keys (ORDER BY 1)
3365            // survive to here when the head projects a Wildcard (the
3366            // group-tail wrapper shape): map them onto the Nth
3367            // projected column so the combined sort works.
3368            let resolved_order: Vec<spg_sql::ast::OrderBy> = stmt
3369                .order_by
3370                .iter()
3371                .map(|o| {
3372                    let mut o = o.clone();
3373                    if let Expr::Literal(spg_sql::ast::Literal::Integer(n)) = &o.expr
3374                        && *n >= 1
3375                        && let Ok(idx) = usize::try_from(*n - 1)
3376                        && idx < columns.len()
3377                    {
3378                        o.expr = Expr::Column(spg_sql::ast::ColumnName {
3379                            qualifier: None,
3380                            name: columns[idx].name.clone(),
3381                        });
3382                    }
3383                    o
3384                })
3385                .collect();
3386            let descs: Vec<bool> = resolved_order.iter().map(|o| o.desc).collect();
3387            let mut tagged: Vec<(Vec<OrderKey>, Row)> = Vec::with_capacity(rows.len());
3388            for r in rows {
3389                // v7.39.12 — a correlated subquery in ORDER BY is resolved
3390                // for this row before the key is built; see
3391                // `Engine::order_by_resolved_for_row`.
3392                let per_row =
3393                    self.order_by_resolved_for_row(&resolved_order, &r, &synth_ctx, cancel)?;
3394                let keys = build_order_keys(
3395                    per_row.as_deref().unwrap_or(&resolved_order),
3396                    &r,
3397                    &synth_ctx,
3398                )?;
3399                tagged.push((keys, r));
3400            }
3401            sort_by_keys(&mut tagged, &descs);
3402            rows = tagged.into_iter().map(|(_, r)| r).collect();
3403        }
3404        apply_offset_and_limit(&mut rows, stmt.offset_literal(), stmt.limit_literal());
3405        Ok(QueryResult::Rows { columns, rows })
3406    }
3407
3408    fn exec_select_cancel_inner(
3409        &self,
3410        stmt: &SelectStatement,
3411        cancel: CancelToken<'_>,
3412    ) -> Result<QueryResult, EngineError> {
3413        cancel.check()?;
3414        // v7.38 P0 元机制 A — first observable point inside the
3415        // planner / executor. Tests use this to inject a delay or
3416        // a cancellation race before any row is produced. Release
3417        // build expands to `let _ = (...);` — zero cost.
3418        crate::injection_point!("planner_first_row_fetch", &stmt.from);
3419        // v7.39 (round 705) — WINDOW-clause definitions nothing referenced.
3420        // PG analyses every definition, referenced or not, so `SELECT i FROM
3421        // t WINDOW w AS (ORDER BY nosuch)` fails there and silently
3422        // succeeded here (the parser used to drop the unreferenced defs
3423        // whole). The check is the CREATE VIEW check's shape (round 700): a
3424        // LIMIT-0 run of the same FROM with the definitions' key
3425        // expressions as the projection — it cannot disagree with what a
3426        // referencing window would have done, because it resolves the same
3427        // names the same way. Zero cost for the ordinary statement: the
3428        // list is empty unless a WINDOW clause left unreferenced defs.
3429        if !stmt.window_check_exprs.is_empty() {
3430            let mut probe = stmt.clone();
3431            probe.items = stmt
3432                .window_check_exprs
3433                .iter()
3434                .map(|e| spg_sql::ast::SelectItem::Expr {
3435                    expr: e.clone(),
3436                    alias: None,
3437                })
3438                .collect();
3439            probe.window_check_exprs = Vec::new();
3440            probe.distinct = false;
3441            probe.distinct_on = Vec::new();
3442            probe.group_by = None;
3443            probe.group_by_all = false;
3444            probe.having = None;
3445            probe.unions = Vec::new();
3446            probe.order_by = Vec::new();
3447            probe.locking = None;
3448            probe.limit = Some(spg_sql::ast::LimitExpr::Literal(0));
3449            probe.offset = None;
3450            probe.limit_with_ties = false;
3451            self.exec_select_cancel_inner(&probe, cancel)?;
3452        }
3453        // v7.39 (read01 round 74) — lower `(f(args)).*`. Naming a record's fields
3454        // takes the catalog, so the parser leaves a marker and the rewrite lands
3455        // here: the call moves into a LATERAL FROM item and the item becomes one
3456        // reference per declared column. `SELECT 'p', (rows_of(2)).*` is
3457        // `SELECT 'p', __rec.id, __rec.v FROM rows_of(2) AS __rec` — reusing the
3458        // set-returning FROM machinery of rounds 65 and 69 rather than growing a
3459        // second one.
3460        if let Some(lowered) = self.lower_record_expansion(stmt)? {
3461            return self.exec_select_cancel_inner(&lowered, cancel);
3462        }
3463        // v7.17.0 Phase 1.2 — user-defined VIEW expansion. If the
3464        // FROM / JOIN graph references any catalogued view name,
3465        // re-parse the view body and prepend it as a synthetic
3466        // CTE. Recurses on views-in-views via the regular CTE
3467        // dispatch below. Fast-path: skip the walker entirely when
3468        // the catalog has no views (the typical OLTP load).
3469        if !self.active_catalog().views_all().is_empty() {
3470            if let Some(rewritten) = self.expand_views_in_select(stmt)? {
3471                return self.exec_select_cancel(&rewritten, cancel);
3472            }
3473        }
3474        // v7.37.6-B(sentori Epic 2 P0)— `SELECT … FROM <partition-parent>`
3475        // gets rewritten to a UNION-ALL over the children that overlap
3476        // the WHERE-derived key range. Uses the same CTE-injection
3477        // trick as VIEW expansion above so downstream resolution
3478        // doesn't need a partition-aware code path.
3479        if let Some(rewritten) = self.expand_partition_parents_in_select(stmt)? {
3480            return self.exec_select_cancel(&rewritten, cancel);
3481        }
3482        // v7.16.2 — information_schema / pg_catalog virtual
3483        // views (mailrs round-10 A.3). If the SELECT touches a
3484        // synthetic meta-table name (`__spg_info_*` /
3485        // `__spg_pg_*` — produced by the parser for
3486        // `information_schema.X` / `pg_catalog.X`), clone the
3487        // catalog, materialise the requested view as a real
3488        // temporary table, and re-execute against an enriched
3489        // engine. Same pattern as `exec_with_ctes` for CTEs.
3490        if !self.meta_views_materialised && select_references_meta_view(stmt) {
3491            return self.exec_select_with_meta_views(stmt, cancel);
3492        }
3493        // v6.10.2 — cold-tier time-travel short-circuit. When the
3494        // primary TableRef carries `AS OF SEGMENT '<id>'`, run a
3495        // dedicated cold-segment scan instead of the regular
3496        // hot+index path. The scope is intentionally narrow for
3497        // v6.10.2 — bare `SELECT * FROM <t> AS OF SEGMENT 'id'`,
3498        // optionally with a single-column-equality WHERE. JOINs /
3499        // aggregates / ORDER BY / subqueries on top of a time-
3500        // travelled scan are STABILITY § "Out of v6.10".
3501        if let Some(from) = &stmt.from
3502            && let Some(seg_id) = from.primary.as_of_segment
3503        {
3504            return self.exec_select_as_of_segment(stmt, from, seg_id);
3505        }
3506        // v6.2.0 / v6.5.0 — virtual-table short-circuits. Detected
3507        // pre-CTE because they don't read from the catalog and
3508        // shouldn't participate in regular FROM resolution.
3509        // v6.2.0 / v6.5.0 / v7.38 (read01 P3.NEW3) — virtual-table
3510        // short-circuits. A meta-view FROM materialises to a fixed row
3511        // set. For a bare `SELECT *` we return it directly; otherwise we
3512        // stage it as a temp table and run the normal pipeline, so
3513        // projection / WHERE / ORDER BY / aggregates work over these views
3514        // (they were `SELECT *`-only before). A real table shadowing the
3515        // name wins (checked first), which also stops the staged re-run
3516        // from recursing back into meta-view detection.
3517        if let Some(from) = &stmt.from
3518            && from.joins.is_empty()
3519            && self.active_catalog().get(&from.primary.name).is_none()
3520        {
3521            let lower = from.primary.name.to_ascii_lowercase();
3522            if let Some(result) = self.meta_view_result(&lower) {
3523                let bare = stmt.where_.is_none()
3524                    && stmt.group_by.is_none()
3525                    && stmt.having.is_none()
3526                    && stmt.unions.is_empty()
3527                    && stmt.order_by.is_empty()
3528                    && stmt.limit.is_none()
3529                    && stmt.offset.is_none()
3530                    && !stmt.distinct
3531                    && stmt.items.iter().all(|i| matches!(i, SelectItem::Wildcard));
3532                if bare {
3533                    return Ok(result);
3534                }
3535                if let QueryResult::Rows { columns, rows } = result {
3536                    let mut catalog = self.active_catalog().clone();
3537                    let cols = infer_column_types(&columns, &rows);
3538                    let schema = TableSchema::new(from.primary.name.clone(), cols);
3539                    catalog.create_table(schema).map_err(EngineError::Storage)?;
3540                    let t = catalog
3541                        .get_mut(&from.primary.name)
3542                        .expect("just-created meta-view table must exist");
3543                    for row in rows {
3544                        t.insert(row).map_err(EngineError::Storage)?;
3545                    }
3546                    let mut eng = Engine::restore(catalog);
3547                    if let Some(c) = self.clock {
3548                        eng = eng.with_clock(c);
3549                    }
3550                    if let Some(f) = self.salt_fn {
3551                        eng = eng.with_salt_fn(f);
3552                    }
3553                    // v7.39 (read01 pgstatfuncs.c) — carry the calling-
3554                    // connection identity so `WHERE pid = pg_backend_pid()`
3555                    // matches inside the staged meta-view run.
3556                    if let Some(f) = self.backend_pid_fn {
3557                        eng.set_backend_pid_fn(f);
3558                    }
3559                    return eng.exec_select_cancel(stmt, cancel);
3560                }
3561                return Ok(result);
3562            }
3563        }
3564        // v4.11: CTEs materialise into a temporary enriched catalog
3565        // *before* anything else — the body SELECT can then refer
3566        // to CTE names via the regular FROM-clause resolution.
3567        // Uncorrelated only: each CTE body runs once against the
3568        // current catalog, not against later CTEs' results (left-
3569        // to-right materialisation would relax this, but we keep
3570        // it simple for v4.11 MVP).
3571        if !stmt.ctes.is_empty() {
3572            return self.exec_with_ctes(stmt, cancel);
3573        }
3574        // v4.10: subqueries (uncorrelated) are resolved here, before
3575        // the executor sees the row loop. We clone the statement so
3576        // we can mutate without disturbing the caller's AST — most
3577        // queries pass through with no subquery nodes and the clone
3578        // is cheap; with subqueries the materialisation cost
3579        // dominates anyway.
3580        let mut stmt_owned;
3581        let stmt_ref: &SelectStatement = if expr_tree_has_subquery(stmt) {
3582            stmt_owned = stmt.clone();
3583            // v7.33 (mailrs 7.32.1) — sublink pull-up first: an
3584            // aggregate-wrapped correlated scalar subquery whose
3585            // correlation key is UNIQUE/PK becomes a LEFT JOIN, so the
3586            // executor streams one join instead of splicing a per-row
3587            // subplan. Runs before the per-row/batch resolver, which then
3588            // only sees the subqueries the pull-up left behind.
3589            self.pull_up_unique_correlated_agg_subqueries(&mut stmt_owned);
3590            // v7.37.4 (A — correlated LIMIT 1 ORDER BY DESC pull-up) —
3591            // the "per-key latest" scalar subquery shape (inbox / feed
3592            // / timeline applications) becomes a CTE + LEFT JOIN
3593            // against a GROUP BY pre-aggregation that reuses the v7.33
3594            // first_ordered argmax executor. Runs AFTER unique-key
3595            // pull-up (so the unique-key fast path still wins for
3596            // single-PK lookups) and BEFORE the EXISTS sublink rewrite.
3597            // Phase 1 (this commit) is skeleton only — no-op pass.
3598            self.pull_up_correlated_limit_one_subqueries(&mut stmt_owned);
3599            // v7.34.2 (mailrs prod NOT EXISTS) — plan-time `[NOT] EXISTS`
3600            // sublink pull-up to semi/anti-join, before the resolver gets
3601            // a chance to walk per-row.
3602            self.pull_up_exists_sublinks(&mut stmt_owned);
3603            // v7.37.4 — if the LIMIT 1 pullup added CTEs, route through
3604            // exec_with_ctes so they materialise once before the body
3605            // SELECT runs. exec_with_ctes strips ctes from the body
3606            // clone, then re-enters select.
3607            if !stmt_owned.ctes.is_empty() {
3608                return self.exec_with_ctes(&stmt_owned, cancel);
3609            }
3610            // v7.37.x (docker-fair INSUBQ attack) — short-circuit
3611            //   SELECT COUNT(*) FROM A WHERE A.pk IN (<uncorrelated subquery>)
3612            // BEFORE `resolve_select_subqueries` materialises the inner
3613            // result as `Vec<Expr::Literal>` (~150 µs for the 6 k-row
3614            // INSUBQ benchmark). Run the inner once, collect the result
3615            // values into a `HashSet<i64>` directly, then probe A.pk per
3616            // value and tally. Returns `Some` when the shape matches.
3617            if let Some(out) = self.try_count_star_pk_in_subquery_fast(&stmt_owned, cancel)? {
3618                return Ok(out);
3619            }
3620            self.resolve_select_subqueries(&mut stmt_owned, cancel)?;
3621            &stmt_owned
3622        } else {
3623            stmt
3624        };
3625        if stmt_ref.unions.is_empty() {
3626            return self.exec_bare_select_cancel(stmt_ref, cancel);
3627        }
3628        self.exec_union_chain(stmt_ref, stmt, cancel)
3629    }
3630
3631    #[allow(clippy::too_many_lines)]
3632    #[allow(clippy::too_many_lines)] // huge match — splitting fragments the planner
3633    /// v7.11.7 — execute `SELECT … FROM unnest(expr) [AS] alias …`.
3634    /// Synthesises a single-column virtual table whose column type
3635    /// is TEXT and whose rows are the array elements. Routes
3636    /// through the regular projection / WHERE / ORDER BY / LIMIT
3637    /// machinery so set-returning UNNEST composes naturally with
3638    /// the rest of the SELECT surface.
3639    fn exec_select_unnest(
3640        &self,
3641        stmt: &SelectStatement,
3642        primary: &TableRef,
3643        cancel: CancelToken<'_>,
3644    ) -> Result<QueryResult, EngineError> {
3645        let expr = primary
3646            .unnest_expr
3647            .as_deref()
3648            .expect("caller guards unnest_expr.is_some()");
3649        // Multi-arg unnest(a, b, …) — parallel zip, NULL-padded.
3650        // N value columns instead of one; the shared builder does
3651        // the work and the tail below (WHERE / agg / projection)
3652        // runs against the wider schema.
3653        let multi: Option<(alloc::vec::Vec<DataType>, alloc::vec::Vec<Row<'static>>)> =
3654            match unnest_zip_args(expr) {
3655                Some(args) => Some(unnest_zip_rows(args)?),
3656                None => None,
3657            };
3658        // Evaluate the array expression once. Empty schema / empty
3659        // row — uncorrelated UNNEST cannot reference outer columns.
3660        // v7.39 (read01 round 49) — the ctx must carry the catalog: the enum
3661        // introspection family (enum_range / enum_first / enum_last) resolves
3662        // its labels from the argument's STATIC enum type against the
3663        // catalog's enum registry. Without it `unnest(enum_range(NULL::mood))`
3664        // fell through to the generic arm, got NULL, and expanded to zero rows
3665        // — while the bare `SELECT enum_range(NULL::mood)` (whose ctx does
3666        // carry the catalog) worked.
3667        let empty_schema: alloc::vec::Vec<ColumnSchema> = alloc::vec::Vec::new();
3668        let ctx = EvalContext::new(&empty_schema, None).with_catalog(self.active_catalog());
3669        let dummy_row = Row::new(alloc::vec::Vec::new());
3670        // v7.11.13 — unnest dispatches per array element type so
3671        // INT[] / BIGINT[] surface their PG types in projection.
3672        // v7.39 (round 758, F31-B8a) — the composite SRF names its own
3673        // columns (PG: lexeme | positions | weights); everything else
3674        // keeps the alias / "unnest" defaults below.
3675        let mut composite_names: Option<&[&str]> = None;
3676        let (dtypes, rows): (alloc::vec::Vec<DataType>, alloc::vec::Vec<Row<'static>>) =
3677            if let Some(m) = multi {
3678                m
3679            } else {
3680                // v7.39 (round 236) — flatten a multidimensional array into
3681                // its row-major elements (PG) before the 1-D-only match.
3682                let unnest_src = {
3683                    let v = eval::eval_expr(expr, &dummy_row, &ctx).map_err(EngineError::Eval)?;
3684                    crate::eval::values::flatten_2d(&v).unwrap_or(v)
3685                };
3686                let mut return_multi: Option<(
3687                    alloc::vec::Vec<DataType>,
3688                    alloc::vec::Vec<Row<'static>>,
3689                )> = None;
3690                let (elem_dtype, rows): (DataType, alloc::vec::Vec<Row<'static>>) = match unnest_src
3691                {
3692                    Value::Null => (DataType::Text, alloc::vec::Vec::new()),
3693                    Value::TextArray(items) => {
3694                        let rows = items
3695                            .into_iter()
3696                            .map(|item| {
3697                                Row::new(alloc::vec![match item {
3698                                    Some(s) => Value::text(s),
3699                                    None => Value::Null,
3700                                }])
3701                            })
3702                            .collect();
3703                        (DataType::Text, rows)
3704                    }
3705                    Value::IntArray(items) => {
3706                        let rows = items
3707                            .into_iter()
3708                            .map(|item| {
3709                                Row::new(alloc::vec![match item {
3710                                    Some(n) => Value::Int(n),
3711                                    None => Value::Null,
3712                                }])
3713                            })
3714                            .collect();
3715                        (DataType::Int, rows)
3716                    }
3717                    Value::BigIntArray(items) => {
3718                        let rows = items
3719                            .into_iter()
3720                            .map(|item| {
3721                                Row::new(alloc::vec![match item {
3722                                    Some(n) => Value::BigInt(n),
3723                                    None => Value::Null,
3724                                }])
3725                            })
3726                            .collect();
3727                        (DataType::BigInt, rows)
3728                    }
3729                    Value::Multirange { kind, ranges } => {
3730                        let rows = ranges
3731                            .iter()
3732                            .map(|sp| {
3733                                Row::new(alloc::vec![Value::Range {
3734                                    kind,
3735                                    lower: sp.lower.clone(),
3736                                    upper: sp.upper.clone(),
3737                                    lower_inc: sp.lower_inc,
3738                                    upper_inc: sp.upper_inc,
3739                                    empty: false,
3740                                }])
3741                            })
3742                            .collect();
3743                        (DataType::Range(kind), rows)
3744                    }
3745                    // v7.39 (round 758, F31-B8a) — unnest(tsvector):
3746                    // one row per lexeme, PG18-measured columns
3747                    // lexeme | positions | weights (`a | {1,3} |
3748                    // {D,D}`); a position-less lexeme (a stripped
3749                    // vector) reads NULL in both array columns.
3750                    Value::TsVector(lexemes) => {
3751                        composite_names = Some(&["lexeme", "positions", "weights"]);
3752                        let rows = lexemes
3753                            .iter()
3754                            .map(|l| {
3755                                let (pos, wts) = if l.positions.is_empty() {
3756                                    (Value::Null, Value::Null)
3757                                } else {
3758                                    let letter = match l.weight {
3759                                        3 => "A",
3760                                        2 => "B",
3761                                        1 => "C",
3762                                        _ => "D",
3763                                    };
3764                                    (
3765                                        Value::SmallIntArray(
3766                                            l.positions
3767                                                .iter()
3768                                                .map(|p| {
3769                                                    Some(i16::try_from(*p).unwrap_or(i16::MAX))
3770                                                })
3771                                                .collect(),
3772                                        ),
3773                                        Value::TextArray(
3774                                            l.positions
3775                                                .iter()
3776                                                .map(|_| Some(letter.into()))
3777                                                .collect(),
3778                                        ),
3779                                    )
3780                                };
3781                                Row::new(alloc::vec![Value::text(l.word.clone()), pos, wts])
3782                            })
3783                            .collect();
3784                        return_multi = Some((
3785                            alloc::vec![
3786                                DataType::Text,
3787                                DataType::SmallIntArray,
3788                                DataType::TextArray
3789                            ],
3790                            rows,
3791                        ));
3792                        (DataType::Text, alloc::vec::Vec::new())
3793                    }
3794                    // v7.39.11 — every remaining array-family value,
3795                    // through the one element menu, so a type does not
3796                    // have to be written out here a second time to be
3797                    // unnestable. `unnest(ARRAY[1,2]::smallint[])`
3798                    // raised "expects an array argument, got
3799                    // smallint[]" until this arm — the arms above name
3800                    // int / bigint / text / json and stop — and so did
3801                    // every catalog vector. Found while closing
3802                    // sentori's §4 against 7.39.10.
3803                    ref v if crate::eval::values::array_len(v).is_some() => {
3804                        let elems = crate::eval::values::array_elements(v).unwrap_or_default();
3805                        let dt = elems
3806                            .iter()
3807                            .find_map(spg_storage::Value::data_type)
3808                            .unwrap_or(DataType::Text);
3809                        let rows = elems
3810                            .into_iter()
3811                            .map(|e| Row::new(alloc::vec![e]))
3812                            .collect();
3813                        (dt, rows)
3814                    }
3815                    other => {
3816                        // v7.39 (round 622, S05a) — see table_access.rs:
3817                        // the same sentence, and it is a type mismatch.
3818                        return Err(EngineError::Eval(EvalError::TypeMismatch {
3819                            detail: alloc::format!(
3820                                "unnest() expects an array argument, got {}",
3821                                crate::conversions::pg_type_name_for_error_opt(other.data_type())
3822                            ),
3823                        }));
3824                    }
3825                };
3826                if let Some(m) = return_multi {
3827                    m
3828                } else {
3829                    (alloc::vec![elem_dtype], rows)
3830                }
3831            };
3832        let alias = primary
3833            .alias
3834            .clone()
3835            .unwrap_or_else(|| "unnest".to_string());
3836        // v7.13.2 — mailrs round-6 S5. Honour PG-standard
3837        // `UNNEST(arr) AS p(col_name)` column-list aliasing:
3838        // entries map positionally over the value columns. Without
3839        // the column list, a single column falls back to the table
3840        // alias (pre-v7.13.2 behaviour); multi-arg columns default
3841        // to PG's `unnest`.
3842        let n_vals = dtypes.len();
3843        let mut schema_cols: alloc::vec::Vec<ColumnSchema> = dtypes
3844            .iter()
3845            .enumerate()
3846            .map(|(i, dt)| {
3847                let name = primary
3848                    .unnest_column_aliases
3849                    .get(i)
3850                    .cloned()
3851                    .unwrap_or_else(|| {
3852                        if let Some(names) = composite_names {
3853                            names
3854                                .get(i)
3855                                .map_or_else(|| "unnest".to_string(), |n| (*n).to_string())
3856                        } else if n_vals == 1 {
3857                            alias.clone()
3858                        } else {
3859                            "unnest".to_string()
3860                        }
3861                    });
3862                ColumnSchema::new(name, *dt, true)
3863            })
3864            .collect();
3865        // v7.39 (read01 round 78) — the item's row type IS this scalar when the
3866        // parser desugared a base-type-returning function here (see
3867        // TableRef::scalar_fn_item); the marker rides the column so it survives
3868        // every EvalContext an inner stage rebuilds.
3869        if primary.scalar_fn_item && schema_cols.len() == 1 {
3870            schema_cols[0].scalar_row_source = true;
3871        }
3872        // WITH ORDINALITY — trailing BIGINT counting rows from 1
3873        // in element order. The alias entry after the value
3874        // columns renames it (PG default: `ordinality`).
3875        let rows = if primary.with_ordinality {
3876            let ord_name = primary
3877                .unnest_column_aliases
3878                .get(n_vals)
3879                .cloned()
3880                .unwrap_or_else(|| "ordinality".to_string());
3881            schema_cols.push(ColumnSchema::new(ord_name, DataType::BigInt, false));
3882            rows.into_iter()
3883                .enumerate()
3884                .map(|(i, row)| {
3885                    let mut vals = row.values.clone();
3886                    vals.push(Value::BigInt(i as i64 + 1));
3887                    Row::new(vals)
3888                })
3889                .collect()
3890        } else {
3891            rows
3892        };
3893        // v7.39 (read01 round 54) — `ev_ctx` threads the catalog; a bare
3894        // `EvalContext::new` drops it and every catalog-dependent cast
3895        // (regclass / enum / composite / domain) silently degrades.
3896        let scan_ctx = self.ev_ctx(&schema_cols, Some(&alias));
3897        // Apply WHERE.
3898        let filtered: alloc::vec::Vec<Row<'static>> = if let Some(w) = &stmt.where_ {
3899            let mut out = alloc::vec::Vec::with_capacity(rows.len());
3900            for row in rows {
3901                cancel.check()?;
3902                let v = eval::eval_expr(w, &row, &scan_ctx).map_err(EngineError::Eval)?;
3903                if matches!(v, Value::Bool(true)) {
3904                    out.push(row);
3905                }
3906            }
3907            out
3908        } else {
3909            rows
3910        };
3911        // v7.17.0 Phase 3.P0-48 — aggregate dispatch over the
3912        // unnest source. Same routing the relational scan path
3913        // already takes — without it `SELECT COUNT(*) FROM
3914        // unnest(ARRAY[…])` either errored at projection time or
3915        // returned the wrong shape.
3916        if aggregate::uses_aggregate_in(stmt, self.speaks_mysql) {
3917            // v7.29 — a per-query memo so correlated scalar
3918            // subqueries batch-evaluate once (group map) instead of
3919            // executing per group.
3920            let agg_memo = core::cell::RefCell::new(memoize::MemoizeCache::default());
3921            let agg_correlated = |e: &Expr, r: &Row<'static>, c: &EvalContext<'_>| {
3922                self.eval_expr_with_correlated(e, r, c, cancel, Some(&mut agg_memo.borrow_mut()))
3923                    .map_err(|err| match err {
3924                        EngineError::Eval(ev) => ev,
3925                        other => eval::EvalError::TypeMismatch {
3926                            detail: alloc::format!("{other}"),
3927                        },
3928                    })
3929            };
3930            // v7.39 (round 656) — hand the rows over as they are rather than
3931            // collecting a second vector of `RowRef` wrappers. Note this is
3932            // a set-returning-function path, NOT the relational scan: the
3933            // measured O(rows) cost lived in `run_single_table_aggregate`,
3934            // and converting these four first was a miss that cost a full
3935            // round — every test stayed green and the number did not move.
3936            let agg = aggregate::run(
3937                stmt,
3938                crate::join::AggRows::Owned(&filtered),
3939                &schema_cols,
3940                Some(&alias),
3941                Some(&agg_correlated),
3942                self.parallel_runner.0.as_deref(),
3943                Some(self.active_catalog()),
3944                Some(self),
3945            )?;
3946            return self.finish_agg_result(agg, stmt, cancel);
3947        }
3948        // Projection.
3949        let projection = build_projection(
3950            &stmt.items,
3951            &schema_cols,
3952            &alias,
3953            self.speaks_mysql,
3954            Some(self.active_catalog()),
3955        )?;
3956        let mut projected_rows: alloc::vec::Vec<Row<'static>> =
3957            alloc::vec::Vec::with_capacity(filtered.len());
3958        // v7.19 P5 — Set-Returning-Function in projection
3959        // position (PG `SELECT unnest(arr) FROM t` shape). When a
3960        // SELECT item evaluates to a top-level unnest(arr) call,
3961        // expand it: for each input row, evaluate the array, emit
3962        // one output row per element, broadcasting non-SRF
3963        // projections from the same input row. Multi-SRF + LCM
3964        // padding stays a documented carve-out; mailrs uses
3965        // single-SRF for redirect_uris.
3966        // v7.39 (read01 round 67) — EVERY set-returning item expands, in lockstep
3967        // (see `expand_srf_row`); a user `RETURNS SETOF` function counts too.
3968        let srf_idxs = self.srf_target_idxs(&projection);
3969        // v7.39 (round 621) — which input row each output row came from. An
3970        // SRF turns one input row into many, and the ORDER BY below used to
3971        // index the EXPANDED rows by the INPUT row's position: the result was
3972        // silently truncated to the input row count and left unsorted, so
3973        // `SELECT unnest(ARRAY[1,2]), y FROM unnest(ARRAY[5,6,7]) y ORDER BY 1`
3974        // answered three of its six rows, in no order. Without the ORDER BY
3975        // the same query was already right.
3976        let mut src_of_row: alloc::vec::Vec<usize> = alloc::vec::Vec::new();
3977        if !srf_idxs.is_empty() {
3978            let (rows, src) =
3979                expand_projection_srfs(self, &projection, &srf_idxs, &filtered, &scan_ctx)?;
3980            projected_rows = rows;
3981            src_of_row = src;
3982        } else {
3983            // v7.24 (round-16 B) — select-list subqueries resolve
3984            // per row (correlated-aware; plain exprs take the fast
3985            // path inside).
3986            let mut proj_memo = memoize::MemoizeCache::default();
3987            for row in &filtered {
3988                let mut vals = alloc::vec::Vec::with_capacity(projection.len());
3989                for p in &projection {
3990                    vals.push(self.eval_expr_with_correlated(
3991                        &p.expr,
3992                        row,
3993                        &scan_ctx,
3994                        cancel,
3995                        Some(&mut proj_memo),
3996                    )?);
3997                }
3998                projected_rows.push(Row::new(vals));
3999            }
4000        }
4001        // ORDER BY / LIMIT — apply on the projected rows (cheap;
4002        // unnest result sets are small by design).
4003        let columns: alloc::vec::Vec<ColumnSchema> = projection
4004            .iter()
4005            // v7.39 (read01 round 54) — keep the column's enum identity through
4006            // the projection (it lives outside the DataType lattice), or a
4007            // derived table / UNION / windowed result forgets it and any outer
4008            // `ORDER BY <enum col>` silently sorts by the label's TEXT.
4009            .map(|p| p.to_column_schema())
4010            .collect();
4011        // Re-evaluate ORDER BY against the source schema (pre-projection
4012        // so col refs by name still resolve through `scan_ctx`).
4013        // v7.39 (read01 round 80) — a positional key means the Nth OUTPUT
4014        // column. Evaluated as an expression it is just the constant N: the same
4015        // key for every row, so the sort ran and changed nothing.
4016        let order_by = resolve_positional_order_by(&stmt.order_by, &projection);
4017        if !order_by.is_empty() {
4018            // v7.39 (round 621) — one entry per OUTPUT row, not per input row.
4019            // A key that names a select-list item reads it out of the expanded
4020            // row (PG sorts AFTER the expansion); one that names a source
4021            // column the query does not project is evaluated on the input row
4022            // it came from, which is what `srf_order_output_cols` decides.
4023            let out_cols = if srf_idxs.is_empty() {
4024                alloc::vec![None; order_by.len()]
4025            } else {
4026                srf_order_output_cols(&order_by, &projection)
4027            };
4028            let mut indexed: alloc::vec::Vec<(usize, Vec<Value<'static>>)> = projected_rows
4029                .iter()
4030                .enumerate()
4031                .map(|(k, out)| -> Result<_, EngineError> {
4032                    let src = src_of_row.get(k).copied().unwrap_or(k);
4033                    let keys: Result<Vec<Value<'static>>, EngineError> = order_by
4034                        .iter()
4035                        .zip(out_cols.iter())
4036                        .map(|(ob, oc)| srf_order_key(ob, *oc, out, &filtered[src], &scan_ctx))
4037                        .collect();
4038                    Ok((k, keys?))
4039                })
4040                .collect::<Result<_, _>>()?;
4041            indexed.sort_by(|a, b| {
4042                for (idx, (ka, kb)) in a.1.iter().zip(b.1.iter()).enumerate() {
4043                    let o = &order_by[idx];
4044                    let cmp = order_by_value_cmp_in(
4045                        o.desc,
4046                        o.nulls_first,
4047                        ka,
4048                        kb,
4049                        scan_ctx.mysql_dialect && !crate::eval::is_binary_coerced(&o.expr),
4050                    );
4051                    if cmp != core::cmp::Ordering::Equal {
4052                        return cmp;
4053                    }
4054                }
4055                core::cmp::Ordering::Equal
4056            });
4057            projected_rows = indexed
4058                .into_iter()
4059                .map(|(i, _)| projected_rows[i].clone())
4060                .collect();
4061        }
4062        // v7.38 (read01) — DISTINCT over a synthetic source was dropped here.
4063        if stmt.distinct {
4064            // v7.38.14 — `FoldSpec::of`, not `::dialect`. The dialect-only
4065            // spec folds EVERY text position, so a column declared
4066            // `COLLATE utf8mb4_bin` had its values merged here exactly the
4067            // way 3b494b6e fixed on the main scan path. The projection is
4068            // already in scope at each of these sites, so the mask needs no
4069            // new plumbing -- it was simply never asked for.
4070            projected_rows = dedup_rows(
4071                projected_rows,
4072                FoldSpec::of_masks(
4073                    scan_ctx.mysql_dialect,
4074                    &fold_mask(&projection),
4075                    &pad_mask(&projection),
4076                ),
4077            );
4078        }
4079        // LIMIT / OFFSET — apply at the tail.
4080        if let Some(offset) = stmt.offset_literal() {
4081            let off = (offset as usize).min(projected_rows.len());
4082            projected_rows.drain(..off);
4083        }
4084        if let Some(limit) = stmt.limit_literal() {
4085            projected_rows.truncate(limit as usize);
4086        }
4087        Ok(QueryResult::Rows {
4088            columns,
4089            rows: projected_rows,
4090        })
4091    }
4092
4093    /// v7.17.0 Phase 3.10 — `FROM generate_series(start, stop [,
4094    /// step])` set-returning source. Mirrors `exec_select_unnest`'s
4095    /// shape: evaluate the arg list once against an empty row,
4096    /// materialise the row stream by stepping start → stop, then
4097    /// route through the standard WHERE / projection / ORDER BY /
4098    /// LIMIT pipeline. Two arg-type combos in v7.17:
4099    ///   * integer / integer [/ integer] — SmallInt, Int, BigInt
4100    ///     (widened to BigInt internally; step defaults to 1)
4101    ///   * timestamp / timestamp / interval — date-range
4102    ///     iteration (mailrs's daily-report pattern)
4103    fn exec_select_generate_series(
4104        &self,
4105        stmt: &SelectStatement,
4106        primary: &TableRef,
4107        cancel: CancelToken<'_>,
4108    ) -> Result<QueryResult, EngineError> {
4109        let args = primary
4110            .generate_series_args
4111            .as_ref()
4112            .expect("caller guards generate_series_args.is_some()");
4113        let (elem_dtype, rows) = generate_series_rows(args, &cancel)?;
4114        let alias = primary
4115            .alias
4116            .clone()
4117            .unwrap_or_else(|| "generate_series".to_string());
4118        // `AS t(n)` — the first column-alias entry renames the
4119        // series column (PG semantics); bare alias keeps the
4120        // pre-existing behaviour of naming the column after it.
4121        let col_name = primary
4122            .unnest_column_aliases
4123            .first()
4124            .cloned()
4125            .unwrap_or_else(|| alias.clone());
4126        let col_schema = ColumnSchema::new(col_name, elem_dtype, true);
4127        let mut schema_cols = alloc::vec![col_schema.clone()];
4128        // WITH ORDINALITY — trailing BIGINT counting rows from 1;
4129        // the second column-alias entry renames it.
4130        let rows = if primary.with_ordinality {
4131            let ord_name = primary
4132                .unnest_column_aliases
4133                .get(1)
4134                .cloned()
4135                .unwrap_or_else(|| "ordinality".to_string());
4136            schema_cols.push(ColumnSchema::new(ord_name, DataType::BigInt, false));
4137            rows.into_iter()
4138                .enumerate()
4139                .map(|(i, row)| {
4140                    let mut vals = row.values.clone();
4141                    vals.push(Value::BigInt(i as i64 + 1));
4142                    Row::new(vals)
4143                })
4144                .collect()
4145        } else {
4146            rows
4147        };
4148        // v7.39 (read01 round 54) — `ev_ctx` threads the catalog; a bare
4149        // `EvalContext::new` drops it and every catalog-dependent cast
4150        // (regclass / enum / composite / domain) silently degrades.
4151        let scan_ctx = self.ev_ctx(&schema_cols, Some(&alias));
4152        // WHERE.
4153        let filtered: alloc::vec::Vec<Row<'static>> = if let Some(w) = &stmt.where_ {
4154            let mut out = alloc::vec::Vec::with_capacity(rows.len());
4155            for row in rows {
4156                cancel.check()?;
4157                let v = eval::eval_expr(w, &row, &scan_ctx).map_err(EngineError::Eval)?;
4158                if matches!(v, Value::Bool(true)) {
4159                    out.push(row);
4160                }
4161            }
4162            out
4163        } else {
4164            rows
4165        };
4166        // v7.17.0 Phase 3.P0-48 — aggregate dispatch for set-
4167        // returning sources. When the SELECT projection contains
4168        // aggregate functions (COUNT/SUM/MIN/MAX/AVG/string_agg/
4169        // …) we route the filtered row stream through the same
4170        // aggregate executor the relational scan path uses, so
4171        // `SELECT COUNT(*) FROM generate_series(1, 100)` returns
4172        // a single 100 row instead of erroring at projection
4173        // time. GROUP BY / HAVING / ORDER BY over the aggregate
4174        // output all ride through `aggregate::run`.
4175        if aggregate::uses_aggregate_in(stmt, self.speaks_mysql) {
4176            // v7.29 — a per-query memo so correlated scalar
4177            // subqueries batch-evaluate once (group map) instead of
4178            // executing per group.
4179            let agg_memo = core::cell::RefCell::new(memoize::MemoizeCache::default());
4180            let agg_correlated = |e: &Expr, r: &Row<'static>, c: &EvalContext<'_>| {
4181                self.eval_expr_with_correlated(e, r, c, cancel, Some(&mut agg_memo.borrow_mut()))
4182                    .map_err(|err| match err {
4183                        EngineError::Eval(ev) => ev,
4184                        other => eval::EvalError::TypeMismatch {
4185                            detail: alloc::format!("{other}"),
4186                        },
4187                    })
4188            };
4189            // v7.39 (round 656) — hand the rows over as they are rather than
4190            // collecting a second vector of `RowRef` wrappers. Note this is
4191            // a set-returning-function path, NOT the relational scan: the
4192            // measured O(rows) cost lived in `run_single_table_aggregate`,
4193            // and converting these four first was a miss that cost a full
4194            // round — every test stayed green and the number did not move.
4195            let agg = aggregate::run(
4196                stmt,
4197                crate::join::AggRows::Owned(&filtered),
4198                &schema_cols,
4199                Some(&alias),
4200                Some(&agg_correlated),
4201                self.parallel_runner.0.as_deref(),
4202                Some(self.active_catalog()),
4203                Some(self),
4204            )?;
4205            return self.finish_agg_result(agg, stmt, cancel);
4206        }
4207        // Projection.
4208        let projection = build_projection(
4209            &stmt.items,
4210            &schema_cols,
4211            &alias,
4212            self.speaks_mysql,
4213            Some(self.active_catalog()),
4214        )?;
4215        // v7.39 (round 621) — and here, for the same reason.
4216        let srf_idxs = self.srf_target_idxs(&projection);
4217        let mut src_of_row: alloc::vec::Vec<usize> = alloc::vec::Vec::new();
4218        let mut projected_rows: alloc::vec::Vec<Row<'static>> =
4219            alloc::vec::Vec::with_capacity(filtered.len());
4220        let mut proj_memo = memoize::MemoizeCache::default();
4221        if !srf_idxs.is_empty() {
4222            let (rows, src) =
4223                expand_projection_srfs(self, &projection, &srf_idxs, &filtered, &scan_ctx)?;
4224            projected_rows = rows;
4225            src_of_row = src;
4226        } else {
4227            for row in &filtered {
4228                let mut vals = alloc::vec::Vec::with_capacity(projection.len());
4229                for p in &projection {
4230                    // v7.24 (round-16 B) — correlated-aware.
4231                    vals.push(self.eval_expr_with_correlated(
4232                        &p.expr,
4233                        row,
4234                        &scan_ctx,
4235                        cancel,
4236                        Some(&mut proj_memo),
4237                    )?);
4238                }
4239                projected_rows.push(Row::new(vals));
4240            }
4241        }
4242        let columns: alloc::vec::Vec<ColumnSchema> = projection
4243            .iter()
4244            // v7.39 (read01 round 54) — keep the column's enum identity through
4245            // the projection (it lives outside the DataType lattice), or a
4246            // derived table / UNION / windowed result forgets it and any outer
4247            // `ORDER BY <enum col>` silently sorts by the label's TEXT.
4248            .map(|p| p.to_column_schema())
4249            .collect();
4250        // ORDER BY against the source schema.
4251        // v7.39 (round 621) — one entry per OUTPUT row (a target-list SRF makes
4252        // more of them than there were inputs), and a positional key means the
4253        // Nth OUTPUT column, which is what `resolve_positional_order_by` does
4254        // and what the other two synthetic-source tails already did.
4255        let order_by = resolve_positional_order_by(&stmt.order_by, &projection);
4256        if !order_by.is_empty() {
4257            let out_cols = if srf_idxs.is_empty() {
4258                alloc::vec![None; order_by.len()]
4259            } else {
4260                srf_order_output_cols(&order_by, &projection)
4261            };
4262            let mut indexed: alloc::vec::Vec<(usize, Vec<Value<'static>>)> = projected_rows
4263                .iter()
4264                .enumerate()
4265                .map(|(k, out)| -> Result<_, EngineError> {
4266                    let r = &filtered[src_of_row.get(k).copied().unwrap_or(k)];
4267                    let keys: Result<Vec<Value<'static>>, EngineError> = order_by
4268                        .iter()
4269                        .zip(out_cols.iter())
4270                        .map(|(ob, oc)| srf_order_key(ob, *oc, out, r, &scan_ctx))
4271                        .collect();
4272                    Ok((k, keys?))
4273                })
4274                .collect::<Result<_, _>>()?;
4275            indexed.sort_by(|a, b| {
4276                for (idx, (ka, kb)) in a.1.iter().zip(b.1.iter()).enumerate() {
4277                    let o = &stmt.order_by[idx];
4278                    let cmp = order_by_value_cmp_in(
4279                        o.desc,
4280                        o.nulls_first,
4281                        ka,
4282                        kb,
4283                        scan_ctx.mysql_dialect && !crate::eval::is_binary_coerced(&o.expr),
4284                    );
4285                    if cmp != core::cmp::Ordering::Equal {
4286                        return cmp;
4287                    }
4288                }
4289                core::cmp::Ordering::Equal
4290            });
4291            projected_rows = indexed
4292                .into_iter()
4293                .map(|(i, _)| projected_rows[i].clone())
4294                .collect();
4295        }
4296        // v7.38 (read01) — DISTINCT over a synthetic source was dropped here.
4297        if stmt.distinct {
4298            // v7.38.14 — `FoldSpec::of`, not `::dialect`. The dialect-only
4299            // spec folds EVERY text position, so a column declared
4300            // `COLLATE utf8mb4_bin` had its values merged here exactly the
4301            // way 3b494b6e fixed on the main scan path. The projection is
4302            // already in scope at each of these sites, so the mask needs no
4303            // new plumbing -- it was simply never asked for.
4304            projected_rows = dedup_rows(
4305                projected_rows,
4306                FoldSpec::of_masks(
4307                    scan_ctx.mysql_dialect,
4308                    &fold_mask(&projection),
4309                    &pad_mask(&projection),
4310                ),
4311            );
4312        }
4313        if let Some(offset) = stmt.offset_literal() {
4314            let off = (offset as usize).min(projected_rows.len());
4315            projected_rows.drain(..off);
4316        }
4317        if let Some(limit) = stmt.limit_literal() {
4318            projected_rows.truncate(limit as usize);
4319        }
4320        Ok(QueryResult::Rows {
4321            columns,
4322            rows: projected_rows,
4323        })
4324    }
4325
4326    /// The FROM shapes that are not an ordinary table scan — joins, the
4327    /// set-returning sources, JSON_TABLE, a derived table, and the rest.
4328    ///
4329    /// `#[inline(never)]` and out of `exec_bare_select_cancel` for the
4330    /// reason round 848 established in the parser: a debug build gives
4331    /// EVERY branch's locals a slot in the frame, whichever branch runs.
4332    /// `exec_bare_select_cancel` measured 64,784 bytes and a nested query
4333    /// stacks several of them; a plain scan reaches none of these
4334    /// branches. Moving them out took the frame to 52,336.
4335    ///
4336    /// `Ok(None)` means "not one of these shapes, carry on".
4337    #[inline(never)]
4338    fn try_from_shape_paths(
4339        &self,
4340        stmt: &SelectStatement,
4341        from: &spg_sql::ast::FromClause,
4342        cancel: CancelToken<'_>,
4343    ) -> Result<Option<QueryResult>, EngineError> {
4344        if !from.joins.is_empty() {
4345            // v7.37.x (docker-fair LEFTJOIN 71 % attack) — LEFT JOIN
4346            // elimination: when a LEFT JOIN's right side is referenced
4347            // ONLY in the ON equality and the right-side join key is
4348            // UNIQUE/PK, the join preserves outer cardinality exactly
4349            // and contributes no values used downstream. Drop the
4350            // entire join. PG does this on the
4351            // `SELECT COUNT(*) FROM A LEFT JOIN B ON B.pk = A.fk` shape
4352            // — A's row count is what survives, B never has to be
4353            // touched.
4354            if let Some(eliminated) = self.try_eliminate_redundant_left_joins(stmt) {
4355                return self.exec_bare_select_cancel(&eliminated, cancel).map(Some);
4356            }
4357            // v7.38 P0 元机制 D — `SPG_TEST_DISABLE_JOINFOLD=1` skips
4358            // the v7.32 joinfold rewrite that turns inner JOINs into a
4359            // single-table scan when the catalogue can prove key-only
4360            // dependency. Tests use this to assert "without joinfold,
4361            // the join still executes correctly" (joinfold is a
4362            // semantically-equivalent rewrite, not a correctness fix).
4363            if !self.env_cfg().disable_joinfold {
4364                if let Some(folded) = self.try_fold_inner_joins(stmt, cancel)? {
4365                    return self.exec_bare_select_cancel(&folded, cancel).map(Some);
4366                }
4367            }
4368            return self.exec_joined_select(stmt, from, cancel).map(Some);
4369        }
4370        // v7.11.7 — `FROM unnest(<expr>) [AS] <alias>`. Synthesise a
4371        // single-column table at SELECT entry by evaluating the
4372        // expression once against the empty row (UNNEST is
4373        // uncorrelated in v7.11; correlated / LATERAL unnest is a
4374        // v7.12 carve-out). Build a virtual `Table` in a heap-only
4375        // catalog, then route to the regular scan path.
4376        if from.primary.unnest_expr.is_some() {
4377            return self
4378                .exec_select_unnest(stmt, &from.primary, cancel)
4379                .map(Some);
4380        }
4381        // v7.37.43-T4.5 — `FROM jsonb_each_text(<expr>)` set-
4382        // returning function. Same dispatch shape as unnest but
4383        // emits a two-column (key TEXT, value TEXT) row stream.
4384        if from.primary.jsonb_each_text_arg.is_some() {
4385            return self
4386                .exec_select_jsonb_each_text(stmt, &from.primary, cancel)
4387                .map(Some);
4388        }
4389        // v7.39 (read01 partitionfuncs.c) — FROM-position table functions
4390        // (pg_partition_tree / pg_partition_ancestors) dispatched by name.
4391        // v7.39 (read01 round 74) — `ROWS FROM (f(a), g(b))` whose entries have no
4392        // array form. Each function runs; the results zip in LOCKSTEP with the
4393        // shorter padded to NULL — the SAME rule the target-list SRFs follow
4394        // (round 67), which is why `srf_values` is what evaluates each entry.
4395        if from.primary.rows_from.is_some() {
4396            let (rows, mut schema_cols) = self.rows_from_rows(&from.primary)?;
4397            for (i, new_name) in from.primary.unnest_column_aliases.iter().enumerate() {
4398                if let Some(col) = schema_cols.get_mut(i) {
4399                    col.name = new_name.clone();
4400                }
4401            }
4402            let alias = from
4403                .primary
4404                .alias
4405                .clone()
4406                .unwrap_or_else(|| from.primary.name.clone());
4407            return self
4408                .exec_select_over_rows(stmt, rows, schema_cols, &alias, cancel)
4409                .map(Some);
4410        }
4411        // v7.39 (round 205, JSON_TABLE) — `FROM JSON_TABLE(doc, '$p'
4412        // COLUMNS (...))`. Materialise the row stream + schema by
4413        // walking the row path, then run the regular pipeline over it.
4414        if let Some(jt) = &from.primary.json_table {
4415            let (rows, schema_cols) = self.json_table_rows(jt, None)?;
4416            let alias = from
4417                .primary
4418                .alias
4419                .clone()
4420                .unwrap_or_else(|| from.primary.name.clone());
4421            return self
4422                .exec_select_over_rows(stmt, rows, schema_cols, &alias, cancel)
4423                .map(Some);
4424        }
4425        if from.primary.table_fn_call.is_some() {
4426            let (rows, mut schema_cols) = self.table_fn_rows(&from.primary)?;
4427            // v7.39 (read01 round 68) — WITH ORDINALITY appends a BIGINT counter
4428            // (from 1, in output order) AFTER the function's own columns. The
4429            // alias list names it like any other, which is why it is appended
4430            // BEFORE the renaming pass below.
4431            let rows = if from.primary.with_ordinality {
4432                schema_cols.push(ColumnSchema::new(
4433                    "ordinality".to_string(),
4434                    DataType::BigInt,
4435                    false,
4436                ));
4437                rows.into_iter()
4438                    .enumerate()
4439                    .map(|(i, r)| {
4440                        let mut vals = r.values;
4441                        vals.push(Value::BigInt(i as i64 + 1));
4442                        Row::new(vals)
4443                    })
4444                    .collect()
4445            } else {
4446                rows
4447            };
4448            for (i, new_name) in from.primary.unnest_column_aliases.iter().enumerate() {
4449                if let Some(col) = schema_cols.get_mut(i) {
4450                    col.name = new_name.clone();
4451                }
4452            }
4453            let alias = from
4454                .primary
4455                .alias
4456                .clone()
4457                .unwrap_or_else(|| from.primary.name.clone());
4458            return self
4459                .exec_select_over_rows(stmt, rows, schema_cols, &alias, cancel)
4460                .map(Some);
4461        }
4462        // v7.37.17 (17.6 siblings) — plain derived table in primary
4463        // position: `FROM ( SELECT … ) alias` (no joins). The inner
4464        // SELECT materialises once (it is uncorrelated by
4465        // construction), then the outer projection / WHERE /
4466        // aggregate / ORDER BY pipeline runs over the synthetic
4467        // table. Joined derived tables keep riding the LATERAL
4468        // machinery in join.rs.
4469        if from.joins.is_empty() && from.primary.lateral_subquery.is_some() {
4470            // v7.39 (round 727) — flatten first. A simple derived table
4471            // (bare-column projection over one stored table, nothing that
4472            // changes cardinality or order) used to force the inner
4473            // SELECT through the SERIAL row-at-a-time projection pipeline
4474            // just to materialise a synthetic table the outer query then
4475            // re-scans: `count(*) FROM (SELECT id v FROM d WHERE …) q`
4476            // measured 18.6 ms against PG's 5 — and bare count over the
4477            // same filter WITHOUT the wrapper is 2 ms here, because it
4478            // rides the fused parallel lane. Rewriting to the unwrapped
4479            // form is PG's subquery pull-up; the whole tree gets the
4480            // fast lanes back.
4481            if let Some(flat) = try_flatten_derived(stmt, &from.primary) {
4482                return self.exec_select_cancel(&flat, cancel).map(Some);
4483            }
4484            // v7.39 (round 742) — `SELECT count(*) FROM (SELECT … ORDER
4485            // BY … OFFSET k) q` is `greatest(count_of_inner - k, 0)`:
4486            // ORDER BY never changes the row count, and OFFSET drops
4487            // exactly k. The materialising path sorted 500k rows to
4488            // count 10k (57 ms); PG runs its parallel sort anyway
4489            // (28 ms). The rewrite skips the sort entirely on both
4490            // counts — a plan PG itself does not have.
4491            if let Some(rewritten) = try_count_over_offset(stmt, &from.primary) {
4492                return self.exec_select_cancel(&rewritten, cancel).map(Some);
4493            }
4494            // v7.39 (round 743) — `count(*) OVER a derived whose only
4495            // item is unnest(ARRAY[k elements])` is `k * count(WHERE)`:
4496            // a constant-length array unnests to exactly k rows per
4497            // input row, NULL elements included. PG expands the set to
4498            // count it (6.6 ms on the panel cell); the identity doesn't.
4499            if let Some(rewritten) = try_count_over_const_unnest(stmt, &from.primary) {
4500                return self.exec_select_cancel(&rewritten, cancel).map(Some);
4501            }
4502            return self
4503                .exec_select_derived(stmt, &from.primary, cancel)
4504                .map(Some);
4505        }
4506        // v7.17.0 Phase 3.10 — `FROM generate_series(start, stop
4507        // [, step])` set-returning source. Dispatch mirrors UNNEST:
4508        // materialise the row stream from a single eval pass, then
4509        // run the regular projection / WHERE / ORDER BY / LIMIT
4510        // pipeline over the synthetic single-column table.
4511        if from.primary.generate_series_args.is_some() {
4512            return self
4513                .exec_select_generate_series(stmt, &from.primary, cancel)
4514                .map(Some);
4515        }
4516        Ok(None)
4517    }
4518
4519    /// Pick an index seek for this WHERE, if any of the four apply:
4520    /// BTree equality, GIN `@@`, trigram LIKE, or JSONB `@>`.
4521    ///
4522    /// `#[inline(never)]` and out of `exec_bare_select_cancel` for the
4523    /// frame reason on `try_from_shape_paths`: in a debug build a
4524    /// closure's locals belong to the enclosing frame, and this one is
4525    /// four seek attempts wide on a function that nests.
4526    #[inline(never)]
4527    fn pick_indexed_rows<'r>(
4528        &'r self,
4529        stmt: &SelectStatement,
4530        table: &'r spg_storage::Table,
4531        schema_cols: &[spg_storage::ColumnSchema],
4532        alias: &str,
4533        ctx: &crate::eval::EvalContext<'_>,
4534        seek_snapshot: &crate::Snapshot,
4535    ) -> Option<crate::index_access::Seeked<'r>> {
4536        stmt.where_.as_ref().and_then(|w| {
4537            // BTree / col=literal seek first — covers the v7.11.3 multi-
4538            // column AND case and the leading-column equality lookup.
4539            try_index_seek(
4540                w,
4541                schema_cols,
4542                self.active_catalog(),
4543                table,
4544                alias,
4545                seek_snapshot,
4546                ctx.mysql_dialect,
4547            )
4548            .or_else(|| {
4549                // v7.12.3 — GIN-accelerated `WHERE col @@
4550                // tsquery` when the column has a `USING gin`
4551                // index. Returns an over-approximate candidate
4552                // set; the WHERE re-eval loop below verifies
4553                // the full `@@` predicate per row.
4554                try_gin_seek(
4555                    w,
4556                    schema_cols,
4557                    self.active_catalog(),
4558                    table,
4559                    alias,
4560                    ctx,
4561                    seek_snapshot,
4562                )
4563                .map(crate::index_access::Seeked::over_approximate)
4564            })
4565            .or_else(|| {
4566                // v7.15.0 — trigram-GIN-accelerated
4567                // `WHERE col LIKE / ILIKE '<pat>'` when the
4568                // column has a `gin_trgm_ops` GIN index.
4569                // Over-approximate candidate set; the WHERE
4570                // re-eval verifies the LIKE per row.
4571                try_trgm_seek(w, schema_cols, table, alias, seek_snapshot)
4572                    .map(crate::index_access::Seeked::over_approximate)
4573            })
4574            .or_else(|| {
4575                // v7.37.8(sentori Epic 5 P2)— real JSONB-GIN
4576                // accelerated `WHERE col @> <jsonb_literal>`
4577                // when the column has a `USING gin` index. The
4578                // posting-list intersection returns an over-
4579                // approximate candidate set; the WHERE re-eval
4580                // verifies the full `@>` predicate per row.
4581                try_gin_jsonb_seek(w, schema_cols, table, alias, seek_snapshot)
4582                    .map(crate::index_access::Seeked::over_approximate)
4583            })
4584        })
4585    }
4586
4587    /// Index-seek fast paths: NSW kNN, the primary-key top-N walk, and
4588    /// the two `count(*)` short-circuits. Out-of-line for the frame
4589    /// reason on `try_from_shape_paths` — an ordinary scan reaches none
4590    /// of them, and in a debug build their locals sit in the frame
4591    /// regardless.
4592    #[inline(never)]
4593    fn try_seek_fast_paths(
4594        &self,
4595        stmt: &SelectStatement,
4596        table: &spg_storage::Table,
4597        schema_cols: &[spg_storage::ColumnSchema],
4598        alias: &str,
4599        seek_snapshot: &crate::Snapshot,
4600        cancel: CancelToken<'_>,
4601    ) -> Result<Option<QueryResult>, EngineError> {
4602        if let Some(nsw_rows) = try_nsw_knn(stmt, table, schema_cols, alias, seek_snapshot) {
4603            // NSW kNN dispatches against the hot-tier vector index only
4604            // (vector cells aren't promoted to cold segments), so wrap
4605            // the returned row indices as `Cow::Borrowed` for the
4606            // unified `materialise_in_order` shape.
4607            let ordered: Vec<Cow<'_, Row<'static>>> = nsw_rows
4608                .into_iter()
4609                .filter_map(|i| table.rows().get(i).map(Cow::Borrowed))
4610                .collect();
4611            return materialise_in_order(stmt, schema_cols, alias, &ordered, self.speaks_mysql)
4612                .map(Some);
4613        }
4614
4615        // v7.34.5 — ORDER BY <indexed col> [DESC|ASC] LIMIT N drives
4616        // the scan via the BTree iterator in the requested direction
4617        // and stops after `OFFSET + LIMIT` candidates pass WHERE. The
4618        // 80 ms `mailrs_prod_plain_limit` baseline at 250 k rows is
4619        // the load-bearing consumer; this skips the materialise-every-
4620        // row + partial-sort tail entirely. Walker output is already
4621        // in ORDER BY order so `materialise_in_order` (no extra sort)
4622        // is the natural sink.
4623        if let Some(walked) = try_pk_walk_top_n(
4624            stmt,
4625            self.active_catalog(),
4626            table,
4627            schema_cols,
4628            alias,
4629            self,
4630            cancel,
4631            self.speaks_mysql,
4632        ) {
4633            return materialise_in_order(stmt, schema_cols, alias, &walked, self.speaks_mysql)
4634                .map(Some);
4635        }
4636
4637        // Index seek: if WHERE is `col = literal` (or commuted) and the
4638        // referenced column has an index, dispatch each locator through
4639        // the catalog (hot tier → borrow, cold tier → page-read +
4640        // decode) and iterate just those rows. Otherwise fall back to a
4641        // v7.37.x (docker-fair INSUBQ attack) — short-circuit COUNT(*)
4642        // FROM A WHERE A.pk IN (large literal list). The post-subquery-
4643        // replacement shape of INSUBQ. Runs BEFORE `indexed_rows` so
4644        // we don't pay the row materialisation cost twice. Returns
4645        // a bare `Rows{count}` if the shape matches.
4646        if aggregate::uses_aggregate_in(stmt, self.speaks_mysql)
4647            && let Some(out) = self.try_count_star_pk_in_list_fast(stmt, table, schema_cols, alias)
4648        {
4649            return Ok(Some(out));
4650        }
4651        // v7.38 (perf) — `count(*) WHERE <indexed BETWEEN>`: count the in-range
4652        // locators directly, skipping row materialisation + WHERE re-eval.
4653        if aggregate::uses_aggregate_in(stmt, self.speaks_mysql)
4654            && let Some(out) = self.try_count_star_indexed_range_fast(
4655                stmt,
4656                table,
4657                schema_cols,
4658                alias,
4659                seek_snapshot,
4660            )
4661        {
4662            return Ok(Some(out));
4663        }
4664        Ok(None)
4665    }
4666
4667    /// The two rewrites that must happen before the FROM clause is even
4668    /// looked at: a meta-view reference needs the catalog views
4669    /// materialised, and a windowed projection belongs to the window
4670    /// executor. Out-of-line for the frame reason on
4671    /// `try_from_shape_paths`.
4672    #[inline(never)]
4673    fn try_pre_from_paths(
4674        &self,
4675        stmt: &SelectStatement,
4676        cancel: CancelToken<'_>,
4677    ) -> Result<Option<QueryResult>, EngineError> {
4678        if !self.meta_views_materialised && select_references_meta_view(stmt) {
4679            return self.exec_select_with_meta_views(stmt, cancel).map(Some);
4680        }
4681        // v4.12: window-function path. When the projection contains
4682        // any `name(args) OVER (...)` we route to the dedicated
4683        // executor — partition + sort + per-row window value before
4684        // the regular projection.
4685        if select_has_window(stmt) {
4686            // v7.37 D.23 — window functions run AFTER GROUP BY aggregation.
4687            // `SELECT g, sum(v), rank() OVER (ORDER BY sum(v)) FROM t GROUP BY g`
4688            // needs the aggregation done first, then windows over the grouped
4689            // rows. Rewrite to an aggregate derived subquery + outer window query
4690            // (which the window-over-derived path, D.13, executes). Only fires on
4691            // the currently-erroring agg+window+GROUP BY shape, so it can't
4692            // regress working window-only or aggregate-only queries.
4693            if let Some(rewritten) = rewrite_agg_before_window(stmt) {
4694                return self.exec_select_cancel(&rewritten, cancel).map(Some);
4695            }
4696            return self.exec_select_with_window(stmt, cancel).map(Some);
4697        }
4698        Ok(None)
4699    }
4700
4701    /// A projection naming `ctid` or another system column: the schema
4702    /// has to be widened with them before the scan. Out-of-line for the
4703    /// frame reason on `try_from_shape_paths`.
4704    #[inline(never)]
4705    fn try_ctid_projection(
4706        &self,
4707        stmt: &SelectStatement,
4708        primary: &spg_sql::ast::TableRef,
4709        table: &spg_storage::Table,
4710        schema_cols: &[spg_storage::ColumnSchema],
4711        alias: &str,
4712        cancel: CancelToken<'_>,
4713    ) -> Result<Option<QueryResult>, EngineError> {
4714        if references_ctid(stmt) {
4715            let snapshot = self.current_snapshot();
4716            let mut ext_cols = schema_cols.to_vec();
4717            for name in SYSTEM_COLUMNS {
4718                ext_cols.push(ColumnSchema::new(name.to_string(), DataType::Text, false));
4719            }
4720            let table_oid =
4721                crate::system_catalog::relation_oid(self.active_catalog(), &primary.name)
4722                    .unwrap_or(0);
4723            let headers = table.headers();
4724            let rows: Vec<Row<'static>> = table
4725                .scan_visible(&snapshot)
4726                .map(|(i, r)| {
4727                    let mut vals = r.values.clone();
4728                    // One block, offsets from 1, as PG numbers them.
4729                    vals.push(Value::Tid(0, i as u32 + 1));
4730                    let h = headers.get(i);
4731                    vals.push(Value::Xid(h.map_or(0, |h| h.xmin as u32)));
4732                    vals.push(Value::Xid(h.map_or(0, |h| h.xmax as u32)));
4733                    // SPG keeps no per-statement command ids; PG shows 0 for
4734                    // every row a reader can see, which is every row here.
4735                    vals.push(Value::Cid(0));
4736                    vals.push(Value::Cid(0));
4737                    vals.push(Value::BigInt(table_oid));
4738                    Row::new(vals)
4739                })
4740                .collect();
4741            return self
4742                .exec_select_over_rows(stmt, rows, ext_cols, alias, cancel)
4743                .map(Some);
4744        }
4745        Ok(None)
4746    }
4747
4748    /// A sequence read as a one-row relation (`SELECT last_value FROM
4749    /// seq`), which PG allows and psql's \\d relies on. Out-of-line for
4750    /// the frame reason on `try_from_shape_paths`.
4751    #[inline(never)]
4752    fn try_sequence_relation(
4753        &self,
4754        stmt: &SelectStatement,
4755        primary: &spg_sql::ast::TableRef,
4756        cancel: CancelToken<'_>,
4757    ) -> Result<Option<QueryResult>, EngineError> {
4758        if self.active_catalog().get(&primary.name).is_none()
4759            && let Some(seq) = self.active_catalog().sequence(&primary.name)
4760        {
4761            let rows = alloc::vec![Row::new(alloc::vec![
4762                Value::BigInt(seq.last_value),
4763                Value::BigInt(0),
4764                Value::Bool(seq.is_called),
4765            ])];
4766            let schema_cols = alloc::vec![
4767                ColumnSchema::new("last_value", DataType::BigInt, false),
4768                ColumnSchema::new("log_cnt", DataType::BigInt, false),
4769                ColumnSchema::new("is_called", DataType::Bool, false),
4770            ];
4771            let alias = primary
4772                .alias
4773                .clone()
4774                .unwrap_or_else(|| primary.name.clone());
4775            return self
4776                .exec_select_over_rows(stmt, rows, schema_cols, &alias, cancel)
4777                .map(Some);
4778        }
4779        Ok(None)
4780    }
4781
4782    pub(crate) fn exec_bare_select_cancel(
4783        &self,
4784        stmt: &SelectStatement,
4785        cancel: CancelToken<'_>,
4786    ) -> Result<QueryResult, EngineError> {
4787        // v7.17.0 Phase 3.P0-49 — `FETCH FIRST N ROWS WITH TIES`
4788        // is meaningless without an ORDER BY; PG raises a hard
4789        // error and SPG mirrors the surface so the same DDL/app
4790        // path behaves identically on cutover.
4791        check_with_ties_requires_order_by(stmt)?;
4792        // v7.39 (round 229) — WHERE / HAVING run before the window pass, so
4793        // PG rejects window calls there outright. Checked here rather than
4794        // on the window path: `HAVING row_number() OVER () = 1` has no
4795        // window in its projection at all.
4796        crate::window::reject_window_in_row_clauses(stmt)?;
4797        // v7.39 (round 232) — the ORDER BY legality rules (positional
4798        // bounds, DISTINCT, DISTINCT ON). Same placement as the window
4799        // check: before anything scans.
4800        crate::orderby::check_order_by_legality(stmt)?;
4801        // v7.37.16 — resolve `USING` column-merge + `NATURAL JOIN` into an
4802        // equivalent statement the regular executor handles (merged join
4803        // columns collapse to a single unqualified output column; NATURAL
4804        // gets its common-column ON synthesised). The rewrite clears the
4805        // flags, so this re-entrant call is a no-op on the second pass.
4806        if let Some(rewritten) = self.desugar_using_natural(stmt)? {
4807            return self.exec_bare_select_cancel(&rewritten, cancel);
4808        }
4809        // v7.38.13 — a GROUP BY with no aggregate, whose select list is
4810        // exactly the group keys, IS a DISTINCT and was paying for the
4811        // aggregate executor to find that out. Same placement and shape
4812        // as the desugar above; the rewrite clears `group_by`, so the
4813        // re-entry is a no-op on the second pass. See `baregroup` for
4814        // what the gate rules out.
4815        if let Some(rewritten) = crate::baregroup::as_distinct(stmt) {
4816            return self.exec_bare_select_cancel(&rewritten, cancel);
4817        }
4818        // v7.39 (RLS) Phase 3 — cross-table joins: wrap each RLS-enabled join
4819        // operand in a security-barrier subquery, then re-enter (the wrapped
4820        // operands are no longer bare RLS tables, so this is a no-op on the
4821        // second pass).
4822        if let Some(rewritten) = self.rls_rewrite_joins(stmt) {
4823            return self.exec_bare_select_cancel(&rewritten, cancel);
4824        }
4825        // v7.39 (RLS) Phase 1 — for a policy-subject (non-superuser) session,
4826        // AND the RLS USING predicate into a single-table SELECT's WHERE.
4827        // Superuser sessions and non-RLS tables get `None` (no clone, no
4828        // change). Applied inline (shadowing `stmt`) rather than via re-entry
4829        // so it can't re-inject on a recursive pass.
4830        let rls_stmt;
4831        let stmt = match self.rls_select_predicate(stmt)? {
4832            Some(pred) => {
4833                let mut s = stmt.clone();
4834                s.where_ = Some(match s.where_.take() {
4835                    Some(existing) => spg_sql::ast::Expr::Binary {
4836                        lhs: alloc::boxed::Box::new(existing),
4837                        op: spg_sql::ast::BinOp::And,
4838                        rhs: alloc::boxed::Box::new(pred),
4839                    },
4840                    None => pred,
4841                });
4842                rls_stmt = s;
4843                &rls_stmt
4844            }
4845            None => stmt,
4846        };
4847        // v7.16.2 — same meta-view dispatch as
4848        // `exec_select_cancel`, applied here too because
4849        // `subquery_replacement` enters this function directly
4850        // for Exists / ScalarSubquery / InSubquery resolution
4851        // (bypassing the top-level entry to avoid double
4852        // subquery walking). Without this dispatch the subquery
4853        // hits `__spg_info_columns` and reports TableNotFound.
4854        if let Some(done) = self.try_pre_from_paths(stmt, cancel)? {
4855            return Ok(done);
4856        }
4857        // Constant SELECT (no FROM) — evaluate each item once against an
4858        // empty dummy row. Useful for `SELECT 1`, `SELECT coalesce(...)`,
4859        // `SELECT '7'::INT`. Column references will surface as
4860        // ColumnNotFound on eval since the schema is empty.
4861        let Some(from) = &stmt.from else {
4862            return self.exec_constant_select(stmt);
4863        };
4864        // Multi-table FROM (one or more joined peers) goes through the
4865        // nested-loop join executor. Single-table FROM stays on the
4866        // existing scan + index-seek path.
4867        if let Some(done) = self.try_from_shape_paths(stmt, from, cancel)? {
4868            return Ok(done);
4869        }
4870        // NOT hooked up. `try_spill_sorted_scan` is written, correct and
4871        // tested — eight ORDER BY shapes byte-identical spilled against
4872        // in-memory, with 103 runs opened to prove the spill ran — and it
4873        // loses on wall clock, which is a hard stop whatever the memory
4874        // buys. Measured round 865, same psql client both sides, same
4875        // machine, row counts verified, and both sides confirmed to be
4876        // doing an external merge rather than an indexed walk:
4877        //
4878        //   PG18        178.7 - 187.0 ms   Sort Method: external merge, 85 MB
4879        //   SPG spilled 269.7 - 299.6 ms   33 spill files at peak
4880        //
4881        // Non-overlapping, about 1.55x. Re-enable by restoring the call
4882        // below once that closes; nothing else has to change, which is
4883        // the point of it being a separate path.
4884        //
4885        //   if let Some(done) = self.try_spill_sorted_scan(stmt, from, cancel)? {
4886        //       return Ok(done);
4887        //   }
4888        //
4889        // v7.37 (round 882) — this walk stays unhooked, but its streaming
4890        // twin `try_spill_sorted_stream` IS hooked, above the ORDER BY
4891        // bail in `try_exec_joined_streaming`. Collecting the answer was
4892        // most of what this one cost: handing rows over as the merge
4893        // produces them holds peak to the budget plus one row, and the
4894        // wall clock lands inside PG18's range rather than 1.55x outside
4895        // it. Numbers in `extsort.rs`'s header.
4896        let primary = &from.primary;
4897        // v7.39 (round 244) — a sequence is selectable as a one-row relation
4898        // in PG (`SELECT last_value FROM seq` — psql's \d and several ORMs
4899        // read it). Synthesize PG's three columns.
4900        if let Some(done) = self.try_sequence_relation(stmt, primary, cancel)? {
4901            return Ok(done);
4902        }
4903        let table = self.active_catalog().get(&primary.name).ok_or_else(|| {
4904            StorageError::TableNotFound {
4905                name: primary.name.clone(),
4906            }
4907        })?;
4908        let schema_cols = &table.schema().columns;
4909        // The qualifier accepted on column refs is the alias (if any) else the
4910        // bare table name.
4911        let alias = primary.alias.as_deref().unwrap_or(primary.name.as_str());
4912        // v7.39 (round 511) — `ctid`, PG's physical row identity. SPG had no
4913        // system columns at all: `SELECT ctid FROM t` answered "column
4914        // \"ctid\" does not exist", which takes out the dedup idiom every
4915        // PG user knows — `DELETE … WHERE ctid NOT IN (SELECT min(ctid) …
4916        // GROUP BY key)`.
4917        //
4918        // The value comes from the row's position, which the scan already
4919        // yields; the column is appended to the schema and the rows only
4920        // when the statement asks for it, so nothing else pays for it. That
4921        // also routes the query down the general path, past the index fast
4922        // paths below — they hand back rows without positions, and a ctid
4923        // that was sometimes right would be worse than none.
4924        if let Some(done) =
4925            self.try_ctid_projection(stmt, primary, table, schema_cols, alias, cancel)?
4926        {
4927            return Ok(done);
4928        }
4929        let ctx = self.ev_ctx(schema_cols, Some(alias));
4930
4931        // NSW kNN planner: `ORDER BY col <-> literal LIMIT k` with no
4932        // WHERE and an NSW index on `col` skips the full scan. The
4933        // walk returns rows already in ascending-distance order, so
4934        // ORDER BY / LIMIT are honoured implicitly.
4935        // Phase C.3 step 2c — compute the reader's MVCC snapshot once
4936        // and thread it into every index-seek fast path below. No-op
4937        // today (every hot header is committed-alive).
4938        let seek_snapshot = self.current_snapshot();
4939        if let Some(done) =
4940            self.try_seek_fast_paths(stmt, table, schema_cols, alias, &seek_snapshot, cancel)?
4941        {
4942            return Ok(done);
4943        }
4944        // full scan over the hot tier (cold-tier rows are only reached
4945        // via index seek in v5.1 — full table scans against cold-tier
4946        // data ship in v5.2 with the freezer's per-segment scan API).
4947        let indexed_rows =
4948            self.pick_indexed_rows(stmt, table, schema_cols, alias, &ctx, &seek_snapshot);
4949
4950        // Aggregate path: filter rows first, then hand off to the
4951        // aggregate executor which does its own projection + ORDER BY.
4952        if aggregate::uses_aggregate_in(stmt, self.speaks_mysql) {
4953            return self.run_single_table_aggregate(
4954                stmt,
4955                table,
4956                schema_cols,
4957                alias,
4958                indexed_rows,
4959                cancel,
4960            );
4961        }
4962        self.run_single_table_scan(stmt, table, schema_cols, alias, indexed_rows, cancel)
4963    }
4964
4965    /// v7.37.43-T4.5 — execute `SELECT … FROM jsonb_each_text(<expr>)`.
4966    /// Sentori migration 0067 uses this with `CROSS JOIN LATERAL`; the
4967    /// uncorrelated FROM-primary case is the simpler shape, used by
4968    /// e2e pins. Materialises the (key, value) pair stream into a
4969    /// synthetic two-column TEXT table, then routes through the
4970    /// regular projection / WHERE / ORDER BY pipeline.
4971    /// v7.39 (read01 partitionfuncs.c) — materialise a FROM-position
4972    /// v7.39 (round 205, JSON_TABLE) — materialise a JSON_TABLE FROM
4973    /// item into (rows, schema). `outer_doc` is `Some` only when this
4974    /// is a NESTED level being expanded against a parent row item's
4975    /// already-parsed sub-document; the top-level call parses the doc
4976    /// expr itself. Row/column paths reuse the existing jsonpath
4977    /// evaluator (`json::json_table_path`); coercion reuses
4978    /// `coerce_value` on the JSON scalar text, so a json string
4979    /// coerces to DATE by its content, matching PG.
4980    #[allow(clippy::type_complexity)]
4981    pub(crate) fn json_table_rows(
4982        &self,
4983        jt: &spg_sql::ast::JsonTable,
4984        outer_doc: Option<&crate::json::JsonValue>,
4985    ) -> Result<(alloc::vec::Vec<Row<'static>>, alloc::vec::Vec<ColumnSchema>), EngineError> {
4986        // Column schema is static (independent of data): flatten the
4987        // COLUMNS tree in declaration order (NESTED contributes its
4988        // children inline, the PG output shape).
4989        let schema = json_table_schema(&jt.columns);
4990
4991        // PASSING variables → a single JsonValue object the jsonpath
4992        // engine reads `$name` from.
4993        let empty_schema: alloc::vec::Vec<ColumnSchema> = alloc::vec::Vec::new();
4994        let ctx = EvalContext::new(&empty_schema, None);
4995        let dummy = Row::new(alloc::vec::Vec::new());
4996        let vars: Option<crate::json::JsonValue> = if jt.passing.is_empty() {
4997            None
4998        } else {
4999            let mut entries = alloc::vec::Vec::new();
5000            for (name, e) in &jt.passing {
5001                let v = eval::eval_expr(e, &dummy, &ctx).map_err(EngineError::Eval)?;
5002                entries.push((name.clone(), value_to_json_value(&v)));
5003            }
5004            Some(crate::json::JsonValue::Object(entries))
5005        };
5006
5007        // The document root: a NESTED level gets it from the parent;
5008        // the top level parses its doc expr.
5009        let root_owned;
5010        let root: &crate::json::JsonValue = match outer_doc {
5011            Some(d) => d,
5012            None => {
5013                let doc_val = eval::eval_expr(&jt.doc, &dummy, &ctx).map_err(EngineError::Eval)?;
5014                let src = match &doc_val {
5015                    Value::Null => return Ok((alloc::vec::Vec::new(), schema)),
5016                    Value::Json(s) | Value::Text(s) => s.as_ref().to_string(),
5017                    other => {
5018                        return Err(EngineError::Unsupported(alloc::format!(
5019                            "JSON_TABLE document must be json/text, got {}",
5020                            crate::conversions::pg_type_name_for_error_opt(other.data_type())
5021                        )));
5022                    }
5023                };
5024                root_owned = crate::json::parse_doc(&src).map_err(EngineError::Eval)?;
5025                &root_owned
5026            }
5027        };
5028
5029        let items = crate::json::json_table_path(root, &jt.row_path, vars.as_ref())
5030            .map_err(EngineError::Eval)?;
5031        let mut rows: alloc::vec::Vec<Row<'static>> = alloc::vec::Vec::new();
5032        for (idx, item) in items.iter().enumerate() {
5033            self.json_table_emit_item(jt, item, idx, vars.as_ref(), &mut rows)?;
5034        }
5035        Ok((rows, schema))
5036    }
5037
5038    /// v7.39 (round 205) — emit the row(s) for one row-pattern item.
5039    /// Regular columns produce one value each; a NESTED column expands
5040    /// as an outer join (each nested match → one row sharing the
5041    /// parent cells; no nested match → one row with the nested cells
5042    /// NULL). Sibling NESTED at one level cross by concatenation of
5043    /// their independent expansions (PG's UNION-of-outer shape).
5044    fn json_table_emit_item(
5045        &self,
5046        jt: &spg_sql::ast::JsonTable,
5047        item: &crate::json::JsonValue,
5048        ordinality: usize,
5049        vars: Option<&crate::json::JsonValue>,
5050        out: &mut alloc::vec::Vec<Row<'static>>,
5051    ) -> Result<(), EngineError> {
5052        use spg_sql::ast::JsonTableColumn as C;
5053        // Parent cells (regular + ordinality), left-to-right; NESTED
5054        // columns contribute a run of child cells appended after.
5055        let mut parent_cells: alloc::vec::Vec<Value<'static>> = alloc::vec::Vec::new();
5056        let mut nested_runs: alloc::vec::Vec<alloc::vec::Vec<Row<'static>>> =
5057            alloc::vec::Vec::new();
5058        let mut nested_widths: alloc::vec::Vec<usize> = alloc::vec::Vec::new();
5059        for col in &jt.columns {
5060            match col {
5061                C::Ordinality { .. } => {
5062                    parent_cells.push(Value::BigInt(ordinality as i64 + 1));
5063                }
5064                C::Regular { .. } => {
5065                    parent_cells.push(self.json_table_column_value(col, item, vars)?);
5066                }
5067                C::Nested { path, columns } => {
5068                    // Recurse: a nested JSON_TABLE over `item` filtered
5069                    // by `path`, with the same PASSING vars.
5070                    let sub = spg_sql::ast::JsonTable {
5071                        doc: jt.doc.clone(), // unused (outer_doc provided)
5072                        row_path: path.clone(),
5073                        columns: columns.clone(),
5074                        passing: alloc::vec::Vec::new(),
5075                    };
5076                    let (nrows, nschema) = self.json_table_rows(&sub, Some(item))?;
5077                    nested_widths.push(nschema.len());
5078                    nested_runs.push(nrows);
5079                }
5080            }
5081        }
5082        if nested_runs.is_empty() {
5083            out.push(Row::new(parent_cells));
5084            return Ok(());
5085        }
5086        // PG sibling-NESTED semantics: each sibling expands
5087        // INDEPENDENTLY and the results CONCATENATE — a row from
5088        // sibling s fills only s's cells, every other sibling's cells
5089        // NULL. An empty sibling contributes ZERO rows (not a NULL
5090        // row). Only when EVERY sibling is empty does the parent still
5091        // emit one all-NULL row (the outer-join guarantee that a parent
5092        // item is never dropped). Verified vs PG18 (r207): a=1,b=2 → 3
5093        // rows; a=1,b=[] → 1 row; all-empty → 1 NULL row.
5094        let before = out.len();
5095        for (s_idx, run) in nested_runs.iter().enumerate() {
5096            for nrow in run {
5097                let mut cells = parent_cells.clone();
5098                for (o_idx, w) in nested_widths.iter().enumerate() {
5099                    if o_idx == s_idx {
5100                        cells.extend(nrow.values.iter().cloned());
5101                    } else {
5102                        for _ in 0..*w {
5103                            cells.push(Value::Null);
5104                        }
5105                    }
5106                }
5107                out.push(Row::new(cells));
5108            }
5109        }
5110        if out.len() == before {
5111            // Every sibling empty → one all-NULL nested row.
5112            let mut cells = parent_cells.clone();
5113            for w in &nested_widths {
5114                for _ in 0..*w {
5115                    cells.push(Value::Null);
5116                }
5117            }
5118            out.push(Row::new(cells));
5119        }
5120        Ok(())
5121    }
5122
5123    /// v7.39 (round 205) — evaluate one Regular column against a row
5124    /// item: EXISTS → bool; else path → at most one value, coerced to
5125    /// the declared type with ON EMPTY / ON ERROR / DEFAULT behaviour.
5126    fn json_table_column_value(
5127        &self,
5128        col: &spg_sql::ast::JsonTableColumn,
5129        item: &crate::json::JsonValue,
5130        vars: Option<&crate::json::JsonValue>,
5131    ) -> Result<Value<'static>, EngineError> {
5132        use spg_sql::ast::{JsonTableColumn as C, JsonTableOnBehavior as B};
5133        let C::Regular {
5134            name,
5135            ty,
5136            path,
5137            exists,
5138            format_json,
5139            wrapper,
5140            on_empty,
5141            on_error,
5142        } = col
5143        else {
5144            unreachable!("caller guards Regular");
5145        };
5146        let matches = crate::json::json_table_path(item, path, vars).map_err(EngineError::Eval)?;
5147        if *exists {
5148            return Ok(Value::Bool(!matches.is_empty()));
5149        }
5150        let empty_schema: alloc::vec::Vec<ColumnSchema> = alloc::vec::Vec::new();
5151        let ctx = EvalContext::new(&empty_schema, None);
5152        let dummy = Row::new(alloc::vec::Vec::new());
5153        let default_of = |b: &B| -> Result<Option<Value<'static>>, EngineError> {
5154            match b {
5155                B::Null => Ok(Some(Value::Null)),
5156                B::Error => Ok(None),
5157                B::Default(e) => Ok(Some(
5158                    eval::eval_expr(e, &dummy, &ctx).map_err(EngineError::Eval)?,
5159                )),
5160            }
5161        };
5162        // Empty match set → ON EMPTY.
5163        if matches.is_empty() {
5164            return match default_of(on_empty)? {
5165                Some(v) => coerce_json_table_default(v, *ty, name),
5166                None => Err(EngineError::Unsupported(alloc::format!(
5167                    "no SQL/JSON item found for JSON_TABLE column {name:?}"
5168                ))),
5169            };
5170        }
5171        let first = &matches[0];
5172        // FORMAT JSON: return the PG-canonical json representation.
5173        // WITH WRAPPER wraps the whole match SET in an array (even a
5174        // single scalar → `[5]`); without it, the single match's json.
5175        if *format_json {
5176            let text = if *wrapper {
5177                crate::json::JsonValue::Array(matches.clone()).canonical_json_text()
5178            } else {
5179                first.canonical_json_text()
5180            };
5181            return Ok(Value::Json(alloc::borrow::Cow::Owned(text)));
5182        }
5183        if first.is_json_null() {
5184            return Ok(Value::Null);
5185        }
5186        // Coerce the scalar text to the declared type; on failure → ON
5187        // ERROR (default NULL, DEFAULT expr, or raise).
5188        let dt = crate::conversions::column_type_to_data_type(*ty);
5189        let scalar = Value::Text(alloc::borrow::Cow::Owned(first.scalar_text()));
5190        match crate::conversions::coerce_value(scalar, dt, name, 0) {
5191            Ok(v) => Ok(v),
5192            Err(e) => match default_of(on_error)? {
5193                Some(v) => coerce_json_table_default(v, *ty, name),
5194                None => Err(e),
5195            },
5196        }
5197    }
5198
5199    /// table function into (rows, default schema). Dispatch by name.
5200    pub(crate) fn table_fn_rows(
5201        &self,
5202        primary: &TableRef,
5203    ) -> Result<(alloc::vec::Vec<Row<'static>>, alloc::vec::Vec<ColumnSchema>), EngineError> {
5204        let (fn_name, args) = primary
5205            .table_fn_call
5206            .as_deref()
5207            .expect("caller guards table_fn_call.is_some()");
5208        let empty_schema: alloc::vec::Vec<ColumnSchema> = alloc::vec::Vec::new();
5209        let ctx = EvalContext::new(&empty_schema, None);
5210        let dummy_row = Row::new(alloc::vec::Vec::new());
5211        let arg0: Option<Value<'static>> = match args.first() {
5212            Some(e) => Some(eval::eval_expr(e, &dummy_row, &ctx).map_err(EngineError::Eval)?),
5213            None => None,
5214        };
5215        match fn_name.as_str() {
5216            // v7.39 (read01 round 76) — `jsonb_populate_record(NULL::t, j)` /
5217            // `…_recordset` (+ json_ variants). The row shape is the BASE
5218            // argument's declared type — a table's or a composite type's
5219            // column list — which only the catalog knows, so the parser hands
5220            // the raw arguments here rather than desugaring blind.
5221            "jsonb_populate_record"
5222            | "json_populate_record"
5223            | "jsonb_populate_recordset"
5224            | "json_populate_recordset" => {
5225                let type_name = match args.first() {
5226                    Some(Expr::Cast {
5227                        target: spg_sql::ast::CastTarget::Named(n),
5228                        ..
5229                    }) => n.clone(),
5230                    _ => {
5231                        return Err(EngineError::Unsupported(alloc::format!(
5232                            "{fn_name}(): first argument must name a row type, \
5233                             e.g. NULL::mytable"
5234                        )));
5235                    }
5236                };
5237                let cat = self.active_catalog();
5238                let cols: alloc::vec::Vec<ColumnSchema> = if let Some(t) = cat.get(&type_name) {
5239                    t.schema().columns.clone()
5240                } else if let Some(c) = cat.composite_types().get(&type_name) {
5241                    c.fields
5242                        .iter()
5243                        .map(|(n, ty)| ColumnSchema::new(n.clone(), *ty, true))
5244                        .collect()
5245                } else {
5246                    return Err(EngineError::Unsupported(alloc::format!(
5247                        "type \"{type_name}\" does not exist"
5248                    )));
5249                };
5250                let json_arg = match args.get(1) {
5251                    Some(e) => eval::eval_expr(e, &dummy_row, &ctx).map_err(EngineError::Eval)?,
5252                    None => Value::Null,
5253                };
5254                // The set form iterates the JSON array; the scalar form is
5255                // the one-element case of the same walk.
5256                let docs: alloc::vec::Vec<Value<'static>> = if fn_name.ends_with("recordset") {
5257                    crate::json::array_element_rows(&json_arg, false, fn_name)
5258                        .map_err(EngineError::Eval)?
5259                        .into_iter()
5260                        .map(|s| s.map_or(Value::Null, Value::json))
5261                        .collect()
5262                } else if matches!(json_arg, Value::Null) {
5263                    alloc::vec::Vec::new()
5264                } else {
5265                    alloc::vec![json_arg]
5266                };
5267                let mut rows = alloc::vec::Vec::with_capacity(docs.len());
5268                for doc in &docs {
5269                    let mut vals = alloc::vec::Vec::with_capacity(cols.len());
5270                    for c in &cols {
5271                        // `->>` semantics: a missing key is NULL, present keys
5272                        // arrive as text and cast to the declared column type.
5273                        let raw = crate::json::path_get(doc, &Value::text(c.name.clone()), true)
5274                            .map_err(EngineError::Eval)?;
5275                        let v = if matches!(raw, Value::Null) {
5276                            Value::Null
5277                        } else {
5278                            crate::conversions::coerce_value(raw, c.ty, "", 0)
5279                                .map_err(|e| EngineError::Unsupported(alloc::format!("{e:?}")))?
5280                        };
5281                        vals.push(v);
5282                    }
5283                    rows.push(Row::new(vals));
5284                }
5285                Ok((rows, cols))
5286            }
5287            // 7.38.1 S5.1 (pg_dump wall #3) — pg_options_to_table:
5288            // a text[] of 'name=value' reloptions/fdw options → one
5289            // (option_name, option_value) row per element. NULL or an
5290            // empty array yields zero rows (PG); an element without
5291            // '=' carries a NULL option_value, matching PG's split.
5292            "pg_options_to_table" => {
5293                let schema = alloc::vec![
5294                    ColumnSchema::new("option_name", DataType::Text, true),
5295                    ColumnSchema::new("option_value", DataType::Text, true),
5296                ];
5297                let mut rows: alloc::vec::Vec<Row<'static>> = alloc::vec::Vec::new();
5298                if let Some(Value::TextArray(items)) = arg0 {
5299                    for item in items.into_iter().flatten() {
5300                        let (name, value) = match item.split_once('=') {
5301                            Some((n, v)) => (Value::text(n), Value::text(v)),
5302                            None => (Value::text(item.as_str()), Value::Null),
5303                        };
5304                        rows.push(Row::new(alloc::vec![name, value]));
5305                    }
5306                }
5307                Ok((rows, schema))
5308            }
5309            // 7.38.1 S5.1 (pg_dump wall) — pg_get_sequence_data(oid):
5310            // PG18's per-sequence state SRF, (last_value, is_called).
5311            // pg_dump reads it joined to pg_sequence for every dumped
5312            // sequence's setval line. The oid resolves through the
5313            // same relation_oid mapping seqrelid publishes.
5314            "pg_get_sequence_data" => {
5315                let schema = alloc::vec![
5316                    ColumnSchema::new("last_value", DataType::BigInt, false),
5317                    ColumnSchema::new("is_called", DataType::Bool, false),
5318                ];
5319                let want = match arg0 {
5320                    Some(Value::Int(n)) => i64::from(n),
5321                    Some(Value::BigInt(n)) => n,
5322                    _ => {
5323                        return Err(EngineError::Unsupported(
5324                            "pg_get_sequence_data(): argument must be a sequence oid".into(),
5325                        ));
5326                    }
5327                };
5328                let cat = self.active_catalog();
5329                let mut rows: alloc::vec::Vec<Row<'static>> = alloc::vec::Vec::new();
5330                for (name, def) in cat.sequences_all() {
5331                    if crate::system_catalog::relation_oid(cat, name) == Some(want) {
5332                        rows.push(Row::new(alloc::vec![
5333                            Value::BigInt(def.last_value),
5334                            Value::Bool(def.is_called),
5335                        ]));
5336                        break;
5337                    }
5338                }
5339                Ok((rows, schema))
5340            }
5341            "pg_partition_tree" => {
5342                let cols = alloc::vec![
5343                    ColumnSchema::new("relid".to_string(), DataType::Text, true),
5344                    ColumnSchema::new("parentrelid".to_string(), DataType::Text, true),
5345                    ColumnSchema::new("isleaf".to_string(), DataType::Bool, true),
5346                    ColumnSchema::new("level".to_string(), DataType::Int, true),
5347                ];
5348                let Some(Value::Text(name)) = &arg0 else {
5349                    // NULL (or missing) argument → zero rows (PG).
5350                    return Ok((alloc::vec::Vec::new(), cols));
5351                };
5352                let entries = crate::partition_walks::tree_of(self.active_catalog(), name.as_ref());
5353                if entries.is_empty() && self.active_catalog().get(name.as_ref()).is_none() {
5354                    return Err(EngineError::Unsupported(alloc::format!(
5355                        "relation \"{name}\" does not exist"
5356                    )));
5357                }
5358                let rows = entries
5359                    .into_iter()
5360                    .map(|(relid, parent, isleaf, level)| {
5361                        Row::new(alloc::vec![
5362                            Value::text(relid),
5363                            parent.map_or(Value::Null, Value::text),
5364                            Value::Bool(isleaf),
5365                            #[allow(clippy::cast_possible_truncation)]
5366                            Value::Int(level as i32),
5367                        ])
5368                    })
5369                    .collect();
5370                Ok((rows, cols))
5371            }
5372            "pg_partition_ancestors" => {
5373                let cols =
5374                    alloc::vec![ColumnSchema::new("relid".to_string(), DataType::Text, true)];
5375                let Some(Value::Text(name)) = &arg0 else {
5376                    return Ok((alloc::vec::Vec::new(), cols));
5377                };
5378                let cat = self.active_catalog();
5379                if cat.get(name.as_ref()).is_none() {
5380                    return Err(EngineError::Unsupported(alloc::format!(
5381                        "relation \"{name}\" does not exist"
5382                    )));
5383                }
5384                // A relation outside any partition tree yields no rows (PG).
5385                let in_tree = cat
5386                    .get(name.as_ref())
5387                    .is_some_and(|t| t.schema().partition_role.is_some());
5388                let rows = if in_tree {
5389                    crate::partition_walks::ancestors_of(cat, name.as_ref())
5390                        .into_iter()
5391                        .map(|n| Row::new(alloc::vec![Value::text(n)]))
5392                        .collect()
5393                } else {
5394                    alloc::vec::Vec::new()
5395                };
5396                Ok((rows, cols))
5397            }
5398            // v7.39 (round 651) — `ts_debug(config, text)`: what the parser
5399            // saw, what each token was called, which dictionary took it
5400            // and what came out. It is a projection of the same tokenizer
5401            // and the same map the indexer uses, so it cannot describe a
5402            // pipeline other than the one that runs.
5403            "ts_debug" => {
5404                use crate::fts::{TokenType, TsDict};
5405                let cols = alloc::vec![
5406                    ColumnSchema::new("alias".to_string(), DataType::Text, false),
5407                    ColumnSchema::new("description".to_string(), DataType::Text, false),
5408                    ColumnSchema::new("token".to_string(), DataType::Text, false),
5409                    ColumnSchema::new("dictionaries".to_string(), DataType::TextArray, false),
5410                    ColumnSchema::new("dictionary".to_string(), DataType::Text, true),
5411                    ColumnSchema::new("lexemes".to_string(), DataType::TextArray, true),
5412                ];
5413                // PG's one-arg form uses the session configuration; the
5414                // two-arg form names one.
5415                let (cfg_name, text) = match (&arg0, args.get(1)) {
5416                    (Some(Value::Text(c)), Some(t)) => {
5417                        let v = eval::eval_expr(t, &dummy_row, &ctx).map_err(EngineError::Eval)?;
5418                        (c.to_string(), crate::eval::value_to_text(&v))
5419                    }
5420                    (Some(v), None) => (
5421                        alloc::string::String::from("english"),
5422                        crate::eval::value_to_text(v),
5423                    ),
5424                    _ => return Ok((alloc::vec::Vec::new(), cols)),
5425                };
5426                let english = match cfg_name
5427                    .trim()
5428                    .trim_start_matches("pg_catalog.")
5429                    .to_ascii_lowercase()
5430                    .as_str()
5431                {
5432                    "english" => true,
5433                    "simple" => false,
5434                    other => {
5435                        return Err(EngineError::Unsupported(alloc::format!(
5436                            "text search configuration \"{other}\" does not exist"
5437                        )));
5438                    }
5439                };
5440                let rows = crate::fts::tokenize_typed(&text)
5441                    .into_iter()
5442                    .map(|tok| {
5443                        let dict = tok.ty.dictionary(english);
5444                        let dname = dict.map(|d| match d {
5445                            TsDict::Simple => "simple",
5446                            TsDict::EnglishStem => "english_stem",
5447                        });
5448                        let folded = tok.text.to_lowercase();
5449                        let lexemes = dict.map(|d| match d {
5450                            TsDict::Simple => alloc::vec![Some(folded.clone())],
5451                            TsDict::EnglishStem => {
5452                                if crate::fts::is_english_stopword(&folded) {
5453                                    alloc::vec::Vec::new()
5454                                } else {
5455                                    alloc::vec![Some(crate::fts::porter_stem(&folded))]
5456                                }
5457                            }
5458                        });
5459                        Row::new(alloc::vec![
5460                            Value::text(tok.ty.alias()),
5461                            Value::text(tok.ty.description()),
5462                            Value::text(tok.text),
5463                            Value::TextArray(
5464                                dname
5465                                    .map(|n| alloc::vec![Some(alloc::string::String::from(n))])
5466                                    .unwrap_or_default(),
5467                            ),
5468                            dname.map_or(Value::Null, Value::text),
5469                            lexemes.map_or(Value::Null, Value::TextArray),
5470                        ])
5471                    })
5472                    .collect();
5473                let _ = TokenType::AsciiWord;
5474                Ok((rows, cols))
5475            }
5476            // v7.39 (round 651) — `ts_token_type('default')`, the list the
5477            // parser actually produces. It is a projection of the
5478            // `TokenType` enum the tokenizer and `pg_ts_config_map` both
5479            // read, so the three cannot disagree about what a token is.
5480            "ts_token_type" => {
5481                use crate::fts::TokenType as T;
5482                let cols = alloc::vec![
5483                    ColumnSchema::new("tokid".to_string(), DataType::Int, false),
5484                    ColumnSchema::new("alias".to_string(), DataType::Text, false),
5485                    ColumnSchema::new("description".to_string(), DataType::Text, false),
5486                ];
5487                // PG takes the parser by name or oid; SPG has the one.
5488                if let Some(Value::Text(p)) = &arg0
5489                    && !p.eq_ignore_ascii_case("default")
5490                    && !p.eq_ignore_ascii_case("pg_catalog.default")
5491                {
5492                    return Err(EngineError::Unsupported(alloc::format!(
5493                        "text search parser \"{p}\" does not exist"
5494                    )));
5495                }
5496                const TYPES: &[T] = &[
5497                    T::AsciiWord,
5498                    T::Word,
5499                    T::NumWord,
5500                    T::Email,
5501                    T::Url,
5502                    T::Host,
5503                    T::SFloat,
5504                    T::Version,
5505                    T::HwordNumPart,
5506                    T::HwordPart,
5507                    T::HwordAsciiPart,
5508                    T::Blank,
5509                    T::Tag,
5510                    T::Protocol,
5511                    T::NumHword,
5512                    T::AsciiHword,
5513                    T::Hword,
5514                    T::UrlPath,
5515                    T::File,
5516                    T::Float,
5517                    T::Int,
5518                    T::Uint,
5519                    T::Entity,
5520                ];
5521                let rows = TYPES
5522                    .iter()
5523                    .map(|t| {
5524                        Row::new(alloc::vec![
5525                            Value::Int(*t as i32),
5526                            Value::text(t.alias()),
5527                            Value::text(t.description()),
5528                        ])
5529                    })
5530                    .collect();
5531                Ok((rows, cols))
5532            }
5533            // v7.39 (read01 round 65) — a set-returning USER function in FROM
5534            // (`FROM rows_of(2)`). Its body runs through the real executor, like
5535            // every other function body since round 63.
5536            other => {
5537                if !self.active_catalog().functions_named(other).is_empty() {
5538                    return self.exec_setof_user_function(other, args, primary.alias.as_deref());
5539                }
5540                Err(EngineError::Unsupported(alloc::format!(
5541                    "table function {other}() is not supported in FROM"
5542                )))
5543            }
5544        }
5545    }
5546
5547    /// v7.39 (read01 round 65) — run a `RETURNS SETOF <type>` / `RETURNS
5548    /// TABLE(…)` function in FROM position. The body is a SELECT; the arguments
5549    /// are bound into it as literals and it goes through the read path, so the
5550    /// rows it yields are exactly the rows a hand-written query would see.
5551    ///
5552    /// The column NAMES come from the declared shape: `RETURNS TABLE(id int, v
5553    /// text)` names them, and a `SETOF <scalar>` yields a single column named
5554    /// after the function — PG's rule, and what a bare `SELECT * FROM f()`
5555    /// shows.
5556    fn exec_setof_user_function(
5557        &self,
5558        name: &str,
5559        args: &[spg_sql::ast::Expr],
5560        // v7.39 (read01 round 65) — `FROM evens() AS x` names the single column
5561        // `x`: for a scalar SETOF, the table alias IS the column name (PG).
5562        alias: Option<&str>,
5563    ) -> Result<(alloc::vec::Vec<Row<'static>>, alloc::vec::Vec<ColumnSchema>), EngineError> {
5564        // The call's arguments belong to the ENCLOSING query, so they are
5565        // evaluated here and the body sees values.
5566        let empty: alloc::vec::Vec<ColumnSchema> = alloc::vec::Vec::new();
5567        let arg_ctx = self.ev_ctx(&empty, None);
5568        let dummy = Row::new(alloc::vec::Vec::new());
5569        let mut vals: alloc::vec::Vec<Value<'static>> = alloc::vec::Vec::new();
5570        for a in args {
5571            vals.push(eval::eval_expr(a, &dummy, &arg_ctx).map_err(EngineError::Eval)?);
5572        }
5573        self.setof_rows_of(name, &vals, alias)
5574    }
5575
5576    /// v7.39 (read01 round 67) — the set-returning core, on already-evaluated
5577    /// arguments. Shared by the FROM position and the target-list expansion, so
5578    /// a function cannot behave differently depending on where it is called.
5579    pub(crate) fn setof_rows_of(
5580        &self,
5581        name: &str,
5582        arg_values: &[Value<'static>],
5583        alias: Option<&str>,
5584    ) -> Result<(alloc::vec::Vec<Row<'static>>, alloc::vec::Vec<ColumnSchema>), EngineError> {
5585        let cat = self.active_catalog();
5586        let overloads = cat.functions_named(name);
5587        let def = overloads
5588            .iter()
5589            .find(|f| spg_storage::function_arg_types(&f.args_repr).len() == arg_values.len())
5590            .ok_or_else(|| {
5591                EngineError::Unsupported(alloc::format!(
5592                    "function {name} does not exist with {} argument(s)",
5593                    arg_values.len()
5594                ))
5595            })?;
5596        let declared = def.returns.trim().to_string();
5597        let upper = declared.to_ascii_uppercase();
5598        if !upper.starts_with("SETOF") && !upper.starts_with("TABLE(") {
5599            return Err(EngineError::Unsupported(alloc::format!(
5600                "function {name}() does not return a set — it cannot be used in FROM"
5601            )));
5602        }
5603
5604        let arg_names_pl = spg_storage::function_arg_names(&def.args_repr);
5605        // v7.39 (read01 round 66) — a plpgsql SETOF body builds its rows with
5606        // RETURN NEXT / RETURN QUERY; the interpreter collects them.
5607        if def.language.eq_ignore_ascii_case("plpgsql") {
5608            let out_rows = self
5609                .call_plpgsql_setof_fn(def, &arg_names_pl, arg_values)
5610                .map_err(EngineError::Eval)?;
5611            let cols = setof_column_shape(&declared, name, alias, out_rows.first());
5612            let rows = out_rows.into_iter().map(Row::new).collect();
5613            return Ok((rows, cols));
5614        }
5615        let body = def.body.trim().trim_end_matches(';');
5616        let stmt = spg_sql::parser::parse_statement(body).map_err(|e| {
5617            EngineError::Unsupported(alloc::format!("function {name} body does not parse: {e}"))
5618        })?;
5619        let spg_sql::ast::Statement::Select(body_select) = stmt else {
5620            return Err(EngineError::Unsupported(alloc::format!(
5621                "function {name}(): a set-returning body must be a SELECT"
5622            )));
5623        };
5624        let arg_names = spg_storage::function_arg_names(&def.args_repr);
5625        let bound = crate::eval::bind_user_fn_args(
5626            self.active_catalog(),
5627            &body_select,
5628            &arg_names,
5629            arg_values,
5630        )
5631        .map_err(EngineError::Eval)?;
5632        let out = self.exec_select_cancel(&bound, crate::CancelToken::none())?;
5633        let QueryResult::Rows { columns, rows } = out else {
5634            return Ok((alloc::vec::Vec::new(), alloc::vec::Vec::new()));
5635        };
5636        // Name the columns from the DECLARED shape — the same rule the plpgsql
5637        // path above uses, so a body's language cannot change the row shape.
5638        let cols = setof_column_shape_from(&declared, name, alias, &columns);
5639        Ok((rows, cols))
5640    }
5641
5642    fn exec_select_jsonb_each_text(
5643        &self,
5644        stmt: &SelectStatement,
5645        primary: &TableRef,
5646        cancel: CancelToken<'_>,
5647    ) -> Result<QueryResult, EngineError> {
5648        let (each_fn, arg_expr) = primary
5649            .jsonb_each_text_arg
5650            .as_ref()
5651            .map(|(name, expr)| (name.as_str(), expr.as_ref()))
5652            .expect("caller guards jsonb_each_text_arg.is_some()");
5653        // v7.37.17 (17.6 siblings) — the plain jsonb_each / json_each
5654        // forms keep JSON rendering in the value column (JSON null
5655        // stays jsonb 'null', strings keep their quotes).
5656        let as_text = each_fn.ends_with("_text");
5657        let empty_schema: alloc::vec::Vec<ColumnSchema> = alloc::vec::Vec::new();
5658        let ctx = EvalContext::new(&empty_schema, None);
5659        let dummy_row = Row::new(alloc::vec::Vec::new());
5660        let arg_value = eval::eval_expr(arg_expr, &dummy_row, &ctx).map_err(EngineError::Eval)?;
5661        let pairs =
5662            crate::json::each_rows(&arg_value, as_text, each_fn).map_err(EngineError::Eval)?;
5663        let rows: alloc::vec::Vec<Row<'static>> = pairs
5664            .into_iter()
5665            .map(|(k, v)| {
5666                let key_val = Value::text(k);
5667                let value_val = match v {
5668                    Some(s) if as_text => Value::text(s),
5669                    Some(s) => Value::Json(alloc::borrow::Cow::Owned(s)),
5670                    None => Value::Null,
5671                };
5672                Row::new(alloc::vec![key_val, value_val])
5673            })
5674            .collect();
5675        let alias = primary.alias.clone().unwrap_or_else(|| each_fn.to_string());
5676        let value_dtype = if as_text {
5677            spg_storage::DataType::Text
5678        } else {
5679            spg_storage::DataType::Json
5680        };
5681        let key_col = ColumnSchema::new("key".to_string(), spg_storage::DataType::Text, false);
5682        let value_col = ColumnSchema::new("value".to_string(), value_dtype, as_text);
5683        let mut schema_cols = alloc::vec![key_col, value_col];
5684        // `AS t(k, v)` renames key/value positionally (PG behaviour); the
5685        // LATERAL-position form of the same call already honours it.
5686        for (i, new_name) in primary.unnest_column_aliases.iter().enumerate() {
5687            if let Some(col) = schema_cols.get_mut(i) {
5688                col.name = new_name.clone();
5689            }
5690        }
5691        // v7.39 (read01 round 54) — `ev_ctx` threads the catalog; a bare
5692        // `EvalContext::new` drops it and every catalog-dependent cast
5693        // (regclass / enum / composite / domain) silently degrades.
5694        let scan_ctx = self.ev_ctx(&schema_cols, Some(&alias));
5695        // WHERE.
5696        let filtered: alloc::vec::Vec<Row<'static>> = if let Some(w) = &stmt.where_ {
5697            let mut out = alloc::vec::Vec::with_capacity(rows.len());
5698            for row in rows {
5699                cancel.check()?;
5700                let v = eval::eval_expr(w, &row, &scan_ctx).map_err(EngineError::Eval)?;
5701                if matches!(v, Value::Bool(true)) {
5702                    out.push(row);
5703                }
5704            }
5705            out
5706        } else {
5707            rows
5708        };
5709        // Aggregate dispatch (e.g. SELECT COUNT(*) FROM jsonb_each_text…).
5710        if aggregate::uses_aggregate_in(stmt, self.speaks_mysql) {
5711            let agg_memo = core::cell::RefCell::new(memoize::MemoizeCache::default());
5712            let agg_correlated = |e: &Expr, r: &Row<'static>, c: &EvalContext<'_>| {
5713                self.eval_expr_with_correlated(e, r, c, cancel, Some(&mut agg_memo.borrow_mut()))
5714                    .map_err(|err| match err {
5715                        EngineError::Eval(ev) => ev,
5716                        other => eval::EvalError::TypeMismatch {
5717                            detail: alloc::format!("{other}"),
5718                        },
5719                    })
5720            };
5721            // v7.39 (round 656) — hand the rows over as they are rather than
5722            // collecting a second vector of `RowRef` wrappers. Note this is
5723            // a set-returning-function path, NOT the relational scan: the
5724            // measured O(rows) cost lived in `run_single_table_aggregate`,
5725            // and converting these four first was a miss that cost a full
5726            // round — every test stayed green and the number did not move.
5727            let agg = aggregate::run(
5728                stmt,
5729                crate::join::AggRows::Owned(&filtered),
5730                &schema_cols,
5731                Some(&alias),
5732                Some(&agg_correlated),
5733                self.parallel_runner.0.as_deref(),
5734                Some(self.active_catalog()),
5735                Some(self),
5736            )?;
5737            return self.finish_agg_result(agg, stmt, cancel);
5738        }
5739        // Projection.
5740        let projection = build_projection(
5741            &stmt.items,
5742            &schema_cols,
5743            &alias,
5744            self.speaks_mysql,
5745            Some(self.active_catalog()),
5746        )?;
5747        let mut projected_rows: alloc::vec::Vec<Row<'static>> =
5748            alloc::vec::Vec::with_capacity(filtered.len());
5749        for row in &filtered {
5750            let mut vals = alloc::vec::Vec::with_capacity(projection.len());
5751            for p in &projection {
5752                let v = eval::eval_expr(&p.expr, row, &scan_ctx).map_err(EngineError::Eval)?;
5753                vals.push(v);
5754            }
5755            projected_rows.push(Row::new(vals));
5756        }
5757        let columns: alloc::vec::Vec<ColumnSchema> = projection
5758            .iter()
5759            // v7.39 (read01 round 54) — keep the column's enum identity through
5760            // the projection (it lives outside the DataType lattice), or a
5761            // derived table / UNION / windowed result forgets it and any outer
5762            // `ORDER BY <enum col>` silently sorts by the label's TEXT.
5763            .map(|p| p.to_column_schema())
5764            .collect();
5765        // ORDER BY.
5766        if !stmt.order_by.is_empty() {
5767            let mut indexed: alloc::vec::Vec<(usize, Vec<Value<'static>>)> = filtered
5768                .iter()
5769                .enumerate()
5770                .map(|(i, r)| -> Result<_, EngineError> {
5771                    let keys: Result<Vec<Value<'static>>, EngineError> = stmt
5772                        .order_by
5773                        .iter()
5774                        .map(|ob| {
5775                            eval::eval_expr(&ob.expr, r, &scan_ctx).map_err(EngineError::Eval)
5776                        })
5777                        .collect();
5778                    Ok((i, keys?))
5779                })
5780                .collect::<Result<_, _>>()?;
5781            indexed.sort_by(|a, b| {
5782                for (idx, (ka, kb)) in a.1.iter().zip(b.1.iter()).enumerate() {
5783                    let o = &stmt.order_by[idx];
5784                    let cmp = order_by_value_cmp_in(
5785                        o.desc,
5786                        o.nulls_first,
5787                        ka,
5788                        kb,
5789                        scan_ctx.mysql_dialect && !crate::eval::is_binary_coerced(&o.expr),
5790                    );
5791                    if cmp != core::cmp::Ordering::Equal {
5792                        return cmp;
5793                    }
5794                }
5795                core::cmp::Ordering::Equal
5796            });
5797            projected_rows = indexed
5798                .into_iter()
5799                .map(|(i, _)| projected_rows[i].clone())
5800                .collect();
5801        }
5802        // v7.38 (read01) — DISTINCT over a synthetic source was dropped here.
5803        if stmt.distinct {
5804            // v7.38.14 — `FoldSpec::of`, not `::dialect`. The dialect-only
5805            // spec folds EVERY text position, so a column declared
5806            // `COLLATE utf8mb4_bin` had its values merged here exactly the
5807            // way 3b494b6e fixed on the main scan path. The projection is
5808            // already in scope at each of these sites, so the mask needs no
5809            // new plumbing -- it was simply never asked for.
5810            projected_rows = dedup_rows(
5811                projected_rows,
5812                FoldSpec::of_masks(
5813                    scan_ctx.mysql_dialect,
5814                    &fold_mask(&projection),
5815                    &pad_mask(&projection),
5816                ),
5817            );
5818        }
5819        if let Some(offset) = stmt.offset_literal() {
5820            let off = (offset as usize).min(projected_rows.len());
5821            projected_rows.drain(..off);
5822        }
5823        if let Some(limit) = stmt.limit_literal() {
5824            projected_rows.truncate(limit as usize);
5825        }
5826        Ok(QueryResult::Rows {
5827            columns,
5828            rows: projected_rows,
5829        })
5830    }
5831
5832    /// v7.37.17 (17.6 siblings) — execute `SELECT … FROM
5833    /// ( SELECT … ) alias` in primary position. The inner SELECT
5834    /// materialises once through the regular bare-select executor
5835    /// (UNION tails included), then the outer WHERE / aggregate /
5836    /// projection / ORDER BY / LIMIT pipeline runs over the
5837    /// synthetic table — the same post-materialisation shape as
5838    /// exec_select_jsonb_each_text, generalised to N columns.
5839    fn exec_select_derived(
5840        &self,
5841        stmt: &SelectStatement,
5842        primary: &TableRef,
5843        cancel: CancelToken<'_>,
5844    ) -> Result<QueryResult, EngineError> {
5845        let inner = primary
5846            .lateral_subquery
5847            .as_deref()
5848            .expect("caller guards lateral_subquery.is_some()");
5849        // exec_select_cancel is the union-aware wrapper — the inner
5850        // SELECT may carry UNION tails on stmt.unions.
5851        let QueryResult::Rows {
5852            columns: inner_cols,
5853            rows,
5854        } = self.exec_select_cancel(inner, cancel)?
5855        else {
5856            return Err(EngineError::Unsupported(
5857                "derived table subquery must return rows".into(),
5858            ));
5859        };
5860        let alias = primary
5861            .alias
5862            .clone()
5863            .unwrap_or_else(|| primary.name.clone());
5864        // `AS t(a, b)` renames the materialised columns positionally
5865        // (extra inner columns keep their own names, PG behaviour).
5866        let mut schema_cols: alloc::vec::Vec<ColumnSchema> = inner_cols;
5867        // v7.39 (read01 round 78) — a column-alias list longer than the item is
5868        // the error PG reports; SPG used to let the extra names through and then
5869        // fail two layers downstream with "column not found: <the extra name>".
5870        let n_out = schema_cols.len() + usize::from(primary.with_ordinality);
5871        if primary.unnest_column_aliases.len() > n_out {
5872            return Err(EngineError::Unsupported(alloc::format!(
5873                "table \"{alias}\" has {n_out} columns available but {} columns specified",
5874                primary.unnest_column_aliases.len()
5875            )));
5876        }
5877        if primary.scalar_fn_item && schema_cols.len() == 1 {
5878            schema_cols[0].scalar_row_source = true;
5879        }
5880        // v7.39 (read01 round 78) — WITH ORDINALITY on a table function that
5881        // rides this channel (regexp_matches): a trailing bigint counter, 1-based.
5882        // The column-alias list, if given, names it like any other column.
5883        let mut rows = rows;
5884        if primary.with_ordinality {
5885            schema_cols.push(ColumnSchema::new(
5886                "ordinality".to_string(),
5887                DataType::BigInt,
5888                false,
5889            ));
5890            rows = rows
5891                .into_iter()
5892                .enumerate()
5893                .map(|(i, r)| {
5894                    let mut v = r.values;
5895                    #[allow(clippy::cast_possible_wrap)]
5896                    v.push(Value::BigInt(i as i64 + 1));
5897                    Row::new(v)
5898                })
5899                .collect();
5900        }
5901        for (i, new_name) in primary.unnest_column_aliases.iter().enumerate() {
5902            if let Some(col) = schema_cols.get_mut(i) {
5903                col.name = new_name.clone();
5904            }
5905        }
5906        self.exec_select_over_rows(stmt, rows, schema_cols, &alias, cancel)
5907    }
5908
5909    /// v7.39 (read01 partitionfuncs.c) — shared synthetic-source SELECT
5910    /// pipeline (WHERE / aggregate / projection / ORDER BY / DISTINCT /
5911    /// OFFSET / LIMIT) over a pre-materialised row set. Drives the
5912    /// derived-table executor and the FROM-position table functions.
5913    fn exec_select_over_rows(
5914        &self,
5915        stmt: &SelectStatement,
5916        rows: alloc::vec::Vec<Row<'static>>,
5917        schema_cols: alloc::vec::Vec<ColumnSchema>,
5918        alias: &str,
5919        cancel: CancelToken<'_>,
5920    ) -> Result<QueryResult, EngineError> {
5921        let scan_ctx = self.ev_ctx(&schema_cols, Some(alias));
5922        // v7.37 D.21 — correlated subqueries in the WHERE / projection may
5923        // reference this derived table's columns (`… WHERE u.gg = t.g` where t
5924        // is `(VALUES …) t`). Resolve them per-row via eval_expr_with_correlated
5925        // (the same path the aggregate branch uses); the old plain eval_expr let
5926        // a ScalarSubquery reach row-eval unresolved ("engine resolver bug").
5927        let corr_memo = core::cell::RefCell::new(memoize::MemoizeCache::default());
5928        // WHERE.
5929        let filtered: alloc::vec::Vec<Row<'static>> = if let Some(w) = &stmt.where_ {
5930            let mut out = alloc::vec::Vec::with_capacity(rows.len());
5931            for row in rows {
5932                cancel.check()?;
5933                let v = self.eval_expr_with_correlated(
5934                    w,
5935                    &row,
5936                    &scan_ctx,
5937                    cancel,
5938                    Some(&mut corr_memo.borrow_mut()),
5939                )?;
5940                if matches!(v, Value::Bool(true)) {
5941                    out.push(row);
5942                }
5943            }
5944            out
5945        } else {
5946            rows
5947        };
5948        // Aggregate dispatch.
5949        if aggregate::uses_aggregate_in(stmt, self.speaks_mysql) {
5950            let agg_memo = core::cell::RefCell::new(memoize::MemoizeCache::default());
5951            let agg_correlated = |e: &Expr, r: &Row<'static>, c: &EvalContext<'_>| {
5952                self.eval_expr_with_correlated(e, r, c, cancel, Some(&mut agg_memo.borrow_mut()))
5953                    .map_err(|err| match err {
5954                        EngineError::Eval(ev) => ev,
5955                        other => eval::EvalError::TypeMismatch {
5956                            detail: alloc::format!("{other}"),
5957                        },
5958                    })
5959            };
5960            // v7.39 (round 656) — hand the rows over as they are rather than
5961            // collecting a second vector of `RowRef` wrappers. Note this is
5962            // a set-returning-function path, NOT the relational scan: the
5963            // measured O(rows) cost lived in `run_single_table_aggregate`,
5964            // and converting these four first was a miss that cost a full
5965            // round — every test stayed green and the number did not move.
5966            let agg = aggregate::run(
5967                stmt,
5968                crate::join::AggRows::Owned(&filtered),
5969                &schema_cols,
5970                Some(alias),
5971                Some(&agg_correlated),
5972                self.parallel_runner.0.as_deref(),
5973                Some(self.active_catalog()),
5974                Some(self),
5975            )?;
5976            return self.finish_agg_result(agg, stmt, cancel);
5977        }
5978        // Projection.
5979        let projection = build_projection(
5980            &stmt.items,
5981            &schema_cols,
5982            alias,
5983            self.speaks_mysql,
5984            Some(self.active_catalog()),
5985        )?;
5986        // v7.39 (round 621) — a target-list SRF expands here too. This tail
5987        // serves VALUES, a derived table and `ROWS FROM (…)`, and knew nothing
5988        // about them: `SELECT unnest(ARRAY[1,2]), x FROM (VALUES (3),(4)) v(x)`
5989        // answered `function unnest(integer[]) does not exist` for a query PG
5990        // answers.
5991        let srf_idxs = self.srf_target_idxs(&projection);
5992        let mut src_of_row: alloc::vec::Vec<usize> = alloc::vec::Vec::new();
5993        let mut projected_rows: alloc::vec::Vec<Row<'static>> =
5994            alloc::vec::Vec::with_capacity(filtered.len());
5995        if !srf_idxs.is_empty() {
5996            let (rows, src) =
5997                expand_projection_srfs(self, &projection, &srf_idxs, &filtered, &scan_ctx)?;
5998            projected_rows = rows;
5999            src_of_row = src;
6000        } else {
6001            for row in &filtered {
6002                let mut vals = alloc::vec::Vec::with_capacity(projection.len());
6003                for p in &projection {
6004                    let v = self.eval_expr_with_correlated(
6005                        &p.expr,
6006                        row,
6007                        &scan_ctx,
6008                        cancel,
6009                        Some(&mut corr_memo.borrow_mut()),
6010                    )?;
6011                    vals.push(v);
6012                }
6013                projected_rows.push(Row::new(vals));
6014            }
6015        }
6016        let columns: alloc::vec::Vec<ColumnSchema> = projection
6017            .iter()
6018            // v7.39 (read01 round 54) — keep the column's enum identity through
6019            // the projection (it lives outside the DataType lattice), or a
6020            // derived table / UNION / windowed result forgets it and any outer
6021            // `ORDER BY <enum col>` silently sorts by the label's TEXT.
6022            .map(|p| p.to_column_schema())
6023            .collect();
6024        // ORDER BY over the source rows (same shape as the other
6025        // synthetic-table executors).
6026        // v7.39 (read01 round 80) — a positional key (`ORDER BY 1`) means the Nth
6027        // OUTPUT column. Evaluated as an expression, as it was here, the literal
6028        // `1` is just the constant 1: the same sort key for every row, so the
6029        // sort ran and changed nothing. `SELECT unnest(ARRAY['B','a','A','b'])
6030        // ORDER BY 1` (which the parser turns into `SELECT * FROM unnest(…)`,
6031        // landing on this executor) came back in input order.
6032        let order_by = resolve_positional_order_by(&stmt.order_by, &projection);
6033        if !order_by.is_empty() {
6034            // v7.39 (round 621) — one entry per OUTPUT row, since a target-list
6035            // SRF makes more of them than there were inputs.
6036            let out_cols = if srf_idxs.is_empty() {
6037                alloc::vec![None; order_by.len()]
6038            } else {
6039                srf_order_output_cols(&order_by, &projection)
6040            };
6041            let mut indexed: alloc::vec::Vec<(usize, Vec<Value<'static>>)> = projected_rows
6042                .iter()
6043                .enumerate()
6044                .map(|(k, out)| -> Result<_, EngineError> {
6045                    let r = &filtered[src_of_row.get(k).copied().unwrap_or(k)];
6046                    let keys: Result<Vec<Value<'static>>, EngineError> = order_by
6047                        .iter()
6048                        .zip(out_cols.iter())
6049                        .map(|(ob, oc)| {
6050                            // v7.39 (read01 round 54) — this path builds its
6051                            // sort keys itself instead of going through
6052                            // `build_order_keys`, so it skipped the enum-ordinal
6053                            // substitution: an OUTER `ORDER BY <enum col>` over
6054                            // a DERIVED TABLE sorted by the label TEXT, not by
6055                            // member order. Silently wrong rows, not an error.
6056                            let v = srf_order_key(ob, *oc, out, r, &scan_ctx)?;
6057                            Ok(
6058                                match crate::orderby::enum_order_ordinal(&ob.expr, &v, &scan_ctx) {
6059                                    Some(ord) => Value::Float(ord),
6060                                    None => v,
6061                                },
6062                            )
6063                        })
6064                        .collect();
6065                    Ok((k, keys?))
6066                })
6067                .collect::<Result<_, _>>()?;
6068            indexed.sort_by(|a, b| {
6069                for (idx, (ka, kb)) in a.1.iter().zip(b.1.iter()).enumerate() {
6070                    let o = &stmt.order_by[idx];
6071                    let cmp = order_by_value_cmp_in(
6072                        o.desc,
6073                        o.nulls_first,
6074                        ka,
6075                        kb,
6076                        scan_ctx.mysql_dialect && !crate::eval::is_binary_coerced(&o.expr),
6077                    );
6078                    if cmp != core::cmp::Ordering::Equal {
6079                        return cmp;
6080                    }
6081                }
6082                core::cmp::Ordering::Equal
6083            });
6084            projected_rows = indexed
6085                .into_iter()
6086                .map(|(i, _)| projected_rows[i].clone())
6087                .collect();
6088        }
6089        // v7.38 (read01) — DISTINCT over a synthetic source was dropped here.
6090        if stmt.distinct {
6091            // v7.38.14 — `FoldSpec::of`, not `::dialect`. The dialect-only
6092            // spec folds EVERY text position, so a column declared
6093            // `COLLATE utf8mb4_bin` had its values merged here exactly the
6094            // way 3b494b6e fixed on the main scan path. The projection is
6095            // already in scope at each of these sites, so the mask needs no
6096            // new plumbing -- it was simply never asked for.
6097            projected_rows = dedup_rows(
6098                projected_rows,
6099                FoldSpec::of_masks(
6100                    scan_ctx.mysql_dialect,
6101                    &fold_mask(&projection),
6102                    &pad_mask(&projection),
6103                ),
6104            );
6105        }
6106        if let Some(offset) = stmt.offset_literal() {
6107            let off = (offset as usize).min(projected_rows.len());
6108            projected_rows.drain(..off);
6109        }
6110        if let Some(limit) = stmt.limit_literal() {
6111            projected_rows.truncate(limit as usize);
6112        }
6113        Ok(QueryResult::Rows {
6114            columns,
6115            rows: projected_rows,
6116        })
6117    }
6118
6119    /// Constant `SELECT` with no FROM: evaluate each projection item
6120    /// once against an empty dummy row (`SELECT 1`, `SELECT '7'::INT`).
6121    fn exec_constant_select(&self, stmt: &SelectStatement) -> Result<QueryResult, EngineError> {
6122        let empty_schema: Vec<ColumnSchema> = Vec::new();
6123        let ctx = self.ev_ctx(&empty_schema, None);
6124        // v7.39 (read01 round 106) — an aggregate with no FROM runs over the
6125        // single implicit row (`SELECT count(*)` → 1, `SELECT sum(5)` → 5,
6126        // `SELECT string_agg('x',',')` → x). Before this it fell through to the
6127        // scalar projection, where the aggregate name looked like an unknown
6128        // function. The WHERE filters that one row, so `… WHERE false` leaves
6129        // the aggregate zero input rows (`count(*)` → 0).
6130        if aggregate::uses_aggregate_in(stmt, self.speaks_mysql) {
6131            let dummy = Row::new(Vec::new());
6132            let passes = match &stmt.where_ {
6133                Some(w) => matches!(eval::eval_expr(w, &dummy, &ctx)?, Value::Bool(true)),
6134                None => true,
6135            };
6136            let rows: Vec<RowRef<'_>> = if passes {
6137                alloc::vec![RowRef::Owned(&dummy)]
6138            } else {
6139                Vec::new()
6140            };
6141            let agg = aggregate::run(
6142                stmt,
6143                crate::join::AggRows::Refs(&rows),
6144                &empty_schema,
6145                None,
6146                None,
6147                self.parallel_runner.0.as_deref(),
6148                Some(self.active_catalog()),
6149                Some(self),
6150            )?;
6151            return self.finish_agg_result(agg, stmt, CancelToken::none());
6152        }
6153        let projection = build_projection(
6154            &stmt.items,
6155            &empty_schema,
6156            "",
6157            self.speaks_mysql,
6158            Some(self.active_catalog()),
6159        )?;
6160        // `SELECT … WHERE cond` with no FROM — the one conceptual
6161        // row survives only when the condition is true (previously
6162        // the WHERE was silently ignored: `SELECT 1 WHERE false`
6163        // returned a row).
6164        let dummy_row = Row::new(Vec::new());
6165        if let Some(w) = &stmt.where_ {
6166            let cond = eval::eval_expr(w, &dummy_row, &ctx)?;
6167            if !crate::eval::predicate_is_true(&cond, "WHERE", ctx.mysql_dialect)? {
6168                let columns: Vec<ColumnSchema> = projection
6169                    .into_iter()
6170                    .map(|p| p.to_column_schema())
6171                    .collect();
6172                return Ok(QueryResult::Rows {
6173                    columns,
6174                    rows: Vec::new(),
6175                });
6176            }
6177        }
6178        // v7.38 (read01, T15) — a top-level SRF that the parser did NOT rewrite
6179        // into a FROM item (regexp_matches, whose rows are arrays and so cannot
6180        // desugar to unnest) expands here: one output row per SRF row, sibling
6181        // scalar columns repeated. unnest / array_elements / path_query reach a
6182        // real FROM via the parser rewrite and never land here.
6183        // v7.39 (read01 round 67) — every SRF in the list, in lockstep.
6184        let srf_idxs = self.srf_target_idxs(&projection);
6185        if !srf_idxs.is_empty() {
6186            let mut rows = expand_srf_row(self, &projection, &srf_idxs, &dummy_row, &ctx)?;
6187            let columns: Vec<ColumnSchema> = projection
6188                .into_iter()
6189                .map(|p| p.to_column_schema())
6190                .collect();
6191            // v7.39 (read01 round 80) — a FROM-less SELECT still has an ORDER BY,
6192            // an OFFSET and a LIMIT, and they apply to the rows the SRF expanded
6193            // to. This returned straight out of the expansion, so
6194            // `SELECT unnest(ARRAY['B','a','A','b']) ORDER BY 1` came back in
6195            // input order — the sort was not wrong, it never ran. (There is
6196            // exactly one conceptual input row here, which is why the ordinary
6197            // scan pipeline is not on this path at all.)
6198            if !stmt.order_by.is_empty() {
6199                let synth_ctx =
6200                    EvalContext::new(&columns, None).with_catalog(self.active_catalog());
6201                let resolved: Vec<spg_sql::ast::OrderBy> = stmt
6202                    .order_by
6203                    .iter()
6204                    .map(|o| {
6205                        let mut o = o.clone();
6206                        if let Expr::Literal(spg_sql::ast::Literal::Integer(n)) = &o.expr
6207                            && *n >= 1
6208                            && let Ok(idx) = usize::try_from(*n - 1)
6209                            && idx < columns.len()
6210                        {
6211                            o.expr = Expr::Column(spg_sql::ast::ColumnName {
6212                                qualifier: None,
6213                                name: columns[idx].name.clone(),
6214                            });
6215                        }
6216                        o
6217                    })
6218                    .collect();
6219                let descs: Vec<bool> = resolved.iter().map(|o| o.desc).collect();
6220                let mut tagged: Vec<(Vec<OrderKey>, Row)> = Vec::with_capacity(rows.len());
6221                for r in rows {
6222                    // v7.39.12 — a correlated subquery in ORDER BY is resolved
6223                    // for this row before the key is built; see
6224                    // `Engine::order_by_resolved_for_row`.
6225                    let per_row = self.order_by_resolved_for_row(
6226                        &resolved,
6227                        &r,
6228                        &synth_ctx,
6229                        CancelToken::none(),
6230                    )?;
6231                    let keys =
6232                        build_order_keys(per_row.as_deref().unwrap_or(&resolved), &r, &synth_ctx)?;
6233                    tagged.push((keys, r));
6234                }
6235                sort_by_keys(&mut tagged, &descs);
6236                rows = tagged.into_iter().map(|(_, r)| r).collect();
6237            }
6238            apply_offset_and_limit(&mut rows, stmt.offset_literal(), stmt.limit_literal());
6239            return Ok(QueryResult::Rows { columns, rows });
6240        }
6241        let mut values = Vec::with_capacity(projection.len());
6242        for p in &projection {
6243            values.push(eval::eval_expr(&p.expr, &dummy_row, &ctx)?);
6244        }
6245        let columns: Vec<ColumnSchema> = projection
6246            .into_iter()
6247            .map(|p| p.to_column_schema())
6248            .collect();
6249        // v7.39 (round 239) — the FROM-less scalar path ignored LIMIT and
6250        // OFFSET entirely, so `SELECT 1 LIMIT 0` returned its row where PG
6251        // returns none. (The SRF and aggregate arms above already applied
6252        // them; this tail was the one that didn't.)
6253        let mut rows = alloc::vec![Row::new(values)];
6254        apply_offset_and_limit(&mut rows, stmt.offset_literal(), stmt.limit_literal());
6255        Ok(QueryResult::Rows { columns, rows })
6256    }
6257
6258    /// v7.37.x (docker-fair INSUBQ attack) — pre-replacement short-
6259    /// circuit. Catches
6260    ///   SELECT COUNT(*) FROM A WHERE A.pk IN (<uncorrelated subquery>)
6261    /// BEFORE `resolve_select_subqueries` materialises the inner result
6262    /// as `Vec<Expr::Literal>`. Runs the inner once, collects the
6263    /// values into a `HashSet<i64>` directly, then probes A.pk per
6264    /// HashSet entry and tallies. Saves the Expr-literal roundtrip
6265    /// (~150 µs / query at INSUBQ benchmark scale).
6266    pub(crate) fn try_count_star_pk_in_subquery_fast(
6267        &self,
6268        stmt: &SelectStatement,
6269        cancel: CancelToken<'_>,
6270    ) -> Result<Option<QueryResult>, EngineError> {
6271        use spg_sql::ast::SelectItem;
6272        if stmt.distinct
6273            || stmt.limit_with_ties
6274            || stmt.group_by.is_some()
6275            || stmt.having.is_some()
6276            || !stmt.unions.is_empty()
6277            || !stmt.order_by.is_empty()
6278            || stmt.limit.is_some()
6279            || stmt.offset.is_some()
6280            || stmt.items.len() != 1
6281        {
6282            return Ok(None);
6283        }
6284        let SelectItem::Expr { expr, .. } = &stmt.items[0] else {
6285            return Ok(None);
6286        };
6287        let is_count_star = matches!(expr, Expr::FunctionCall { name, args }
6288            if name.eq_ignore_ascii_case("count_star") && args.is_empty());
6289        if !is_count_star {
6290            return Ok(None);
6291        }
6292        let Some(from) = stmt.from.as_ref() else {
6293            return Ok(None);
6294        };
6295        if !from.joins.is_empty()
6296            || from.primary.lateral_subquery.is_some()
6297            || from.primary.unnest_expr.is_some()
6298            || from.primary.generate_series_args.is_some()
6299            || from.primary.table_fn_call.is_some()
6300            || from.primary.as_of_segment.is_some()
6301        {
6302            return Ok(None);
6303        }
6304        let Some(where_expr) = stmt.where_.as_ref() else {
6305            return Ok(None);
6306        };
6307        // The WHERE conjunct must be a bare `<col> IN (subquery)` with
6308        // negated=false; no other predicates.
6309        let Expr::InSubquery {
6310            expr: col_expr,
6311            subquery,
6312            negated: false,
6313        } = where_expr
6314        else {
6315            return Ok(None);
6316        };
6317        let Expr::Column(c) = col_expr.as_ref() else {
6318            return Ok(None);
6319        };
6320        let outer_alias = from
6321            .primary
6322            .alias
6323            .as_deref()
6324            .unwrap_or(from.primary.name.as_str());
6325        if let Some(q) = c.qualifier.as_deref()
6326            && !q.eq_ignore_ascii_case(outer_alias)
6327        {
6328            return Ok(None);
6329        }
6330        // Outer column must be a single-column PK on integer family.
6331        let catalog = self.active_catalog();
6332        let Some(outer_table) = catalog.get(from.primary.name.as_str()) else {
6333            return Ok(None);
6334        };
6335        let outer_schema = outer_table.schema();
6336        let Some(outer_pos) = outer_schema
6337            .columns
6338            .iter()
6339            .position(|s| s.name.eq_ignore_ascii_case(&c.name))
6340        else {
6341            return Ok(None);
6342        };
6343        if !matches!(
6344            outer_schema.columns[outer_pos].ty,
6345            spg_storage::DataType::BigInt
6346                | spg_storage::DataType::Int
6347                | spg_storage::DataType::SmallInt
6348        ) {
6349            return Ok(None);
6350        }
6351        if !outer_schema
6352            .uniqueness_constraints
6353            .iter()
6354            .any(|u| u.is_primary_key && u.columns.as_slice() == [outer_pos])
6355        {
6356            return Ok(None);
6357        }
6358        let Some(idx) = outer_table.index_on(outer_pos) else {
6359            return Ok(None);
6360        };
6361        // Inner must be uncorrelated. The cheap-correlation pre-check
6362        // exists upstream; here we just attempt the bare exec.
6363        if crate::subquery::select_is_correlated(subquery) {
6364            return Ok(None);
6365        }
6366        let mut inner = (**subquery).clone();
6367        self.resolve_select_subqueries(&mut inner, cancel)?;
6368        let r = match self.exec_bare_select_cancel(&inner, cancel) {
6369            Ok(r) => r,
6370            Err(_) => return Ok(None),
6371        };
6372        let QueryResult::Rows { columns, rows, .. } = r else {
6373            return Ok(None);
6374        };
6375        if columns.len() != 1 {
6376            return Ok(None);
6377        }
6378        // v7.37.43 (INSUBQ B-1) — inner-uniqueness check. If the inner
6379        // subquery projects a column known to be UNIQUE/PK on its table
6380        // (statically: `SELECT <col> FROM <tbl> WHERE …` where <col> is
6381        // in `tbl.uniqueness_constraints`), survivor values are
6382        // guaranteed distinct and the per-survivor `HashSet::insert`
6383        // dedup check is redundant. ~25 ns × N_inner-survivors saved.
6384        //
6385        // Inlined check — gated on: no DISTINCT/GROUP/UNION/JOIN, single
6386        // projection that is a bare Column ref, table-column lookup in
6387        // catalog confirms the column appears as a unique constraint's
6388        // sole member. UNIQUE NOT NULL is required — a nullable unique
6389        // column may have multiple NULLs, but NULLs are already skipped
6390        // above (`Value::Null => continue`), so a UNIQUE-only column is
6391        // still safe to dedup-skip.
6392        let inner_unique = (|| -> bool {
6393            if inner.distinct
6394                || inner.group_by.is_some()
6395                || !inner.unions.is_empty()
6396                || inner.having.is_some()
6397                || inner.items.len() != 1
6398            {
6399                return false;
6400            }
6401            let Some(inner_from) = inner.from.as_ref() else {
6402                return false;
6403            };
6404            if !inner_from.joins.is_empty()
6405                || inner_from.primary.lateral_subquery.is_some()
6406                || inner_from.primary.unnest_expr.is_some()
6407                || inner_from.primary.generate_series_args.is_some()
6408                || inner_from.primary.table_fn_call.is_some()
6409            {
6410                return false;
6411            }
6412            let SelectItem::Expr { expr: proj, .. } = &inner.items[0] else {
6413                return false;
6414            };
6415            let Expr::Column(pc) = proj else {
6416                return false;
6417            };
6418            let inner_alias = inner_from
6419                .primary
6420                .alias
6421                .as_deref()
6422                .unwrap_or(inner_from.primary.name.as_str());
6423            if let Some(q) = pc.qualifier.as_deref()
6424                && !q.eq_ignore_ascii_case(inner_alias)
6425            {
6426                return false;
6427            }
6428            let Some(inner_table) = catalog.get(inner_from.primary.name.as_str()) else {
6429                return false;
6430            };
6431            let isch = inner_table.schema();
6432            let Some(ipos) = isch
6433                .columns
6434                .iter()
6435                .position(|s| s.name.eq_ignore_ascii_case(&pc.name))
6436            else {
6437                return false;
6438            };
6439            isch.uniqueness_constraints
6440                .iter()
6441                .any(|u| u.columns.as_slice() == [ipos])
6442        })();
6443        // Collect inner i64 values directly into a HashSet, then probe.
6444        let mut count: i64 = 0;
6445        let mut probed = if inner_unique {
6446            hashbrown::HashSet::<i64>::new()
6447        } else {
6448            hashbrown::HashSet::<i64>::with_capacity(rows.len())
6449        };
6450        for row in &rows {
6451            let v = row.values.first().cloned().unwrap_or(Value::Null);
6452            let n = match v {
6453                Value::BigInt(n) => n,
6454                Value::Int(n) => i64::from(n),
6455                Value::SmallInt(n) => i64::from(n),
6456                Value::Null => continue,
6457                _ => return Ok(None),
6458            };
6459            // De-duplicate inner key set so a duplicate inner value
6460            // doesn't double-count the same outer row. Skipped when
6461            // the inner projection is statically unique.
6462            if !inner_unique && !probed.insert(n) {
6463                continue;
6464            }
6465            // v7.37.43 (INSUBQ B-2 + B-4) — direct i64 PK probe, skipping
6466            // the `IndexKey::from_value` enum-dispatch and the per-call
6467            // `IndexKey` wrapper construction. The outer column is
6468            // already gated to integer-family above, so an i64 key
6469            // always corresponds to a valid PK lookup.
6470            if !idx.lookup_eq_i64(n).is_empty() {
6471                count += 1;
6472            }
6473        }
6474        let columns_out = alloc::vec![ColumnSchema::new(
6475            "count".to_string(),
6476            spg_storage::DataType::BigInt,
6477            false,
6478        )];
6479        let rows_out = alloc::vec![Row::new(alloc::vec![Value::BigInt(count)])];
6480        Ok(Some(QueryResult::Rows {
6481            columns: columns_out,
6482            rows: rows_out,
6483        }))
6484    }
6485
6486    /// v7.37.x (docker-fair INSUBQ attack) — short-circuit
6487    ///   SELECT COUNT(*) FROM A WHERE A.pk IN (literal list)
6488    /// (the post-subquery-replacement shape of the INSUBQ probe
6489    /// `SELECT COUNT(*) FROM A WHERE A.pk IN (SELECT k FROM B WHERE …)`).
6490    /// The general aggregate path materialises every seeked row into
6491    /// a `Vec<Cow<Row>>`, then runs the aggregate executor over it.
6492    /// For COUNT(*) we only care how many keys hit; iterate the list
6493    /// and tally `idx.lookup_eq(key)` non-empty results, skipping the
6494    /// row materialisation, the aggregate state machine, and the per-
6495    /// row WHERE re-eval (the seek already filtered by the same list).
6496    /// Returns `None` when the shape doesn't match.
6497    fn try_count_star_pk_in_list_fast(
6498        &self,
6499        stmt: &SelectStatement,
6500        table: &spg_storage::Table,
6501        schema_cols: &[ColumnSchema],
6502        alias: &str,
6503    ) -> Option<QueryResult> {
6504        use spg_sql::ast::{ColumnName, SelectItem};
6505        // Gates on the SELECT shape.
6506        if stmt.distinct
6507            || stmt.limit_with_ties
6508            || stmt.group_by.is_some()
6509            || stmt.having.is_some()
6510            || !stmt.unions.is_empty()
6511            || !stmt.order_by.is_empty()
6512            || stmt.limit.is_some()
6513            || stmt.offset.is_some()
6514            || stmt.items.len() != 1
6515        {
6516            return None;
6517        }
6518        let SelectItem::Expr { expr, .. } = &stmt.items[0] else {
6519            return None;
6520        };
6521        let is_count_star = matches!(expr, Expr::FunctionCall { name, args }
6522            if name.eq_ignore_ascii_case("count_star") && args.is_empty());
6523        if !is_count_star {
6524            return None;
6525        }
6526        // WHERE must be `<col> IN (literal list)` with no other
6527        // conjuncts (the seek result is a true subset of the row
6528        // population for this predicate).
6529        let where_expr = stmt.where_.as_ref()?;
6530        let Expr::InList {
6531            expr: col_expr,
6532            list,
6533            negated: false,
6534        } = where_expr
6535        else {
6536            return None;
6537        };
6538        let Expr::Column(c) = col_expr.as_ref() else {
6539            return None;
6540        };
6541        if let Some(q) = c.qualifier.as_deref()
6542            && !q.eq_ignore_ascii_case(alias)
6543        {
6544            return None;
6545        }
6546        let col_pos = schema_cols
6547            .iter()
6548            .position(|s| s.name.eq_ignore_ascii_case(&c.name))?;
6549        // The column must be a single-column PK on an integer family
6550        // — the same gate the SCALARSQ + LEFT-ANTI-JOIN fast paths use,
6551        // so the antiset stays collision-free under `HashSet<i64>`.
6552        let schema = table.schema();
6553        if !matches!(
6554            schema.columns[col_pos].ty,
6555            spg_storage::DataType::BigInt
6556                | spg_storage::DataType::Int
6557                | spg_storage::DataType::SmallInt
6558        ) {
6559            return None;
6560        }
6561        if !schema
6562            .uniqueness_constraints
6563            .iter()
6564            .any(|u| u.is_primary_key && u.columns.as_slice() == [col_pos])
6565        {
6566            return None;
6567        }
6568        let idx = table.index_on(col_pos)?;
6569        // Tally non-empty seek results across all literal values.
6570        let mut count: i64 = 0;
6571        for lit in list {
6572            let Expr::Literal(l) = lit else {
6573                return None;
6574            };
6575            // r1039 — through the shared resolver, so a literal spelled
6576            // in another type ('5' against an integer PK) is read as the
6577            // column's before it becomes a key. This tally answers from
6578            // the index alone, so a key in the wrong space would return a
6579            // COUNT of zero rather than fall back to a scan.
6580            let col = schema.columns.get(col_pos)?;
6581            let v = crate::index_access::literal_as_column_value(l, col, col_pos)?;
6582            let key = spg_storage::IndexKey::from_value_for_column(&v, col.ty)?;
6583            if !idx.lookup_eq(&key).is_empty() {
6584                count += 1;
6585            }
6586        }
6587        let columns = alloc::vec![ColumnSchema::new(
6588            "count".to_string(),
6589            spg_storage::DataType::BigInt,
6590            false,
6591        )];
6592        let rows = alloc::vec![Row::new(alloc::vec![Value::BigInt(count)])];
6593        let _ = ColumnName {
6594            qualifier: None,
6595            name: String::new(),
6596        };
6597        Some(QueryResult::Rows { columns, rows })
6598    }
6599
6600    /// v7.38 (perf, exact-range count) — `SELECT count(*) FROM t WHERE <col>
6601    /// BETWEEN a AND b` on an indexed column. The index range walk yields
6602    /// exactly the matching (visible) rows, so we count locators directly —
6603    /// skipping the row materialisation, the aggregate state machine, and the
6604    /// per-row WHERE re-eval the general path pays. Turns the `range_count`
6605    /// endpoint from tied-with-PG (superset re-eval) into a clear win. None
6606    /// when the shape doesn't match.
6607    fn try_count_star_indexed_range_fast(
6608        &self,
6609        stmt: &SelectStatement,
6610        table: &spg_storage::Table,
6611        schema_cols: &[ColumnSchema],
6612        alias: &str,
6613        snapshot: &spg_storage::snapshot::Snapshot,
6614    ) -> Option<QueryResult> {
6615        use spg_sql::ast::SelectItem;
6616        if stmt.distinct
6617            || stmt.limit_with_ties
6618            || stmt.group_by.is_some()
6619            || stmt.having.is_some()
6620            || !stmt.unions.is_empty()
6621            || !stmt.order_by.is_empty()
6622            || stmt.limit.is_some()
6623            || stmt.offset.is_some()
6624            || stmt.items.len() != 1
6625        {
6626            return None;
6627        }
6628        let SelectItem::Expr { expr, .. } = &stmt.items[0] else {
6629            return None;
6630        };
6631        let is_count_star = matches!(expr, Expr::FunctionCall { name, args }
6632            if name.eq_ignore_ascii_case("count_star") && args.is_empty());
6633        if !is_count_star {
6634            return None;
6635        }
6636        let where_expr = stmt.where_.as_ref()?;
6637        let count = crate::index_access::try_range_count(
6638            where_expr,
6639            schema_cols,
6640            table,
6641            alias,
6642            snapshot,
6643            self.speaks_mysql,
6644        )?;
6645        let columns = alloc::vec![ColumnSchema::new(
6646            "count".to_string(),
6647            spg_storage::DataType::BigInt,
6648            false,
6649        )];
6650        let rows = alloc::vec![Row::new(alloc::vec![Value::BigInt(count)])];
6651        Some(QueryResult::Rows { columns, rows })
6652    }
6653
6654    /// Single-table aggregate path: filter the (optionally index-seeked)
6655    /// rows, then hand off to the aggregate executor which does its own
6656    /// projection + ORDER BY before `finish_agg_result` applies LIMIT.
6657    fn run_single_table_aggregate<'a>(
6658        &self,
6659        stmt: &SelectStatement,
6660        table: &'a spg_storage::Table,
6661        schema_cols: &'a [ColumnSchema],
6662        alias: &str,
6663        indexed_rows: Option<crate::index_access::Seeked<'a>>,
6664        cancel: CancelToken<'_>,
6665    ) -> Result<QueryResult, EngineError> {
6666        // v7.38 (read01 U15) — per-scan sampler cell for TABLESAMPLE
6667        // REPEATABLE (see run_single_table_scan). Aggregates
6668        // (`count(*) FROM t TABLESAMPLE …`) filter through this ctx too.
6669        let sample_cell: core::cell::Cell<Option<u64>> = core::cell::Cell::new(None);
6670        let ctx = self
6671            .ev_ctx(schema_cols, Some(alias))
6672            .with_sample_rng(&sample_cell);
6673        // v7.39 (round 657) — pre-sized. Pushing 500k pointers into a
6674        // `Vec::new()` walks the doubling chain 8, 16, … 262144, 524288,
6675        // and every abandoned buffer on the way stays resident: RSS is a
6676        // high-water mark, so the intermediates are paid for even though
6677        // they are freed. Round 656 measured the scan at 17 bytes/row
6678        // where the survivor list itself only needs 8.
6679        let mut filtered: Vec<&Row<'static>> = if stmt.where_.is_none() {
6680            Vec::with_capacity(table.rows().len())
6681        } else {
6682            // With a WHERE, the row count is an UPPER bound and reserving it
6683            // is the worse trade: `… WHERE id = 5` over 50M rows would take
6684            // 400 MB of pointers to hold one survivor. Let it grow.
6685            Vec::new()
6686        };
6687        // v6.2.6 — Memoize: per-query LRU cache for correlated
6688        // scalar subqueries. Fresh per row-loop entry so each
6689        // SELECT execution gets an isolated cache.
6690        let mut memo = memoize::MemoizeCache::new();
6691        // v7.37 (perf) — single-table aggregate's WHERE filter
6692        // pre-7.37 ran the slow tree-walker (`eval_expr_with_
6693        // correlated`) per row, even for subquery-free WHEREs that
6694        // the single-table SCAN path has compiled since v7.32
6695        // (perf knife D). The asymmetry meant a fold-to-filter
6696        // rewrite (joinfold) that swapped a JOIN for a single-table
6697        // aggregate over a compiled WHERE saw the tree-walker
6698        // instead — 25 k rows × `m.mailbox_id IN (25 lits)` cost
6699        // ~9 ms via the walker, vs ~1 ms via the compiled InSet
6700        // step. Compile once if eligible; fall back to the walker
6701        // for subquery-bearing or non-compilable WHEREs.
6702        let compiled_where: Option<eval::CompiledExpr> = stmt
6703            .where_
6704            .as_ref()
6705            .filter(|w| eval::fully_compilable(w))
6706            .map(|w| {
6707                // v7.38.8 — the scan filter runs the cheap half of its
6708                // conjunction first. Called from HERE and not from
6709                // `eval::compiled`, deliberately: the row loop lives in
6710                // that file, and adding a function to it cost this
6711                // query 11 % through layout alone while doing no work
6712                // for it. See `crate::qualorder`.
6713                match crate::qualorder::reordered(w) {
6714                    Some(r) => eval::compile_expr(&r, &ctx),
6715                    None => eval::compile_expr(w, &ctx),
6716                }
6717            });
6718        let mut eval_stack: Vec<Value<'static>> = Vec::new();
6719        let mut row_passes_where = |row: &Row<'static>,
6720                                    eval_stack: &mut Vec<Value<'static>>,
6721                                    memo: &mut memoize::MemoizeCache|
6722         -> Result<bool, EngineError> {
6723            match (&compiled_where, &stmt.where_) {
6724                (Some(cw), _) => {
6725                    // v7.39 (round 479) — the predicate wants a bool, not a
6726                    // Value. The owned entry ended in `Value::into_owned`
6727                    // and the caller then dropped it, once per row; round
6728                    // 478's profile put that pair above the comparison
6729                    // itself.
6730                    Ok(eval::compiled::eval_compiled_pred(
6731                        cw,
6732                        row,
6733                        &ctx,
6734                        eval_stack,
6735                        ctx.mysql_dialect,
6736                    )
6737                    .map_err(EngineError::Eval)?)
6738                }
6739                (None, Some(w)) => {
6740                    let cond = self.eval_expr_with_correlated(w, row, &ctx, cancel, Some(memo))?;
6741                    Ok(crate::eval::predicate_is_true(
6742                        &cond,
6743                        "WHERE",
6744                        ctx.mysql_dialect,
6745                    )?)
6746                }
6747                (None, None) => Ok(true),
6748            }
6749        };
6750        if let Some(seeked) = &indexed_rows {
6751            // v7.38.19 — an EXACT seek has already applied the whole
6752            // predicate, so asking again is asking the index's question
6753            // a second time, once per row.
6754            //
6755            // Profiled on `count(*) FROM events WHERE project_id = 3`
6756            // over 200,000 rows: `try_index_seek` 1,814 leaf samples and
6757            // `binop::compare` 1,633 — and `compare`'s first arm is
6758            // `(Int, Int) => a.cmp(b)`, so it was never that a
6759            // comparison is expensive. It was that 25,000 of them were
6760            // re-deciding what the walk had decided. The same query with
6761            // `GROUP BY project_id` bolted on ran in half the time,
6762            // doing strictly more work, because that path reached the
6763            // rows differently.
6764            //
6765            // `exact` is false for every arm that has not proven it —
6766            // the GIN, trigram and jsonb walks, an `AND` whose other
6767            // conjuncts went unapplied, a collated key, a type whose key
6768            // cannot name it. See `index_access::Seeked`.
6769            if seeked.exact {
6770                filtered.extend(seeked.rows.iter().map(Cow::as_ref));
6771            } else {
6772                for cow in &seeked.rows {
6773                    let row = cow.as_ref();
6774                    if !row_passes_where(row, &mut eval_stack, &mut memo)? {
6775                        continue;
6776                    }
6777                    filtered.push(row);
6778                }
6779            }
6780        }
6781        // v7.36 (cold-tier coverage) — single-table aggregate's
6782        // non-indexed full scan was hot-only and silently lost cold
6783        // rows on COUNT/SUM/etc. Materialise cold rows once into
6784        // `cold_rows_storage` (Vec<Row<'static>>) so the `filtered: Vec<&Row<'static>>`
6785        // shape stays unchanged; the cold rows live until the end of
6786        // the aggregate run.
6787        let cold_rows_storage = if indexed_rows.is_none() {
6788            self.iter_cold_rows_of_table(table)
6789        } else {
6790            Vec::new()
6791        };
6792        if indexed_rows.is_none() {
6793            // v7.37.15 (Phase C.3, step 2) — MVCC visibility gate for the
6794            // single-table aggregate full-scan path. Mirrors the gate on
6795            // `run_single_table_scan`: this is a user-query result path,
6796            // so under gate-on (`SPG_MVCC_INPLACE`) it must skip rows the
6797            // reader's snapshot cannot see (e.g. tombstoned versions),
6798            // otherwise COUNT/SUM/etc. would tally dead rows. A no-op
6799            // under the default gate-off: every hot row is frozen or
6800            // committed-and-alive, so `is_row_visible` returns true.
6801            // Cold-tier rows are frozen (visible) by definition — left
6802            // ungated, matching the plain-scan path.
6803            let scan_snapshot = self.current_snapshot();
6804            // v7.39 (pg_stat knife B) — this full-scan branch walks
6805            // headers directly (serial and sharded alike); count the
6806            // sequential scan here.
6807            table.note_seq_scan();
6808            // v7.39 (parallel-agg P2) — the visibility probe + WHERE
6809            // filter dominate the pre-aggregate wall time on big
6810            // scans (P1's ground truth: accumulation is only ~17%).
6811            // Shard THAT work when the host injected an executor and
6812            // the WHERE is compiled (the compiled evaluator is pure
6813            // over &row; the tree-walker fallback can hit correlated
6814            // subqueries and stays serial). Shards return surviving
6815            // ROW INDICES — &Row can't cross the Box<dyn Any>'s
6816            // 'static bound — and the main thread only dereferences.
6817            let n = table.row_count();
6818            let par = self.parallel_runner.0.as_deref().filter(|_| {
6819                n >= crate::PARALLEL_MIN_ROWS && (stmt.where_.is_none() || compiled_where.is_some())
6820            });
6821            // v7.38.11 — ask the BRIN summary first. When it prunes,
6822            // the work left is a few thousand rows and sharding it
6823            // costs more than it saves, so the serial pruned loop below
6824            // takes it; the shard machinery is left exactly as it was
6825            // rather than taught about slots.
6826            let brin_slots = stmt
6827                .where_
6828                .as_ref()
6829                .and_then(|w| crate::brin::candidate_slots(w, table));
6830            let brin_prunes = brin_slots
6831                .as_ref()
6832                .is_some_and(|s| s.iter().map(core::ops::Range::len).sum::<usize>() * 2 < n);
6833            if let Some(r) = par
6834                && !brin_prunes
6835            {
6836                let n_shards = (n / crate::PARALLEL_MIN_ROWS).clamp(2, 8);
6837                let chunk = n.div_ceil(n_shards);
6838                type ShardOut = Result<alloc::vec::Vec<usize>, EngineError>;
6839                let cw = &compiled_where;
6840                let snap_ref = &scan_snapshot;
6841                let results = r.run_shards(n_shards, &|s| {
6842                    let lo = s * chunk;
6843                    let hi = ((s + 1) * chunk).min(n);
6844                    let mut keep: alloc::vec::Vec<usize> = alloc::vec::Vec::with_capacity(hi - lo);
6845                    // EvalContext carries Cells (sampler / row counters)
6846                    // and is !Sync — each shard builds its own from the
6847                    // same Sync inputs. The compiled WHERE is gated to
6848                    // the pure-scalar whitelist, which reads none of the
6849                    // session state the engine-built ctx would add
6850                    // (TABLESAMPLE's __tsm_fract is not whitelisted, so
6851                    // sampled scans never take this branch).
6852                    let shard_ctx = EvalContext::new(schema_cols, Some(alias));
6853                    let mut stack: Vec<Value<'static>> = Vec::new();
6854                    let out: ShardOut = (|| {
6855                        for i in lo..hi {
6856                            if !table.is_row_visible(i, snap_ref) {
6857                                continue;
6858                            }
6859                            let row = &table.rows()[i];
6860                            // v7.39 (round 480) — the parallel full-scan
6861                            // shard is the path the aggregate benchmark
6862                            // actually takes, and it was still on the OWNED
6863                            // entry: round 480's profile attributed 68.7 %
6864                            // of `drop_glue<Value>` to this closure, which
6865                            // is why round 479's fix to the indexed path
6866                            // barely moved the total.
6867                            //
6868                            // The `matches!(…, Value::Bool(true))` form was
6869                            // also a narrower reading than the rest of the
6870                            // engine uses — `predicate_is_true` is what
6871                            // handles NULL and MySQL truthiness — so the
6872                            // bool entry fixes the shape as well as the cost.
6873                            let pass = match cw {
6874                                Some(c) => eval::compiled::eval_compiled_pred(
6875                                    c,
6876                                    row,
6877                                    &shard_ctx,
6878                                    &mut stack,
6879                                    shard_ctx.mysql_dialect,
6880                                )
6881                                .map_err(EngineError::Eval)?,
6882                                None => true,
6883                            };
6884                            if pass {
6885                                keep.push(i);
6886                            }
6887                        }
6888                        Ok(keep)
6889                    })();
6890                    alloc::boxed::Box::new(out)
6891                });
6892                // v7.39 (round 567) — `rows()` is a 32-way trie, so
6893                // indexing it is four dependent loads and a scan that
6894                // reads every row paid them every row. A profile of
6895                // `SELECT sum(id)` over 500k rows put 37.8% of the
6896                // connection thread's CPU on THIS ONE LINE. The cursor
6897                // holds the leaf, making that one descent per 32.
6898                let mut rows_cur = table.rows().run_cursor();
6899                for boxed in results {
6900                    let shard = boxed
6901                        .downcast::<ShardOut>()
6902                        .expect("runner echoes the closure's box");
6903                    for i in (*shard)? {
6904                        if let Some(row) = rows_cur.get(i) {
6905                            filtered.push(row);
6906                        }
6907                    }
6908                }
6909            } else {
6910                let mut rows_cur = table.rows().run_cursor();
6911                // v7.38.11 — the slots the BRIN summary could not rule
6912                // out. The predicate still runs on every row that
6913                // survives: the summary decides what to SKIP, never
6914                // what to return.
6915                let ranges = brin_slots.unwrap_or_else(|| alloc::vec![0..n]);
6916                for range in ranges {
6917                    for i in range {
6918                        if !table.is_row_visible(i, &scan_snapshot) {
6919                            continue;
6920                        }
6921                        let Some(row) = rows_cur.get(i) else { continue };
6922                        if !row_passes_where(row, &mut eval_stack, &mut memo)? {
6923                            continue;
6924                        }
6925                        filtered.push(row);
6926                    }
6927                }
6928            }
6929            for row in &cold_rows_storage {
6930                if !row_passes_where(row, &mut eval_stack, &mut memo)? {
6931                    continue;
6932                }
6933                filtered.push(row);
6934            }
6935        }
6936        // v7.29 — a per-query memo so correlated scalar
6937        // subqueries batch-evaluate once (group map) instead of
6938        // executing per group.
6939        let agg_memo = core::cell::RefCell::new(memoize::MemoizeCache::default());
6940        let agg_correlated = |e: &Expr, r: &Row<'static>, c: &EvalContext<'_>| {
6941            self.eval_expr_with_correlated(e, r, c, cancel, Some(&mut agg_memo.borrow_mut()))
6942                .map_err(|err| match err {
6943                    EngineError::Eval(ev) => ev,
6944                    other => eval::EvalError::TypeMismatch {
6945                        detail: alloc::format!("{other}"),
6946                    },
6947                })
6948        };
6949        // v7.39 (round 656) — the plain relational scan. This collect() was
6950        // the measured defect: one 64-byte `RowRef` per surviving row to
6951        // wrap an 8-byte pointer `filtered` already holds. Scalar
6952        // aggregates measured ~81 bytes/row of working memory because of
6953        // it — 40 MB at 500k rows, 3.2 GB at 50M, for a query that returns
6954        // one number. `AggRows::Ptrs` reads the pointers directly.
6955        let agg = aggregate::run(
6956            stmt,
6957            crate::join::AggRows::Ptrs(&filtered),
6958            schema_cols,
6959            Some(alias),
6960            Some(&agg_correlated),
6961            self.parallel_runner.0.as_deref(),
6962            Some(self.active_catalog()),
6963            Some(self),
6964        )?;
6965        self.finish_agg_result(agg, stmt, cancel)
6966    }
6967
6968    /// Single-table scan + projection path: WHERE filter (compiled when
6969    /// subquery-free), ORDER BY keying, SRF expansion / projection, then
6970    /// sort + WITH TIES / DISTINCT / OFFSET-LIMIT.
6971    fn run_single_table_scan<'a>(
6972        &self,
6973        stmt: &SelectStatement,
6974        table: &'a spg_storage::Table,
6975        schema_cols: &'a [ColumnSchema],
6976        alias: &str,
6977        indexed_rows: Option<crate::index_access::Seeked<'a>>,
6978        cancel: CancelToken<'_>,
6979    ) -> Result<QueryResult, EngineError> {
6980        // v7.38 (read01 U15) — a fresh per-scan sampler cell for
6981        // `TABLESAMPLE … REPEATABLE(seed)`. Created before the ctx so the
6982        // deterministic `__tsm_fract(seed)` draws share one scan-local
6983        // state (isolated from the global random() PRNG); a fresh cell per
6984        // scan makes a repeat / rescan reproduce the same sample. Unused
6985        // and cheap when the query carries no sample.
6986        let sample_cell: core::cell::Cell<Option<u64>> = core::cell::Cell::new(None);
6987        let ctx = self
6988            .ev_ctx(schema_cols, Some(alias))
6989            .with_sample_rng(&sample_cell);
6990        let projection = build_projection(
6991            &stmt.items,
6992            schema_cols,
6993            alias,
6994            self.speaks_mysql,
6995            Some(self.active_catalog()),
6996        )?;
6997        // v7.19 P5 — single-table SELECT path for SRF
6998        // `SELECT unnest(arr) FROM t` shape. Detect a top-level
6999        // unnest in the projection list. When present, the
7000        // per-row processor emits one output row per array
7001        // element (broadcasting non-SRF projections from the
7002        // same input row). Empty / NULL arrays emit zero rows
7003        // for that input — PG semantics.
7004        // v7.39 (read01 round 67) — every SRF in the target list, in lockstep.
7005        let srf_idxs = self.srf_target_idxs(&projection);
7006        let srf_position = srf_idxs.first().copied();
7007        // v7.39 (round 599) — the SRF analysis is per QUERY, not per row.
7008        let mut srf_plan = if srf_position.is_some() {
7009            Some(build_srf_plan(self, &projection, &srf_idxs, &ctx)?)
7010        } else {
7011            None
7012        };
7013
7014        // Materialise the filter pass into `(order_key, projected_row)`
7015        // tuples. The order key is `None` when there's no ORDER BY clause.
7016        let mut tagged: Vec<(Vec<OrderKey>, Row<'static>)> = Vec::new();
7017        // v7.33 (C1, ceiling-first/never-die) — charge each accumulated
7018        // output row to the per-query byte budget as it is built, so a
7019        // fat single-table scan / sort REJECTS with QueryBytesExceeded
7020        // at ~the ceiling instead of materialising the whole table and
7021        // only noticing at the final enforce_row_limit check. Without
7022        // this, N concurrent fat scans peak at N×table and OOM the host.
7023        // `max_query_bytes = None` (the embedded default) = no ceiling,
7024        // so existing unbudgeted behaviour is byte-identical.
7025        let mut budget = ByteBudget::new(self.max_query_bytes);
7026        // v6.2.6 — Memoize per-row WHERE eval shares one cache.
7027        let mut memo = memoize::MemoizeCache::new();
7028        // v7.32 (perf knife D) — subquery-free WHERE compiles once;
7029        // the row loop then runs a flat step program instead of a
7030        // tree interpretation per row.
7031        let compiled_where: Option<eval::CompiledExpr> = stmt
7032            .where_
7033            .as_ref()
7034            .filter(|w| eval::fully_compilable(w))
7035            .map(|w| {
7036                // v7.38.8 — the scan filter runs the cheap half of its
7037                // conjunction first. Called from HERE and not from
7038                // `eval::compiled`, deliberately: the row loop lives in
7039                // that file, and adding a function to it cost this
7040                // query 11 % through layout alone while doing no work
7041                // for it. See `crate::qualorder`.
7042                match crate::qualorder::reordered(w) {
7043                    Some(r) => eval::compile_expr(&r, &ctx),
7044                    None => eval::compile_expr(w, &ctx),
7045                }
7046            });
7047        let mut eval_stack: Vec<Value<'static>> = Vec::new();
7048        // v7.37.x (docker-fair SCALARSQ attack) — pre-analyse every
7049        // SELECT-item scalar subquery for the PK-probe fast path. The
7050        // analysis (gate checks + catalog lookups) takes ~500 ns; doing
7051        // it once per query instead of once per row × 100 rows saves
7052        // ~50 µs and lets the per-row evaluation reduce to a single
7053        // index probe + outer-column read.
7054        let scalarsq_fast: Vec<Option<crate::ScalarPkProbeFastPath>> = projection
7055            .iter()
7056            .map(|p| {
7057                if let Expr::ScalarSubquery(inner) = &p.expr {
7058                    self.analyse_scalar_count_pk_eq_probe(inner, schema_cols, alias)
7059                } else {
7060                    None
7061                }
7062            })
7063            .collect();
7064        let any_scalarsq_fast = scalarsq_fast.iter().any(Option::is_some);
7065        // v7.39 (round 487) — a projection item that is a bare column
7066        // reference binds its position ONCE per query.
7067        //
7068        // Per row it used to walk `eval_expr_with_correlated` (a memo
7069        // lookup for "does this have a subquery", then an un-memoised
7070        // `expr_may_use_in_set` tree walk), then `eval_expr`'s dispatch,
7071        // then `resolve_column`, which finds the column by scanning the
7072        // schema and comparing NAMES. On `SELECT g FROM h` that chain was
7073        // 19 % of self time for what is ultimately one cell read.
7074        //
7075        // `compile_column_pos` is the Step VM's resolver, already
7076        // `pub(crate)` and already reused by the aggregate's bind-once
7077        // path: it mirrors `resolve_column`'s happy layers and returns
7078        // None for anything that would reach an error, an ambiguity, or a
7079        // miss, so those still go the interpreter's way and keep its
7080        // exact message. A composite column is excluded for the same
7081        // reason `compile_into` excludes it — it must be rehydrated from
7082        // stored JSON, which is not a cell read.
7083        let proj_direct = bind_direct_columns(&projection, &ctx);
7084        let any_proj_direct = proj_direct.iter().any(Option::is_some);
7085        // v7.39 (round 605) — a projection item that cannot depend on the row
7086        // is evaluated once. `SELECT ('{"a":1}')::JSONB FROM j` cost TEN
7087        // allocations a row against one for a plain column, `'abc' || 'def'`
7088        // six and `upper('abc')` five, all of them producing the same value
7089        // 50,000 times. An item that fails to evaluate is left alone, so its
7090        // error still comes from the row loop in the interpreter's wording.
7091        let proj_const: Vec<Option<Value<'static>>> = projection
7092            .iter()
7093            .map(|p| crate::eval::compiled::constant_projection_value(&p.expr, &ctx))
7094            .collect();
7095        let any_proj_const = proj_const.iter().any(Option::is_some);
7096        crate::bump_counter!(crate::select::SCAN_PATH_ENTERED);
7097        // v7.39 (read01 round 80) — positional ORDER BY over a WILDCARD
7098        // projection. Statement prep (`resolve_order_by_position`) can only map
7099        // `ORDER BY 1` onto the first SELECT item when that item is an
7100        // expression; a `*` is not one, so the literal survived to here and was
7101        // evaluated as the CONSTANT 1 — the same key for every row, i.e. no sort
7102        // at all. The parser rewrites `SELECT unnest(a) x` into
7103        // `SELECT * FROM unnest(a) x`, so that innocuous-looking shape landed
7104        // exactly here: `SELECT unnest(ARRAY['B','a','A','b']) ORDER BY 1` came
7105        // back in input order. The projection is built by now, so the Nth output
7106        // column is known — resolve against it.
7107        let order_by = resolve_positional_order_by(&stmt.order_by, &projection);
7108        // v7.39 (round 600) — the ORDER BY of an SRF query is decided on the
7109        // EXPANDED rows, so a key naming a select-list item reads that item.
7110        let srf_order_cols: Vec<Option<usize>> = if srf_position.is_some() {
7111            srf_order_output_cols(&order_by, &projection)
7112        } else {
7113            Vec::new()
7114        };
7115        let srf_key_bound: Vec<Option<usize>> = (0..order_by.len()).map(Some).collect();
7116        // v7.37.x (docker-fair SCALARSQ attack) — early-limit gate for
7117        // the no-ORDER-BY-no-DISTINCT-no-TIES-no-SRF-no-WHERE shape.
7118        // Hoisted above the closure so the projection-eval path can
7119        // gate `memo` passing on it: the SELECT-item correlated-scalar
7120        // batch path scans the FULL inner table once (~5 ms for 12.5 k
7121        // rows) and is only a win when N outer rows is large; for small
7122        // LIMITed shapes a per-row PK seek (~5 µs × 100 = 500 µs) wins.
7123        let early_cap: Option<usize> = if order_by.is_empty()
7124            && !stmt.distinct
7125            && !stmt.limit_with_ties
7126            && srf_position.is_none()
7127            && stmt.where_.is_none()
7128        {
7129            stmt.limit_literal()
7130                .map(|n| n.saturating_add(stmt.offset_literal().unwrap_or(0)) as usize)
7131        } else {
7132            None
7133        };
7134        // v7.38 (read01 B8) — streaming top-N budget. For `ORDER BY …
7135        // LIMIT k` (no DISTINCT / WITH TIES / SRF, and not forced to
7136        // full-sort by the test gate) keep only the running top-`keep`
7137        // rows in memory instead of materialising every projected row,
7138        // so a `… ORDER BY col LIMIT 10` over a huge table is O(keep)
7139        // space, not O(rows). `None` = accumulate everything (the prior
7140        // behaviour). The final `partial_sort_tagged(keep)` below still
7141        // runs and produces the identical rows.
7142        // v7.39 (round 683) — the declared collation for each ORDER BY
7143        // position, resolved once and carried beside `descs` for the same
7144        // reason `descs` is carried: it is per key position, not per row.
7145        let order_colls = crate::orderby::order_by_collations(&order_by, &ctx)?;
7146        let topk_stream: Option<(usize, Vec<bool>)> = if !order_by.is_empty()
7147            && !stmt.distinct
7148            && !stmt.limit_with_ties
7149            && srf_position.is_none()
7150            && !self.env_cfg().disable_topk
7151        {
7152            stmt.limit_literal().and_then(|l| {
7153                let keep = (l as usize).saturating_add(stmt.offset_literal().unwrap_or(0) as usize);
7154                (keep >= 1).then(|| (keep, order_by.iter().map(|o| o.desc).collect()))
7155            })
7156        } else {
7157            None
7158        };
7159        // v7.38.19 — when the sort column is one the projection already
7160        // carries, build no key at all and sort by reading it.
7161        //
7162        // Restricted to the FULL sort: a top-N compares against a stored
7163        // boundary key and `WITH TIES` extends past the limit through the
7164        // keys, both of which need one to exist. DISTINCT keys on them
7165        // too, and an SRF's keys come from the EXPANDED row.
7166        // A COLLATION does not rule it out, but it has to be one that
7167        // orders these values the way bytes do -- decided on the values
7168        // themselves, further down, once they exist.
7169        let sort_by_output: Option<Vec<usize>> = if stmt.distinct
7170            || stmt.limit_with_ties
7171            || srf_position.is_some()
7172            || topk_stream.is_some()
7173        {
7174            None
7175        } else {
7176            order_by_output_cols_if_identical(&order_by, &projection, schema_cols)
7177        };
7178        // v7.37.16 — streaming DISTINCT seen-set: norm-hash → indices of
7179        // kept rows in `tagged`. Probing on the PROJECTED row as soon as
7180        // it is built means a duplicate costs neither a build_order_keys
7181        // eval (the dominant per-row cost of `DISTINCT … ORDER BY`) nor
7182        // a tagged slot, and the sort below runs over u survivors, not
7183        // n input rows — PG's hash-distinct-then-sort plan shape.
7184        let mut seen_distinct: hashbrown::HashMap<u64, crate::distinct::DistinctBucket> =
7185            hashbrown::HashMap::new();
7186        let distinct_hb = hashbrown::DefaultHashBuilder::default();
7187        // v7.38.13 — which output positions must NOT fold. Built once per
7188        // scan from the projection, which carries the source column's
7189        // byte-wise-ness; see `FoldSpec`.
7190        let distinct_mask = fold_mask(&projection);
7191        // v7.39 (round 485) — one projection buffer for the whole scan
7192        // rather than a fresh `Vec` per input row. A row that survives
7193        // the DISTINCT probe takes the buffer with it (`mem::take`) and
7194        // the next row allocates a new one; a row that duplicates an
7195        // earlier one leaves the buffer — and its capacity — in place.
7196        // The round-485 counter says 49 900 of `distinct_proj`'s 50 000
7197        // projected rows are duplicates, so that is 49 900 allocate /
7198        // free pairs the scan no longer performs. Shapes where every row
7199        // survives (plain projection, `DISTINCT` over a unique column)
7200        // allocate exactly as often as before.
7201        let mut proj_buf: Vec<Value<'static>> = Vec::new();
7202        // v7.39 (round 571) — buffers handed back by the top-N trim.
7203        // Round 485 made the scan share ONE projection buffer, but a
7204        // surviving row takes it (`mem::take`) and without DISTINCT
7205        // almost every row survives, so the next one starts from zero
7206        // capacity and allocates. The trim drops `keep` rows at a time
7207        // and their buffers come back here instead of being freed.
7208        let mut proj_pool: Vec<Vec<Value<'static>>> = Vec::new();
7209        let mut key_pool: Vec<Vec<crate::orderby::OrderKey>> = Vec::new();
7210        // v7.39 (round 581) — the worst row the accumulator is currently
7211        // keeping. Anything that loses to it cannot reach the answer, so
7212        // it is dropped before its projection is ever built.
7213        let mut topk_boundary: Option<Vec<crate::orderby::OrderKey>> = None;
7214        // v7.38.20 — the boundary's own leading eight bytes, so a losing
7215        // row can be turned away before a key is built for it. Kept
7216        // beside the boundary and refreshed with it; `None` whenever the
7217        // boundary's first key is not one this can read, which sends
7218        // every row down the ordinary path.
7219        // v7.38.21 — and whether those bytes may be trusted under the
7220        // collation in force, which is the boundary's own text to answer.
7221        let mut topk_boundary_prefix: Option<(crate::orderby::PrefixKind, u64, bool)> = None;
7222        // v7.39 (round 582) — resolve each ORDER BY column once, not
7223        // once per row. See `order_by_bound_positions`.
7224        let order_bound =
7225            crate::orderby::order_by_bound_positions(&order_by, schema_cols, Some(alias));
7226        // v7.39.12 — a correlated scalar subquery in ORDER BY is
7227        // resolved for the row before its key is built.
7228        //
7229        // Uncorrelated subqueries are replaced by a literal before
7230        // execution; a correlated one cannot be, so it reached the
7231        // per-row evaluator — the one place that cannot run a subquery
7232        // — and the statement raised "subquery reached row eval".
7233        // Reported by sentori against 7.39.11; see
7234        // `Engine::order_by_resolved_for_row`.
7235        //
7236        // The `any` runs once, here, so an ordinary ORDER BY pays one
7237        // bool per row and nothing else.
7238        let order_has_subquery = order_by
7239            .iter()
7240            .any(|o| crate::subquery::expr_has_subquery(&o.expr));
7241        let unbound: Vec<Option<usize>> = alloc::vec![None; order_by.len()];
7242        // v7.39 (round 581) — and it stops asking when the answer is
7243        // always "keep".
7244        //
7245        // The check earns its place only on rows it rejects. Over
7246        // ascending ids, `ORDER BY id DESC` never rejects one — every
7247        // row beats the current worst — so the comparison is pure
7248        // overhead there, measured at +5.5% in three batches out of
7249        // three. After a window of rows it looks at what it has
7250        // actually rejected and switches itself off if the shape is not
7251        // paying. The answers do not depend on it either way.
7252        // v7.38.21 — resolved once per query, not per row.
7253        //
7254        // No collation at all is the case v7.38.20 shipped. A DECLARED
7255        // one may still be answered by bytes, and which collations those
7256        // are is `Collated::ascii_byte_order`'s to say — the same
7257        // allowlist `byte_order_answers_the_collation` consults, so the
7258        // two cannot come to disagree about a collation. What that
7259        // allowlist requires of the TEXT is checked per row and on the
7260        // boundary, because a streaming top-N has no batch to check.
7261        let boundary_no_collation = order_colls.iter().all(Option::is_none);
7262        let boundary_collations_permit = boundary_no_collation
7263            || order_colls
7264                .iter()
7265                .flatten()
7266                .all(crate::collate::Collated::ascii_byte_order);
7267        const BOUNDARY_WINDOW: u32 = 8192;
7268        let mut boundary_checks: u32 = 0;
7269        let mut boundary_rejects: u32 = 0;
7270        let mut boundary_check_on = true;
7271        // Inline the per-row work in a closure so the indexed and full-
7272        // scan branches share the body.
7273        // v7.38.19 — `check_where` is per CALL SITE, not per closure: the
7274        // full-scan loops below must apply the predicate, and the
7275        // indexed loop must not when the seek already did. A captured
7276        // flag would have to be right for both.
7277        let mut process_row = |row: &Row<'static>,
7278                               loop_idx: usize,
7279                               check_where: bool|
7280         -> Result<(), EngineError> {
7281            if loop_idx.is_multiple_of(256) {
7282                cancel.check()?;
7283            }
7284            if !check_where {
7285                // The seek answered the whole predicate. See
7286                // `index_access::Seeked`.
7287            } else if let Some(cw) = &compiled_where {
7288                let cond = eval::eval_compiled(cw, row, &ctx, &mut eval_stack)
7289                    .map_err(EngineError::Eval)?;
7290                if !crate::eval::predicate_is_true(&cond, "WHERE", ctx.mysql_dialect)? {
7291                    return Ok(());
7292                }
7293            } else if let Some(where_expr) = &stmt.where_ {
7294                let cond =
7295                    self.eval_expr_with_correlated(where_expr, row, &ctx, cancel, Some(&mut memo))?;
7296                if !crate::eval::predicate_is_true(&cond, "WHERE", ctx.mysql_dialect)? {
7297                    return Ok(());
7298                }
7299            }
7300            // Under DISTINCT the keys are built AFTER the dup probe
7301            // (survivors only); the non-distinct order is unchanged.
7302            // v7.39 (round 600) — an SRF query's keys are built per EXPANDED
7303            // row further down, and building them here would evaluate the
7304            // ORDER BY against the INPUT row: a key naming the SRF's own
7305            // output became a scalar call to it, which is where
7306            // "function unnest(integer[]) does not exist" came from.
7307            let order_keys = if order_by.is_empty()
7308                || stmt.distinct
7309                || srf_position.is_some()
7310                // v7.38.19 — the branch below builds whatever key it
7311                // needs from the projected values, collation included,
7312                // so nothing has to be built here for it.
7313                //
7314                // A draft that skipped them here but still let the
7315                // COLLATED case fall through to the key-based sort put a
7316                // mixed column back in INSERT order: every key empty,
7317                // every row equal, a stable sort faithfully preserving
7318                // nothing. The rule is one decision, not two.
7319                || sort_by_output.is_some()
7320            {
7321                Vec::new()
7322            } else {
7323                // v7.38.20 — turn a decisively losing row away before
7324                // its key is built. Only the FIRST key is read, and only
7325                // its leading eight bytes; a tie there decides nothing
7326                // and falls through to the full path below.
7327                //
7328                // ASC only: under DESC the boundary is the largest kept
7329                // key and the comparison flips, which this deliberately
7330                // does not try to express — a second direction in a
7331                // fast-path predicate is how one of them ends up wrong.
7332                if boundary_check_on
7333                    && let Some((_, descs)) = &topk_stream
7334                    && !descs.first().copied().unwrap_or(false)
7335                    && order_by.len() == 1
7336                    && boundary_collations_permit
7337                    && let Some((bkind, bp, boundary_is_ascii)) = topk_boundary_prefix
7338                    && let Some((rkind, rp, row_is_ascii)) =
7339                        crate::orderby::first_key_prefix(&order_bound, row)
7340                    && bkind == rkind
7341                    && (boundary_no_collation || (boundary_is_ascii && row_is_ascii))
7342                    && rp > bp
7343                {
7344                    boundary_checks += 1;
7345                    boundary_rejects += 1;
7346                    if boundary_checks == BOUNDARY_WINDOW {
7347                        boundary_check_on = boundary_rejects.saturating_mul(4) >= boundary_checks;
7348                    }
7349                    return Ok(());
7350                }
7351                let mut buf = key_pool.pop().unwrap_or_default();
7352                if order_has_subquery {
7353                    // A substituted literal is no longer a bound column.
7354                    let per_row = self.order_by_resolved_for_row(&order_by, row, &ctx, cancel)?;
7355                    crate::orderby::build_order_keys_bound(
7356                        per_row.as_deref().unwrap_or(&order_by),
7357                        &unbound,
7358                        &order_colls,
7359                        row,
7360                        &ctx,
7361                        &mut buf,
7362                    )?;
7363                } else {
7364                    crate::orderby::build_order_keys_bound(
7365                        &order_by,
7366                        &order_bound,
7367                        &order_colls,
7368                        row,
7369                        &ctx,
7370                        &mut buf,
7371                    )?;
7372                }
7373                // v7.39 (round 581) — reject before projecting.
7374                //
7375                // `ORDER BY g DESC, id DESC LIMIT 10` over 500k rows with
7376                // 50 distinct `g` decides nearly every row on the FIRST
7377                // key, and PG answers it FASTER than the single-key form
7378                // (7.4 ms against 10.4) because a rejected row costs it
7379                // one comparison. SPG built both keys AND the projected
7380                // row for all 500k before throwing them away. The keys
7381                // are needed to compare; the projection is not.
7382                if boundary_check_on
7383                    && let Some((_, descs)) = &topk_stream
7384                    && let Some(b) = &topk_boundary
7385                {
7386                    boundary_checks += 1;
7387                    let loses = crate::orderby::cmp_multi_key_in(&buf, b, descs, &order_colls)
7388                        == core::cmp::Ordering::Greater;
7389                    if loses {
7390                        boundary_rejects += 1;
7391                    }
7392                    if boundary_checks == BOUNDARY_WINDOW {
7393                        // Keep asking only if it has been rejecting at
7394                        // least a quarter of what it saw.
7395                        boundary_check_on = boundary_rejects.saturating_mul(4) >= boundary_checks;
7396                    }
7397                    if loses {
7398                        buf.clear();
7399                        key_pool.push(buf);
7400                        return Ok(());
7401                    }
7402                }
7403                buf
7404            };
7405            if srf_position.is_some() {
7406                let plan = srf_plan.as_mut().expect("srf_position implies a plan");
7407                for out in expand_srf_row_with(self, plan, &projection, row, &ctx)? {
7408                    if stmt.distinct {
7409                        let bucket = seen_distinct
7410                            .entry(norm_hash_row(
7411                                &out,
7412                                &distinct_hb,
7413                                FoldSpec::of(ctx.mysql_dialect, &distinct_mask),
7414                            ))
7415                            .or_default();
7416                        if bucket.iter().any(|i| {
7417                            row_eq_norm(
7418                                &tagged[i].1,
7419                                &out,
7420                                FoldSpec::of(ctx.mysql_dialect, &distinct_mask),
7421                            )
7422                        }) {
7423                            continue;
7424                        }
7425                        bucket.push(tagged.len());
7426                    }
7427                    budget.charge(approx_row_bytes(&out))?;
7428                    // The keys come from THIS expanded row: a key naming a
7429                    // select-list item reads its value, anything else is
7430                    // still evaluated against the input row.
7431                    let keys = if order_by.is_empty() {
7432                        Vec::new()
7433                    } else {
7434                        let mut kv: Vec<Value<'static>> = Vec::with_capacity(order_by.len());
7435                        for (k, ob) in order_by.iter().enumerate() {
7436                            kv.push(match srf_order_cols.get(k).copied().flatten() {
7437                                Some(p) => out.values.get(p).cloned().unwrap_or(Value::Null),
7438                                None => eval::eval_expr(&ob.expr, row, &ctx)
7439                                    .map_err(EngineError::Eval)?,
7440                            });
7441                        }
7442                        // Packed by the same code every other ORDER BY uses,
7443                        // so DESC / NULLS FIRST / the MySQL rule are not
7444                        // restated here.
7445                        let key_row = Row::new(kv);
7446                        let mut buf = Vec::new();
7447                        crate::orderby::build_order_keys_bound(
7448                            &order_by,
7449                            &srf_key_bound,
7450                            &order_colls,
7451                            &key_row,
7452                            &ctx,
7453                            &mut buf,
7454                        )?;
7455                        buf
7456                    };
7457                    tagged.push((keys, out));
7458                }
7459            } else {
7460                let values = &mut proj_buf;
7461                values.clear();
7462                values.reserve(projection.len());
7463                for (i, p) in projection.iter().enumerate() {
7464                    // v7.37.x (docker-fair SCALARSQ attack) — pre-
7465                    // analysed PK-probe fast path. The per-row work is
7466                    // a read of outer.col from the row plus an index
7467                    // probe — no Expr clone, no walker, no
7468                    // `eval_expr_with_correlated` framework.
7469                    if any_scalarsq_fast && let Some(fp) = &scalarsq_fast[i] {
7470                        values.push(self.probe_with_pk_fast_path(fp, row));
7471                        continue;
7472                    }
7473                    // v7.39 (round 605) — the same value every row.
7474                    if any_proj_const && let Some(v) = &proj_const[i] {
7475                        values.push(v.clone());
7476                        continue;
7477                    }
7478                    // v7.39 (round 487) — bound column: read the cell.
7479                    // This is `rehydrate_cell`'s body for a non-composite
7480                    // column, which is what the whole chain below reduces
7481                    // to once the name has been resolved.
7482                    if any_proj_direct && let Some(pos) = proj_direct[i] {
7483                        crate::bump_counter!(crate::select::PROJ_DIRECT_FIRE);
7484                        values.push(row.values[pos].clone().into_owned());
7485                        continue;
7486                    }
7487                    // v7.24 (round-16 B) — correlated-aware.
7488                    // v7.37.x (docker-fair SCALARSQ attack) — share the
7489                    // per-row memo with projection. Required for the
7490                    // batch-evaluated correlated-scalar path to fire on
7491                    // SELECT-item scalar subqueries; otherwise each row
7492                    // re-executes the inner.
7493                    //
7494                    // Skip the memo when the outer row count is small
7495                    // (early-limited): the batch path scans the FULL
7496                    // inner table to build a GroupMap (~5 ms for a
7497                    // 12.5 k-row inner), while per-row execution with a
7498                    // PK index seek is ~5 µs per call — much cheaper for
7499                    // N ≤ ~1000 outer rows.
7500                    let pass_memo = early_cap.is_none_or(|cap| cap > 1000);
7501                    let memo_arg = if pass_memo { Some(&mut memo) } else { None };
7502                    values.push(
7503                        self.eval_expr_with_correlated(&p.expr, row, &ctx, cancel, memo_arg)?,
7504                    );
7505                }
7506                crate::bump_counter!(crate::select::PROJ_ROW_BUILT);
7507                if stmt.distinct {
7508                    let bucket = seen_distinct
7509                        .entry(norm_hash_values(
7510                            &proj_buf,
7511                            &distinct_hb,
7512                            FoldSpec::of(ctx.mysql_dialect, &distinct_mask),
7513                        ))
7514                        .or_default();
7515                    if bucket.iter().any(|i| {
7516                        values_eq_norm(
7517                            &tagged[i].1.values,
7518                            &proj_buf,
7519                            FoldSpec::of(ctx.mysql_dialect, &distinct_mask),
7520                        )
7521                    }) {
7522                        crate::bump_counter!(crate::select::DISTINCT_DUP_DROPPED);
7523                        return Ok(());
7524                    }
7525                    bucket.push(tagged.len());
7526                }
7527                let out = Row::new(core::mem::replace(
7528                    &mut proj_buf,
7529                    proj_pool.pop().unwrap_or_default(),
7530                ));
7531                let order_keys = if stmt.distinct && !order_by.is_empty() {
7532                    // v7.38.13 — `&order_bound`, not `&[]`. Round 582 added
7533                    // the bound-cell path precisely so an ORDER BY key that
7534                    // names a column is READ instead of evaluated, and the
7535                    // non-DISTINCT branch above has passed it ever since;
7536                    // this branch never did, so `SELECT DISTINCT k .. ORDER
7537                    // BY k` resolved "k" by string for every surviving row.
7538                    let mut buf = key_pool.pop().unwrap_or_default();
7539                    if order_has_subquery {
7540                        // A substituted literal is no longer a bound column.
7541                        let per_row =
7542                            self.order_by_resolved_for_row(&order_by, row, &ctx, cancel)?;
7543                        crate::orderby::build_order_keys_bound(
7544                            per_row.as_deref().unwrap_or(&order_by),
7545                            &unbound,
7546                            &order_colls,
7547                            row,
7548                            &ctx,
7549                            &mut buf,
7550                        )?;
7551                    } else {
7552                        crate::orderby::build_order_keys_bound(
7553                            &order_by,
7554                            &order_bound,
7555                            &order_colls,
7556                            row,
7557                            &ctx,
7558                            &mut buf,
7559                        )?;
7560                    }
7561                    buf
7562                } else {
7563                    order_keys
7564                };
7565                budget.charge(approx_row_bytes(&out))?;
7566                tagged.push((order_keys, out));
7567            }
7568            // Streaming top-N: bound the accumulator to O(keep) rows.
7569            if let Some((k, descs)) = &topk_stream {
7570                crate::orderby::topk_trim_recycling(
7571                    &mut tagged,
7572                    *k,
7573                    descs,
7574                    &mut proj_pool,
7575                    &mut key_pool,
7576                    &mut topk_boundary,
7577                );
7578                // The prefix follows the boundary it summarises.
7579                topk_boundary_prefix = topk_boundary
7580                    .as_ref()
7581                    .and_then(|b| b.first())
7582                    .and_then(crate::orderby::order_key_prefix);
7583            }
7584            Ok(())
7585        };
7586        // v7.37.15 (Phase C.3, step 2) — MVCC visibility gate for the
7587        // load-bearing full-scan path. This is the primary single-table
7588        // executor; pre-C.3 it read every hot-tier row raw. Once C.3's
7589        // in-place writers retain dead/old versions, an ungated scan
7590        // here would return them, so the gate must land BEFORE the
7591        // writers flip (see the plan's activation-order rule). A no-op
7592        // today: every hot row is frozen or committed-and-alive under
7593        // the reader's snapshot, so `is_row_visible` returns true for
7594        // all of them (verified by the full e2e suite staying green).
7595        let scan_snapshot = self.current_snapshot();
7596        let mut emitted: usize = 0;
7597        if let Some(seeked) = &indexed_rows {
7598            let recheck = !seeked.exact;
7599            for (loop_idx, cow) in seeked.rows.iter().enumerate() {
7600                if let Some(cap) = early_cap
7601                    && emitted >= cap
7602                {
7603                    break;
7604                }
7605                process_row(cow.as_ref(), loop_idx, recheck)?;
7606                emitted = emitted.saturating_add(1);
7607            }
7608        } else {
7609            // v7.39 (round 570) — the row store is a 32-way trie, so
7610            // indexing it is four dependent loads. Round 567 measured
7611            // -18% on the aggregate scan from holding the leaf between
7612            // rows; this is the same loop for the projecting scan.
7613            let mut rows_cur = table.rows().run_cursor();
7614            // v7.38.11 — see the aggregate scan above: a BRIN index on a
7615            // column this WHERE bounds says which slots cannot match.
7616            let brin_slots = stmt
7617                .where_
7618                .as_ref()
7619                .and_then(|w| crate::brin::candidate_slots(w, table))
7620                .unwrap_or_else(|| alloc::vec![0..table.row_count()]);
7621            for i in brin_slots.into_iter().flatten() {
7622                if let Some(cap) = early_cap
7623                    && emitted >= cap
7624                {
7625                    break;
7626                }
7627                // Skip rows this snapshot cannot see (invisible rows do
7628                // not count toward the LIMIT).
7629                if !table.is_row_visible(i, &scan_snapshot) {
7630                    continue;
7631                }
7632                let Some(row) = rows_cur.get(i) else { continue };
7633                process_row(row, i, true)?;
7634                emitted = emitted.saturating_add(1);
7635            }
7636            // v7.35.1 (mailrs prod #6 follow-up) — fold cold-tier
7637            // rows into the same loop. The full-scan path here is the
7638            // load-bearing single-table SELECT executor, and pre-
7639            // 7.35.1 it only walked `table.rows()` (hot), so any
7640            // `SELECT … FROM t` against a table with cold segments
7641            // silently returned a subset.
7642            let cold_rows = self.iter_cold_rows_of_table(table);
7643            for (offset, row) in cold_rows.iter().enumerate() {
7644                if let Some(cap) = early_cap
7645                    && emitted >= cap
7646                {
7647                    break;
7648                }
7649                process_row(row, table.row_count() + offset, true)?;
7650                emitted = emitted.saturating_add(1);
7651            }
7652        }
7653
7654        // (DISTINCT already de-duped STREAMING inside process_row, so the
7655        // sort below only sees the u survivors and the partial-sort
7656        // budget applies to DISTINCT too.)
7657        if !order_by.is_empty() {
7658            // Partial-sort fast path: when LIMIT is small relative to
7659            // the row count, select_nth_unstable + sort just the
7660            // prefix is O(n + k log k) instead of O(n log n).
7661            // WITH TIES needs the full sort so the tie extension can
7662            // scan past `limit` to find rows that share the last-kept
7663            // row's key.
7664            let keep = if stmt.limit_with_ties
7665                // v7.38 元机制 D acceptor — `SPG_TEST_DISABLE_TOPK=1`
7666                // forces the full-sort fallback by suppressing the
7667                // partial-sort `keep` budget. See
7668                // `xtests/sigil/test-mode-gucs.md`.
7669                || self.env_cfg().disable_topk
7670            {
7671                None
7672            } else {
7673                stmt.limit_literal()
7674                    .map(|l| l as usize + stmt.offset_literal().map_or(0, |o| o as usize))
7675            };
7676            let descs: Vec<bool> = order_by.iter().map(|o| o.desc).collect();
7677            if let Some(cols) = &sort_by_output {
7678                // No keys were built; the sort reads the projected row.
7679                // The comparator is the value-level one the window
7680                // functions and the key path both defer to, so DESC,
7681                // NULLS placement, the MySQL fold and the collation are
7682                // not restated here.
7683                let terms: Vec<(usize, bool, Option<bool>)> = cols
7684                    .iter()
7685                    .zip(order_by.iter())
7686                    .map(|(c, o)| (*c, o.desc, o.nulls_first))
7687                    .collect();
7688                let mysql = ctx.mysql_dialect;
7689                // v7.38.19 — sort a PERMUTATION carrying the first eight
7690                // bytes, not the rows.
7691                //
7692                // The elements above are `(Vec<OrderKey>, Row)`, 48 bytes,
7693                // and driftsort moves them ~n log n times: 7.4 M moves at
7694                // 400,000 rows. Worse, every comparison chases three
7695                // dependent loads PER SIDE to reach the byte it wants --
7696                // the row's `Vec`, the `Value`, then the string's own
7697                // buffer -- and a profile of this sort put 35% of its
7698                // working samples in the sort machinery around that.
7699                //
7700                // A `(u64, u32)` is 16 bytes and the comparison reads it
7701                // straight out of the array. The u64 is the first eight
7702                // bytes big-endian, zero-padded, which ORDERS THE SAME as
7703                // the string: if two differ inside those bytes they differ
7704                // at the same index either way, and a string shorter than
7705                // eight pads with zeros exactly where `[u8]`'s own
7706                // comparison runs out. Equal prefixes fall through to the
7707                // full comparator, so nothing rests on the padding being
7708                // clever.
7709                //
7710                // The tail-break on the index is what keeps the sort
7711                // STABLE, which `sort_by` was giving for free and an
7712                // unstable sort over a permutation would not.
7713                // v7.38.19 — three ways to sort these rows, and which
7714                // one is right turns on the values, which is why it is
7715                // decided here rather than at plan time.
7716                //
7717                //   * the collation orders these values the way bytes do
7718                //     -- take the eight-byte key below
7719                //   * it does not, but there IS a collation -- build its
7720                //     sort key once per row and order the permutation on
7721                //     those, which is what the key path did, done from
7722                //     the projected value instead of during the scan
7723                //   * no collation at all -- the eight-byte key again
7724                //
7725                // The middle case is the one a draft got wrong by
7726                // leaving the rows to a key path whose keys it had just
7727                // skipped building.
7728                let mut keep_sorted = false;
7729                let bytes_answer = byte_order_answers_the_collation(&tagged, &terms, &order_colls);
7730                if !bytes_answer && let Some(coll) = order_colls.first().and_then(Option::as_ref) {
7731                    let (first_col, first_desc, _) = terms[0];
7732                    let mut order: Vec<(Vec<u8>, u32)> = Vec::with_capacity(tagged.len());
7733                    for (i, row) in tagged.iter().enumerate() {
7734                        let k = match row.1.values.get(first_col) {
7735                            Some(Value::Text(t)) => coll.sort_key_of(t).unwrap_or_else(|| {
7736                                let mut v = Vec::with_capacity(t.len() + 1);
7737                                v.push(0);
7738                                v.extend_from_slice(t.as_bytes());
7739                                v
7740                            }),
7741                            _ => Vec::new(),
7742                        };
7743                        order.push((k, u32::try_from(i).unwrap_or(u32::MAX)));
7744                    }
7745                    order.sort_by(|(ka, ia), (kb, ib)| {
7746                        let c = ka.cmp(kb);
7747                        let c = if first_desc { c.reverse() } else { c };
7748                        if c != core::cmp::Ordering::Equal {
7749                            return c;
7750                        }
7751                        row_cmp_by_index(&tagged, &terms, &order_colls, mysql, *ia, *ib)
7752                            .then_with(|| ia.cmp(ib))
7753                    });
7754                    let mut slots: Vec<Option<(Vec<crate::orderby::OrderKey>, Row<'static>)>> =
7755                        core::mem::take(&mut tagged).into_iter().map(Some).collect();
7756                    tagged = order
7757                        .iter()
7758                        .map(|&(_, i)| {
7759                            slots[i as usize]
7760                                .take()
7761                                .expect("the permutation names each row once")
7762                        })
7763                        .collect();
7764                    keep_sorted = true;
7765                }
7766                // v7.38.20 — a key that does NOT discriminate is still
7767                // worth sorting on, as long as the runs it leaves are
7768                // handled once instead of n log n times.
7769                //
7770                // `text (26 values)` is two hundred identical characters
7771                // drawn from twenty-six letters, so every eight-byte
7772                // prefix inside a letter is the same and 15,384 rows tie
7773                // on it. A comparison sort then asks ~7.4 M questions of
7774                // which nearly all are a two-hundred-byte `memcmp`
7775                // answering EQUAL: profiled, 30% of the working samples
7776                // sat in `memcmp` and 37% in the sort machinery.
7777                //
7778                // Sorting the integer keys is cheap. What each run needs
7779                // afterwards is ONE pass: if every value in it is equal,
7780                // input order already IS the stable answer, and proving
7781                // that costs n-1 comparisons rather than n log n. Only a
7782                // run that is not all-equal gets sorted.
7783                //
7784                // Single-term only. With a second ORDER BY column an
7785                // all-equal first term does not settle the row order --
7786                // the later terms still speak -- and the shortcut would
7787                // drop them.
7788                let all_keys = if keep_sorted {
7789                    None
7790                } else {
7791                    sort_keys_of(&tagged, terms[0].0)
7792                };
7793                let low_card = !keep_sorted
7794                    && terms.len() == 1
7795                    && all_keys
7796                        .as_ref()
7797                        .is_some_and(|(keys, exact)| !*exact && !key_discriminates(keys));
7798                let keyed =
7799                    all_keys.filter(|(keys, exact)| *exact || key_discriminates(keys) || low_card);
7800                if keep_sorted {
7801                    // The collated permutation above already placed every
7802                    // row. A draft let the byte-order fallback run after
7803                    // it and undo the whole thing.
7804                } else if let Some((mut order, exact)) = keyed {
7805                    let (first_col, first_desc, _) = terms[0];
7806                    let row_cmp = |ia: u32, ib: u32| -> core::cmp::Ordering {
7807                        let (a, b) = (&tagged[ia as usize], &tagged[ib as usize]);
7808                        for (col, desc, nf) in &terms {
7809                            let (Some(va), Some(vb)) = (a.1.values.get(*col), b.1.values.get(*col))
7810                            else {
7811                                continue;
7812                            };
7813                            let ord = match (va, vb) {
7814                                (Value::Text(x), Value::Text(y)) if !mysql => {
7815                                    let c = crate::orderby::str_cmp_prefix_first(x, y);
7816                                    if *desc { c.reverse() } else { c }
7817                                }
7818                                _ => {
7819                                    crate::orderby::order_by_value_cmp_in(*desc, *nf, va, vb, mysql)
7820                                }
7821                            };
7822                            if ord != core::cmp::Ordering::Equal {
7823                                return ord;
7824                            }
7825                        }
7826                        core::cmp::Ordering::Equal
7827                    };
7828                    let _ = first_col;
7829                    if low_card {
7830                        // Integer sort first, then one pass per run.
7831                        order.sort_unstable_by(|&(pa, ia), &(pb, ib)| {
7832                            let c = pa.cmp(&pb);
7833                            let c = if first_desc { c.reverse() } else { c };
7834                            c.then_with(|| ia.cmp(&ib))
7835                        });
7836                        let mut lo = 0;
7837                        while lo < order.len() {
7838                            let mut hi = lo + 1;
7839                            while hi < order.len() && order[hi].0 == order[lo].0 {
7840                                hi += 1;
7841                            }
7842                            if hi - lo > 1 {
7843                                let head = tagged[order[lo].1 as usize].1.values.get(first_col);
7844                                let uniform = order[lo + 1..hi].iter().all(|&(_, i)| {
7845                                    tagged[i as usize].1.values.get(first_col) == head
7846                                });
7847                                if !uniform {
7848                                    order[lo..hi].sort_by(|&(_, ia), &(_, ib)| {
7849                                        row_cmp(ia, ib).then_with(|| ia.cmp(&ib))
7850                                    });
7851                                }
7852                                // A uniform run is already in index
7853                                // order, which IS the stable answer.
7854                            }
7855                            lo = hi;
7856                        }
7857                    } else {
7858                        order.sort_unstable_by(|&(pa, ia), &(pb, ib)| {
7859                            let c = pa.cmp(&pb);
7860                            let c = if first_desc { c.reverse() } else { c };
7861                            if c != core::cmp::Ordering::Equal {
7862                                return c;
7863                            }
7864                            // An EXACT key that ties means the values are
7865                            // equal, so only the remaining terms can speak.
7866                            // A prefix that ties has decided nothing yet and
7867                            // the first term must be asked again, which
7868                            // `row_cmp` does by walking every term from the
7869                            // start.
7870                            if exact && terms.len() == 1 {
7871                                return ia.cmp(&ib);
7872                            }
7873                            row_cmp(ia, ib).then_with(|| ia.cmp(&ib))
7874                        });
7875                    }
7876                    let mut slots: Vec<Option<(Vec<crate::orderby::OrderKey>, Row<'static>)>> =
7877                        core::mem::take(&mut tagged).into_iter().map(Some).collect();
7878                    tagged = order
7879                        .iter()
7880                        .map(|&(_, i)| {
7881                            slots[i as usize]
7882                                .take()
7883                                .expect("the permutation names each row once")
7884                        })
7885                        .collect();
7886                } else {
7887                    tagged.sort_by(|a, b| {
7888                        for (i, (col, desc, nf)) in terms.iter().enumerate() {
7889                            let va = a.1.values.get(*col);
7890                            let vb = b.1.values.get(*col);
7891                            let (Some(va), Some(vb)) = (va, vb) else {
7892                                continue;
7893                            };
7894                            let _ = i;
7895                            // v7.38.19 — two non-NULL strings, no MySQL fold, is
7896                            // where a text sort spends every one of its ~7 M
7897                            // comparisons, and the shared comparator cannot be
7898                            // inlined into this loop: it carries NULL placement,
7899                            // the fold, the NUMERIC bignum gate and the float
7900                            // total order. Answering that one pair here is the
7901                            // same answer by the same route — `value_cmp`'s
7902                            // leading same-variant arm is `x.cmp(y)`, and the
7903                            // raw comparator's last act is this reverse.
7904                            let ord = match (va, vb) {
7905                                (Value::Text(x), Value::Text(y)) if !mysql => {
7906                                    let c = crate::orderby::str_cmp_prefix_first(x, y);
7907                                    if *desc { c.reverse() } else { c }
7908                                }
7909                                _ => {
7910                                    crate::orderby::order_by_value_cmp_in(*desc, *nf, va, vb, mysql)
7911                                }
7912                            };
7913                            if ord != core::cmp::Ordering::Equal {
7914                                return ord;
7915                            }
7916                        }
7917                        core::cmp::Ordering::Equal
7918                    });
7919                }
7920            } else {
7921                crate::orderby::partial_sort_tagged_in(&mut tagged, keep, &descs, &order_colls);
7922            }
7923        }
7924
7925        // v7.17.0 Phase 3.P0-49 — `FETCH FIRST … WITH TIES` extends
7926        // past the truncated tail through every row that shares the
7927        // last-kept row's ORDER BY key. The tie check uses the
7928        // already-computed `(order_keys, row)` pairs so it matches
7929        // the sort comparator exactly. DISTINCT + WITH TIES falls
7930        // through to the no-ties path (PG also disallows their
7931        // combination; SPG silently drops the tie extension here so
7932        // the customer doesn't see a hard error mid-query — the
7933        // user-visible result is still correct, just narrower).
7934        let output_rows: Vec<Row<'static>> = if stmt.limit_with_ties && !stmt.distinct {
7935            apply_offset_and_limit_tagged(
7936                &mut tagged,
7937                stmt.offset_literal(),
7938                stmt.limit_literal(),
7939                true,
7940            );
7941            tagged.into_iter().map(|(_, r)| r).collect()
7942        } else {
7943            // DISTINCT already de-duped pre-sort above.
7944            let mut output_rows: Vec<Row<'static>> = tagged.into_iter().map(|(_, r)| r).collect();
7945            apply_offset_and_limit(
7946                &mut output_rows,
7947                stmt.offset_literal(),
7948                stmt.limit_literal(),
7949            );
7950            output_rows
7951        };
7952
7953        let columns: Vec<ColumnSchema> = projection
7954            .into_iter()
7955            .map(|p| p.to_column_schema())
7956            .collect();
7957
7958        Ok(QueryResult::Rows {
7959            columns,
7960            rows: output_rows,
7961        })
7962    }
7963
7964    /// v7.31 (perf — PG lesson #1): shared aggregate finisher. Apply
7965    /// OFFSET/LIMIT first, then evaluate the deferred subquery-bearing
7966    /// select items for the surviving rows only — PG's Result-above-
7967    /// Limit shape, where SubPlan loops equal the OUTPUT row count
7968    /// (50) instead of the group count (24k).
7969    fn finish_agg_result(
7970        &self,
7971        mut agg: aggregate::AggResult,
7972        stmt: &SelectStatement,
7973        cancel: CancelToken<'_>,
7974    ) -> Result<QueryResult, EngineError> {
7975        apply_offset_and_limit(&mut agg.rows, stmt.offset_literal(), stmt.limit_literal());
7976        if !agg.deferred.is_empty() {
7977            apply_offset_and_limit(
7978                &mut agg.synth_rows,
7979                stmt.offset_literal(),
7980                stmt.limit_literal(),
7981            );
7982            let ctx = EvalContext::new(&agg.synth_schema, None);
7983            let mut memo = memoize::MemoizeCache::default();
7984            // v7.32 (architecture v2 P3) — keyed index-probe seeding.
7985            // Deferred subqueries are referenced only by surviving
7986            // select-list rows (≤ LIMIT), so their correlation keys are
7987            // exactly the ≤LIMIT group keys in `synth_rows`. Pre-build
7988            // each batchable subquery's group map over just those keys
7989            // via per-key index seek; the per-row splice loop below then
7990            // reuses the seeded map. A join-shaped or un-indexed inner
7991            // falls through to the all-keys batch inside the call (built
7992            // eagerly here instead of lazily on row 0 — same cost), so
7993            // it still pays the full scan, never the 715 ms per-row
7994            // direct eval; its index-nested-loop probe is the next
7995            // knife. Genuinely non-batchable shapes return None and are
7996            // left unseeded for the loop's per-row resolver, as before.
7997            for (_, expr) in &agg.deferred {
7998                let mut subs: Vec<&SelectStatement> = Vec::new();
7999                collect_scalar_subqueries(expr, &mut subs);
8000                for sub in subs {
8001                    let repr = alloc::format!("{sub}");
8002                    if memo.group_maps.contains_key(&repr) {
8003                        continue;
8004                    }
8005                    if let Some(gm) = self.try_batch_correlated_scalar(
8006                        sub,
8007                        Some((&agg.synth_rows, &ctx)),
8008                        cancel,
8009                    )? {
8010                        memo.group_maps.insert(repr, Some(alloc::rc::Rc::new(gm)));
8011                    }
8012                }
8013            }
8014            for (ri, srow) in agg.synth_rows.iter().enumerate() {
8015                cancel.check()?;
8016                for (col, expr) in &agg.deferred {
8017                    let v =
8018                        self.eval_expr_with_correlated(expr, srow, &ctx, cancel, Some(&mut memo))?;
8019                    if let Some(cell) = agg.rows[ri].values.get_mut(*col) {
8020                        *cell = v;
8021                    }
8022                }
8023            }
8024        }
8025        Ok(QueryResult::Rows {
8026            columns: agg.columns,
8027            rows: agg.rows,
8028        })
8029    }
8030
8031    /// v7.37 — streaming projection for the joined-non-aggregate
8032    /// shape (multi-table FROM, all projection items bound, no
8033    /// ORDER BY / DISTINCT / GROUP BY / HAVING / LIMIT / OFFSET /
8034    /// UNION). Walks the deferred join survivors and emits
8035    /// `&[&Value]` borrowed straight out of the source tables — no
8036    /// `.cloned()`, no `Vec<Row<'static>>`. Skips the 25 k × 3-TEXT clone tax
8037    /// on the mailrs `PROJ` shape (about 4 ms saved).
8038    ///
8039    /// Returns `Ok(None)` when the shape doesn't qualify; the caller
8040    /// then falls back to the materialising path.
8041    /// v7.37 (round 831) — stream a joinless SELECT straight off the
8042    /// stored table, one row at a time, without ever building a row set.
8043    ///
8044    /// Returns `Ok(None)` for anything this cannot serve, and the caller
8045    /// falls through to the deferred-join path exactly as before: a
8046    /// missing table, or a cold tier whose hydration the fallback handles.
8047    /// Sort a single-table scan through the external sorter, so the
8048    /// answer's size is bounded by `work_mem` and not by the input.
8049    ///
8050    /// Sorting held every row twice — the scan's `Vec<Row>` and the
8051    /// sort's `Vec<(keys, Row)>` beside it — with nothing bounding
8052    /// either: 807 MB at 400k rows, whatever `work_mem` said. A large
8053    /// enough ORDER BY took the server down, which is a liveness
8054    /// problem before it is a performance one.
8055    ///
8056    /// A SEPARATE walk rather than a change to `run_single_table_scan`,
8057    /// following what round 831 did for the joinless shape. That
8058    /// function is 552 lines whose projection loop is entangled with
8059    /// DISTINCT (which indexes back into the tagged vector) and with
8060    /// streaming top-N (whose boundary moves as the scan runs); both
8061    /// assume the projection has already happened when a row is
8062    /// pushed, which is exactly what spilling has to defer. Two earlier
8063    /// attempts tried to rework that loop and were reverted. Here the
8064    /// existing path is untouched and this one only claims shapes it
8065    /// can serve, so a decline costs nothing.
8066    ///
8067    /// Records are SOURCE rows, not projected ones: `finish` re-derives
8068    /// keys from what it decodes, and an ORDER BY key need not be in
8069    /// the projection — `SELECT pad FROM big ORDER BY id` (round 835).
8070    fn try_spill_sorted_scan(
8071        &self,
8072        stmt: &SelectStatement,
8073        from: &FromClause,
8074        cancel: CancelToken<'_>,
8075    ) -> Result<Option<QueryResult>, EngineError> {
8076        // Shapes this walk does not serve. Each one either needs the
8077        // whole tagged vector addressable (DISTINCT probes back into
8078        // it, WITH TIES re-reads its tail) or is already bounded
8079        // without spilling (a LIMIT makes the partial sort O(keep)).
8080        if !self.can_spill()
8081            || stmt.order_by.is_empty()
8082            || stmt.distinct
8083            || stmt.limit_with_ties
8084            || stmt.limit_literal().is_some()
8085            || !from.joins.is_empty()
8086            || from.primary.lateral_subquery.is_some()
8087            || from.primary.unnest_expr.is_some()
8088            || from.primary.generate_series_args.is_some()
8089            || select_has_window(stmt)
8090        {
8091            return Ok(None);
8092        }
8093        // A parent's rows are its children's. These walks scan the named
8094        // relation alone, so a partitioned or inherited parent comes back
8095        // short — and silently: the corpus caught `SELECT id FROM pr
8096        // ORDER BY id` and `SELECT k FROM pl ORDER BY k` returning the
8097        // parent's own rows instead of the partitions'. `ONLY` is exactly
8098        // the case that does not fan out, so it stays, which is the test
8099        // the FROM-clause fan-out itself makes.
8100        if !from.primary.only
8101            && crate::partition::has_children(self.active_catalog(), &from.primary.name)
8102        {
8103            return Ok(None);
8104        }
8105        let Some(table) = self.active_catalog().get(&from.primary.name) else {
8106            return Ok(None);
8107        };
8108        // Cold-tier rows live outside `rows()`; this walk would drop
8109        // them silently, the same reason round 831's walk declines.
8110        if table.has_cold_rows_fast() {
8111            return Ok(None);
8112        }
8113
8114        let alias = from
8115            .primary
8116            .alias
8117            .as_deref()
8118            .unwrap_or(from.primary.name.as_str());
8119        let cols = table.schema().columns.clone();
8120        let sess = self.dml_session();
8121        let ctx = EvalContext::new(&cols, Some(alias))
8122            .with_catalog(self.active_catalog())
8123            .with_session(&sess);
8124        let projection = build_projection(
8125            &stmt.items,
8126            &cols,
8127            alias,
8128            self.speaks_mysql,
8129            Some(self.active_catalog()),
8130        )?;
8131        let order_by = stmt.order_by.clone();
8132        // The same one-shot resolution the general path does (round
8133        // 582): each ORDER BY column is bound once, not once per row.
8134        let order_bound = crate::orderby::order_by_bound_positions(&order_by, &cols, Some(alias));
8135        let descs: Vec<bool> = order_by.iter().map(|o| o.desc).collect();
8136        // Resolved BEFORE the scan, because it now decides what the sort
8137        // STORES and not just what it decodes (round 995).
8138        let needed = Self::sort_record_columns_needed(&stmt.items, &order_bound, cols.len(), &ctx);
8139
8140        // v7.38.22 — resolved HERE, because this path did not resolve
8141        // them at all.
8142        //
8143        // Every published SPG through 7.38.21 answered `ORDER BY s COLLATE
8144        // "en_US.utf8"` in BYTE order on this path — and swallowed an
8145        // unknown collation name rather than raising — because the sorter
8146        // below compared with an empty collation slice. The materialising
8147        // path honoured both. Which answer a query got depended on which
8148        // path the planner took, and this is the path a plain single-table
8149        // SELECT takes.
8150        let order_colls = crate::orderby::order_by_collations(&order_by, &ctx)?;
8151        // v7.39.12 — a correlated scalar subquery in ORDER BY is
8152        // resolved for the row before its key is built.
8153        //
8154        // Uncorrelated subqueries are replaced by a literal before
8155        // execution; a correlated one cannot be, so it reached the
8156        // per-row evaluator — the one place that cannot run a subquery
8157        // — and the statement raised "subquery reached row eval".
8158        // Reported by sentori against 7.39.11; see
8159        // `Engine::order_by_resolved_for_row`.
8160        //
8161        // The `any` runs once, here, so an ordinary ORDER BY pays one
8162        // bool per row and nothing else.
8163        let order_has_subquery = order_by
8164            .iter()
8165            .any(|o| crate::subquery::expr_has_subquery(&o.expr));
8166        let unbound: Vec<Option<usize>> = alloc::vec![None; order_by.len()];
8167        let mut sorter = crate::extsort::ExternalSorter::new(
8168            self.temp_run_factory,
8169            self.session_work_mem_bytes(),
8170            cols.clone(),
8171            &descs,
8172            &order_colls,
8173        )
8174        .with_stats(&self.spill_stats)
8175        .with_pruned(&needed);
8176        let snapshot = self.current_snapshot();
8177        // One key buffer for the whole scan: `push` drains it and leaves
8178        // the capacity behind.
8179        let mut keys: Vec<OrderKey> = Vec::new();
8180        // r1024 — compile the predicate once for the scan.
8181        //
8182        // These two sorted-spill scans are the paths a single-table SELECT
8183        // with an ORDER BY takes, and they were the last row-returning ones
8184        // still walking the expression tree per row. r1023 did the
8185        // no-ORDER-BY sibling; the sweep's two remaining losing cells are
8186        // exactly this shape.
8187        //
8188        // Found from the profile's CALL TREE rather than its leaves. The
8189        // leaves say what is expensive — `eval_expr` 320, `apply_binary`
8190        // 261, `mod_op` 178 — and two attempts at reasoning out which
8191        // function asked for it were both wrong. The tree names the caller
8192        // chain, and it named this one.
8193        let compiled_where: Option<crate::eval::CompiledExpr> = stmt
8194            .where_
8195            .as_ref()
8196            .filter(|w| crate::eval::fully_compilable(w))
8197            .map(|w| crate::eval::compile_expr(w, &ctx));
8198        let mut eval_stack: Vec<Value<'static>> = Vec::new();
8199        for (i, row) in table.scan_visible_from(0, &snapshot) {
8200            if i.is_multiple_of(256) {
8201                cancel.check()?;
8202            }
8203            if let Some(c) = &compiled_where {
8204                if !crate::eval::compiled::eval_compiled_pred(
8205                    c,
8206                    row,
8207                    &ctx,
8208                    &mut eval_stack,
8209                    ctx.mysql_dialect,
8210                )? {
8211                    continue;
8212                }
8213            } else if let Some(w) = &stmt.where_ {
8214                let cond = crate::eval::eval_expr(w, row, &ctx).map_err(EngineError::Eval)?;
8215                if !crate::eval::predicate_is_true(&cond, "WHERE", ctx.mysql_dialect)? {
8216                    continue;
8217                }
8218            }
8219            keys.clear();
8220            // The same collations the sorter compares with, and the
8221            // re-derivation below is handed the same ones. `finish`'s
8222            // contract is that a key comes back the way it was pushed;
8223            // a collation is part of the way it was pushed.
8224            if order_has_subquery {
8225                // A substituted literal is no longer a bound column.
8226                let per_row = self.order_by_resolved_for_row(&order_by, row, &ctx, cancel)?;
8227                crate::orderby::build_order_keys_bound(
8228                    per_row.as_deref().unwrap_or(&order_by),
8229                    &unbound,
8230                    &order_colls,
8231                    row,
8232                    &ctx,
8233                    &mut keys,
8234                )?;
8235            } else {
8236                crate::orderby::build_order_keys_bound(
8237                    &order_by,
8238                    &order_bound,
8239                    &order_colls,
8240                    row,
8241                    &ctx,
8242                    &mut keys,
8243                )?;
8244            }
8245            sorter.push(&mut keys, row)?;
8246        }
8247
8248        let key_ctx = &ctx;
8249        let rows = sorter.finish(
8250            |src, buf| {
8251                crate::orderby::build_order_keys_rederived(
8252                    &order_by,
8253                    &order_bound,
8254                    &order_colls,
8255                    src,
8256                    key_ctx,
8257                    buf,
8258                )
8259            },
8260            |src| {
8261                let mut values = Vec::with_capacity(projection.len());
8262                for p in &projection {
8263                    values.push(
8264                        crate::eval::eval_expr(&p.expr, src, key_ctx).map_err(EngineError::Eval)?,
8265                    );
8266                }
8267                Ok(Row::new(values))
8268            },
8269        )?;
8270
8271        let columns: Vec<ColumnSchema> = projection.iter().map(|p| p.to_column_schema()).collect();
8272        Ok(Some(QueryResult::Rows { columns, rows }))
8273    }
8274
8275    /// v7.37 (round 882) — the bounded sort of `try_spill_sorted_scan`,
8276    /// handing each row to the consumer instead of collecting the answer.
8277    ///
8278    /// That walk bounds the SORT and then returns `QueryResult::Rows`,
8279    /// which holds every output row. Measured at `work_mem = 4 MB` over
8280    /// 200-byte rows, RSS above the server's own baseline while the
8281    /// query runs grew +30 MB at 100k rows, +68 MB at 200k and +137 MB
8282    /// at 400k — linear — while the spill underneath worked correctly
8283    /// (9 / 17 / 33 runs, witnessed DURING the query; `FileRun::drop`
8284    /// removes each file, so a count taken afterwards reads 0 whatever
8285    /// happened, and an earlier reading of "no spill at all" was that
8286    /// blind witness). The growth is the collected result, not the sort.
8287    ///
8288    /// Emitting makes peak the budget, one buffer per run and a single
8289    /// row — the state a merge already holds at every step. It also
8290    /// frees each projected row as the next is built rather than
8291    /// accumulating them, which is where the time is: a profile of the
8292    /// collecting walk put the allocator at 586 samples, more than every
8293    /// sort comparison combined (420), against 19 for `push` itself.
8294    /// v7.37 (round 923) — which of a sort record's columns the output half
8295    /// reads. The record is the SOURCE row (round 836), so a narrow projection
8296    /// decoded every column: skipping one 200-byte text halves a decode
8297    /// (2.17 -> 1.14 ms per pass at 10k rows, priced additively).
8298    ///
8299    /// Timid on purpose — a wrong mask is a SILENT wrong answer, a pruned
8300    /// column reads NULL. Answers only when every projection item is a bare
8301    /// column reference AND every ORDER BY key is a bound column; anything
8302    /// else returns empty, decoding everything as before.
8303    /// `explain.rs`'s `collect_column_refs` is NOT used: its `_ => {}` arm
8304    /// drops references from expression kinds it does not enumerate.
8305    ///
8306    /// ORDER BY columns are included — the merge re-derives keys from the
8307    /// decoded row on the spilled path, so pruning one would sort NULLs.
8308    pub(crate) fn sort_record_columns_needed(
8309        items: &[SelectItem],
8310        order_bound: &[Option<usize>],
8311        arity: usize,
8312        ctx: &EvalContext,
8313    ) -> Vec<bool> {
8314        let all_bare = items.iter().all(|i| {
8315            matches!(
8316                i,
8317                SelectItem::Expr {
8318                    expr: Expr::Column(_),
8319                    ..
8320                }
8321            )
8322        });
8323        if !all_bare || order_bound.iter().any(Option::is_none) {
8324            return Vec::new();
8325        }
8326        let mut mask = alloc::vec![false; arity];
8327        for item in items {
8328            if let SelectItem::Expr {
8329                expr: Expr::Column(c),
8330                ..
8331            } = item
8332            {
8333                match crate::eval::find_column_pos(c, ctx) {
8334                    Some(p) if p < arity => mask[p] = true,
8335                    _ => return Vec::new(),
8336                }
8337            }
8338        }
8339        for p in order_bound.iter().flatten() {
8340            if *p < arity {
8341                mask[*p] = true;
8342            } else {
8343                return Vec::new();
8344            }
8345        }
8346        mask
8347    }
8348
8349    /// r1025 — `ORDER BY <indexed NOT NULL column>` walks the index instead
8350    /// of sorting.
8351    ///
8352    /// PG serves such an ordering from the index and never sorts. We sorted:
8353    /// measured at 400,000 rows, `SELECT pad FROM t ORDER BY id` costs
8354    /// 138-144 ms against PG18's 64-75, and the call tree puts the cost in
8355    /// the sorter's own round trip — `ExternalSorter::finish_each` →
8356    /// `next_row` → `decode_row_body_dense_pruned` → `read_value_body`.
8357    /// Every row is encoded into the sorter's arena and decoded back out,
8358    /// for an order the index already holds.
8359    ///
8360    /// The walk exists — `try_pk_walk_top_n` — and requires a `LIMIT`,
8361    /// because it was built for top-N. This is the unbounded sibling.
8362    ///
8363    /// NOT NULL is a hard gate, not a simplification: a NULL key is absent
8364    /// from a btree, so walking one would silently drop those rows. That is
8365    /// exactly the defect r1020 fixed on the top-N path, where it had
8366    /// shipped.
8367    /// r1044 — the index this statement's ORDER BY can be WALKED on,
8368    /// instead of sorted, or `None`.
8369    ///
8370    /// Extracted so `EXPLAIN` can ask the same question the executor
8371    /// answers. It could not, and said so: `SELECT pad FROM t ORDER BY
8372    /// id` on a 400,000-row table planned as `Sort` over `Seq Scan`
8373    /// while the executor walked the primary key — 34.9 ms against
8374    /// 147.0 for the same query ordered by an unindexed column, so the
8375    /// walk was plainly running. Round 551 fixed a different case of
8376    /// this and wrote the reason down: EXPLAIN is the first thing any
8377    /// performance question opens, and an instrument that misnames the
8378    /// access path is worse than one that says nothing.
8379    ///
8380    /// The gate is here once. Two copies of it is how the plan and the
8381    /// executor come to disagree again.
8382    /// v7.39.13 — the shape refusals both ordered-walk gates make.
8383    ///
8384    /// One list, because two of them would be two answers to "can this
8385    /// statement walk an index", and a walk that runs where EXPLAIN says
8386    /// it does not is the defect r1044 exists to prevent.
8387    /// v7.39.13 — `WHERE lead = <literal> ORDER BY next [DESC] LIMIT n`
8388    /// behind an index on `(lead, next, …)`: one seek to the key prefix,
8389    /// then n steps inside it.
8390    ///
8391    /// Sentori's busiest read, and the one shape they have reported
8392    /// unchanged for three versions: `WHERE project_id = ? ORDER BY
8393    /// received_at DESC LIMIT 20`. PostgreSQL 18 answers it with
8394    /// `Limit -> Index Scan`; SPG planned `Sort -> Seq Scan` and sorted
8395    /// the table to return twenty rows, roughly 250x behind.
8396    ///
8397    /// The ordered walk that existed could only start at an index's
8398    /// LEADING column, so an index on `(project_id, received_at)` could
8399    /// serve `ORDER BY project_id` and nothing else. What was missing is
8400    /// below it: a tree walk bounded by a key prefix, which
8401    /// `Index::iter_prefix_desc` now provides.
8402    ///
8403    /// The equality conjunct only NARROWS the walk — the statement's own
8404    /// `WHERE` still runs per row — so picking the wrong conjunct can
8405    /// cost time and cannot change an answer.
8406    pub(crate) fn index_prefix_walk_target(
8407        &self,
8408        stmt: &SelectStatement,
8409        from: &FromClause,
8410    ) -> Option<(String, usize, alloc::vec::Vec<spg_storage::IndexKey>)> {
8411        if self.walk_shape_refused(stmt, from) {
8412            return None;
8413        }
8414        // One ORDER BY term for now: a second one would have to be the
8415        // next key column again, and the tree walks one direction.
8416        if stmt.order_by.len() != 1 || stmt.distinct {
8417            return None;
8418        }
8419        let table = self.active_catalog().get(&from.primary.name)?;
8420        let alias = from
8421            .primary
8422            .alias
8423            .as_deref()
8424            .unwrap_or(from.primary.name.as_str());
8425        let cols = &table.schema().columns;
8426        let order = &stmt.order_by[0];
8427        let Expr::Column(oc) = &order.expr else {
8428            return None;
8429        };
8430        if let Some(q) = &oc.qualifier
8431            && !q.eq_ignore_ascii_case(alias)
8432        {
8433            return None;
8434        }
8435        let order_pos = cols
8436            .iter()
8437            .position(|c| c.name.eq_ignore_ascii_case(&oc.name))?;
8438        // The walk comes out in the tree's order, so it may only take an
8439        // ORDER BY whose order that IS — the same question the leading-
8440        // column gate asks, for the same reason.
8441        let order_col = cols.get(order_pos)?;
8442        if crate::index_access::collated_column(order_col, table.db_collation()).is_none()
8443            && !crate::collate::column_key_is_bytewise(order_col, self.speaks_mysql)
8444        {
8445            return None;
8446        }
8447        // A NULL key is not in the tree, and this walk has no separate
8448        // pass for those rows the way the leading-column one does.
8449        if order_col.nullable {
8450            return None;
8451        }
8452        let where_ = stmt.where_.as_ref()?;
8453        for index in table.indices() {
8454            if !matches!(index.kind, spg_storage::IndexKind::BTreeMulti(_))
8455                || index.expression.is_some()
8456                || index.partial_predicate.is_some()
8457            {
8458                continue;
8459            }
8460            // The ORDER BY column must be the key component that follows
8461            // the equality-bound prefix.
8462            if index.extra_column_positions.first() != Some(&order_pos) {
8463                continue;
8464            }
8465            let lead_pos = index.column_position;
8466            let lead_col = cols.get(lead_pos)?;
8467            // The prefix is compared with the tree's own ordering, so the
8468            // leading column has to be one the tree orders bytewise too.
8469            if crate::index_access::collated_column(lead_col, table.db_collation()).is_none()
8470                && !crate::collate::column_key_is_bytewise(lead_col, self.speaks_mysql)
8471            {
8472                continue;
8473            }
8474            let Some(key) = self.eq_literal_key_for(where_, lead_pos, cols, alias) else {
8475                continue;
8476            };
8477            return Some((index.name.clone(), order_pos, alloc::vec![key]));
8478        }
8479        None
8480    }
8481
8482    /// The index key a top-level `AND` conjunct binds `col_pos` to, when
8483    /// one of them is `col = <literal>` (either way round).
8484    ///
8485    /// Only literals: a column reference or a function would have to be
8486    /// evaluated per row, and this runs once for the whole statement.
8487    fn eq_literal_key_for(
8488        &self,
8489        where_: &Expr,
8490        col_pos: usize,
8491        cols: &[ColumnSchema],
8492        alias: &str,
8493    ) -> Option<spg_storage::IndexKey> {
8494        let col = cols.get(col_pos)?;
8495        let mut found: Option<spg_storage::IndexKey> = None;
8496        let mut stack: alloc::vec::Vec<&Expr> = alloc::vec![where_];
8497        while let Some(e) = stack.pop() {
8498            match e {
8499                Expr::Binary {
8500                    lhs,
8501                    op: spg_sql::ast::BinOp::And,
8502                    rhs,
8503                } => {
8504                    stack.push(lhs);
8505                    stack.push(rhs);
8506                }
8507                Expr::Binary {
8508                    lhs,
8509                    op: spg_sql::ast::BinOp::Eq,
8510                    rhs,
8511                } => {
8512                    let names_col = |x: &Expr| match x {
8513                        Expr::Column(c) => {
8514                            c.name.eq_ignore_ascii_case(&col.name)
8515                                && c.qualifier
8516                                    .as_ref()
8517                                    .is_none_or(|q| q.eq_ignore_ascii_case(alias))
8518                        }
8519                        _ => false,
8520                    };
8521                    let lit = if names_col(lhs) {
8522                        Some(&**rhs)
8523                    } else if names_col(rhs) {
8524                        Some(&**lhs)
8525                    } else {
8526                        None
8527                    };
8528                    // v7.39.13 — a BARE literal means whatever the
8529                    // COLUMN says it means, and
8530                    // `literal_as_column_value` is the one place that
8531                    // decision is made. Asking
8532                    // `literal_expr_to_value` instead made this the
8533                    // fifth copy of it, and it read every string
8534                    // literal as text: `WHERE k = '\x07'` on a `bytea`
8535                    // column built no key at all, so the walk declined
8536                    // and the plan went back to sorting the table —
8537                    // while the EQUALITY seek beside it, which does ask
8538                    // the one funnel, used the very same index.
8539                    //
8540                    // Anything that is not a bare literal — a cast, a
8541                    // negation — already carries its own type, and
8542                    // `from_value_for_column` decides whether that type
8543                    // keys for this column.
8544                    let v = match lit {
8545                        Some(Expr::Literal(l)) => {
8546                            crate::index_access::literal_as_column_value(l, col, col_pos)
8547                        }
8548                        Some(other) => {
8549                            crate::conversions::literal_expr_to_value(other.clone()).ok()
8550                        }
8551                        None => None,
8552                    };
8553                    if let Some(v) = v
8554                        && !v.is_null()
8555                        && let Some(k) = spg_storage::IndexKey::from_value_for_column(&v, col.ty)
8556                    {
8557                        found = Some(k);
8558                    }
8559                }
8560                _ => {}
8561            }
8562        }
8563        found
8564    }
8565
8566    fn walk_shape_refused(&self, stmt: &SelectStatement, from: &FromClause) -> bool {
8567        // A non-literal count is refused: `LIMIT $1` is rewritten to a
8568        // literal by `resolve_limit_exprs` before dispatch, so anything
8569        // still carrying a placeholder here has not been through it.
8570        let literal_count = |e: &Option<spg_sql::ast::LimitExpr>| {
8571            matches!(e, None | Some(spg_sql::ast::LimitExpr::Literal(_)))
8572        };
8573        if stmt.order_by.is_empty()
8574            || !stmt.distinct_on.is_empty()
8575            || stmt.limit_with_ties
8576            || !literal_count(&stmt.limit)
8577            || !literal_count(&stmt.offset)
8578            || stmt.having.is_some()
8579            || stmt.group_by.is_some()
8580            || !stmt.unions.is_empty()
8581            || !from.joins.is_empty()
8582            || from.primary.lateral_subquery.is_some()
8583            || from.primary.unnest_expr.is_some()
8584            || from.primary.as_of_segment.is_some()
8585            || from.primary.generate_series_args.is_some()
8586            || select_has_window(stmt)
8587            || aggregate::uses_aggregate_in(stmt, self.speaks_mysql)
8588        {
8589            return true;
8590        }
8591        if stmt
8592            .items
8593            .iter()
8594            .any(|i| matches!(i, SelectItem::Expr { expr, .. } if is_top_level_unnest(expr)))
8595        {
8596            return true;
8597        }
8598        let Some(table) = self.active_catalog().get(&from.primary.name) else {
8599            return true;
8600        };
8601        if table.has_cold_rows_fast() {
8602            return true;
8603        }
8604        !from.primary.only
8605            && crate::partition::has_children(self.active_catalog(), &from.primary.name)
8606    }
8607
8608    pub(crate) fn index_order_walk_target(
8609        &self,
8610        stmt: &SelectStatement,
8611        from: &FromClause,
8612    ) -> Option<(String, usize)> {
8613        // v7.39.11 — LIMIT / OFFSET join the walk instead of refusing it.
8614        //
8615        // Reported by sentori against 7.39.10 and measured on their own
8616        // busiest read: "the most recent N events for this project",
8617        // backed by an index on exactly that ordering. PostgreSQL 18
8618        // answered it with `Limit -> Index Scan`; SPG with
8619        // `Limit -> Sort -> Seq Scan`, 4.998 ms against 0.021 — the
8620        // whole table sorted to return twenty rows.
8621        //
8622        // The walk was built for this shape — `iter_desc`'s own doc says
8623        // "the ORDER BY <indexed col> DESC + LIMIT N executor path" —
8624        // and then the gate refused every statement that had a LIMIT, so
8625        // the one query it was written for could never reach it. The
8626        // capability was here; the routing was not.
8627        //
8628        // A non-literal count is refused: `LIMIT $1` is rewritten to a
8629        // literal by `resolve_limit_exprs` before dispatch, so anything
8630        // still carrying a placeholder here has not been through it.
8631        let literal_count = |e: &Option<spg_sql::ast::LimitExpr>| {
8632            matches!(e, None | Some(spg_sql::ast::LimitExpr::Literal(_)))
8633        };
8634        if self.walk_shape_refused(stmt, from) {
8635            return None;
8636        }
8637        let table = self.active_catalog().get(&from.primary.name)?;
8638        let alias = from
8639            .primary
8640            .alias
8641            .as_deref()
8642            .unwrap_or(from.primary.name.as_str());
8643        let cols = &table.schema().columns;
8644        let order = &stmt.order_by[0];
8645        let Expr::Column(oc) = &order.expr else {
8646            return None;
8647        };
8648        if let Some(q) = &oc.qualifier
8649            && !q.eq_ignore_ascii_case(alias)
8650        {
8651            return None;
8652        }
8653        let order_pos = cols
8654            .iter()
8655            .position(|c| c.name.eq_ignore_ascii_case(&oc.name))?;
8656        // r1047 — DISTINCT joins the walk when the projection IS the
8657        // order column, and only then. The index's keys are canonical
8658        // (r1039: representation equality is value equality — the
8659        // property every seek already depends on), so one key is one
8660        // distinct value and the walk can emit the first passing row of
8661        // each key group instead of hashing every row. On the release
8662        // sweep's `SELECT DISTINCT n FROM t ORDER BY n` — 400,000 rows,
8663        // 1,000 distinct values — the hash path priced at 21.3-22.7 ms
8664        // with an ablation floor of 14.8, because the hash must
8665        // normalize and probe ALL the rows; the walk visits each key
8666        // once. A wider projection makes DISTINCT about the whole tuple,
8667        // not the key, so anything else still declines.
8668        if stmt.distinct {
8669            let only_the_order_column = stmt.items.len() == 1
8670                && match &stmt.items[0] {
8671                    SelectItem::Expr {
8672                        expr: Expr::Column(c),
8673                        ..
8674                    } => {
8675                        c.name.eq_ignore_ascii_case(&oc.name)
8676                            && match &c.qualifier {
8677                                Some(q) => q.eq_ignore_ascii_case(alias),
8678                                None => true,
8679                            }
8680                    }
8681                    _ => false,
8682                };
8683            if !only_the_order_column {
8684                return None;
8685            }
8686        }
8687        // r1046 — a nullable key no longer refuses the walk; it changes
8688        // what the walk has to do. A NULL key is not in the btree, so
8689        // walking alone would silently drop those rows — the r1020
8690        // defect, which shipped once. The walk emits them separately, at
8691        // the end SQL puts them.
8692        //
8693        // Refusing was costing every nullable indexed column a 3.4x:
8694        // `SELECT id FROM t ORDER BY b` over 400,000 rows measured
8695        // 72.0 ms with the column nullable and 20.2 with the same data
8696        // under NOT NULL. `NOT NULL` is not the default, so that was the
8697        // common case paying for the uncommon one.
8698        // v7.39.11 — the walk comes out in the tree's order, so it may
8699        // only take an ORDER BY whose order that IS.
8700        //
8701        // The B-tree walks in BYTE order unless the column's keys are
8702        // ICU sort keys. `try_pk_walk_top_n` has asked this since
8703        // v7.38.18; this gate never did, and the answer changed when an
8704        // index appeared. Measured on `alpha / Beta / GAMMA / delta`
8705        // over a MySQL-dialect session, `SELECT t FROM s ORDER BY t`:
8706        //
8707        //   no index   alpha Beta delta GAMMA   (MySQL's own order)
8708        //   indexed    Beta GAMMA alpha delta   (bytes)
8709        //
8710        // No row is wrong and nothing raises; only the order changes,
8711        // and it changes because an index exists. Ordering is the one
8712        // thing a walk contributes, so when it is the wrong ordering
8713        // there is nothing left to keep.
8714        let order_col = cols.get(order_pos)?;
8715        if crate::index_access::collated_column(order_col, table.db_collation()).is_none()
8716            && !crate::collate::column_key_is_bytewise(order_col, self.speaks_mysql)
8717        {
8718            return None;
8719        }
8720        // v7.39.11 — a composite B-tree LEADING on the ORDER BY column
8721        // walks it too, which is what `try_pk_walk_top_n` has always
8722        // done and what this gate did not know.
8723        //
8724        // Keys sort by the whole tuple, so the leading component comes
8725        // out in order — `Index::iter_asc` says so, and the materialising
8726        // top-N walk has relied on it since v7.38.1. The consequence of
8727        // the two gates disagreeing was the thing r1044 exists to
8728        // prevent: measured on a table indexed `(a, b)`, `SELECT a FROM
8729        // m ORDER BY a LIMIT 2` planned as `Limit -> Sort -> Seq Scan`
8730        // while the executor plainly walked the index — a projection
8731        // that divides by zero on the last row in key order returned two
8732        // rows instead of raising. EXPLAIN is the first thing any
8733        // performance question opens, and an instrument that misnames
8734        // the access path is worse than one that says nothing.
8735        let index = table
8736            .index_on(order_pos)
8737            .filter(|i| matches!(i.kind, spg_storage::IndexKind::BTree(_)))
8738            .or_else(|| {
8739                table.indices().iter().find(|i| {
8740                    matches!(i.kind, spg_storage::IndexKind::BTreeMulti(_))
8741                        && i.column_position == order_pos
8742                })
8743            })?;
8744        if index.expression.is_some() || index.partial_predicate.is_some() {
8745            return None;
8746        }
8747        // v7.39.11 — more than one ORDER BY term walks when the index
8748        // holds exactly that ordering.
8749        //
8750        // Keys sort by the whole tuple, so `iter_asc` over a composite
8751        // B-tree IS `ORDER BY a, b` — the walk needs no new machinery,
8752        // only permission. Reported by sentori against 7.39.10:
8753        // `ORDER BY a, b LIMIT 10` planned as `Seq Scan -> Sort` here
8754        // against an `Incremental Sort` over an index scan on
8755        // PostgreSQL 18, on a table indexed for it.
8756        //
8757        // Three things have to hold, and each of them is the tree's
8758        // limitation rather than a conservative choice:
8759        //
8760        //   * the terms are the index's key columns, in its order, from
8761        //     the leading one — a suffix or a permutation is a different
8762        //     ordering;
8763        //   * every term runs the same direction, because the tree is
8764        //     walked one way for all of them. `(a, b DESC)` is what
8765        //     PostgreSQL serves from an index whose SECOND key is
8766        //     descending, and SPG's tree does not scan per column;
8767        //   * every key column is NOT NULL. A NULL key is not in the
8768        //     tree at all, and the separate pass that emits those rows
8769        //     (r1046) knows how to place them for ONE column, not for a
8770        //     tuple.
8771        if stmt.order_by.len() > 1 {
8772            let keys: Vec<usize> = core::iter::once(index.column_position)
8773                .chain(index.extra_column_positions.iter().copied())
8774                .collect();
8775            if stmt.order_by.len() > keys.len() {
8776                return None;
8777            }
8778            let desc = stmt.order_by[0].desc;
8779            for (term, &key_pos) in stmt.order_by.iter().zip(keys.iter()) {
8780                if term.desc != desc {
8781                    return None;
8782                }
8783                let Expr::Column(c) = &term.expr else {
8784                    return None;
8785                };
8786                if let Some(q) = &c.qualifier
8787                    && !q.eq_ignore_ascii_case(alias)
8788                {
8789                    return None;
8790                }
8791                let pos = cols
8792                    .iter()
8793                    .position(|s| s.name.eq_ignore_ascii_case(&c.name))?;
8794                if pos != key_pos {
8795                    return None;
8796                }
8797                let col = cols.get(pos)?;
8798                if col.nullable {
8799                    return None;
8800                }
8801                if crate::index_access::collated_column(col, table.db_collation()).is_none()
8802                    && !crate::collate::column_key_is_bytewise(col, self.speaks_mysql)
8803                {
8804                    return None;
8805                }
8806            }
8807        }
8808        Some((index.name.clone(), order_pos))
8809    }
8810
8811    fn try_index_order_stream<F>(
8812        &self,
8813        stmt: &SelectStatement,
8814        from: &FromClause,
8815        cancel: CancelToken<'_>,
8816        emit: &mut F,
8817    ) -> Result<Option<usize>, EngineError>
8818    where
8819        F: FnMut(crate::StreamItem<'_>) -> Result<(), EngineError>,
8820    {
8821        // r1044 — the shape gate lives in `index_order_walk_target`, so
8822        // `EXPLAIN` answers the same question. What stays here is the
8823        // part that RAISES (an illegal ORDER BY has to keep erroring
8824        // from where it did) and the bindings the walk needs.
8825        crate::orderby::check_order_by_legality(stmt)?;
8826        crate::orderby::check_order_by_positions(stmt)?;
8827        crate::window::reject_window_in_row_clauses(stmt)?;
8828        // v7.39.13 — the prefix walk first: it serves a shape the
8829        // leading-column walk cannot, and refuses everything that one
8830        // takes.
8831        let (order_pos, prefix) = match self.index_prefix_walk_target(stmt, from) {
8832            Some((_, pos, keys)) => (pos, Some(keys)),
8833            None => match self.index_order_walk_target(stmt, from) {
8834                Some((_, pos)) => (pos, None),
8835                None => return Ok(None),
8836            },
8837        };
8838        let Some(table) = self.active_catalog().get(&from.primary.name) else {
8839            return Ok(None);
8840        };
8841        let alias = from
8842            .primary
8843            .alias
8844            .as_deref()
8845            .unwrap_or(from.primary.name.as_str());
8846        let cols = table.schema().columns.clone();
8847        let order = &stmt.order_by[0];
8848        // v7.39.11 — the same lookup the gate made; see
8849        // `index_order_walk_target`.
8850        let Some(index) = (if prefix.is_some() {
8851            // The prefix planner named an index whose FIRST extra key
8852            // column is the order column; the lookup below looks for one
8853            // whose LEADING column is, and would find the wrong tree.
8854            table.indices().iter().find(|i| {
8855                matches!(i.kind, spg_storage::IndexKind::BTreeMulti(_))
8856                    && i.extra_column_positions.first() == Some(&order_pos)
8857                    && i.expression.is_none()
8858                    && i.partial_predicate.is_none()
8859            })
8860        } else {
8861            table
8862                .index_on(order_pos)
8863                .filter(|i| matches!(i.kind, spg_storage::IndexKind::BTree(_)))
8864                .or_else(|| {
8865                    table.indices().iter().find(|i| {
8866                        matches!(i.kind, spg_storage::IndexKind::BTreeMulti(_))
8867                            && i.column_position == order_pos
8868                    })
8869                })
8870        }) else {
8871            return Ok(None);
8872        };
8873
8874        let sess = self.dml_session();
8875        let ctx = EvalContext::new(&cols, Some(alias))
8876            .with_catalog(self.active_catalog())
8877            .with_session(&sess);
8878        let projection = build_projection(
8879            &stmt.items,
8880            &cols,
8881            alias,
8882            self.speaks_mysql,
8883            Some(self.active_catalog()),
8884        )?;
8885        let columns: Vec<ColumnSchema> = projection.iter().map(|p| p.to_column_schema()).collect();
8886        emit(crate::StreamItem::Header(&columns))?;
8887        let bound_pos: Vec<Option<usize>> = projection
8888            .iter()
8889            .map(|p| match &p.expr {
8890                Expr::Column(c) => match crate::eval::locate_column(c, &ctx) {
8891                    Ok(Some(pos)) => Some(pos),
8892                    _ => None,
8893                },
8894                _ => None,
8895            })
8896            .collect();
8897
8898        let compiled_where: Option<crate::eval::CompiledExpr> = stmt
8899            .where_
8900            .as_ref()
8901            .filter(|w| crate::eval::fully_compilable(w))
8902            .map(|w| crate::eval::compile_expr(w, &ctx));
8903        let mut eval_stack: Vec<Value<'static>> = Vec::new();
8904        let mut values: Vec<Value<'static>> = Vec::with_capacity(projection.len());
8905        let snapshot = self.current_snapshot();
8906
8907        // A btree holds one locator per row VERSION, so a row whose key was
8908        // updated can sit under two keys and a dead one can sit beside its
8909        // replacement. The visibility gate drops the dead; `seen` drops a
8910        // live row that the walk reaches twice, which would otherwise be a
8911        // duplicated output row rather than a slow one.
8912        let mut emitted_rows = alloc::vec![false; table.rows().len()];
8913
8914        // r1046 — the rows the index cannot hold.
8915        //
8916        // A NULL key is not in the btree, so the walk below never reaches
8917        // those rows; they are emitted here, at the end SQL puts them.
8918        // PG's default is NULLS LAST ascending and NULLS FIRST
8919        // descending, and an explicit `NULLS FIRST` / `NULLS LAST` wins —
8920        // the same rule `order_by_value_cmp_raw` applies to the sort this
8921        // replaces, so the two orders agree.
8922        //
8923        // Finding them costs one pass over the column. That pass is why
8924        // this is still worth doing: the sort it replaces encodes and
8925        // decodes every row, and the walk plus the pass measured 72.0 ms
8926        // down to about 22 on 400,000 rows.
8927        let nulls_first = order.nulls_first.unwrap_or(order.desc);
8928        // r1047 — under DISTINCT the walk emits the FIRST passing row of
8929        // each key group and skips the rest; the gate admits DISTINCT
8930        // only when the projection is the order column itself, so one
8931        // canonical key is one output row. NULL is one distinct value,
8932        // so the NULL pass stops at its first emit too.
8933        let distinct = stmt.distinct;
8934        let mut count = 0usize;
8935        let mut visited = 0usize;
8936        // v7.39.11 — OFFSET and LIMIT, applied as the walk goes.
8937        //
8938        // Both count PASSING rows, so a skipped row still has to run the
8939        // predicate and the projection — `stream_filter_project` is
8940        // `stream_project_row` without the emit, which is exactly that.
8941        // Stopping at `remaining == 0` is the whole point: twenty rows
8942        // off the end of an index instead of a sorted table.
8943        let mut to_skip = stmt.offset_literal().unwrap_or(0) as usize;
8944        let mut remaining: Option<usize> = stmt.limit_literal().map(|l| l as usize);
8945        let mut emit_null_rows = |emitted_rows: &mut alloc::vec::Vec<bool>,
8946                                  eval_stack: &mut Vec<Value<'static>>,
8947                                  values: &mut Vec<Value<'static>>,
8948                                  visited: &mut usize,
8949                                  to_skip: &mut usize,
8950                                  remaining: &mut Option<usize>,
8951                                  emit: &mut F|
8952         -> Result<usize, EngineError> {
8953            if !cols[order_pos].nullable {
8954                return Ok(0);
8955            }
8956            // v7.39.11 — nothing to emit once the LIMIT is met, and
8957            // finding that out must not cost a scan.
8958            //
8959            // This pass looks for NULL-keyed rows by walking the whole
8960            // heap, because they are not in the tree. That is the price
8961            // r1046 measured and accepted for an UNBOUNDED order. With
8962            // a LIMIT the walk above has usually already produced every
8963            // row the caller asked for, and scanning 400,000 rows to
8964            // add none of them is the whole cost of the query: the
8965            // release sweep's `SELECT pad FROM t ORDER BY n LIMIT 10`
8966            // over a nullable indexed NUMERIC went 0.237 ms at 50,000
8967            // rows and 2.251 at 400,000 — linear, against PostgreSQL's
8968            // 0.155 and 0.182 — the moment this gate started accepting
8969            // LIMIT. The `remaining` check below sits after the
8970            // per-row filters, so it could never be reached.
8971            if *remaining == Some(0) {
8972                return Ok(0);
8973            }
8974            let mut n = 0usize;
8975            for (ri, row) in table.rows().iter().enumerate() {
8976                if !matches!(row.values.get(order_pos), Some(Value::Null)) {
8977                    continue;
8978                }
8979                if emitted_rows.get(ri).copied().unwrap_or(true) {
8980                    continue;
8981                }
8982                if !table.is_row_visible(ri, &snapshot) {
8983                    continue;
8984                }
8985                *visited += 1;
8986                if visited.is_multiple_of(256) {
8987                    cancel.check()?;
8988                }
8989                emitted_rows[ri] = true;
8990                if *remaining == Some(0) {
8991                    break;
8992                }
8993                let passed = if *to_skip > 0 {
8994                    let p = Self::stream_filter_project(
8995                        row,
8996                        stmt.where_.as_ref(),
8997                        compiled_where.as_ref(),
8998                        eval_stack,
8999                        &projection,
9000                        &bound_pos,
9001                        &ctx,
9002                        values,
9003                    )?;
9004                    if p {
9005                        *to_skip -= 1;
9006                    }
9007                    false
9008                } else {
9009                    Self::stream_project_row(
9010                        row,
9011                        stmt.where_.as_ref(),
9012                        compiled_where.as_ref(),
9013                        eval_stack,
9014                        &projection,
9015                        &bound_pos,
9016                        &ctx,
9017                        values,
9018                        emit,
9019                    )?
9020                };
9021                if passed {
9022                    n += 1;
9023                    if let Some(r) = remaining.as_mut() {
9024                        *r -= 1;
9025                        if *r == 0 {
9026                            break;
9027                        }
9028                    }
9029                    if distinct {
9030                        break;
9031                    }
9032                }
9033            }
9034            Ok(n)
9035        };
9036
9037        if nulls_first {
9038            count += emit_null_rows(
9039                &mut emitted_rows,
9040                &mut eval_stack,
9041                &mut values,
9042                &mut visited,
9043                &mut to_skip,
9044                &mut remaining,
9045                emit,
9046            )?;
9047        }
9048
9049        // v7.39.13 — a prefix walk when the statement binds the index's
9050        // leading column, the whole tree otherwise. The key is not read
9051        // by the loop, so the two shapes meet as posting lists.
9052        let walker: alloc::boxed::Box<dyn Iterator<Item = &spg_storage::PostingList>> =
9053            match prefix.as_ref().and_then(|p| {
9054                if order.desc {
9055                    index.iter_prefix_desc(p).map(
9056                        |it| -> alloc::boxed::Box<dyn Iterator<Item = &spg_storage::PostingList>> {
9057                            alloc::boxed::Box::new(it.map(|(_, l)| l))
9058                        },
9059                    )
9060                } else {
9061                    index.iter_prefix_asc(p).map(
9062                        |it| -> alloc::boxed::Box<dyn Iterator<Item = &spg_storage::PostingList>> {
9063                            alloc::boxed::Box::new(it.map(|(_, l)| l))
9064                        },
9065                    )
9066                }
9067            }) {
9068                Some(it) => it,
9069                None if order.desc => alloc::boxed::Box::new(index.iter_desc().map(|(_, l)| l)),
9070                None => alloc::boxed::Box::new(index.iter_asc().map(|(_, l)| l)),
9071            };
9072        'walk: for locators in walker {
9073            if remaining == Some(0) {
9074                break;
9075            }
9076            for loc in locators {
9077                let spg_storage::RowLocator::Hot(ri) = *loc else {
9078                    continue;
9079                };
9080                if emitted_rows.get(ri).copied().unwrap_or(true) {
9081                    continue;
9082                }
9083                if !table.is_row_visible(ri, &snapshot) {
9084                    continue;
9085                }
9086                let Some(row) = table.rows().get(ri) else {
9087                    continue;
9088                };
9089                visited += 1;
9090                if visited.is_multiple_of(256) {
9091                    cancel.check()?;
9092                }
9093                emitted_rows[ri] = true;
9094                // v7.39.11 — a skipped row still runs the predicate and
9095                // the projection, because OFFSET counts rows that PASS;
9096                // it just does not reach the client.
9097                let passed = if to_skip > 0 {
9098                    let p = Self::stream_filter_project(
9099                        row,
9100                        stmt.where_.as_ref(),
9101                        compiled_where.as_ref(),
9102                        &mut eval_stack,
9103                        &projection,
9104                        &bound_pos,
9105                        &ctx,
9106                        &mut values,
9107                    )?;
9108                    if p {
9109                        to_skip -= 1;
9110                    }
9111                    false
9112                } else {
9113                    Self::stream_project_row(
9114                        row,
9115                        stmt.where_.as_ref(),
9116                        compiled_where.as_ref(),
9117                        &mut eval_stack,
9118                        &projection,
9119                        &bound_pos,
9120                        &ctx,
9121                        &mut values,
9122                        emit,
9123                    )?
9124                };
9125                if passed {
9126                    count += 1;
9127                    if let Some(r) = remaining.as_mut() {
9128                        *r -= 1;
9129                        if *r == 0 {
9130                            break 'walk;
9131                        }
9132                    }
9133                    // One row per key group: the rest are the same value.
9134                    if distinct {
9135                        break;
9136                    }
9137                }
9138            }
9139        }
9140
9141        if !nulls_first {
9142            count += emit_null_rows(
9143                &mut emitted_rows,
9144                &mut eval_stack,
9145                &mut values,
9146                &mut visited,
9147                &mut to_skip,
9148                &mut remaining,
9149                emit,
9150            )?;
9151        }
9152        Ok(Some(count))
9153    }
9154
9155    /// r1031 — `ORDER BY` over NOT NULL integer columns, sorted without
9156    /// building an `OrderKey` vector per row.
9157    ///
9158    /// The row-returning sorted scan allocates twice per row: one
9159    /// `Vec<OrderKey>` for the sort keys and one `Vec<Value>` for the
9160    /// projection. Counted over 400 k rows (r1030,
9161    /// `docs/PERF_SORTED_SCAN_ALLOCATIONS_2026-08-15.md`), that is 800,067
9162    /// allocations and 208 MB of traffic for an answer of four hundred
9163    /// thousand integers.
9164    ///
9165    /// The key half is pure ceremony on this shape.
9166    /// `sort_tagged_by_inline_int_key` already sorts indices rather than
9167    /// rows, so the per-row vector is built, has one integer taken out of
9168    /// it, and is then dragged through the permutation — it exists to carry
9169    /// a number the row's column already held. This lane carries the number
9170    /// instead, in a fixed-size array that lives inside the buffer element
9171    /// and allocates nothing. Same idea as the predicate VM's integer lane.
9172    ///
9173    /// Declines to `None` for anything it does not cover, and every caller
9174    /// falls through to the general path, so the gate list is the
9175    /// specification.
9176    ///
9177    /// Ties: equal keys keep scan order, as the stable sort on the general
9178    /// path does. Rows that tie on every ORDER BY term are entitled to any
9179    /// order among themselves either way — see `STABILITY.md`.
9180    fn try_int_key_sorted_stream<F>(
9181        &self,
9182        stmt: &SelectStatement,
9183        from: &FromClause,
9184        cancel: CancelToken<'_>,
9185        emit: &mut F,
9186    ) -> Result<Option<usize>, EngineError>
9187    where
9188        F: FnMut(crate::StreamItem<'_>) -> Result<(), EngineError>,
9189    {
9190        /// Sort terms this lane carries inline. Four covers every ORDER BY
9191        /// in the endpoint sweep and in the dogfood corpus; wider ones fall
9192        /// through rather than growing the buffer element for everybody.
9193        const MAX_KEYS: usize = 4;
9194
9195        if stmt.order_by.is_empty()
9196            || stmt.order_by.len() > MAX_KEYS
9197            // v7.38.14 — DISTINCT is admitted when the projected set is
9198            // exactly the ORDER BY set, and only then. This lane sorts, and
9199            // when the sort key determines the projected row every duplicate
9200            // lands ADJACENT to its twin -- so the de-duplication is a
9201            // comparison with the previous row rather than a hash table, and
9202            // the reason this lane declined DISTINCT disappears with it. The
9203            // seen-set it could not offer held indices into a materialised
9204            // vector; there is no seen-set now.
9205            //
9206            // The gate is as narrow as the bare-GROUP-BY rewrite's for the
9207            // same reason: `ORDER BY a` over a projection of `a, b` does NOT
9208            // place duplicates of the PAIR adjacent, so set EQUALITY, never
9209            // overlap.
9210            || (stmt.distinct && !Self::distinct_is_adjacent_after_sort(stmt))
9211            || stmt.limit_with_ties
9212            || stmt.limit.is_some()
9213            || stmt.offset.is_some()
9214            || stmt.having.is_some()
9215            || stmt.group_by.is_some()
9216            || !stmt.unions.is_empty()
9217            || !from.joins.is_empty()
9218            || from.primary.lateral_subquery.is_some()
9219            || from.primary.unnest_expr.is_some()
9220            || from.primary.as_of_segment.is_some()
9221            || from.primary.generate_series_args.is_some()
9222            || select_has_window(stmt)
9223            || aggregate::uses_aggregate_in(stmt, self.speaks_mysql)
9224        {
9225            return Ok(None);
9226        }
9227        if stmt
9228            .items
9229            .iter()
9230            .any(|i| matches!(i, SelectItem::Expr { expr, .. } if is_top_level_unnest(expr)))
9231        {
9232            return Ok(None);
9233        }
9234        crate::orderby::check_order_by_legality(stmt)?;
9235        crate::orderby::check_order_by_positions(stmt)?;
9236        crate::window::reject_window_in_row_clauses(stmt)?;
9237        let Some(table) = self.active_catalog().get(&from.primary.name) else {
9238            return Ok(None);
9239        };
9240        if table.has_cold_rows_fast() {
9241            return Ok(None);
9242        }
9243        if !from.primary.only
9244            && crate::partition::has_children(self.active_catalog(), &from.primary.name)
9245        {
9246            return Ok(None);
9247        }
9248        let alias = from
9249            .primary
9250            .alias
9251            .as_deref()
9252            .unwrap_or(from.primary.name.as_str());
9253        let cols = table.schema().columns.clone();
9254
9255        // Every ORDER BY term must be a NOT NULL integer column of this
9256        // table. NOT NULL is what lets the key be a bare integer: with
9257        // NULLs the lane would have to carry their ordering too, and
9258        // getting that subtly wrong is the r1020 defect.
9259        let mut key_pos = [0usize; MAX_KEYS];
9260        let mut descs = [false; MAX_KEYS];
9261        // PG's default is NULLS LAST for ASC and NULLS FIRST for DESC,
9262        // which the AST records as `None`; `unwrap_or(desc)` is how the
9263        // rest of the engine resolves it.
9264        let mut nulls_first = [false; MAX_KEYS];
9265        let n_keys = stmt.order_by.len();
9266        for (slot, order) in stmt.order_by.iter().enumerate() {
9267            let Expr::Column(oc) = &order.expr else {
9268                return Ok(None);
9269            };
9270            if let Some(q) = &oc.qualifier
9271                && !q.eq_ignore_ascii_case(alias)
9272            {
9273                return Ok(None);
9274            }
9275            let Some(pos) = cols
9276                .iter()
9277                .position(|c| c.name.eq_ignore_ascii_case(&oc.name))
9278            else {
9279                return Ok(None);
9280            };
9281            if !matches!(
9282                cols[pos].ty,
9283                spg_storage::DataType::SmallInt
9284                    | spg_storage::DataType::Int
9285                    | spg_storage::DataType::BigInt
9286            ) {
9287                return Ok(None);
9288            }
9289            key_pos[slot] = pos;
9290            descs[slot] = order.desc;
9291            nulls_first[slot] = order.nulls_first.unwrap_or(order.desc);
9292        }
9293
9294        let sess = self.dml_session();
9295        let ctx = EvalContext::new(&cols, Some(alias))
9296            .with_catalog(self.active_catalog())
9297            .with_session(&sess);
9298        let projection = build_projection(
9299            &stmt.items,
9300            &cols,
9301            alias,
9302            self.speaks_mysql,
9303            Some(self.active_catalog()),
9304        )?;
9305        let columns: Vec<ColumnSchema> = projection.iter().map(|p| p.to_column_schema()).collect();
9306        let bound_pos: Vec<Option<usize>> = projection
9307            .iter()
9308            .map(|p| match &p.expr {
9309                Expr::Column(c) => match crate::eval::locate_column(c, &ctx) {
9310                    Ok(Some(pos)) => Some(pos),
9311                    _ => None,
9312                },
9313                _ => None,
9314            })
9315            .collect();
9316        let compiled_where: Option<crate::eval::CompiledExpr> = stmt
9317            .where_
9318            .as_ref()
9319            .filter(|w| crate::eval::fully_compilable(w))
9320            .map(|w| crate::eval::compile_expr(w, &ctx));
9321
9322        // The same first-observable point the materialising planner fires,
9323        // placed after the gates so it fires exactly once: this lane runs
9324        // BEFORE that planner and would otherwise be a hole in the
9325        // panic-isolation and cancellation-race coverage rather than a
9326        // faster path through it.
9327        crate::injection_point!("planner_first_row_fetch", &stmt.from);
9328
9329        let mut eval_stack: Vec<Value<'static>> = Vec::new();
9330        let mut values: Vec<Value<'static>> = Vec::with_capacity(projection.len());
9331        let mut budget = ByteBudget::new(self.max_query_bytes);
9332        let snapshot = self.current_snapshot();
9333        // Keys, a NULL bit per key slot, and the row. The bitmask keeps
9334        // the element small: a nullable key still costs one bit rather
9335        // than a second array.
9336        let mut sorted: Vec<([i64; MAX_KEYS], u8, Vec<Value<'static>>)> = Vec::new();
9337
9338        for (ri, row) in table.rows().iter().enumerate() {
9339            if ri.is_multiple_of(256) {
9340                cancel.check()?;
9341            }
9342            if !table.is_row_visible(ri, &snapshot) {
9343                continue;
9344            }
9345            // The key comes from the STORED row, before projection: an
9346            // ORDER BY column need not appear in the select list.
9347            let mut keys = [0i64; MAX_KEYS];
9348            let mut nulls = 0u8;
9349            let mut keyed = true;
9350            for slot in 0..n_keys {
9351                match row.values.get(key_pos[slot]) {
9352                    Some(Value::SmallInt(v)) => keys[slot] = i64::from(*v),
9353                    Some(Value::Int(v)) => keys[slot] = i64::from(*v),
9354                    Some(Value::BigInt(v)) => keys[slot] = *v,
9355                    Some(Value::Null) | None => nulls |= 1 << slot,
9356                    // An integer column holding something else is a row
9357                    // this lane cannot order; hand the whole query back
9358                    // rather than guess at it.
9359                    _ => {
9360                        keyed = false;
9361                        break;
9362                    }
9363                }
9364            }
9365            if !keyed {
9366                return Ok(None);
9367            }
9368            if !Self::stream_filter_project(
9369                row,
9370                stmt.where_.as_ref(),
9371                compiled_where.as_ref(),
9372                &mut eval_stack,
9373                &projection,
9374                &bound_pos,
9375                &ctx,
9376                &mut values,
9377            )? {
9378                continue;
9379            }
9380            budget.charge(crate::bytebudget::approx_values_bytes(&values))?;
9381            sorted.push((keys, nulls, core::mem::take(&mut values)));
9382            values.reserve(projection.len());
9383        }
9384
9385        sorted.sort_by(|a, b| {
9386            use core::cmp::Ordering;
9387            for slot in 0..n_keys {
9388                let bit = 1u8 << slot;
9389                let ord = match (a.1 & bit != 0, b.1 & bit != 0) {
9390                    (true, true) => Ordering::Equal,
9391                    // Where the NULLs go is already decided — `nulls_first`
9392                    // resolved DESC's default when it was read. Reversing
9393                    // this for DESC as well would apply the direction
9394                    // twice and put them at the wrong end.
9395                    (true, false) => {
9396                        if nulls_first[slot] {
9397                            Ordering::Less
9398                        } else {
9399                            Ordering::Greater
9400                        }
9401                    }
9402                    (false, true) => {
9403                        if nulls_first[slot] {
9404                            Ordering::Greater
9405                        } else {
9406                            Ordering::Less
9407                        }
9408                    }
9409                    (false, false) => {
9410                        let o = a.0[slot].cmp(&b.0[slot]);
9411                        if descs[slot] { o.reverse() } else { o }
9412                    }
9413                };
9414                if ord != Ordering::Equal {
9415                    return ord;
9416                }
9417            }
9418            Ordering::Equal
9419        });
9420
9421        emit(crate::StreamItem::Header(&columns))?;
9422        // v7.38.14 — DISTINCT, de-duplicated against the PREVIOUS row.
9423        //
9424        // The gate above only admits DISTINCT when the sort key determines
9425        // the projected row, so every duplicate is adjacent to its twin by
9426        // the time this loop runs and one comparison replaces a hash table
9427        // of every row seen. Equality is `values_eq_norm` with the same mask
9428        // the materialising path builds -- deliberately the same function,
9429        // because a de-duplication that disagreed with the one on the other
9430        // path would make the answer depend on which lane a query took.
9431        //
9432        // A query that did not ask for DISTINCT pays one already-false bool
9433        // test per row: the short-circuit means the comparison never runs
9434        // and `prev` is never written.
9435        let dedup_mask = fold_mask(&projection);
9436        let fold = FoldSpec::of(self.speaks_mysql, &dedup_mask);
9437        let mut count = 0usize;
9438        let mut prev: Option<&[Value<'static>]> = None;
9439        for (_, _, vals) in &sorted {
9440            if stmt.distinct
9441                && let Some(p) = prev
9442                && values_eq_norm(p, vals, fold)
9443            {
9444                continue;
9445            }
9446            emit(crate::StreamItem::Row(crate::RowCells::Values(vals)))?;
9447            count += 1;
9448            if stmt.distinct {
9449                prev = Some(vals);
9450            }
9451        }
9452        Ok(Some(count))
9453    }
9454
9455    /// v7.38.14 — would sorting place every duplicate next to its twin?
9456    ///
9457    /// True when the projected expressions and the ORDER BY expressions are the
9458    /// same SET. Then the sort key determines the projected row, so equal rows
9459    /// are adjacent afterwards and an adjacent comparison de-duplicates exactly
9460    /// as a hash would -- and, because both sort paths are stable, the survivor
9461    /// is the first-seen row, which is the one the hash keeps too.
9462    ///
9463    /// A wildcard's expansion is not known here, so it is not a set this can
9464    /// compare; an ordinal ORDER BY names a select-list position rather than a
9465    /// value and is left alone.
9466    fn distinct_is_adjacent_after_sort(stmt: &SelectStatement) -> bool {
9467        if stmt.order_by.is_empty() || !stmt.distinct_on.is_empty() {
9468            return false;
9469        }
9470        let mut projected: alloc::vec::Vec<&Expr> =
9471            alloc::vec::Vec::with_capacity(stmt.items.len());
9472        for item in &stmt.items {
9473            match item {
9474                SelectItem::Wildcard | SelectItem::QualifiedWildcard(_) => return false,
9475                SelectItem::Expr { expr, .. } => projected.push(expr),
9476            }
9477        }
9478        if projected.is_empty() {
9479            return false;
9480        }
9481        let keys: alloc::vec::Vec<&Expr> = stmt.order_by.iter().map(|o| &o.expr).collect();
9482        if keys
9483            .iter()
9484            .any(|k| matches!(k, Expr::Literal(spg_sql::ast::Literal::Integer(_))))
9485        {
9486            return false;
9487        }
9488        projected.iter().all(|p| keys.contains(p)) && keys.iter().all(|k| projected.contains(k))
9489    }
9490
9491    fn try_spill_sorted_stream<F>(
9492        &self,
9493        stmt: &SelectStatement,
9494        from: &FromClause,
9495        cancel: CancelToken<'_>,
9496        emit: &mut F,
9497    ) -> Result<Option<usize>, EngineError>
9498    where
9499        F: FnMut(crate::StreamItem<'_>) -> Result<(), EngineError>,
9500    {
9501        // The shapes `try_spill_sorted_scan` declines, plus the ones the
9502        // streaming executor does not carry (a LIMIT is already bounded
9503        // by a partial sort; the rest need the answer addressable).
9504        if !self.can_spill()
9505            || stmt.order_by.is_empty()
9506            || stmt.distinct
9507            || stmt.limit_with_ties
9508            || stmt.limit.is_some()
9509            || stmt.offset.is_some()
9510            || stmt.having.is_some()
9511            || stmt.group_by.is_some()
9512            || !stmt.unions.is_empty()
9513            || !from.joins.is_empty()
9514            || from.primary.lateral_subquery.is_some()
9515            || from.primary.unnest_expr.is_some()
9516            || from.primary.as_of_segment.is_some()
9517            || from.primary.generate_series_args.is_some()
9518            || select_has_window(stmt)
9519            || aggregate::uses_aggregate_in(stmt, self.speaks_mysql)
9520        {
9521            return Ok(None);
9522        }
9523        if stmt
9524            .items
9525            .iter()
9526            .any(|i| matches!(i, SelectItem::Expr { expr, .. } if is_top_level_unnest(expr)))
9527        {
9528            return Ok(None);
9529        }
9530        // Everything `exec_bare_select_cancel` does before it scans runs
9531        // BELOW this path, so a statement claimed here skips it. Three of
9532        // those were missed on the way in and each was caught by a
9533        // different gate — the ORDER BY rules by an e2e (`SELECT a FROM t
9534        // ORDER BY 2` sorted happily instead of raising 42P10), the
9535        // cancellation check by another, the partition fan-out by the
9536        // differential corpus. What is reconciled, item by item: with-ties
9537        // needs ORDER BY (gated above), USING/NATURAL and RLS join
9538        // rewrites (joins gated above), the single-table RLS predicate
9539        // (the dispatcher declines a policy-subject table before this is
9540        // reached), the meta-view dispatch (those names are not in the
9541        // catalog, so the lookup below declines). These three are calls,
9542        // so the message and SQLSTATE are the ones the fall-back gives —
9543        // `select_has_window` above reads the select list and ORDER BY but
9544        // not WHERE, which is the case the third one covers.
9545        crate::orderby::check_order_by_legality(stmt)?;
9546        crate::orderby::check_order_by_positions(stmt)?;
9547        crate::window::reject_window_in_row_clauses(stmt)?;
9548        // A parent's rows are its children's. These walks scan the named
9549        // relation alone, so a partitioned or inherited parent comes back
9550        // short — and silently: the corpus caught `SELECT id FROM pr
9551        // ORDER BY id` and `SELECT k FROM pl ORDER BY k` returning the
9552        // parent's own rows instead of the partitions'. `ONLY` is exactly
9553        // the case that does not fan out, so it stays, which is the test
9554        // the FROM-clause fan-out itself makes.
9555        if !from.primary.only
9556            && crate::partition::has_children(self.active_catalog(), &from.primary.name)
9557        {
9558            return Ok(None);
9559        }
9560        let Some(table) = self.active_catalog().get(&from.primary.name) else {
9561            return Ok(None);
9562        };
9563        // Cold-tier rows live outside `rows()`; this walk would drop
9564        // them silently, the same reason round 831's walk declines.
9565        if table.has_cold_rows_fast() {
9566            return Ok(None);
9567        }
9568
9569        let alias = from
9570            .primary
9571            .alias
9572            .as_deref()
9573            .unwrap_or(from.primary.name.as_str());
9574        let cols = table.schema().columns.clone();
9575        let sess = self.dml_session();
9576        let ctx = EvalContext::new(&cols, Some(alias))
9577            .with_catalog(self.active_catalog())
9578            .with_session(&sess);
9579        let projection = build_projection(
9580            &stmt.items,
9581            &cols,
9582            alias,
9583            self.speaks_mysql,
9584            Some(self.active_catalog()),
9585        )?;
9586        let order_by = stmt.order_by.clone();
9587        // The same one-shot resolution the general path does (round
9588        // 582): each ORDER BY column is bound once, not once per row.
9589        let order_bound = crate::orderby::order_by_bound_positions(&order_by, &cols, Some(alias));
9590        let descs: Vec<bool> = order_by.iter().map(|o| o.desc).collect();
9591        // Resolved BEFORE the scan, because it now decides what the sort
9592        // STORES and not just what it decodes (round 995).
9593        let needed = Self::sort_record_columns_needed(&stmt.items, &order_bound, cols.len(), &ctx);
9594
9595        // v7.38.22 — resolved HERE, because this path did not resolve
9596        // them at all.
9597        //
9598        // Every published SPG through 7.38.21 answered `ORDER BY s COLLATE
9599        // "en_US.utf8"` in BYTE order on this path — and swallowed an
9600        // unknown collation name rather than raising — because the sorter
9601        // below compared with an empty collation slice. The materialising
9602        // path honoured both. Which answer a query got depended on which
9603        // path the planner took, and this is the path a plain single-table
9604        // SELECT takes.
9605        let order_colls = crate::orderby::order_by_collations(&order_by, &ctx)?;
9606        // v7.39.12 — a correlated scalar subquery in ORDER BY is
9607        // resolved for the row before its key is built.
9608        //
9609        // Uncorrelated subqueries are replaced by a literal before
9610        // execution; a correlated one cannot be, so it reached the
9611        // per-row evaluator — the one place that cannot run a subquery
9612        // — and the statement raised "subquery reached row eval".
9613        // Reported by sentori against 7.39.11; see
9614        // `Engine::order_by_resolved_for_row`.
9615        //
9616        // The `any` runs once, here, so an ordinary ORDER BY pays one
9617        // bool per row and nothing else.
9618        let order_has_subquery = order_by
9619            .iter()
9620            .any(|o| crate::subquery::expr_has_subquery(&o.expr));
9621        let unbound: Vec<Option<usize>> = alloc::vec![None; order_by.len()];
9622        let mut sorter = crate::extsort::ExternalSorter::new(
9623            self.temp_run_factory,
9624            self.session_work_mem_bytes(),
9625            cols.clone(),
9626            &descs,
9627            &order_colls,
9628        )
9629        .with_stats(&self.spill_stats)
9630        .with_pruned(&needed);
9631        let snapshot = self.current_snapshot();
9632        // One key buffer for the whole scan: `push` drains it and leaves
9633        // the capacity behind.
9634        let mut keys: Vec<OrderKey> = Vec::new();
9635        // r1024 — compile the predicate once for the scan.
9636        //
9637        // These two sorted-spill scans are the paths a single-table SELECT
9638        // with an ORDER BY takes, and they were the last row-returning ones
9639        // still walking the expression tree per row. r1023 did the
9640        // no-ORDER-BY sibling; the sweep's two remaining losing cells are
9641        // exactly this shape.
9642        //
9643        // Found from the profile's CALL TREE rather than its leaves. The
9644        // leaves say what is expensive — `eval_expr` 320, `apply_binary`
9645        // 261, `mod_op` 178 — and two attempts at reasoning out which
9646        // function asked for it were both wrong. The tree names the caller
9647        // chain, and it named this one.
9648        let compiled_where: Option<crate::eval::CompiledExpr> = stmt
9649            .where_
9650            .as_ref()
9651            .filter(|w| crate::eval::fully_compilable(w))
9652            .map(|w| crate::eval::compile_expr(w, &ctx));
9653        let mut eval_stack: Vec<Value<'static>> = Vec::new();
9654        for (i, row) in table.scan_visible_from(0, &snapshot) {
9655            if i.is_multiple_of(256) {
9656                cancel.check()?;
9657            }
9658            if let Some(c) = &compiled_where {
9659                if !crate::eval::compiled::eval_compiled_pred(
9660                    c,
9661                    row,
9662                    &ctx,
9663                    &mut eval_stack,
9664                    ctx.mysql_dialect,
9665                )? {
9666                    continue;
9667                }
9668            } else if let Some(w) = &stmt.where_ {
9669                let cond = crate::eval::eval_expr(w, row, &ctx).map_err(EngineError::Eval)?;
9670                if !crate::eval::predicate_is_true(&cond, "WHERE", ctx.mysql_dialect)? {
9671                    continue;
9672                }
9673            }
9674            keys.clear();
9675            // The same collations the sorter compares with, and the
9676            // re-derivation below is handed the same ones. `finish`'s
9677            // contract is that a key comes back the way it was pushed;
9678            // a collation is part of the way it was pushed.
9679            if order_has_subquery {
9680                // A substituted literal is no longer a bound column.
9681                let per_row = self.order_by_resolved_for_row(&order_by, row, &ctx, cancel)?;
9682                crate::orderby::build_order_keys_bound(
9683                    per_row.as_deref().unwrap_or(&order_by),
9684                    &unbound,
9685                    &order_colls,
9686                    row,
9687                    &ctx,
9688                    &mut keys,
9689                )?;
9690            } else {
9691                crate::orderby::build_order_keys_bound(
9692                    &order_by,
9693                    &order_bound,
9694                    &order_colls,
9695                    row,
9696                    &ctx,
9697                    &mut keys,
9698                )?;
9699            }
9700            sorter.push(&mut keys, row)?;
9701        }
9702
9703        let columns: Vec<ColumnSchema> = projection.iter().map(|p| p.to_column_schema()).collect();
9704        emit(crate::StreamItem::Header(&columns))?;
9705
9706        let key_ctx = &ctx;
9707        let mut emitted_since_check = 0usize;
9708        let n = sorter.finish_each(
9709            |src, buf| {
9710                crate::orderby::build_order_keys_rederived(
9711                    &order_by,
9712                    &order_bound,
9713                    &order_colls,
9714                    src,
9715                    key_ctx,
9716                    buf,
9717                )
9718            },
9719            |src, values| {
9720                for p in &projection {
9721                    values.push(
9722                        crate::eval::eval_expr(&p.expr, src, key_ctx).map_err(EngineError::Eval)?,
9723                    );
9724                }
9725                Ok(())
9726            },
9727            |cells| {
9728                // The merge is the long half of a big sort, and the scan's
9729                // check above stops running once it ends: a cancelled
9730                // `SELECT pad FROM big ORDER BY id` delivered all 120k rows
9731                // anyway. Same stride as the scan.
9732                emitted_since_check += 1;
9733                if emitted_since_check >= 256 {
9734                    emitted_since_check = 0;
9735                    cancel.check()?;
9736                }
9737                emit(crate::StreamItem::Row(crate::RowCells::Values(cells)))
9738            },
9739        )?;
9740        Ok(Some(n))
9741    }
9742
9743    /// One row of the single-table streaming walk: the WHERE test, the
9744    /// projection, the emit. Returns whether a row was emitted.
9745    ///
9746    /// v7.39 (round 970) — factored out because the walk now has two ways
9747    /// to reach a row, the sequential scan and an index seek's candidate
9748    /// positions, and both must do IDENTICALLY this. A copy in each is how
9749    /// two paths for one job drift; this file already carries the cost of
9750    /// that lesson twice (rounds 823 and 961, both resolvers).
9751    ///
9752    /// `#[inline]` so the scan loop keeps the shape round 957 measured it
9753    /// in — a shared hot path pays for a new abstraction whether or not it
9754    /// uses it, and this one is on the scan.
9755    #[inline]
9756    #[allow(clippy::too_many_arguments)]
9757    fn stream_filter_project(
9758        row: &spg_storage::Row<'static>,
9759        where_: Option<&Expr>,
9760        // r1023 — the same WHERE, compiled once by the caller. `None` means
9761        // the expression did not qualify and `where_` is evaluated as before.
9762        compiled_where: Option<&crate::eval::CompiledExpr>,
9763        eval_stack: &mut Vec<Value<'static>>,
9764        projection: &[ProjectedItem],
9765        bound_pos: &[Option<usize>],
9766        ctx: &crate::eval::EvalContext<'_>,
9767        values: &mut Vec<Value<'static>>,
9768    ) -> Result<bool, EngineError> {
9769        // r1023 — this scan ran its predicate through the TREE INTERPRETER,
9770        // once per row, and it was the only row-returning path that did.
9771        // The aggregate path, `table_access`, and the PK walker all compile
9772        // theirs. Profiled: on `SELECT pad FROM d WHERE id % 3 = 0` the
9773        // server's live samples were `eval_expr` 99, `apply_binary` 81,
9774        // `mod_op` 29 — the interpreter, not delivery.
9775        //
9776        // The arithmetic accounted for it exactly. Over the wire, the same
9777        // filter costs 6.375 ms returning rows and 0.679 ms counting them;
9778        // the 5.70 ms difference over 50,000 scanned rows is 114 ns each,
9779        // which is what an interpreted predicate costs against the compiled
9780        // lane's 11.7. It was named "delivery after a filter" before this
9781        // profile, and it was never delivery.
9782        if let Some(c) = compiled_where {
9783            if !crate::eval::compiled::eval_compiled_pred(
9784                c,
9785                row,
9786                ctx,
9787                eval_stack,
9788                ctx.mysql_dialect,
9789            )? {
9790                return Ok(false);
9791            }
9792        } else if let Some(w) = where_ {
9793            let cond = crate::eval::eval_expr(w, row, ctx).map_err(EngineError::Eval)?;
9794            if !crate::eval::predicate_is_true(&cond, "WHERE", ctx.mysql_dialect)? {
9795                return Ok(false);
9796            }
9797        }
9798        values.clear();
9799        for (p, bound) in projection.iter().zip(bound_pos) {
9800            values.push(match bound {
9801                Some(pos) => crate::eval::column_at(*pos, row, ctx).map_err(EngineError::Eval)?,
9802                None => crate::eval::eval_expr(&p.expr, row, ctx).map_err(EngineError::Eval)?,
9803            });
9804        }
9805        Ok(true)
9806    }
9807
9808    /// The same filter and projection, then emit. Split from
9809    /// [`Self::stream_filter_project`] so a path that has to BUFFER rows
9810    /// before it can emit them — a sort — runs the identical predicate and
9811    /// projection rather than a second copy of them.
9812    #[allow(clippy::too_many_arguments)]
9813    fn stream_project_row<F>(
9814        row: &spg_storage::Row<'static>,
9815        where_: Option<&Expr>,
9816        compiled_where: Option<&crate::eval::CompiledExpr>,
9817        eval_stack: &mut Vec<Value<'static>>,
9818        projection: &[ProjectedItem],
9819        bound_pos: &[Option<usize>],
9820        ctx: &crate::eval::EvalContext<'_>,
9821        values: &mut Vec<Value<'static>>,
9822        emit: &mut F,
9823    ) -> Result<bool, EngineError>
9824    where
9825        F: FnMut(crate::StreamItem<'_>) -> Result<(), EngineError>,
9826    {
9827        if !Self::stream_filter_project(
9828            row,
9829            where_,
9830            compiled_where,
9831            eval_stack,
9832            projection,
9833            bound_pos,
9834            ctx,
9835            values,
9836        )? {
9837            return Ok(false);
9838        }
9839        emit(crate::StreamItem::Row(crate::RowCells::Values(values)))?;
9840        Ok(true)
9841    }
9842
9843    fn try_stream_single_table<F>(
9844        &self,
9845        stmt: &SelectStatement,
9846        from: &FromClause,
9847        cancel: CancelToken<'_>,
9848        emit: &mut F,
9849    ) -> Result<Option<usize>, EngineError>
9850    where
9851        F: FnMut(crate::StreamItem<'_>) -> Result<(), EngineError>,
9852    {
9853        let Some(table) = self.active_catalog().get(&from.primary.name) else {
9854            return Ok(None);
9855        };
9856        // Cold-tier rows live outside `rows()`; the materialising fallback
9857        // covers both tiers and this walk would silently drop them.
9858        if table.has_cold_rows_fast() {
9859            return Ok(None);
9860        }
9861        let alias = from
9862            .primary
9863            .alias
9864            .as_deref()
9865            .unwrap_or(from.primary.name.as_str());
9866        let cols = table.schema().columns.clone();
9867        let sess = self.dml_session();
9868        let ctx = EvalContext::new(&cols, Some(alias))
9869            .with_catalog(self.active_catalog())
9870            .with_session(&sess);
9871        let projection = build_projection(
9872            &stmt.items,
9873            &cols,
9874            alias,
9875            self.speaks_mysql,
9876            Some(self.active_catalog()),
9877        )?;
9878
9879        let columns: Vec<ColumnSchema> = projection.iter().map(|p| p.to_column_schema()).collect();
9880        emit(crate::StreamItem::Header(&columns))?;
9881
9882        // v7.37 (round 957) — resolve each bare-column projection ONCE
9883        // instead of once per row. `find_column_pos`-style resolution is a
9884        // linear walk of the schema comparing column-name strings, and the
9885        // row loop below ran it for every cell of every row: measured at
9886        // 400k rows, binding it out of the loop took `SELECT pad` from
9887        // 16.5-17.5 ms to 10.9-11.7 ms (-41%, two windows, round 954).
9888        //
9889        // ORDER BY has bound its keys this way since round 582
9890        // (`order_by_bound_positions`); the projection never did.
9891        //
9892        // `locate_column` is the same resolution `resolve_column` performs,
9893        // returning the site instead of the value, so the two cannot drift
9894        // apart the way a second hand-written resolver would. Anything it
9895        // declines — an expression, a whole-row reference, a name that does
9896        // not resolve — binds to `None` and takes the general path below,
9897        // errors included, so an empty table still reports nothing rather
9898        // than raising at bind time.
9899        let bound_pos: Vec<Option<usize>> = projection
9900            .iter()
9901            .map(|p| match &p.expr {
9902                Expr::Column(c) => match crate::eval::locate_column(c, &ctx) {
9903                    Ok(Some(pos)) => Some(pos),
9904                    _ => None,
9905                },
9906                _ => None,
9907            })
9908            .collect();
9909
9910        // One snapshot for the whole scan, as the materialising path takes.
9911        let snapshot = self.current_snapshot();
9912
9913        // v7.39 (round 970) — ask the indices BEFORE walking the table.
9914        //
9915        // This walk had no index step at all, and it is preferred over the
9916        // materialising path, which does have one (`pick_indexed_rows` ->
9917        // `try_index_seek`). So a primary-key point lookup — the commonest
9918        // statement there is — read every row: measured on 500k rows,
9919        // `SELECT * FROM big WHERE id = 250000` took 14.947 ms against
9920        // PG18.4's 0.172 ms, and the cost tracked the TABLE (1k 0.315 ms,
9921        // 10k 1.660, 100k 3.518), which is not what O(log n) looks like.
9922        //
9923        // The control that named it: `... OFFSET 0` — semantically the same
9924        // query — answered in 0.159 ms, because OFFSET is one of the shape
9925        // gates that declines this walk and sends the statement to the path
9926        // that seeks. `LIMIT 1` and `GROUP BY` did the same. The three have
9927        // no semantics in common; what they share is making this function
9928        // stand down.
9929        //
9930        // The seek only NARROWS: every candidate still goes through the
9931        // full WHERE below, exactly as the mutation paths use it, so a
9932        // partial index match cannot change an answer. Positions come back
9933        // already visibility-filtered and already capped at a quarter of the
9934        // table (round 490), so a seek can never cost more than the scan it
9935        // replaces, and `None` means "walk the table" as before.
9936        //
9937        // Sorted because the scan would have produced table order and the
9938        // index produces key order. Without an ORDER BY neither is promised,
9939        // but a walk that silently reorders its answer when an index happens
9940        // to exist is a difference nobody asked for.
9941        let seek_positions: Option<Vec<usize>> = stmt.where_.as_ref().and_then(|w| {
9942            crate::index_access::try_index_seek_positions(
9943                w,
9944                &cols,
9945                table,
9946                alias,
9947                &snapshot,
9948                self.speaks_mysql,
9949            )
9950        });
9951
9952        let mut values: Vec<Value<'static>> = Vec::with_capacity(projection.len());
9953        // r1023 — compile the predicate once for the whole scan. Same gate
9954        // every other path uses: `fully_compilable` or keep the interpreter,
9955        // so a shape the VM cannot take answers exactly as it did before.
9956        let compiled_where: Option<crate::eval::CompiledExpr> = stmt
9957            .where_
9958            .as_ref()
9959            .filter(|w| crate::eval::fully_compilable(w))
9960            .map(|w| crate::eval::compile_expr(w, &ctx));
9961        let mut eval_stack: Vec<Value<'static>> = Vec::new();
9962        let mut count: usize = 0;
9963        match seek_positions {
9964            Some(mut positions) => {
9965                positions.sort_unstable();
9966                for (n, pos) in positions.into_iter().enumerate() {
9967                    if n.is_multiple_of(256) {
9968                        cancel.check()?;
9969                    }
9970                    let Some(row) = table.rows().get(pos) else {
9971                        continue;
9972                    };
9973                    if Self::stream_project_row(
9974                        row,
9975                        stmt.where_.as_ref(),
9976                        compiled_where.as_ref(),
9977                        &mut eval_stack,
9978                        &projection,
9979                        &bound_pos,
9980                        &ctx,
9981                        &mut values,
9982                        emit,
9983                    )? {
9984                        count += 1;
9985                    }
9986                }
9987            }
9988            None => {
9989                // v7.38.11 — the streaming scan is the path a client
9990                // reaches over the wire, so it is the one that has to
9991                // ask the BRIN summary which slots can be skipped. The
9992                // predicate still runs on every row that survives.
9993                let slots = stmt
9994                    .where_
9995                    .as_ref()
9996                    .and_then(|w| crate::brin::candidate_slots(w, table))
9997                    .unwrap_or_else(|| alloc::vec![0..table.row_count()]);
9998                for (i, row) in table.scan_visible_slots(slots, &snapshot) {
9999                    if i.is_multiple_of(256) {
10000                        cancel.check()?;
10001                    }
10002                    if Self::stream_project_row(
10003                        row,
10004                        stmt.where_.as_ref(),
10005                        compiled_where.as_ref(),
10006                        &mut eval_stack,
10007                        &projection,
10008                        &bound_pos,
10009                        &ctx,
10010                        &mut values,
10011                        emit,
10012                    )? {
10013                        count += 1;
10014                    }
10015                }
10016            }
10017        }
10018        Ok(Some(count))
10019    }
10020
10021    pub(crate) fn try_exec_joined_streaming<F>(
10022        &self,
10023        stmt: &SelectStatement,
10024        cancel: CancelToken<'_>,
10025        emit: &mut F,
10026    ) -> Result<Option<usize>, EngineError>
10027    where
10028        F: FnMut(crate::StreamItem<'_>) -> Result<(), EngineError>,
10029    {
10030        // Shape gates — keep the streamable surface narrow on
10031        // purpose. The fall-back path still handles everything else.
10032        let Some(from) = &stmt.from else {
10033            return Ok(None);
10034        };
10035        // v7.37 (round 830) — decline anything a row-security policy binds
10036        // for this session. Policies are injected in
10037        // `exec_bare_select_cancel`, below this path, so a statement claimed
10038        // here would read the table unfiltered: measured, `SELECT val FROM
10039        // sec` returned all three rows to a session whose policy allows two,
10040        // while `SELECT upper(val) FROM sec` — declined by the shape gates
10041        // and so materialised — returned the correct two.
10042        //
10043        // Declining sends it to the path that enforces. Teaching this one to
10044        // inject the predicate itself would keep the streaming benefit for
10045        // RLS tables and is the better end state; it is not what a
10046        // correctness fix should carry, and the fall-back is exactly as
10047        // correct, only slower.
10048        if self.select_reads_policy_subject_table(stmt) {
10049            return Ok(None);
10050        }
10051        // r1058 — a WITH list this path never materialises: the CTE
10052        // name would be resolved as a physical relation and error
10053        // ("relation \"big\" does not exist" over the extended
10054        // protocol, caught by the perm-runner's wire legs). The
10055        // materialising fallback owns CTE execution.
10056        if !stmt.ctes.is_empty() {
10057            return Ok(None);
10058        }
10059        // r1058 — rewritten system catalogs (`__spg_pg_stat_user_
10060        // tables` and kin) exist only as synth arms on the
10061        // materialising path; claiming one here errored "relation
10062        // does not exist" over the extended protocol for a query the
10063        // simple protocol answered. Prefix test only — a genuinely
10064        // missing relation must keep erroring in-path.
10065        if from.primary.name.starts_with("__spg_")
10066            || from
10067                .joins
10068                .iter()
10069                .any(|j| j.table.name.starts_with("__spg_"))
10070        {
10071            return Ok(None);
10072        }
10073        // r1058 — decline partitioned / inheritance parents, same
10074        // shape of bug as the RLS decline above: this path scans the
10075        // named table's own (empty) heap, so `SELECT id, region FROM
10076        // cust` on a partition parent streamed ZERO rows over the wire
10077        // while COUNT(*) — an aggregate, materialised below — said 3.
10078        // Caught by the perm-runner's server permutations; the
10079        // materialising fallback expands children correctly.
10080        if crate::partition::has_children(self.active_catalog(), &from.primary.name)
10081            || from
10082                .joins
10083                .iter()
10084                .any(|j| crate::partition::has_children(self.active_catalog(), &j.table.name))
10085        {
10086            return Ok(None);
10087        }
10088        // v7.39 (round 790) — single-table SELECTs stream too. This
10089        // gate said "joins only" because the path was written for
10090        // mailrs's joined PROJ shape; a plain `SELECT <cols> FROM t`
10091        // fell to the materialising fallback, which builds the whole
10092        // `Vec<Row<'static>>` and only then iterates it. Measured on
10093        // 300k rows: 181 MB single-table vs 70 MB for the SAME rows
10094        // reached through a one-row JOIN — 2.6x, purely for lacking a
10095        // join. The deferred-join structure handles one source as the
10096        // degenerate stride-1 case, so the walk below is unchanged.
10097        let _single_table = from.joins.is_empty();
10098        // An ORDER BY that the bounded sort can serve streams; everything
10099        // else still falls to the materialising fallback below.
10100        // r1025 — an ordering the index already holds needs no sort at all.
10101        // Tried before the spill sort, which is the path it replaces.
10102        if !stmt.order_by.is_empty()
10103            && from.joins.is_empty()
10104            && let Some(n) = self.try_index_order_stream(stmt, from, cancel, emit)?
10105        {
10106            return Ok(Some(n));
10107        }
10108        if !stmt.order_by.is_empty()
10109            && from.joins.is_empty()
10110            && let Some(n) = self.try_spill_sorted_stream(stmt, from, cancel, emit)?
10111        {
10112            return Ok(Some(n));
10113        }
10114        // r1031 — integer keys carried inline instead of an `OrderKey`
10115        // vector per row. Tried AFTER the spill sort on purpose: this lane
10116        // buffers the whole answer, so anything the spill path would take
10117        // must keep taking it rather than be turned back into an in-memory
10118        // sort that answers with a budget error.
10119        if !stmt.order_by.is_empty()
10120            && from.joins.is_empty()
10121            && let Some(n) = self.try_int_key_sorted_stream(stmt, from, cancel, emit)?
10122        {
10123            return Ok(Some(n));
10124        }
10125        if !stmt.order_by.is_empty()
10126            || stmt.limit.is_some()
10127            || stmt.offset.is_some()
10128            || stmt.having.is_some()
10129            || stmt.group_by.is_some()
10130            || stmt.distinct
10131            || !stmt.unions.is_empty()
10132            || stmt.limit_with_ties
10133        {
10134            return Ok(None);
10135        }
10136        if aggregate::uses_aggregate_in(stmt, self.speaks_mysql) {
10137            return Ok(None);
10138        }
10139        // No window / SRF on the streaming path.
10140        if select_has_window(stmt) {
10141            return Ok(None);
10142        }
10143        if stmt
10144            .items
10145            .iter()
10146            .any(|i| matches!(i, SelectItem::Expr { expr, .. } if is_top_level_unnest(expr)))
10147        {
10148            return Ok(None);
10149        }
10150        // v7.37 (round 831) — a joinless FROM over a plain stored table
10151        // never needs the deferred structure, and building one costs the
10152        // whole table. `materialise_table_ref_filtered` clones every row
10153        // into a `Vec<Row<'static>>` before anything is filtered or
10154        // projected, so peak cost tracks the TABLE, not the result:
10155        // measured over 300k rows of 200 bytes, `SELECT id FROM big` and
10156        // `SELECT pad FROM big` both cost +107 MB over baseline, the narrow
10157        // projection saving nothing, while an arithmetic projection — which
10158        // the shape gates decline, so it materialises through the ordinary
10159        // executor — cost +21 MB.
10160        //
10161        // Scanning in batches and releasing each one is what `cursor_fill`
10162        // already does for a lazy cursor, and it is the same walk: resume
10163        // from a slot, take visible rows, evaluate, hand them over, drop
10164        // them. Round 800's finding stands and is why this reads rows OUT
10165        // rather than seeding the join by index — touching the stored
10166        // `PersistentVec` in place makes the whole table resident, which is
10167        // worse than the copy. Each batch is copied, then freed.
10168        if from.joins.is_empty()
10169            && from.primary.unnest_expr.is_none()
10170            && from.primary.lateral_subquery.is_none()
10171            && from.primary.as_of_segment.is_none()
10172            && from.primary.generate_series_args.is_none()
10173            && let Some(n) = self.try_stream_single_table(stmt, from, cancel, emit)?
10174        {
10175            return Ok(Some(n));
10176        }
10177        // Build the deferred join under the regular byte budget.
10178        let mut budget = ByteBudget::new(self.max_query_bytes);
10179        let deferred = {
10180            let mut needed = alloc::collections::BTreeSet::new();
10181            let prunable = collect_qualified_refs(stmt, &mut needed).is_some();
10182            self.build_joined_filtered_rows(
10183                from,
10184                stmt.where_.as_ref(),
10185                cancel,
10186                if prunable { Some(&needed) } else { None },
10187                &mut budget,
10188            )?
10189        };
10190        let combined_schema = &deferred.combined_schema;
10191        // v7.39 (read01 round 53) — carry the catalog (see join.rs): a
10192        // `::regclass` / enum cast in a joined projection or HAVING needs it.
10193        // v7.39 (round 525) — and the session: a joined SELECT's WHERE is
10194        // the same predicate the unjoined shape carries.
10195        let joined_sess = self.dml_session();
10196        // v7.38.18 — and the DIALECT. This context carried the catalog and
10197        // the session and not the one field that decides how text
10198        // compares, so a joined row was evaluated in PostgreSQL
10199        // semantics inside a MySQL session.
10200        //
10201        // It showed up only where the two sides had DIFFERENT text types:
10202        // `a.c = b.s` with `c CHAR(8)` and `s TEXT` answered false, and a
10203        // join on it returned no rows, while `a.c = b.c` and `a.s = b.s`
10204        // were fine and the same comparison inside one table was fine.
10205        // Same-type pairs agree byte-for-byte after an ASCII lowercase,
10206        // so the wrong semantics were invisible until a CHAR's padding
10207        // had to be stripped and PostgreSQL's arm does not strip it.
10208        //
10209        // `with_engine` is what sets it; the next line already reaches
10210        // for `self.backslash_escapes`, so the dialect was in hand.
10211        let ctx = EvalContext::new(combined_schema, None)
10212            .with_catalog(self.active_catalog())
10213            .with_engine(self)
10214            .with_session(&joined_sess);
10215        let projection = build_projection(
10216            &stmt.items,
10217            combined_schema,
10218            "",
10219            self.speaks_mysql,
10220            Some(self.active_catalog()),
10221        )?;
10222        // Every projection item must be a bound qualified column —
10223        // anything that needs `eval_expr_with_correlated` keeps the
10224        // materialising path.
10225        let bound_pos = |e: &Expr| -> Option<usize> {
10226            match e {
10227                // v7.39 (round 822) — an UNQUALIFIED column resolves here
10228                // too. The `qualifier.is_some()` guard this replaces meant
10229                // `SELECT pad FROM big` — the commonest projection there is
10230                // — never reached the streaming walk: it fell out at this
10231                // gate and re-ran on the materialising path, after the
10232                // deferred join structure had already been built and paid
10233                // for. Measured (round 821, statement_timeout=120 over 400k
10234                // rows): `big.pad` and `b.pad` streamed and cancelled at
10235                // ~65k rows in 0.14 s, while bare `pad` ran to completion in
10236                // 0.80 s with the timeout never consulted. `find_column_pos`
10237                // has always handled the unqualified case (it falls through
10238                // to a by-name match), so the guard narrowed the gate for no
10239                // reason it recorded.
10240                Expr::Column(c) => eval::find_column_pos(c, &ctx),
10241                _ => None,
10242            }
10243        };
10244        let proj_decomposed: Vec<(usize, usize)> = {
10245            let mut out = Vec::with_capacity(projection.len());
10246            for p in &projection {
10247                let Some(abs) = bound_pos(&p.expr) else {
10248                    return Ok(None);
10249                };
10250                let Some(k) = deferred
10251                    .offsets
10252                    .partition_point(|&o| o <= abs)
10253                    .checked_sub(1)
10254                else {
10255                    return Ok(None);
10256                };
10257                out.push((k, abs - deferred.offsets[k]));
10258            }
10259            out
10260        };
10261        // Emit columns once.
10262        let columns: Vec<ColumnSchema> = projection
10263            .iter()
10264            // v7.39 (read01 round 54) — keep the column's enum identity through
10265            // the projection (it lives outside the DataType lattice), or a
10266            // derived table / UNION / windowed result forgets it and any outer
10267            // `ORDER BY <enum col>` silently sorts by the label's TEXT.
10268            .map(|p| p.to_column_schema())
10269            .collect();
10270        emit(crate::StreamItem::Header(&columns))?;
10271        let sources_ref = &deferred.sources;
10272        let stride = deferred.stride;
10273        let survivors_ref = &deferred.survivors;
10274        let n_surv = if stride == 0 {
10275            0
10276        } else {
10277            survivors_ref.len() / stride
10278        };
10279        // Reused per-row cell-ref scratch — pushes are zero-alloc
10280        // after the first row.
10281        let null_value = Value::Null;
10282        let mut cell_refs: Vec<&Value> = Vec::with_capacity(projection.len());
10283        let mut count: usize = 0;
10284        for surv_i in 0..n_surv {
10285            if surv_i.is_multiple_of(256) {
10286                cancel.check()?;
10287            }
10288            let tuple = &survivors_ref[surv_i * stride..(surv_i + 1) * stride];
10289            cell_refs.clear();
10290            for &(k, col_in_src) in &proj_decomposed {
10291                let ri = tuple[k];
10292                let v: &Value = if ri == usize::MAX {
10293                    &null_value
10294                } else {
10295                    sources_ref[k]
10296                        .get(ri)
10297                        .and_then(|r| r.values.get(col_in_src))
10298                        .unwrap_or(&null_value)
10299                };
10300                cell_refs.push(v);
10301            }
10302            emit(crate::StreamItem::Row(crate::RowCells::Refs(&cell_refs)))?;
10303            count += 1;
10304        }
10305        Ok(Some(count))
10306    }
10307
10308    fn exec_joined_select(
10309        &self,
10310        stmt: &SelectStatement,
10311        from: &FromClause,
10312        cancel: CancelToken<'_>,
10313    ) -> Result<QueryResult, EngineError> {
10314        // v7.37.x (docker-fair NOTEX attack) — short-circuit COUNT(*)
10315        // over a LEFT ANTI JOIN. The v7.37.27 NOT EXISTS pullup
10316        // rewrites `SELECT COUNT(*) FROM A WHERE NOT EXISTS (SELECT 1
10317        // FROM B WHERE B.k = A.k)` into
10318        //   SELECT COUNT(*) FROM A LEFT JOIN B ON B.k = A.k
10319        //   WHERE B.k IS NULL
10320        // The general join executor builds a hash, probes every outer
10321        // tuple, materialises (left_padded_with_null) for every miss,
10322        // then runs the aggregate over the result set. For COUNT(*) we
10323        // only need the count — skip the tuple materialisation. Build
10324        // a HashSet of B's unique join values, scan A's PK index, and
10325        // increment the counter on each miss. PG's Merge Anti-Join
10326        // does roughly this; ours becomes a simple HashSet probe.
10327        if let Some(out) = self.try_count_star_left_anti_join_fast(stmt, from)? {
10328            return Ok(out);
10329        }
10330        // v7.34.5 (mailrs prod #5) — walker-driven join + early stop.
10331        // When ORDER BY is on an indexed primary column, walking the
10332        // btree in the requested direction lets the streamer break
10333        // after `LIMIT + OFFSET` survivors without ever materialising
10334        // the rest of the join — the 80 ms `mailrs_prod_not_exists`
10335        // plateau is exactly this shape.
10336        if let Some(out) = self.try_streamed_inner_join_walk_topn(stmt, from, cancel)? {
10337            return Ok(out);
10338        }
10339        // v7.30.3 (mailrs round-26) — the bounded single-join path
10340        // first; peak memory scales with LIMIT instead of the table.
10341        if let Some(out) = self.try_streamed_inner_join_topn(stmt, from, cancel)? {
10342            return Ok(out);
10343        }
10344        // v7.17.0 Phase 3.P0-43 + P0-41 — delegate the join +
10345        // WHERE materialisation to the shared helper so the LATERAL
10346        // / UNNEST / regular-catalog paths route through one place.
10347        // (`build_joined_filtered_rows` carries LATERAL support as
10348        // of Phase 3.P0-41.) Downstream we still handle aggregate /
10349        // projection / ORDER BY / DISTINCT / LIMIT inline because
10350        // those depend on the SelectStatement's items list.
10351        let mut budget = ByteBudget::new(self.max_query_bytes);
10352        let deferred = {
10353            let mut needed = alloc::collections::BTreeSet::new();
10354            let prunable = collect_qualified_refs(stmt, &mut needed).is_some();
10355            self.build_joined_filtered_rows(
10356                from,
10357                stmt.where_.as_ref(),
10358                cancel,
10359                if prunable { Some(&needed) } else { None },
10360                &mut budget,
10361            )?
10362        };
10363        let combined_schema = &deferred.combined_schema;
10364        // v7.39 (read01 round 53) — carry the catalog (see join.rs): a
10365        // `::regclass` / enum cast in a joined projection or HAVING needs it.
10366        // v7.39 (round 525) — and the session: a joined SELECT's WHERE is
10367        // the same predicate the unjoined shape carries.
10368        let joined_sess = self.dml_session();
10369        // v7.38.18 — and the DIALECT. This context carried the catalog and
10370        // the session and not the one field that decides how text
10371        // compares, so a joined row was evaluated in PostgreSQL
10372        // semantics inside a MySQL session.
10373        //
10374        // It showed up only where the two sides had DIFFERENT text types:
10375        // `a.c = b.s` with `c CHAR(8)` and `s TEXT` answered false, and a
10376        // join on it returned no rows, while `a.c = b.c` and `a.s = b.s`
10377        // were fine and the same comparison inside one table was fine.
10378        // Same-type pairs agree byte-for-byte after an ASCII lowercase,
10379        // so the wrong semantics were invisible until a CHAR's padding
10380        // had to be stripped and PostgreSQL's arm does not strip it.
10381        //
10382        // `with_engine` is what sets it; the next line already reaches
10383        // for `self.backslash_escapes`, so the dialect was in hand.
10384        let ctx = EvalContext::new(combined_schema, None)
10385            .with_catalog(self.active_catalog())
10386            .with_engine(self)
10387            .with_session(&joined_sess);
10388        // Aggregate path: handle GROUP BY / aggregate calls over the
10389        // joined+filtered rows.
10390        if aggregate::uses_aggregate_in(stmt, self.speaks_mysql) {
10391            // v7.32 (P4 borrow channel, increment 2) — borrow each
10392            // surviving join tuple as a RowRef::Tuple; the aggregate
10393            // engine reads source cells by reference (bound fast path =
10394            // zero clone) instead of consuming materialised combined
10395            // Rows. This is where the +211k materialise_tuple_vals
10396            // clones disappear for the join+aggregate shape.
10397            let refs = deferred.row_refs();
10398            // v7.29 — a per-query memo so correlated scalar
10399            // subqueries batch-evaluate once (group map) instead of
10400            // executing per group.
10401            let agg_memo = core::cell::RefCell::new(memoize::MemoizeCache::default());
10402            let agg_correlated = |e: &Expr, r: &Row<'static>, c: &EvalContext<'_>| {
10403                self.eval_expr_with_correlated(e, r, c, cancel, Some(&mut agg_memo.borrow_mut()))
10404                    .map_err(|err| match err {
10405                        EngineError::Eval(ev) => ev,
10406                        other => eval::EvalError::TypeMismatch {
10407                            detail: alloc::format!("{other}"),
10408                        },
10409                    })
10410            };
10411            let agg = aggregate::run(
10412                stmt,
10413                crate::join::AggRows::Refs(&refs),
10414                combined_schema,
10415                None,
10416                Some(&agg_correlated),
10417                self.parallel_runner.0.as_deref(),
10418                Some(self.active_catalog()),
10419                Some(self),
10420            )?;
10421            return self.finish_agg_result(agg, stmt, cancel);
10422        }
10423
10424        let projection = build_projection(
10425            &stmt.items,
10426            combined_schema,
10427            "",
10428            self.speaks_mysql,
10429            Some(self.active_catalog()),
10430        )?;
10431        // v7.39 (round 734) — a set-returning projection over a JOIN.
10432        // This executor's projection loop treats every item as a scalar,
10433        // so `SELECT unnest(ARRAY[a.id, b.g]) FROM a JOIN b …` died with
10434        // "function unnest(integer[]) does not exist" where PG expands
10435        // it. The row-set executor already carries the full SRF pipeline
10436        // (lockstep expansion, ORDER-BY-on-expanded-rows, the round-733
10437        // sharding): materialise the joined survivors and hand over. The
10438        // WHERE is cleared — the join already applied it, and combined
10439        // columns resolve identically in both executors.
10440        if !self.srf_target_idxs(&projection).is_empty() {
10441            let refs = deferred.row_refs();
10442            let rows: Vec<Row<'static>> = refs.iter().map(|r| r.as_row().into_owned()).collect();
10443            let mut s2 = stmt.clone();
10444            s2.where_ = None;
10445            let schema = combined_schema.clone();
10446            return self.exec_select_over_rows(&s2, rows, schema, "", cancel);
10447        }
10448        // v7.33 (P4 borrow channel, increment 3) — project directly off
10449        // the deferred row-index tuples instead of materialising an
10450        // intermediate combined Row per survivor. A bound qualified
10451        // column is read by reference (`RowRef::get` → `tuple_value`) and
10452        // cloned ONCE into the output row; the old `materialise()` (a full
10453        // combined Row plus a source→intermediate clone per referenced
10454        // cell, for every survivor) is gone. A row materialises on demand
10455        // only when a projection or ORDER BY expression needs the eval
10456        // path (subquery / function / arithmetic / unqualified column).
10457        // Same bind-once classification the aggregate input fast path uses
10458        // (`accumulate_groups`), reading the same `tuple_value` mapping the
10459        // differential gate already covers.
10460        let refs = deferred.row_refs();
10461        let bound_pos = |e: &Expr| -> Option<usize> {
10462            match e {
10463                Expr::Column(c) if c.qualifier.is_some() => eval::find_column_pos(c, &ctx),
10464                _ => None,
10465            }
10466        };
10467        let proj_pos: Vec<Option<usize>> = projection.iter().map(|p| bound_pos(&p.expr)).collect();
10468        let all_proj_bound = proj_pos.iter().all(Option::is_some);
10469        // v7.36 (perf — mailrs Phase 1, PROJ SPGS 8.93 → ?) —
10470        // pre-decompose each bound projection position into
10471        // `(source_k, col_in_source)` so the per-row column read
10472        // skips the per-cell `tuple_value` partition_point + slice
10473        // walk. For PROJ_25k (5 cols × 25k rows = 125k tuple_value
10474        // calls) that walk dominated; this version reaches into
10475        // `pipe.sources[k].get(tuple[k])?.values[col]` directly.
10476        let proj_decomposed: Vec<Option<(usize, usize)>> = proj_pos
10477            .iter()
10478            .map(|p| {
10479                p.and_then(|abs| {
10480                    let k = deferred
10481                        .offsets
10482                        .partition_point(|&o| o <= abs)
10483                        .checked_sub(1)?;
10484                    Some((k, abs - deferred.offsets[k]))
10485                })
10486            })
10487            .collect();
10488        // v7.39 (round 962) — which projection items are whole-row
10489        // references, and to which join source. The test is
10490        // `locate_column` declining the name, which is the SAME resolver
10491        // the evaluation path uses, so this cannot drift from it: a real
10492        // column carrying an alias's name resolves to a position and is
10493        // not reported here. The source index comes from the alias
10494        // prefix, the way the combined schema names its columns.
10495        let whole_row_src: Vec<Option<usize>> = projection
10496            .iter()
10497            .map(|p| {
10498                let Expr::Column(c) = &p.expr else {
10499                    return None;
10500                };
10501                if !matches!(eval::locate_column(c, &ctx), Ok(None)) {
10502                    return None;
10503                }
10504                let prefix = alloc::format!("{name}.", name = c.name);
10505                let abs = deferred
10506                    .combined_schema
10507                    .iter()
10508                    .position(|s| s.name.starts_with(&prefix))?;
10509                deferred
10510                    .offsets
10511                    .partition_point(|&o| o <= abs)
10512                    .checked_sub(1)
10513            })
10514            .collect();
10515        // ORDER BY (when present) still evaluates against a materialised
10516        // Row — keep the order-key encoder correct rather than fork it.
10517        let need_eval_row = !all_proj_bound || !stmt.order_by.is_empty();
10518        let mut tagged: Vec<(Vec<OrderKey>, Row<'static>)> = Vec::new();
10519        let mut proj_memo = memoize::MemoizeCache::default();
10520        let sources_ref = &deferred.sources;
10521        let stride = deferred.stride;
10522        let survivors_ref = &deferred.survivors;
10523        let n_surv = survivors_ref.len() / stride.max(1);
10524        // v7.38 (read01 B8) — streaming top-N budget (see the sibling
10525        // single-table path). Bounds this JOIN projection's accumulator
10526        // to O(keep) for `ORDER BY … LIMIT k`.
10527        let topk_stream: Option<(usize, Vec<bool>)> = if !stmt.order_by.is_empty()
10528            && !stmt.distinct
10529            && !stmt.limit_with_ties
10530            && !self.env_cfg().disable_topk
10531        {
10532            stmt.limit_literal().and_then(|l| {
10533                let keep = (l as usize).saturating_add(stmt.offset_literal().unwrap_or(0) as usize);
10534                (keep >= 1).then(|| (keep, stmt.order_by.iter().map(|o| o.desc).collect()))
10535            })
10536        } else {
10537            None
10538        };
10539        // v7.37.16 — streaming DISTINCT seen-set (see scan-path twin).
10540        let mut seen_distinct: hashbrown::HashMap<u64, crate::distinct::DistinctBucket> =
10541            hashbrown::HashMap::new();
10542        let distinct_hb = hashbrown::DefaultHashBuilder::default();
10543        // v7.38.13 — which output positions must NOT fold. Built once per
10544        // scan from the projection, which carries the source column's
10545        // byte-wise-ness; see `FoldSpec`.
10546        let distinct_mask = fold_mask(&projection);
10547        for surv_i in 0..n_surv {
10548            let tuple = &survivors_ref[surv_i * stride..(surv_i + 1) * stride];
10549            let row = &refs[surv_i];
10550            let materialised: Option<Cow<'_, Row<'static>>> = if need_eval_row {
10551                Some(row.as_row())
10552            } else {
10553                None
10554            };
10555            let mut values = Vec::with_capacity(projection.len());
10556            for (i, p) in projection.iter().enumerate() {
10557                if let Some((k, col_in_src)) = proj_decomposed[i] {
10558                    // v7.36 — direct (source_k, col) lookup, no
10559                    // partition_point. tuple[k] is the row index in
10560                    // sources[k]; LEFT-NULL slots are `usize::MAX`.
10561                    let ri = tuple[k];
10562                    let v: Value<'static> = if ri == usize::MAX {
10563                        Value::Null
10564                    } else {
10565                        sources_ref[k]
10566                            .get(ri)
10567                            .and_then(|r| r.values.get(col_in_src))
10568                            .cloned()
10569                            .map(Value::into_owned)
10570                            .unwrap_or(Value::Null)
10571                    };
10572                    values.push(v);
10573                } else if let Some(pos) = proj_pos[i] {
10574                    // Bound but couldn't decompose (shouldn't normally
10575                    // happen — keep as a safe path).
10576                    values.push(
10577                        row.get(pos)
10578                            .cloned()
10579                            .map(Value::into_owned)
10580                            .unwrap_or(Value::Null),
10581                    );
10582                } else if let Some(k) = whole_row_src[i]
10583                    && tuple[k] == usize::MAX
10584                {
10585                    // v7.39 (round 962) — a whole-row reference to a side
10586                    // an OUTER join null-extended is NULL, not a
10587                    // composite whose fields are all NULL. PG18.4 answers
10588                    // `SELECT jb FROM wr LEFT JOIN jb ON <no match>` with
10589                    // an empty cell; round 961 answered `(,)`.
10590                    //
10591                    // The evaluator below cannot tell the two apart: it
10592                    // reads the MATERIALISED combined row, where a
10593                    // null-extended side is indistinguishable from a real
10594                    // row whose every column is NULL — and that row is
10595                    // `(,)` in PG too, so guessing by "all fields NULL"
10596                    // would trade one wrong answer for another. The
10597                    // tuple, which is still in hand here, does know:
10598                    // `usize::MAX` is the sentinel the join writes for
10599                    // exactly this.
10600                    values.push(Value::Null);
10601                } else {
10602                    // Eval path — `materialised` is Some whenever any
10603                    // projection item is non-bound (need_eval_row true).
10604                    // v7.24 (round-16 B) — select-list subqueries under a
10605                    // JOIN go through the correlated-aware evaluator too.
10606                    let mrow = materialised.as_deref().expect("materialised for eval");
10607                    values.push(self.eval_expr_with_correlated(
10608                        &p.expr,
10609                        mrow,
10610                        &ctx,
10611                        cancel,
10612                        Some(&mut proj_memo),
10613                    )?);
10614                }
10615            }
10616            let out_row = Row::new(values);
10617            // v7.37.16 — streaming DISTINCT (see the scan-path twin):
10618            // probe on the projected row; duplicates skip the
10619            // build_order_keys eval and never enter `tagged`.
10620            if stmt.distinct {
10621                let bucket = seen_distinct
10622                    .entry(norm_hash_row(
10623                        &out_row,
10624                        &distinct_hb,
10625                        FoldSpec::of(ctx.mysql_dialect, &distinct_mask),
10626                    ))
10627                    .or_default();
10628                if bucket.iter().any(|i| {
10629                    row_eq_norm(
10630                        &tagged[i].1,
10631                        &out_row,
10632                        FoldSpec::of(ctx.mysql_dialect, &distinct_mask),
10633                    )
10634                }) {
10635                    continue;
10636                }
10637                bucket.push(tagged.len());
10638            }
10639            let order_keys = if stmt.order_by.is_empty() {
10640                Vec::new()
10641            } else {
10642                let mrow = materialised.as_deref().expect("materialised for order by");
10643                build_order_keys(&stmt.order_by, mrow, &ctx)?
10644            };
10645            budget.charge(approx_row_bytes(&out_row))?;
10646            tagged.push((order_keys, out_row));
10647            if let Some((k, descs)) = &topk_stream {
10648                topk_trim(&mut tagged, *k, descs);
10649            }
10650        }
10651        if !stmt.order_by.is_empty() {
10652            // v7.38 元机制 D acceptor — see other call site above.
10653            let keep = if self.env_cfg().disable_topk {
10654                None
10655            } else {
10656                stmt.limit_literal()
10657                    .map(|l| l as usize + stmt.offset_literal().map_or(0, |o| o as usize))
10658            };
10659            let descs: Vec<bool> = stmt.order_by.iter().map(|o| o.desc).collect();
10660            // v7.39 (round 688) — the join's ORDER BY resolves its keys
10661            // against `ctx`, which is built from `build_combined_schema`, so
10662            // this is where a declared collation reaches the sort. There was
10663            // exactly ONE resolver call in the engine before this — the
10664            // single-table scan's — which is why every other shape sorted by
10665            // bytes no matter what the schemas carried.
10666            let colls = crate::orderby::order_by_collations(&stmt.order_by, &ctx)?;
10667            crate::orderby::partial_sort_tagged_in(&mut tagged, keep, &descs, &colls);
10668        }
10669        let mut output_rows: Vec<Row<'static>> = tagged.into_iter().map(|(_, r)| r).collect();
10670        apply_offset_and_limit(
10671            &mut output_rows,
10672            stmt.offset_literal(),
10673            stmt.limit_literal(),
10674        );
10675        let columns: Vec<ColumnSchema> = projection
10676            .into_iter()
10677            .map(|p| p.to_column_schema())
10678            .collect();
10679        Ok(QueryResult::Rows {
10680            columns,
10681            rows: output_rows,
10682        })
10683    }
10684}
10685
10686impl Engine {
10687    /// v6.10.2 — cold-tier time-travel scan. Resolves the segment
10688    /// by id, decodes each row body against the table's current
10689    /// schema, applies the SELECT's projection + optional WHERE +
10690    /// optional LIMIT, returns a `Rows` result. JOINs / aggregates
10691    /// / ORDER BY are unsupported on this path (STABILITY carve-
10692    /// out); operators wanting them should restore the segment
10693    /// into a regular table first.
10694    fn exec_select_as_of_segment(
10695        &self,
10696        stmt: &SelectStatement,
10697        from: &spg_sql::ast::FromClause,
10698        segment_id: u32,
10699    ) -> Result<QueryResult, EngineError> {
10700        // v6.10.2 scope: no joins, no aggregates, no ORDER BY,
10701        // no GROUP BY / HAVING / UNION / OFFSET / DISTINCT.
10702        if !from.joins.is_empty()
10703            || stmt.group_by.is_some()
10704            || stmt.having.is_some()
10705            || !stmt.unions.is_empty()
10706            || !stmt.order_by.is_empty()
10707            || stmt.offset.is_some()
10708            || stmt.distinct
10709            || aggregate::uses_aggregate_in(stmt, self.speaks_mysql)
10710        {
10711            return Err(EngineError::Unsupported(
10712                "AS OF SEGMENT supports SELECT projection + WHERE + LIMIT only \
10713                 (joins / aggregates / ORDER BY are STABILITY § \"Out of v6.10\")"
10714                    .into(),
10715            ));
10716        }
10717        let table = self
10718            .active_catalog()
10719            .get(&from.primary.name)
10720            .ok_or_else(|| StorageError::TableNotFound {
10721                name: from.primary.name.clone(),
10722            })?;
10723        let schema = table.schema().clone();
10724        let schema_cols = &schema.columns;
10725        let alias = from
10726            .primary
10727            .alias
10728            .as_deref()
10729            .unwrap_or(from.primary.name.as_str());
10730        let ctx = self.ev_ctx(schema_cols, Some(alias));
10731        let seg = self
10732            .active_catalog()
10733            .cold_segment(segment_id)
10734            .ok_or_else(|| {
10735                EngineError::Unsupported(alloc::format!(
10736                    "AS OF SEGMENT: cold segment {segment_id} not registered"
10737                ))
10738            })?;
10739        let mut out_rows: Vec<Row<'static>> = Vec::new();
10740        let mut limit_remaining: Option<usize> =
10741            stmt.limit_literal().and_then(|n| usize::try_from(n).ok());
10742        for (_key, body) in seg.scan() {
10743            let (row, _consumed) =
10744                spg_storage::decode_row_body_dense(&body, &schema, seg.codec_version())
10745                    .map_err(EngineError::Storage)?;
10746            if let Some(where_expr) = &stmt.where_ {
10747                let cond = self.eval_expr_simple(where_expr, &row, &ctx)?;
10748                if !crate::eval::predicate_is_true(&cond, "WHERE", ctx.mysql_dialect)? {
10749                    continue;
10750                }
10751            }
10752            // Projection.
10753            let projected = self.project_row_simple(&row, &stmt.items, schema_cols, alias)?;
10754            out_rows.push(projected);
10755            if let Some(rem) = limit_remaining.as_mut() {
10756                if *rem == 0 {
10757                    out_rows.pop();
10758                    break;
10759                }
10760                *rem -= 1;
10761            }
10762        }
10763        // Output column schema: derive from SELECT items.
10764        let columns = self.derive_output_columns(&stmt.items, schema_cols, alias);
10765        Ok(QueryResult::Rows {
10766            columns,
10767            rows: out_rows,
10768        })
10769    }
10770
10771    /// v6.10.2 — simple-path WHERE eval that doesn't go through
10772    /// the correlated-subquery / Memoize machinery. AS OF SEGMENT
10773    /// scan paths predicate against a snapshot frozen segment, no
10774    /// cross-row state.
10775    fn eval_expr_simple(
10776        &self,
10777        expr: &Expr,
10778        row: &Row<'static>,
10779        ctx: &EvalContext,
10780    ) -> Result<Value<'static>, EngineError> {
10781        let cancel = CancelToken::none();
10782        self.eval_expr_with_correlated(expr, row, ctx, cancel, None)
10783    }
10784}
10785
10786// ---- SELECT result / projection / generate-series / SRF helpers (lib.rs split 12) ----
10787
10788/// One row-producing projection: an expression to evaluate, the resulting
10789/// column's user-visible name, its inferred type, and nullability.
10790#[derive(Debug, Clone)]
10791pub(crate) struct ProjectedItem {
10792    pub(crate) expr: Expr,
10793    pub(crate) output_name: String,
10794    pub(crate) ty: DataType,
10795    pub(crate) nullable: bool,
10796    /// v7.39 (read01 round 54) — a projected enum column keeps its enum
10797    /// identity. Enum-ness lives outside the DataType lattice (the value is a
10798    /// Text), so a projection that dropped this made the RESULT schema forget
10799    /// it — and a UNION's combined `ORDER BY <enum col>`, which sorts against
10800    /// that schema, silently fell back to TEXT order instead of member order.
10801    pub(crate) user_enum_type: Option<String>,
10802    /// v7.39 (round 425) — a projected MySQL temporal column keeps its
10803    /// declared fractional-seconds precision, so the renderer can pad to
10804    /// exactly that many digits (`DATETIME(3)` shows `.250`, and `.000` for
10805    /// a whole second). Like `user_enum_type` this lives outside the
10806    /// DataType lattice, so a projection that dropped it made the RESULT
10807    /// schema forget how wide the fraction should print.
10808    pub(crate) mysql_fsp: Option<u8>,
10809    /// v7.39 (round 688) — and its declared collation, the third thing to
10810    /// live outside the DataType lattice and the third to be lost the same
10811    /// way. Measured: `SELECT a.loc FROM a JOIN b … ORDER BY a.loc` over a
10812    /// column declared `COLLATE "en_US.utf8"` sorted by bytes, because the
10813    /// projection rebuilt the output column and the ORDER BY resolves
10814    /// against THAT schema.
10815    pub(crate) collation_name: Option<String>,
10816    /// v7.38.13 — and whether this position must NOT fold when DISTINCT
10817    /// de-dups it. The fourth thing to live outside the DataType lattice
10818    /// and the fourth to be lost the same way: a column declared
10819    /// `COLLATE utf8mb4_bin` is byte-wise, `SELECT DISTINCT t` folded it
10820    /// anyway, and `'a'` and `'A'` came back as one row where MariaDB 11
10821    /// returns two.
10822    ///
10823    /// A BOOL rather than the `Collation` enum on purpose. The enum's
10824    /// storage default is `Binary`, but the FOLD default under MySQL is
10825    /// case-insensitive — carrying the enum would silently mean
10826    /// "exempt" for every projected expression that is not a column.
10827    /// This field states the question it answers.
10828    pub(crate) fold_exempt: bool,
10829    /// v7.38.18 — does this column's collation make trailing spaces
10830    /// insignificant? A separate question from `fold_exempt`:
10831    /// `utf8mb4_bin` is fold-exempt AND pads, `utf8mb4_0900_ai_ci`
10832    /// folds and does not. Read off the same column, at the same
10833    /// place, so the two masks cannot drift apart.
10834    pub(crate) pads: bool,
10835}
10836
10837impl ProjectedItem {
10838    /// v7.38.14 — the output column this projected item describes.
10839    ///
10840    /// There were TWENTY-ONE places converting a `ProjectedItem` into a
10841    /// `ColumnSchema`, each written as `ColumnSchema::new(..)` followed by a
10842    /// hand-picked list of attributes to copy after it, and the lists did not
10843    /// agree: six carried enum identity, the collation NAME and MySQL fsp; ten
10844    /// carried the first and last but not the name; five carried nothing at
10845    /// all. Not one carried `collation`, the enum every MySQL text comparison
10846    /// actually reads.
10847    ///
10848    /// That is how a declared collation vanished between a subquery and the
10849    /// query that selects from it: the inner SELECT's output schema claimed
10850    /// `ColumnSchema::new`'s default, which is `Binary` — a value downstream
10851    /// reads as "byte-wise ON PURPOSE" rather than as "unknown", so the loss
10852    /// presents as a deliberate declaration.
10853    ///
10854    /// One conversion, so a field added to either type has one place to be
10855    /// remembered instead of twenty-one.
10856    pub(crate) fn to_column_schema(&self) -> ColumnSchema {
10857        let mut c = ColumnSchema::new(self.output_name.clone(), self.ty, self.nullable);
10858        c.user_enum_type.clone_from(&self.user_enum_type);
10859        c.collation_name.clone_from(&self.collation_name);
10860        c.mysql_fsp = self.mysql_fsp;
10861        // `fold_exempt` is the projection's answer to the same question
10862        // `ColumnSchema::collation` answers downstream, and it was computed
10863        // from the source column. Keeping the two in step here is what stops
10864        // a de-duplication site further on from asking the schema and being
10865        // told the opposite of what the projection knew.
10866        c.collation = if self.fold_exempt {
10867            spg_storage::Collation::Binary
10868        } else {
10869            spg_storage::Collation::CaseInsensitive
10870        };
10871        c
10872    }
10873}
10874
10875/// Dedupe a row set, preserving first-seen order. `Row`'s `PartialEq` is
10876/// structural (`Vec<Value<'static>>` ⇒ pairwise `Value` equality), which gives SQL
10877/// `NULL = NULL → TRUE` and `NaN = NaN → FALSE`. The first agrees with
10878/// the spec's "two NULLs are not distinct"; the second is a tolerated
10879/// quirk for v1 (no NaN literals are reachable from the SQL surface).
10880/// v7.37 D.23 — is this expression a bare (non-window) aggregate call?
10881fn expr_is_aggregate_call(e: &Expr) -> bool {
10882    match e {
10883        Expr::FunctionCall { name, .. } => crate::aggregate::is_aggregate_name(name),
10884        Expr::AggregateOrdered { .. } => true,
10885        _ => false,
10886    }
10887}
10888
10889/// Collect distinct top-level aggregate call expressions (dedup by value). Does
10890/// not recurse into an aggregate's own args (it's hoisted whole). Reuses the same
10891/// pragmatic variant set as `rewrite_window_to_columns`; aggregates nested in
10892/// uncovered variants simply aren't hoisted (the query keeps erroring, no worse
10893/// than today — never a regression on a working query).
10894fn collect_agg_exprs(e: &Expr, out: &mut Vec<Expr>) {
10895    if expr_is_aggregate_call(e) {
10896        if !out.iter().any(|x| x == e) {
10897            out.push(e.clone());
10898        }
10899        return;
10900    }
10901    match e {
10902        Expr::Binary { lhs, rhs, .. } => {
10903            collect_agg_exprs(lhs, out);
10904            collect_agg_exprs(rhs, out);
10905        }
10906        Expr::Unary { expr, .. }
10907        | Expr::Cast { expr, .. }
10908        | Expr::IsNull { expr, .. }
10909        | Expr::BoolTest { expr, .. }
10910        | Expr::FieldAccess { base: expr, .. } => collect_agg_exprs(expr, out),
10911        Expr::FunctionCall { args, .. } => {
10912            for a in args {
10913                collect_agg_exprs(a, out);
10914            }
10915        }
10916        Expr::Like { expr, pattern, .. } => {
10917            collect_agg_exprs(expr, out);
10918            collect_agg_exprs(pattern, out);
10919        }
10920        Expr::Extract { source, .. } => collect_agg_exprs(source, out),
10921        Expr::WindowFunction {
10922            args,
10923            partition_by,
10924            order_by,
10925            ..
10926        } => {
10927            for a in args {
10928                collect_agg_exprs(a, out);
10929            }
10930            for p in partition_by {
10931                collect_agg_exprs(p, out);
10932            }
10933            for (o, _, _) in order_by {
10934                collect_agg_exprs(o, out);
10935            }
10936        }
10937        _ => {}
10938    }
10939}
10940
10941/// Replace each aggregate call in `aggs` with a `Column(__aggN)` reference.
10942fn replace_agg_exprs(e: &mut Expr, aggs: &[Expr]) {
10943    if expr_is_aggregate_call(e) {
10944        if let Some(idx) = aggs.iter().position(|x| x == e) {
10945            *e = Expr::Column(ColumnName {
10946                qualifier: None,
10947                name: alloc::format!("__agg{idx}"),
10948            });
10949        }
10950        return;
10951    }
10952    match e {
10953        Expr::Binary { lhs, rhs, .. } => {
10954            replace_agg_exprs(lhs, aggs);
10955            replace_agg_exprs(rhs, aggs);
10956        }
10957        Expr::Unary { expr, .. }
10958        | Expr::Cast { expr, .. }
10959        | Expr::IsNull { expr, .. }
10960        | Expr::BoolTest { expr, .. }
10961        | Expr::FieldAccess { base: expr, .. } => replace_agg_exprs(expr, aggs),
10962        Expr::FunctionCall { args, .. } => {
10963            for a in args {
10964                replace_agg_exprs(a, aggs);
10965            }
10966        }
10967        Expr::Like { expr, pattern, .. } => {
10968            replace_agg_exprs(expr, aggs);
10969            replace_agg_exprs(pattern, aggs);
10970        }
10971        Expr::Extract { source, .. } => replace_agg_exprs(source, aggs),
10972        Expr::WindowFunction {
10973            args,
10974            partition_by,
10975            order_by,
10976            ..
10977        } => {
10978            for a in args {
10979                replace_agg_exprs(a, aggs);
10980            }
10981            for p in partition_by {
10982                replace_agg_exprs(p, aggs);
10983            }
10984            for (o, _, _) in order_by {
10985                replace_agg_exprs(o, aggs);
10986            }
10987        }
10988        _ => {}
10989    }
10990}
10991
10992/// v7.37 D.23 — window functions run AFTER GROUP BY aggregation. Rewrite
10993/// `SELECT g, sum(v), rank() OVER (ORDER BY sum(v)) FROM t GROUP BY g` into an
10994/// aggregate derived subquery (`SELECT g, sum(v) AS __agg0 FROM t GROUP BY g`) +
10995/// an outer window query over it (`SELECT g, __agg0, rank() OVER (ORDER BY
10996/// __agg0) FROM (...) __aggwin`), which the window-over-derived path (D.13) runs.
10997/// Returns None outside the bounded subset (leaves current behaviour). Only fires
10998/// on the currently-erroring agg+window+GROUP BY shape → cannot regress working
10999/// window-only / aggregate-only queries.
11000fn rewrite_agg_before_window(stmt: &SelectStatement) -> Option<SelectStatement> {
11001    if !(crate::aggregate::uses_aggregate(stmt) || stmt.group_by.is_some()) {
11002        return None;
11003    }
11004    // Bounded subset: no set-ops; GROUP BY keys must be simple columns.
11005    if !stmt.unions.is_empty() {
11006        return None;
11007    }
11008    let group_cols: Vec<Expr> = stmt.group_by.clone().unwrap_or_default();
11009    if group_cols.iter().any(|g| !matches!(g, Expr::Column(_))) {
11010        return None;
11011    }
11012    stmt.from.as_ref()?;
11013    // Collect the aggregate calls to hoist from projection + outer ORDER BY.
11014    let mut aggs: Vec<Expr> = Vec::new();
11015    for item in &stmt.items {
11016        if let SelectItem::Expr { expr, .. } = item {
11017            collect_agg_exprs(expr, &mut aggs);
11018        }
11019    }
11020    for ob in &stmt.order_by {
11021        collect_agg_exprs(&ob.expr, &mut aggs);
11022    }
11023    // Inner aggregate subquery: group cols (by name) + each aggregate as __aggN.
11024    let mut inner_items: Vec<SelectItem> = Vec::new();
11025    for g in &group_cols {
11026        inner_items.push(SelectItem::Expr {
11027            expr: g.clone(),
11028            alias: None,
11029        });
11030    }
11031    for (i, a) in aggs.iter().enumerate() {
11032        inner_items.push(SelectItem::Expr {
11033            expr: a.clone(),
11034            alias: Some(alloc::format!("__agg{i}")),
11035        });
11036    }
11037    let inner = SelectStatement {
11038        items: inner_items,
11039        distinct: false,
11040        distinct_on: Vec::new(),
11041        unions: Vec::new(),
11042        order_by: Vec::new(),
11043        limit: None,
11044        offset: None,
11045        limit_with_ties: false,
11046        window_check_exprs: Vec::new(),
11047        ..stmt.clone()
11048    };
11049    let derived = TableRef {
11050        name: "__aggwin".into(),
11051        alias: Some("__aggwin".into()),
11052        only: false,
11053        as_of_segment: None,
11054        unnest_expr: None,
11055        unnest_column_aliases: Vec::new(),
11056        with_ordinality: false,
11057        generate_series_args: None,
11058        lateral_subquery: Some(alloc::boxed::Box::new(inner)),
11059        jsonb_each_text_arg: None,
11060        table_fn_call: None,
11061        rows_from: None,
11062        json_table: None,
11063        scalar_fn_item: false,
11064    };
11065    // Outer window query over the derived rows: aggregates → __aggN column refs.
11066    let mut outer_items = stmt.items.clone();
11067    for item in &mut outer_items {
11068        if let SelectItem::Expr { expr, alias } = item {
11069            // Preserve PG's column label for a bare aggregate projection.
11070            if alias.is_none()
11071                && let Expr::FunctionCall { name, .. } = expr
11072                && crate::aggregate::is_aggregate_name(name)
11073            {
11074                *alias = Some(name.to_ascii_lowercase());
11075            }
11076            replace_agg_exprs(expr, &aggs);
11077        }
11078    }
11079    let mut outer_order = stmt.order_by.clone();
11080    for ob in &mut outer_order {
11081        replace_agg_exprs(&mut ob.expr, &aggs);
11082    }
11083    let mut outer_distinct_on = stmt.distinct_on.clone();
11084    for e in &mut outer_distinct_on {
11085        replace_agg_exprs(e, &aggs);
11086    }
11087    Some(SelectStatement {
11088        locking: None,
11089        ctes: Vec::new(),
11090        distinct: stmt.distinct,
11091        distinct_on: outer_distinct_on,
11092        items: outer_items,
11093        from: Some(FromClause {
11094            primary: derived,
11095            joins: Vec::new(),
11096        }),
11097        where_: None,
11098        group_by: None,
11099        group_by_all: false,
11100        having: None,
11101        unions: Vec::new(),
11102        order_by: outer_order,
11103        limit: stmt.limit.clone(),
11104        offset: stmt.offset.clone(),
11105        limit_with_ties: stmt.limit_with_ties,
11106        window_check_exprs: Vec::new(),
11107    })
11108}
11109
11110/// v7.39 (round 591) — the right-hand side of a set operation, bucketed for
11111/// membership.
11112///
11113/// INTERSECT, EXCEPT and their ALL forms all ask "is this left row over
11114/// there?", and all four answered by scanning the whole right side once per
11115/// left row. The cost was (left rows x right rows), which is why
11116/// `500k INTERSECT 1000` took 1.67 s while the same two inputs the other way
11117/// round took 20 ms: a left row that MATCHES stops the scan early, and a left
11118/// row that does not pays for all of it. Over 100k left rows, raising the
11119/// right side from 100 to 10,000 took 35 ms to 2848.
11120///
11121/// This is the shape round 485 already solved for DISTINCT, and it reuses
11122/// that machinery: bucket by `norm_hash_row`, whose only guarantee is the one
11123/// needed here — rows `row_eq_norm` calls equal hash the same — and settle
11124/// every bucket with the exact comparator, so a collision costs time and
11125/// never an answer.
11126struct PeerIndex<'r> {
11127    bh: hashbrown::DefaultHashBuilder,
11128    buckets: hashbrown::HashMap<u64, Vec<usize>>,
11129    rows: &'r [Row<'static>],
11130    fold: FoldSpec<'r>,
11131}
11132
11133impl<'r> PeerIndex<'r> {
11134    fn build(rows: &'r [Row<'static>], fold: FoldSpec<'r>) -> Self {
11135        // ONE hasher for the whole pass: the default builder is seeded per
11136        // instance, so a fresh one per row would put equal rows in different
11137        // buckets.
11138        let bh = hashbrown::DefaultHashBuilder::default();
11139        let mut buckets: hashbrown::HashMap<u64, Vec<usize>> =
11140            hashbrown::HashMap::with_capacity(rows.len());
11141        for (i, r) in rows.iter().enumerate() {
11142            buckets
11143                .entry(norm_hash_row(r, &bh, fold))
11144                .or_default()
11145                .push(i);
11146        }
11147        Self {
11148            bh,
11149            buckets,
11150            rows,
11151            fold,
11152        }
11153    }
11154
11155    fn contains(&self, r: &Row<'static>) -> bool {
11156        let h = norm_hash_row(r, &self.bh, self.fold);
11157        self.buckets
11158            .get(&h)
11159            .is_some_and(|b| b.iter().any(|&i| row_eq_norm(&self.rows[i], r, self.fold)))
11160    }
11161
11162    /// Remove ONE occurrence, so the multiset forms cancel row for row the
11163    /// way the pool they replaced did.
11164    fn take_one(&mut self, r: &Row<'static>) -> bool {
11165        let h = norm_hash_row(r, &self.bh, self.fold);
11166        let Some(b) = self.buckets.get_mut(&h) else {
11167            return false;
11168        };
11169        let Some(pos) = b
11170            .iter()
11171            .position(|&i| row_eq_norm(&self.rows[i], r, self.fold))
11172        else {
11173            return false;
11174        };
11175        b.swap_remove(pos);
11176        true
11177    }
11178}
11179
11180pub(crate) fn dedup_rows(rows: Vec<Row<'static>>, fold: FoldSpec<'_>) -> Vec<Row<'static>> {
11181    dedup_by_row(rows, |r| r, fold)
11182}
11183
11184/// v7.37.16 — hash-bucketed DISTINCT. The old `out.iter().any(row_eq_norm)`
11185/// was O(n·u) — `SELECT DISTINCT v` over 50 k rows with ~39 k unique values
11186/// ran 4 SECONDS (80 µs/row) vs PG's ~5 ms. Bucket rows by `norm_hash_row`
11187/// and run the exact `row_eq_norm` only within a bucket: first-occurrence
11188/// order is preserved, and correctness needs only the one-way guarantee
11189/// "row_eq_norm-Equal ⇒ equal hash" (collisions are re-checked exactly).
11190/// Small inputs keep the linear scan — no hasher setup for a 10-row page.
11191fn dedup_by_row<T>(
11192    items: Vec<T>,
11193    row_of: impl Fn(&T) -> &Row<'static>,
11194    fold: FoldSpec<'_>,
11195) -> Vec<T> {
11196    if items.len() <= 32 {
11197        let mut out: Vec<T> = Vec::with_capacity(items.len());
11198        for it in items {
11199            if !out
11200                .iter()
11201                .any(|seen| row_eq_norm(row_of(seen), row_of(&it), fold))
11202            {
11203                out.push(it);
11204            }
11205        }
11206        return out;
11207    }
11208    // ONE BuildHasher instance for the whole pass — the default builder
11209    // is randomly seeded PER INSTANCE, so a fresh one per row would give
11210    // equal rows different hashes and never dedup.
11211    let bh = hashbrown::DefaultHashBuilder::default();
11212    let mut out: Vec<T> = Vec::with_capacity(items.len().min(1024));
11213    let mut buckets: hashbrown::HashMap<u64, crate::distinct::DistinctBucket> =
11214        hashbrown::HashMap::with_capacity(items.len());
11215    for it in items {
11216        let h = norm_hash_row(row_of(&it), &bh, fold);
11217        let bucket = buckets.entry(h).or_default();
11218        if !bucket
11219            .iter()
11220            .any(|i| row_eq_norm(row_of(&out[i]), row_of(&it), fold))
11221        {
11222            bucket.push(out.len());
11223            out.push(it);
11224        }
11225    }
11226    out
11227}
11228
11229/// Hash companion to [`row_eq_norm`]. Guarantees only the direction dedup
11230/// needs: rows that `row_eq_norm` deems Equal hash identically; DISTINCT
11231/// rows may collide (buckets are re-checked with the exact comparator).
11232///
11233/// Domain design mirrors `value_cmp`'s equivalence classes:
11234/// - The numeric family (SmallInt/Int/BigInt/Float/Numeric/NumericBig)
11235///   shares one domain: a value that is an integer fitting i64 hashes the
11236///   i64 (so `Int(1)`, `BigInt(1)`, `Float(1.0)`, `Numeric(1.00)` agree);
11237///   anything else hashes the f64 approximation computed by THE SAME
11238///   formula the value_cmp float arms use (`numeric_to_f64`), so
11239///   `Numeric(0.5) == Float(0.5)` agree bit-for-bit. NaN (any family)
11240///   hashes a constant; ±Inf hash their f64 bits; -0.0 folds into 0.0.
11241///   Known un-closable corner: an integer in [2^53, 2^63) can compare
11242///   Equal to a float via value_cmp's lossy f64 arm while hashing in the
11243///   exact-i64 domain — mixed int/float rows at that magnitude may miss a
11244///   dedup (PG itself compares int8↔float8 in the lossy float8 domain).
11245/// - Text and BpChar share a trailing-blank-trimmed byte domain (value_cmp
11246///   compares them blank-insensitively; plain Text pairs that differ only
11247///   in trailing blanks merely collide and are separated exactly).
11248/// - Families value_cmp compares exactly (Bool/Date/Time/Timestamp/…)
11249///   hash their fields under a distinct tag.
11250/// - Everything value_cmp falls back to debug-format ordering for
11251///   (Json, arrays, vectors, geometry, ranges, …) shares one constant
11252///   bucket — degrades to the exact linear scan, never wrong.
11253fn norm_hash_row(
11254    row: &Row<'static>,
11255    bh: &hashbrown::DefaultHashBuilder,
11256    fold: FoldSpec<'_>,
11257) -> u64 {
11258    norm_hash_values(&row.values, bh, fold)
11259}
11260
11261/// v7.39 (round 485) — the same hash over a bare value slice, so the
11262/// DISTINCT probe can run against a reused buffer instead of demanding a
11263/// `Row` that has to be allocated first (see `values_eq_norm`).
11264fn norm_hash_values(
11265    values: &[Value<'static>],
11266    bh: &hashbrown::DefaultHashBuilder,
11267    fold: FoldSpec<'_>,
11268) -> u64 {
11269    use core::hash::{BuildHasher, Hash, Hasher};
11270    let mut h = bh.build_hasher();
11271    for (i, v) in values.iter().enumerate() {
11272        // v7.39 (round 410) — hash the folded key when the MySQL collation
11273        // deduplicates a text value, so `row_eq_norm`-equal rows (`'a'` vs
11274        // `'A'` vs `'a '`) share a hash bucket.
11275        //
11276        // v7.38.13 — per POSITION, in lockstep with `values_eq_norm`. A
11277        // byte-wise column that folded here while the comparator did not
11278        // would scatter equal rows across buckets and stop de-duplicating
11279        // at all; the hash and the comparator have to read the same mask.
11280        if fold.folds(i)
11281            && let Some(folded) = mysql_dedup_fold(v, fold.pads_at(i))
11282        {
11283            folded.hash(&mut h);
11284            continue;
11285        }
11286        norm_hash_value(v, &mut h);
11287    }
11288    h.finish()
11289}
11290
11291/// r1044 — `10^p` as an `i128`, or `None` past what one holds.
11292///
11293/// `i128::MAX` is about 1.7e38, so 10^38 is the last power that fits.
11294const fn pow10_i128(p: u16) -> Option<i128> {
11295    const P: [i128; 39] = {
11296        let mut t = [1i128; 39];
11297        let mut i = 1;
11298        while i < 39 {
11299            t[i] = t[i - 1] * 10;
11300            i += 1;
11301        }
11302        t
11303    };
11304    if (p as usize) < P.len() {
11305        Some(P[p as usize])
11306    } else {
11307        None
11308    }
11309}
11310
11311fn norm_hash_value<H: core::hash::Hasher>(v: &Value<'static>, h: &mut H) {
11312    const TAG_NULL: u8 = 0;
11313    const TAG_BOOL: u8 = 1;
11314    const TAG_NUM_I64: u8 = 2;
11315    const TAG_NUM_F64: u8 = 3;
11316    const TAG_TEXT: u8 = 4;
11317    const TAG_DATE: u8 = 6;
11318    const TAG_TIME: u8 = 7;
11319    const TAG_TIMESTAMP: u8 = 8;
11320    const TAG_TIMETZ: u8 = 10;
11321    const TAG_UUID: u8 = 11;
11322    const TAG_MONEY: u8 = 12;
11323    const TAG_BYTES: u8 = 13;
11324    const TAG_INTERVAL: u8 = 14;
11325    const TAG_CHAR1: u8 = 15;
11326    const TAG_OPAQUE: u8 = 255;
11327    // One shared writer for the numeric family: an integer value
11328    // representable as i64 goes exact (round-trip probe — no_std, so no
11329    // f64::trunc); otherwise the f64 approximation. -0.0 round-trips
11330    // through 0i64, folding it into 0.0 as value_cmp requires.
11331    let num_f64 = |h: &mut H, x: f64| {
11332        if x.is_nan() {
11333            h.write_u8(TAG_NUM_F64);
11334            h.write_u64(0x7ff8_dead_beef_0001); // one bucket for every NaN
11335            return;
11336        }
11337        const TWO63: f64 = 9_223_372_036_854_775_808.0;
11338        if (-TWO63..TWO63).contains(&x) {
11339            #[allow(clippy::cast_possible_truncation)]
11340            let n = x as i64;
11341            #[allow(clippy::cast_precision_loss)]
11342            if (n as f64) == x {
11343                h.write_u8(TAG_NUM_I64);
11344                h.write_i64(n);
11345                return;
11346            }
11347        }
11348        h.write_u8(TAG_NUM_F64);
11349        h.write_u64(x.to_bits());
11350    };
11351    match v {
11352        Value::Null => h.write_u8(TAG_NULL),
11353        Value::Bool(b) => {
11354            h.write_u8(TAG_BOOL);
11355            h.write_u8(u8::from(*b));
11356        }
11357        Value::SmallInt(n) => {
11358            h.write_u8(TAG_NUM_I64);
11359            h.write_i64(i64::from(*n));
11360        }
11361        Value::Int(n) => {
11362            h.write_u8(TAG_NUM_I64);
11363            h.write_i64(i64::from(*n));
11364        }
11365        Value::BigInt(n) => {
11366            h.write_u8(TAG_NUM_I64);
11367            h.write_i64(*n);
11368        }
11369        Value::Float(x) => num_f64(h, *x),
11370        Value::Numeric {
11371            scaled,
11372            scale,
11373            kind,
11374        } => match kind {
11375            spg_storage::NumericKind::NaN => num_f64(h, f64::NAN),
11376            spg_storage::NumericKind::PosInf => num_f64(h, f64::INFINITY),
11377            spg_storage::NumericKind::NegInf => num_f64(h, f64::NEG_INFINITY),
11378            spg_storage::NumericKind::Finite => {
11379                // Reduce trailing fractional zeros so 1.50 and 1.5 share a
11380                // representation, then: exact integers fitting i64 go to the
11381                // i64 domain; everything else uses numeric_to_f64 — the SAME
11382                // formula value_cmp's Numeric↔Float arm compares with.
11383                // r1044 — the reduction is required (`1.5` and `1.50` are
11384                // one value and must land in one bucket) and it used to
11385                // walk one digit at a time. That is O(scale), and scale
11386                // is not small in practice: `n / 100` on a NUMERIC
11387                // column stores `9.1900000000000000`, scale 16, so the
11388                // loop ran fourteen times PER ROW.
11389                //
11390                // Priced by ablation rather than guessed at — removing
11391                // the loop entirely took `SELECT DISTINCT n FROM t ORDER
11392                // BY n` over 400,000 rows from 52 ms to 14.8, against
11393                // PostgreSQL's 12.2-13.8. Two `pow10` lookup tables
11394                // tried first moved it not at all, which is why this one
11395                // was measured before it was written.
11396                //
11397                // Binary search over the same powers finds the whole
11398                // run of trailing zeros in at most six tests and one
11399                // division, instead of one test and one division per
11400                // digit.
11401                let (mut s, mut sc) = (*scaled, *scale);
11402                if sc > 0 && s != 0 {
11403                    let mut lo: u16 = 0;
11404                    let mut hi: u16 = sc;
11405                    while lo < hi {
11406                        let mid = (lo + hi).div_ceil(2);
11407                        match pow10_i128(mid) {
11408                            Some(p) if s % p == 0 => lo = mid,
11409                            _ => hi = mid - 1,
11410                        }
11411                    }
11412                    if lo > 0 {
11413                        if let Some(p) = pow10_i128(lo) {
11414                            s /= p;
11415                            sc -= lo;
11416                        }
11417                    }
11418                }
11419                if sc == 0 {
11420                    if let Ok(n) = i64::try_from(s) {
11421                        h.write_u8(TAG_NUM_I64);
11422                        h.write_i64(n);
11423                    } else {
11424                        num_f64(h, crate::orderby::numeric_to_f64(s, 0));
11425                    }
11426                } else {
11427                    num_f64(h, crate::orderby::numeric_to_f64(s, sc));
11428                }
11429            }
11430        },
11431        // Beyond-i128 NUMERIC compares exactly via numeric_bignum_cmp; a
11432        // value that also fits i128 reuses the Numeric path above so
11433        // Big(5) and Numeric(5) agree. A genuinely huge one can't equal
11434        // any i128-representable value — constant bucket is safe.
11435        Value::NumericBig(b) => match b.to_i128() {
11436            Some(s) => norm_hash_value(
11437                &Value::Numeric {
11438                    scaled: s,
11439                    scale: b.scale(),
11440                    kind: spg_storage::NumericKind::Finite,
11441                },
11442                h,
11443            ),
11444            None => h.write_u8(TAG_OPAQUE),
11445        },
11446        // value_cmp compares Text↔BpChar blank-insensitively (both sides
11447        // trimmed), so both hash the trimmed bytes. Text pairs differing
11448        // only in trailing blanks collide and are split exactly in-bucket.
11449        Value::Text(s) | Value::BpChar(s) => {
11450            h.write_u8(TAG_TEXT);
11451            h.write(s.trim_end_matches(' ').as_bytes());
11452        }
11453        Value::Char1(c) => {
11454            h.write_u8(TAG_CHAR1);
11455            h.write_u8(*c);
11456        }
11457        Value::Date(d) => {
11458            h.write_u8(TAG_DATE);
11459            h.write_i32(*d);
11460        }
11461        Value::Time(t) => {
11462            h.write_u8(TAG_TIME);
11463            h.write_i64(*t);
11464        }
11465        Value::Timestamp(t) => {
11466            h.write_u8(TAG_TIMESTAMP);
11467            h.write_i64(*t);
11468        }
11469        Value::TimeTz { us, offset_secs } => {
11470            h.write_u8(TAG_TIMETZ);
11471            h.write_i64(*us);
11472            h.write_i32(*offset_secs);
11473        }
11474        Value::Uuid(u) => {
11475            h.write_u8(TAG_UUID);
11476            h.write(u);
11477        }
11478        Value::Money(c) => {
11479            h.write_u8(TAG_MONEY);
11480            h.write_i64(*c);
11481        }
11482        Value::Bytes(b) => {
11483            h.write_u8(TAG_BYTES);
11484            h.write(b.as_ref());
11485        }
11486        Value::Interval {
11487            months,
11488            days,
11489            micros,
11490            kind,
11491        } => {
11492            h.write_u8(TAG_INTERVAL);
11493            h.write_i32(*months);
11494            h.write_i32(*days);
11495            h.write_i64(*micros);
11496        }
11497        // v7.37.16 — REAL joined the numeric value_cmp family (widened
11498        // to f64, same formulas as the arms), so it hashes in the shared
11499        // numeric domain: Real(1.5) must agree with Float(1.5)/Int/…
11500        // f32→f64 is exact, so equal-under-cmp implies equal bits here.
11501        Value::Real(x) => num_f64(h, f64::from(*x)),
11502        // Json (structural equality), vector families (float rendering),
11503        // arrays / geometry / net / ranges / composites (debug-format
11504        // fallback): one constant bucket — exact linear within.
11505        _ => h.write_u8(TAG_OPAQUE),
11506    }
11507}
11508
11509/// v7.38 (read01) — row equality for DISTINCT / UNION / INTERSECT / EXCEPT that
11510/// treats numerically-equal exact values as one regardless of type or scale
11511/// (`1 = 1.0 = 1.00`), matching PG (and GROUP BY). Uses the scale-aware
11512/// `orderby::value_cmp`, so `Int(1)` and `Numeric{10,1}` compare Equal; plain
11513/// `Row` `==` would keep them distinct.
11514/// v7.39 (round 410) — under the MySQL dialect a set operation / DISTINCT
11515/// deduplicates by the session collation (`utf8mb4_uca1400_ai_ci`, which is
11516/// case- and accent-insensitive and PAD SPACE): `'a'`, `'A'`, and `'a '`
11517/// collapse to one row, exactly as GROUP BY already folds its keys. Returns
11518/// the folded comparison key for a text value, None for anything else (which
11519/// keeps the byte-exact `value_cmp` path).
11520fn mysql_dedup_fold(v: &Value, pads: bool) -> Option<String> {
11521    match v {
11522        // v7.38.17 — CHAR's trailing spaces are padding; TEXT's are
11523        // data. The comment above named `utf8mb4_uca1400_ai_ci`, which
11524        // is MariaDB's default and PAD SPACE. SPG advertises MySQL 8.0,
11525        // whose default is NO PAD, so `'alpha'` and `'alpha  '` are two
11526        // rows to a `SELECT DISTINCT` and one to `count(DISTINCT)` was
11527        // the same question answered twice.
11528        // v7.38.18 — a CHAR's padding is the TYPE's and never counts; a
11529        // TEXT's is the collation's, which `pads` carries per position.
11530        Value::BpChar(s) => Some(spg_storage::mysql_compare_fold_char(s)),
11531        Value::Text(s) if pads => Some(spg_storage::mysql_compare_fold_char(s)),
11532        Value::Text(s) => Some(spg_storage::mysql_compare_fold(s)),
11533        _ => None,
11534    }
11535}
11536
11537/// v7.39 (round 485) — how many projected rows the single-table scan
11538/// builds, and how many of those the DISTINCT probe throws away again.
11539///
11540/// The round-485 profile of `SELECT DISTINCT g FROM h ORDER BY g` put
11541/// 21 % of all samples in malloc/free called straight from the scan
11542/// closure. The closure's one per-row allocation is the projected
11543/// `Vec<Value>`, and under DISTINCT most of those are discarded a few
11544/// instructions later — but "most" is a guess until it is a number, so
11545/// these count it. (Round 480 was spent acting on an inference about a
11546/// branch that turned out never to run.)
11547/// v7.39 (round 488) — reachability counters for round 487's projection
11548/// binding. The interleaved panel says round 487 costs `group_500k` 13 %,
11549/// and a never-called-function probe rules out code layout — so the
11550/// question is whether that shape reaches this code at all, which is a
11551/// number, not an inference.
11552pub static SCAN_PATH_ENTERED: core::sync::atomic::AtomicU64 = core::sync::atomic::AtomicU64::new(0);
11553pub static PROJ_DIRECT_FIRE: core::sync::atomic::AtomicU64 = core::sync::atomic::AtomicU64::new(0);
11554
11555pub static PROJ_ROW_BUILT: core::sync::atomic::AtomicU64 = core::sync::atomic::AtomicU64::new(0);
11556pub static DISTINCT_DUP_DROPPED: core::sync::atomic::AtomicU64 =
11557    core::sync::atomic::AtomicU64::new(0);
11558
11559/// v7.38.13 — how DISTINCT must compare one row of output.
11560///
11561/// The MySQL default collation folds case and trailing spaces when it
11562/// de-dups, but a column declared `COLLATE utf8mb4_bin` is BYTE-WISE and
11563/// must not fold — `e2e_mysql_collate_binary_round370` calls the
11564/// alternative "a silent data-integrity bug: `'a'` and `'A'` de-dup as
11565/// one when the schema asked to keep them apart", and names DISTINCT as
11566/// one of the sites that has to honour it.
11567///
11568/// It did not. `values_eq_norm` took a bare `bool` and folded every Text
11569/// value in a MySQL session, because a bool cannot see a column. The
11570/// GROUP BY path consults the schema and was right all along; the test
11571/// only ever exercised that spelling, so the DISTINCT hole was never
11572/// covered. `SELECT DISTINCT t` answered 2 where MariaDB 11 answers 4.
11573///
11574/// `binary` is indexed by OUTPUT POSITION; a position past its end folds,
11575/// which is what a caller with no schema to offer gets.
11576#[derive(Clone, Copy)]
11577pub(crate) struct FoldSpec<'c> {
11578    mysql: bool,
11579    binary: &'c [bool],
11580    /// v7.38.18 — the padding mask, in lockstep with `binary`. Read the
11581    /// note on `folds`: a hash and its comparator must consult the same
11582    /// masks or equal rows scatter across buckets.
11583    pads: &'c [bool],
11584}
11585
11586impl<'c> FoldSpec<'c> {
11587    /// No column information — every Text position folds under MySQL.
11588    pub(crate) const fn dialect(mysql: bool) -> Self {
11589        Self {
11590            mysql,
11591            binary: &[],
11592            pads: &[],
11593        }
11594    }
11595
11596    /// The mask read off the output columns.
11597    pub(crate) fn of(mysql: bool, binary: &'c [bool]) -> Self {
11598        Self {
11599            mysql,
11600            binary,
11601            pads: &[],
11602        }
11603    }
11604
11605    /// The masks read off the output columns — fold-exemption AND
11606    /// padding, which are different questions about the same collation.
11607    pub(crate) fn of_masks(mysql: bool, binary: &'c [bool], pads: &'c [bool]) -> Self {
11608        Self {
11609            mysql,
11610            binary,
11611            pads,
11612        }
11613    }
11614
11615    /// Does position `i` treat trailing spaces as insignificant?
11616    #[inline]
11617    fn pads_at(&self, i: usize) -> bool {
11618        self.pads.get(i).copied().unwrap_or(false)
11619    }
11620
11621    /// Does position `i` fold?
11622    #[inline]
11623    fn folds(&self, i: usize) -> bool {
11624        self.mysql && !self.binary.get(i).copied().unwrap_or(false)
11625    }
11626}
11627
11628/// The fold-exempt mask for a projection.
11629///
11630/// Read off `ProjectedItem`, not off the output `ColumnSchema`: the
11631/// projection rebuilds that schema through `ColumnSchema::new`, whose
11632/// collation default is `Binary` — a mask built from it would mark
11633/// EVERY column byte-wise and stop DISTINCT folding at all.
11634/// The padding mask for a projection, read off the same items as
11635/// [`fold_mask`] so the two cannot come from different places.
11636pub(crate) fn pad_mask(projection: &[ProjectedItem]) -> alloc::vec::Vec<bool> {
11637    projection.iter().map(|p| p.pads).collect()
11638}
11639
11640pub(crate) fn fold_mask(projection: &[ProjectedItem]) -> alloc::vec::Vec<bool> {
11641    projection.iter().map(|p| p.fold_exempt).collect()
11642}
11643
11644/// v7.38.14 — the same mask, from an OUTPUT SCHEMA instead of a
11645/// projection.
11646///
11647/// Some de-duplication sites hold `Vec<ColumnSchema>` and never see the
11648/// `ProjectedItem`s it came from. `ProjectedItem::fold_exempt` is built
11649/// from exactly this test (`select.rs`, `build_projection`), so the two
11650/// must keep answering identically -- a site that decided "byte-wise" one
11651/// way while its neighbour decided the other is how the answer came to
11652/// depend on which executor ran the query.
11653///
11654/// The direction matters: `Collation::Binary` is `ColumnSchema::new`'s
11655/// DEFAULT, so a schema rebuilt without carrying the field reads as
11656/// "byte-wise on purpose" here. That is a real trap and it has caught
11657/// five fields so far; it is why S4 of this release exists.
11658/// v7.38.18 — the padding mask from output columns, the sibling of
11659/// [`fold_mask_of_columns`]. Whether a column folds and whether it
11660/// pads are different questions about the same collation.
11661pub(crate) fn pad_mask_of_columns(columns: &[ColumnSchema]) -> alloc::vec::Vec<bool> {
11662    columns
11663        .iter()
11664        .map(|c| crate::collate::pads_space(c.collation_name.as_deref()))
11665        .collect()
11666}
11667
11668pub(crate) fn fold_mask_of_columns(columns: &[ColumnSchema]) -> alloc::vec::Vec<bool> {
11669    columns
11670        .iter()
11671        .map(|c| matches!(c.collation, spg_storage::Collation::Binary))
11672        .collect()
11673}
11674
11675pub(crate) fn row_eq_norm(a: &Row<'static>, b: &Row<'static>, fold: FoldSpec<'_>) -> bool {
11676    values_eq_norm(&a.values, &b.values, fold)
11677}
11678
11679/// v7.39 (round 485) — `row_eq_norm` over bare value slices, so the
11680/// DISTINCT probe can compare a reused projection buffer against a kept
11681/// row without building a `Row` for it.
11682pub(crate) fn values_eq_norm(
11683    a: &[Value<'static>],
11684    b: &[Value<'static>],
11685    fold: FoldSpec<'_>,
11686) -> bool {
11687    a.len() == b.len()
11688        && a.iter().zip(b).enumerate().all(|(i, (x, y))| {
11689            if fold.folds(i)
11690                && let (Some(fx), Some(fy)) = (
11691                    mysql_dedup_fold(x, fold.pads_at(i)),
11692                    mysql_dedup_fold(y, fold.pads_at(i)),
11693                )
11694            {
11695                return fx == fy;
11696            }
11697            crate::orderby::value_cmp(x, y) == core::cmp::Ordering::Equal
11698        })
11699}
11700
11701/// Coerce a `Value` to an `f64` sort key for ORDER BY. Numbers map directly;
11702/// NULL sorts last (treated as `+∞`); booleans are 0.0 / 1.0; text uses lex
11703/// order via the byte values; vectors are not sortable.
11704pub(crate) fn value_to_order_key(v: &Value) -> Result<OrderKey, EngineError> {
11705    // v7.37.16 — TEXT rides a FULL-precision key: carry the whole string
11706    // so values sharing a ≥6-byte common prefix (`product_001` vs
11707    // `product_002`, ISO timestamps stored as text, prefixed IDs / SKUs)
11708    // order by their exact bytes instead of the old lossy f64 coarse key.
11709    // Comparison is byte-lexicographic (see `order_key_elem_cmp`), which
11710    // matches PG's default C / binary text collation. Every other type
11711    // keeps the lossless-enough `f64` fast path below.
11712    if let Value::Text(s) = v {
11713        return Ok(OrderKey::Text(crate::orderby::CompactText::new(s.as_ref())));
11714    }
11715    // v7.39 (bpchar epic) — bpchar sorts by its blank-stripped form then
11716    // byte order (PG bpcharcmp under C collation), so mixed-pad values of
11717    // the same logical string order equal.
11718    if let Value::BpChar(s) = v {
11719        return Ok(OrderKey::Text(crate::orderby::CompactText::new(
11720            s.trim_end_matches(' '),
11721        )));
11722    }
11723    // v7.38 (read01 P6.24) — jsonb sorts by PG's type-aware total order, so
11724    // carry the parsed value and compare it structurally (see
11725    // `order_key_elem_cmp`). Unparseable text falls back to a Text key.
11726    if let Value::Json(s) = v {
11727        return Ok(match crate::json::parse(s) {
11728            Ok(jv) => OrderKey::Json(jv),
11729            Err(_) => OrderKey::Text(crate::orderby::CompactText::new(s.as_ref())),
11730        });
11731    }
11732    // v7.37 — byte-orderable types PG sorts byte-wise but that have no
11733    // meaningful f64 projection. bytea/uuid/macaddr sort by their raw bytes;
11734    // inet/cidr by `[family, addr.., bits]` (family, then address, then mask),
11735    // matching PG's network ordering.
11736    match v {
11737        Value::Bytes(b) => return Ok(OrderKey::Bytes(b.as_ref().to_vec())),
11738        // v7.38 (read01, T3.C3) — arbitrary-precision NUMERIC sorts by exact value.
11739        Value::NumericBig(b) => {
11740            return Ok(OrderKey::Numeric(alloc::boxed::Box::new(
11741                spg_storage::NumericKey::from_big(b),
11742            )));
11743        }
11744        Value::Uuid(u) => return Ok(OrderKey::Bytes(u.to_vec())),
11745        Value::Macaddr(m) => return Ok(OrderKey::Bytes(m.to_vec())),
11746        Value::Macaddr8(m) => return Ok(OrderKey::Bytes(m.to_vec())),
11747        Value::PgLsn(l) => return Ok(OrderKey::Bytes(l.to_be_bytes().to_vec())),
11748        Value::Inet { family, bits, addr } | Value::Cidr { family, bits, addr } => {
11749            let mut key = alloc::vec::Vec::with_capacity(18);
11750            key.push(*family);
11751            key.extend_from_slice(addr);
11752            key.push(*bits);
11753            return Ok(OrderKey::Bytes(key));
11754        }
11755        _ => {}
11756    }
11757    // v7.38 (read01, U16) — one-dimensional arrays sort element-wise, then
11758    // shorter-first (PG: `{1} < {1,2} < {2} < {10}`). Each element carries its
11759    // own OrderKey so integer arrays sort numerically; a NULL element rides to
11760    // the end via the +INF sentinel.
11761    let inf = || OrderKey::NullBig;
11762    let arr = match v {
11763        Value::IntArray(a) => Some(
11764            a.iter()
11765                .map(|o| o.map_or_else(inf, |n| OrderKey::Int(i128::from(n))))
11766                .collect(),
11767        ),
11768        Value::SmallIntArray(a) => Some(
11769            a.iter()
11770                .map(|o| o.map_or_else(inf, |n| OrderKey::Int(i128::from(n))))
11771                .collect(),
11772        ),
11773        Value::BigIntArray(a) => Some(
11774            a.iter()
11775                .map(|o| o.map_or_else(inf, |n| OrderKey::Int(i128::from(n))))
11776                .collect(),
11777        ),
11778        Value::BoolArray(a) => Some(
11779            a.iter()
11780                .map(|o| o.map_or_else(inf, |b| OrderKey::Int(i128::from(b))))
11781                .collect(),
11782        ),
11783        Value::TextArray(a) => Some(
11784            a.iter()
11785                .map(|o| {
11786                    o.as_ref()
11787                        .map_or_else(inf, |s| OrderKey::Text(crate::orderby::CompactText::new(s)))
11788                })
11789                .collect(),
11790        ),
11791        #[allow(clippy::cast_precision_loss)]
11792        Value::FloatArray(a) => Some(
11793            a.iter()
11794                .map(|o| o.map_or(OrderKey::NullBig, OrderKey::Num))
11795                .collect(),
11796        ),
11797        // r1040 — array elements take the same exact key their scalar
11798        // form does; an f64 projection here would order `{0.1}` against
11799        // `{0.1000000000000000001}` by luck.
11800        Value::NumericArray(a) => Some(
11801            a.iter()
11802                .map(|o| {
11803                    o.map_or_else(inf, |(m, s)| {
11804                        OrderKey::Numeric(alloc::boxed::Box::new(
11805                            spg_storage::NumericKey::from_numeric(
11806                                m,
11807                                s,
11808                                spg_storage::NumericKind::Finite,
11809                            ),
11810                        ))
11811                    })
11812                })
11813                .collect(),
11814        ),
11815        Value::DateArray(a) => Some(
11816            a.iter()
11817                .map(|o| o.map_or_else(inf, |n| OrderKey::Int(i128::from(n))))
11818                .collect(),
11819        ),
11820        _ => None,
11821    };
11822    if let Some(elements) = arr {
11823        return Ok(OrderKey::Array(elements));
11824    }
11825    // v7.39 (read01 round 56) — a COMPOSITE sorts field by field, left to
11826    // right, which is exactly the lexicographic element order an Array key
11827    // already gives: `(2,'b') < (9,'a')` because the leading field decides.
11828    if let Value::Composite(fields) = v {
11829        let elements = fields
11830            .iter()
11831            .map(|(_, fv)| value_to_order_key(fv))
11832            .collect::<Result<alloc::vec::Vec<_>, _>>()?;
11833        return Ok(OrderKey::Array(elements));
11834    }
11835    // v7.38 (read01 U31) — the integer-valued types carry an EXACT i128 key.
11836    // Projecting these to f64 (the historic path) silently collapses BigInt /
11837    // Timestamp / Time / TimeTz / Money values past 2^53, so `ORDER BY` gave
11838    // the wrong order for large ids and microsecond timestamps.
11839    match v {
11840        Value::SmallInt(n) => return Ok(OrderKey::Int(i128::from(*n))),
11841        Value::Int(n) => return Ok(OrderKey::Int(i128::from(*n))),
11842        Value::BigInt(n) => return Ok(OrderKey::Int(i128::from(*n))),
11843        // PG TIME/TIMESTAMP/DATE/MONEY/YEAR are ordered by their underlying
11844        // integer (days / micros / cents / calendar year); TIMETZ by the
11845        // UTC-equivalent micros (local wall - offset) so the same physical
11846        // instant in different zones sorts equal.
11847        Value::Date(d) => return Ok(OrderKey::Int(i128::from(*d))),
11848        Value::Timestamp(t) => return Ok(OrderKey::Int(i128::from(*t))),
11849        Value::Time(us) => return Ok(OrderKey::Int(i128::from(*us))),
11850        Value::Year(y) => return Ok(OrderKey::Int(i128::from(*y))),
11851        // v7.39.13 — the UTC instant is only HALF the key.
11852        //
11853        // This ordered by the instant alone, so the values that share
11854        // one were called equal and a stable sort then returned them in
11855        // insertion order — an answer, not a tie-break. Measured on
11856        // PostgreSQL 18.6 against this engine, six rows, one column:
11857        //
11858        // ```text
11859        //   PG 18.6        SPG 7.39.12
11860        //   07:00:00+01    07:00:00+01
11861        //   06:59:59+00    06:59:59+00
11862        //   09:00:00+02    07:00:00+00   <- the four that share
11863        //   07:00:00+00    02:00:00-05      07:00 UTC, in the
11864        //   02:00:00-05    09:00:00+02      order they were written
11865        //   01:00:00-06    01:00:00-06
11866        // ```
11867        //
11868        // PostgreSQL breaks the tie by OFFSET DESCENDING, and
11869        // `'07:00:00+00' = '02:00:00-05'` is FALSE there. Shifting the
11870        // instant left by 32 bits leaves room for the offset underneath
11871        // it — `i128` holds both exactly, where `i64` could not — and
11872        // `compare` in `eval::binop` orders the same pair the same way,
11873        // from the same measurement.
11874        Value::TimeTz { us, offset_secs } => {
11875            return Ok(OrderKey::Int(i128::from(spg_storage::timetz_sort_key(
11876                *us,
11877                *offset_secs,
11878            ))));
11879        }
11880        Value::Money(c) => return Ok(OrderKey::Int(i128::from(*c))),
11881        _ => {}
11882    }
11883    let num = match v {
11884        // Callers without NULLS FIRST/LAST context (array elements,
11885        // histogram sampling) put NULL last, as before.
11886        Value::Null => return Ok(OrderKey::NullBig),
11887        // v7.17.0 Phase 3.P0-38 — range ordering is not supported
11888        // in v7.17.0 (needs lex-then-inclusivity tiebreak).
11889        Value::Range { .. } => {
11890            return Err(EngineError::Unsupported(
11891                "ORDER BY of a range value is not supported in v7.17.0".into(),
11892            ));
11893        }
11894        // v7.17.0 Phase 3.P0-39 — hstore is not orderable.
11895        Value::Hstore(_) => {
11896            return Err(EngineError::Unsupported(
11897                "ORDER BY of a hstore value is not supported".into(),
11898            ));
11899        }
11900        // v7.17.0 Phase 3.P0-40 — 2D arrays not orderable.
11901        Value::IntArray2D(_) | Value::BigIntArray2D(_) | Value::TextArray2D(_) => {
11902            return Err(EngineError::Unsupported(
11903                "ORDER BY of a 2D array is not supported in v7.17.0".into(),
11904            ));
11905        }
11906        // r1039/r1040 — the exact canonical key, not an f64 projection.
11907        //
11908        // r1039 fixed the three specials, which carry a canonical zero in
11909        // `scaled` and so all sorted as the number 0. The projection
11910        // itself was the rest of the defect: "precision losses here only
11911        // matter for tie-breaks well past 15 significant digits" was the
11912        // comment, and the measurement disagreed — f64 called
11913        // `0.1` and `0.1000000000000000001` Equal, and a stable sort then
11914        // returned them in insertion order. Three of ten values came back
11915        // in the wrong place against PG18.4.
11916        Value::Numeric {
11917            scaled,
11918            scale,
11919            kind,
11920        } => {
11921            return Ok(OrderKey::Numeric(alloc::boxed::Box::new(
11922                spg_storage::NumericKey::from_numeric(*scaled, *scale, *kind),
11923            )));
11924        }
11925        Value::Float(x) => *x,
11926        // v7.37.16 — REAL sorts by its exact f64 widening (it had no
11927        // arm and fell through to the unsupported error).
11928        Value::Real(x) => f64::from(*x),
11929        Value::Bool(b) => {
11930            if *b {
11931                1.0
11932            } else {
11933                0.0
11934            }
11935        }
11936        Value::Vector(_) | Value::Sq8Vector(_) | Value::HalfVector(_) => {
11937            return Err(EngineError::Unsupported(
11938                "ORDER BY of a raw vector column is not meaningful — use `<->`".into(),
11939            ));
11940        }
11941        // v7.37 — PG orders INTERVAL by its total time, treating a month as
11942        // 30 days (`1 hour < 90 min < 1 day < 1 mon`). Project to total micros;
11943        // f64 is exact for any interval under ~285 years, and only ORDER BY
11944        // tie-breaks past that magnitude lose precision. Matches the
11945        // min/max(interval) comparator in aggregate.rs.
11946        #[allow(clippy::cast_precision_loss)]
11947        Value::Interval {
11948            months,
11949            days,
11950            micros,
11951            kind,
11952        } => {
11953            let total = i128::from(*months) * 30 * 86_400_000_000
11954                + i128::from(*days) * 86_400_000_000
11955                + i128::from(*micros);
11956            total as f64
11957        }
11958        Value::Json(_) => {
11959            return Err(EngineError::Unsupported(
11960                "ORDER BY of a JSON value is not supported — cast the document to text first"
11961                    .into(),
11962            ));
11963        }
11964        // v7.5.0 — Value is #[non_exhaustive]; future variants need
11965        // an explicit ORDER BY mapping. Surface as Unsupported until
11966        // engine support is added.
11967        _ => {
11968            return Err(EngineError::Unsupported(
11969                "ORDER BY of this value type is not supported".into(),
11970            ));
11971        }
11972    };
11973    Ok(OrderKey::Num(num))
11974}
11975
11976/// Find the schema entry that a SELECT-list `Expr::Column` refers to.
11977/// Mirrors `resolve_column` in `eval.rs`, but returns a proper
11978/// `EngineError` so the projection-build path keeps `UnknownQualifier`
11979/// vs `ColumnNotFound` distinct.
11980/// PG's name for the physical row identity. It is reserved there — no table
11981/// can have a column called this — which is what lets `*` skip it by name.
11982pub(crate) const CTID_COLUMN: &str = "ctid";
11983
11984/// v7.39 (round 512) — PG's system columns, in the order they are appended.
11985/// All six are reserved names there, which is what lets `*` skip them and
11986/// lets a scan tell them from a user column without a flag.
11987pub(crate) const SYSTEM_COLUMNS: [&str; 6] = ["ctid", "xmin", "xmax", "cmin", "cmax", "tableoid"];
11988
11989/// Is this name one of them?
11990pub(crate) fn is_system_column(name: &str) -> bool {
11991    SYSTEM_COLUMNS.iter().any(|s| name.eq_ignore_ascii_case(s))
11992}
11993
11994/// Where the scan's appended system columns begin, if this schema carries
11995/// them: the trailing six, named in order. A catalog view with a column of
11996/// its own called `xmin` does not match, which is the point.
11997fn system_column_tail_start(cols: &[ColumnSchema]) -> Option<usize> {
11998    let start = cols.len().checked_sub(SYSTEM_COLUMNS.len())?;
11999    cols[start..]
12000        .iter()
12001        .zip(SYSTEM_COLUMNS)
12002        .all(|(c, name)| c.name.eq_ignore_ascii_case(name))
12003        .then_some(start)
12004}
12005
12006/// v7.39 (round 540) — which positions `*` must skip.
12007///
12008/// The rule stays round 512's — the synthetic columns are the trailing
12009/// six of a relation's block, matched by POSITION so a genuine `xmin`
12010/// column is not lost — but a JOINED schema names its columns
12011/// `alias.column` and lays the peers out end to end, so a peer's six sit
12012/// in the MIDDLE of the whole list. Grouping by qualifier first puts the
12013/// "trailing six" test back on the block it was written for.
12014fn synthetic_system_positions(cols: &[ColumnSchema]) -> alloc::vec::Vec<bool> {
12015    let mut skip = alloc::vec![false; cols.len()];
12016    fn qualifier(n: &str) -> Option<&str> {
12017        n.rsplit_once('.').map(|(q, _)| q)
12018    }
12019    fn bare(n: &str) -> &str {
12020        n.rsplit('.').next().unwrap_or(n)
12021    }
12022    let mut i = 0;
12023    while i < cols.len() {
12024        let q = qualifier(&cols[i].name);
12025        let mut end = i;
12026        while end < cols.len() && qualifier(&cols[end].name) == q {
12027            end += 1;
12028        }
12029        if let Some(start) = (end - i)
12030            .checked_sub(SYSTEM_COLUMNS.len())
12031            .map(|off| i + off)
12032            && cols[start..end]
12033                .iter()
12034                .zip(SYSTEM_COLUMNS)
12035                .all(|(c, name)| bare(&c.name).eq_ignore_ascii_case(name))
12036        {
12037            for s in skip.iter_mut().take(end).skip(start) {
12038                *s = true;
12039            }
12040        }
12041        i = end;
12042    }
12043    skip
12044}
12045
12046/// v7.39 (round 511) — does this statement name `ctid` anywhere it would be
12047/// read? Only then is the column materialised.
12048pub(crate) fn expr_references_ctid(e: &Expr) -> bool {
12049    let mut found = false;
12050    crate::expr_analysis::visit_expr_columns_and_subqueries(
12051        e,
12052        &mut |c| {
12053            if is_system_column(&c.name) {
12054                found = true;
12055            }
12056        },
12057        &mut |_| {},
12058    );
12059    found
12060}
12061
12062fn references_ctid(stmt: &SelectStatement) -> bool {
12063    let in_expr = expr_references_ctid;
12064    stmt.items.iter().any(|i| match i {
12065        SelectItem::Expr { expr, .. } => in_expr(expr),
12066        _ => false,
12067    }) || stmt.where_.as_ref().is_some_and(in_expr)
12068        || stmt.order_by.iter().any(|o| in_expr(&o.expr))
12069        || stmt
12070            .group_by
12071            .as_ref()
12072            .is_some_and(|g| g.iter().any(in_expr))
12073        || stmt.having.as_ref().is_some_and(in_expr)
12074}
12075
12076/// v7.39 (round 961) — the whole-row schema for `SELECT t FROM t`, which
12077/// is a name the projection has to TYPE before any row exists.
12078///
12079/// Evaluation has answered this since round T9 (`resolve_column` builds a
12080/// `Value::Composite` of every column), but the typing side below had no
12081/// such branch and raised `column "t" does not exist` first — so the
12082/// feature was unreachable through a projection. Measured against PG18.4:
12083/// `SELECT wr FROM wr` answers `(7,z)` there and errored here.
12084///
12085/// The type is `Jsonb` + a composite marker, which is exactly how a
12086/// column DECLARED as a composite type is described (`ddl.rs`, round 56):
12087/// the value travels as a `Value::Composite` and renders in the canonical
12088/// `(7,z)` form. SPG has no catalog entry for a table's implicit row type,
12089/// so the marker names the alias and no rehydration keys off it — the
12090/// value arrives already built.
12091fn whole_row_projection_schema(alias: &str) -> ColumnSchema {
12092    let mut s = ColumnSchema::new(
12093        alloc::string::String::from(alias),
12094        spg_storage::DataType::Jsonb,
12095        true,
12096    );
12097    s.user_composite_type = Some(alloc::string::String::from(alias));
12098    s
12099}
12100
12101/// v7.39.3 — `mysql` makes the name comparison case-INSENSITIVE, which
12102/// is MySQL's rule for column names (measured on 9.7.2: `mycol`,
12103/// `MYCOL` and a backquoted `MyCol` all resolve the same column).
12104///
12105/// SPG compared byte for byte and its lexer folds an UNQUOTED
12106/// identifier, so a table restored from a `mysqldump` — where every
12107/// identifier is backquoted and keeps its case — had every mixed-case
12108/// column unreachable from ordinary unquoted SQL. Same "two spellings,
12109/// two things" defect v7.39.1 closed for relation names.
12110pub(crate) fn resolve_projection_column<'a>(
12111    c: &ColumnName,
12112    schema_cols: &'a [ColumnSchema],
12113    table_alias: &str,
12114    mysql: bool,
12115) -> Result<Cow<'a, ColumnSchema>, EngineError> {
12116    let same = |a: &str, b: &str| {
12117        if mysql {
12118            a.eq_ignore_ascii_case(b)
12119        } else {
12120            a == b
12121        }
12122    };
12123    if let Some(q) = &c.qualifier {
12124        let composite = alloc::format!("{q}.{name}", name = c.name);
12125        if let Some(s) = schema_cols.iter().find(|s| same(&s.name, &composite)) {
12126            return Ok(Cow::Borrowed(s));
12127        }
12128        // Single-table case: the qualifier may equal the active alias —
12129        // then look for the bare column name.
12130        if same(q, table_alias)
12131            && let Some(s) = schema_cols.iter().find(|s| same(&s.name, &c.name))
12132        {
12133            return Ok(Cow::Borrowed(s));
12134        }
12135        // For multi-table schemas the qualifier is unknown only if no
12136        // column bears the "<q>." prefix. For single-table, the alias
12137        // mismatch alone is enough.
12138        let prefix = alloc::format!("{q}.");
12139        let qualifier_known =
12140            same(q, table_alias) || schema_cols.iter().any(|s| s.name.starts_with(&prefix));
12141        if !qualifier_known {
12142            return Err(EngineError::Eval(EvalError::UnknownQualifier {
12143                qualifier: q.clone(),
12144                column: c.name.clone(),
12145            }));
12146        }
12147        return Err(EngineError::Eval(EvalError::ColumnNotFound {
12148            name: c.name.clone(),
12149        }));
12150    }
12151    if let Some(s) = schema_cols.iter().find(|s| same(&s.name, &c.name)) {
12152        return Ok(Cow::Borrowed(s));
12153    }
12154    let suffix = alloc::format!(".{name}", name = c.name);
12155    let mut matches = schema_cols.iter().filter(|s| s.name.ends_with(&suffix));
12156    let first = matches.next();
12157    let extra = matches.next();
12158    match (first, extra) {
12159        (Some(s), None) => Ok(Cow::Borrowed(s)),
12160        (Some(_), Some(_)) => Err(EngineError::Eval(EvalError::TypeMismatch {
12161            detail: alloc::format!("column reference \"{}\" is ambiguous", c.name),
12162        })),
12163        // The whole-row reference, checked LAST so a real column carrying
12164        // the alias's name still wins — the same precedence
12165        // `resolve_column` applies on the evaluation side.
12166        //
12167        // Two schema shapes reach here. A single-table (or subquery, or
12168        // CTE) scan carries its alias and bare column names, so the name
12169        // has to equal the alias. A JOIN's combined schema carries no
12170        // alias at all and qualifies every column `alias.col`, so the
12171        // alias is identified by the prefix instead — which is exactly
12172        // how `whole_row_composite` picks the fields out on the
12173        // evaluation side. Measured: `SELECT wr FROM wr JOIN jb ON …`
12174        // answers `(7,z)` on PG18.4 and errored here until this arm
12175        // covered the joined shape too.
12176        _ if !table_alias.is_empty() && c.name == table_alias => {
12177            Ok(Cow::Owned(whole_row_projection_schema(table_alias)))
12178        }
12179        _ if table_alias.is_empty() && {
12180            let prefix = alloc::format!("{name}.", name = c.name);
12181            schema_cols.iter().any(|s| s.name.starts_with(&prefix))
12182        } =>
12183        {
12184            Ok(Cow::Owned(whole_row_projection_schema(&c.name)))
12185        }
12186        _ => Err(EngineError::Eval(EvalError::ColumnNotFound {
12187            name: c.name.clone(),
12188        })),
12189    }
12190}
12191
12192/// v7.40.0 — a column the grouping-set rewrite injected purely to sort
12193/// on, and which must not reach the client. Two families: `__grp_ord_*`
12194/// carries a branch's `grouping()` mask (round 135), `__grp_key_*`
12195/// carries a key the rollup orders by that the query did not project —
12196/// without it `SELECT SUM(qty) … GROUP BY qty WITH ROLLUP` answered
12197/// `column "qty" does not exist`, because a UNION's ORDER BY can only
12198/// name output columns.
12199fn is_synthetic_group_col(name: &str) -> bool {
12200    name.starts_with("__grp_ord_") || name.starts_with("__grp_key_")
12201}
12202
12203/// v7.39 (round 135) — drop the synthetic `__grp_ord_*` columns injected by the
12204/// parser to carry per-branch GROUPING() masks into a grouping-set query's
12205/// ORDER BY. They must never reach the output. No-op unless such a column is
12206/// present, so the common path is untouched.
12207/// v7.39 (round 529) — the LIMIT / OFFSET that DISTINCT ON deferred.
12208///
12209/// PG limits what the dedup LEFT, not what fed it; SPG limited first, so
12210/// a `LIMIT 2` that should have answered two groups answered one.
12211fn apply_deferred_limit(
12212    rows: alloc::vec::Vec<Row<'static>>,
12213    deferred: &(
12214        Option<spg_sql::ast::LimitExpr>,
12215        Option<spg_sql::ast::LimitExpr>,
12216    ),
12217) -> alloc::vec::Vec<Row<'static>> {
12218    let count = |e: &Option<spg_sql::ast::LimitExpr>| match e {
12219        Some(spg_sql::ast::LimitExpr::Literal(n)) => Some(*n as usize),
12220        _ => None,
12221    };
12222    let mut rows = rows;
12223    if let Some(off) = count(&deferred.1) {
12224        rows = rows.split_off(off.min(rows.len()));
12225    }
12226    if let Some(lim) = count(&deferred.0) {
12227        rows.truncate(lim);
12228    }
12229    rows
12230}
12231
12232fn strip_synthetic_order_cols(result: QueryResult) -> QueryResult {
12233    let QueryResult::Rows { columns, rows } = result else {
12234        return result;
12235    };
12236    if !columns.iter().any(|c| is_synthetic_group_col(&c.name)) {
12237        return QueryResult::Rows { columns, rows };
12238    }
12239    let keep: Vec<usize> = columns
12240        .iter()
12241        .enumerate()
12242        .filter(|(_, c)| !is_synthetic_group_col(&c.name))
12243        .map(|(i, _)| i)
12244        .collect();
12245    let new_cols: Vec<ColumnSchema> = keep.iter().map(|&i| columns[i].clone()).collect();
12246    let new_rows: Vec<Row<'static>> = rows
12247        .into_iter()
12248        .map(|r| Row::new(keep.iter().map(|&i| r.values[i].clone()).collect()))
12249        .collect();
12250    QueryResult::Rows {
12251        columns: new_cols,
12252        rows: new_rows,
12253    }
12254}
12255
12256/// v7.39 (round 487) — bind every projection item that is a bare column
12257/// reference to its position, once per query.
12258///
12259/// `#[inline(never)]` and out of line on purpose. Round 486 established
12260/// that adding code inside these scan bodies moves neighbouring hot
12261/// functions around under fat LTO: the first version of this had the loop
12262/// inline in `run_single_table_scan` and four aggregate shapes that never
12263/// touch that function — `full_agg`, `join_agg`, `group_500k`,
12264/// `filter_agg` — went up ~5 %, reproduced against the parent commit on
12265/// the same machine. Keeping it out of line kept them still.
12266#[inline(never)]
12267fn bind_direct_columns(
12268    projection: &[ProjectedItem],
12269    ctx: &eval::EvalContext<'_>,
12270) -> Vec<Option<usize>> {
12271    projection
12272        .iter()
12273        .map(|p| match &p.expr {
12274            Expr::Column(c) => eval::compile_column_pos(c, ctx).filter(|pos| {
12275                // Same exclusion `compile_into` makes: a composite column
12276                // has to be rehydrated from stored JSON, which is not a
12277                // cell read.
12278                ctx.columns
12279                    .get(*pos)
12280                    .is_none_or(|sc| sc.user_composite_type.is_none())
12281            }),
12282            _ => None,
12283        })
12284        .collect()
12285}
12286
12287/// v7.39 (round 505) — the name an un-aliased projected expression reports.
12288///
12289/// PG18 names a call for its function and everything else `?column?`;
12290/// measured with `\gdesc`. SPG used to print the parsed expression back
12291/// out for both dialects, so `SELECT upper(s)` reported `upper(s)` and
12292/// name-keyed row access found nothing under `upper`.
12293///
12294/// The MySQL half is NOT this rule and is deliberately left alone here:
12295/// MariaDB echoes the item's SOURCE TEXT verbatim (`a+b`, spacing and all),
12296/// which needs the parser to hand over spans the AST does not carry yet.
12297/// Until it does, a MySQL session keeps the printed form — closer to what
12298/// MariaDB answers than `?column?` would be.
12299pub(crate) fn default_output_name(expr: &Expr, mysql: bool) -> String {
12300    if mysql {
12301        return expr.to_string();
12302    }
12303    spg_sql::ast::figure_column_name(expr).unwrap_or_else(|| "?column?".to_string())
12304}
12305
12306pub(crate) fn build_projection(
12307    items: &[SelectItem],
12308    schema_cols: &[ColumnSchema],
12309    table_alias: &str,
12310    mysql: bool,
12311    cat: Option<&Catalog>,
12312) -> Result<Vec<ProjectedItem>, EngineError> {
12313    build_projection_hiding_tail(items, schema_cols, table_alias, mysql, 0, cat)
12314}
12315
12316/// v7.39 (round 592) — `build_projection` with the last `hidden_tail` columns
12317/// invisible to `*`.
12318///
12319/// The windowed-SELECT path appends a synthetic `__win_N` column per window
12320/// function so the rewritten projection can reference the computed values as
12321/// ordinary columns. `*` then expanded them too, and
12322/// `SELECT wr.*, row_number() OVER (ORDER BY id) FROM wr` came back with an
12323/// EXTRA column — the internal name's value, repeated. A wrong answer, and a
12324/// silent one: the row simply had one more field than the client asked for.
12325///
12326/// Hidden by POSITION rather than by name, for the reason round 512 recorded
12327/// about the system columns: a name test looks safe until a real column
12328/// happens to carry the name. These are appended last, so the count is what
12329/// identifies them.
12330pub(crate) fn build_projection_hiding_tail(
12331    items: &[SelectItem],
12332    schema_cols: &[ColumnSchema],
12333    table_alias: &str,
12334    mysql: bool,
12335    hidden_tail: usize,
12336    // v7.38.19 — the catalog, so a user-defined function's DECLARED
12337    // return type reaches the projection. Without it `describe_expr`
12338    // cannot type `f_sql()` and the column falls back to text, which is
12339    // what psql reads to decide alignment: `SELECT 7::bigint, f_sql()`
12340    // right-aligned one cell and left-aligned the other while both held
12341    // a bigint. Reported by sentori against 7.38.18 (their §2.2), who
12342    // also established that the EXECUTOR was never confused -- CTAS off
12343    // the same expression gives a bigint column, and arithmetic on it
12344    // works. Only the type travelling in the RowDescription was wrong.
12345    cat: Option<&Catalog>,
12346) -> Result<Vec<ProjectedItem>, EngineError> {
12347    let visible = schema_cols.len().saturating_sub(hidden_tail);
12348    // v7.39 (round 462) — a join's combined schema qualifies every column
12349    // `alias.col` so the deferred-join cell lookups resolve by composite
12350    // name. That is an internal convention, and `*` was handing it to the
12351    // client: PG18 answers `SELECT * FROM a JOIN b` with the BARE names
12352    // (`id, g, id, h` — duplicates and all), SPG answered `a.id, a.g,
12353    // b.id, b.h`, so name-keyed row access found nothing. Round 128 had
12354    // already learned this for `q.*`; plain `*` never got the same rule.
12355    //
12356    // The signal is the schema itself, not the call site: only a combined
12357    // join schema arrives with no table alias AND every column qualified.
12358    // A single-table schema carries its alias, an empty schema has nothing
12359    // to strip, and a synthetic schema's names carry no dot.
12360    let joined_schema = table_alias.is_empty()
12361        && !schema_cols.is_empty()
12362        && schema_cols.iter().all(|c| c.name.contains('.'));
12363    let bare_name = |name: &str| -> String {
12364        if !joined_schema {
12365            return name.to_string();
12366        }
12367        match name.split_once('.') {
12368            Some((_, rest)) if !rest.is_empty() => rest.to_string(),
12369            _ => name.to_string(),
12370        }
12371    };
12372    let mut out = Vec::new();
12373    for item in items {
12374        match item {
12375            SelectItem::Wildcard => {
12376                // v7.39 (round 511) — `*` never expands a system column, as
12377                // PG's does not. They join the schema only when the statement
12378                // asked for them, so this matters for the mixed shape
12379                // `SELECT *, ctid FROM t`.
12380                //
12381                // v7.39 (round 512) — by POSITION, not by name. Matching on
12382                // the name alone looked safe because PG reserves them, and it
12383                // is not: `pg_replication_slots` genuinely has a column called
12384                // `xmin`, and `SELECT * FROM pg_replication_slots` lost it.
12385                // Only the trailing six, in the order the scan appends them,
12386                // are the synthetic ones.
12387                let sys_skip = synthetic_system_positions(schema_cols);
12388                for (idx, col) in schema_cols.iter().enumerate() {
12389                    if sys_skip[idx] || idx >= visible {
12390                        continue;
12391                    }
12392                    out.push(ProjectedItem {
12393                        expr: Expr::Column(ColumnName {
12394                            qualifier: None,
12395                            name: col.name.clone(),
12396                        }),
12397                        output_name: bare_name(&col.name),
12398                        ty: col.ty,
12399                        nullable: col.nullable,
12400                        user_enum_type: col.user_enum_type.clone(),
12401                        mysql_fsp: col.mysql_fsp,
12402                        collation_name: col.collation_name.clone(),
12403                        fold_exempt: matches!(col.collation, spg_storage::Collation::Binary),
12404                        pads: crate::collate::pads_space(col.collation_name.as_deref()),
12405                    });
12406                }
12407            }
12408            // v7.39 (round 128) — `q.*` expands to every column belonging to
12409            // the qualifier `q`. Single-table schemas carry bare column names
12410            // reachable via `table_alias`; a join's combined schema carries
12411            // `alias.col` names, so a column belongs to `q` when its name has
12412            // the `q.` prefix. PG labels the expanded columns by their bare
12413            // name, so the `alias.` prefix is stripped from the output name.
12414            SelectItem::QualifiedWildcard(q) => {
12415                let prefix = alloc::format!("{q}.");
12416                let single_table = !table_alias.is_empty() && q == table_alias;
12417                let mut matched = 0usize;
12418                for col in &schema_cols[..visible] {
12419                    let belongs =
12420                        col.name.starts_with(&prefix) || (single_table && !col.name.contains('.'));
12421                    if !belongs {
12422                        continue;
12423                    }
12424                    matched += 1;
12425                    let output_name = col
12426                        .name
12427                        .strip_prefix(&prefix)
12428                        .unwrap_or(&col.name)
12429                        .to_string();
12430                    out.push(ProjectedItem {
12431                        expr: Expr::Column(ColumnName {
12432                            qualifier: None,
12433                            name: col.name.clone(),
12434                        }),
12435                        output_name,
12436                        ty: col.ty,
12437                        nullable: col.nullable,
12438                        user_enum_type: col.user_enum_type.clone(),
12439                        mysql_fsp: col.mysql_fsp,
12440                        collation_name: col.collation_name.clone(),
12441                        fold_exempt: matches!(col.collation, spg_storage::Collation::Binary),
12442                        pads: crate::collate::pads_space(col.collation_name.as_deref()),
12443                    });
12444                }
12445                if matched == 0 {
12446                    // `q.*` names no column, so the reference IS the star.
12447                    return Err(EngineError::Eval(EvalError::UnknownQualifier {
12448                        qualifier: q.clone(),
12449                        column: alloc::string::String::from("*"),
12450                    }));
12451                }
12452            }
12453            SelectItem::Expr { expr, alias } => {
12454                // Plain column ref keeps full schema info (real type +
12455                // nullability). For compound expressions try the
12456                // describe-side function-return-type table first
12457                // (e.g. `SELECT now()` → Timestamptz, `SELECT
12458                // concat(…)` → Text). Falls back to nullable Text
12459                // for shapes the describe path can't resolve.
12460                if let Expr::Column(c) = expr {
12461                    let sch = resolve_projection_column(c, schema_cols, table_alias, mysql)?;
12462                    let output_name = alias.clone().unwrap_or_else(|| c.name.clone());
12463                    out.push(ProjectedItem {
12464                        expr: expr.clone(),
12465                        output_name,
12466                        ty: sch.ty,
12467                        nullable: sch.nullable,
12468                        // v7.39 (read01 round 54) — a bare enum column keeps
12469                        // its enum identity through the projection.
12470                        user_enum_type: sch.user_enum_type.clone(),
12471                        mysql_fsp: sch.mysql_fsp,
12472                        collation_name: sch.collation_name.clone(),
12473                        // v7.38.13 — and its byte-wise-ness. This is the
12474                        // site `SELECT DISTINCT t FROM t` arrives at.
12475                        fold_exempt: matches!(sch.collation, spg_storage::Collation::Binary),
12476                        pads: crate::collate::pads_space(sch.collation_name.as_deref()),
12477                    });
12478                } else if let Some(shape) = describe::describe_expr_in(expr, schema_cols, cat) {
12479                    let output_name = alias
12480                        .clone()
12481                        .unwrap_or_else(|| default_output_name(expr, mysql));
12482                    out.push(ProjectedItem {
12483                        expr: expr.clone(),
12484                        // v7.38.18 — a projected EXPRESSION has no column collation
12485                        // to read, so it takes the session default, which is MySQL
12486                        // 8.0's `utf8mb4_0900_ai_ci`: NO PAD.
12487                        pads: false,
12488                        output_name,
12489                        ty: shape.ty,
12490                        // v7.39 (round 258) — a projected EXPRESSION keeps its
12491                        // enum identity too, not just a bare column. `FROM
12492                        // (VALUES ('happy'::mood), …) t(m)` lowers to constant
12493                        // SELECTs, so the derived column arrived here as a cast
12494                        // and lost the enum — making the outer ORDER BY / min /
12495                        // max / array_agg sort by the label's TEXT.
12496                        nullable: shape.nullable,
12497                        user_enum_type: None,
12498                        mysql_fsp: crate::eval::expr_mysql_fsp(expr, schema_cols),
12499                        // A bare column reference keeps its collation; any
12500                        // other expression produces a new value and has none.
12501                        collation_name: match expr {
12502                            Expr::Column(c) => schema_cols
12503                                .iter()
12504                                .find(|sc| sc.name.eq_ignore_ascii_case(&c.name))
12505                                .and_then(|sc| sc.collation_name.clone()),
12506                            _ => None,
12507                        },
12508                        fold_exempt: match expr {
12509                            Expr::Column(c) => schema_cols
12510                                .iter()
12511                                .find(|sc| sc.name.eq_ignore_ascii_case(&c.name))
12512                                .is_some_and(|sc| {
12513                                    matches!(sc.collation, spg_storage::Collation::Binary)
12514                                }),
12515                            // Not a column: no declared collation to honour,
12516                            // so the session default applies and it folds.
12517                            _ => false,
12518                        },
12519                    });
12520                } else {
12521                    let output_name = alias
12522                        .clone()
12523                        .unwrap_or_else(|| default_output_name(expr, mysql));
12524                    out.push(ProjectedItem {
12525                        expr: expr.clone(),
12526                        // v7.38.18 — a projected EXPRESSION has no column collation
12527                        // to read, so it takes the session default, which is MySQL
12528                        // 8.0's `utf8mb4_0900_ai_ci`: NO PAD.
12529                        pads: false,
12530                        output_name,
12531                        // A user ENUM has no DataType of its own, so
12532                        // `describe_expr` cannot type `'ok'::mood` and the
12533                        // item lands HERE, defaulting to text — which is why
12534                        // pg_typeof answered `text` and a derived table sorted
12535                        // enum values by their label.
12536                        ty: DataType::Text,
12537                        nullable: true,
12538                        user_enum_type: crate::eval::expr_enum_type_name_pub(expr, schema_cols)
12539                            .map(alloc::string::String::from),
12540                        mysql_fsp: crate::eval::expr_mysql_fsp(expr, schema_cols),
12541                        collation_name: match expr {
12542                            Expr::Column(c) => schema_cols
12543                                .iter()
12544                                .find(|sc| sc.name.eq_ignore_ascii_case(&c.name))
12545                                .and_then(|sc| sc.collation_name.clone()),
12546                            _ => None,
12547                        },
12548                        fold_exempt: match expr {
12549                            Expr::Column(c) => schema_cols
12550                                .iter()
12551                                .find(|sc| sc.name.eq_ignore_ascii_case(&c.name))
12552                                .is_some_and(|sc| {
12553                                    matches!(sc.collation, spg_storage::Collation::Binary)
12554                                }),
12555                            // Not a column: no declared collation to honour,
12556                            // so the session default applies and it folds.
12557                            _ => false,
12558                        },
12559                    });
12560                }
12561            }
12562        }
12563    }
12564    Ok(out)
12565}
12566
12567// ---- v4.12 window-function helpers ----
12568// The (partition-key, order-key, original-index) tuple shape used
12569// across these helpers is intrinsic to the planner. Factoring it
12570// into a typedef adds indirection without making the code clearer,
12571// so several lints are allowed inline on the affected functions
12572// rather than module-wide.
12573
12574/// v4.22: pick more specific column types from observed rows when
12575/// the projection builder defaulted to Text (the v1.x behavior for
12576/// non-column expressions). Lets `WITH t(n) AS (SELECT 1 ...)`
12577/// land an Int column in the CTE storage table rather than failing
12578/// the insert with "expected TEXT, got INT".
12579pub(crate) fn infer_column_types(
12580    columns: &[ColumnSchema],
12581    rows: &[Row<'static>],
12582) -> Vec<ColumnSchema> {
12583    let mut out = columns.to_vec();
12584    for (col_idx, col) in out.iter_mut().enumerate() {
12585        if col.ty != DataType::Text {
12586            continue;
12587        }
12588        let mut inferred: Option<DataType> = None;
12589        let mut all_null = true;
12590        for row in rows {
12591            let Some(v) = row.values.get(col_idx) else {
12592                continue;
12593            };
12594            let ty = match v {
12595                Value::Null => continue,
12596                Value::SmallInt(_) => DataType::SmallInt,
12597                Value::Int(_) => DataType::Int,
12598                Value::BigInt(_) => DataType::BigInt,
12599                Value::Float(_) => DataType::Float,
12600                Value::Bool(_) => DataType::Bool,
12601                Value::Vector(_) => DataType::Vector {
12602                    dim: 0,
12603                    encoding: VecEncoding::F32,
12604                },
12605                // v7.38 (read01 U16) — carry array values through with an
12606                // array type so a recursive CTE that projects an array
12607                // (e.g. a SEARCH/CYCLE ord / path column) types the working
12608                // column as an array, not Text.
12609                Value::TextArray(_) => DataType::TextArray,
12610                Value::IntArray(_) => DataType::IntArray,
12611                Value::BigIntArray(_) => DataType::BigIntArray,
12612                Value::SmallIntArray(_) => DataType::SmallIntArray,
12613                Value::FloatArray(_) => DataType::FloatArray,
12614                Value::BoolArray(_) => DataType::BoolArray,
12615                // v7.39 (GUC knife 2) — an interval projection describes
12616                // as INTERVAL (typed drivers read the RowDescription OID).
12617                Value::Interval { .. } => DataType::Interval,
12618                _ => DataType::Text,
12619            };
12620            all_null = false;
12621            inferred = Some(match inferred {
12622                None => ty,
12623                Some(prev) if prev == ty => prev,
12624                Some(_) => DataType::Text,
12625            });
12626        }
12627        if let Some(t) = inferred {
12628            col.ty = t;
12629            col.nullable = true;
12630        } else if all_null {
12631            col.nullable = true;
12632        }
12633    }
12634    out
12635}
12636
12637/// Numeric widening rank for UNION type resolution (higher = wider).
12638fn numeric_rank(t: DataType) -> Option<u8> {
12639    match t {
12640        DataType::SmallInt => Some(1),
12641        DataType::Int => Some(2),
12642        DataType::BigInt => Some(3),
12643        DataType::Numeric { .. } => Some(4),
12644        DataType::Float => Some(5),
12645        _ => None,
12646    }
12647}
12648
12649/// Resolve the common result type for a UNION / VALUES column from the
12650/// set of concrete (non-NULL) branch types, following the safe subset
12651/// of PG's type resolution:
12652///   * all-numeric  → the widest numeric (int ∪ bigint → bigint, … ∪
12653///     numeric → numeric, … ∪ float → float);
12654///   * DATE ∪ TIMESTAMP → TIMESTAMP;
12655///   * exactly one concrete non-TEXT type mixed with TEXT literals →
12656///     that concrete type (the TEXT cells get parsed into it).
12657/// Returns `None` for anything ambiguous, so the caller leaves the
12658/// column untouched rather than risk a wrong or failing coercion.
12659fn resolve_union_common_type(types: &[DataType]) -> Option<DataType> {
12660    // NB: types are collected from RUNTIME values, which are coarser
12661    // than the schema (e.g. a timestamptz cell is Value::Timestamp), so
12662    // a single-concrete-type fast path must NOT overwrite the column
12663    // type — it would downgrade tstz to ts. NULL-only unification (PG:
12664    // `VALUES (NULL),(1.5)` types the column numeric even on the NULL
12665    // row's pg_typeof) needs schema-level resolution — recorded, not
12666    // attempted here.
12667    if types.len() < 2 {
12668        return None;
12669    }
12670    if types.iter().all(|t| numeric_rank(*t).is_some()) {
12671        return types
12672            .iter()
12673            .max_by_key(|t| numeric_rank(**t).unwrap_or(0))
12674            .copied();
12675    }
12676    let non_text: Vec<&DataType> = types
12677        .iter()
12678        .filter(|t| !matches!(t, DataType::Text))
12679        .collect();
12680    // v7.38 (T-tstz Phase 1) — temporal common type, per PG18.4: if any branch
12681    // is timestamptz the result is timestamptz (tstz ∪ ts, tstz ∪ date), else
12682    // if any is timestamp the result is timestamp (ts ∪ date). All values are
12683    // the same UTC-micros instant, so widening date/ts to tstz is lossless.
12684    if non_text.iter().all(|t| {
12685        matches!(
12686            t,
12687            DataType::Date | DataType::Timestamp | DataType::Timestamptz
12688        )
12689    }) && non_text
12690        .iter()
12691        .any(|t| matches!(t, DataType::Timestamp | DataType::Timestamptz))
12692    {
12693        if non_text.iter().any(|t| matches!(t, DataType::Timestamptz)) {
12694            return Some(DataType::Timestamptz);
12695        }
12696        return Some(DataType::Timestamp);
12697    }
12698    // A single concrete non-TEXT type mixed with TEXT literals.
12699    if non_text.len() == 1 {
12700        return Some(*non_text[0]);
12701    }
12702    // v7.37.16 — SEVERAL concrete types mixed with TEXT literals
12703    // (`VALUES ('NaN'::float8),(1.0),('NaN')` → float8 ∪ numeric ∪
12704    // text): resolve the concrete set first (PG treats the unknown-
12705    // typed string literals as castable to whatever the knowns
12706    // resolve to), then the TEXT cells parse into that target — the
12707    // caller's coercion dry-run still abandons the column if any
12708    // literal doesn't parse.
12709    if !non_text.is_empty() && non_text.len() < types.len() {
12710        let concrete: Vec<DataType> = non_text.iter().map(|t| **t).collect();
12711        return resolve_union_common_type(&concrete);
12712    }
12713    None
12714}
12715
12716/// Coerce every cell of a UNION / VALUES result column to one common
12717/// type (see [`resolve_union_common_type`]). Conservative: a column
12718/// whose branches already agree, or whose types don't resolve, or where
12719/// any cell fails to coerce, is left exactly as it was — this never
12720/// turns a previously-working query into an error.
12721fn unify_union_columns(columns: &mut [ColumnSchema], rows: &mut [Row<'static>]) {
12722    for col_idx in 0..columns.len() {
12723        let mut seen: Vec<DataType> = Vec::new();
12724        for row in rows.iter() {
12725            if let Some(dt) = row.values.get(col_idx).and_then(Value::data_type) {
12726                if !seen.contains(&dt) {
12727                    seen.push(dt);
12728                }
12729            }
12730        }
12731        // v7.37.16 — a single concrete runtime type under a TEXT-typed
12732        // column means the column type came off a NULL (or unknown-text)
12733        // branch: NULL literals describe as TEXT (`L::Null → Text`), so
12734        // `VALUES (NULL),(1.5)` left the column "text" while every
12735        // non-NULL cell is numeric. Adopt the concrete type — schema
12736        // only, no cell changes. tstz-safe by construction: a real
12737        // timestamptz column's schema type is Timestamptz, not Text, so
12738        // the coarser runtime type (Value::Timestamp) can't downgrade it
12739        // through this arm; and a real text column's non-NULL cells are
12740        // Text, which keeps seen == [Text] and skips it.
12741        if seen.len() == 1
12742            && matches!(columns[col_idx].ty, DataType::Text)
12743            && !matches!(seen[0], DataType::Text)
12744        {
12745            columns[col_idx].ty = seen[0];
12746            continue;
12747        }
12748        let Some(target) = resolve_union_common_type(&seen) else {
12749            continue;
12750        };
12751        // v7.38 (read01) — an unconstrained NUMERIC result column keeps each
12752        // value's own scale in PG (`VALUES (1.0),(1.00)` renders `1.0` / `1.00`,
12753        // not `1.00` / `1.00`). So when the common type is NUMERIC, leave an
12754        // existing numeric cell untouched and only promote integers (to scale 0)
12755        // rather than rescaling everything to the widest scale.
12756        let scale_preserving_numeric = matches!(target, DataType::Numeric { .. });
12757        // Dry-run the coercion; abandon the whole column if any fails.
12758        let mut coerced: Vec<Option<Value<'static>>> = Vec::with_capacity(rows.len());
12759        let mut ok = true;
12760        for row in rows.iter() {
12761            match row.values.get(col_idx) {
12762                Some(Value::Numeric { .. }) if scale_preserving_numeric => {
12763                    coerced.push(Some(row.values[col_idx].clone()));
12764                }
12765                Some(v) => {
12766                    let cell_target = if scale_preserving_numeric {
12767                        DataType::Numeric {
12768                            precision: 0,
12769                            scale: 0,
12770                        }
12771                    } else {
12772                        target
12773                    };
12774                    match crate::conversions::coerce_value(
12775                        v.clone(),
12776                        cell_target,
12777                        &columns[col_idx].name,
12778                        col_idx,
12779                    ) {
12780                        Ok(cv) => coerced.push(Some(cv)),
12781                        Err(_) => {
12782                            ok = false;
12783                            break;
12784                        }
12785                    }
12786                }
12787                None => coerced.push(None),
12788            }
12789        }
12790        if !ok {
12791            continue;
12792        }
12793        for (row, cv) in rows.iter_mut().zip(coerced) {
12794            if let (Some(slot), Some(nv)) = (row.values.get_mut(col_idx), cv) {
12795                *slot = nv;
12796            }
12797        }
12798        columns[col_idx].ty = target;
12799    }
12800}
12801
12802/// v4.22: encode a Row to a comparable byte key for UNION-DISTINCT
12803/// dedup inside the recursive iteration. Crude but deterministic
12804/// — Debug prints embed type discriminants so NULL ≠ "" ≠ 0.
12805fn encode_row_key(row: &Row<'static>) -> Vec<u8> {
12806    let mut out = Vec::new();
12807    for v in &row.values {
12808        // v7.38 (read01) — UNION / DISTINCT dedup must treat numerically-equal
12809        // exact values as one, regardless of type or scale (`1 = 1.0 = 1.00`),
12810        // like PG (and like GROUP BY, which already normalizes). The old
12811        // `{v:?}` key made `Numeric{10,1}` differ from `Numeric{100,2}`. Encode
12812        // the exact-decimal family through one scale-stripped canonical form.
12813        match v {
12814            Value::SmallInt(n) => encode_numeric_key(&mut out, i128::from(*n), 0),
12815            Value::Int(n) => encode_numeric_key(&mut out, i128::from(*n), 0),
12816            Value::BigInt(n) => encode_numeric_key(&mut out, i128::from(*n), 0),
12817            Value::Numeric { scaled, scale, .. } => encode_numeric_key(&mut out, *scaled, *scale),
12818            other => {
12819                let s = alloc::format!("{other:?}|");
12820                out.extend_from_slice(s.as_bytes());
12821            }
12822        }
12823    }
12824    out
12825}
12826
12827/// Append a scale-independent canonical key for an exact-decimal value: strip
12828/// trailing fractional zeros so `1`, `1.0`, `1.00` all key the same. The `\x01`
12829/// tag keeps a numeric key from colliding with a text value's `{v:?}` form.
12830fn encode_numeric_key(out: &mut Vec<u8>, mut scaled: i128, mut scale: u16) {
12831    while scale > 0 && scaled % 10 == 0 {
12832        scaled /= 10;
12833        scale -= 1;
12834    }
12835    let s = alloc::format!("\u{1}{scaled}e-{scale}|");
12836    out.extend_from_slice(s.as_bytes());
12837}
12838
12839/// Multi-arg `unnest(a, b, …)` — evaluate each array argument
12840/// (uncorrelated; outer refs were substituted upstream), then zip
12841/// them in parallel, NULL-padding shorter arrays to the longest
12842/// (PG's ROWS FROM shorthand). Shared by the primary-position
12843/// executor and the join-position materialiser, which both detect
12844/// the parser's `__unnest_zip` marker call.
12845pub(crate) fn unnest_zip_rows(
12846    args: &[Expr],
12847) -> Result<(alloc::vec::Vec<DataType>, alloc::vec::Vec<Row<'static>>), EngineError> {
12848    let empty_schema: alloc::vec::Vec<ColumnSchema> = alloc::vec::Vec::new();
12849    let ctx = EvalContext::new(&empty_schema, None);
12850    let dummy_row = Row::new(alloc::vec::Vec::new());
12851    let mut dtypes: alloc::vec::Vec<DataType> = alloc::vec::Vec::with_capacity(args.len());
12852    let mut columns: alloc::vec::Vec<alloc::vec::Vec<Value<'static>>> =
12853        alloc::vec::Vec::with_capacity(args.len());
12854    for a in args {
12855        let v = eval::eval_expr(a, &dummy_row, &ctx).map_err(EngineError::Eval)?;
12856        // v7.39.13 — the element menu the rest of the workspace already
12857        // has, not a third copy of a shortened one.
12858        //
12859        // This arm listed Text, Int and BigInt and refused everything
12860        // else, so `unnest(uuid[], text[])` raised while
12861        // `unnest(uuid[])` — a different path — did not. A shipped
12862        // endpoint of a customer's returned 500 on every call because
12863        // of it. `array_elements` and `array_element_type` are the two
12864        // halves of the menu that `array_element_at`'s own comment
12865        // describes: "previously only matched Text/Int/BigInt arrays
12866        // and errored on every other element type". Same sentence,
12867        // third arm.
12868        let (dt, items): (DataType, alloc::vec::Vec<Value<'static>>) = if matches!(v, Value::Null) {
12869            (DataType::Text, alloc::vec::Vec::new())
12870        } else if let Some(items) = crate::eval::values::array_elements(&v) {
12871            let dt = v
12872                .data_type()
12873                .and_then(crate::describe::array_element_type)
12874                .unwrap_or(DataType::Text);
12875            (dt, items)
12876        } else {
12877            return Err(EngineError::Unsupported(alloc::format!(
12878                "unnest() expects array arguments, got {}",
12879                crate::conversions::pg_type_name_for_error_opt(v.data_type())
12880            )));
12881        };
12882        dtypes.push(dt);
12883        columns.push(items);
12884    }
12885    let max_len = columns.iter().map(|c| c.len()).max().unwrap_or(0);
12886    let mut rows: alloc::vec::Vec<Row<'static>> = alloc::vec::Vec::with_capacity(max_len);
12887    for i in 0..max_len {
12888        let vals: alloc::vec::Vec<Value<'static>> = columns
12889            .iter()
12890            .map(|c| c.get(i).cloned().unwrap_or(Value::Null))
12891            .collect();
12892        rows.push(Row::new(vals));
12893    }
12894    Ok((dtypes, rows))
12895}
12896
12897/// Detect the parser's multi-arg unnest marker on an unnest_expr.
12898pub(crate) fn unnest_zip_args(expr: &Expr) -> Option<&[Expr]> {
12899    match expr {
12900        Expr::FunctionCall { name, args } if name == "__unnest_zip" => Some(args.as_slice()),
12901        _ => None,
12902    }
12903}
12904
12905/// Evaluate generate_series arguments (uncorrelated — outer refs
12906/// were substituted upstream where applicable) and build the row
12907/// stream. Dispatches on the start value's shape and rejects
12908/// mixed-shape calls early (e.g. start = timestamp, stop =
12909/// integer) so the caller gets a clean error rather than a panic.
12910/// Shared by the primary-position executor and the join-position
12911/// materialiser.
12912pub(crate) fn generate_series_rows(
12913    args: &[Expr],
12914    cancel: &CancelToken<'_>,
12915) -> Result<(DataType, alloc::vec::Vec<Row<'static>>), EngineError> {
12916    let empty_schema: alloc::vec::Vec<ColumnSchema> = alloc::vec::Vec::new();
12917    let ctx = EvalContext::new(&empty_schema, None);
12918    let dummy_row = Row::new(alloc::vec::Vec::new());
12919    let mut arg_values: alloc::vec::Vec<Value<'static>> =
12920        alloc::vec::Vec::with_capacity(args.len());
12921    for a in args {
12922        arg_values.push(eval::eval_expr(a, &dummy_row, &ctx).map_err(EngineError::Eval)?);
12923    }
12924    generate_series_from_values(arg_values, args, cancel)
12925}
12926
12927/// v7.39 (read01 round 96) — the value-producing core of `generate_series`,
12928/// split out so the SELECT-list SRF path (`top_level_srf_output`) shares the
12929/// full integer / numeric / timestamp overload set with the FROM-clause path.
12930/// Before this split the target-list arm reimplemented only the integer case,
12931/// so `SELECT generate_series(1,2), generate_series(ts, ts, interval)` yielded
12932/// NULL for the timestamp column instead of the series. `arg_values` are the
12933/// already-evaluated arguments; `args` is kept only for the timestamptz-vs-
12934/// timestamp type resolution (it inspects the argument expressions' types).
12935pub(crate) fn generate_series_from_values(
12936    mut arg_values: alloc::vec::Vec<Value<'static>>,
12937    args: &[Expr],
12938    cancel: &CancelToken<'_>,
12939) -> Result<(DataType, alloc::vec::Vec<Row<'static>>), EngineError> {
12940    // PG: a NULL bound or step yields zero rows (also keeps the
12941    // NULL-padded lateral probe alive — schema without data).
12942    if arg_values.iter().any(|v| matches!(v, Value::Null)) {
12943        return Ok((DataType::BigInt, alloc::vec::Vec::new()));
12944    }
12945    // PG resolves `generate_series(date, date, interval)` to the
12946    // timestamp/timestamptz overload by implicitly casting each date
12947    // bound up to a timestamp at midnight (verified vs live PG18.4:
12948    // date args yield rows anchored at 00:00:00). SPG's TZ-naive
12949    // timestamp model renders the same instants, so fold any Date
12950    // bound to its midnight Timestamp (canonical `days *
12951    // 86_400_000_000`, matching cast.rs `cast_to_timestamp`) before
12952    // the shape match so the existing timestamp arm drives the walk.
12953    // v7.39 (read01 round 76) — WHICH timestamp overload PG picks matters:
12954    // `generate_series(date, date, interval)` has no date overload, and among
12955    // the two candidates PG prefers the timestamptz one (timestamptz is the
12956    // preferred type of the datetime category), so the column comes back
12957    // `timestamp with time zone` — the rows render with a `+00` offset. A
12958    // timestamptz bound obviously lands there too. Only genuinely
12959    // timestamp-typed bounds keep the TZ-naive result type.
12960    let empty_cols: alloc::vec::Vec<ColumnSchema> = alloc::vec::Vec::new();
12961    let tz = arg_values.iter().any(|v| matches!(v, Value::Date(_)))
12962        || args.iter().any(|a| {
12963            crate::describe::describe_expr(a, &empty_cols)
12964                .is_some_and(|s| matches!(s.ty, DataType::Timestamptz))
12965        });
12966    for v in &mut arg_values {
12967        if let Value::Date(d) = *v {
12968            *v = Value::Timestamp(crate::conversions::date_days_to_micros(d));
12969        }
12970    }
12971    match arg_values.as_slice() {
12972        [Value::Timestamp(start), Value::Timestamp(stop), step] => {
12973            let interval_step = match step {
12974                Value::Interval { .. } => step.clone(),
12975                // v7.38 (read01) — PG resolves an unknown-type string step
12976                // (`generate_series(date, date, '2 days')`) to INTERVAL; accept
12977                // a bare text step by parsing it the same way `::interval` does.
12978                Value::Text(s) => crate::conversions::coerce_value(
12979                    Value::text(s.as_ref()),
12980                    DataType::Interval,
12981                    "",
12982                    0,
12983                )
12984                .map_err(|_| {
12985                    EngineError::Unsupported(alloc::format!(
12986                        "generate_series(timestamp, timestamp, …): \
12987                         could not parse step {s:?} as INTERVAL"
12988                    ))
12989                })?,
12990                other => {
12991                    return Err(EngineError::Unsupported(alloc::format!(
12992                        "generate_series(timestamp, timestamp, …): \
12993                         step must be INTERVAL, got {}",
12994                        crate::conversions::pg_type_name_for_error_opt(other.data_type())
12995                    )));
12996                }
12997            };
12998            let rows = generate_series_timestamps(*start, *stop, interval_step, cancel)?;
12999            Ok((
13000                if tz {
13001                    DataType::Timestamptz
13002                } else {
13003                    DataType::Timestamp
13004                },
13005                rows,
13006            ))
13007        }
13008        [start, stop, step]
13009            if value_is_integer(start) && value_is_integer(stop) && value_is_integer(step) =>
13010        {
13011            let s = value_to_i64(start);
13012            let e = value_to_i64(stop);
13013            let st = value_to_i64(step);
13014            // PG types the series by the argument type: int4 args → int4
13015            // elements, int8 (bigint) args → int8. Any BigInt operand widens.
13016            let wide = value_is_bigint(start) || value_is_bigint(stop) || value_is_bigint(step);
13017            let rows = generate_series_integers(s, e, st, wide, cancel)?;
13018            Ok((
13019                if wide {
13020                    DataType::BigInt
13021                } else {
13022                    DataType::Int
13023                },
13024                rows,
13025            ))
13026        }
13027        [start, stop] if value_is_integer(start) && value_is_integer(stop) => {
13028            let s = value_to_i64(start);
13029            let e = value_to_i64(stop);
13030            let wide = value_is_bigint(start) || value_is_bigint(stop);
13031            let rows = generate_series_integers(s, e, 1, wide, cancel)?;
13032            Ok((
13033                if wide {
13034                    DataType::BigInt
13035                } else {
13036                    DataType::Int
13037                },
13038                rows,
13039            ))
13040        }
13041        // v7.39 (read01 numeric.c) — the NUMERIC overload. PG walks the
13042        // series in exact numeric arithmetic; NaN / infinity bounds and a
13043        // zero step get dedicated wordings, and a mixed int/numeric call
13044        // resolves here via the implicit int→numeric cast.
13045        [_, _] | [_, _, _]
13046            if arg_values
13047                .iter()
13048                .any(|v| matches!(v, Value::Numeric { .. } | Value::NumericBig(_)))
13049                && arg_values.iter().all(|v| {
13050                    matches!(v, Value::Numeric { .. } | Value::NumericBig(_)) || value_is_integer(v)
13051                }) =>
13052        {
13053            use spg_storage::NumericKind as K;
13054            let words: [(&str, &str); 3] = [
13055                (
13056                    "start value cannot be NaN",
13057                    "start value cannot be infinity",
13058                ),
13059                ("stop value cannot be NaN", "stop value cannot be infinity"),
13060                ("step size cannot be NaN", "step size cannot be infinity"),
13061            ];
13062            for (i, v) in arg_values.iter().enumerate() {
13063                if let Value::Numeric { kind, .. } = v {
13064                    if *kind != K::Finite {
13065                        let (nan_w, inf_w) = words[i];
13066                        return Err(EngineError::Unsupported(
13067                            if *kind == K::NaN { nan_w } else { inf_w }.into(),
13068                        ));
13069                    }
13070                }
13071            }
13072            let big =
13073                |v: &Value<'_>| eval::binop::value_to_bignum(v).expect("finite numeric or integer");
13074            let start = big(&arg_values[0]);
13075            let stop = big(&arg_values[1]);
13076            let step = if arg_values.len() == 3 {
13077                big(&arg_values[2])
13078            } else {
13079                spg_storage::bignum::BigNumeric::from_i128(1, 0)
13080            };
13081            if step.is_zero() {
13082                return Err(EngineError::Unsupported(
13083                    "step size cannot equal zero".into(),
13084                ));
13085            }
13086            let descending = step.parts().0;
13087            let mut rows = alloc::vec::Vec::new();
13088            let mut cur = start;
13089            const MAX_ROWS: usize = 10_000_000;
13090            loop {
13091                cancel.check()?;
13092                let c = cur.cmp(&stop);
13093                if descending {
13094                    if c == core::cmp::Ordering::Less {
13095                        break;
13096                    }
13097                } else if c == core::cmp::Ordering::Greater {
13098                    break;
13099                }
13100                if rows.len() >= MAX_ROWS {
13101                    return Err(EngineError::Unsupported(alloc::format!(
13102                        "generate_series() result exceeds {MAX_ROWS} rows"
13103                    )));
13104                }
13105                rows.push(Row::new(alloc::vec![eval::binop::bignum_to_value(
13106                    cur.clone()
13107                )]));
13108                cur = cur.add(&step);
13109            }
13110            Ok((
13111                DataType::Numeric {
13112                    precision: 0,
13113                    scale: 0,
13114                },
13115                rows,
13116            ))
13117        }
13118        _ => Err(EngineError::Unsupported(alloc::format!(
13119            "generate_series(): v7.17 supports integer or (timestamp, timestamp, interval) \
13120             argument shapes; got {}",
13121            arg_values
13122                .iter()
13123                .map(|v| crate::conversions::pg_type_name_for_error_opt(v.data_type()))
13124                .collect::<alloc::vec::Vec<_>>()
13125                .join(", ")
13126        ))),
13127    }
13128}
13129
13130/// v7.17.0 Phase 3.10 — integer-mode generate_series materialiser.
13131/// Step direction follows the sign: positive step iterates upward
13132/// (stops when current > stop); negative iterates downward; zero
13133/// errors. Caller-facing row stream is `BigInt`-typed so a single
13134/// projection schema covers SmallInt / Int / BigInt callers.
13135fn generate_series_integers(
13136    start: i64,
13137    stop: i64,
13138    step: i64,
13139    wide: bool,
13140    cancel: &CancelToken<'_>,
13141) -> Result<alloc::vec::Vec<Row<'static>>, EngineError> {
13142    if step == 0 {
13143        return Err(EngineError::Unsupported(
13144            "step size cannot equal zero".into(),
13145        ));
13146    }
13147    let mut out = alloc::vec::Vec::new();
13148    let mut cur = start;
13149    // Hard cap to keep a runaway call from eating all memory. PG
13150    // has no such cap but does honour query timeout; SPG's cancel
13151    // token will fire too — this is a defense-in-depth backstop.
13152    const MAX_ROWS: usize = 10_000_000;
13153    loop {
13154        cancel.check()?;
13155        if step > 0 && cur > stop {
13156            break;
13157        }
13158        if step < 0 && cur < stop {
13159            break;
13160        }
13161        out.push(Row::new(alloc::vec![if wide {
13162            Value::BigInt(cur)
13163        } else {
13164            Value::Int(cur as i32)
13165        }]));
13166        if out.len() > MAX_ROWS {
13167            return Err(EngineError::Unsupported(alloc::format!(
13168                "generate_series(): exceeded {MAX_ROWS} rows; \
13169                 narrow start/stop or use a larger step"
13170            )));
13171        }
13172        cur = match cur.checked_add(step) {
13173            Some(n) => n,
13174            None => break,
13175        };
13176    }
13177    Ok(out)
13178}
13179
13180/// v7.17.0 Phase 3.10 — timestamp-mode generate_series. step is a
13181/// `Value::Interval { months, micros }` per the caller's guard;
13182/// each iteration adds the interval via `apply_binary_interval`
13183/// so month-shifting handles short-month rollover (PG semantics).
13184fn generate_series_timestamps(
13185    start: i64,
13186    stop: i64,
13187    step: Value,
13188    cancel: &CancelToken<'_>,
13189) -> Result<alloc::vec::Vec<Row<'static>>, EngineError> {
13190    let (months, days, micros) = match &step {
13191        Value::Interval {
13192            months,
13193            days,
13194            micros,
13195            kind,
13196        } => (*months, *days, *micros),
13197        _ => unreachable!("caller guards step.is_interval"),
13198    };
13199    if months == 0 && days == 0 && micros == 0 {
13200        return Err(EngineError::Unsupported(
13201            "generate_series(): INTERVAL step cannot be zero".into(),
13202        ));
13203    }
13204    let ascending = months > 0 || days > 0 || micros > 0;
13205    let mut out = alloc::vec::Vec::new();
13206    let mut cur = Value::Timestamp(start);
13207    const MAX_ROWS: usize = 10_000_000;
13208    loop {
13209        cancel.check()?;
13210        let cur_t = match cur {
13211            Value::Timestamp(t) => t,
13212            _ => unreachable!("loop invariant: cur is Timestamp"),
13213        };
13214        if ascending && cur_t > stop {
13215            break;
13216        }
13217        if !ascending && cur_t < stop {
13218            break;
13219        }
13220        out.push(Row::new(alloc::vec![Value::Timestamp(cur_t)]));
13221        if out.len() > MAX_ROWS {
13222            return Err(EngineError::Unsupported(alloc::format!(
13223                "generate_series(): exceeded {MAX_ROWS} rows; \
13224                 narrow start/stop or use a larger step"
13225            )));
13226        }
13227        let next = eval::apply_binary_interval(
13228            spg_sql::ast::BinOp::Add,
13229            &cur,
13230            &Value::Interval {
13231                months,
13232                days,
13233                micros,
13234                kind: spg_storage::IntervalKind::Finite,
13235            },
13236        )
13237        .map_err(EngineError::Eval)?;
13238        cur = match next {
13239            Some(v) => v,
13240            None => break,
13241        };
13242    }
13243    Ok(out)
13244}
13245
13246/// v7.17.0 Phase 3.P0-49 — PG-canonical: `FETCH FIRST <n> ROWS
13247/// WITH TIES` requires an `ORDER BY`. Without one, there's no
13248/// way to identify "ties" deterministically, so PG errors at
13249/// plan time. SPG mirrors that surface so the same DDL / app
13250/// behaviour holds on cutover.
13251fn check_with_ties_requires_order_by(stmt: &SelectStatement) -> Result<(), EngineError> {
13252    if stmt.limit_with_ties && stmt.order_by.is_empty() {
13253        return Err(EngineError::Unsupported(alloc::string::String::from(
13254            "WITH TIES cannot be specified without ORDER BY clause",
13255        )));
13256    }
13257    Ok(())
13258}
13259
13260/// v7.19 P5 — true iff `expr` is `unnest(arg)` at the top level
13261/// (case-insensitive). Used by `exec_select_cancel`'s
13262/// projection loop to detect Set-Returning-Function rows that
13263/// need per-row expansion. Only the top-level call counts —
13264/// `coalesce(unnest(arr), 'x')` is NOT a SRF row from the
13265/// projection's perspective; it would surface as an "unknown
13266/// function" mismatch downstream, which is what we want
13267/// (multi-SRF / nested SRF is documented carve-out for v7.19).
13268fn is_top_level_unnest(expr: &spg_sql::ast::Expr) -> bool {
13269    top_level_srf_kind(expr).is_some()
13270}
13271
13272/// v7.38 (read01, T15) — which set-returning function a top-level SELECT-list
13273/// call is, if any. Matching is allocation-free (`eq_ignore_ascii_case`, no
13274/// `to_ascii_lowercase`) because `top_level_srf_output` classifies once per
13275/// source row.
13276#[derive(Clone, Copy, PartialEq, Eq)]
13277pub(crate) enum SrfKind {
13278    Unnest,
13279    /// v7.39 (read01 round 67) — `generate_series(a, b[, step])` in the target
13280    /// list. It used to be handled ONLY by the parser's lift into FROM, so a
13281    /// second one in the same list came back as "unknown function".
13282    GenerateSeries,
13283    GenerateSubscripts,
13284    /// `_text` variants unwrap scalars to their lexeme; the plain forms render
13285    /// every value as compact JSON text.
13286    ArrayElements {
13287        as_text: bool,
13288    },
13289    PathQuery,
13290    RegexpMatches,
13291    Each {
13292        as_text: bool,
13293    },
13294    ObjectKeys,
13295}
13296
13297/// Case-insensitive match against any of `names`.
13298fn name_is(name: &str, names: &[&str]) -> bool {
13299    names.iter().any(|n| name.eq_ignore_ascii_case(n))
13300}
13301
13302pub(crate) fn top_level_srf_kind(expr: &spg_sql::ast::Expr) -> Option<SrfKind> {
13303    let spg_sql::ast::Expr::FunctionCall { name, args } = expr else {
13304        return None;
13305    };
13306    let n = args.len();
13307    // v7.38 (read01) — generate_subscripts(arr, dim) is set-returning in the
13308    // SELECT list (it returned an array there before) and shares the unnest
13309    // expansion machinery.
13310    if n == 1 && name.eq_ignore_ascii_case("unnest") {
13311        return Some(SrfKind::Unnest);
13312    }
13313    if (2..=3).contains(&n) && name.eq_ignore_ascii_case("generate_series") {
13314        return Some(SrfKind::GenerateSeries);
13315    }
13316    if n == 2 && name.eq_ignore_ascii_case("generate_subscripts") {
13317        return Some(SrfKind::GenerateSubscripts);
13318    }
13319    // v7.38 (read01, T15) — the jsonb/json SRF family and regexp_matches expand
13320    // per element / match in the SELECT list; they collapsed to a single row
13321    // (a TextArray, or an "unknown function" error for `each`) before.
13322    if n == 1 && name_is(name, &["jsonb_array_elements", "json_array_elements"]) {
13323        return Some(SrfKind::ArrayElements { as_text: false });
13324    }
13325    if n == 1
13326        && name_is(
13327            name,
13328            &["jsonb_array_elements_text", "json_array_elements_text"],
13329        )
13330    {
13331        return Some(SrfKind::ArrayElements { as_text: true });
13332    }
13333    // v7.39 (jsonpath depth) — 3rd arg = vars, 4th = silent.
13334    if (2..=4).contains(&n) && name_is(name, &["jsonb_path_query", "json_path_query"]) {
13335        return Some(SrfKind::PathQuery);
13336    }
13337    if (2..=3).contains(&n) && name.eq_ignore_ascii_case("regexp_matches") {
13338        return Some(SrfKind::RegexpMatches);
13339    }
13340    if n == 1 && name_is(name, &["jsonb_each", "json_each"]) {
13341        return Some(SrfKind::Each { as_text: false });
13342    }
13343    if n == 1 && name_is(name, &["jsonb_each_text", "json_each_text"]) {
13344        return Some(SrfKind::Each { as_text: true });
13345    }
13346    if n == 1 && name_is(name, &["jsonb_object_keys", "json_object_keys"]) {
13347        return Some(SrfKind::ObjectKeys);
13348    }
13349    None
13350}
13351
13352/// v7.38 (read01) — the row-set a top-level SELECT-list SRF emits: the elements
13353/// for `unnest(arr)`, or the 1-based subscripts `1..=length` for
13354/// `generate_subscripts(arr, 1)` (a non-1 dimension over a 1-D array yields no
13355/// rows, as in PG).
13356pub(crate) fn top_level_srf_output(
13357    expr: &spg_sql::ast::Expr,
13358    row: &Row<'static>,
13359    ctx: &EvalContext<'_>,
13360) -> Result<Vec<Value<'static>>, EngineError> {
13361    let (Some(kind), spg_sql::ast::Expr::FunctionCall { name, args }) =
13362        (top_level_srf_kind(expr), expr)
13363    else {
13364        return Err(EngineError::Unsupported(
13365            "expected a SELECT-list SRF call".into(),
13366        ));
13367    };
13368    match kind {
13369        SrfKind::Unnest => {
13370            // v7.39 (round 743) — `unnest(ARRAY[e1, …, ek])` evaluates
13371            // the elements DIRECTLY: the old path built the whole
13372            // Value::Array (one eval + a clone per element) only for
13373            // array_value_to_elements to clone every element back out.
13374            // Any other argument shape (a column, a function result)
13375            // keeps the build-then-split path.
13376            if let spg_sql::ast::Expr::Array(items) = &args[0] {
13377                return items
13378                    .iter()
13379                    .map(|e| eval::eval_expr(e, row, ctx).map_err(EngineError::Eval))
13380                    .collect();
13381            }
13382            let arr = eval::eval_expr(&args[0], row, ctx).map_err(EngineError::Eval)?;
13383            array_value_to_elements(&arr)
13384        }
13385        SrfKind::GenerateSeries => {
13386            // v7.39 (read01 round 96) — evaluate the args against the actual
13387            // row, then hand off to the shared core so the numeric and
13388            // timestamp/timestamptz overloads work here too (this arm used to
13389            // handle only integers, silently NULLing a temporal/numeric series
13390            // when it shared a target list with another SRF).
13391            let mut arg_values: Vec<Value<'static>> = Vec::with_capacity(args.len());
13392            for a in args {
13393                arg_values.push(eval::eval_expr(a, row, ctx).map_err(EngineError::Eval)?);
13394            }
13395            let (_, rows) = generate_series_from_values(arg_values, args, &CancelToken::none())?;
13396            Ok(rows
13397                .into_iter()
13398                .map(|r| r.values.into_iter().next().unwrap_or(Value::Null))
13399                .collect())
13400        }
13401        SrfKind::GenerateSubscripts => {
13402            let arr = eval::eval_expr(&args[0], row, ctx).map_err(EngineError::Eval)?;
13403            let dim = eval::eval_expr(&args[1], row, ctx).map_err(EngineError::Eval)?;
13404            if !matches!(dim, Value::Int(1) | Value::BigInt(1) | Value::SmallInt(1)) {
13405                return Ok(Vec::new());
13406            }
13407            let len = array_value_to_elements(&arr)?.len();
13408            Ok((1..=len).map(|i| Value::Int(i as i32)).collect())
13409        }
13410        // One Value per array element (`_text` → text / SQL NULL, plain → the
13411        // element's compact JSON text) — the element list the FROM-clause form
13412        // materialises.
13413        SrfKind::ArrayElements { as_text } => {
13414            let arg = eval::eval_expr(&args[0], row, ctx).map_err(EngineError::Eval)?;
13415            if matches!(arg, Value::Null) {
13416                return Ok(Vec::new());
13417            }
13418            let items =
13419                crate::json::array_element_rows(&arg, as_text, name).map_err(EngineError::Eval)?;
13420            Ok(items
13421                .into_iter()
13422                .map(|opt| opt.map(Value::text).unwrap_or(Value::Null))
13423                .collect())
13424        }
13425        // The scalar form already yields a TextArray of the keys (or errors on
13426        // a non-object, like PG); expand it into rows.
13427        SrfKind::ObjectKeys => {
13428            let v = eval::eval_expr(expr, row, ctx).map_err(EngineError::Eval)?;
13429            array_value_to_elements(&v)
13430        }
13431        // One row per match, each a text[] of the pattern's capture groups.
13432        SrfKind::RegexpMatches => {
13433            let vals: Vec<Value<'static>> = args
13434                .iter()
13435                .map(|a| eval::eval_expr(a, row, ctx).map_err(EngineError::Eval))
13436                .collect::<Result<_, _>>()?;
13437            crate::eval::regexp_matches_rows(&vals).map_err(EngineError::Eval)
13438        }
13439        // One composite `(key, value)` row per object member (plain → jsonb
13440        // value, `_text` → text / SQL NULL).
13441        SrfKind::Each { as_text } => {
13442            let arg = eval::eval_expr(&args[0], row, ctx).map_err(EngineError::Eval)?;
13443            if matches!(arg, Value::Null) {
13444                return Ok(Vec::new());
13445            }
13446            let pairs = crate::json::each_rows(&arg, as_text, name).map_err(EngineError::Eval)?;
13447            Ok(pairs
13448                .into_iter()
13449                .map(|(k, v)| {
13450                    let val = if as_text {
13451                        v.map(Value::text).unwrap_or(Value::Null)
13452                    } else {
13453                        v.map(Value::json).unwrap_or(Value::Null)
13454                    };
13455                    Value::Composite(alloc::vec![
13456                        ("key".to_string(), Value::text(k)),
13457                        ("value".to_string(), val),
13458                    ])
13459                })
13460                .collect())
13461        }
13462        // One Value per matched JSON value.
13463        SrfKind::PathQuery => {
13464            let doc = eval::eval_expr(&args[0], row, ctx).map_err(EngineError::Eval)?;
13465            let path = eval::eval_expr(&args[1], row, ctx).map_err(EngineError::Eval)?;
13466            // v7.39 — optional vars document (3rd arg).
13467            let vars = match args.get(2) {
13468                Some(a) => {
13469                    let v = eval::eval_expr(a, row, ctx).map_err(EngineError::Eval)?;
13470                    crate::json::parse_path_vars(&v).map_err(EngineError::Eval)?
13471                }
13472                None => None,
13473            };
13474            match crate::json::path_query_vars(&doc, &path, vars.as_ref())
13475                .map_err(EngineError::Eval)?
13476            {
13477                Value::Null => Ok(Vec::new()),
13478                Value::TextArray(items) => Ok(items
13479                    .into_iter()
13480                    .map(|opt| opt.map(Value::text).unwrap_or(Value::Null))
13481                    .collect()),
13482                other => Ok(alloc::vec![other]),
13483            }
13484        }
13485    }
13486}
13487
13488/// v7.19 P5 — turn an array-typed `Value` into the element list
13489/// `unnest()` projection emits. NULL → empty list (PG: `unnest(NULL)
13490/// = (no rows)`). Non-array values fall through to a type-mismatch
13491/// error.
13492pub(crate) fn array_value_to_elements(v: &Value) -> Result<Vec<Value<'static>>, EngineError> {
13493    // v7.39 (round 236) — PG unnests a multidimensional array into its
13494    // elements in row-major order (`unnest(ARRAY[[1,2],[3,4]])` is four
13495    // rows). SPG stores 2-D arrays as their own variants, which fell
13496    // through to the type-mismatch arm below.
13497    if let Some(flat) = crate::eval::values::flatten_2d(v) {
13498        return array_value_to_elements(&flat);
13499    }
13500    // v7.39.11 — every array-family value, through the one element
13501    // menu. The arms below name int / bigint / text / json and stop, so
13502    // `SELECT unnest(ARRAY[1,2]::smallint[])` raised "expects an array
13503    // argument, got smallint[]" — the type it had just been given —
13504    // and so did every catalog vector. Found while closing sentori's
13505    // §4 against 7.39.10; the FROM-clause unnest has the same arm.
13506    if crate::eval::values::array_len(v).is_some() {
13507        if let Some(elems) = crate::eval::values::array_elements(v) {
13508            return Ok(elems);
13509        }
13510    }
13511    match v {
13512        Value::Null => Ok(Vec::new()),
13513        Value::TextArray(items) => Ok(items
13514            .iter()
13515            .map(|opt| {
13516                opt.as_ref()
13517                    .map(|s| Value::text(s.clone()))
13518                    .unwrap_or(Value::Null)
13519            })
13520            .collect()),
13521        Value::IntArray(items) => Ok(items
13522            .iter()
13523            .map(|opt| opt.map(Value::Int).unwrap_or(Value::Null))
13524            .collect()),
13525        Value::BigIntArray(items) => Ok(items
13526            .iter()
13527            .map(|opt| opt.map(Value::BigInt).unwrap_or(Value::Null))
13528            .collect()),
13529        // v7.39 (read01 multirangetypes.c) — unnest(anymultirange): one
13530        // range per canonical span.
13531        Value::Multirange { kind, ranges } => Ok(ranges
13532            .iter()
13533            .map(|s| Value::Range {
13534                kind: *kind,
13535                lower: s.lower.clone(),
13536                upper: s.upper.clone(),
13537                lower_inc: s.lower_inc,
13538                upper_inc: s.upper_inc,
13539                empty: false,
13540            })
13541            .collect()),
13542        other => Err(EngineError::Eval(EvalError::TypeMismatch {
13543            detail: alloc::format!(
13544                "unnest() expects an array argument, got {}",
13545                crate::conversions::pg_type_name_for_error_opt(other.data_type())
13546            ),
13547        })),
13548    }
13549}
13550
13551impl Engine {
13552    /// v7.17.0 Phase 1.2 — find every catalog VIEW referenced in
13553    /// the SELECT's FROM / JOIN graph, re-parse each view's body
13554    /// source, and prepend it as a synthetic CTE on the
13555    /// returned SelectStatement. Returns `None` when no view
13556    /// references are found (caller proceeds with the original
13557    /// statement); returns `Some(rewritten)` otherwise (caller
13558    /// re-runs exec_select_cancel on the rewritten form so the
13559    /// regular CTE materialiser handles it).
13560    fn expand_views_in_select(
13561        &self,
13562        stmt: &SelectStatement,
13563    ) -> Result<Option<SelectStatement>, EngineError> {
13564        let cat = self.active_catalog();
13565        let mut referenced: Vec<String> = Vec::new();
13566        if let Some(from) = &stmt.from {
13567            collect_view_refs(&from.primary, cat, &mut referenced);
13568            for j in &from.joins {
13569                collect_view_refs(&j.table, cat, &mut referenced);
13570            }
13571        }
13572        // Don't expand a view name that's already shadowed by a
13573        // CTE on the same SELECT — the CTE wins per PG.
13574        referenced.retain(|n| !stmt.ctes.iter().any(|c| c.name == *n));
13575        if referenced.is_empty() {
13576            return Ok(None);
13577        }
13578        let mut new_ctes: Vec<spg_sql::ast::Cte> = Vec::with_capacity(referenced.len());
13579        for name in &referenced {
13580            let view = cat.view(name).ok_or_else(|| {
13581                EngineError::Storage(spg_storage::StorageError::Corrupt(alloc::format!(
13582                    "view {name:?} disappeared mid-expansion"
13583                )))
13584            })?;
13585            let parsed = spg_sql::parser::parse_statement(&view.body).map_err(|e| {
13586                EngineError::Unsupported(alloc::format!("view {name:?} body re-parse failed: {e}"))
13587            })?;
13588            let Statement::Select(body) = parsed else {
13589                return Err(EngineError::Unsupported(alloc::format!(
13590                    "view {name:?} body is not a SELECT (catalog corruption)"
13591                )));
13592            };
13593            new_ctes.push(spg_sql::ast::Cte {
13594                name: name.clone(),
13595                body: spg_sql::ast::CteBody::Select(body),
13596                recursive: false,
13597                column_overrides: view.columns.clone(),
13598                search: None,
13599                cycle: None,
13600            });
13601        }
13602        let mut out = stmt.clone();
13603        // Prepend so view CTEs are visible to caller-supplied CTEs.
13604        new_ctes.extend(out.ctes);
13605        out.ctes = new_ctes;
13606        Ok(Some(out))
13607    }
13608
13609    /// v7.37.6-B(sentori Epic 2 P0)— if `stmt`'s FROM-clause references
13610    /// any partition-parent table, rewrite the SELECT so each parent
13611    /// reference resolves to a CTE whose body is a `UNION ALL` over the
13612    /// children that pass the WHERE-derived partition-key range. Returns
13613    /// `None`(no rewrite needed)when no parent is referenced or all
13614    /// references are shadowed by a same-name CTE.
13615    ///
13616    /// Pruning vocabulary at v7.37.6-B:
13617    ///   * Flat `AND` chain over `<key> {>= | > | < | <= | =} literal`
13618    ///     and `<key> BETWEEN literal AND literal`.
13619    ///   * Anything outside that(OR / nested IN / function call on the
13620    ///     key)defaults to "no pruning" — every child + DEFAULT lands
13621    ///     in the UNION. Correctness is preserved; only the plan size
13622    ///     widens.
13623    fn expand_partition_parents_in_select(
13624        &self,
13625        stmt: &SelectStatement,
13626    ) -> Result<Option<SelectStatement>, EngineError> {
13627        let cat = self.active_catalog();
13628        let Some(from) = &stmt.from else {
13629            return Ok(None);
13630        };
13631        let mut parent_refs: Vec<String> = Vec::new();
13632        collect_partition_parent_refs(&from.primary, cat, &mut parent_refs);
13633        for j in &from.joins {
13634            collect_partition_parent_refs(&j.table, cat, &mut parent_refs);
13635        }
13636        // Drop names shadowed by a CTE on the same SELECT(PG semantics
13637        // — same as view expansion above).
13638        parent_refs.retain(|n| !stmt.ctes.iter().any(|c| c.name.eq_ignore_ascii_case(n)));
13639        if parent_refs.is_empty() {
13640            return Ok(None);
13641        }
13642        // Synthesise a CTE name per parent so the existing
13643        // "CTE shadows a real table" guard doesn't fire (the parent
13644        // IS a real table in the catalog, unlike VIEW expansion's
13645        // case). The FROM-clause TableRef walker below rewrites
13646        // every parent reference to point at the synthetic CTE.
13647        let synth_name = |p: &str| alloc::format!("__spg_partition_{p}");
13648        let mut new_ctes: Vec<spg_sql::ast::Cte> = Vec::with_capacity(parent_refs.len());
13649        let mut expanded_parents: Vec<alloc::string::String> = Vec::new();
13650        for parent_name in &parent_refs {
13651            // No children = no rewrite. The parent itself is a real
13652            // (empty-rows) table — the regular FROM-resolution path
13653            // will scan it and return 0 rows, matching the
13654            // "partition parent with no children" plan. Skipping the
13655            // CTE here also avoids `SELECT * FROM parent` re-entering
13656            // this rewrite on the synthetic body (infinite recursion).
13657            let Some(body) = self.build_partition_parent_union_body(parent_name, stmt)? else {
13658                continue;
13659            };
13660            new_ctes.push(spg_sql::ast::Cte {
13661                name: synth_name(parent_name),
13662                body: spg_sql::ast::CteBody::Select(body),
13663                recursive: false,
13664                column_overrides: Vec::new(),
13665                search: None,
13666                cycle: None,
13667            });
13668            expanded_parents.push(parent_name.clone());
13669        }
13670        if expanded_parents.is_empty() {
13671            return Ok(None);
13672        }
13673        let mut out = stmt.clone();
13674        if let Some(from) = out.from.as_mut() {
13675            rewrite_partition_parent_table_ref(&mut from.primary, &expanded_parents, &synth_name);
13676            for j in &mut from.joins {
13677                rewrite_partition_parent_table_ref(&mut j.table, &expanded_parents, &synth_name);
13678            }
13679        }
13680        new_ctes.extend(out.ctes);
13681        out.ctes = new_ctes;
13682        Ok(Some(out))
13683    }
13684
13685    /// Build the `SELECT * FROM child1 UNION ALL …` body for one parent.
13686    /// Children include every overlap-hit `Range` plus(always)the
13687    /// `Default` child(if any). Returns `Ok(None)` when no children
13688    /// would survive — caller skips the CTE injection and lets the
13689    /// parent fall through to the regular(empty-rows)scan path,
13690    /// avoiding the infinite recursion that an empty-body CTE
13691    /// referencing the parent name would trigger.
13692    /// v7.37.16 (16.10) — public helper invoked from explain.rs to
13693    /// surface "which children survive the WHERE-clause prune" in
13694    /// EXPLAIN output. Returns `None` when `parent_name` isn't
13695    /// actually a partition parent; otherwise returns the list of
13696    /// children the planner would scan (same algorithm as
13697    /// [`Self::build_partition_parent_union_body`] but without the
13698    /// SQL re-parse).
13699    /// v7.39 (round 224) — the kept-children prune keyed off a bare WHERE
13700    /// expression (the PG-shaped EXPLAIN's scan builder has no full
13701    /// SelectStatement in hand). Wraps the original by synthesising a
13702    /// minimal statement carrying just the predicate.
13703    pub(crate) fn explain_partition_kept_children_by_where(
13704        &self,
13705        parent_name: &str,
13706        where_: Option<&spg_sql::ast::Expr>,
13707    ) -> Option<Vec<alloc::string::String>> {
13708        let mut synth = SelectStatement::default();
13709        synth.where_ = where_.cloned();
13710        self.explain_partition_kept_children(parent_name, &synth)
13711    }
13712
13713    pub(crate) fn explain_partition_kept_children(
13714        &self,
13715        parent_name: &str,
13716        outer: &SelectStatement,
13717    ) -> Option<Vec<alloc::string::String>> {
13718        use spg_storage::PartitionRole;
13719        let cat = self.active_catalog();
13720        let parent = cat.get(parent_name)?;
13721        let (key_position, parent_kind) = match &parent.schema().partition_role {
13722            Some(PartitionRole::Parent {
13723                key_column_positions,
13724                kind,
13725                ..
13726            }) => (*key_column_positions.first().unwrap_or(&0), *kind),
13727            _ => return None,
13728        };
13729        let key_col_name = parent.schema().columns[key_position].name.clone();
13730        let (lo_bound, hi_bound) = match outer.where_.as_ref() {
13731            Some(expr) => extract_key_range(expr, &key_col_name),
13732            None => (None, None),
13733        };
13734        let eq_value: Option<spg_storage::Value<'static>> = match outer.where_.as_ref() {
13735            Some(expr) => extract_key_eq_value(expr, &key_col_name),
13736            None => None,
13737        };
13738        let children = crate::partition::children_of_parent(cat, parent_name);
13739        let mut kept: Vec<alloc::string::String> = Vec::new();
13740        let mut default_child: Option<alloc::string::String> = None;
13741        for child_name in &children {
13742            let Some(child) = cat.get(child_name) else {
13743                continue;
13744            };
13745            match &child.schema().partition_role {
13746                Some(PartitionRole::Range { lower, upper, .. }) => {
13747                    if range_satisfies_filter(lower, upper, lo_bound.as_ref(), hi_bound.as_ref()) {
13748                        kept.push(child_name.clone());
13749                    }
13750                }
13751                Some(PartitionRole::List { values, .. }) => match &eq_value {
13752                    Some(v) => {
13753                        if values.iter().any(|b| b.equals_value(v)) {
13754                            kept.push(child_name.clone());
13755                        }
13756                    }
13757                    None => kept.push(child_name.clone()),
13758                },
13759                Some(PartitionRole::Hash {
13760                    modulus, remainder, ..
13761                }) => match &eq_value {
13762                    Some(v) => {
13763                        let h = crate::partition::pg_compatible_hash(v);
13764                        if h.rem_euclid(u64::from(*modulus)) == u64::from(*remainder) {
13765                            kept.push(child_name.clone());
13766                        }
13767                    }
13768                    None => kept.push(child_name.clone()),
13769                },
13770                Some(PartitionRole::Default { .. }) => {
13771                    default_child = Some(child_name.clone());
13772                }
13773                _ => {}
13774            }
13775        }
13776        let _ = parent_kind;
13777        if let Some(d) = default_child {
13778            if kept.is_empty() || eq_value.is_none() {
13779                kept.push(d);
13780            }
13781        }
13782        Some(kept)
13783    }
13784
13785    fn build_partition_parent_union_body(
13786        &self,
13787        parent_name: &str,
13788        outer: &SelectStatement,
13789    ) -> Result<Option<SelectStatement>, EngineError> {
13790        use spg_storage::PartitionRole;
13791        let cat = self.active_catalog();
13792        let parent = cat.get(parent_name).ok_or_else(|| {
13793            EngineError::Storage(spg_storage::StorageError::Corrupt(alloc::format!(
13794                "partition parent {parent_name:?} disappeared mid-expansion"
13795            )))
13796        })?;
13797        let (key_position, parent_kind) = match &parent.schema().partition_role {
13798            Some(PartitionRole::Parent {
13799                key_column_positions,
13800                kind,
13801                ..
13802            }) => (*key_column_positions.first().unwrap_or(&0), *kind),
13803            // v7.39 (round 645) — an INHERITANCE parent, which has no
13804            // role of its own: the relationship is recorded only in the
13805            // children. Three things differ from a partition parent and
13806            // all three are in this body.
13807            //
13808            //   * The parent HOLDS ROWS, so it is a term of the union —
13809            //     `FROM ONLY`, or expanding it would recurse.
13810            //   * There is no partition key, so there is nothing to
13811            //     prune: every child is a term.
13812            //   * A child may declare columns of its own, so the terms
13813            //     name the PARENT's columns rather than `*`. PG's
13814            //     `SELECT * FROM parent` returns the parent's shape.
13815            //
13816            // Answered from this match rather than a branch before it —
13817            // round 644 measured what an extra early return beside an
13818            // existing test costs in this file.
13819            _ if crate::partition::has_inheritance_children(cat, parent_name) => {
13820                let cols = parent
13821                    .schema()
13822                    .columns
13823                    .iter()
13824                    .map(|c| quote_ident_for_sql(&c.name))
13825                    .collect::<Vec<_>>()
13826                    .join(", ");
13827                let carry_sys = references_ctid(outer);
13828                let sys = if carry_sys {
13829                    let mut t = alloc::string::String::new();
13830                    for s in SYSTEM_COLUMNS {
13831                        t.push_str(", ");
13832                        t.push_str(s);
13833                    }
13834                    t
13835                } else {
13836                    alloc::string::String::new()
13837                };
13838                let mut body = alloc::format!(
13839                    "SELECT {cols}{sys} FROM ONLY {}",
13840                    quote_ident_for_sql(parent_name)
13841                );
13842                for child in crate::partition::children_of_parent(cat, parent_name) {
13843                    body.push_str(&alloc::format!(
13844                        " UNION ALL SELECT {cols}{sys} FROM {}",
13845                        quote_ident_for_sql(&child)
13846                    ));
13847                }
13848                return parse_select_or_corrupt(&body).map(Some);
13849            }
13850            _ => {
13851                return Err(EngineError::Unsupported(alloc::format!(
13852                    "partition expansion: {parent_name:?} is not a parent"
13853                )));
13854            }
13855        };
13856        let key_col_name = parent.schema().columns[key_position].name.clone();
13857        // v7.37.16 (16.7) — for RANGE we extract a (lo, hi) interval
13858        // off the WHERE; for LIST / HASH we extract a single `=`
13859        // literal (and the rest of the planner falls back to "keep
13860        // every child" — same conservative path as 16.1/16.2).
13861        let (lo_bound, hi_bound) = match outer.where_.as_ref() {
13862            Some(expr) => extract_key_range(expr, &key_col_name),
13863            None => (None, None),
13864        };
13865        let eq_value: Option<spg_storage::Value<'static>> = match outer.where_.as_ref() {
13866            Some(expr) => extract_key_eq_value(expr, &key_col_name),
13867            None => None,
13868        };
13869        let children = crate::partition::children_of_parent(cat, parent_name);
13870        let mut kept: Vec<String> = Vec::new();
13871        let mut default_child: Option<String> = None;
13872        // First pass — apply per-strategy gates, defer DEFAULT until
13873        // we know whether some non-DEFAULT child matched.
13874        for child_name in &children {
13875            let Some(child) = cat.get(child_name) else {
13876                continue;
13877            };
13878            match &child.schema().partition_role {
13879                Some(PartitionRole::Range { lower, upper, .. }) => {
13880                    if range_satisfies_filter(lower, upper, lo_bound.as_ref(), hi_bound.as_ref()) {
13881                        kept.push(child_name.clone());
13882                    }
13883                }
13884                // v7.37.16 (16.7) — LIST pruning: if WHERE has `key
13885                // = <lit>`, only the child whose values contain that
13886                // literal survives. Otherwise (no equality predicate
13887                // or planner couldn't extract one) keep the child
13888                // conservatively.
13889                Some(PartitionRole::List { values, .. }) => match &eq_value {
13890                    Some(v) => {
13891                        if values.iter().any(|b| b.equals_value(v)) {
13892                            kept.push(child_name.clone());
13893                        }
13894                    }
13895                    None => kept.push(child_name.clone()),
13896                },
13897                // v7.37.16 (16.7) — HASH pruning: with `key = <lit>`
13898                // we know the residue class deterministically, so
13899                // only the matching REMAINDER child survives.
13900                Some(PartitionRole::Hash {
13901                    modulus, remainder, ..
13902                }) => match &eq_value {
13903                    Some(v) => {
13904                        let h = crate::partition::pg_compatible_hash(v);
13905                        if h.rem_euclid(u64::from(*modulus)) == u64::from(*remainder) {
13906                            kept.push(child_name.clone());
13907                        }
13908                    }
13909                    None => kept.push(child_name.clone()),
13910                },
13911                Some(PartitionRole::Default { .. }) => {
13912                    default_child = Some(child_name.clone());
13913                }
13914                _ => {}
13915            }
13916        }
13917        // PG-style DEFAULT semantics: the DEFAULT child must be
13918        // scanned iff some row could fall outside every concrete
13919        // child's bound predicate. We approximate that as "no
13920        // concrete child matched" (== full prune) — strictly
13921        // conservative for LIST / HASH (DEFAULT also catches rows
13922        // outside the union of value-sets / residues), and matches
13923        // PG for the equality case where we *do* know the routing
13924        // outcome.
13925        let _ = parent_kind; // used to silence dead-code lint while 16.8-9 lands.
13926        if let Some(d) = default_child {
13927            if kept.is_empty() {
13928                kept.push(d);
13929            } else if eq_value.is_none() {
13930                // Without an equality literal, the DEFAULT child may
13931                // still hold matching rows (e.g. LIKE on TEXT keys
13932                // for which a LIST partition exists). Keep it.
13933                kept.push(d);
13934            }
13935        }
13936        // Build the UNION ALL body text and re-parse — keeps the
13937        // rewrite expressible in surface SQL so the engine's existing
13938        // parser path handles the AST shape uniformly.
13939        if kept.is_empty() {
13940            // No children survive — caller falls back to scanning the
13941            // (empty) parent table. Returning None here is what
13942            // prevents the synthetic CTE from referring back to the
13943            // parent name and re-entering this rewrite pass.
13944            let _ = parent_name;
13945            return Ok(None);
13946        }
13947        // v7.39 (round 622, S05a) — the system columns of the CHILD the row
13948        // actually lives in.
13949        //
13950        // The parent is read through a synthetic CTE, so a `tableoid` on it
13951        // resolved against that CTE: every row of every child reported
13952        // `__spg_partition_pm`, an internal name no user ever typed, where
13953        // PG reports `pm_a` / `pm_b`. That is not only a leak — it silently
13954        // empties `WHERE tableoid::regclass::TEXT = 'pm_a'`, which is how
13955        // one asks "which partition is this row in", answering 0 rows where
13956        // PG answers 1. `ctid` had the same shape: it numbered the CTE's
13957        // output, so rows in different children got distinct ctids instead
13958        // of each child's own physical position.
13959        //
13960        // Naming them in the term is what carries them: the child scan
13961        // materialises its own six because the statement now references
13962        // them, and they land in SYSTEM_COLUMNS order right after the user
13963        // columns — the exact layout the positional `*` skip already
13964        // expects. Only done when the outer statement asks for one, so a
13965        // plain `SELECT * FROM parent` scans exactly what it scanned.
13966        let carry_sys = references_ctid(outer);
13967        let mut body = alloc::string::String::new();
13968        for (i, child_name) in kept.iter().enumerate() {
13969            if i > 0 {
13970                body.push_str(" UNION ALL ");
13971            }
13972            body.push_str("SELECT *");
13973            if carry_sys {
13974                for sys in SYSTEM_COLUMNS {
13975                    body.push_str(", ");
13976                    body.push_str(sys);
13977                }
13978            }
13979            body.push_str(" FROM ");
13980            body.push_str(&quote_ident_for_sql(child_name));
13981        }
13982        parse_select_or_corrupt(&body).map(Some)
13983    }
13984}
13985
13986/// Rewrite a `TableRef` pointing at a partition parent so it
13987/// references the synthetic CTE created by the expansion. If the
13988/// original ref had no alias, preserve the parent name as an alias
13989/// so column references like `events_partitioned.received_at`
13990/// keep resolving.
13991fn rewrite_partition_parent_table_ref(
13992    t: &mut spg_sql::ast::TableRef,
13993    parents: &[alloc::string::String],
13994    synth_name: &impl Fn(&str) -> alloc::string::String,
13995) {
13996    if t.lateral_subquery.is_some() || t.unnest_expr.is_some() || t.generate_series_args.is_some() {
13997        return;
13998    }
13999    // v7.39 (round 644) — an ONLY reference stays pointed at the parent
14000    // itself. The rewrite is keyed on the NAME, so in
14001    // `FROM ONLY po a JOIN po b` the un-qualified `b` put `po` on the
14002    // parent list and this then rewrote BOTH — including the one that
14003    // asked not to descend. PG answers 0 for that join; SPG answered 2.
14004    // Folded into the existing test — see the note in
14005    // `collect_partition_parent_refs` for what a separate one cost.
14006    if t.only || !parents.iter().any(|p| p == &t.name) {
14007        return;
14008    }
14009    if t.alias.is_none() {
14010        t.alias = Some(t.name.clone());
14011    }
14012    t.name = synth_name(&t.name);
14013}
14014
14015/// Walk a `TableRef` and push its `name` if it resolves to a partition
14016/// parent in `cat`. Skips `lateral_subquery` / `unnest_expr` /
14017/// `generate_series_args` references — those aren't catalog tables.
14018fn collect_partition_parent_refs(
14019    t: &spg_sql::ast::TableRef,
14020    cat: &spg_storage::Catalog,
14021    out: &mut Vec<alloc::string::String>,
14022) {
14023    if t.lateral_subquery.is_some() || t.unnest_expr.is_some() || t.generate_series_args.is_some() {
14024        return;
14025    }
14026    // v7.39 (round 644) — `FROM ONLY <parent>` scans the parent alone.
14027    // The keyword used to be absorbed at parse time, so this fanned out
14028    // anyway and `SELECT count(*) FROM ONLY <partitioned parent>`
14029    // answered 2 where PG answers 0.
14030    //
14031    // Folded into the existing test rather than given an early return of
14032    // its own: as two extra lines in this function's body it cost
14033    // `WHERE g BETWEEN 10 AND 20` **26x**, 5.9 ms to 155 ms, measured
14034    // outside the panel. Rounds 641 and 643 met the same wall from the
14035    // other two directions — adding to a hot function and taking away
14036    // from a cold one. What goes in a body near the row loop is a
14037    // codegen decision whatever its shape.
14038    if !t.only && crate::partition::has_children(cat, &t.name) {
14039        out.push(t.name.clone());
14040    }
14041}
14042
14043/// v7.37.6-B partition-key range derived from a WHERE expression.
14044/// `i64` microseconds since epoch with the same sign convention as
14045/// `Value::Timestamp`. Inclusive bool: `true` ⇒ inclusive(`>=` / `<=`
14046/// / `=`),`false` ⇒ exclusive(`>` / `<`).
14047#[derive(Debug, Clone, Copy)]
14048pub(crate) struct PartitionFilterBound {
14049    pub micros: i64,
14050    pub inclusive: bool,
14051}
14052
14053/// Walk a flat AND chain looking for `<key> <op> <timestamptz-literal>`
14054/// shapes; tighten the running lo / hi as we go. Anything outside that
14055/// (OR / nested calls / non-key columns)is ignored — caller treats
14056/// `None` as "no constraint on that side."
14057fn extract_key_range(
14058    expr: &spg_sql::ast::Expr,
14059    key_col: &str,
14060) -> (Option<PartitionFilterBound>, Option<PartitionFilterBound>) {
14061    let mut lo: Option<PartitionFilterBound> = None;
14062    let mut hi: Option<PartitionFilterBound> = None;
14063    let mut stack: Vec<&spg_sql::ast::Expr> = alloc::vec![expr];
14064    while let Some(e) = stack.pop() {
14065        match e {
14066            spg_sql::ast::Expr::Binary {
14067                lhs,
14068                op: spg_sql::ast::BinOp::And,
14069                rhs,
14070            } => {
14071                stack.push(lhs);
14072                stack.push(rhs);
14073            }
14074            // BETWEEN is desugared at parse time into `lhs >= low AND
14075            // lhs <= high`, so it lands here as two regular Binary
14076            // arms via the AND walker above.
14077            spg_sql::ast::Expr::Binary { lhs, op, rhs } => {
14078                let (col_ref, lit_side, swapped) = if is_column_ref(lhs, key_col) {
14079                    (Some(lhs.as_ref()), rhs.as_ref(), false)
14080                } else if is_column_ref(rhs, key_col) {
14081                    (Some(rhs.as_ref()), lhs.as_ref(), true)
14082                } else {
14083                    (None, lhs.as_ref(), false)
14084                };
14085                if col_ref.is_none() {
14086                    continue;
14087                }
14088                let Some(lit) = literal_to_micros(lit_side) else {
14089                    continue;
14090                };
14091                use spg_sql::ast::BinOp::{Eq, Gt, GtEq, Lt, LtEq};
14092                let effective_op = if swapped {
14093                    match op {
14094                        Lt => Gt,
14095                        LtEq => GtEq,
14096                        Gt => Lt,
14097                        GtEq => LtEq,
14098                        other => *other,
14099                    }
14100                } else {
14101                    *op
14102                };
14103                match effective_op {
14104                    Eq => {
14105                        tighten_lo(
14106                            &mut lo,
14107                            PartitionFilterBound {
14108                                micros: lit,
14109                                inclusive: true,
14110                            },
14111                        );
14112                        tighten_hi(
14113                            &mut hi,
14114                            PartitionFilterBound {
14115                                micros: lit,
14116                                inclusive: true,
14117                            },
14118                        );
14119                    }
14120                    GtEq => {
14121                        tighten_lo(
14122                            &mut lo,
14123                            PartitionFilterBound {
14124                                micros: lit,
14125                                inclusive: true,
14126                            },
14127                        );
14128                    }
14129                    Gt => {
14130                        tighten_lo(
14131                            &mut lo,
14132                            PartitionFilterBound {
14133                                micros: lit,
14134                                inclusive: false,
14135                            },
14136                        );
14137                    }
14138                    LtEq => {
14139                        tighten_hi(
14140                            &mut hi,
14141                            PartitionFilterBound {
14142                                micros: lit,
14143                                inclusive: true,
14144                            },
14145                        );
14146                    }
14147                    Lt => {
14148                        tighten_hi(
14149                            &mut hi,
14150                            PartitionFilterBound {
14151                                micros: lit,
14152                                inclusive: false,
14153                            },
14154                        );
14155                    }
14156                    _ => {}
14157                }
14158            }
14159            _ => {}
14160        }
14161    }
14162    (lo, hi)
14163}
14164
14165fn tighten_lo(slot: &mut Option<PartitionFilterBound>, new: PartitionFilterBound) {
14166    match slot {
14167        None => *slot = Some(new),
14168        Some(cur) => {
14169            if new.micros > cur.micros
14170                || (new.micros == cur.micros && !new.inclusive && cur.inclusive)
14171            {
14172                *slot = Some(new);
14173            }
14174        }
14175    }
14176}
14177
14178fn tighten_hi(slot: &mut Option<PartitionFilterBound>, new: PartitionFilterBound) {
14179    match slot {
14180        None => *slot = Some(new),
14181        Some(cur) => {
14182            if new.micros < cur.micros
14183                || (new.micros == cur.micros && !new.inclusive && cur.inclusive)
14184            {
14185                *slot = Some(new);
14186            }
14187        }
14188    }
14189}
14190
14191fn is_column_ref(e: &spg_sql::ast::Expr, key_col: &str) -> bool {
14192    if let spg_sql::ast::Expr::Column(c) = e {
14193        c.name.eq_ignore_ascii_case(key_col)
14194    } else {
14195        false
14196    }
14197}
14198
14199/// v7.37.16 (16.7) — walk an AND-chain WHERE and pull a single
14200/// `key_col = <literal>` predicate out for LIST/HASH partition
14201/// pruning. Returns `None` when no equality literal can be lifted
14202/// (planner then keeps every child — correctness preserved). The
14203/// returned `Value<'static>` is an owned coercion so the caller can
14204/// outlive any AST node it was extracted from.
14205pub(crate) fn extract_key_eq_value(
14206    expr: &spg_sql::ast::Expr,
14207    key_col: &str,
14208) -> Option<spg_storage::Value<'static>> {
14209    let mut stack: Vec<&spg_sql::ast::Expr> = alloc::vec![expr];
14210    while let Some(e) = stack.pop() {
14211        match e {
14212            spg_sql::ast::Expr::Binary {
14213                lhs,
14214                op: spg_sql::ast::BinOp::And,
14215                rhs,
14216            } => {
14217                stack.push(lhs);
14218                stack.push(rhs);
14219            }
14220            spg_sql::ast::Expr::Binary {
14221                lhs,
14222                op: spg_sql::ast::BinOp::Eq,
14223                rhs,
14224            } => {
14225                let lit_side = if is_column_ref(lhs, key_col) {
14226                    rhs.as_ref()
14227                } else if is_column_ref(rhs, key_col) {
14228                    lhs.as_ref()
14229                } else {
14230                    continue;
14231                };
14232                let cloned = lit_side.clone();
14233                let Ok(v) = crate::conversions::literal_expr_to_value(cloned) else {
14234                    continue;
14235                };
14236                // Coerce to an owned Value<'static> so the caller
14237                // can hold it past the WHERE expression's lifetime.
14238                let owned: spg_storage::Value<'static> = match v {
14239                    spg_storage::Value::Text(s) => {
14240                        spg_storage::Value::Text(alloc::borrow::Cow::Owned(s.into_owned()))
14241                    }
14242                    spg_storage::Value::SmallInt(n) => spg_storage::Value::SmallInt(n),
14243                    spg_storage::Value::Int(n) => spg_storage::Value::Int(n),
14244                    spg_storage::Value::BigInt(n) => spg_storage::Value::BigInt(n),
14245                    spg_storage::Value::Date(d) => spg_storage::Value::Date(d),
14246                    spg_storage::Value::Timestamp(t) => spg_storage::Value::Timestamp(t),
14247                    spg_storage::Value::Bool(b) => spg_storage::Value::Bool(b),
14248                    spg_storage::Value::Null => spg_storage::Value::Null,
14249                    // Anything else (Vector / Json / Bytes / Numeric /
14250                    // arrays / interval / …) isn't a current partition
14251                    // key type; skip without pruning.
14252                    _ => continue,
14253                };
14254                return Some(owned);
14255            }
14256            _ => {}
14257        }
14258    }
14259    None
14260}
14261
14262/// Coerce a literal Expr(after the parser folded sequence calls etc.)
14263/// to i64 microseconds. Mirrors `evaluate_partition_bound`'s shape so
14264/// pruning and routing agree on the literal vocabulary. Returns
14265/// `None` when the literal isn't recognised(planner then skips
14266/// pruning on that branch — correctness preserved).
14267fn literal_to_micros(e: &spg_sql::ast::Expr) -> Option<i64> {
14268    let cloned = e.clone();
14269    let value = crate::conversions::literal_expr_to_value(cloned).ok()?;
14270    match value {
14271        spg_storage::Value::Timestamp(m) => Some(m),
14272        spg_storage::Value::Date(days) => Some(i64::from(days) * 86_400i64 * 1_000_000i64),
14273        spg_storage::Value::Text(s) => crate::eval::parse_timestamp_literal(&s),
14274        _ => None,
14275    }
14276}
14277
14278/// `[range_lo, range_hi)` of a child is kept iff it can hold any row
14279/// satisfying the WHERE-derived filter range. PG-style half-open:
14280/// child upper exclusive. Filter inclusivity is honoured per-bound.
14281fn range_satisfies_filter(
14282    range_lo: &spg_storage::PartitionBound,
14283    range_hi: &spg_storage::PartitionBound,
14284    filter_lo: Option<&PartitionFilterBound>,
14285    filter_hi: Option<&PartitionFilterBound>,
14286) -> bool {
14287    use spg_storage::PartitionBound;
14288    // For each filter side, reject children that can't host any row
14289    // matching the predicate.
14290    if let Some(lo) = filter_lo {
14291        // child upper bound vs filter lower:
14292        //   if filter is x >= L, child rejects iff child.hi <= L
14293        //   if filter is x  > L, child rejects iff child.hi <= L
14294        //   (child.hi exclusive, so equality with L still rejects)
14295        match range_hi {
14296            PartitionBound::MinValue => return false,
14297            PartitionBound::MaxValue => {}
14298            PartitionBound::TimestampTz(hi) => {
14299                if *hi <= lo.micros {
14300                    return false;
14301                }
14302            }
14303            // v7.37.16 (16.6) — non-TIMESTAMPTZ bounds aren't
14304            // matched against TIMESTAMPTZ filters here; keep child
14305            // (conservative: don't prune).
14306            PartitionBound::BigInt(_)
14307            | PartitionBound::Int(_)
14308            | PartitionBound::SmallInt(_)
14309            | PartitionBound::Date(_)
14310            | PartitionBound::Text(_) => {}
14311        }
14312    }
14313    if let Some(hi) = filter_hi {
14314        // child lower bound vs filter upper:
14315        //   if filter is x <= U, child rejects iff child.lo > U
14316        //   if filter is x  < U, child rejects iff child.lo >= U
14317        match range_lo {
14318            PartitionBound::MaxValue => return false,
14319            PartitionBound::MinValue => {}
14320            PartitionBound::TimestampTz(lo) => {
14321                let rejects = if hi.inclusive {
14322                    *lo > hi.micros
14323                } else {
14324                    *lo >= hi.micros
14325                };
14326                if rejects {
14327                    return false;
14328                }
14329            }
14330            PartitionBound::BigInt(_)
14331            | PartitionBound::Int(_)
14332            | PartitionBound::SmallInt(_)
14333            | PartitionBound::Date(_)
14334            | PartitionBound::Text(_) => {}
14335        }
14336    }
14337    true
14338}
14339
14340fn quote_ident_for_sql(name: &str) -> alloc::string::String {
14341    // Match spg-sql's quoting rule(unquoted when ASCII-lowercase
14342    // identifier, otherwise quoted). Conservative: always quote so
14343    // children with reserved names round-trip safely through the
14344    // CTE-body parse.
14345    let mut out = alloc::string::String::with_capacity(name.len() + 2);
14346    out.push('"');
14347    for c in name.chars() {
14348        if c == '"' {
14349            out.push('"');
14350        }
14351        out.push(c);
14352    }
14353    out.push('"');
14354    out
14355}
14356
14357fn parse_select_or_corrupt(sql: &str) -> Result<SelectStatement, EngineError> {
14358    let parsed = spg_sql::parser::parse_statement(sql).map_err(|e| {
14359        EngineError::Unsupported(alloc::format!(
14360            "partition expansion: generated SQL {sql:?} failed to re-parse: {e}"
14361        ))
14362    })?;
14363    let Statement::Select(body) = parsed else {
14364        return Err(EngineError::Unsupported(alloc::format!(
14365            "partition expansion: generated SQL {sql:?} is not a SELECT"
14366        )));
14367    };
14368    Ok(body)
14369}
14370
14371/// v7.39 (read01 round 65/66) — the column shape a set-returning function
14372/// exposes. `RETURNS TABLE(id int, v text)` names them; a `SETOF <scalar>`
14373/// yields ONE column named after the call's alias when there is one (`FROM
14374/// odds() AS x` → `x`), else after the function. Get this wrong and the alias
14375/// resolves to the whole ROW: `SELECT x::text FROM odds() AS x` renders `(1)`.
14376fn setof_column_shape_from(
14377    declared: &str,
14378    name: &str,
14379    alias: Option<&str>,
14380    got: &[ColumnSchema],
14381) -> alloc::vec::Vec<ColumnSchema> {
14382    let upper = declared.to_ascii_uppercase();
14383    if upper.starts_with("TABLE(") {
14384        let raw = &declared["TABLE(".len()..declared.len() - 1];
14385        return raw
14386            .split(',')
14387            .zip(got.iter())
14388            .map(|(decl, g)| {
14389                let cname = decl.split_whitespace().next().unwrap_or(g.name.as_str());
14390                ColumnSchema::new(cname.to_string(), g.ty, true)
14391            })
14392            .collect();
14393    }
14394    let cname = alias.unwrap_or(name);
14395    got.first()
14396        .map(|c| alloc::vec![ColumnSchema::new(cname.to_string(), c.ty, true)])
14397        .unwrap_or_default()
14398}
14399
14400/// The plpgsql twin: the interpreter hands back raw value rows, so the types
14401/// come off the first row.
14402fn setof_column_shape(
14403    declared: &str,
14404    name: &str,
14405    alias: Option<&str>,
14406    first_row: Option<&alloc::vec::Vec<Value<'static>>>,
14407) -> alloc::vec::Vec<ColumnSchema> {
14408    let got: alloc::vec::Vec<ColumnSchema> = first_row
14409        .map(|r| {
14410            r.iter()
14411                .enumerate()
14412                .map(|(i, v)| {
14413                    ColumnSchema::new(
14414                        alloc::format!("col{i}"),
14415                        v.data_type().unwrap_or(DataType::Text),
14416                        true,
14417                    )
14418                })
14419                .collect()
14420        })
14421        .unwrap_or_default();
14422    setof_column_shape_from(declared, name, alias, &got)
14423}
14424
14425/// v7.39 (read01 round 67) — expand every set-returning call in a target list
14426/// for ONE input row, PG's ProjectSet semantics.
14427///
14428/// Several SRFs in one list run in **LOCKSTEP**, not as a cross product: the
14429/// output has as many rows as the LONGEST of them, and a shorter one is padded
14430/// with NULLs. (`SELECT generate_series(1,3), generate_series(10,11)` →
14431/// `1/10, 2/11, 3/NULL`.) A single SRF is the degenerate case of that, and an
14432/// SRF that yields no rows at all contributes none — `SELECT unnest('{}'::int[])`
14433/// is zero rows, not one NULL row.
14434///
14435/// Non-SRF items repeat, evaluated once per output row from the same input row.
14436/// v7.39 (read01 round 79) — where an aggregate may NOT appear. Both of these
14437/// used to reach the scalar function dispatcher, which reported the aggregate as
14438/// an *unknown function* — the same "symptom two layers above the cause" shape
14439/// round 78 found with SRFs. Neither can be diagnosed down there: the dispatcher
14440/// sees a call, not the clause it came from. The statement knows.
14441/// v7.39 (round 294, E3 Phase 1b) — PG's rules on WHERE a row-locking
14442/// clause may appear.
14443///
14444/// PG rejects `FOR UPDATE` on exactly the shapes that have no
14445/// identifiable base row to lock, each with its own wording. SPG
14446/// accepted all of them and locked nothing, so a query that PG refuses
14447/// outright came back looking like it had taken locks.
14448///
14449/// Every wording read off live PG 18.4.
14450impl crate::Engine {
14451    /// v7.39.2 — a column name in WHERE / ORDER BY / GROUP BY / HAVING
14452    /// that names nothing is refused before the scan, not when a row
14453    /// reaches it.
14454    ///
14455    /// The projection resolves its names eagerly; a predicate only meets
14456    /// them per row. So on an EMPTY table `SELECT a FROM t WHERE nosuch
14457    /// = 1` answered zero rows and no error, and the same statement over
14458    /// a table with one row raised. Measured on PostgreSQL 18.6 and
14459    /// MySQL 9.7.2: both refuse it whatever the row count. A typo in a
14460    /// predicate therefore passed a test written against an empty
14461    /// fixture and failed in production — or, worse, ran nightly over an
14462    /// empty window and reported nothing.
14463    ///
14464    /// Deliberately narrow: ONE plain base table, nothing else. A join,
14465    /// a CTE, a set operation, a lateral or function source, or a
14466    /// subquery in the clause all bring a second scope into which a name
14467    /// may legitimately resolve, and refusing one of those would be a
14468    /// worse defect than the one this closes. Those shapes keep the
14469    /// old behaviour; the walk below does not descend into a subquery
14470    /// for the same reason.
14471    /// v7.39.2 — refuse a call whose argument count no overload accepts,
14472    /// BEFORE the scan rather than per row.
14473    ///
14474    /// `SELECT lower(t, n) FROM t` answered zero rows and no error over
14475    /// an EMPTY table and raised the moment the table had one row in it,
14476    /// because the arity check lives inside the row-time dispatch. It is
14477    /// the same shape as the unknown-column-in-a-predicate defect closed
14478    /// earlier in this release, and it hides in the same place: a query
14479    /// written against an empty fixture passes its test.
14480    ///
14481    /// The accepted counts come from `eval::arity::REFUSED_ARITIES`,
14482    /// which is derived by asking the dispatch itself offline and can
14483    /// only ever UNDER-refuse — see that file for why the two other
14484    /// candidate oracles were refuted.
14485    /// v7.39.3 — MySQL's column names are case-insensitive; PostgreSQL's
14486    /// quoted ones are not. See `EvalContext::col_eq`.
14487    fn col_name_eq(&self, a: &str, b: &str) -> bool {
14488        if self.speaks_mysql {
14489            a.eq_ignore_ascii_case(b)
14490        } else {
14491            a == b
14492        }
14493    }
14494
14495    pub(crate) fn validate_function_arity(
14496        &self,
14497        stmt: &SelectStatement,
14498    ) -> Result<(), EngineError> {
14499        let mut calls: Vec<(alloc::string::String, Vec<Expr>)> = Vec::new();
14500        for it in &stmt.items {
14501            if let spg_sql::ast::SelectItem::Expr { expr, .. } = it {
14502                collect_function_calls(expr, &mut calls);
14503            }
14504        }
14505        if let Some(w) = &stmt.where_ {
14506            collect_function_calls(w, &mut calls);
14507        }
14508        for o in &stmt.order_by {
14509            collect_function_calls(&o.expr, &mut calls);
14510        }
14511        // The columns a name in this statement could resolve to. Only
14512        // plain base tables; anything else and the types are not
14513        // statically knowable, so nothing is refused early.
14514        let cat = self.active_catalog();
14515        let mut cols: Vec<ColumnSchema> = Vec::new();
14516        if let Some(from) = &stmt.from {
14517            for t in core::iter::once(&from.primary).chain(from.joins.iter().map(|j| &j.table)) {
14518                if let Some(table) = cat.get(&t.name) {
14519                    cols.extend(table.schema().columns.iter().cloned());
14520                }
14521            }
14522        }
14523        for (name, args) in calls {
14524            let Ok(i) = crate::eval::arity::REFUSED_ARITIES
14525                .binary_search_by(|(n, _)| (*n).cmp(name.as_str()))
14526            else {
14527                continue;
14528            };
14529            if !crate::eval::arity::REFUSED_ARITIES[i]
14530                .1
14531                .contains(&args.len())
14532            {
14533                continue;
14534            }
14535            // v7.39.2 — PostgreSQL names the SIGNATURE it could not
14536            // match, and before the scan there are no values to read a
14537            // type from. Where every argument's type is knowable
14538            // statically — a column of a source table, or a literal —
14539            // the sentence is PostgreSQL's exactly; where one is not,
14540            // this leaves the call to the row-time raise, which has the
14541            // values. Refusing early with a WORSE message would trade
14542            // one defect for another.
14543            let mut types: Vec<alloc::string::String> = Vec::new();
14544            for a in &args {
14545                let Some(t) = static_arg_type(a, &cols) else {
14546                    types.clear();
14547                    break;
14548                };
14549                types.push(t);
14550            }
14551            if types.len() != args.len() {
14552                continue;
14553            }
14554            return Err(EngineError::Eval(EvalError::WrongArity {
14555                name,
14556                types: types.join(", "),
14557            }));
14558        }
14559        Ok(())
14560    }
14561
14562    pub(crate) fn validate_clause_columns(
14563        &self,
14564        stmt: &SelectStatement,
14565    ) -> Result<(), EngineError> {
14566        let Some(from) = &stmt.from else {
14567            return Ok(());
14568        };
14569        if !stmt.ctes.is_empty() {
14570            return Ok(());
14571        }
14572        // v7.39.2 — every source, not just the first. A join is checkable
14573        // for the same reason one table is: with no CTE and no
14574        // subquery-shaped source, a bare name has to come from one of
14575        // them. Refusing the check for joins left `SELECT … FROM a JOIN b
14576        // … WHERE nosuch = 1` labelled `'field list'` where MySQL 9.7.2
14577        // says `'where clause'`.
14578        let plain = |t: &spg_sql::ast::TableRef| -> bool {
14579            t.unnest_expr.is_none()
14580                && t.generate_series_args.is_none()
14581                && t.lateral_subquery.is_none()
14582                && t.jsonb_each_text_arg.is_none()
14583                && t.table_fn_call.is_none()
14584                && t.rows_from.is_none()
14585                && t.json_table.is_none()
14586                && !t.scalar_fn_item
14587        };
14588        let cat = self.active_catalog();
14589        let mut sources: Vec<(String, &spg_storage::Table)> = Vec::new();
14590        for t in core::iter::once(&from.primary).chain(from.joins.iter().map(|j| &j.table)) {
14591            if !plain(t) {
14592                return Ok(());
14593            }
14594            let Some(table) = cat.get(&t.name) else {
14595                return Ok(());
14596            };
14597            sources.push((t.alias.clone().unwrap_or_else(|| t.name.clone()), table));
14598        }
14599        let known = |c: &spg_sql::ast::ColumnName| -> bool {
14600            // A system column is not in a table's list and is a perfectly
14601            // good predicate: `WHERE ctid = '(0,4)'::tid` and `WHERE
14602            // tableoid::regclass::text = 'pm_a'` are both real, and the
14603            // first draft of this check refused them. The e2e suite said
14604            // so immediately, which is what it is for.
14605            if is_system_column(&c.name) {
14606                return true;
14607            }
14608            if let Some(q) = &c.qualifier {
14609                // A qualifier must name one of this statement's sources,
14610                // and that source must carry the column. An alias
14611                // REPLACES the written name, which is PostgreSQL's rule
14612                // and MySQL's: `FROM pg_cast c WHERE pg_cast.oid <> 0`
14613                // is an error on both.
14614                return match sources.iter().find(|(a, _)| a == q) {
14615                    Some((_, t)) => t
14616                        .schema()
14617                        .columns
14618                        .iter()
14619                        .any(|sc| self.col_name_eq(&sc.name, &c.name)),
14620                    None => false,
14621                };
14622            }
14623            sources
14624                .iter()
14625                .any(|(_, t)| {
14626                    t.schema()
14627                        .columns
14628                        .iter()
14629                        .any(|sc| self.col_name_eq(&sc.name, &c.name))
14630                })
14631                // An output name the statement itself defines: ORDER BY,
14632                // GROUP BY and HAVING may all name one.
14633                || stmt.items.iter().any(|it| match it {
14634                    SelectItem::Expr { expr, alias } => {
14635                        alias.as_deref() == Some(c.name.as_str())
14636                            || matches!(expr, Expr::Column(pc) if pc.name == c.name)
14637                    }
14638                    _ => false,
14639                })
14640        };
14641        // v7.39.2 — the CLAUSE travels with the reference, because MySQL
14642        // names it: `Unknown column 'x' in 'where clause'`, `'order
14643        // clause'`, `'group statement'`, `'having clause'`. Measured on
14644        // 9.7.2, and a driver's error handling reads the sentence as well
14645        // as the number. PostgreSQL says only `column "x" does not
14646        // exist`, with no clause, so its wording is unchanged.
14647        //
14648        // This walk is the only place the clause is still known: by the
14649        // time a row-time resolver meets the name, the expression has
14650        // been detached from the statement that held it.
14651        let mut refs: Vec<(spg_sql::ast::ColumnName, &'static str)> = Vec::new();
14652        let mut push = |e: &Expr, ctx: &'static str, out: &mut Vec<_>| {
14653            let mut here: Vec<spg_sql::ast::ColumnName> = Vec::new();
14654            collect_plain_column_refs(e, &mut here);
14655            out.extend(here.into_iter().map(|c| (c, ctx)));
14656        };
14657        if let Some(w) = &stmt.where_ {
14658            push(w, "where clause", &mut refs);
14659        }
14660        if let Some(g) = &stmt.group_by {
14661            for e in g {
14662                push(e, "group statement", &mut refs);
14663            }
14664        }
14665        if let Some(h) = &stmt.having {
14666            push(h, "having clause", &mut refs);
14667        }
14668        for o in &stmt.order_by {
14669            push(&o.expr, "order clause", &mut refs);
14670        }
14671        // v7.39.2 — and the join predicates, which MySQL calls the `on
14672        // clause`. Measured on 9.7.2: `Unknown column 'j1.nosuch' in 'on
14673        // clause'`, qualifier and all.
14674        for j in &from.joins {
14675            if let Some(on) = &j.on {
14676                push(on, "on clause", &mut refs);
14677            }
14678        }
14679        for (c, ctx) in &refs {
14680            if !known(c) {
14681                if self.speaks_mysql {
14682                    // The QUALIFIER travels with it: MySQL 9.7.2 answers
14683                    // `Unknown column 'j1.nosuch' in 'on clause'`, not the
14684                    // bare name. Measured.
14685                    let shown = match &c.qualifier {
14686                        Some(q) => alloc::format!("{q}.{}", c.name),
14687                        None => c.name.clone(),
14688                    };
14689                    return Err(EngineError::Eval(EvalError::TypeMismatch {
14690                        detail: alloc::format!("Unknown column '{shown}' in '{ctx}'"),
14691                    }));
14692                }
14693                // PostgreSQL 18.6 names the missing TABLE when the
14694                // qualifier is the part that resolves to nothing
14695                // (`missing FROM-clause entry for table "pg_cast"`) and
14696                // the COLUMN otherwise. Raising the column error for both
14697                // dropped the table name a caller matches on.
14698                if let Some(q) = &c.qualifier
14699                    && !sources.iter().any(|(a, _)| a == q)
14700                {
14701                    return Err(EngineError::Eval(EvalError::UnknownQualifier {
14702                        qualifier: q.clone(),
14703                        column: c.name.clone(),
14704                    }));
14705                }
14706                // v7.39.2 — and a qualified reference whose qualifier
14707                // DOES resolve prints the whole thing, unquoted:
14708                // `column ea.no_such does not exist` (measured on PG
14709                // 18.6). The bare `column "no_such" does not exist` drops
14710                // the alias a caller matches on, which is what the
14711                // sqlx round-20 pin says.
14712                if let Some(q) = &c.qualifier {
14713                    return Err(EngineError::Eval(EvalError::QualifiedColumnNotFound {
14714                        qualifier: q.clone(),
14715                        column: c.name.clone(),
14716                    }));
14717                }
14718                return Err(EngineError::Eval(EvalError::ColumnNotFound {
14719                    name: c.name.clone(),
14720                }));
14721            }
14722        }
14723        Ok(())
14724    }
14725}
14726
14727/// v7.39.2 — the column references of an expression, NOT descending into
14728/// a subquery.
14729///
14730/// A correlated subquery resolves its names against an outer scope this
14731/// walk cannot see, so descending would refuse valid queries. Missing a
14732/// typo inside one is the safe direction; refusing a good query is not.
14733/// v7.39.2 — the type PostgreSQL would name for an argument, when it
14734/// can be known without a row: a column of a source table, or a
14735/// literal. `None` for anything else, which is what keeps the pre-scan
14736/// refusal from printing a worse sentence than the row-time one.
14737pub(crate) fn static_arg_type(e: &Expr, cols: &[ColumnSchema]) -> Option<alloc::string::String> {
14738    use spg_sql::ast::Literal as L;
14739    match e {
14740        Expr::Column(c) => cols
14741            .iter()
14742            .find(|s| s.name.eq_ignore_ascii_case(&c.name))
14743            .map(|s| crate::conversions::pg_type_name_for_error(s.ty)),
14744        // A bare literal has no type yet on PostgreSQL — it names it
14745        // `unknown` in this very sentence — except where the lexeme
14746        // fixes one.
14747        Expr::Literal(L::String(_)) | Expr::Literal(L::Null) => {
14748            Some(alloc::string::String::from("unknown"))
14749        }
14750        Expr::Literal(L::Integer(_)) => Some(alloc::string::String::from("integer")),
14751        Expr::Literal(L::Bool(_)) => Some(alloc::string::String::from("boolean")),
14752        _ => None,
14753    }
14754}
14755
14756/// v7.39.2 — the function calls of an expression, name and argument
14757/// count, NOT descending into a subquery (its scope is its own).
14758fn collect_function_calls(e: &Expr, out: &mut Vec<(alloc::string::String, Vec<Expr>)>) {
14759    match e {
14760        Expr::FunctionCall { name, args } => {
14761            out.push((name.to_ascii_lowercase(), args.clone()));
14762            for a in args {
14763                collect_function_calls(a, out);
14764            }
14765        }
14766        Expr::Binary { lhs, rhs, .. } => {
14767            collect_function_calls(lhs, out);
14768            collect_function_calls(rhs, out);
14769        }
14770        Expr::Unary { expr, .. } | Expr::Collate { expr, .. } | Expr::Cast { expr, .. } => {
14771            collect_function_calls(expr, out);
14772        }
14773        _ => {}
14774    }
14775}
14776
14777fn collect_plain_column_refs(e: &Expr, out: &mut Vec<spg_sql::ast::ColumnName>) {
14778    match e {
14779        Expr::Column(c) => out.push(c.clone()),
14780        Expr::Binary { lhs, rhs, .. } => {
14781            collect_plain_column_refs(lhs, out);
14782            collect_plain_column_refs(rhs, out);
14783        }
14784        Expr::Unary { expr, .. } | Expr::Collate { expr, .. } | Expr::Cast { expr, .. } => {
14785            collect_plain_column_refs(expr, out);
14786        }
14787        Expr::FunctionCall { args, .. } => {
14788            for a in args {
14789                collect_plain_column_refs(a, out);
14790            }
14791        }
14792        _ => {}
14793    }
14794}
14795
14796fn validate_locking_clause(stmt: &SelectStatement) -> Result<(), EngineError> {
14797    let Some(lock) = &stmt.locking else {
14798        return Ok(());
14799    };
14800    let verb = lock_clause_verb(lock.strength);
14801    let refuse = |what: &str| {
14802        Err(EngineError::Unsupported(alloc::format!(
14803            "{verb} is not allowed with {what}"
14804        )))
14805    };
14806    if !stmt.unions.is_empty() {
14807        return refuse("UNION/INTERSECT/EXCEPT");
14808    }
14809    if stmt.distinct || !stmt.distinct_on.is_empty() {
14810        return refuse("DISTINCT clause");
14811    }
14812    if stmt.group_by.is_some() || stmt.group_by_all {
14813        return refuse("GROUP BY clause");
14814    }
14815    let has_agg = stmt.items.iter().any(|it| match it {
14816        spg_sql::ast::SelectItem::Expr { expr, .. } => crate::aggregate::contains_aggregate(expr),
14817        _ => false,
14818    });
14819    if has_agg {
14820        return refuse("aggregate functions");
14821    }
14822    // `FOR UPDATE OF t` must name a relation that is actually in FROM.
14823    for want in &lock.of_tables {
14824        if !locking_from_names(stmt)
14825            .iter()
14826            .any(|n| n.eq_ignore_ascii_case(want))
14827        {
14828            return Err(EngineError::Unsupported(alloc::format!(
14829                "relation \"{want}\" in {verb} clause not found in FROM clause"
14830            )));
14831        }
14832    }
14833    Ok(())
14834}
14835
14836/// How PG names the clause in its diagnostics.
14837const fn lock_clause_verb(s: spg_sql::ast::LockStrength) -> &'static str {
14838    use spg_sql::ast::LockStrength as LS;
14839    match s {
14840        LS::Update => "FOR UPDATE",
14841        LS::NoKeyUpdate => "FOR NO KEY UPDATE",
14842        LS::Share => "FOR SHARE",
14843        LS::KeyShare => "FOR KEY SHARE",
14844    }
14845}
14846
14847/// Every relation name (or alias) the FROM clause exposes.
14848fn locking_from_names(stmt: &SelectStatement) -> alloc::vec::Vec<String> {
14849    let mut out = alloc::vec::Vec::new();
14850    if let Some(f) = &stmt.from {
14851        let mut push = |t: &spg_sql::ast::TableRef| {
14852            if let Some(a) = &t.alias {
14853                out.push(a.clone());
14854            }
14855            out.push(t.name.clone());
14856        };
14857        push(&f.primary);
14858        for j in &f.joins {
14859            push(&j.table);
14860        }
14861    }
14862    out
14863}
14864
14865fn validate_aggregate_placement(stmt: &SelectStatement) -> Result<(), EngineError> {
14866    use spg_sql::ast::Expr;
14867    if let Some(w) = &stmt.where_
14868        && aggregate::contains_aggregate(w)
14869    {
14870        return Err(EngineError::Unsupported(
14871            "aggregate functions are not allowed in WHERE".into(),
14872        ));
14873    }
14874    let mut nested = false;
14875    let mut check = |e: &Expr| {
14876        let mut probe = e.clone();
14877        crate::expr_analysis::rewrite_nodes_mut(&mut probe, &mut |n| {
14878            let args = match n {
14879                Expr::FunctionCall { name, args } if aggregate::is_aggregate_name(name) => args,
14880                _ => return false,
14881            };
14882            if args.iter().any(aggregate::contains_aggregate) {
14883                nested = true;
14884            }
14885            false
14886        });
14887    };
14888    for it in &stmt.items {
14889        if let spg_sql::ast::SelectItem::Expr { expr, .. } = it {
14890            check(expr);
14891        }
14892    }
14893    if let Some(h) = &stmt.having {
14894        check(h);
14895    }
14896    for o in &stmt.order_by {
14897        check(&o.expr);
14898    }
14899    if nested {
14900        return Err(EngineError::Unsupported(
14901            "aggregate function calls cannot be nested".into(),
14902        ));
14903    }
14904    Ok(())
14905}
14906
14907/// v7.39 (read01 round 78) — an SRF may sit ANYWHERE inside a target-list
14908/// expression, not only as the whole item: `upper(unnest(a))`, `unnest(a) + 10`,
14909/// `'x:' || unnest(a)`, `(regexp_matches(s, p, 'g'))::text`. PG evaluates the SRF
14910/// to a set and then applies the enclosing expression once per element. SPG only
14911/// ever recognised an SRF that WAS the item, so everything above died on
14912/// "unknown function unnest" — the set-returning call, wrapped in anything at
14913/// all, fell through to the scalar function dispatcher which has no such name.
14914///
14915/// Each SRF node is lifted out into a synthetic column (`__srf_k`), the tree is
14916/// rewritten to read that column, and the rewritten expression is evaluated once
14917/// per output row against the input row extended with the lifted values. The
14918/// lift is by VALUE, not by literal: a text[] or a jsonb keeps its type exactly.
14919/// v7.39 (read01 round 80) — `ORDER BY <n>` names the Nth OUTPUT column. Three
14920/// executors (the single-table scan, the synthetic-table pipeline, and the
14921/// unnest FROM path) each evaluated the key as an ordinary expression, where the
14922/// literal `n` is just the constant n — the same sort key for every row. The
14923/// sort therefore ran and changed nothing, which is why nobody noticed: rows came
14924/// back in input order, not in a wrong order. Statement prep resolves the common
14925/// case, but only when the SELECT item is an expression — a `*` is not one, and
14926/// `SELECT unnest(a) x` becomes `SELECT * FROM unnest(a) x`, so the everyday
14927/// spelling landed on exactly the shape prep could not resolve.
14928///
14929/// A set-returning item is left alone: copying it into ORDER BY would make the
14930/// key "the whole set", evaluated once per INPUT row.
14931fn resolve_positional_order_by(
14932    order_by: &[spg_sql::ast::OrderBy],
14933    projection: &[ProjectedItem],
14934) -> alloc::vec::Vec<spg_sql::ast::OrderBy> {
14935    order_by
14936        .iter()
14937        .filter_map(|o| {
14938            let mut o = o.clone();
14939            if let Expr::Literal(spg_sql::ast::Literal::Integer(n)) = &o.expr
14940                && *n >= 1
14941                && let Ok(idx) = usize::try_from(*n - 1)
14942                && let Some(item) = projection.get(idx)
14943                && !expr_contains_builtin_srf(&item.expr)
14944            {
14945                // 7.38.1 S6.1 (gendiff fourth leg) — an ordinal whose
14946                // item is itself an integer LITERAL must not be
14947                // substituted textually: the literal would read as an
14948                // ordinal again downstream, and `SELECT 10 … ORDER BY
14949                // 1` died with "position 10 is not in select list"
14950                // where PG happily returns the rows. Ordering by a
14951                // constant orders nothing, so the key drops.
14952                if matches!(item.expr, Expr::Literal(spg_sql::ast::Literal::Integer(_))) {
14953                    return None;
14954                }
14955                o.expr = item.expr.clone();
14956            }
14957            Some(o)
14958        })
14959        .collect()
14960}
14961
14962/// v7.39 (read01 round 80) — does a BUILTIN set-returning call appear anywhere in
14963/// this expression? Statement preparation (`resolve_order_by_position`) runs
14964/// before any catalog is in hand, and it only needs to know "is this item's value
14965/// a set", which the builtin SRFs answer syntactically.
14966pub(crate) fn expr_contains_builtin_srf(e: &spg_sql::ast::Expr) -> bool {
14967    let mut found = false;
14968    let mut probe = e.clone();
14969    crate::expr_analysis::rewrite_nodes_mut(&mut probe, &mut |n| {
14970        if is_top_level_unnest(n) {
14971            found = true;
14972            return true;
14973        }
14974        false
14975    });
14976    found
14977}
14978
14979/// v7.39 (round 599) — everything about a target-list SRF that does not
14980/// depend on the row.
14981///
14982/// `expand_srf_row` derived all of this again for EVERY input row: it cloned
14983/// each SRF-bearing projection expression, walked and rewrote the tree,
14984/// formatted a `__srf_N` name per node, and copied the whole column schema.
14985/// A counting allocator put the path at 24 allocations per input row for a
14986/// single-element `unnest`, against 0 for the same scan without one — 211 MB
14987/// where the plain scan took 4.3 — and the shape held whatever the array
14988/// contained, which is what invariant work looks like.
14989struct SrfPlan {
14990    /// The lifted SRF calls, in slot order.
14991    nodes: alloc::vec::Vec<spg_sql::ast::Expr>,
14992    /// Per projection position, the expression with its SRF calls replaced
14993    /// by `__srf_N` column references. `None` means the item has none.
14994    rewritten: alloc::vec::Vec<Option<spg_sql::ast::Expr>>,
14995    /// The input schema followed by one column per slot. Only the slots'
14996    /// TYPES vary per row, and they are patched in place.
14997    ext_cols: alloc::vec::Vec<ColumnSchema>,
14998    /// v7.39 (round 743) — the rewritten projection COMPILED against the
14999    /// extended schema, once per plan. The per-output-row evaluation ran
15000    /// the interpreter (~560 ns/row on the unnest panel cell); the Step
15001    /// VM reads the `__srf_N` slots as plain columns. `None` = that item
15002    /// is not fully compilable and keeps the interpreter.
15003    compiled: alloc::vec::Vec<Option<eval::CompiledExpr>>,
15004    base_cols: usize,
15005}
15006
15007fn build_srf_plan(
15008    engine: &Engine,
15009    projection: &[ProjectedItem],
15010    srf_idxs: &[usize],
15011    ctx: &EvalContext<'_>,
15012) -> Result<SrfPlan, EngineError> {
15013    // Lift every SRF node out of every item that contains one.
15014    let mut nodes: Vec<spg_sql::ast::Expr> = Vec::new();
15015    let mut rewritten: Vec<Option<spg_sql::ast::Expr>> = alloc::vec![None; projection.len()];
15016    let mut reject: Option<EngineError> = None;
15017    for &i in srf_idxs {
15018        let mut e = projection[i].expr.clone();
15019        crate::expr_analysis::rewrite_nodes_mut(&mut e, &mut |n| {
15020            if reject.is_some() {
15021                return true;
15022            }
15023            // PG refuses a set-returning function inside a conditional: the set
15024            // would have to be produced before anyone knows whether the branch
15025            // is even taken.
15026            let conditional = match n {
15027                spg_sql::ast::Expr::Case { .. } => Some("CASE"),
15028                spg_sql::ast::Expr::FunctionCall { name, .. }
15029                    if name.eq_ignore_ascii_case("coalesce") =>
15030                {
15031                    Some("COALESCE")
15032                }
15033                _ => None,
15034            };
15035            if let Some(kind) = conditional
15036                && engine.expr_contains_srf(n)
15037            {
15038                reject = Some(EngineError::Unsupported(alloc::format!(
15039                    "set-returning functions are not allowed in {kind}"
15040                )));
15041                return true;
15042            }
15043            if !engine.is_srf_node(n) {
15044                return false;
15045            }
15046            let slot = nodes.len();
15047            nodes.push(n.clone());
15048            *n = spg_sql::ast::Expr::Column(spg_sql::ast::ColumnName {
15049                qualifier: None,
15050                name: alloc::format!("__srf_{slot}"),
15051            });
15052            true
15053        });
15054        rewritten[i] = Some(e);
15055    }
15056    if let Some(err) = reject {
15057        return Err(err);
15058    }
15059    let base_cols = ctx.columns.len();
15060    let mut ext_cols: Vec<ColumnSchema> = ctx.columns.to_vec();
15061    for slot in 0..nodes.len() {
15062        ext_cols.push(ColumnSchema::new(
15063            alloc::format!("__srf_{slot}"),
15064            DataType::Text,
15065            true,
15066        ));
15067    }
15068    // v7.39 (round 743) — compile the rewritten items against the
15069    // EXTENDED schema. The slot columns' declared type is a per-row
15070    // patched detail the compiled column read does not consult.
15071    let compiled: Vec<Option<eval::CompiledExpr>> = {
15072        let mut ext_ctx = ctx.clone();
15073        ext_ctx.columns = &ext_cols;
15074        projection
15075            .iter()
15076            .enumerate()
15077            .map(|(i, p)| {
15078                let e = rewritten[i].as_ref().unwrap_or(&p.expr);
15079                if eval::fully_compilable(e) {
15080                    Some(eval::compile_expr(e, &ext_ctx))
15081                } else {
15082                    None
15083                }
15084            })
15085            .collect()
15086    };
15087    Ok(SrfPlan {
15088        nodes,
15089        rewritten,
15090        ext_cols,
15091        compiled,
15092        base_cols,
15093    })
15094}
15095
15096/// One input row expanded through a plan built once for the whole scan.
15097/// v7.39 (round 621) — expand a projection whose target list contains
15098/// set-returning items, remembering which INPUT row each output row came from.
15099///
15100/// The three materialised-source tails — `FROM unnest(…)`, `FROM
15101/// generate_series(…)`, and the one that serves VALUES / a derived table /
15102/// `ROWS FROM (…)` — are near-copies of each other, and only the first knew
15103/// about target-list SRFs. So `SELECT unnest(ARRAY[1,2]), x FROM (VALUES (3),(4))
15104/// v(x)` answered `function unnest(integer[]) does not exist` on all the
15105/// others, for a query PG answers. Sharing the expansion is the point: a
15106/// fourth copy would have been the fourth place to forget.
15107fn expand_projection_srfs(
15108    engine: &Engine,
15109    projection: &[ProjectedItem],
15110    srf_idxs: &[usize],
15111    filtered: &[Row<'static>],
15112    ctx: &EvalContext<'_>,
15113) -> Result<(alloc::vec::Vec<Row<'static>>, alloc::vec::Vec<usize>), EngineError> {
15114    let mut out = alloc::vec::Vec::with_capacity(filtered.len());
15115    let mut src = alloc::vec::Vec::with_capacity(filtered.len());
15116    // v7.39 (round 726) — ONE plan for the whole scan. The per-row
15117    // spelling rebuilt it for every input row: a full clone of the
15118    // rewritten projection trees and the extended schema, 50k times on
15119    // the panel's unnest cell.
15120    let mut plan = build_srf_plan(engine, projection, srf_idxs, ctx)?;
15121    // v7.39 (round 733) — shard the expansion. Each shard clones the
15122    // plan (its ext_cols slot types are per-row mutable) and builds a
15123    // MINIMAL context — EvalContext is not Sync — which is sound only
15124    // when every expression involved is pure: the whole projection and
15125    // every SRF argument must be fully_compilable, or the row loop
15126    // stays serial with the full session context.
15127    // The projection is judged in its REWRITTEN form — the SRF call
15128    // itself is never compilable, but after the lift it is a plain
15129    // `__srf_N` column reference.
15130    let all_pure = projection
15131        .iter()
15132        .enumerate()
15133        .all(|(i, p)| eval::fully_compilable(plan.rewritten[i].as_ref().unwrap_or(&p.expr)))
15134        && plan.nodes.iter().all(|n| match n {
15135            Expr::FunctionCall { args, .. } => args.iter().all(eval::fully_compilable),
15136            other => eval::fully_compilable(other),
15137        });
15138    if all_pure
15139        && filtered.len() >= crate::PARALLEL_MIN_ROWS / 5
15140        && let Some(r) = engine.parallel_runner.0.as_deref()
15141    {
15142        let n_shards = (filtered.len() / (crate::PARALLEL_MIN_ROWS / 5)).clamp(2, 8);
15143        let chunk = filtered.len().div_ceil(n_shards);
15144        type ShardOut = Result<(Vec<Row<'static>>, Vec<usize>), EngineError>;
15145        let schema_cols = ctx.columns;
15146        let alias = ctx.table_alias;
15147        let mysql = ctx.mysql_dialect;
15148        let style = ctx.render_style;
15149        let plan_ref = &plan;
15150        let results = r.run_shards(n_shards, &|si| {
15151            let lo = si * chunk;
15152            let hi = ((si + 1) * chunk).min(filtered.len());
15153            let mut sctx = eval::EvalContext::new(schema_cols, alias);
15154            sctx.mysql_dialect = mysql;
15155            sctx.render_style = style;
15156            // v7.39 (round 743) — SrfPlan is no longer Clone (it carries
15157            // compiled programs); each shard rebuilds it, which also
15158            // recompiles against the shard's own context. Build errors
15159            // were already surfaced by the outer build above.
15160            let mut local_plan = match build_srf_plan(engine, projection, srf_idxs, &sctx) {
15161                Ok(p) => p,
15162                Err(e) => return alloc::boxed::Box::new(ShardOut::Err(e)) as _,
15163            };
15164            let mut run = || -> ShardOut {
15165                let mut o: Vec<Row<'static>> = Vec::with_capacity(hi - lo);
15166                let mut sidx: Vec<usize> = Vec::with_capacity(hi - lo);
15167                for (i, row) in filtered[lo..hi].iter().enumerate() {
15168                    let expanded =
15169                        expand_srf_row_with(engine, &mut local_plan, projection, row, &sctx)?;
15170                    sidx.extend(core::iter::repeat_n(lo + i, expanded.len()));
15171                    o.extend(expanded);
15172                }
15173                Ok((o, sidx))
15174            };
15175            alloc::boxed::Box::new(run())
15176        });
15177        for boxed in results {
15178            let shard = boxed
15179                .downcast::<ShardOut>()
15180                .expect("runner echoes the closure's box");
15181            let (o, sidx) = (*shard)?;
15182            out.extend(o);
15183            src.extend(sidx);
15184        }
15185        return Ok((out, src));
15186    }
15187    for (i, row) in filtered.iter().enumerate() {
15188        let expanded = expand_srf_row_with(engine, &mut plan, projection, row, ctx)?;
15189        src.extend(core::iter::repeat_n(i, expanded.len()));
15190        out.extend(expanded);
15191    }
15192    Ok((out, src))
15193}
15194
15195/// v7.39 (round 621) — one ORDER BY key, read from wherever it lives.
15196///
15197/// A key that names a select-list item reads it out of the EXPANDED row,
15198/// because PG sorts after the expansion. A key that names a source column the
15199/// query does not project is evaluated against the input row that output row
15200/// came from. `out_col` is `srf_order_output_cols`'s verdict for this key.
15201fn srf_order_key(
15202    ob: &spg_sql::ast::OrderBy,
15203    out_col: Option<usize>,
15204    out: &Row<'static>,
15205    src: &Row<'static>,
15206    ctx: &EvalContext<'_>,
15207) -> Result<Value<'static>, EngineError> {
15208    match out_col {
15209        Some(i) => Ok(out.values.get(i).cloned().unwrap_or(Value::Null)),
15210        None => eval::eval_expr(&ob.expr, src, ctx).map_err(EngineError::Eval),
15211    }
15212}
15213
15214fn expand_srf_row_with(
15215    engine: &Engine,
15216    plan: &mut SrfPlan,
15217    projection: &[ProjectedItem],
15218    row: &Row<'static>,
15219    ctx: &EvalContext<'_>,
15220) -> Result<Vec<Row<'static>>, EngineError> {
15221    let mut lists: Vec<Vec<Value<'static>>> = Vec::with_capacity(plan.nodes.len());
15222    for n in &plan.nodes {
15223        lists.push(engine.srf_values(n, row, ctx)?);
15224    }
15225    let n_rows = lists.iter().map(Vec::len).max().unwrap_or(0);
15226    // Only the slots' element types depend on the row; the names and the
15227    // input schema around them do not.
15228    for (slot, list) in lists.iter().enumerate() {
15229        plan.ext_cols[plan.base_cols + slot].ty = list
15230            .iter()
15231            .find_map(|v| v.data_type())
15232            .unwrap_or(DataType::Text);
15233    }
15234    let mut ext_ctx = ctx.clone();
15235    ext_ctx.columns = &plan.ext_cols;
15236    let mut out = Vec::with_capacity(n_rows);
15237    // v7.39 (round 726) — the base columns are the SAME for every
15238    // expanded row; clone them once and rewrite only the SRF slots per
15239    // k. The old form cloned the whole input row per OUTPUT row — for
15240    // `unnest(ARRAY[id, g])` over d that was a 100k-fold clone of a
15241    // TEXT column the projection never reads.
15242    let base_len = row.values.len();
15243    let mut ext_vals = row.values.clone();
15244    ext_vals.resize(base_len + lists.len(), Value::Null);
15245    let mut eval_stack: alloc::vec::Vec<Value<'static>> = alloc::vec::Vec::new();
15246    for k in 0..n_rows {
15247        for (slot, list) in lists.iter().enumerate() {
15248            // Past the end of THIS srf's rows → NULL (PG pads).
15249            ext_vals[base_len + slot] = list.get(k).cloned().unwrap_or(Value::Null);
15250        }
15251        let ext_row = Row::new(core::mem::take(&mut ext_vals));
15252        let mut vals = Vec::with_capacity(projection.len());
15253        for (i, p) in projection.iter().enumerate() {
15254            // v7.39 (round 743) — compiled when possible; the
15255            // interpreter for the rest, with its exact wording.
15256            vals.push(match &plan.compiled[i] {
15257                Some(c) => eval::eval_compiled(c, &ext_row, &ext_ctx, &mut eval_stack)
15258                    .map_err(EngineError::Eval)?,
15259                None => {
15260                    let expr = plan.rewritten[i].as_ref().unwrap_or(&p.expr);
15261                    eval::eval_expr(expr, &ext_row, &ext_ctx).map_err(EngineError::Eval)?
15262                }
15263            });
15264        }
15265        ext_vals = ext_row.values;
15266        out.push(Row::new(vals));
15267    }
15268    Ok(out)
15269}
15270
15271/// The one-shot spelling, for the callers that expand a single row.
15272/// v7.39 (round 600) — which output column each ORDER BY key names, for a
15273/// query whose target list contains a set-returning function.
15274///
15275/// The keys used to be built from the INPUT row, before the SRF expanded, so
15276/// anything that named the SRF's own output was evaluated as a scalar call:
15277/// `SELECT unnest(ARRAY[g,id]) v FROM sr ORDER BY v` answered
15278/// "function unnest(integer[]) does not exist", and so did the spellings that
15279/// repeat the call or reach it through `ORDER BY 1`. Where it did not error
15280/// it silently did nothing — `SELECT DISTINCT unnest(…) … ORDER BY 1` came
15281/// back in input order. PG sorts AFTER the expansion, so a key that names a
15282/// select-list item reads that item's value out of the expanded row.
15283///
15284/// `None` keeps the key on the input row, which is where an ORDER BY naming
15285/// a column the query does not project has to be evaluated.
15286/// v7.38.19 — the output column an ORDER BY term reads, when reading it
15287/// is provably the same as building a key from the input row.
15288///
15289/// A sort key is a COPY of the sort column, made because the source row
15290/// is gone by the time the sort runs — only the projection survives. On
15291/// `SELECT s_long FROM t ORDER BY s_long` that copy is of data the
15292/// projected row already holds, and on 400,000 rows of 192-character
15293/// text it is 400,000 allocations, 400,000 frees and 77 MB of copying.
15294/// A profile of that cell put the allocator at 2,025 leaf samples of the
15295/// working set, second only to the comparison chain.
15296///
15297/// The condition is narrow on purpose. `srf_order_output_cols` resolves
15298/// an ORDER BY term the way SQL does — a positional ordinal, or a name
15299/// matching the select list — and SQL resolves against the select list
15300/// BEFORE the input columns. The key path resolves against the INPUT
15301/// columns. For `SELECT g AS id … ORDER BY id` on a table that also has
15302/// an `id`, those are different columns, and swapping one for the other
15303/// would change answers rather than timings.
15304///
15305/// So this takes only the case where the two cannot disagree: a bare
15306/// unqualified column name, matching exactly one output item, whose own
15307/// expression is that same column. The projected cell then IS the input
15308/// cell, and the key would have been its copy.
15309/// True when comparing two of this column's VALUES gives the same order
15310/// as comparing the sort KEYS built from them.
15311///
15312/// It does not hold widely. A user ENUM stores its label as text but
15313/// orders by DECLARATION position; an array orders element-wise; a
15314/// domain or composite carries its own rules. For those the two paths
15315/// answer differently, and a sort that skipped the key would silently
15316/// reorder the result. This is the short list where they agree.
15317fn value_order_is_key_order(col: &ColumnSchema) -> bool {
15318    use spg_storage::DataType as T;
15319    col.user_enum_type.is_none()
15320        && col.user_domain_type.is_none()
15321        && col.user_composite_type.is_none()
15322        && col.collation_name.is_none()
15323        && col.collation == spg_storage::Collation::Binary
15324        && matches!(
15325            col.ty,
15326            T::SmallInt | T::Int | T::BigInt | T::Text | T::Varchar(_) | T::Bool | T::Uuid
15327        )
15328}
15329
15330/// The full ORDER BY comparison between two rows, named by index.
15331///
15332/// v7.38.19 — what a permutation sort falls back to when its key ties.
15333fn row_cmp_by_index(
15334    tagged: &[(Vec<crate::orderby::OrderKey>, Row<'static>)],
15335    terms: &[(usize, bool, Option<bool>)],
15336    colls: &[Option<crate::collate::Collated>],
15337    mysql: bool,
15338    ia: u32,
15339    ib: u32,
15340) -> core::cmp::Ordering {
15341    let (a, b) = (&tagged[ia as usize], &tagged[ib as usize]);
15342    for (i, (col, desc, nf)) in terms.iter().enumerate() {
15343        let (Some(va), Some(vb)) = (a.1.values.get(*col), b.1.values.get(*col)) else {
15344            continue;
15345        };
15346        let ord = match (va, vb) {
15347            (Value::Text(x), Value::Text(y)) => match colls.get(i).and_then(Option::as_ref) {
15348                Some(c) => {
15349                    let o = c.compare(x, y);
15350                    if *desc { o.reverse() } else { o }
15351                }
15352                None if !mysql => {
15353                    let o = crate::orderby::str_cmp_prefix_first(x, y);
15354                    if *desc { o.reverse() } else { o }
15355                }
15356                None => crate::orderby::order_by_value_cmp_in(*desc, *nf, va, vb, mysql),
15357            },
15358            _ => crate::orderby::order_by_value_cmp_in(*desc, *nf, va, vb, mysql),
15359        };
15360        if ord != core::cmp::Ordering::Equal {
15361            return ord;
15362        }
15363    }
15364    core::cmp::Ordering::Equal
15365}
15366
15367/// Whether ordering these rows by BYTES is what the collation in force
15368/// would have answered anyway.
15369///
15370/// v7.38.19 — a collated sort used to be shut out of the keyed path
15371/// entirely, and the cost of that showed up the moment the byte path
15372/// got fast: on the same fixture, the same binary took 92 ms under `C`
15373/// and 371 ms under `en_US`, so declaring a collation had become a
15374/// four-fold tax on a query that sorts md5 hex.
15375///
15376/// It need not be. For several locales `[0-9a-z]` orders exactly as
15377/// bytes do -- `collate::ascii_byte_order` carries that fact, and the
15378/// test beside it re-derives the whole allowlist by sorting a corpus
15379/// twice rather than asserting it. So when the collation is one of
15380/// those AND every value in every sort column is drawn from that
15381/// alphabet, the byte answer IS the collated answer.
15382///
15383/// Both halves are required. A collation outside the list can put `z`
15384/// between `s` and `t`; a value outside the alphabet can be `Ápple`,
15385/// which no locale in the list orders by its bytes. Either one and this
15386/// returns false, and the sort takes the collator's own path.
15387fn byte_order_answers_the_collation(
15388    tagged: &[(Vec<crate::orderby::OrderKey>, Row<'static>)],
15389    terms: &[(usize, bool, Option<bool>)],
15390    colls: &[Option<crate::collate::Collated>],
15391) -> bool {
15392    if colls.iter().all(Option::is_none) {
15393        return true;
15394    }
15395    if !colls
15396        .iter()
15397        .flatten()
15398        .all(crate::collate::Collated::ascii_byte_order)
15399    {
15400        return false;
15401    }
15402    tagged.iter().all(|(_, row)| {
15403        terms.iter().all(|(col, _, _)| match row.values.get(*col) {
15404            // Only TEXT is collation-sensitive; a number or a NULL
15405            // orders the same under every collation there is.
15406            Some(Value::Text(t)) => crate::collate::is_ascii_alnum_lower(t),
15407            _ => true,
15408        })
15409    })
15410}
15411
15412/// An eight-byte key for each row's sort column, paired with the row's
15413/// index — or `None` when the column cannot give one on every row.
15414///
15415/// v7.38.19 — the pair is what the sort array holds instead of the row.
15416/// Two kinds of column can supply it:
15417///
15418///   * an INTEGER, whose whole value fits. Flipping the sign bit maps
15419///     the signed order onto the unsigned one, so the key is EXACT and
15420///     a comparison never has to look at the row at all.
15421///   * TEXT, as the first eight bytes big-endian, zero-padded. That
15422///     orders the same as the string — two that differ inside those
15423///     bytes differ at the same index either way, and one shorter than
15424///     eight pads with zeros exactly where `[u8]`'s own comparison runs
15425///     out — but it is a PREFIX, so equal keys must still ask the full
15426///     comparator.
15427///
15428/// The `None` is the safety of it: a NULL or any other type has no
15429/// faithful eight-byte key, so such a column takes the ordinary path
15430/// rather than being given a made-up one.
15431fn sort_keys_of(
15432    tagged: &[(Vec<crate::orderby::OrderKey>, Row<'static>)],
15433    col: usize,
15434) -> Option<(Vec<(u64, u32)>, bool)> {
15435    let n = u32::try_from(tagged.len()).ok()?;
15436    let mut out: Vec<(u64, u32)> = Vec::with_capacity(tagged.len());
15437    let exact = match tagged.first()?.1.values.get(col)? {
15438        Value::Text(_) => false,
15439        Value::SmallInt(_) | Value::Int(_) | Value::BigInt(_) => true,
15440        _ => return None,
15441    };
15442    for (i, row) in (0..n).zip(tagged.iter()) {
15443        let key = match row.1.values.get(col) {
15444            Some(Value::Text(t)) if !exact => {
15445                let mut k = [0u8; 8];
15446                let bytes = t.as_bytes();
15447                let take = bytes.len().min(8);
15448                k[..take].copy_from_slice(&bytes[..take]);
15449                u64::from_be_bytes(k)
15450            }
15451            Some(Value::SmallInt(v)) if exact => (i64::from(*v) as u64) ^ (1 << 63),
15452            Some(Value::Int(v)) if exact => (i64::from(*v) as u64) ^ (1 << 63),
15453            Some(Value::BigInt(v)) if exact => (*v as u64) ^ (1 << 63),
15454            _ => return None,
15455        };
15456        out.push((key, i));
15457    }
15458    Some((out, exact))
15459}
15460
15461/// Whether a PREFIX key is worth sorting a permutation on.
15462///
15463/// v7.38.19 — it is not always, and the panel says so in one cell. The
15464/// `text (26 values)` fixture is two hundred identical characters drawn
15465/// from twenty-six letters, so every eight-byte prefix inside a letter
15466/// is the same and 15,000 rows tie on it. Each tie then pays the prefix
15467/// compare, a two-hundred-byte comparison, AND a random read into a
15468/// 400,000-element array — while sorting the rows in place keeps the
15469/// partition contiguous. Measured: 160 ms sorting rows, 247 ms sorting
15470/// the permutation, on the very fixture built to be degenerate.
15471///
15472/// So the permutation is taken when the key DECIDES, and a sample says
15473/// whether it does. An exact key always decides; a prefix has to earn
15474/// it.
15475fn key_discriminates(keys: &[(u64, u32)]) -> bool {
15476    const SAMPLE: usize = 1024;
15477    let step = (keys.len() / SAMPLE).max(1);
15478    let mut seen: Vec<u64> = keys
15479        .iter()
15480        .step_by(step)
15481        .take(SAMPLE)
15482        .map(|&(k, _)| k)
15483        .collect();
15484    let taken = seen.len();
15485    if taken < 8 {
15486        return true;
15487    }
15488    seen.sort_unstable();
15489    seen.dedup();
15490    seen.len() * 2 >= taken
15491}
15492
15493fn order_by_output_cols_if_identical(
15494    order_by: &[spg_sql::ast::OrderBy],
15495    projection: &[ProjectedItem],
15496    schema_cols: &[ColumnSchema],
15497) -> Option<Vec<usize>> {
15498    if order_by.is_empty() {
15499        return None;
15500    }
15501    let mut out = Vec::with_capacity(order_by.len());
15502    for ob in order_by {
15503        let Expr::Column(c) = &ob.expr else {
15504            return None;
15505        };
15506        if c.qualifier.is_some() {
15507            return None;
15508        }
15509        let mut hit = None;
15510        for (i, p) in projection.iter().enumerate() {
15511            if !p.output_name.eq_ignore_ascii_case(&c.name) {
15512                continue;
15513            }
15514            if hit.is_some() {
15515                return None; // ambiguous — SQL would reject it too
15516            }
15517            // The item must BE that column, not merely be named for it.
15518            let Expr::Column(pc) = &p.expr else {
15519                return None;
15520            };
15521            if !pc.name.eq_ignore_ascii_case(&c.name) {
15522                return None;
15523            }
15524            let sc = schema_cols
15525                .iter()
15526                .find(|s| s.name.eq_ignore_ascii_case(&c.name))?;
15527            if !value_order_is_key_order(sc) {
15528                return None;
15529            }
15530            hit = Some(i);
15531        }
15532        out.push(hit?);
15533    }
15534    Some(out)
15535}
15536
15537fn srf_order_output_cols(
15538    order_by: &[spg_sql::ast::OrderBy],
15539    projection: &[ProjectedItem],
15540) -> Vec<Option<usize>> {
15541    order_by
15542        .iter()
15543        .map(|ob| {
15544            // A positive ordinal is the Nth output column, directly.
15545            // `resolve_positional_order_by` deliberately leaves an ordinal
15546            // pointing at a set-returning item alone — copying the call into
15547            // ORDER BY would have made the key "the whole set" back when keys
15548            // came from the input row. Reading the expanded row's column is
15549            // what it should have meant, and is what this does.
15550            if let Expr::Literal(spg_sql::ast::Literal::Integer(n)) = &ob.expr
15551                && *n >= 1
15552                && let Ok(idx) = usize::try_from(*n - 1)
15553                && idx < projection.len()
15554            {
15555                return Some(idx);
15556            }
15557            // An unqualified name matching exactly one output name. SQL
15558            // resolves ORDER BY against the select list first, so this wins
15559            // over an input column of the same name — which is the whole
15560            // point of `SELECT g AS id … ORDER BY id`.
15561            if let Expr::Column(c) = &ob.expr
15562                && c.qualifier.is_none()
15563            {
15564                let mut hit = None;
15565                for (i, p) in projection.iter().enumerate() {
15566                    if p.output_name.eq_ignore_ascii_case(&c.name) {
15567                        if hit.is_some() {
15568                            hit = None;
15569                            break;
15570                        }
15571                        hit = Some(i);
15572                    }
15573                }
15574                if hit.is_some() {
15575                    return hit;
15576                }
15577            }
15578            // Or the same expression as a select-list item — which is what
15579            // `ORDER BY 1` becomes once `resolve_positional_order_by` has
15580            // run, and what a repeated `ORDER BY unnest(…)` is.
15581            projection.iter().position(|p| p.expr == ob.expr)
15582        })
15583        .collect()
15584}
15585
15586fn expand_srf_row(
15587    engine: &Engine,
15588    projection: &[ProjectedItem],
15589    srf_idxs: &[usize],
15590    row: &Row<'static>,
15591    ctx: &EvalContext<'_>,
15592) -> Result<Vec<Row<'static>>, EngineError> {
15593    let mut plan = build_srf_plan(engine, projection, srf_idxs, ctx)?;
15594    expand_srf_row_with(engine, &mut plan, projection, row, ctx)
15595}
15596
15597impl Engine {
15598    /// The rows one target-list SRF yields for an input row. `None` from
15599    /// `srf_target_idxs` means the expression is not set-returning at all.
15600    fn srf_values(
15601        &self,
15602        expr: &spg_sql::ast::Expr,
15603        row: &Row<'static>,
15604        ctx: &EvalContext<'_>,
15605    ) -> Result<Vec<Value<'static>>, EngineError> {
15606        if top_level_srf_kind(expr).is_some() {
15607            return top_level_srf_output(expr, row, ctx);
15608        }
15609        // A user set-returning function. Its body runs through the real
15610        // executor, like every function body since round 63.
15611        let spg_sql::ast::Expr::FunctionCall { name, args } = expr else {
15612            return Err(EngineError::Unsupported(
15613                "expected a SELECT-list SRF call".into(),
15614            ));
15615        };
15616        let mut vals: alloc::vec::Vec<Value<'static>> = alloc::vec::Vec::new();
15617        for a in args {
15618            vals.push(eval::eval_expr(a, row, ctx).map_err(EngineError::Eval)?);
15619        }
15620        let (rows, cols) = self.setof_rows_of(name, &vals, None)?;
15621        // v7.39 (read01 round 68) — in a target list a multi-column function is
15622        // a RECORD, one composite value per row: `SELECT rows_of(2)` gives
15623        // `(2,b)`, `(3,c)`. Value::Composite has existed since round 56; this is
15624        // what it is for. A single-column function contributes its bare value.
15625        Ok(rows
15626            .into_iter()
15627            .map(|r| {
15628                if r.values.len() == 1 {
15629                    r.values.into_iter().next().unwrap_or(Value::Null)
15630                } else {
15631                    Value::Composite(
15632                        cols.iter()
15633                            .map(|c| c.name.clone())
15634                            .zip(r.values)
15635                            .collect::<alloc::vec::Vec<_>>(),
15636                    )
15637                }
15638            })
15639            .collect())
15640    }
15641
15642    /// Is THIS node a set-returning call: one of the builtin kinds, or a user
15643    /// function declared `RETURNS SETOF` / `RETURNS TABLE`.
15644    fn is_srf_node(&self, e: &spg_sql::ast::Expr) -> bool {
15645        if is_top_level_unnest(e) {
15646            return true;
15647        }
15648        let spg_sql::ast::Expr::FunctionCall { name, .. } = e else {
15649            return false;
15650        };
15651        self.active_catalog().functions_named(name).iter().any(|f| {
15652            let r = f.returns.trim().to_ascii_uppercase();
15653            r.starts_with("SETOF") || r.starts_with("TABLE(")
15654        })
15655    }
15656
15657    /// Does an SRF appear ANYWHERE in this expression (not only as its root)?
15658    fn expr_contains_srf(&self, e: &spg_sql::ast::Expr) -> bool {
15659        let mut found = false;
15660        let mut probe = e.clone();
15661        crate::expr_analysis::rewrite_nodes_mut(&mut probe, &mut |n| {
15662            if self.is_srf_node(n) {
15663                found = true;
15664                return true;
15665            }
15666            false
15667        });
15668        found
15669    }
15670
15671    /// Which projection items CONTAIN a set-returning call. Before round 78 this
15672    /// asked whether the item WAS one, so `upper(unnest(a))` looked like an
15673    /// ordinary scalar call all the way down to the function dispatcher, which
15674    /// then reported `unnest` as an unknown function.
15675    fn srf_target_idxs(&self, projection: &[ProjectedItem]) -> alloc::vec::Vec<usize> {
15676        projection
15677            .iter()
15678            .enumerate()
15679            .filter(|(_, p)| self.expr_contains_srf(&p.expr))
15680            .map(|(i, _)| i)
15681            .collect()
15682    }
15683}
15684
15685impl Engine {
15686    /// v7.39 (read01 round 74) — see the call site. `None` when the statement has
15687    /// no `(f(args)).*` item.
15688    fn lower_record_expansion(
15689        &self,
15690        stmt: &SelectStatement,
15691    ) -> Result<Option<SelectStatement>, EngineError> {
15692        use spg_sql::ast::{Expr, SelectItem};
15693        let is_marker = |it: &SelectItem| {
15694            matches!(it, SelectItem::Expr { expr: Expr::FunctionCall { name, .. }, .. }
15695                if name == "__record_expand")
15696        };
15697        if !stmt.items.iter().any(is_marker) {
15698            return Ok(None);
15699        }
15700        let mut out = stmt.clone();
15701        let mut items: alloc::vec::Vec<SelectItem> = alloc::vec::Vec::new();
15702        let mut lateral_refs: alloc::vec::Vec<TableRef> = alloc::vec::Vec::new();
15703        for (n, item) in stmt.items.iter().enumerate() {
15704            if !is_marker(item) {
15705                items.push(item.clone());
15706                continue;
15707            }
15708            let SelectItem::Expr {
15709                expr: Expr::FunctionCall { args, .. },
15710                ..
15711            } = item
15712            else {
15713                unreachable!("checked by is_marker");
15714            };
15715            let Some(Expr::FunctionCall {
15716                name: fname,
15717                args: fargs,
15718            }) = args.first()
15719            else {
15720                return Err(EngineError::Unsupported(
15721                    "(<expr>).* expands a function's record — it needs a function call".into(),
15722                ));
15723            };
15724            let cols = self.setof_declared_columns(fname)?;
15725            let alias = alloc::format!("__rec{n}");
15726            let mut tref = bare_table_ref_named(&alias);
15727            tref.table_fn_call = Some(alloc::boxed::Box::new((
15728                fname.to_ascii_lowercase(),
15729                fargs.clone(),
15730            )));
15731            tref.alias = Some(alias.clone());
15732            lateral_refs.push(tref);
15733            for c in cols {
15734                items.push(SelectItem::Expr {
15735                    expr: Expr::Column(spg_sql::ast::ColumnName {
15736                        qualifier: Some(alias.clone()),
15737                        name: c,
15738                    }),
15739                    alias: None,
15740                });
15741            }
15742        }
15743        out.items = items;
15744        // The function joins the FROM. With no FROM it BECOMES the FROM; with one
15745        // it is a cross join, which is what `SELECT …, (f(t.c)).* FROM t` means
15746        // (the arguments may reference the outer row — the round-69 correlation).
15747        for tref in lateral_refs {
15748            match &mut out.from {
15749                None => {
15750                    out.from = Some(spg_sql::ast::FromClause {
15751                        primary: tref,
15752                        joins: alloc::vec::Vec::new(),
15753                    });
15754                }
15755                Some(from) => from.joins.push(spg_sql::ast::FromJoin {
15756                    kind: spg_sql::ast::JoinKind::Cross,
15757                    table: tref,
15758                    on: None,
15759                    using_cols: None,
15760                    natural: false,
15761                }),
15762            }
15763        }
15764        Ok(Some(out))
15765    }
15766
15767    /// The column NAMES a set-returning function declares: `RETURNS TABLE(id int,
15768    /// v text)` names them; a `SETOF <scalar>` is one column named after the
15769    /// function.
15770    fn setof_declared_columns(
15771        &self,
15772        name: &str,
15773    ) -> Result<alloc::vec::Vec<alloc::string::String>, EngineError> {
15774        let cat = self.active_catalog();
15775        let overloads = cat.functions_named(name);
15776        let def = overloads.first().ok_or_else(|| {
15777            EngineError::Unsupported(alloc::format!("function {name} does not exist"))
15778        })?;
15779        let declared = def.returns.trim();
15780        let upper = declared.to_ascii_uppercase();
15781        if upper.starts_with("TABLE(") {
15782            let raw = &declared["TABLE(".len()..declared.len() - 1];
15783            return Ok(raw
15784                .split(',')
15785                .map(|d| d.split_whitespace().next().unwrap_or("col").to_string())
15786                .collect());
15787        }
15788        Ok(alloc::vec![name.to_string()])
15789    }
15790}
15791
15792/// A bare `TableRef` with a name — the FROM item a lowered record expansion adds.
15793/// v7.39 (round 205, JSON_TABLE) — the static output schema of a
15794/// COLUMNS list (data-independent), NESTED children inlined in
15795/// declaration order (PG's flattened output shape).
15796/// v7.39 (round 205) — pub(crate) shim so join.rs infers a wrapped
15797/// correlated JSON_TABLE's static schema without evaluating its doc.
15798pub(crate) fn json_table_schema_pub(
15799    cols: &[spg_sql::ast::JsonTableColumn],
15800) -> alloc::vec::Vec<ColumnSchema> {
15801    json_table_schema(cols)
15802}
15803
15804fn json_table_schema(cols: &[spg_sql::ast::JsonTableColumn]) -> alloc::vec::Vec<ColumnSchema> {
15805    use spg_sql::ast::JsonTableColumn as C;
15806    let mut out = alloc::vec::Vec::new();
15807    for c in cols {
15808        match c {
15809            C::Ordinality { name } => {
15810                out.push(ColumnSchema::new(name.clone(), DataType::BigInt, false));
15811            }
15812            C::Regular {
15813                name, ty, exists, ..
15814            } => {
15815                let dt = if *exists {
15816                    DataType::Bool
15817                } else {
15818                    crate::conversions::column_type_to_data_type(*ty)
15819                };
15820                out.push(ColumnSchema::new(name.clone(), dt, true));
15821            }
15822            C::Nested { columns, .. } => out.extend(json_table_schema(columns)),
15823        }
15824    }
15825    out
15826}
15827
15828/// v7.39 (round 205) — coerce a DEFAULT / literal value to a
15829/// JSON_TABLE column's declared type (the DEFAULT expr may be a
15830/// string literal like `'none'` that must land as the column type).
15831fn coerce_json_table_default(
15832    v: Value<'static>,
15833    ty: spg_sql::ast::ColumnTypeName,
15834    name: &str,
15835) -> Result<Value<'static>, EngineError> {
15836    if v.is_null() {
15837        return Ok(Value::Null);
15838    }
15839    let dt = crate::conversions::column_type_to_data_type(ty);
15840    crate::conversions::coerce_value(v, dt, name, 0)
15841}
15842
15843/// v7.39 (round 205) — a runtime Value → JsonValue for PASSING vars.
15844fn value_to_json_value(v: &Value<'_>) -> crate::json::JsonValue {
15845    use crate::json::JsonValue as J;
15846    match v {
15847        Value::Null => J::Null,
15848        Value::Bool(b) => J::Bool(*b),
15849        Value::SmallInt(n) => J::Number(f64::from(*n)),
15850        Value::Int(n) => J::Number(f64::from(*n)),
15851        Value::BigInt(n) => J::Number(*n as f64),
15852        Value::Float(x) => J::Number(*x),
15853        Value::Json(s) => crate::json::parse_doc(s).unwrap_or(J::Null),
15854        other => J::String(crate::eval::value_to_text(other)),
15855    }
15856}
15857
15858fn bare_table_ref_named(name: &str) -> TableRef {
15859    TableRef {
15860        name: name.to_string(),
15861        alias: None,
15862        only: false,
15863        as_of_segment: None,
15864        unnest_expr: None,
15865        unnest_column_aliases: alloc::vec::Vec::new(),
15866        with_ordinality: false,
15867        generate_series_args: None,
15868        lateral_subquery: None,
15869        jsonb_each_text_arg: None,
15870        table_fn_call: None,
15871        rows_from: None,
15872        json_table: None,
15873        scalar_fn_item: false,
15874    }
15875}
15876
15877impl Engine {
15878    /// v7.39 (read01 round 74) — run a `ROWS FROM (…)` list. Each entry yields its
15879    /// own rows; they zip in lockstep and a short one pads with NULL. `__array`
15880    /// entries are the array-able SRFs, already lowered by the parser into their
15881    /// scalar array form.
15882    fn rows_from_rows(
15883        &self,
15884        primary: &TableRef,
15885    ) -> Result<(alloc::vec::Vec<Row<'static>>, alloc::vec::Vec<ColumnSchema>), EngineError> {
15886        let entries = primary
15887            .rows_from
15888            .as_ref()
15889            .expect("caller guards rows_from.is_some()");
15890        let empty: alloc::vec::Vec<ColumnSchema> = alloc::vec::Vec::new();
15891        let ctx = self.ev_ctx(&empty, None);
15892        let dummy = Row::new(alloc::vec::Vec::new());
15893        let mut lists: alloc::vec::Vec<alloc::vec::Vec<Value<'static>>> = alloc::vec::Vec::new();
15894        let mut cols: alloc::vec::Vec<ColumnSchema> = alloc::vec::Vec::new();
15895        for (name, args) in entries {
15896            let (vals, colname) = if name == "__array" {
15897                // The parser lowered this one to `<array expr>`; its rows are the
15898                // array's elements.
15899                let arr = eval::eval_expr(&args[0], &dummy, &ctx).map_err(EngineError::Eval)?;
15900                (
15901                    array_value_to_elements(&arr)?,
15902                    alloc::string::String::from("unnest"),
15903                )
15904            } else {
15905                let call = spg_sql::ast::Expr::FunctionCall {
15906                    name: name.clone(),
15907                    args: args.clone(),
15908                };
15909                (self.srf_values(&call, &dummy, &ctx)?, name.clone())
15910            };
15911            let ty = vals
15912                .first()
15913                .and_then(spg_storage::Value::data_type)
15914                .unwrap_or(DataType::Text);
15915            cols.push(ColumnSchema::new(colname, ty, true));
15916            lists.push(vals);
15917        }
15918        let n = lists.iter().map(alloc::vec::Vec::len).max().unwrap_or(0);
15919        let mut rows: alloc::vec::Vec<Row<'static>> = alloc::vec::Vec::with_capacity(n);
15920        for k in 0..n {
15921            let mut vals: alloc::vec::Vec<Value<'static>> =
15922                alloc::vec::Vec::with_capacity(lists.len() + 1);
15923            for l in &lists {
15924                vals.push(l.get(k).cloned().unwrap_or(Value::Null));
15925            }
15926            rows.push(Row::new(vals));
15927        }
15928        if primary.with_ordinality {
15929            cols.push(ColumnSchema::new(
15930                "ordinality".to_string(),
15931                DataType::BigInt,
15932                false,
15933            ));
15934            rows = rows
15935                .into_iter()
15936                .enumerate()
15937                .map(|(i, r)| {
15938                    let mut v = r.values;
15939                    v.push(Value::BigInt(i as i64 + 1));
15940                    Row::new(v)
15941                })
15942                .collect();
15943        }
15944        Ok((rows, cols))
15945    }
15946}
15947
15948/// v7.39 (round 232) — PG names the offending set operation in its
15949/// arity / type-mismatch messages ("each UNION query must have the same
15950/// number of columns"). `UNION ALL` is still spelled UNION there.
15951fn set_op_name(kind: UnionKind) -> &'static str {
15952    match kind {
15953        UnionKind::All | UnionKind::Distinct => "UNION",
15954        UnionKind::Intersect | UnionKind::IntersectAll => "INTERSECT",
15955        UnionKind::Except | UnionKind::ExceptAll => "EXCEPT",
15956    }
15957}
15958
15959/// v7.39 (round 233) — which output columns of a branch are PG's `unknown`
15960/// type: a bare string or NULL literal that no context has typed yet. SPG
15961/// has no `Unknown` DataType (both describe as TEXT), so the witness has to
15962/// be the syntax. A wildcard or a non-literal expression is never unknown.
15963/// 7.38.1 S5.1 — is this branch item a reg* cast? Its result column
15964/// LABELS as text (the wire render) but the value is an oid-carrying
15965/// dual, so a UNION with a numeric column must not be refused on the
15966/// label (pg_dump: `SELECT classid … UNION ALL SELECT
15967/// 'pg_opfamily'::regclass …`).
15968fn branch_regcast_mask(stmt: &SelectStatement) -> Vec<bool> {
15969    fn is_regcast(e: &Expr) -> bool {
15970        matches!(
15971            e,
15972            Expr::Cast {
15973                target: spg_sql::ast::CastTarget::RegType | spg_sql::ast::CastTarget::RegClass,
15974                ..
15975            }
15976        )
15977    }
15978    stmt.items
15979        .iter()
15980        .map(|item| match item {
15981            SelectItem::Expr { expr, .. } => is_regcast(expr),
15982            _ => false,
15983        })
15984        .collect()
15985}
15986
15987fn branch_unknown_mask(stmt: &SelectStatement) -> Vec<bool> {
15988    stmt.items
15989        .iter()
15990        .map(|item| match item {
15991            SelectItem::Expr { expr, .. } => matches!(
15992                expr,
15993                Expr::Literal(spg_sql::ast::Literal::String(_))
15994                    | Expr::Literal(spg_sql::ast::Literal::Null)
15995            ),
15996            _ => false,
15997        })
15998        .collect()
15999}
16000
16001/// v7.39 (round 233) — retype one branch column's cells, reporting the
16002/// conversion failure the way PG does rather than leaving the column
16003/// half-converted. Used when the other branch typed an untyped literal.
16004fn coerce_branch_column(
16005    rows: &mut [Row<'static>],
16006    col_idx: usize,
16007    target: DataType,
16008    col_name: &str,
16009) -> Result<(), EngineError> {
16010    for row in rows.iter_mut() {
16011        let Some(slot) = row.values.get_mut(col_idx) else {
16012            continue;
16013        };
16014        if matches!(slot, Value::Null) {
16015            continue;
16016        }
16017        *slot = crate::conversions::coerce_value(slot.clone(), target, col_name, col_idx)?;
16018    }
16019    Ok(())
16020}
16021
16022/// v7.39 (round 727) — PG-style pull-up of a SIMPLE derived table:
16023/// `SELECT … FROM (SELECT <bare columns> FROM t [WHERE …]) q …`
16024/// rewrites to `SELECT …' FROM t [WHERE inner AND outer'] …` with every
16025/// reference to q's output columns substituted by the underlying column.
16026///
16027/// Admission is deliberately narrow — anything that changes cardinality,
16028/// order, or scope stays on the materialising path:
16029/// * outer: no CTEs / unions / DISTINCT [ON] / windows, single derived
16030///   FROM with no ordinality or positional column aliases, and no
16031///   subquery anywhere its expressions (an inner scope could reference
16032///   q too — descending is a later knife);
16033/// * inner: one stored table, bare-column projection only, no
16034///   CTE/union/DISTINCT/GROUP/HAVING/ORDER/LIMIT/OFFSET/windows/locking;
16035/// * every outer column reference must resolve inside q's output list —
16036///   a name that does not is an ERROR today, and flattening would
16037///   silently legalise it against the base table.
16038fn try_flatten_derived(stmt: &SelectStatement, primary: &TableRef) -> Option<SelectStatement> {
16039    use spg_sql::ast::SelectItem;
16040    let inner = primary.lateral_subquery.as_deref()?;
16041    // Outer shape.
16042    if !stmt.ctes.is_empty()
16043        || !stmt.unions.is_empty()
16044        || stmt.distinct
16045        || !stmt.distinct_on.is_empty()
16046        || !stmt.window_check_exprs.is_empty()
16047        || stmt.locking.is_some()
16048        || primary.with_ordinality
16049        || !primary.unnest_column_aliases.is_empty()
16050    {
16051        return None;
16052    }
16053    // Inner shape.
16054    if !inner.ctes.is_empty()
16055        || !inner.unions.is_empty()
16056        || inner.distinct
16057        || !inner.distinct_on.is_empty()
16058        || inner.group_by.is_some()
16059        || inner.group_by_all
16060        || inner.having.is_some()
16061        || !inner.order_by.is_empty()
16062        || inner.limit.is_some()
16063        || inner.offset.is_some()
16064        || !inner.window_check_exprs.is_empty()
16065        || inner.locking.is_some()
16066    {
16067        return None;
16068    }
16069    let ifrom = inner.from.as_ref()?;
16070    let it = &ifrom.primary;
16071    if !ifrom.joins.is_empty()
16072        || it.name.is_empty()
16073        || it.lateral_subquery.is_some()
16074        || it.unnest_expr.is_some()
16075        || it.generate_series_args.is_some()
16076        || it.as_of_segment.is_some()
16077        || it.jsonb_each_text_arg.is_some()
16078        || it.table_fn_call.is_some()
16079        || it.rows_from.is_some()
16080        || it.json_table.is_some()
16081        || it.with_ordinality
16082        || !it.unnest_column_aliases.is_empty()
16083    {
16084        return None;
16085    }
16086    if inner.where_.as_ref().is_some_and(crate::expr_has_subquery) {
16087        return None;
16088    }
16089    // The output map: q's visible name -> the underlying column.
16090    let inner_alias = it.alias.clone().unwrap_or_else(|| it.name.clone());
16091    let mut map: alloc::collections::BTreeMap<String, spg_sql::ast::ColumnName> =
16092        alloc::collections::BTreeMap::new();
16093    for item in &inner.items {
16094        let SelectItem::Expr { expr, alias } = item else {
16095            return None;
16096        };
16097        let Expr::Column(c) = expr else {
16098            return None;
16099        };
16100        if let Some(q) = c.qualifier.as_deref()
16101            && !q.eq_ignore_ascii_case(&inner_alias)
16102        {
16103            return None;
16104        }
16105        let out_name = alias.clone().unwrap_or_else(|| c.name.clone());
16106        // A duplicated output name would make substitution ambiguous.
16107        if map
16108            .insert(out_name.to_ascii_lowercase(), c.clone())
16109            .is_some()
16110        {
16111            return None;
16112        }
16113    }
16114    if map.is_empty() {
16115        return None;
16116    }
16117    let derived_alias = primary
16118        .alias
16119        .clone()
16120        .unwrap_or_else(|| primary.name.clone())
16121        .to_ascii_lowercase();
16122    // Substitute in a clone; bail (None) on the first reference the map
16123    // cannot answer.
16124    let mut out = stmt.clone();
16125    let ok = core::cell::Cell::new(true);
16126    let mut subst = |e: &mut Expr| -> bool {
16127        match e {
16128            Expr::Column(c) => {
16129                match c.qualifier.as_deref() {
16130                    Some(q) if q.eq_ignore_ascii_case(&derived_alias) => {}
16131                    None => {}
16132                    Some(_) => {
16133                        ok.set(false);
16134                        return true;
16135                    }
16136                }
16137                match map.get(&c.name.to_ascii_lowercase()) {
16138                    Some(target) => *c = target.clone(),
16139                    None => ok.set(false),
16140                }
16141                true
16142            }
16143            // Any subquery could reference q from its own scope;
16144            // descending is a later knife — bail for now.
16145            Expr::ScalarSubquery(_)
16146            | Expr::Exists { .. }
16147            | Expr::InSubquery { .. }
16148            | Expr::RowInSubquery { .. }
16149            | Expr::RowCmpSubquery { .. } => {
16150                ok.set(false);
16151                true
16152            }
16153            _ => false,
16154        }
16155    };
16156    for item in &mut out.items {
16157        match item {
16158            SelectItem::Expr { expr, .. } => {
16159                crate::expr_analysis::rewrite_nodes_mut(expr, &mut subst);
16160            }
16161            // `SELECT * FROM (…) q` means q's columns, in q's order.
16162            SelectItem::Wildcard | SelectItem::QualifiedWildcard(_) => return None,
16163        }
16164    }
16165    if let Some(w) = &mut out.where_ {
16166        crate::expr_analysis::rewrite_nodes_mut(w, &mut subst);
16167    }
16168    if let Some(gs) = &mut out.group_by {
16169        for g in gs {
16170            crate::expr_analysis::rewrite_nodes_mut(g, &mut subst);
16171        }
16172    }
16173    if let Some(h) = &mut out.having {
16174        crate::expr_analysis::rewrite_nodes_mut(h, &mut subst);
16175    }
16176    for o in &mut out.order_by {
16177        crate::expr_analysis::rewrite_nodes_mut(&mut o.expr, &mut subst);
16178    }
16179    for d in &mut out.distinct_on {
16180        crate::expr_analysis::rewrite_nodes_mut(d, &mut subst);
16181    }
16182    if !ok.get() {
16183        return None;
16184    }
16185    // FROM becomes the stored table; the filters conjoin.
16186    out.from = Some(spg_sql::ast::FromClause {
16187        primary: it.clone(),
16188        joins: Vec::new(),
16189    });
16190    out.where_ = match (inner.where_.clone(), out.where_.take()) {
16191        (Some(a), Some(b)) => Some(Expr::Binary {
16192            lhs: alloc::boxed::Box::new(a),
16193            op: spg_sql::ast::BinOp::And,
16194            rhs: alloc::boxed::Box::new(b),
16195        }),
16196        (Some(a), None) => Some(a),
16197        (None, b) => b,
16198    };
16199    Some(out)
16200}
16201
16202/// v7.39 (round 742) — rewrite `SELECT count(*) FROM (SELECT <plain>
16203/// FROM t [WHERE p] ORDER BY … OFFSET k [no LIMIT]) q` into
16204/// `SELECT greatest(count(*) - k, 0) FROM t [WHERE p]`. Sound because
16205/// ORDER BY is count-invariant and OFFSET k drops exactly min(k, n)
16206/// rows. Admission mirrors the flatten's conservatism; a LIMIT, a
16207/// DISTINCT, an SRF, or an unprovable inner shape stays put.
16208fn try_count_over_offset(stmt: &SelectStatement, primary: &TableRef) -> Option<SelectStatement> {
16209    use spg_sql::ast::{Expr as E, LimitExpr, SelectItem};
16210    let inner = primary.lateral_subquery.as_deref()?;
16211    // Outer: exactly `SELECT count(*)`, nothing else.
16212    if !stmt.ctes.is_empty()
16213        || !stmt.unions.is_empty()
16214        || stmt.distinct
16215        || !stmt.distinct_on.is_empty()
16216        || stmt.where_.is_some()
16217        || stmt.group_by.is_some()
16218        || stmt.having.is_some()
16219        || !stmt.order_by.is_empty()
16220        || stmt.limit.is_some()
16221        || stmt.offset.is_some()
16222        || stmt.items.len() != 1
16223    {
16224        return None;
16225    }
16226    let SelectItem::Expr { expr, .. } = &stmt.items[0] else {
16227        return None;
16228    };
16229    let E::FunctionCall { name, args } = expr else {
16230        return None;
16231    };
16232    if !name.eq_ignore_ascii_case("count_star") || !args.is_empty() {
16233        return None;
16234    }
16235    // Inner: flatten-shaped plus ORDER BY and a literal OFFSET, no LIMIT.
16236    let Some(LimitExpr::Literal(k)) = &inner.offset else {
16237        return None;
16238    };
16239    let k = i64::from(*k);
16240    if inner.limit.is_some() || inner.order_by.is_empty() {
16241        return None;
16242    }
16243    let mut counted = inner.clone();
16244    counted.order_by = Vec::new();
16245    counted.offset = None;
16246    // The stripped inner must now be a provable simple shape (its
16247    // items become irrelevant — count(*) reads none of them — but an
16248    // SRF item would change the row count, so the flatten predicate's
16249    // scrutiny still applies).
16250    let base = matview_flatten_probe(&counted)?;
16251    let mut out = stmt.clone();
16252    out.items = alloc::vec![SelectItem::Expr {
16253        expr: E::FunctionCall {
16254            name: String::from("greatest"),
16255            args: alloc::vec![
16256                E::Binary {
16257                    lhs: alloc::boxed::Box::new(E::FunctionCall {
16258                        name: String::from("count_star"),
16259                        args: alloc::vec![],
16260                    }),
16261                    op: spg_sql::ast::BinOp::Sub,
16262                    rhs: alloc::boxed::Box::new(E::Literal(spg_sql::ast::Literal::Integer(k))),
16263                },
16264                E::Literal(spg_sql::ast::Literal::Integer(0)),
16265            ],
16266        },
16267        alias: Some(String::from("count")),
16268    }];
16269    out.from = Some(spg_sql::ast::FromClause {
16270        primary: base,
16271        joins: Vec::new(),
16272    });
16273    out.where_ = counted.where_.clone();
16274    Some(out)
16275}
16276
16277/// The inner-shape probe `try_count_over_offset` shares with the
16278/// flatten: single stored table, no modifiers, no subqueries, no SRF
16279/// items. Returns the base TableRef.
16280fn matview_flatten_probe(inner: &SelectStatement) -> Option<TableRef> {
16281    use spg_sql::ast::SelectItem;
16282    if !inner.ctes.is_empty()
16283        || !inner.unions.is_empty()
16284        || inner.distinct
16285        || !inner.distinct_on.is_empty()
16286        || inner.group_by.is_some()
16287        || inner.group_by_all
16288        || inner.having.is_some()
16289        || !inner.order_by.is_empty()
16290        || inner.limit.is_some()
16291        || inner.offset.is_some()
16292        || !inner.window_check_exprs.is_empty()
16293        || inner.locking.is_some()
16294    {
16295        return None;
16296    }
16297    let ifrom = inner.from.as_ref()?;
16298    let it = &ifrom.primary;
16299    if !ifrom.joins.is_empty()
16300        || it.name.is_empty()
16301        || it.lateral_subquery.is_some()
16302        || it.unnest_expr.is_some()
16303        || it.generate_series_args.is_some()
16304        || it.as_of_segment.is_some()
16305        || it.jsonb_each_text_arg.is_some()
16306        || it.table_fn_call.is_some()
16307        || it.rows_from.is_some()
16308        || it.json_table.is_some()
16309        || it.with_ordinality
16310    {
16311        return None;
16312    }
16313    for item in &inner.items {
16314        match item {
16315            SelectItem::Expr { expr, .. } => {
16316                if crate::expr_has_subquery(expr) || expr_contains_builtin_srf(expr) {
16317                    return None;
16318                }
16319            }
16320            SelectItem::Wildcard => {}
16321            SelectItem::QualifiedWildcard(_) => return None,
16322        }
16323    }
16324    if inner.where_.as_ref().is_some_and(crate::expr_has_subquery) {
16325        return None;
16326    }
16327    Some(it.clone())
16328}
16329
16330/// v7.39 (round 743) — rewrite `SELECT count(*) FROM (SELECT
16331/// unnest(ARRAY[e1..ek]) [AS v] FROM t [WHERE p]) q` into
16332/// `SELECT count(*) * k FROM t [WHERE p]`. Sound because a
16333/// constant-LENGTH array literal unnests to exactly k rows per input
16334/// row (NULL elements are rows too). One SRF item only, elements
16335/// subquery-free, and the stripped inner must pass the same probe the
16336/// count-over-offset rewrite uses.
16337fn try_count_over_const_unnest(
16338    stmt: &SelectStatement,
16339    primary: &TableRef,
16340) -> Option<SelectStatement> {
16341    use spg_sql::ast::{Expr as E, SelectItem};
16342    let inner = primary.lateral_subquery.as_deref()?;
16343    if !stmt.ctes.is_empty()
16344        || !stmt.unions.is_empty()
16345        || stmt.distinct
16346        || !stmt.distinct_on.is_empty()
16347        || stmt.where_.is_some()
16348        || stmt.group_by.is_some()
16349        || stmt.having.is_some()
16350        || !stmt.order_by.is_empty()
16351        || stmt.limit.is_some()
16352        || stmt.offset.is_some()
16353        || stmt.items.len() != 1
16354    {
16355        return None;
16356    }
16357    let SelectItem::Expr { expr, .. } = &stmt.items[0] else {
16358        return None;
16359    };
16360    let E::FunctionCall { name, args } = expr else {
16361        return None;
16362    };
16363    if !name.eq_ignore_ascii_case("count_star") || !args.is_empty() {
16364        return None;
16365    }
16366    // Inner: exactly one item, and it is unnest(ARRAY[...]).
16367    if inner.items.len() != 1
16368        || !inner.order_by.is_empty()
16369        || inner.limit.is_some()
16370        || inner.offset.is_some()
16371    {
16372        return None;
16373    }
16374    let SelectItem::Expr { expr: item, .. } = &inner.items[0] else {
16375        return None;
16376    };
16377    let E::FunctionCall {
16378        name: fname,
16379        args: fargs,
16380    } = item
16381    else {
16382        return None;
16383    };
16384    if !fname.eq_ignore_ascii_case("unnest") || fargs.len() != 1 {
16385        return None;
16386    }
16387    let E::Array(elems) = &fargs[0] else {
16388        return None;
16389    };
16390    if elems.is_empty() || elems.iter().any(crate::expr_has_subquery) {
16391        return None;
16392    }
16393    let k = elems.len() as i64;
16394    // The stripped inner (the SRF item replaced by a plain constant)
16395    // must be the provable simple shape.
16396    let mut counted = inner.clone();
16397    counted.items = alloc::vec![SelectItem::Expr {
16398        expr: E::Literal(spg_sql::ast::Literal::Integer(1)),
16399        alias: None,
16400    }];
16401    let base = matview_flatten_probe(&counted)?;
16402    let mut out = stmt.clone();
16403    out.items = alloc::vec![SelectItem::Expr {
16404        expr: E::Binary {
16405            lhs: alloc::boxed::Box::new(E::FunctionCall {
16406                name: String::from("count_star"),
16407                args: alloc::vec![],
16408            }),
16409            op: spg_sql::ast::BinOp::Mul,
16410            rhs: alloc::boxed::Box::new(E::Literal(spg_sql::ast::Literal::Integer(k))),
16411        },
16412        alias: Some(String::from("count")),
16413    }];
16414    out.from = Some(spg_sql::ast::FromClause {
16415        primary: base,
16416        joins: Vec::new(),
16417    });
16418    out.where_ = counted.where_.clone();
16419    Some(out)
16420}