MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / try_plan_scalar_subquery

Function try_plan_scalar_subquery

nodedb-sql/src/planner/subquery.rs:365–390  ·  view source on GitHub ↗

Plan a scalar subquery (e.g., `(SELECT AVG(amount) FROM orders)`). Rewrites `col > (SELECT AVG(amount) FROM orders)` as: cross-join with the aggregate result (1 row), then filter `col > result_col`. The cross-join produces a cartesian product, but since the aggregate returns exactly 1 row, every outer row gets paired with that single result row.

(
    subquery: &ast::Query,
    catalog: &dyn SqlCatalog,
    functions: &FunctionRegistry,
    temporal: crate::TemporalScope,
)

Source from the content-addressed store, hash-verified

363/// The cross-join produces a cartesian product, but since the aggregate returns
364/// exactly 1 row, every outer row gets paired with that single result row.
365fn try_plan_scalar_subquery(
366 subquery: &ast::Query,
367 catalog: &dyn SqlCatalog,
368 functions: &FunctionRegistry,
369 temporal: crate::TemporalScope,
370) -> Result<Option<ScalarSubqueryResult>> {
371 let inner_plan = super::select::plan_query(subquery, catalog, functions, temporal)?;
372
373 // Extract the result column name from the subquery's SELECT list.
374 let result_col = match extract_scalar_column(subquery) {
375 Some(col) => col,
376 None => return Ok(None),
377 };
378
379 let replacement = Expr::Identifier(ast::Ident::new(&result_col));
380
381 Ok(Some(ScalarSubqueryResult {
382 join: SubqueryJoin {
383 outer_column: String::new(),
384 inner_plan,
385 inner_column: String::new(),
386 join_type: JoinType::Cross,
387 },
388 replacement_expr: replacement,
389 }))
390}
391
392/// Extract the projected column name from a scalar subquery.
393///

Callers 1

extract_recursiveFunction · 0.85

Calls 2

plan_queryFunction · 0.85
extract_scalar_columnFunction · 0.85

Tested by

no test coverage detected