fix(dynamodb): recognize every HASH attribute of a composite GSI partition key (#2474)

Query only validated/matched the first HASH attribute of a GSI's
partition key, ignoring the rest.

Closes #2461

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-08-22 17:29:20 -05:00
1 parent 0397826b90
commit 768e96a33f
5 files changed
+167 -35

No files matched your search

@@ -24,6 +24,7 @@ class DynamoDbAccessPathValidatorTest {
private DynamoDbAccessPath tablePath;
private DynamoDbAccessPath gsiPath;
private DynamoDbAccessPath lsiPath;
private DynamoDbAccessPath compositePkGsiPath;
@BeforeEach
void setUp() {
@@ -31,11 +32,18 @@ class DynamoDbAccessPathValidatorTest {
table.setKeySchema(List.of(
new KeySchemaElement("pk", "HASH"),
new KeySchemaElement("sk", "RANGE")));
table.setGlobalSecondaryIndexes(List.of(new GlobalSecondaryIndex(
"status-index",
List.of(new KeySchemaElement("status", "HASH"),
new KeySchemaElement("createdAt", "RANGE")),
null, "INCLUDE", List.of("summary"))));
table.setGlobalSecondaryIndexes(List.of(
new GlobalSecondaryIndex(
"status-index",
List.of(new KeySchemaElement("status", "HASH"),
new KeySchemaElement("createdAt", "RANGE")),
null, "INCLUDE", List.of("summary")),
new GlobalSecondaryIndex(
"tenant-region-index",
List.of(new KeySchemaElement("tenantId", "HASH"),
new KeySchemaElement("region", "HASH"),
new KeySchemaElement("createdAt", "RANGE")),
null, "ALL", null)));
table.setLocalSecondaryIndexes(List.of(new LocalSecondaryIndex(
"alternate-index",
List.of(new KeySchemaElement("pk", "HASH"),
@@ -45,6 +53,45 @@ class DynamoDbAccessPathValidatorTest {
tablePath = DynamoDbAccessPath.resolve(table, null);
gsiPath = DynamoDbAccessPath.resolve(table, "status-index");
lsiPath = DynamoDbAccessPath.resolve(table, "alternate-index");
compositePkGsiPath = DynamoDbAccessPath.resolve(table, "tenant-region-index");
}
@Test
void acceptsCompositePartitionKeyQueryWhenEveryHashAttributeHasEquality() {
assertDoesNotThrow(() -> validateExpression(compositePkGsiPath,
"tenantId = :t AND region = :r"));
assertDoesNotThrow(() -> validateExpression(compositePkGsiPath,
"region = :r AND tenantId = :t"));
}
@Test
void rejectsCompositePartitionKeyQueryMissingOneHashAttribute() {
AwsException error = assertThrows(AwsException.class,
() -> validateExpression(compositePkGsiPath, "tenantId = :t"));
assertEquals("Query condition missed key schema element: region", error.getMessage());
}
@Test
void rejectsCompositePartitionKeyQueryWithNonEqualityCondition() {
assertThrows(AwsException.class, () -> validateExpression(compositePkGsiPath,
"tenantId = :t AND region > :r"));
}
@Test
void validatesLegacyCompositePartitionKeyConditions() {
ObjectNode valid = mapper.createObjectNode();
valid.set("tenantId", legacyCondition("EQ", attributeValues("acme")));
valid.set("region", legacyCondition("EQ", attributeValues("us")));
assertDoesNotThrow(() -> DynamoDbAccessPathValidator.validateQuery(
compositePkGsiPath, valid, null, null, null, null));
ObjectNode missingRegion = mapper.createObjectNode();
missingRegion.set("tenantId", legacyCondition("EQ", attributeValues("acme")));
AwsException error = assertThrows(AwsException.class,
() -> DynamoDbAccessPathValidator.validateQuery(
compositePkGsiPath, missingRegion, null, null, null, null));
assertEquals("Query condition missed key schema element: region", error.getMessage());
}
@Test
@@ -532,6 +532,86 @@ class DynamoDbServiceTest {
5L, 5L, List.of(gsi), region);
}
private void createTableWithCompositePartitionKeyGsi(String region) {
GlobalSecondaryIndex gsi = new GlobalSecondaryIndex(
"tenantRegionIndex",
List.of(
new KeySchemaElement("tenantId", "HASH"),
new KeySchemaElement("region", "HASH"),
new KeySchemaElement("createdAt", "RANGE")),
null, "ALL", null);
service.createTable("Accounts",
List.of(new KeySchemaElement("id", "HASH")),
List.of(
new AttributeDefinition("id", "S"),
new AttributeDefinition("tenantId", "S"),
new AttributeDefinition("region", "S"),
new AttributeDefinition("createdAt", "S")),
5L, 5L, List.of(gsi), region);
service.putItem("Accounts",
item("id", "1", "tenantId", "acme", "region", "us", "createdAt", "2026-01-01"), region);
service.putItem("Accounts",
item("id", "2", "tenantId", "acme", "region", "eu", "createdAt", "2026-01-02"), region);
}
@Test
void queryOnCompositePartitionKeyGsiRequiresEveryHashAttribute() {
String region = "eu-west-1";
createTableWithCompositePartitionKeyGsi(region);
ObjectNode exprValues = mapper.createObjectNode();
exprValues.set(":t", attributeValue("S", "acme"));
AwsException error = assertThrows(AwsException.class, () -> service.query(
"Accounts", null, exprValues, "tenantId = :t",
null, null, null, "tenantRegionIndex", null, null, region));
assertEquals("ValidationException", error.getErrorCode());
}
@Test
void queryOnCompositePartitionKeyGsiFiltersByEveryHashAttribute() {
String region = "eu-west-1";
createTableWithCompositePartitionKeyGsi(region);
ObjectNode exprValues = mapper.createObjectNode();
exprValues.set(":t", attributeValue("S", "acme"));
exprValues.set(":r", attributeValue("S", "us"));
DynamoDbService.QueryResult results = service.query(
"Accounts", null, exprValues, "tenantId = :t AND region = :r",
null, null, null, "tenantRegionIndex", null, null, region);
assertEquals(List.of("1"), results.items().stream()
.map(item -> item.get("id").get("S").asText())
.toList());
}
@Test
void legacyQueryOnCompositePartitionKeyGsiFiltersByEveryHashAttribute() {
String region = "eu-west-1";
createTableWithCompositePartitionKeyGsi(region);
ObjectNode keyConditions = mapper.createObjectNode();
keyConditions.set("tenantId", legacyEqCondition("acme"));
keyConditions.set("region", legacyEqCondition("us"));
DynamoDbService.QueryResult results = service.query("Accounts", keyConditions, null, null,
null, null, null, "tenantRegionIndex", null, null, region);
assertEquals(List.of("1"), results.items().stream()
.map(item -> item.get("id").get("S").asText())
.toList());
}
private ObjectNode legacyEqCondition(String value) {
ObjectNode condition = mapper.createObjectNode();
condition.put("ComparisonOperator", "EQ");
var attrList = mapper.createArrayNode();
attrList.add(attributeValue("S", value));
condition.set("AttributeValueList", attrList);
return condition;
}
@Test
void queryAppliesFilterExpressionAfterKeyCondition() {
String region = "eu-west-1";