Class VectorIndexHelper

java.lang.Object
com.cockroachlabs.data.ddl.VectorIndexHelper

public final class VectorIndexHelper extends Object
Generates CockroachDB v26.2+ CREATE VECTOR INDEX DDL.

In CockroachDB v26.2, the legacy CREATE INDEX ... USING cspann syntax is replaced by the dedicated CREATE VECTOR INDEX statement, which supports prefix columns for pre-filtered vector searches.

Migration from v24.x / v25.x:

 -- Old (v24.x/v25.x):
 CREATE INDEX idx ON docs USING cspann (embedding vector_cosine_ops);

 -- New (v26.2+):
 CREATE VECTOR INDEX idx ON docs (embedding vector_cosine_ops);

 -- With prefix columns (v26.2+):
 CREATE VECTOR INDEX idx ON docs (tenant_id, embedding vector_cosine_ops);
 
  • Method Details

    • generateCreateVectorIndex

      public static String generateCreateVectorIndex(String indexName, String tableName, String vectorColumn, DistanceMetric metric, String... prefixColumns)
      Generate a v26.2-compatible CREATE VECTOR INDEX statement.
      Parameters:
      indexName - the index name
      tableName - the table containing the vector column
      vectorColumn - the column holding VECTOR data
      metric - the distance metric (determines the opclass)
      prefixColumns - optional prefix columns for pre-filtering (may be empty)
      Returns:
      the DDL statement
    • generateCreateCspannIndex

      public static String generateCreateCspannIndex(String indexName, String tableName, String vectorColumn, DistanceMetric metric)
      Generate the legacy v24.x/v25.x CREATE INDEX ... USING cspann statement. Provided for backward compatibility.
      Parameters:
      indexName - the index name
      tableName - the table containing the vector column
      vectorColumn - the column holding VECTOR data
      metric - the distance metric
      Returns:
      the DDL statement
    • generateDropIndex

      public static String generateDropIndex(String indexName)
      Generate a DROP INDEX IF EXISTS statement for a vector index.
      Parameters:
      indexName - the index to drop
      Returns:
      the DDL statement
    • enableVectorIndexSetting

      public static String enableVectorIndexSetting()
      Generate the cluster setting statement to enable vector indexes (v26.2+).
      Returns:
      the SET CLUSTER SETTING statement
    • setBeamSize

      public static String setBeamSize(int beamSize)
      Generate the session-level beam size setting.
      Parameters:
      beamSize - the desired beam size (must be > 0)
      Returns:
      the SET statement
      Throws:
      IllegalArgumentException - if beamSize is not positive