Skip to main content

Vector Top-K

SQL function: cuvs_top_k

Official cuVS reference: C++ API

Select the smallest or largest values in every dense vector row.

Quickstart

The call below expects the registered relation input_vectors (the input role), passed as a parenthesized SELECT subquery. Substitute your own relations and column names.

SELECT *
FROM cuvs_top_k(
input => (SELECT id, d0, d1 FROM input_vectors),
k => 1,
select => 'max'
)
ORDER BY row_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
inputyestableDense-vector rows consumed by this operation.

Vector element types

Element typeValid metrics
Float32Not applicable

Arguments and options

Scalar SQL arguments

ArgumentTypeRequiredDescription
kintegeryesNumber of values selected from each row.
selectenum ("min", "max")yesSelect the minimum or maximum values.

SQL value argument schemas

ArgumentRequiredLiteral shapeDefaultConstraintsDescription
kyesintegerNo defaultminimum 1; maximum 4294967295Number of values selected from each row.
selectyesstringNo defaultone of "min", "max"Select the minimum or maximum values.

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) 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
row_ordinalUInt64noZero-based physical ordinal of the evaluated input row; it disambiguates duplicate input IDs.
idsame_as_input.idnoLogical ID copied from the input relation.
rankUInt32noOne-based value rank within an input row. Order consumers explicitly by row_ordinal, rank.
positionInt64noZero-based dimension index of the selected value within the input vector.
valueFloat32noSelected Float32 input vector value; rank follows ascending values for min and descending values for max.

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.
  • k must not exceed the vector dimension. Empty input returns an empty result with the stable schema.
  • Equal selected values are ordered by position; when ties straddle rank k, the selected positions among them are unspecified.

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