Extract aggregate expressions from SELECT projection.
(
items: &[ast::SelectItem],
functions: &FunctionRegistry,
)
| 353 | |
| 354 | /// Extract aggregate expressions from SELECT projection. |
| 355 | pub 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)] |
| 381 | mod tests { |
no test coverage detected