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

Function plan_lateral_top_k

nodedb-sql/src/planner/lateral/plan.rs:131–186  ·  view source on GitHub ↗
(
    outer_plan: SqlPlan,
    outer_alias: Option<String>,
    select: &sqlparser::ast::Select,
    subquery: &ast::Query,
    equi_keys: Vec<super::correlation::CorrelationEq>,
    inner_limit: usiz

Source from the content-addressed store, hash-verified

129/// Plan the `LateralTopK` variant: equi-correlated + ORDER BY + LIMIT k.
130#[allow(clippy::too_many_arguments)]
131fn plan_lateral_top_k(
132 outer_plan: SqlPlan,
133 outer_alias: Option<String>,
134 select: &sqlparser::ast::Select,
135 subquery: &ast::Query,
136 equi_keys: Vec<super::correlation::CorrelationEq>,
137 inner_limit: usize,
138 lateral_alias: &str,
139 left_join: bool,
140 outer_projection: Vec<Projection>,
141) -> Result<SqlPlan> {
142 // Build a bare inner Scan without correlation filters (those are injected
143 // at runtime per outer row).
144 let inner_collection = extract_inner_collection(select)?;
145 let inner_filters = inner_non_correlated_filters(select, outer_alias.as_deref().unwrap_or(""))?;
146
147 // Extract ORDER BY from the inner subquery.
148 // For LATERAL inner scans we only need simple column-expression sort keys;
149 // the full search-trigger machinery (vector/hybrid search) is not applicable
150 // here, so we convert expressions directly.
151 let inner_order_by = if let Some(order_by) = &subquery.order_by {
152 match &order_by.kind {
153 ast::OrderByKind::Expressions(exprs) => exprs
154 .iter()
155 .filter_map(|o| {
156 convert_expr(&o.expr).ok().map(|expr| SortKey {
157 expr,
158 ascending: o.options.asc.unwrap_or(true),
159 nulls_first: o.options.nulls_first.unwrap_or(false),
160 })
161 })
162 .collect(),
163 ast::OrderByKind::All(_) => Vec::new(),
164 }
165 } else {
166 Vec::new()
167 };
168
169 let correlation_keys: Vec<(String, String)> = equi_keys
170 .into_iter()
171 .map(|c| (c.outer_col, c.inner_col))
172 .collect();
173
174 Ok(SqlPlan::LateralTopK {
175 outer: Box::new(outer_plan),
176 outer_alias,
177 inner_collection,
178 inner_filters,
179 inner_order_by,
180 inner_limit,
181 correlation_keys,
182 lateral_alias: lateral_alias.to_string(),
183 projection: outer_projection,
184 left_join,
185 })
186}
187
188/// Extract the collection name from a single-table inner SELECT.

Callers 1

plan_lateral_joinFunction · 0.85

Calls 7

extract_inner_collectionFunction · 0.85
convert_exprFunction · 0.85
collectMethod · 0.80
to_stringMethod · 0.80
iterMethod · 0.45
okMethod · 0.45

Tested by

no test coverage detected