Skip to main content

KMeans

SQL function: cuvs_kmeans

K-means clustering assignments for dense vector rows.

Signature

cuvs_kmeans((input relation subquery), options_json)

Quickstart

SELECT id, cluster_id
FROM cuvs_kmeans(
(SELECT item_id, d0, d1 FROM input_vectors),
'{"input":{"id":"item_id","vector":{"columns":["d0","d1"]}},"n_clusters":8}'
)
ORDER BY row_ordinal;

Relation inputs

Every execution relation argument is a parenthesized SELECT subquery. The planner retains that relation as a real logical and physical child; a bare table identifier or quoted table-name string is rejected. Metadata validation instead uses a registered named table or view in its relation envelope.

RoleRequiredValidation referenceDescription
inputyestableDense-vector rows consumed by the fit-and-transform operation.

See Vector Inputs for the ID and dense-vector type, null, finite-value, and runtime-dimension contract.

Scalar arguments & JSON options

Scalar SQL arguments

ArgumentTypeRequiredDescription
options_jsonJSON string literalyescuVS operation options and relation-column bindings

JSON options

OptionRequiredJSON shapeDefaultConstraintsDescription
initnostring"kmeans++"one of "kmeans++", "random"Centroid initialization strategy for this fitted model.
inputyesobjectNames the input ID column and one dense-vector binding shape.
max_iternointeger100minimum 1; maximum 2147483647Maximum fitting iterations for this invocation.
metricnostring"l2_expanded"one of "l2_expanded", "l2_sqrt_expanded"L2 distance form used while fitting and assigning the current input relation.
n_clustersyesintegerminimum 1; maximum 2147483647Number of clusters fitted for this one statement.
n_initnointeger1minimum 1; maximum 2147483647Number of initialization attempts performed within this call.
tolnonumber0.0001minimum 0Non-negative convergence tolerance used by the fitted model.

Vector binding shapes

id: Non-null logical row ID column. IDs may repeat; result ordinals disambiguate physical rows.

ShapeJSONContract
Wide Float32 columns{"vector":{"columns":["d0","d1"]}}Ordered, unique non-null Float32 feature columns; order defines vector dimensions.
List column{"vector":{"column":"embedding"}}One non-null FixedSizeList<Float32, D>, List<Float32>, or LargeList<Float32> column.

Choose exactly one dense-vector binding shape.

Output schema

ColumnTypeNullableDescription
row_ordinalUInt64noZero-based ordinal of the evaluated input row; it disambiguates duplicate IDs.
idsame_as_input.idnoLogical ID copied from the input relation.
cluster_idInt32noQuery-local numeric assignment label, not a stable business or topic identifier.

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.

Limitations & lifecycle

  • Validation resolves named tables or views and reads schemas only; it does not execute relation scans or GPU work.
  • Bounded cuVS execution is unavailable until the lower-level peak-memory preflight contract exists.
  • Execution relation arguments require parenthesized subqueries; dry-run validation accepts registered named relations only.
  • Fits a model and returns assignments within one statement; it does not return a reusable model, centroids, or inertia.
  • The evaluated input must be non-empty and n_clusters must not exceed its row count.
  • cluster_id values are query-local labels. Do not attach permanent business meaning to their numeric values.

Validate before running

Validation checks registered relation metadata, bindings, dtypes, and options without scanning rows or touching the GPU:

SELECT * FROM gpu_validate_call(
'cuvs_kmeans',
'{"options":{"input":{"id":"item_id","vector":{"columns":["d0","d1"]}},"n_clusters":8},"relations":{"input":{"table":"input_vectors"}},"schema_version":1}'
);

See GPU Function Catalog API for the full gpu_validate_call contract.