mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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:
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
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user