Skip to content

Commit 9eeee61

Browse files
sobychackoilayaperumalg
authored andcommitted
GH-3181: fix: Validate index name exists in CassandraVectorStore
Fixes: #3181 #3181 When using CassandraVectorStore with an invalid index name and initializeSchema=false, the application would throw a generic NoSuchElementException from Optional.get() with message "No value present" when trying to access the index metadata. Changes: - Added explicit validation of index existence in checkSchemaValid() method - Improved error handling in getIndexSimilarity() method to provide a clear error message when an index doesn't exist - Added integration test to verify proper error handling with invalid index names Signed-off-by: Soby Chacko <soby.chacko@broadcom.com>
1 parent eba7bde commit 9eeee61

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

vector-stores/spring-ai-cassandra-store/src/main/java/org/springframework/ai/vectorstore/cassandra/CassandraVectorStore.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
4444
import com.datastax.oss.driver.api.core.data.CqlVector;
4545
import com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata;
46+
import com.datastax.oss.driver.api.core.metadata.schema.IndexMetadata;
4647
import com.datastax.oss.driver.api.core.metadata.schema.TableMetadata;
4748
import com.datastax.oss.driver.api.core.type.DataType;
4849
import com.datastax.oss.driver.api.core.type.DataTypes;
@@ -397,11 +398,16 @@ void checkSchemaValid() {
397398

398399
private Similarity getIndexSimilarity(TableMetadata metadata) {
399400

400-
return Similarity.valueOf(metadata.getIndex(this.schema.index())
401-
.get()
402-
.getOptions()
403-
.getOrDefault("similarity_function", "COSINE")
404-
.toUpperCase());
401+
Optional<IndexMetadata> indexMetadata = metadata.getIndex(this.schema.index());
402+
403+
if (indexMetadata.isEmpty()) {
404+
throw new IllegalStateException(
405+
String.format("Index %s does not exist in table %s", this.schema.index(), this.schema.table));
406+
}
407+
408+
return Similarity
409+
.valueOf(indexMetadata.get().getOptions().getOrDefault("similarity_function", "COSINE").toUpperCase());
410+
405411
}
406412

407413
private PreparedStatement prepareDeleteStatement() {
@@ -554,6 +560,9 @@ void checkSchemaValid(int vectorDimension) {
554560
.getTable(this.schema.table)
555561
.get();
556562

563+
Preconditions.checkState(tableMetadata.getIndex(this.schema.index()).isPresent(), "index %s does not exist",
564+
this.schema.index());
565+
557566
Preconditions.checkState(tableMetadata.getColumn(this.schema.content).isPresent(), "column %s does not exist",
558567
this.schema.content);
559568

vector-stores/spring-ai-cassandra-store/src/test/java/org/springframework/ai/vectorstore/cassandra/CassandraVectorStoreIT.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,31 @@ void getNativeClientTest() {
522522
});
523523
}
524524

525+
@Test
526+
void throwsExceptionOnInvalidIndexNameWithSchemaValidation() {
527+
this.contextRunner.run(context -> {
528+
// Create valid schema first, then close
529+
try (CassandraVectorStore validStore = createTestStore(context, new SchemaColumn("meta1", DataTypes.TEXT),
530+
new SchemaColumn("meta2", DataTypes.TEXT))) {
531+
// Nothing to do here. This should not fail as the Schema now exists
532+
}
533+
534+
// Now try with invalid index name but don't reinitialize schema
535+
CassandraVectorStore.Builder invalidBuilder = storeBuilder(context.getBean(CqlSession.class),
536+
context.getBean(EmbeddingModel.class))
537+
.addMetadataColumns(new SchemaColumn("meta1", DataTypes.TEXT),
538+
new SchemaColumn("meta2", DataTypes.TEXT))
539+
.indexName("non_existent_index_name")
540+
.initializeSchema(false);
541+
542+
IllegalStateException exception = Assertions.assertThrows(IllegalStateException.class,
543+
invalidBuilder::build);
544+
545+
assertThat(exception.getMessage()).contains("non_existent_index_name");
546+
assertThat(exception.getMessage()).contains("does not exist");
547+
});
548+
}
549+
525550
@SpringBootConfiguration
526551
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
527552
public static class TestApplication {

0 commit comments

Comments
 (0)