## Code Analysis ### Correlated subqueries become per-row SubPlans `crates/sql/src/optimizer/subselect.rs:68-83` ```rust pub(crate) fn make_subplan(ctx: &mut Ctx<'_>, mut link: Expr, planned: bool, width: usize) -> Expr { let plan = link.subquery_mut().expect("a subquery expression"); let args = replace_correlation_vars(plan, width); if planned { let plan = std::mem::replace(plan, Plan::OneRow); return Expr::SubPlan(Box::new(build_subplan(ctx, link, plan, None, args))); } let subplan = SubPlan { link, args, init_plan: false, planned: false, startup_cost: 0.0, per_call_cost: 0.0 }; match ctx.defer_subplans { true => Expr::SubPlan(Box::new(subplan)), false => plan_subplan(ctx, subplan), } } ``` `replace_correlation_vars` supplies the enclosing-row expressions as `SubPlan` arguments. This is the production path relevant to a correlated subquery rather than a fixture-only or mock path. ### ANY expressions are included in the same planning path `crates/sql/src/optimizer/subselect.rs:328-337` ```rust pub fn process_sublinks(ctx: &mut Ctx<'_>, e: Expr, width: usize) -> Expr { match e { link @ (Expr::Exists(_) | Expr::Scalar(_) | Expr::ArraySubquery(..) | Expr::AnySubquery(..)) => { let link = link.map_children(&mut |c| process_sublinks(ctx, c, width)); make_subplan(ctx, link, false, width) } other => other.map_children(&mut |c| process_sublinks(ctx, c, width)), } } ``` The match explicitly sends `Expr::AnySubquery` through `make_subplan`, so the correlated quantified form is handled by the new SubPlan planning route. ### SubPlan evaluates arguments for the current row `crates/sql/src/expr.rs:132-152` ```rust pub struct SubPlan { pub link: Expr, pub args: Vec, pub init_plan: bool, pub planned: bool, pub startup_cost: f64, pub per_call_cost: f64, } fn eval(&self, ctx: &mut Ctx<'_>, row: &[Value]) -> Result { let args = self.args.iter().map(|a| a.eval(ctx, row)).collect::>>()?; self.link.eval_sublink(ctx, row, &args) } ``` This implementation evaluates the saved arguments against the enclosing row before evaluating the linked subquery, matching the required per-row correlation model. ### Observed execution #### triggering query The runner invoked `psql` against the local PostgreSQL-compatible SQL service with the following query (the password and container identifier are omitted): ```sql SELECT o.id, o.k, EXISTS (SELECT 1 FROM sub_inner i WHERE i.k = o.k) AS has_match, (SELECT i.value FROM sub_inner i WHERE i.k = o.k ORDER BY i.id LIMIT 1) AS first_value, o.k = ANY (SELECT i.k FROM sub_inner i WHERE i.k >= o.k) AS quantified_correlated, o.k IN (SELECT i.k FROM sub_inner i) AS independent_in, (SELECT count(*) FROM sub_inner WHERE k = 20) AS independent_count FROM sub_outer o ORDER BY o.id; ``` The runner recorded this result for the combined query: ```text expression.subqueryAnyExpr: expected right child to return 3 values but returned 2 ``` The captured `correlated-independent.txt` response body was empty, so this error is retained as the runner's recorded assertion output rather than represented as a copied SQL response table. #### independent and non-quantified readback The isolated follow-up query produced these captured rows: ```text id | has_match | first_value ----+-----------+------------ 1 | t | 100 2 | t | 200 3 | f | 4 | f | (4 rows) id | independent_in | independent_count ----+-----------------+------------------ 1 | t | 2 2 | t | 2 3 | | 2 4 | f | 2 (4 rows) ``` The same captured follow-up `EXPLAIN` shows indexed access to `sub_inner.k` and a cacheable independent subquery. These results support that the fixtures and neighboring correlated/independent forms executed, while the combined correlated `ANY` form failed during planning. ### Result The captured run supports a planner failure specific to the correlated quantified `ANY` expression: it reports a result-shape error before returning rows, while the isolated correlated `EXISTS`/scalar and independent `IN`/count forms return data. The checked-out source routes `AnySubquery` through the per-row `SubPlan` machinery, providing the production code path for the reported failure. ### Test context The browser navigation to the SQL port returned `ERR_EMPTY_RESPONSE` because the target is a PostgreSQL-compatible service rather than an HTTP route. The SQL verification then used a local nested Doltgres session; no stubs, mocks, or bypasses were recorded.