Extract a float from a function argument expression.
(expr: &ast::Expr, option: &str)
| 246 | |
| 247 | /// Extract a float from a function argument expression. |
| 248 | fn extract_f64(expr: &ast::Expr, option: &str) -> Result<f64> { |
| 249 | match expr { |
| 250 | ast::Expr::Value(v) => match &v.value { |
| 251 | ast::Value::Number(n, _) => n.parse::<f64>().map_err(|_| SqlError::Unsupported { |
| 252 | detail: format!("ANN option '{option}' expects a number, got: {n}"), |
| 253 | }), |
| 254 | _ => Err(SqlError::Unsupported { |
| 255 | detail: format!("ANN option '{option}' expects a number, got: {expr}"), |
| 256 | }), |
| 257 | }, |
| 258 | // Handle negative floats: -0.5 parses as UnaryOp { Minus, 0.5 } |
| 259 | ast::Expr::UnaryOp { |
| 260 | op: ast::UnaryOperator::Minus, |
| 261 | expr: inner, |
| 262 | } => extract_f64(inner, option).map(|f| -f), |
| 263 | _ => Err(SqlError::Unsupported { |
| 264 | detail: format!("ANN option '{option}' expects a number, got: {expr}"), |
| 265 | }), |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | #[cfg(test)] |
| 270 | mod tests { |