MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / build_vector_search_sql

Function build_vector_search_sql

nodedb-client/src/remote/sql.rs:27–42  ·  view source on GitHub ↗

Build the SQL for a vector search. Always emits the canonical `SELECT * FROM [ WHERE ] ORDER BY vector_distance(ARRAY[...]) LIMIT ` shape so the optional `WHERE` clause precedes `ORDER BY` (the SEARCH preprocessor's "trailing append" form would have placed `WHERE` after `ORDER BY`, which is invalid SQL).

(
    collection: &str,
    query: &[f32],
    k: usize,
    filter: Option<&MetadataFilter>,
)

Source from the content-addressed store, hash-verified

25/// preprocessor's "trailing append" form would have placed `WHERE` after
26/// `ORDER BY`, which is invalid SQL).
27pub(super) fn build_vector_search_sql(
28 collection: &str,
29 query: &[f32],
30 k: usize,
31 filter: Option<&MetadataFilter>,
32) -> NodeDbResult<String> {
33 let collection = quote_identifier(collection);
34 let where_clause = match filter {
35 Some(f) => format!(" WHERE {}", render_metadata_filter(f)?),
36 None => String::new(),
37 };
38 Ok(format!(
39 "SELECT * FROM {collection}{where_clause} ORDER BY vector_distance({}) LIMIT {k}",
40 format_vector_array(query),
41 ))
42}
43
44/// Crate-internal accessor so peer modules (e.g. the field-aware
45/// `vector_search_field` override in `client.rs`) can reuse the same

Calls 1

quote_identifierFunction · 0.85