mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(dynamodb): refuse a query with more conditions than the key schema has (#7935)
DynamoDB counts the conditions of a KeyConditionExpression or KeyConditions after it finds the table and index. When there are more conditions than key attributes in the table or index key schema, it gives "The number of query conditions (n) exceeds the number of key attributes defined in the schema (k)". This comes before the missing key, unsupported condition and key type checks.
This commit is contained in:
1 parent
fddd6b4d83
commit
77cb014f9d
3 files changed
+86
-1
No files matched your search
@@ -153,7 +153,9 @@ final class DynamoDbAccessPathValidator {
|
||||
|
||||
boolean conditionOnNonKeyAttribute = false;
|
||||
|
||||
for (Expr rawCondition : keyConditions(expression)) {
|
||||
List<Expr> conditions = keyConditions(expression);
|
||||
requireConditionCountWithinKeySchema(conditions.size(), accessPath);
|
||||
for (Expr rawCondition : conditions) {
|
||||
Expr condition = normalizeOperandOrder(rawCondition);
|
||||
String attribute = conditionAttribute(condition, names);
|
||||
if (attribute != null && partitionKeySet.contains(attribute)) {
|
||||
@@ -190,6 +192,14 @@ final class DynamoDbAccessPathValidator {
|
||||
return partitionKeyValuePlaceholder;
|
||||
}
|
||||
|
||||
private static void requireConditionCountWithinKeySchema(int conditions, DynamoDbAccessPath accessPath) {
|
||||
int keyAttributes = accessPath.keySchema().size();
|
||||
if (keyAttributes < conditions) {
|
||||
throw validationException("The number of query conditions (" + conditions
|
||||
+ ") exceeds the number of key attributes defined in the schema (" + keyAttributes + ")");
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateCompositeSortKeyConditions(List<String> sortKeys,
|
||||
Map<String, Boolean> equalities) {
|
||||
for (int laterIndex = 1; laterIndex < sortKeys.size(); laterIndex++) {
|
||||
@@ -306,6 +316,7 @@ final class DynamoDbAccessPathValidator {
|
||||
private static void validateLegacyKeyConditions(TableDefinition table,
|
||||
DynamoDbAccessPath accessPath,
|
||||
JsonNode keyConditions) {
|
||||
requireConditionCountWithinKeySchema(keyConditions == null ? 0 : keyConditions.size(), accessPath);
|
||||
List<String> partitionKeys = accessPath.partitionKeyNames();
|
||||
for (String partitionKey : partitionKeys) {
|
||||
if (keyConditions == null || !keyConditions.has(partitionKey)) {
|
||||
|
||||
@@ -140,6 +140,39 @@ class DynamoDbAccessPathIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(13)
|
||||
void queryRefusesMoreConditionsThanTheKeySchemaHas() {
|
||||
List<String> conditions = List.of("""
|
||||
"KeyConditionExpression":"pk = :pk AND sk > :a AND x = :b",
|
||||
"ExpressionAttributeValues":{":pk":{"N":"1"},":a":{"S":"a"},":b":{"S":"b"}}
|
||||
""", """
|
||||
"IndexName":"alternate-index",
|
||||
"KeyConditionExpression":"pk = :pk AND alternate = :a AND sk = :b",
|
||||
"ExpressionAttributeValues":{":pk":{"S":"p1"},":a":{"S":"a"},":b":{"S":"b"}}
|
||||
""", """
|
||||
"IndexName":"status-index",
|
||||
"Select":"ALL_ATTRIBUTES",
|
||||
"KeyConditionExpression":"#s = :s AND createdAt > :a AND x = :b",
|
||||
"ExpressionAttributeNames":{"#s":"status"},
|
||||
"ExpressionAttributeValues":{":s":{"S":"open"},":a":{"S":"a"},":b":{"S":"b"}}
|
||||
""", """
|
||||
"KeyConditions":{
|
||||
"pk":{"ComparisonOperator":"LT","AttributeValueList":[{"S":"p1"}]},
|
||||
"sk":{"ComparisonOperator":"EQ","AttributeValueList":[{"S":"a"}]},
|
||||
"x":{"ComparisonOperator":"EQ","AttributeValueList":[{"S":"b"}]}
|
||||
}
|
||||
""");
|
||||
conditions.forEach(condition -> request("DynamoDB_20120810.Query", """
|
||||
{"TableName":"%s",
|
||||
%s}
|
||||
""".formatted(TABLE, condition))
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo("The number of query conditions (3) exceeds the number of key attributes "
|
||||
+ "defined in the schema (2)")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void tableAndLsiUseTheirSelectedKeySchemas() {
|
||||
|
||||
@@ -422,6 +422,47 @@ class DynamoDbAccessPathValidatorTest {
|
||||
assertEquals("Query condition missed key schema element: pk", error.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', textBlock = """
|
||||
| pk = :pk AND sk > :a AND x = :b | 3 | 2
|
||||
| x = :a AND y = :b AND z = :c | 3 | 2
|
||||
| pk > :pk AND sk > :a AND x = :b | 3 | 2
|
||||
| pk = :pk AND (sk > :a AND x = :b) | 3 | 2
|
||||
alternate-index | pk = :pk AND alternate = :a AND sk = :b | 3 | 2
|
||||
tenant-region-index | tenantId = :t AND region = :r AND createdAt = :c AND x = :x | 4 | 3
|
||||
""")
|
||||
void refusesMoreKeyConditionsThanTheKeySchemaHas(String indexName, String expression,
|
||||
int conditions, int keyAttributes) {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> validateExpression(DynamoDbAccessPath.resolve(table, indexName), expression));
|
||||
|
||||
assertEquals("The number of query conditions (" + conditions + ") exceeds the number of key attributes "
|
||||
+ "defined in the schema (" + keyAttributes + ")", error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void countsKeyConditionsBeforeCheckingTheirValueTypes() {
|
||||
ObjectNode values = expressionValues(":pk", ":sk", ":v");
|
||||
values.set(":pk", numberValue("1"));
|
||||
AwsException expressionError = assertThrows(AwsException.class,
|
||||
() -> DynamoDbAccessPathValidator.validateQuery(
|
||||
table, tablePath, null, "pk = :pk AND sk > :sk AND status = :v",
|
||||
null, null, null, values));
|
||||
|
||||
ObjectNode legacy = mapper.createObjectNode();
|
||||
legacy.set("pk", legacyCondition("EQ", attributeValues("p1")));
|
||||
legacy.set("sk", legacyCondition("EQ", mapper.createArrayNode().add(numberValue("1"))));
|
||||
legacy.set("status", legacyCondition("EQ", attributeValues("open")));
|
||||
AwsException legacyError = assertThrows(AwsException.class,
|
||||
() -> DynamoDbAccessPathValidator.validateQuery(
|
||||
table, tablePath, legacy, null, null, null, null, null));
|
||||
|
||||
String message = "The number of query conditions (3) exceeds the number of key attributes "
|
||||
+ "defined in the schema (2)";
|
||||
assertEquals(message, expressionError.getMessage());
|
||||
assertEquals(message, legacyError.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stillRejectsANonKeyAttributeAlongsideTheFullKey() {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
|
||||
Reference in new issue
Block a user