mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
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:
1 parent
0397826b90
commit
768e96a33f
5 files changed
+167
-35
No files matched your search
@@ -43,11 +43,14 @@ record DynamoDbAccessPath(String indexName, Kind kind, List<KeySchemaElement> ke
|
||||
}
|
||||
|
||||
String partitionKeyName() {
|
||||
return partitionKeyNames().getFirst();
|
||||
}
|
||||
|
||||
List<String> partitionKeyNames() {
|
||||
return keySchema.stream()
|
||||
.filter(key -> "HASH".equals(key.getKeyType()))
|
||||
.map(KeySchemaElement::getAttributeName)
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
.toList();
|
||||
}
|
||||
|
||||
List<String> sortKeyNames() {
|
||||
|
||||
+21
-16
@@ -89,9 +89,10 @@ final class DynamoDbAccessPathValidator {
|
||||
|
||||
List<Expr> conditions = root instanceof AndExpr and
|
||||
? and.operands() : List.of(root);
|
||||
String partitionKey = accessPath.partitionKeyName();
|
||||
List<String> partitionKeys = accessPath.partitionKeyNames();
|
||||
Set<String> partitionKeySet = Set.copyOf(partitionKeys);
|
||||
Set<String> sortKeys = Set.copyOf(accessPath.sortKeyNames());
|
||||
int partitionConditions = 0;
|
||||
Set<String> conditionedPartitionKeys = new HashSet<>();
|
||||
String partitionKeyValuePlaceholder = null;
|
||||
Set<String> conditionedAttributes = new HashSet<>();
|
||||
|
||||
@@ -101,12 +102,14 @@ final class DynamoDbAccessPathValidator {
|
||||
throw new AwsException("ValidationException",
|
||||
"KeyConditionExpressions must only contain one condition per key", 400);
|
||||
}
|
||||
if (partitionKey.equals(attribute)) {
|
||||
partitionConditions++;
|
||||
if (attribute != null && partitionKeySet.contains(attribute)) {
|
||||
if (!isPartitionKeyEquality(condition)) {
|
||||
throw new AwsException("ValidationException", "Query key condition not supported", 400);
|
||||
}
|
||||
partitionKeyValuePlaceholder = ((PlaceholderOperand) ((CompareExpr) condition).right()).name();
|
||||
conditionedPartitionKeys.add(attribute);
|
||||
if (attribute.equals(partitionKeys.getFirst())) {
|
||||
partitionKeyValuePlaceholder = ((PlaceholderOperand) ((CompareExpr) condition).right()).name();
|
||||
}
|
||||
} else if (attribute != null && sortKeys.contains(attribute)) {
|
||||
if (!isSupportedSortKeyCondition(condition)) {
|
||||
throw new AwsException("ValidationException", "Query key condition not supported", 400);
|
||||
@@ -116,13 +119,12 @@ final class DynamoDbAccessPathValidator {
|
||||
}
|
||||
}
|
||||
|
||||
if (partitionConditions == 0) {
|
||||
if (!conditionedPartitionKeys.containsAll(partitionKeySet)) {
|
||||
String missing = partitionKeys.stream()
|
||||
.filter(pk -> !conditionedPartitionKeys.contains(pk))
|
||||
.findFirst().orElseThrow();
|
||||
throw new AwsException("ValidationException",
|
||||
"Query condition missed key schema element: " + partitionKey, 400);
|
||||
}
|
||||
if (partitionConditions > 1) {
|
||||
throw new AwsException("ValidationException",
|
||||
"KeyConditionExpressions must only contain one condition per key", 400);
|
||||
"Query condition missed key schema element: " + missing, 400);
|
||||
}
|
||||
return partitionKeyValuePlaceholder;
|
||||
}
|
||||
@@ -165,18 +167,21 @@ final class DynamoDbAccessPathValidator {
|
||||
}
|
||||
|
||||
private static void validateLegacyKeyConditions(DynamoDbAccessPath accessPath, JsonNode keyConditions) {
|
||||
String partitionKey = accessPath.partitionKeyName();
|
||||
if (keyConditions == null || !keyConditions.has(partitionKey)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Query condition missed key schema element: " + partitionKey, 400);
|
||||
List<String> partitionKeys = accessPath.partitionKeyNames();
|
||||
for (String partitionKey : partitionKeys) {
|
||||
if (keyConditions == null || !keyConditions.has(partitionKey)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Query condition missed key schema element: " + partitionKey, 400);
|
||||
}
|
||||
}
|
||||
|
||||
Set<String> partitionKeySet = Set.copyOf(partitionKeys);
|
||||
Set<String> sortKeys = Set.copyOf(accessPath.sortKeyNames());
|
||||
keyConditions.fields().forEachRemaining(entry -> {
|
||||
String attribute = entry.getKey();
|
||||
String operator = entry.getValue().path("ComparisonOperator").asText();
|
||||
int values = entry.getValue().path("AttributeValueList").size();
|
||||
if (partitionKey.equals(attribute)) {
|
||||
if (partitionKeySet.contains(attribute)) {
|
||||
if (!"EQ".equals(operator) || values != 1) {
|
||||
throw new AwsException("ValidationException", "Query key condition not supported", 400);
|
||||
}
|
||||
|
||||
@@ -737,6 +737,7 @@ public class DynamoDbService {
|
||||
String partitionKeyValuePlaceholder = DynamoDbAccessPathValidator.validateQuery(
|
||||
accessPath, keyConditions, keyConditionExpression, filterExpression, null, exprAttrNames);
|
||||
String pkName = accessPath.partitionKeyName();
|
||||
List<String> pkNames = accessPath.partitionKeyNames();
|
||||
String skName = accessPath.sortKeyName();
|
||||
List<String> sortKeyNames = accessPath.sortKeyNames();
|
||||
|
||||
@@ -746,21 +747,17 @@ public class DynamoDbService {
|
||||
List<JsonNode> results = new ArrayList<>();
|
||||
|
||||
if (keyConditions != null) {
|
||||
// Legacy KeyConditions format
|
||||
JsonNode pkCondition = keyConditions.get(pkName);
|
||||
String pkValue = extractComparisonValue(pkCondition);
|
||||
|
||||
for (JsonNode item : items.values()) {
|
||||
if (!item.has(pkName)) continue;
|
||||
if (matchesAttributeValue(item.get(pkName), pkValue)) {
|
||||
if (skName != null && keyConditions.has(skName)) {
|
||||
JsonNode skCondition = keyConditions.get(skName);
|
||||
if (matchesKeyCondition(item.get(skName), skCondition)) {
|
||||
results.add(item);
|
||||
}
|
||||
} else {
|
||||
boolean partitionKeyMatches = pkNames.stream().allMatch(name -> item.has(name)
|
||||
&& matchesAttributeValue(item.get(name), extractComparisonValue(keyConditions.get(name))));
|
||||
if (!partitionKeyMatches) continue;
|
||||
if (skName != null && keyConditions.has(skName)) {
|
||||
JsonNode skCondition = keyConditions.get(skName);
|
||||
if (matchesKeyCondition(item.get(skName), skCondition)) {
|
||||
results.add(item);
|
||||
}
|
||||
} else {
|
||||
results.add(item);
|
||||
}
|
||||
}
|
||||
} else if (keyConditionExpression != null) {
|
||||
|
||||
Reference in new issue
Block a user