Resolve the canonical index for a `GROUPING(col)` argument. Matches by display string against `canonical_names`.
(col_expr: &ast::Expr, canonical_keys: &[SqlExpr])
| 184 | /// |
| 185 | /// Matches by display string against `canonical_names`. |
| 186 | pub fn resolve_grouping_col(col_expr: &ast::Expr, canonical_keys: &[SqlExpr]) -> Result<usize> { |
| 187 | let display = format!("{col_expr}"); |
| 188 | // Find by rebuilding the display of each canonical key via its SqlExpr. |
| 189 | // Since canonical_keys are built from the same AST exprs, we can compare |
| 190 | // their SqlExpr display strings. |
| 191 | for (i, key) in canonical_keys.iter().enumerate() { |
| 192 | if format!("{key:?}").contains(&display) { |
| 193 | return Ok(i); |
| 194 | } |
| 195 | } |
| 196 | // Fallback: try normalized ident match. |
| 197 | if let ast::Expr::Identifier(ident) = col_expr { |
| 198 | let name = normalize_ident(ident); |
| 199 | for (i, key) in canonical_keys.iter().enumerate() { |
| 200 | if let SqlExpr::Column { name: col_name, .. } = key |
| 201 | && col_name.eq_ignore_ascii_case(&name) |
| 202 | { |
| 203 | return Ok(i); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | Err(SqlError::Unsupported { |
| 208 | detail: format!( |
| 209 | "GROUPING({col_expr}) references a column not found in the canonical key list" |
| 210 | ), |
| 211 | }) |
| 212 | } |
| 213 | |
| 214 | #[cfg(test)] |
| 215 | mod tests { |
no test coverage detected