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

Function try_plan_derived_from

nodedb-sql/src/planner/select/select_stmt.rs:373–426  ·  view source on GitHub ↗

Desugar `FROM (SELECT ...) AS alias` into a synthetic single-CTE plan. Recognises the single-source, non-LATERAL derived-table pattern. The inner subquery is planned with the original catalog; the outer SELECT is replanned with a `CteCatalog` that resolves the alias to a schemaless source. The result is wrapped as `SqlPlan::Cte` so the `convert_cte` lowering takes care of execution. Returns `Ok(

(
    select: &Select,
    catalog: &dyn SqlCatalog,
    functions: &FunctionRegistry,
    temporal: TemporalScope,
)

Source from the content-addressed store, hash-verified

371/// Returns `Ok(None)` when the FROM clause is not a single derived
372/// table, so the caller falls through to the regular planning path.
373fn try_plan_derived_from(
374 select: &Select,
375 catalog: &dyn SqlCatalog,
376 functions: &FunctionRegistry,
377 temporal: TemporalScope,
378) -> Result<Option<SqlPlan>> {
379 if select.from.len() != 1 {
380 return Ok(None);
381 }
382 let from = &select.from[0];
383 if !from.joins.is_empty() {
384 return Ok(None);
385 }
386 let (subquery, alias_ident) = match &from.relation {
387 ast::TableFactor::Derived {
388 lateral: false,
389 subquery,
390 alias: Some(alias),
391 ..
392 } => (subquery, alias),
393 _ => return Ok(None),
394 };
395
396 let alias_name = normalize_ident(&alias_ident.name);
397 let inner_plan = plan_query(subquery, catalog, functions, temporal)?;
398
399 // Replan the outer SELECT against a catalog that resolves the alias
400 // as a schemaless source. The outer can reference `alias.col`
401 // qualified or unqualified — the resolver treats CTE rows as a
402 // schemaless document so any projected column flows through.
403 let derived_catalog = CteCatalog {
404 inner: catalog,
405 cte_names: vec![alias_name.clone()],
406 };
407 let mut outer_select = select.clone();
408 outer_select.from[0].relation = ast::TableFactor::Table {
409 name: ast::ObjectName::from(vec![ast::Ident::new(alias_name.clone())]),
410 alias: None,
411 args: None,
412 with_hints: Vec::new(),
413 version: None,
414 with_ordinality: false,
415 partitions: Vec::new(),
416 json_path: None,
417 sample: None,
418 index_hints: Vec::new(),
419 };
420 let outer_plan = plan_select(&outer_select, &derived_catalog, functions, temporal)?;
421
422 Ok(Some(SqlPlan::Cte {
423 definitions: vec![(alias_name, inner_plan)],
424 outer: Box::new(outer_plan),
425 }))
426}
427
428/// Dispatch to the JOIN planner if the FROM contains joins.
429fn try_plan_join(

Callers 1

plan_selectFunction · 0.85

Calls 6

normalize_identFunction · 0.85
plan_queryFunction · 0.85
plan_selectFunction · 0.85
lenMethod · 0.45
is_emptyMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected