Given an expression that's literal int encoding position, lookup the corresponding expression in the select_exprs list, if the index is within the bounds and it is indeed a position literal, otherwise, returns planning error. If input expression is not an int literal, returns expression as-is.
(
expr: Expr,
select_exprs: &[Expr],
)
| 207 | /// otherwise, returns planning error. |
| 208 | /// If input expression is not an int literal, returns expression as-is. |
| 209 | pub(crate) fn resolve_positions_to_exprs( |
| 210 | expr: Expr, |
| 211 | select_exprs: &[Expr], |
| 212 | ) -> Result<Expr> { |
| 213 | match expr { |
| 214 | // sql_expr_to_logical_expr maps number to i64 |
| 215 | // https://github.com/apache/datafusion/blob/8d175c759e17190980f270b5894348dc4cff9bbf/datafusion/src/sql/planner.rs#L882-L887 |
| 216 | Expr::Literal(ScalarValue::Int64(Some(position)), _) |
| 217 | if position > 0_i64 && position <= select_exprs.len() as i64 => |
| 218 | { |
| 219 | let index = (position - 1) as usize; |
| 220 | let select_expr = &select_exprs[index]; |
| 221 | Ok(match select_expr { |
| 222 | Expr::Alias(Alias { expr, .. }) => *expr.clone(), |
| 223 | _ => select_expr.clone(), |
| 224 | }) |
| 225 | } |
| 226 | Expr::Literal(ScalarValue::Int64(Some(position)), _) => plan_err!( |
| 227 | "Cannot find column with position {} in SELECT clause. Valid columns: 1 to {}", |
| 228 | position, |
| 229 | select_exprs.len() |
| 230 | ), |
| 231 | _ => Ok(expr), |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /// Rebuilds an `Expr` with columns that refer to aliases replaced by the |
| 236 | /// alias' underlying `Expr`. |
searching dependent graphs…