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

@@ -53,6 +53,7 @@ final class DynamoDbAccessPathValidator {
}
validateFilterExpression(accessPath, filterExpression, expressionAttributeNames);
validateLegacyQueryFilter(accessPath, queryFilter);
validateFilterProjection(table, accessPath, filterExpression, queryFilter, expressionAttributeNames);
return partitionKeyValuePlaceholder;
}
@@ -103,6 +104,33 @@ final class DynamoDbAccessPathValidator {
}
}
// Query refuses a filter on attributes the index does not project, for GSIs and LSIs alike.
private static void validateFilterProjection(TableDefinition table, DynamoDbAccessPath accessPath,
String filterExpression, JsonNode legacyFilter,
JsonNode expressionAttributeNames) {
if (!accessPath.isIndex() || "ALL".equals(accessPath.projectionType())) {
return;
}
Set<String> filterAttributes = new LinkedHashSet<>();
if (filterExpression != null) {
collectAttributes(parseExpression(filterExpression, "FilterExpression"),
expressionAttributeNames, filterAttributes);
}
if (legacyFilter != null) {
legacyFilter.fieldNames().forEachRemaining(filterAttributes::add);
}
filterAttributes.removeAll(accessPath.projectedAttributeNames(table));
if (!filterAttributes.isEmpty()) {
throw unprojectedFilterAttributes(accessPath.indexName(), filterAttributes);
}
}
static AwsException unprojectedFilterAttributes(String indexName, Collection<String> attributes) {
return validationException("One or more parameter values were invalid: Secondary index "
+ indexName + " does not project one or more filter attributes: ["
+ String.join(", ", attributes) + "]");
}
static AwsException unprojectedGsiAttributes(String indexName, Collection<String> attributes) {
return validationException("One or more parameter values were invalid: Global secondary index "
+ indexName + " does not project [" + String.join(", ", attributes) + "]");
@@ -497,11 +497,7 @@ class DynamoDbPartiQLHandler {
}
List<String> unprojected = keys.unprojectedFilterAttributes(accessPath.projectedAttributeNames(table));
if (!unprojected.isEmpty()) {
throw new AwsException("ValidationException",
"One or more parameter values were invalid: Secondary index "
+ accessPath.indexName()
+ " does not project one or more filter attributes: ["
+ String.join(", ", unprojected) + "]", 400);
throw DynamoDbAccessPathValidator.unprojectedFilterAttributes(accessPath.indexName(), unprojected);
}
}
@@ -1776,7 +1776,9 @@ public class DynamoDbService {
// is not read, does not count toward ScannedCount, and the cursor
// anchors to the previous scanned item (which, with a filter, may well
// be an item that was not returned).
int sz = readItemSize(item, accessPath, table);
// Scan reads and filters only what the index holds, so non-projected attributes never match.
JsonNode filtered = indexItem(item, accessPath, table);
int sz = DynamoDbItemSize.calculateItemSize(filtered);
if (accSize > 0 && accSize + sz > MAX_RESPONSE_BYTES) {
lastEvaluatedKey = buildKeyNode(table, lastScanned, lekPkName, lekSkName, indexScan);
break;
@@ -1787,8 +1789,8 @@ public class DynamoDbService {
scannedItems.add(item);
lastScanned = item;
boolean matched = (filterExpression == null
|| matchesFilterExpression(item, filterExpression, expressionAttrNames, expressionAttrValues))
&& (scanFilter == null || matchesScanFilterPublic(item, scanFilter, conditionalOperator));
|| matchesFilterExpression(filtered, filterExpression, expressionAttrNames, expressionAttrValues))
&& (scanFilter == null || matchesScanFilterPublic(filtered, scanFilter, conditionalOperator));
if (matched) {
results.add(item);
}
@@ -1812,11 +1814,15 @@ public class DynamoDbService {
// index stores, not on the full base item. Characterised on real AWS (us-east-1,
// 2026-09-05): querying a 20KB item through a KEYS_ONLY GSI costs 0.5 units.
private int readItemSize(JsonNode item, DynamoDbAccessPath accessPath, TableDefinition table) {
return DynamoDbItemSize.calculateItemSize(indexItem(item, accessPath, table));
}
private static JsonNode indexItem(JsonNode item, DynamoDbAccessPath accessPath, TableDefinition table) {
if (!accessPath.isIndex() || "ALL".equals(accessPath.projectionType())) {
return DynamoDbItemSize.calculateItemSize(item);
return item;
}
return DynamoDbItemSize.calculateItemSize(ProjectionEvaluator.trimToAttributes(
(ObjectNode) item, accessPath.projectedAttributeNames(table)));
return ProjectionEvaluator.trimToAttributes(
(ObjectNode) item, accessPath.projectedAttributeNames(table));
}
// The 1 MB page counts the size above. The bill of a base table read counts a vector