fix(dynamodb): accept grouped OR and NOT in PartiQL UPDATE and DELETE (#9332)

#9305 ran the full expression check on every write condition in
DynamoDbService.evaluateCondition, and PartiQL UPDATE and DELETE pass a
condition built from the WHERE clause there, with a grouped OR or NOT in a
second pair of parentheses, so the redundant parentheses check refused
statements DynamoDB accepts. The check now runs where the typed backend takes
a put, update or delete, so Step Functions tasks still refuse an unknown
function, and PartiQL writes behave as they did before #9305.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-24 06:45:56 -04:00
1 parent 0acd3307b3
commit ee51e7b993
5 files changed
+64 -14

No files matched your search

@@ -3748,6 +3748,11 @@ public class DynamoDbService {
// --- Condition expression evaluation ---
public void validateConditionExpression(String conditionExpression, JsonNode exprAttrNames,
JsonNode exprAttrValues) {
validateExpression(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
}
public void validateFilterExpression(String filterExpression, JsonNode exprAttrNames, JsonNode exprAttrValues) {
validateExpression(filterExpression, "FilterExpression", exprAttrNames, exprAttrValues);
}
@@ -3760,7 +3765,7 @@ public class DynamoDbService {
private void evaluateCondition(JsonNode existingItem, String conditionExpression,
JsonNode exprAttrNames, JsonNode exprAttrValues, String returnValuesOnConditionCheckFailure) {
validateExpression(conditionExpression, "ConditionExpression", exprAttrNames, exprAttrValues);
DynamoDbReservedWords.check(conditionExpression, "ConditionExpression");
if (!matchesFilterExpression(existingItem, conditionExpression, exprAttrNames, exprAttrValues)) {
if ("ALL_OLD".equals(returnValuesOnConditionCheckFailure)){
throw new ConditionalCheckFailedException(existingItem);
@@ -119,6 +119,8 @@ public class NativeDynamoDbBackend implements DynamoDbBackend {
@Override
public void putItem(Scope scope, String tableName, JsonNode item, String conditionExpression,
JsonNode expressionAttributeNames, JsonNode expressionAttributeValues) {
dynamoDbService.validateConditionExpression(conditionExpression, expressionAttributeNames,
expressionAttributeValues);
RequestScopes.runAs(scope.accountId(), () -> dynamoDbService.putItem(tableName,
DynamoDbMapOrder.sortedItem(item), conditionExpression,
expressionAttributeNames, expressionAttributeValues, scope.region(), "NONE"));
@@ -132,6 +134,8 @@ public class NativeDynamoDbBackend implements DynamoDbBackend {
@Override
public void deleteItem(Scope scope, String tableName, JsonNode key, String conditionExpression,
JsonNode expressionAttributeNames, JsonNode expressionAttributeValues) {
dynamoDbService.validateConditionExpression(conditionExpression, expressionAttributeNames,
expressionAttributeValues);
RequestScopes.runAs(scope.accountId(), () -> dynamoDbService.deleteItem(tableName, key, conditionExpression,
expressionAttributeNames, expressionAttributeValues, scope.region(), "NONE"));
}
@@ -140,6 +144,8 @@ public class NativeDynamoDbBackend implements DynamoDbBackend {
public JsonNode updateItem(Scope scope, String tableName, JsonNode key, JsonNode attributeUpdates,
String updateExpression, JsonNode expressionAttributeNames,
JsonNode expressionAttributeValues, String returnValues, String conditionExpression) {
dynamoDbService.validateConditionExpression(conditionExpression, expressionAttributeNames,
expressionAttributeValues);
DynamoDbService.UpdateResult result = RequestScopes.callAs(scope.accountId(), () ->
dynamoDbService.updateItem(tableName, key,
DynamoDbMapOrder.sortedAttributeUpdates(attributeUpdates), updateExpression,
@@ -1029,6 +1029,29 @@ class DynamoDbPartiQLEdgeCaseIntegrationTest {
.body("message", equalTo(refused + list)));
}
@Test
@Order(38)
void writesWithAGroupedOrOrNotInTheWhereClause() {
String attributes = "\"y\":{\"N\":\"1\"},\"z\":{\"N\":\"1\"}";
String where = " WHERE pk='grouped-or' AND sk='1' AND ";
putItem("grouped-or", attributes);
statement("UPDATE " + QUOTED_TABLE + " SET a=1" + where + "(y=1 OR y=2)").statusCode(200);
statement("UPDATE " + QUOTED_TABLE + " SET b=1" + where + "NOT (y=3 OR y=2)").statusCode(200);
transaction("UPDATE " + QUOTED_TABLE + " SET c=1" + where + "NOT (y=3 AND z=1)").statusCode(200);
getItem("grouped-or")
.body("Item.a.N", equalTo("1"))
.body("Item.b.N", equalTo("1"))
.body("Item.c.N", equalTo("1"));
statement("DELETE FROM " + QUOTED_TABLE + where + "(y=1 OR y=2)").statusCode(200);
getItem("grouped-or").body("Item", nullValue());
putItem("grouped-or", attributes);
transaction("DELETE FROM " + QUOTED_TABLE + where + "NOT (y=3 OR y=2)").statusCode(200);
getItem("grouped-or").body("Item", nullValue());
}
private static void putItem(String pk, String attributes) {
putItem(pk, "1", attributes);
}
@@ -492,19 +492,6 @@ class DynamoDbServiceTest {
assertThrows(AwsException.class, () -> service.describeTable("NonExistent", "eu-west-1"));
}
@ParameterizedTest
@CsvSource(delimiter = '|', textBlock = """
nope(userId) | nope
ATTRIBUTE_EXISTS(userId) | ATTRIBUTE_EXISTS
""")
void putItemRefusesAnUnknownConditionFunctionWithoutTheHandler(String condition, String function) {
createUsersTable("us-east-1");
AwsException exception = assertThrows(AwsException.class, () -> service.putItem("Users",
item("userId", "u1"), condition, null, null, "us-east-1", "NONE"));
assertEquals("Invalid ConditionExpression: Invalid function name; function: " + function,
exception.getMessage());
}
@Test
void applyReplicaUpdatesRejectsBlankRegion() {
createUsersTable("us-east-1");
@@ -179,6 +179,35 @@ class DynamoDbNativeBackendTest {
assertEquals("Invalid FilterExpression: Invalid function name; function: " + function, refused.getMessage());
}
@ParameterizedTest
@CsvSource(delimiter = '|', textBlock = """
putItem | nope(id) | nope
putItem | ATTRIBUTE_EXISTS(id) | ATTRIBUTE_EXISTS
updateItem | nope(id) | nope
updateItem | ATTRIBUTE_EXISTS(id) | ATTRIBUTE_EXISTS
deleteItem | nope(id) | nope
deleteItem | ATTRIBUTE_EXISTS(id) | ATTRIBUTE_EXISTS
""")
void typedWritesRefuseAnUnknownConditionFunction(String call, String condition, String function)
throws Exception {
createTable("");
JsonNode key = json("{\"id\":{\"S\":\"k\"}}");
backend.putItem(SCOPE, TABLE, key, null, null, null);
AwsException refused = assertThrows(AwsException.class, () -> {
switch (call) {
case "putItem" -> backend.putItem(SCOPE, TABLE, key, condition, null, null);
case "updateItem" -> backend.updateItem(SCOPE, TABLE, key, null, "SET n = :v", null,
json("{\":v\":{\"N\":\"1\"}}"), "NONE", condition);
default -> backend.deleteItem(SCOPE, TABLE, key, condition, null, null);
}
});
assertEquals("ValidationException", refused.getErrorCode());
assertEquals("Invalid ConditionExpression: Invalid function name; function: " + function,
refused.getMessage());
}
@Test
void typedUpdateItemReturnsOnlyTheRequestedImage() throws Exception {
backend.createTable(SCOPE, TABLE, List.of(new KeySchemaElement("id", "HASH")),