Skip to main content

IVF-Flat

SQL function: cuvs_ivf_flat

Official cuVS reference: C API

Query-local inverted-file approximate nearest-neighbor search.

Quickstart

The call below expects the registered relations dataset_vectors (the dataset role) and query_vectors (the queries role), each passed as a parenthesized SELECT subquery. Substitute your own relations and column names.

SELECT *
FROM cuvs_ivf_flat(
dataset => (SELECT id, d0, d1 FROM dataset_vectors),
queries => (SELECT id, d0, d1 FROM query_vectors),
k => 8,
metric => 'l2_expanded',
n_lists => 16,
n_probes => 16
)
ORDER BY query_ordinal, rank;

Inputs

Each relation argument is a parenthesized SELECT subquery that the planner keeps as a real child; metadata validation resolves a registered table or view for the same role instead. See Vector Inputs for the relation identity rules and the ID, dense-vector type, null, finite-value, and runtime-dimension contract.

RoleRequiredValidation referenceDescription
datasetyestableDense-vector rows indexed for nearest-neighbor search.
queriesyestableDense-vector query rows matched against the evaluated dataset.

Vector element types

Element typeValid metrics
Float32l2_expanded, l2_sqrt_expanded, cosine, inner_product
Int8l2_expanded, l2_sqrt_expanded, cosine, inner_product
UInt8l2_expanded, l2_sqrt_expanded, cosine, inner_product

Arguments and options

Scalar SQL arguments

ArgumentTypeRequiredDescription
kintegeryesNumber of neighbors returned for each evaluated query row.
metricenum ("l2_expanded", "l2_sqrt_expanded", "cosine", "inner_product")noDistance or score metric; native default l2_expanded. Distance metrics rank lower values first; inner product ranks higher values first.
n_listsintegeryesNumber of inverted lists; must not exceed the evaluated dataset row count.
n_probesintegernoLists probed per query; native default 20, and must not exceed n_lists.
kmeans_n_itersintegernoIterations for training list centers; native default 20.
kmeans_trainset_fractionnumbernoFraction of the dataset used for training centers; native default 0.5.

SQL value argument schemas

ArgumentRequiredLiteral shapeDefaultConstraintsDescription
kyesintegerNo defaultminimum 1; maximum 4294967295Number of neighbors returned for each evaluated query row.
kmeans_n_itersnointeger20minimum 1; maximum 4294967295Iterations for training list centers; native default 20.
kmeans_trainset_fractionnonumber0.5greater than 0; maximum 1Fraction of the dataset used for training centers; native default 0.5.
metricnostring"l2_expanded"one of "l2_expanded", "l2_sqrt_expanded", "cosine", "inner_product"Distance or score metric; native default l2_expanded. Distance metrics rank lower values first; inner product ranks higher values first. Supported element/metric combinations: Float32: l2_expanded, l2_sqrt_expanded, cosine, inner_product; Int8: l2_expanded, l2_sqrt_expanded, cosine, inner_product; UInt8: l2_expanded, l2_sqrt_expanded, cosine, inner_product.
n_listsyesintegerNo defaultminimum 1; maximum 4294967295Number of inverted lists; must not exceed the evaluated dataset row count.
n_probesnointeger20minimum 1; maximum 4294967295Lists probed per query; native default 20, and must not exceed n_lists.

Vector binding shapes

Each relation subquery must project a non-null id field followed by either one or more non-null feature fields of a supported element type (Float32, Int8, UInt8) or one non-null list vector field named vector.

For wide vectors, the projection order defines the feature dimensions. A list vector relation must contain no feature field beside id and vector.

Output

ColumnTypeNullableDescription
query_ordinalUInt64noZero-based ordinal of the evaluated query row; it disambiguates duplicate query IDs.
query_idsame_as_queries.idnoLogical ID copied from the queries relation.
neighbor_ordinalUInt64noZero-based ordinal of the matched dataset row; it disambiguates duplicate dataset IDs.
neighbor_idsame_as_dataset.idnoLogical ID copied from the matched dataset row.
rankUInt32noOne-based neighbor rank within a query. Order consumers explicitly by query_ordinal, rank.
distanceFloat32noMetric value; smaller is better for distance metrics, while inner_product prefers larger values.

Concrete schemas are call-specific. Run gpu_validate_call against registered relations to inspect the output schema after the actual ID types and literal options are validated.

Limits

  • Validation resolves named tables or views and reads schemas only; it does not execute relation scans or GPU work.
  • Execution relation arguments require parenthesized subqueries; dry-run validation accepts registered named relations only.
  • Builds and destroys its query-local index in the statement; no index persists across statements.
  • Results are approximate unless all lists are probed. Fewer than k neighbors may be returned for a query; missing (query, rank) rows are omitted and valid ranks remain contiguous.
  • The evaluated dataset must be non-empty, k and n_lists must not exceed its row count, and n_probes must not exceed n_lists.
  • Dataset and query dimensions and element types must match. Cosine requires at least two dimensions.

To dry-run validate relation metadata, column types, and options without execution, see gpu_validate_call.