mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): honor OR in legacy Query and Scan filters (#8031)
* fix(dynamodb): honor OR in legacy Query and Scan filters Pass ConditionalOperator to the shared legacy read-filter evaluator while retaining default AND behavior and read-before-filter pagination. Reuse one nested-map fixture builder for the four existing expressions. Separate follow-up requested in upstream #8023. Signed-off-by: Andre Kurait <akurait@amazon.com> * fix(dynamodb): validate legacy binary filters before matching Validate every legacy QueryFilter and ScanFilter binary operand through the existing attribute validator before evaluating items. Keep ConditionalOperator validation first and retain valid-filter short circuit behavior. Add actual JSON wire invalid and valid binary controls. Signed-off-by: Andre Kurait <akurait@amazon.com> --------- Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
1 parent
aea5c9ad05
commit
b954b4fb87
8 files changed
+213
-14
No files matched your search
@@ -1707,6 +1707,15 @@ public class DynamoDbService {
|
||||
JsonNode expressionAttrNames, JsonNode expressionAttrValues,
|
||||
JsonNode scanFilter, Integer limit, JsonNode exclusiveStartKey,
|
||||
String indexName, Integer segment, Integer totalSegments, String region) {
|
||||
return scan(tableName, filterExpression, expressionAttrNames, expressionAttrValues,
|
||||
scanFilter, limit, exclusiveStartKey, indexName, segment, totalSegments, "AND", region);
|
||||
}
|
||||
|
||||
public ScanResult scan(String tableName, String filterExpression,
|
||||
JsonNode expressionAttrNames, JsonNode expressionAttrValues,
|
||||
JsonNode scanFilter, Integer limit, JsonNode exclusiveStartKey,
|
||||
String indexName, Integer segment, Integer totalSegments,
|
||||
String conditionalOperator, String region) {
|
||||
DynamoDbReservedWords.check(filterExpression, "FilterExpression");
|
||||
String canonicalTableName = canonicalTableName(region, tableName);
|
||||
String storageKey = regionKey(region, canonicalTableName);
|
||||
@@ -1779,7 +1788,7 @@ public class DynamoDbService {
|
||||
lastScanned = item;
|
||||
boolean matched = (filterExpression == null
|
||||
|| matchesFilterExpression(item, filterExpression, expressionAttrNames, expressionAttrValues))
|
||||
&& (scanFilter == null || matchesScanFilter(item, scanFilter));
|
||||
&& (scanFilter == null || matchesScanFilterPublic(item, scanFilter, conditionalOperator));
|
||||
if (matched) {
|
||||
results.add(item);
|
||||
}
|
||||
@@ -1818,25 +1827,27 @@ public class DynamoDbService {
|
||||
}
|
||||
|
||||
public boolean matchesScanFilterPublic(JsonNode item, JsonNode scanFilter) {
|
||||
return matchesScanFilter(item, scanFilter);
|
||||
return matchesScanFilterPublic(item, scanFilter, "AND");
|
||||
}
|
||||
|
||||
public boolean matchesKeyConditionPublic(JsonNode attrValue, JsonNode condition) {
|
||||
return matchesKeyCondition(attrValue, condition);
|
||||
}
|
||||
|
||||
private boolean matchesScanFilter(JsonNode item, JsonNode scanFilter) {
|
||||
public boolean matchesScanFilterPublic(JsonNode item, JsonNode scanFilter, String conditionalOperator) {
|
||||
boolean useOr = "OR".equals(conditionalOperator);
|
||||
Iterator<Map.Entry<String, JsonNode>> fields = scanFilter.fields();
|
||||
while (fields.hasNext()) {
|
||||
Map.Entry<String, JsonNode> entry = fields.next();
|
||||
String attrName = entry.getKey();
|
||||
JsonNode condition = entry.getValue();
|
||||
JsonNode attrValue = item.get(attrName);
|
||||
if (!matchesKeyCondition(attrValue, condition)) {
|
||||
return false;
|
||||
boolean matched = matchesKeyCondition(attrValue, condition);
|
||||
if (matched == useOr) {
|
||||
return useOr;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return !useOr;
|
||||
}
|
||||
|
||||
// --- Batch Operations ---
|
||||
|
||||
@@ -1270,6 +1270,12 @@ public class NativeDynamoDbJsonHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private void requireReadableFilterValues(JsonNode filter) {
|
||||
ObjectNode values = legacyValues(filter);
|
||||
values.forEach(DynamoDbAttributeValueValidator::requireBase64);
|
||||
requireReadableValues(values, "");
|
||||
}
|
||||
|
||||
private static final List<String> CONDITIONAL_OPERATORS = List.of("AND", "OR");
|
||||
|
||||
private static void requireValidConditionalOperator(JsonNode request, String filterMember) {
|
||||
@@ -1345,7 +1351,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
requireReadableValues(exprAttrValues, "");
|
||||
requireReadableValues(exclusiveStartKey, "");
|
||||
requireReadableValues(legacyValues(keyConditions), "");
|
||||
requireReadableValues(legacyValues(queryFilter), "");
|
||||
requireReadableFilterValues(queryFilter);
|
||||
|
||||
if (keyConditionExpr == null && keyConditions == null) {
|
||||
throw new AwsException("ValidationException",
|
||||
@@ -1416,7 +1422,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
if (queryFilter != null) {
|
||||
final JsonNode qf = queryFilter;
|
||||
queryItems = queryItems.stream()
|
||||
.filter(i -> dynamoDbService.matchesScanFilterPublic(i, qf))
|
||||
.filter(i -> dynamoDbService.matchesScanFilterPublic(i, qf, textOrNull(request, "ConditionalOperator")))
|
||||
.toList();
|
||||
}
|
||||
// Apply index projection (KEYS_ONLY / INCLUDE) when querying a secondary index
|
||||
@@ -1513,7 +1519,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
requireValidConditionalOperator(request, "ScanFilter");
|
||||
requireReadableValues(exprAttrValues, "");
|
||||
requireReadableValues(exclusiveStartKey, "");
|
||||
requireReadableValues(legacyValues(scanFilter), "");
|
||||
requireReadableFilterValues(scanFilter);
|
||||
|
||||
DynamoDbExpressionSize.checkReadWithSize(filterExpr, "FilterExpression");
|
||||
DynamoDbExpressionSize.checkRead(projectionExpressionScan, "ProjectionExpression");
|
||||
@@ -1565,7 +1571,8 @@ public class NativeDynamoDbJsonHandler {
|
||||
|
||||
DynamoDbService.ScanResult result = dynamoDbService.scan(
|
||||
tableName, filterExpr, exprAttrNames, exprAttrValues, scanFilter, limit,
|
||||
exclusiveStartKey, indexNameScan, segment, totalSegments, region);
|
||||
exclusiveStartKey, indexNameScan, segment, totalSegments,
|
||||
textOrNull(request, "ConditionalOperator"), region);
|
||||
|
||||
List<JsonNode> scanItems = result.items();
|
||||
// Apply index projection (KEYS_ONLY / INCLUDE) when scanning a secondary index
|
||||
|
||||
Reference in new issue
Block a user