Skip to main content

parse_vector_literal

Function parse_vector_literal 

Source
pub fn parse_vector_literal(s: &str) -> Result<Vec<f32>>
Expand description

Parse a bracket-array literal like "[0.1, 0.2, 0.3]" (or "[1, 2, 3]") into a Vec<f32>. The parser/insert pipeline stores vector literals as strings in InsertQuery::rows (a Vec<Vec<String>>); this helper is the inverse — turn the string back into a typed vector at the boundary where we actually need element-typed data.

Accepts:

  • [] → empty vector (caller’s dimension check rejects it for VECTOR(N≥1))
  • [0.1, 0.2, 0.3] → standard float syntax
  • [1, 2, 3] → integers, coerced to f32 (matches VALUES (1, 2) for REAL columns; we widen ints to floats automatically)
  • whitespace tolerated everywhere (Python/JSON/pgvector convention)

Rejects with a descriptive message:

  • missing [ / ]
  • non-numeric elements (['foo', 0.1])
  • NaN / Inf literals (we accept them via f32::from_str but caller can reject if undesired — for now we let them through; HNSW etc. will reject NaN at index time)