Extract a float array from ARRAY[...], make_array(...), or a JSON-array string literal like `'[1.0, 0.5, 0.0]'`.
(expr: &ast::Expr)
| 205 | /// Extract a float array from ARRAY[...], make_array(...), or a JSON-array |
| 206 | /// string literal like `'[1.0, 0.5, 0.0]'`. |
| 207 | pub(super) fn extract_float_array(expr: &ast::Expr) -> Result<Vec<f32>> { |
| 208 | match expr { |
| 209 | ast::Expr::Array(ast::Array { elem, .. }) => elem |
| 210 | .iter() |
| 211 | .map(|e| extract_float(e).map(|f| f as f32)) |
| 212 | .collect(), |
| 213 | ast::Expr::Function(func) => { |
| 214 | let name = func |
| 215 | .name |
| 216 | .0 |
| 217 | .iter() |
| 218 | .map(|p| match p { |
| 219 | ast::ObjectNamePart::Identifier(ident) => normalize_ident(ident), |
| 220 | _ => String::new(), |
| 221 | }) |
| 222 | .collect::<Vec<_>>() |
| 223 | .join("."); |
| 224 | if name == "make_array" || name == "array" { |
| 225 | let args = extract_func_args(func)?; |
| 226 | args.iter() |
| 227 | .map(|e| extract_float(e).map(|f| f as f32)) |
| 228 | .collect() |
| 229 | } else { |
| 230 | Err(SqlError::Unsupported { |
| 231 | detail: format!("expected array, got function: {name}"), |
| 232 | }) |
| 233 | } |
| 234 | } |
| 235 | // Accept JSON-array string literals: `'[1.0, 0.5, 0.0]'`. |
| 236 | // This is the canonical pgvector-compatible form for embedding vectors |
| 237 | // passed as SQL string literals. |
| 238 | ast::Expr::Value(v) => { |
| 239 | let s = match &v.value { |
| 240 | sqlparser::ast::Value::SingleQuotedString(s) => s.as_str(), |
| 241 | sqlparser::ast::Value::DoubleQuotedString(s) => s.as_str(), |
| 242 | _ => { |
| 243 | return Err(SqlError::Unsupported { |
| 244 | detail: format!("expected array literal, got: {expr}"), |
| 245 | }); |
| 246 | } |
| 247 | }; |
| 248 | let trimmed = s.trim(); |
| 249 | if !trimmed.starts_with('[') || !trimmed.ends_with(']') { |
| 250 | return Err(SqlError::Unsupported { |
| 251 | detail: format!("expected JSON array string, got: {s:?}"), |
| 252 | }); |
| 253 | } |
| 254 | let inner = &trimmed[1..trimmed.len() - 1]; |
| 255 | if inner.trim().is_empty() { |
| 256 | return Ok(Vec::new()); |
| 257 | } |
| 258 | inner |
| 259 | .split(',') |
| 260 | .map(|part| { |
| 261 | part.trim() |
| 262 | .parse::<f32>() |
| 263 | .map_err(|_| SqlError::Unsupported { |
| 264 | detail: format!("cannot parse float from array element: {part:?}"), |
no test coverage detected