fix(dynamodb): keep index Query/Scan filters to projected attributes (#7922)

Query and Scan on a KEYS_ONLY or INCLUDE index evaluated FilterExpression,
QueryFilter and ScanFilter against the base-table item, so filters matched
attributes the index does not project.

Query now refuses such a filter on GSIs and LSIs, inside validateQuery so it
runs before the projection check, with the same ValidationException the
PartiQL path returns via a shared helper. Scan evaluates the filter on the
index projection, so non-projected attributes never match.

Closes #7896
This commit is contained in:
Mithil batra authored and GitHub committed 2026-11-11 15:28:12 -04:00
1 parent 2fe25793c8
commit cd0c733b1c
5 files changed
+140 -11

No files matched your search

@@ -970,6 +970,80 @@ class DynamoDbAccessPathIntegrationTest {
}
}
@Test
@Order(31)
void indexFiltersOnlySeeProjectedAttributes() {
request("DynamoDB_20120810.PutItem", """
{
"TableName":"%s",
"Item":{"pk":{"S":"filter-projection"},"sk":{"S":"s1"},
"status":{"S":"filter-projection"},"createdAt":{"S":"2026-03-01"},
"alternate":{"S":"filter-projection"},"summary":{"S":"filter-projection"},
"details":{"S":"filter-projection"}}
}
""".formatted(TABLE))
.statusCode(200);
// Query refuses a filter the index cannot evaluate, on GSIs and LSIs alike.
request("DynamoDB_20120810.Query", """
{
"TableName":"%s",
"IndexName":"status-index",
"KeyConditionExpression":"#status = :v",
"FilterExpression":"#d = :v AND summary = :v",
"ExpressionAttributeNames":{"#status":"status","#d":"details"},
"ExpressionAttributeValues":{":v":{"S":"filter-projection"}}
}
""".formatted(TABLE))
.statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", equalTo("One or more parameter values were invalid: Secondary index "
+ "status-index does not project one or more filter attributes: [details]"));
request("DynamoDB_20120810.Query", """
{
"TableName":"%s",
"IndexName":"alternate-index",
"KeyConditions":{"pk":{"ComparisonOperator":"EQ","AttributeValueList":[{"S":"filter-projection"}]}},
"QueryFilter":{"details":{"ComparisonOperator":"EQ","AttributeValueList":[{"S":"filter-projection"}]}}
}
""".formatted(TABLE))
.statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", equalTo("One or more parameter values were invalid: Secondary index "
+ "alternate-index does not project one or more filter attributes: [details]"));
// Scan evaluates the filter on what the index holds, so a non-projected attribute never matches.
request("DynamoDB_20120810.Scan", """
{
"TableName":"%s",
"IndexName":"status-index",
"FilterExpression":"details = :v",
"ExpressionAttributeValues":{":v":{"S":"filter-projection"}}
}
""".formatted(TABLE))
.statusCode(200)
.body("Count", equalTo(0));
request("DynamoDB_20120810.Scan", """
{
"TableName":"%s",
"IndexName":"status-index",
"ScanFilter":{"details":{"ComparisonOperator":"EQ","AttributeValueList":[{"S":"filter-projection"}]}}
}
""".formatted(TABLE))
.statusCode(200)
.body("Count", equalTo(0));
request("DynamoDB_20120810.Scan", """
{
"TableName":"%s",
"IndexName":"status-index",
"FilterExpression":"summary = :v",
"ExpressionAttributeValues":{":v":{"S":"filter-projection"}}
}
""".formatted(TABLE))
.statusCode(200)
.body("Count", equalTo(1));
}
@Test
@Order(99)
void deleteTable() {
@@ -351,6 +351,31 @@ class DynamoDbAccessPathValidatorTest {
table, lsiPath, "ALL_ATTRIBUTES", null, null, null));
}
@Test
void queryRejectsIndexFilterOnNonProjectedAttributes() {
ObjectNode names = mapper.createObjectNode();
names.put("#z", "zeta");
ObjectNode queryFilter = mapper.createObjectNode();
queryFilter.putObject("details");
assertDoesNotThrow(() -> queryWithFilter(gsiPath, "status = :status", "summary = :s", null, null));
AwsException expressionError = assertThrows(AwsException.class, () -> queryWithFilter(gsiPath,
"status = :status", "#z.a = :v OR summary = :s OR size(#z) > :n", null, names));
assertEquals("One or more parameter values were invalid: Secondary index status-index "
+ "does not project one or more filter attributes: [zeta]", expressionError.getMessage());
AwsException legacyError = assertThrows(AwsException.class,
() -> queryWithFilter(lsiPath, "pk = :pk", null, queryFilter, null));
assertEquals("One or more parameter values were invalid: Secondary index alternate-index "
+ "does not project one or more filter attributes: [details]", legacyError.getMessage());
assertDoesNotThrow(() -> queryWithFilter(tablePath, "pk = :pk", "details = :d", queryFilter, null));
}
private void queryWithFilter(DynamoDbAccessPath accessPath, String keyCondition, String filter,
ObjectNode queryFilter, ObjectNode names) {
DynamoDbAccessPathValidator.validateQuery(table, accessPath, null, keyCondition, filter,
queryFilter, names, expressionValues(extractPlaceholders(keyCondition)));
}
@Test
void listsTheUnprojectedGsiAttributesOnceInRequestOrder() {
ObjectNode names = mapper.createObjectNode();