feat(dynamodb): vector indexes and SearchVectors (#4215)

* feat(dynamodb): define vector indexes on the table control plane

* feat(dynamodb): serve SearchVectors against vector indexes

* feat(dynamodb): validate vector attributes on write

* feat(dynamodb): report vector write capacity on writes

* fix(dynamodb): reject a PartiQL read of a vector index

* test(dynamodb): cover vector search in Floci's own suite

* fix(dynamodb): match AWS on vector member paths and the f32 number format

* fix(dynamodb): match AWS on the vector search validation gaps

* perf(dynamodb): stop retaining and recomputing per candidate in vector search

* docs(dynamodb): document that indexes are not stored
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-09-23 08:57:08 -04:00
1 parent 6fadba8d57
commit 133ef62d27
20 files changed
+3014 -9

No files matched your search

@@ -0,0 +1,215 @@
package io.github.hectorvent.floci.services.dynamodb;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;
import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
import io.github.hectorvent.floci.services.dynamodb.model.VectorIndex;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* The two phases a vector index added by UpdateTable walks through.
*
* <p>The phases are derived from one stored instant on read, so a test moves through them by
* rewinding that instant rather than by sleeping, the way {@code AcmIssuanceServiceTest} rewinds
* a certificate's validation wait. Both durations are 0 under {@code @QuarkusTest}, which is why
* this coverage lives in a unit test.
*/
class DynamoDbVectorIndexLifecycleTest {
private static final String REGION = "us-east-1";
private static final String TABLE = "Docs";
private static final int ALLOCATION_SECONDS = 4;
private static final int BACKFILL_SECONDS = 10;
private DynamoDbService service;
@BeforeEach
void setUp() {
service = new DynamoDbService(new InMemoryStorage<>(), null, null, null,
new RegionResolver(REGION, "000000000000"), null, null, null, null,
ALLOCATION_SECONDS, BACKFILL_SECONDS);
TableDefinition table = service.createTable(TABLE,
List.of(new KeySchemaElement("pk", "HASH")),
List.of(new AttributeDefinition("pk", "S")),
null, null, List.of(), List.of(), List.of(), "PAY_PER_REQUEST", REGION);
// The handler stamps the billing mode after CreateTable returns, and UpdateTable reads it
// back whenever the request does not resend BillingMode.
table.setBillingMode("PAY_PER_REQUEST");
}
private static VectorIndex vectorIndex(String indexName) {
return new VectorIndex(indexName, "embedding", List.of(), "ALL", List.of(), 3L, "COSINE");
}
private static DynamoDbService.VectorIndexCreate create(String indexName, String memberPath) {
return new DynamoDbService.VectorIndexCreate(vectorIndex(indexName), memberPath);
}
private TableDefinition addIndex(String indexName) {
return service.updateTable(TABLE, null, null, List.of(), List.of(), List.of(),
List.of(create(indexName, "vectorIndexUpdates.1.member.create")), List.of(), null, REGION);
}
private TableDefinition deleteIndex(String indexName) {
return service.updateTable(TABLE, null, null, List.of(), List.of(), List.of(),
List.of(), List.of(indexName), null, REGION);
}
private void elapse(String indexName, int seconds) {
service.describeTable(TABLE, REGION).findVectorIndex(indexName).orElseThrow()
.setCreationStartedAt(Instant.now().minusSeconds(seconds));
}
private VectorIndex describeIndex(String indexName) {
return service.describeTable(TABLE, REGION).findVectorIndex(indexName).orElseThrow();
}
@Test
void indexCreatedWithTheTableIsActiveAndNeverReportsBackfilling() {
service.createTable("Fresh",
List.of(new KeySchemaElement("pk", "HASH")),
List.of(new AttributeDefinition("pk", "S")),
null, null, List.of(), List.of(), List.of(create("vix", "vectorIndexes.1.member")),
"PAY_PER_REQUEST", REGION);
TableDefinition described = service.describeTable("Fresh", REGION);
VectorIndex index = described.findVectorIndex("vix").orElseThrow();
assertEquals("ACTIVE", described.getTableStatus());
assertEquals("ACTIVE", index.getIndexStatus());
assertFalse(service.reportsVectorIndexBackfilling(index));
}
@Test
void resourceAllocationReportsAnUpdatingTableAndBackfillingFalse() {
assertEquals("UPDATING", addIndex("vix").getTableStatus());
TableDefinition described = service.describeTable(TABLE, REGION);
VectorIndex index = described.findVectorIndex("vix").orElseThrow();
assertEquals("UPDATING", described.getTableStatus());
assertEquals("CREATING", index.getIndexStatus());
assertTrue(service.reportsVectorIndexBackfilling(index));
assertFalse(service.isVectorIndexBackfilling(index));
}
@Test
void deleteDuringResourceAllocationIsRefused() {
addIndex("vix");
AwsException refused = assertThrows(AwsException.class, () -> deleteIndex("vix"));
assertEquals("ResourceInUseException", refused.getErrorCode());
assertEquals("Attempt to change a resource which is still in use: Index creation is in "
+ "resource allocation phase. Retry deletion during backfilling phase or when "
+ "the index is active. Table: " + TABLE + " Index: vix", refused.getMessage());
assertEquals("CREATING", describeIndex("vix").getIndexStatus());
}
@Test
void backfillingReportsAnActiveTableAndACreatingIndex() {
addIndex("vix");
elapse("vix", ALLOCATION_SECONDS + 1);
TableDefinition described = service.describeTable(TABLE, REGION);
VectorIndex index = described.findVectorIndex("vix").orElseThrow();
assertEquals("ACTIVE", described.getTableStatus());
assertEquals("CREATING", index.getIndexStatus());
assertTrue(service.isVectorIndexBackfilling(index));
}
@Test
void deleteDuringBackfillingSucceeds() {
addIndex("vix");
elapse("vix", ALLOCATION_SECONDS + 1);
assertTrue(deleteIndex("vix").getVectorIndexes().isEmpty());
}
@Test
void indexEndsActiveWithBackfillingGone() {
addIndex("vix");
elapse("vix", ALLOCATION_SECONDS + BACKFILL_SECONDS + 1);
TableDefinition described = service.describeTable(TABLE, REGION);
VectorIndex index = described.findVectorIndex("vix").orElseThrow();
assertEquals("ACTIVE", described.getTableStatus());
assertEquals("ACTIVE", index.getIndexStatus());
assertFalse(service.reportsVectorIndexBackfilling(index));
}
@Test
void twoCreatesInOneRequestExceedTheOnlineIndexLimit() {
AwsException refused = assertThrows(AwsException.class, () ->
service.updateTable(TABLE, null, null, List.of(), List.of(), List.of(),
List.of(create("one", "vectorIndexUpdates.1.member.create"),
create("two", "vectorIndexUpdates.2.member.create")),
List.of(), null, REGION));
assertEquals("LimitExceededException", refused.getErrorCode());
assertEquals("Subscriber limit exceeded: Only 1 online index can be created or deleted "
+ "simultaneously per table", refused.getMessage());
assertTrue(service.describeTable(TABLE, REGION).getVectorIndexes().isEmpty());
}
@Test
void secondCreateWhileAnIndexBuildsExceedsTheOnlineIndexLimit() {
addIndex("first");
AwsException refused = assertThrows(AwsException.class, () -> addIndex("second"));
assertEquals("LimitExceededException", refused.getErrorCode());
assertEquals("Subscriber limit exceeded: Only 1 online index can be created or deleted "
+ "simultaneously per table", refused.getMessage());
assertEquals(1, service.describeTable(TABLE, REGION).getVectorIndexes().size());
}
@Test
void anotherIndexMayBeCreatedOnceTheFirstIsActive() {
addIndex("first");
elapse("first", ALLOCATION_SECONDS + BACKFILL_SECONDS + 1);
service.describeTable(TABLE, REGION);
addIndex("second");
assertEquals(List.of("first", "second"),
service.describeTable(TABLE, REGION).getVectorIndexes().stream()
.map(VectorIndex::getIndexName).toList());
}
@Test
void deleteTableIsRefusedWhileAVectorIndexBuilds() {
addIndex("vix");
AwsException refused = assertThrows(AwsException.class,
() -> service.deleteTable(TABLE, REGION));
assertEquals("ResourceInUseException", refused.getErrorCode());
assertEquals("Attempt to change a resource which is still in use: Cannot delete table "
+ "while indexes are being created, updated, or deleted.", refused.getMessage());
}
@Test
void deleteTableSucceedsOnceTheIndexIsActive() {
addIndex("vix");
elapse("vix", ALLOCATION_SECONDS + BACKFILL_SECONDS + 1);
service.deleteTable(TABLE, REGION);
assertThrows(AwsException.class, () -> service.describeTable(TABLE, REGION));
}
}
@@ -0,0 +1,109 @@
package io.github.hectorvent.floci.services.dynamodb;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*;
/**
* Every number below was measured on real DynamoDB in eu-west-2 on 2026-09-23.
*/
class DynamoDbVectorScoringTest {
private static final float[] QUERY = {1f, 0f, 0f};
@Test
void scoresTheIdenticalVector() {
float[] stored = {1f, 0f, 0f};
assertEquals(0.0, score("COSINE", QUERY, stored));
assertEquals(0.0, score("EUCLIDEAN", QUERY, stored));
assertEquals(1.0, score("DOT_PRODUCT", QUERY, stored));
}
@Test
void scoresAUnitNormMix() {
float[] stored = {0.6f, 0.8f, 0f};
assertEquals(0.3999999761581421, score("COSINE", QUERY, stored));
assertEquals(0.8944271802902222, score("EUCLIDEAN", QUERY, stored));
assertEquals(0.6000000238418579, score("DOT_PRODUCT", QUERY, stored));
}
@Test
void scoresBeyondF32Precision() {
float[] stored = {0.1f, 16777217f, 1.000000059604644775390625f};
assertEquals(1.0, score("COSINE", QUERY, stored));
assertEquals(16777216.0, score("EUCLIDEAN", QUERY, stored));
assertEquals(0.10000000149011612, score("DOT_PRODUCT", QUERY, stored));
}
/**
* The exact sum is 16777223, which needs more than 24 bits. A double accumulator answers
* 16777224, a sequential f32 loop answers 16777216 and a four-lane reduction answers
* 16777222, so this case alone does not separate the last two.
*/
@Test
void sumsEightDimensionsAsAPairwiseTree() {
float[] query = ones(8);
float[] stored = ones(8);
stored[0] = 16777216f;
assertEquals(16777222f, score("DOT_PRODUCT", query, stored));
}
/** At sixteen dimensions the four-lane reduction answers 16777228 and the tree 16777230. */
@Test
void sumsSixteenDimensionsAsAPairwiseTree() {
float[] query = ones(16);
float[] stored = ones(16);
stored[0] = 16777216f;
assertEquals(16777230f, score("DOT_PRODUCT", query, stored));
}
@Test
void dotProductIsTheOnlyFunctionWhereHigherIsCloser() {
assertTrue(DynamoDbVectorScoring.higherIsCloser("DOT_PRODUCT"));
assertFalse(DynamoDbVectorScoring.higherIsCloser("COSINE"));
assertFalse(DynamoDbVectorScoring.higherIsCloser("EUCLIDEAN"));
}
@Test
void rendersTheStoredFloatAsShortestPlainDecimal() {
assertEquals("1.0", DynamoDbVectorScoring.render(1f));
assertEquals("0.0", DynamoDbVectorScoring.render(0f));
assertEquals("0.6", DynamoDbVectorScoring.render(0.6f));
assertEquals("0.8", DynamoDbVectorScoring.render(0.8f));
assertEquals("0.1", DynamoDbVectorScoring.render(0.1f));
assertEquals("16777216.0", DynamoDbVectorScoring.render(16777217f));
assertEquals("1.0", DynamoDbVectorScoring.render(1.000000059604644775390625f));
assertEquals("7.0", DynamoDbVectorScoring.render(7f));
assertEquals("-0.5", DynamoDbVectorScoring.render(-0.5f));
assertEquals("0.0", DynamoDbVectorScoring.render(-0f));
assertEquals("0.000123", DynamoDbVectorScoring.render(0.000123f));
assertEquals("1234568000.0", DynamoDbVectorScoring.render(1234567900f));
assertEquals("-10000000000.0", DynamoDbVectorScoring.render(-1e10f));
}
@Test
void rendersOutsideThePlainBandInScientificNotation() {
assertEquals("0.000001", DynamoDbVectorScoring.render(1e-6f));
assertEquals("0.0000011", DynamoDbVectorScoring.render(1.1e-6f));
assertEquals("9.9e-7", DynamoDbVectorScoring.render(9.9e-7f));
assertEquals("1e-7", DynamoDbVectorScoring.render(1e-7f));
assertEquals("1.25e-7", DynamoDbVectorScoring.render(1.25e-7f));
assertEquals("9000000000000.0", DynamoDbVectorScoring.render(9e12f));
assertEquals("1e13", DynamoDbVectorScoring.render(1e13f));
assertEquals("1.5e13", DynamoDbVectorScoring.render(1.5e13f));
assertEquals("1e30", DynamoDbVectorScoring.render(1e30f));
assertEquals("3.4e38", DynamoDbVectorScoring.render(3.4e38f));
}
private static double score(String distanceFunction, float[] query, float[] stored) {
return new DynamoDbVectorScoring.Scorer(distanceFunction, query).score(stored);
}
private static float[] ones(int dimensions) {
float[] vector = new float[dimensions];
Arrays.fill(vector, 1f);
return vector;
}
}
@@ -0,0 +1,981 @@
package io.github.hectorvent.floci.services.dynamodb;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.response.ValidatableResponse;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.util.ArrayList;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* The vector index surface over the wire: DescribeTable round-tripping, the three distance
* functions, projection, and the exact wording of every rejected request.
*
* <p>Both phase durations are 0 in the test configuration, so an index added by UpdateTable is
* ACTIVE at once here. {@link DynamoDbVectorIndexLifecycleTest} covers the two phases.
*/
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class DynamoDbVectorSearchIntegrationTest {
private static final String CT = "application/x-amz-json-1.0";
private static final String TARGET = "DynamoDB_20120810.";
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final String DOCS = "VectorSearchDocs";
private static final String PLAIN = "VectorPlainIndex";
private static final String SCHEMA = "VectorSchemaIndex";
private static final String REJECTED = "VectorRejectedTable";
private static final String QUERY_VECTOR = """
[{"N": "1"}, {"N": "0"}, {"N": "0"}]""";
private static int testPort;
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
// @AfterAll runs after Quarkus has reset the shared port, so it needs its own copy. Taken
// per test rather than in createTables, which a filtered single-method run never reaches.
@BeforeEach
void capturePort() {
testPort = RestAssured.port;
}
@AfterAll
static void cleanup() {
for (String table : List.of(DOCS, PLAIN, SCHEMA)) {
given()
.port(testPort)
.header("X-Amz-Target", TARGET + "DeleteTable")
.contentType(CT)
.body("""
{"TableName": "%s"}
""".formatted(table))
.when()
.post("/")
.then()
.statusCode(200);
}
}
private static ValidatableResponse call(String action, String body) {
return given()
.header("X-Amz-Target", TARGET + action)
.contentType(CT)
.body(body)
.when()
.post("/")
.then();
}
private static JsonNode json(String action, String body) throws Exception {
return MAPPER.readTree(call(action, body).statusCode(200).extract().asString());
}
private static void expectValidation(String action, String body, String message) {
call(action, body)
.statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", equalTo(message));
}
/** A SearchVectors body for the shared query vector, plus any further members verbatim. */
private static String search(String table, String index, int topK, String extraMembers) {
return """
{
"TableName": "%s",
"IndexName": "%s",
"SearchVector": %s,
"TopK": %d%s
}
""".formatted(table, index, QUERY_VECTOR, topK, extraMembers);
}
private static List<String> keysOf(JsonNode response) {
List<String> keys = new ArrayList<>();
for (JsonNode result : response.path("SearchResults")) {
keys.add(result.path("Item").path("pk").path("S").asText());
}
return keys;
}
private static List<Double> scoresOf(JsonNode response) {
List<Double> scores = new ArrayList<>();
for (JsonNode result : response.path("SearchResults")) {
scores.add(result.path("Score").asDouble());
}
return scores;
}
private static List<String> vectorOf(JsonNode searchResult) {
List<String> components = new ArrayList<>();
for (JsonNode component : searchResult.path("Item").path("embedding").path("L")) {
components.add(component.path("N").asText());
}
return components;
}
@Test
@Order(1)
void createTables() {
call("CreateTable", """
{
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST",
"VectorIndexes": [
{"IndexName": "cosine",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "COSINE"},
{"IndexName": "euclid",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "EUCLIDEAN"},
{"IndexName": "dotp",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "DOT_PRODUCT"}
]
}
""".formatted(DOCS))
.statusCode(200)
.body("TableDescription.VectorIndexes.size()", equalTo(3))
.body("TableDescription.VectorIndexes.IndexName",
contains("cosine", "euclid", "dotp"))
.body("TableDescription.VectorIndexes.IndexStatus",
contains("ACTIVE", "ACTIVE", "ACTIVE"));
call("CreateTable", """
{
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST",
"VectorIndexes": [
{"IndexName": "plain",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "COSINE"}
]
}
""".formatted(PLAIN))
.statusCode(200);
call("CreateTable", """
{
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [
{"AttributeName": "pk", "AttributeType": "S"},
{"AttributeName": "tenant", "AttributeType": "S"},
{"AttributeName": "category", "AttributeType": "S"}
],
"BillingMode": "PAY_PER_REQUEST",
"VectorIndexes": [
{"IndexName": "schema",
"VectorAttribute": {"AttributeName": "embedding"},
"SearchSchema": [
{"AttributeName": "tenant", "SearchSchemaElementType": "HASH"},
{"AttributeName": "category", "SearchSchemaElementType": "INLINE_FILTER"}
],
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "COSINE"}
]
}
""".formatted(SCHEMA))
.statusCode(200);
}
@Test
@Order(2)
void seedItems() {
putVector("a", "1", "0", "0");
putVector("b", "0", "1", "0");
putVector("c", "-1", "0", "0");
putVector("d", "0.6", "0.8", "0");
call("PutItem", """
{
"TableName": "%s",
"Item": {"pk": {"S": "novec"}, "label": {"S": "no vector"}}
}
""".formatted(DOCS))
.statusCode(200);
}
private static void putVector(String key, String x, String y, String z) {
call("PutItem", """
{
"TableName": "%s",
"Item": {
"pk": {"S": "%s"},
"label": {"S": "item %s"},
"embedding": {"L": [{"N": "%s"}, {"N": "%s"}, {"N": "%s"}]}
}
}
""".formatted(DOCS, key, key, x, y, z))
.statusCode(200);
}
@Test
@Order(3)
void describeTableRoundTripsTheVectorIndex() {
call("DescribeTable", """
{"TableName": "%s"}
""".formatted(SCHEMA))
.statusCode(200)
.body("Table.VectorIndexes.size()", equalTo(1))
.body("Table.VectorIndexes[0].IndexName", equalTo("schema"))
.body("Table.VectorIndexes[0].Dimensions", equalTo(3))
.body("Table.VectorIndexes[0].DistanceFunction", equalTo("COSINE"))
.body("Table.VectorIndexes[0].VectorAttribute.AttributeName", equalTo("embedding"))
.body("Table.VectorIndexes[0].Projection.ProjectionType", equalTo("ALL"))
.body("Table.VectorIndexes[0].Projection.NonKeyAttributes", nullValue())
.body("Table.VectorIndexes[0].SearchSchema.AttributeName",
contains("tenant", "category"))
.body("Table.VectorIndexes[0].SearchSchema.SearchSchemaElementType",
contains("HASH", "INLINE_FILTER"))
.body("Table.VectorIndexes[0].IndexStatus", equalTo("ACTIVE"))
.body("Table.VectorIndexes[0].IndexArn",
equalTo("arn:aws:dynamodb:us-east-1:000000000000:table/"
+ SCHEMA + "/index/schema"))
.body("Table.VectorIndexes[0].ItemCount", equalTo(0))
.body("Table.VectorIndexes[0].IndexSizeBytes", equalTo(0))
.body("Table.VectorIndexes[0].Backfilling", nullValue());
}
@Test
@Order(4)
void searchRanksByCosineDistance() throws Exception {
JsonNode response = json("SearchVectors", search(DOCS, "cosine", 4, ""));
assertEquals(List.of("a", "d", "b", "c"), keysOf(response));
assertEquals(List.of(0.0, 0.3999999761581421, 1.0, 2.0), scoresOf(response));
}
@Test
@Order(5)
void searchRanksByEuclideanDistance() throws Exception {
JsonNode response = json("SearchVectors", search(DOCS, "euclid", 4, ""));
assertEquals(List.of("a", "d", "b", "c"), keysOf(response));
assertEquals(List.of(0.0, 0.8944271802902222, 1.4142135381698608, 2.0),
scoresOf(response));
}
@Test
@Order(6)
void searchRanksByDotProductDescending() throws Exception {
JsonNode response = json("SearchVectors", search(DOCS, "dotp", 4, ""));
assertEquals(List.of("a", "d", "b", "c"), keysOf(response));
assertEquals(List.of(1.0, 0.6000000238418579, 0.0, -1.0), scoresOf(response));
}
@Test
@Order(7)
void topKCutsTheResultSet() throws Exception {
JsonNode response = json("SearchVectors", search(DOCS, "cosine", 2, ""));
assertEquals(List.of("a", "d"), keysOf(response));
}
@Test
@Order(8)
void itemWithoutTheVectorAttributeStaysOutOfTheIndex() throws Exception {
JsonNode response = json("SearchVectors", search(DOCS, "cosine", 100, ""));
assertEquals(4, response.path("SearchResults").size());
assertFalse(keysOf(response).contains("novec"));
}
@Test
@Order(9)
void vectorAttributeIsAbsentUnlessProjected() throws Exception {
JsonNode response = json("SearchVectors", search(DOCS, "cosine", 1, ""));
JsonNode item = response.path("SearchResults").path(0).path("Item");
assertEquals("a", item.path("pk").path("S").asText());
assertEquals("item a", item.path("label").path("S").asText());
assertTrue(item.path("embedding").isMissingNode(), "the vector is not projected by default");
}
@Test
@Order(10)
void projectionExpressionReturnsTheIndexF32Copy() throws Exception {
JsonNode response = json("SearchVectors", search(DOCS, "cosine", 2, """
, "ProjectionExpression": "pk, embedding"
"""));
JsonNode results = response.path("SearchResults");
assertTrue(results.path(0).path("Item").path("label").isMissingNode());
assertEquals(List.of("1.0", "0.0", "0.0"), vectorOf(results.path(0)));
// The third component was written as "0" and comes back as "0.0": a hit carries the
// index's own f32 copy rather than the text the item stores.
assertEquals(List.of("0.6", "0.8", "0.0"), vectorOf(results.path(1)));
}
@Test
@Order(11)
void searchReportsRequestBytesUnderTotalAndIndexesOnly() throws Exception {
JsonNode total = json("SearchVectors", search(DOCS, "cosine", 1, """
, "ReturnConsumedCapacity": "TOTAL"
"""));
assertEquals(1024.0,
total.path("ConsumedCapacity").path("VectorSearchRequestBytes").asDouble());
assertTrue(total.path("ConsumedCapacity").path("CapacityUnits").isMissingNode());
assertTrue(total.path("ConsumedCapacity").path("TableName").isMissingNode());
JsonNode indexes = json("SearchVectors", search(DOCS, "cosine", 1, """
, "ReturnConsumedCapacity": "INDEXES"
"""));
assertEquals(1024.0,
indexes.path("ConsumedCapacity").path("VectorSearchRequestBytes").asDouble());
JsonNode none = json("SearchVectors", search(DOCS, "cosine", 1, """
, "ReturnConsumedCapacity": "NONE"
"""));
assertTrue(none.path("ConsumedCapacity").isMissingNode());
}
@Test
@Order(12)
void createTableRejectsAVectorIndexOnAProvisionedTable() {
expectValidation("CreateTable", """
{
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"VectorIndexes": [
{"IndexName": "vix",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "COSINE"}
]
}
""".formatted(REJECTED),
"One or more parameter values were invalid: Vector indexes are only supported "
+ "for PAY_PER_REQUEST tables");
}
@Test
@Order(13)
void createTableRejectsASearchSchemaAttributeWithNoDefinition() {
expectValidation("CreateTable", """
{
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST",
"VectorIndexes": [
{"IndexName": "vix",
"VectorAttribute": {"AttributeName": "embedding"},
"SearchSchema": [{"AttributeName": "tenant", "SearchSchemaElementType": "HASH"}],
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "COSINE"}
]
}
""".formatted(REJECTED),
"One or more parameter values were invalid: One element in SearchSchema is not "
+ "defined in attribute definitions");
}
@Test
@Order(14)
void createTableRejectsDimensionsAboveTheLimit() {
expectValidation("CreateTable", """
{
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST",
"VectorIndexes": [
{"IndexName": "vix",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 5000,
"DistanceFunction": "COSINE"}
]
}
""".formatted(REJECTED),
"One or more parameter values were invalid: Number of dimensions must be between "
+ "1 and 4096 inclusive.");
}
@Test
@Order(15)
void createTableRejectsMoreThanFiveVectorIndexes() {
List<String> indexes = new ArrayList<>();
for (int i = 1; i <= 6; i++) {
indexes.add("""
{"IndexName": "vix%d",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "COSINE"}""".formatted(i));
}
expectValidation("CreateTable", """
{
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST",
"VectorIndexes": [%s]
}
""".formatted(REJECTED, String.join(",\n", indexes)),
"One or more parameter values were invalid: VectorIndex count exceeds the "
+ "per-table limit of 5");
}
@Test
@Order(16)
void createTableRejectsTwoDimensionsForOneVectorAttribute() {
expectValidation("CreateTable", """
{
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST",
"VectorIndexes": [
{"IndexName": "three",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "COSINE"},
{"IndexName": "four",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 4,
"DistanceFunction": "COSINE"}
]
}
""".formatted(REJECTED),
"One or more parameter values were invalid: Conflicting attribute definition for "
+ "'embedding'. All VectorIndexes on the same vector attribute must use the same "
+ "dimensions.");
}
@Test
@Order(17)
void searchVectorsRejectsATopKAboveTheLimit() {
expectValidation("SearchVectors", search(DOCS, "cosine", 101, ""),
"Provided TopK value '101' is out of valid range. The value must be between 1 "
+ "and 100 inclusive");
}
@Test
@Order(18)
void searchVectorsRequiresAConditionOnTheSearchSchemaHash() {
expectValidation("SearchVectors", search(SCHEMA, "schema", 4, ""),
"SearchConditionExpression must be provided when SearchSchema has a HASH key");
}
@Test
@Order(19)
void searchVectorsRejectsANonEqualityComparator() {
expectValidation("SearchVectors", search(SCHEMA, "schema", 4, """
,
"SearchConditionExpression": "tenant < :t",
"ExpressionAttributeValues": {":t": {"S": "acme"}}
"""),
"Invalid SearchConditionExpression: Invalid comparator used in "
+ "SearchConditionExpression");
}
@Test
@Order(20)
void searchVectorsRejectsADimensionMismatch() {
expectValidation("SearchVectors", """
{
"TableName": "%s",
"IndexName": "plain",
"SearchVector": [{"N": "1"}, {"N": "0"}],
"TopK": 4
}
""".formatted(PLAIN),
"Input search vector dimension 2 does not match vector index dimension 3");
}
@Test
@Order(21)
void searchVectorsRejectsAConditionOutsideTheSearchSchema() {
expectValidation("SearchVectors", search(PLAIN, "plain", 4, """
,
"SearchConditionExpression": "tenant = :t",
"ExpressionAttributeValues": {":t": {"S": "acme"}}
"""),
"SearchConditionExpression must not contain any attributes that is not in "
+ "SearchSchema. Invalid attribute: tenant");
}
@Test
@Order(22)
void searchVectorsRejectsANonNumericSearchVector() {
expectValidation("SearchVectors", """
{
"TableName": "%s",
"IndexName": "plain",
"SearchVector": [{"N": "1"}, {"S": "x"}, {"N": "0"}],
"TopK": 4
}
""".formatted(PLAIN),
"Search vector contains invalid values. All values in the search vector must be "
+ "a 32-bit floating-point number attribute");
}
@Test
@Order(23)
void putItemRejectsAVectorOfTheWrongLength() {
expectValidation("PutItem", """
{
"TableName": "%s",
"Item": {
"pk": {"S": "short"},
"embedding": {"L": [{"N": "1"}, {"N": "0"}]}
}
}
""".formatted(PLAIN),
"One or more parameter values were invalid. Invalid size for parameter embedding, "
+ "Expected: 3, Actual: 2 IndexName: plain");
}
@Test
@Order(24)
void putItemRejectsANonNumericVectorComponent() {
expectValidation("PutItem", """
{
"TableName": "%s",
"Item": {
"pk": {"S": "typed"},
"embedding": {"L": [{"N": "1"}, {"S": "x"}, {"N": "0"}]}
}
}
""".formatted(PLAIN),
"One or more parameter values were invalid. Invalid type for parameter "
+ "embedding[1], Expected: 32-bit floating point number, Actual: S. "
+ "IndexName: plain");
}
@Test
@Order(25)
void putItemRejectsAVectorAttributeThatIsNotAList() {
expectValidation("PutItem", """
{
"TableName": "%s",
"Item": {
"pk": {"S": "scalar"},
"embedding": {"S": "not a vector"}
}
}
""".formatted(PLAIN),
"One or more parameter values were invalid. Invalid type for parameter embedding, "
+ "Expected: 32-bit floating point number list IndexName: plain");
}
@Test
@Order(26)
void putItemRejectsAnEmptySearchSchemaHashValue() {
expectValidation("PutItem", """
{
"TableName": "%s",
"Item": {
"pk": {"S": "blank-tenant"},
"tenant": {"S": ""},
"embedding": {"L": [{"N": "1"}, {"N": "0"}, {"N": "0"}]}
}
}
""".formatted(SCHEMA),
"One or more parameter values are not valid. A value specified for a secondary "
+ "index key is not supported. The AttributeValue for a key attribute cannot "
+ "contain an empty string value. IndexName: schema, IndexKey: tenant");
}
/**
* The conformance suite probes SearchVectors against a table that does not exist to decide
* whether the operation is served at all. Any other answer turns all 52 vector tests into
* skips rather than failures.
*/
@Test
@Order(27)
void searchVectorsOnAMissingTableIsResourceNotFound() {
call("SearchVectors", search("VectorNoSuchTable", "cosine", 1, ""))
.statusCode(400)
.body("__type", equalTo("ResourceNotFoundException"));
}
@Test
@Order(28)
void partiQLRejectsAVectorIndex() {
expectValidation("ExecuteStatement", """
{"Statement": "SELECT * FROM \\"%s\\".\\"plain\\" WHERE pk = 'a'"}
""".formatted(PLAIN),
"Scan operation not supported on this index type");
}
@Test
@Order(29)
void createTableRejectsADistanceFunctionOutsideTheEnum() {
for (String distanceFunction : List.of("MANHATTAN", "cosine")) {
expectValidation("CreateTable", """
{
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST",
"VectorIndexes": [
{"IndexName": "vix",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "%s"}
]
}
""".formatted(REJECTED, distanceFunction),
"1 validation error detected: Value '" + distanceFunction + "' at "
+ "'vectorIndexes.1.member.distanceFunction' failed to satisfy constraint: "
+ "Member must satisfy enum value set: [DOT_PRODUCT, COSINE, EUCLIDEAN]");
}
}
@Test
@Order(30)
void updateTableRejectsADistanceFunctionOutsideTheEnum() {
expectValidation("UpdateTable", """
{
"TableName": "%s",
"VectorIndexUpdates": [
{"Create": {"IndexName": "vix",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "MANHATTAN"}}
]
}
""".formatted(PLAIN),
"1 validation error detected: Value 'MANHATTAN' at "
+ "'vectorIndexUpdates.1.member.create.distanceFunction' failed to satisfy "
+ "constraint: Member must satisfy enum value set: [DOT_PRODUCT, COSINE, EUCLIDEAN]");
}
@Test
@Order(31)
void createTableRequiresDimensionsVectorAttributeAndProjection() {
expectValidation("CreateTable", vectorIndexTable("""
{"IndexName": "vix",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"DistanceFunction": "COSINE"}"""),
requiredMember("dimensions"));
expectValidation("CreateTable", vectorIndexTable("""
{"IndexName": "vix",
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "COSINE"}"""),
requiredMember("vectorAttribute"));
expectValidation("CreateTable", vectorIndexTable("""
{"IndexName": "vix",
"VectorAttribute": {"AttributeName": "embedding"},
"Dimensions": 3,
"DistanceFunction": "COSINE"}"""),
requiredMember("projection"));
}
/** A CreateTable body carrying the one vector index given. */
private static String vectorIndexTable(String vectorIndex) {
return """
{
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST",
"VectorIndexes": [%s]
}
""".formatted(REJECTED, vectorIndex);
}
private static String requiredMember(String member) {
return "1 validation error detected: Value null at 'vectorIndexes.1.member." + member
+ "' failed to satisfy constraint: Member must not be null";
}
@Test
@Order(32)
void createTableRejectsAVectorIndexNamedLikeAnotherIndex() {
expectValidation("CreateTable", """
{
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [
{"AttributeName": "pk", "AttributeType": "S"},
{"AttributeName": "gsk", "AttributeType": "S"}
],
"BillingMode": "PAY_PER_REQUEST",
"GlobalSecondaryIndexes": [
{"IndexName": "dup",
"KeySchema": [{"AttributeName": "gsk", "KeyType": "HASH"}],
"Projection": {"ProjectionType": "ALL"}}
],
"VectorIndexes": [
{"IndexName": "dup",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "COSINE"}
]
}
""".formatted(REJECTED),
"One or more parameter values were invalid: Duplicate index name: dup");
expectValidation("CreateTable", """
{
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST",
"VectorIndexes": [
{"IndexName": "dup",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "COSINE"},
{"IndexName": "dup",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "COSINE"}
]
}
""".formatted(REJECTED),
"One or more parameter values were invalid: Duplicate index name: dup");
}
@Test
@Order(33)
void searchVectorsChecksTheTopKRangeBeforeTheTableAndTheIndex() {
String outOfRange = "Provided TopK value '1000' is out of valid range. The value must be "
+ "between 1 and 100 inclusive";
expectValidation("SearchVectors", search("VectorNoSuchTable", "cosine", 1000, ""), outOfRange);
expectValidation("SearchVectors", search(DOCS, "VectorNoSuchIndex", 1000, ""), outOfRange);
}
/**
* AWS deserialises SearchVectors with serde and points at the body's closing brace, so the
* column is the length of the compact JSON an SDK sends. Both bodies below are compact, which
* is what pins the rule rather than the shape of the message.
*/
@Test
@Order(34)
void searchVectorsReportsAnOmittedMemberAtTheBodyLength() {
String withoutVector = """
{"TableName":"%s","IndexName":"plain","TopK":1}""".formatted(PLAIN);
expectValidation("SearchVectors", withoutVector,
"missing field `SearchVector` at line 1 column " + withoutVector.length());
String withoutIndex = """
{"TableName":"%s","SearchVector":[{"N":"1"},{"N":"0"},{"N":"0"}],"TopK":1}"""
.formatted(PLAIN);
expectValidation("SearchVectors", withoutIndex,
"missing field `IndexName` at line 1 column " + withoutIndex.length());
}
@Test
@Order(35)
void updateTableRejectsLeavingPayPerRequestWhileAVectorIndexExists() {
expectValidation("UpdateTable", """
{"TableName": "%s", "BillingMode": "PROVISIONED"}
""".formatted(PLAIN),
"One or more parameter values were invalid: Vector indexes are only supported "
+ "for PAY_PER_REQUEST tables");
}
@Test
@Order(36)
void updateTableReportsThePositionInTheVectorIndexUpdatesArray() {
expectValidation("UpdateTable", """
{
"TableName": "%s",
"VectorIndexUpdates": [
{"Delete": {"IndexName": "plain"}},
{"Create": {"IndexName": "ab",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3,
"DistanceFunction": "COSINE"}}
]
}
""".formatted(PLAIN),
"1 validation error detected: Value 'ab' at "
+ "'vectorIndexUpdates.2.member.create.indexName' failed to satisfy constraint: "
+ "Member must have length greater than or equal to 3");
call("DescribeTable", """
{"TableName": "%s"}
""".formatted(PLAIN))
.statusCode(200)
.body("Table.VectorIndexes.IndexName", contains("plain"));
}
@Test
@Order(37)
void createTableRejectsAnIncludeProjectionWithNoNonKeyAttributes() {
expectValidation("CreateTable", """
{
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST",
"VectorIndexes": [
{"IndexName": "vix",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "INCLUDE"},
"Dimensions": 3,
"DistanceFunction": "COSINE"}
]
}
""".formatted(REJECTED),
"One or more parameter values were invalid: ProjectionType is INCLUDE, but "
+ "NonKeyAttributes is not specified");
}
@Test
@Order(38)
void updateTableRejectsAKeysOnlyProjectionWithNonKeyAttributes() {
expectValidation("UpdateTable", """
{
"TableName": "%s",
"VectorIndexUpdates": [
{"Create": {"IndexName": "vix",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "KEYS_ONLY",
"NonKeyAttributes": ["label"]},
"Dimensions": 3,
"DistanceFunction": "COSINE"}}
]
}
""".formatted(PLAIN),
"One or more parameter values were invalid: ProjectionType is KEYS_ONLY, but "
+ "NonKeyAttributes is specified");
}
@Test
@Order(39)
void createTableReportsTheNestedVectorAttributeMember() {
expectValidation("CreateTable", """
{
"TableName": "vec_nested_attr",
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"BillingMode": "PAY_PER_REQUEST",
"VectorIndexes": [
{"IndexName": "vix", "VectorAttribute": {},
"Projection": {"ProjectionType": "ALL"},
"Dimensions": 3, "DistanceFunction": "COSINE"}
]
}
""",
"1 validation error detected: Value null at "
+ "'vectorIndexes.1.member.vectorAttribute.attributeName' failed to satisfy "
+ "constraint: Member must not be null");
}
@Test
@Order(40)
void createTableRequiresADistanceFunction() {
expectValidation("CreateTable", """
{
"TableName": "vec_no_distance",
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"BillingMode": "PAY_PER_REQUEST",
"VectorIndexes": [
{"IndexName": "vix", "VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "ALL"}, "Dimensions": 3}
]
}
""",
"1 validation error detected: Value null at "
+ "'vectorIndexes.1.member.distanceFunction' failed to satisfy constraint: "
+ "Member must not be null");
}
@Test
@Order(41)
void searchVectorsReportsAnOmittedTopKAtTheBodyLength() {
String withoutTopK = """
{"TableName":"%s","IndexName":"plain","SearchVector":[{"N":"1"},{"N":"0"},{"N":"0"}]}"""
.formatted(PLAIN);
expectValidation("SearchVectors", withoutTopK,
"missing field `TopK` at line 1 column " + withoutTopK.length());
}
/** SearchVectors is served by its own frontend, which omits the envelope other operations add. */
@Test
@Order(42)
void searchVectorsRejectsAReturnConsumedCapacityWithNoEnvelope() {
expectValidation("SearchVectors", """
{"TableName": "%s", "IndexName": "plain",
"SearchVector": [{"N": "1"}, {"N": "0"}, {"N": "0"}],
"TopK": 1, "ReturnConsumedCapacity": "BOGUS"}
""".formatted(PLAIN),
"Value 'BOGUS' at 'returnConsumedCapacity' failed to satisfy constraint: "
+ "Member must satisfy enum value set: [INDEXES, TOTAL, NONE]");
}
/** An empty list is a length constraint on the member, the way a GSI reports it. */
@Test
@Order(43)
void createTableRejectsAnEmptyNonKeyAttributesList() {
expectValidation("CreateTable", """
{
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST",
"VectorIndexes": [
{"IndexName": "vix",
"VectorAttribute": {"AttributeName": "embedding"},
"Projection": {"ProjectionType": "INCLUDE", "NonKeyAttributes": []},
"Dimensions": 3,
"DistanceFunction": "COSINE"}
]
}
""".formatted(REJECTED),
"1 validation error detected: Value '[]' at "
+ "'vectorIndexes.1.member.projection.nonKeyAttributes' failed to satisfy "
+ "constraint: Member must have length greater than or equal to 1");
}
}
@@ -5,7 +5,9 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex;
import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;
import io.github.hectorvent.floci.services.dynamodb.model.LocalSecondaryIndex;
import io.github.hectorvent.floci.services.dynamodb.model.SearchSchemaElement;
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
import io.github.hectorvent.floci.services.dynamodb.model.VectorIndex;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -186,4 +188,86 @@ class DynamoDbWriteCapacityTest {
var cost = DynamoDbWriteCapacity.forWrite(table, big, fullItem());
assertEquals(3.0, cost.table(), "the larger of the two images rounds up per 1KB");
}
/**
* Two vector indexes on one attribute, differing only in projection. That pair is what
* separates the projection term of VectorWriteRequestBytes from every other term, and it
* mirrors the fixture the conformance suite measured against real DynamoDB.
*/
private static TableDefinition vectorTable() {
TableDefinition vectorIndexed = new TableDefinition();
vectorIndexed.setKeySchema(List.of(new KeySchemaElement("pk", "HASH")));
vectorIndexed.setVectorIndexes(List.of(
new VectorIndex("vix", "embedding", List.of(), "ALL", List.of(), 3L, "COSINE"),
new VectorIndex("vkeys", "embedding", List.of(), "KEYS_ONLY", List.of(), 3L, "COSINE")));
return vectorIndexed;
}
private static ObjectNode vectorItem(String key) {
return item("""
{"pk": {"S": "%s"},
"embedding": {"L": [{"N": "1"}, {"N": "0"}, {"N": "0"}]}}
""".formatted(key));
}
@Test
void vectorEntryBelowTheFloorReportsTheFloor() {
DynamoDbWriteCapacity.Cost cost =
DynamoDbWriteCapacity.forWrite(vectorTable(), null, vectorItem("f0"));
assertEquals(Map.of("vix", 1024.0, "vkeys", 1024.0), cost.vectorBytes());
assertEquals(1.0, cost.total(), "bytes processed are not capacity units");
}
@Test
void vectorFloorBindsPerEntryRatherThanPerWrite() {
ObjectNode blobbed = vectorItem("3a");
blobbed.set("blob", item("""
{"S": "%s"}
""".formatted("x".repeat(1500))));
DynamoDbWriteCapacity.Cost cost = DynamoDbWriteCapacity.forWrite(vectorTable(), null, blobbed);
// 4 for the key, 1504 for the blob, 9 for the vector's name, 4 per dimension.
assertEquals(1529.0, cost.vectorBytes().get("vix"));
assertEquals(1024.0, cost.vectorBytes().get("vkeys"), "KEYS_ONLY never holds the blob");
}
@Test
void identicalOverwriteChargesNoVectorWrite() {
DynamoDbWriteCapacity.Cost cost =
DynamoDbWriteCapacity.forWrite(vectorTable(), vectorItem("a"), vectorItem("a"));
assertTrue(cost.vectorBytes().isEmpty(), "replication is delta-based");
}
@Test
void vectorIndexIsChargedOnlyWhenItProjectsTheChange() {
ObjectNode before = vectorItem("a");
before.set("note", item("""
{"S": "before"}
"""));
ObjectNode after = vectorItem("a");
after.set("note", item("""
{"S": "after!"}
"""));
DynamoDbWriteCapacity.Cost cost = DynamoDbWriteCapacity.forWrite(vectorTable(), before, after);
assertEquals(1024.0, cost.vectorBytes().get("vix"));
assertNull(cost.vectorBytes().get("vkeys"), "the KEYS_ONLY entry did not change");
}
@Test
void itemWithoutTheVectorAttributeEntersNoVectorIndex() {
DynamoDbWriteCapacity.Cost cost = DynamoDbWriteCapacity.forWrite(vectorTable(), null, item("""
{"pk": {"S": "plain"}, "label": {"S": "no-vector"}}
"""));
assertTrue(cost.vectorBytes().isEmpty());
}
@Test
void itemMissingTheSearchSchemaHashEntersNoVectorIndex() {
TableDefinition partitioned = new TableDefinition();
partitioned.setKeySchema(List.of(new KeySchemaElement("pk", "HASH")));
partitioned.setVectorIndexes(List.of(new VectorIndex("schema", "embedding",
List.of(new SearchSchemaElement("tenant", "HASH")), "ALL", List.of(), 3L, "COSINE")));
DynamoDbWriteCapacity.Cost cost =
DynamoDbWriteCapacity.forWrite(partitioned, null, vectorItem("no-tenant"));
assertTrue(cost.vectorBytes().isEmpty());
}
}