-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create index command implementation (#1526)
- Loading branch information
1 parent
b7fdd8a
commit b1147b9
Showing
24 changed files
with
1,040 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
src/main/java/io/stargate/sgv2/jsonapi/api/model/command/impl/AddIndexCommand.java
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
src/main/java/io/stargate/sgv2/jsonapi/api/model/command/impl/CreateIndexCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package io.stargate.sgv2.jsonapi.api.model.command.impl; | ||
|
||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import io.stargate.sgv2.jsonapi.api.model.command.CollectionCommand; | ||
import io.stargate.sgv2.jsonapi.service.schema.SimilarityFunction; | ||
import jakarta.annotation.Nullable; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Pattern; | ||
import jakarta.validation.constraints.Size; | ||
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType; | ||
import org.eclipse.microprofile.openapi.annotations.media.Schema; | ||
|
||
// TODO, hide table feature detail before it goes public | ||
@Schema(description = "Command that creates an index for a column in a table.") | ||
@JsonTypeName("createIndex") | ||
public record CreateIndexCommand( | ||
@NotNull | ||
@Size(min = 1, max = 48) | ||
@Pattern(regexp = "[a-zA-Z][a-zA-Z0-9_]*") | ||
@Schema(description = "Name of the column to create the index on") | ||
String name, | ||
@NotNull | ||
@Schema( | ||
description = "Definition for created index for a column.", | ||
type = SchemaType.OBJECT) | ||
Definition definition, | ||
@Nullable @Schema(description = "Creating index command option.", type = SchemaType.OBJECT) | ||
Options options) | ||
implements CollectionCommand { | ||
public record Definition( | ||
@NotNull | ||
@Size(min = 1, max = 48) | ||
@Pattern(regexp = "[a-zA-Z][a-zA-Z0-9_]*") | ||
@Schema(description = "Name of the column for which index to be created.") | ||
String column, | ||
@Nullable @Schema(description = "Different indexing options.", type = SchemaType.OBJECT) | ||
Options options) { | ||
// This is index definition options for text and vector column types. | ||
public record Options( | ||
@Nullable | ||
@Schema( | ||
description = "Ignore case in matching string values.", | ||
defaultValue = "true", | ||
type = SchemaType.BOOLEAN, | ||
implementation = Boolean.class) | ||
Boolean caseSensitive, | ||
@Nullable | ||
@Schema( | ||
description = "When set to true, perform Unicode normalization on indexed strings.", | ||
defaultValue = "false", | ||
type = SchemaType.BOOLEAN, | ||
implementation = Boolean.class) | ||
Boolean normalize, | ||
@Nullable | ||
@Schema( | ||
description = | ||
"When set to true, index will converts alphabetic, numeric, and symbolic characters to the ascii equivalent, if one exists.", | ||
defaultValue = "false", | ||
type = SchemaType.BOOLEAN, | ||
implementation = Boolean.class) | ||
Boolean ascii, | ||
@Nullable | ||
@Pattern( | ||
regexp = "(dot_product|cosine|euclidean)", | ||
message = "function name can only be 'dot_product', 'cosine' or 'euclidean'") | ||
@Schema( | ||
description = | ||
"Similarity function algorithm that needs to be used for vector search", | ||
defaultValue = "cosine", | ||
type = SchemaType.STRING, | ||
implementation = String.class) | ||
SimilarityFunction metric, | ||
@Nullable | ||
@Size(min = 1, max = 48) | ||
@Pattern(regexp = "[a-zA-Z][a-zA-Z0-9_]*") | ||
@Schema(description = "Model name used to generate the embeddings.") | ||
String sourceModel) {} | ||
} | ||
|
||
// This is index command option irrespective of column definition. | ||
public record Options( | ||
@Schema( | ||
description = "Flag to ignore if index already exists", | ||
defaultValue = "false", | ||
type = SchemaType.BOOLEAN, | ||
implementation = Boolean.class) | ||
Boolean ifNotExists) {} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public CommandName commandName() { | ||
return CommandName.CREATE_INDEX; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/io/stargate/sgv2/jsonapi/config/constants/VectorConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.stargate.sgv2.jsonapi.config.constants; | ||
|
||
import io.smallrye.config.ConfigMapping; | ||
import java.util.Set; | ||
|
||
@ConfigMapping(prefix = "stargate.jsonapi.vector") | ||
public interface VectorConstant { | ||
/* | ||
Supported Source Models for Vector Index in Cassandra | ||
*/ | ||
Set<String> SUPPORTED_SOURCES = | ||
Set.of( | ||
"ada002", "openai_v3_small", "openai_v3_large", "bert", "gecko", "nv_qa_4", "cohere_v3"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/io/stargate/sgv2/jsonapi/service/cqldriver/override/ExtendedCreateIndex.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package io.stargate.sgv2.jsonapi.service.cqldriver.override; | ||
|
||
import com.datastax.oss.driver.api.core.CqlIdentifier; | ||
import com.datastax.oss.driver.internal.querybuilder.CqlHelper; | ||
import com.datastax.oss.driver.internal.querybuilder.schema.DefaultCreateIndex; | ||
import com.datastax.oss.driver.internal.querybuilder.schema.OptionsUtils; | ||
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap; | ||
import com.datastax.oss.driver.shaded.guava.common.collect.UnmodifiableIterator; | ||
import java.util.Map; | ||
|
||
/** | ||
* An extension of the {@link DefaultCreateIndex} class, This is needed because the column name | ||
* appended to the builder needs to use `asCql(true)` to keep the quotes. | ||
*/ | ||
public class ExtendedCreateIndex extends DefaultCreateIndex { | ||
|
||
public ExtendedCreateIndex(DefaultCreateIndex defaultCreateIndex) { | ||
|
||
super( | ||
defaultCreateIndex.getIndex(), | ||
defaultCreateIndex.isIfNotExists(), | ||
defaultCreateIndex.getKeyspace(), | ||
defaultCreateIndex.getTable(), | ||
defaultCreateIndex.getColumnToIndexType(), | ||
defaultCreateIndex.getUsingClass(), | ||
// This is fine as the internal options object is ImmutableMap | ||
(ImmutableMap<String, Object>) defaultCreateIndex.getOptions()); | ||
} | ||
|
||
@Override | ||
public String asCql() { | ||
StringBuilder builder = new StringBuilder("CREATE "); | ||
if (this.getUsingClass() != null) { | ||
builder.append("CUSTOM "); | ||
} | ||
|
||
builder.append("INDEX"); | ||
if (this.isIfNotExists()) { | ||
builder.append(" IF NOT EXISTS"); | ||
} | ||
|
||
if (this.getIndex() != null) { | ||
builder.append(' ').append(this.getIndex().asCql(true)); | ||
} | ||
|
||
if (this.getTable() == null) { | ||
return builder.toString(); | ||
} else { | ||
builder.append(" ON "); | ||
CqlHelper.qualify(this.getKeyspace(), this.getTable(), builder); | ||
if (this.getColumnToIndexType().isEmpty()) { | ||
return builder.toString(); | ||
} else { | ||
builder.append(" ("); | ||
boolean firstColumn = true; | ||
UnmodifiableIterator var3 = this.getColumnToIndexType().entrySet().iterator(); | ||
|
||
while (var3.hasNext()) { | ||
Map.Entry<CqlIdentifier, String> entry = (Map.Entry) var3.next(); | ||
if (firstColumn) { | ||
firstColumn = false; | ||
} else { | ||
builder.append(","); | ||
} | ||
|
||
if (((String) entry.getValue()).equals("__NO_INDEX_TYPE")) { | ||
builder.append(entry.getKey().asCql(true)); | ||
} else { | ||
builder | ||
.append((String) entry.getValue()) | ||
.append("(") | ||
.append(entry.getKey().asCql(true)) | ||
.append(")"); | ||
} | ||
} | ||
|
||
builder.append(")"); | ||
if (this.getUsingClass() != null) { | ||
builder.append(" USING '").append(this.getUsingClass()).append('\''); | ||
} | ||
|
||
if (!this.getOptions().isEmpty()) { | ||
builder.append(OptionsUtils.buildOptions(this.getOptions(), true)); | ||
} | ||
|
||
return builder.toString(); | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/io/stargate/sgv2/jsonapi/service/cqldriver/override/ExtendedVectorType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.stargate.sgv2.jsonapi.service.cqldriver.override; | ||
|
||
import com.datastax.oss.driver.api.core.type.DataType; | ||
import com.datastax.oss.driver.internal.core.type.DefaultVectorType; | ||
|
||
/** | ||
* Extended vector type to support vector size This is needed because java drivers | ||
* DataTypes.vectorOf() method has a bug | ||
*/ | ||
public class ExtendedVectorType extends DefaultVectorType { | ||
public ExtendedVectorType(DataType subtype, int vectorSize) { | ||
super(subtype, vectorSize); | ||
} | ||
|
||
@Override | ||
public String asCql(boolean includeFrozen, boolean pretty) { | ||
return "VECTOR<" + getElementType().asCql(includeFrozen, pretty) + "," + getDimensions() + ">"; | ||
} | ||
} |
54 changes: 0 additions & 54 deletions
54
src/main/java/io/stargate/sgv2/jsonapi/service/operation/tables/AddIndexOperation.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.