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

Function extract_aggregates_from_projection

nodedb-sql/src/planner/aggregate.rs:355–378  ·  view source on GitHub ↗

Extract aggregate expressions from SELECT projection.

(
    items: &[ast::SelectItem],
    functions: &FunctionRegistry,
)

Source from the content-addressed store, hash-verified

353
354/// Extract aggregate expressions from SELECT projection.
355pub fn extract_aggregates_from_projection(
356 items: &[ast::SelectItem],
357 functions: &FunctionRegistry,
358) -> Result<Vec<AggregateExpr>> {
359 let mut aggregates = Vec::new();
360 for item in items {
361 let (expr, alias): (&ast::Expr, String) = match item {
362 // Lowercase the unparsed expression text so the resulting
363 // alias (which becomes the JSON row key after the
364 // `apply_user_aliases_to_rows` rename) matches the pgwire
365 // lookup key built by `expr_column_names`, which also
366 // lowercases. Without this match the row description
367 // column `count(distinct user_id)` would not resolve to
368 // the JSON value stored under `COUNT(DISTINCT user_id)`,
369 // and the client would see NULL.
370 ast::SelectItem::UnnamedExpr(expr) => (expr, format!("{expr}").to_lowercase()),
371 ast::SelectItem::ExprWithAlias { expr, alias } => (expr, normalize_ident(alias)),
372 _ => continue,
373 };
374 let mut extracted = crate::aggregate_walk::extract_aggregates(expr, &alias, functions)?;
375 aggregates.append(&mut extracted);
376 }
377 Ok(aggregates)
378}
379
380#[cfg(test)]
381mod tests {

Callers 2

plan_aggregateFunction · 0.85
plan_join_from_selectFunction · 0.85

Calls 3

normalize_identFunction · 0.85
extract_aggregatesFunction · 0.50
appendMethod · 0.45

Tested by

no test coverage detected