fix(dynamodb): return the item that a PartiQL write's failed condition saw with ALL_OLD (#6910)

This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-10-29 07:44:59 -04:00
1 parent e24bc36f64
commit 772b444850
4 files changed
+107 -10

No files matched your search

@@ -36,8 +36,8 @@ class DynamoDbPartiQLHandler {
return switch (stmt) {
case Stmt.Select s -> executeSelect(s, ctx, region);
case Stmt.Insert s -> executeInsert(s, region);
case Stmt.Update s -> executeUpdate(s, region);
case Stmt.Delete s -> executeDelete(s, region);
case Stmt.Update s -> executeUpdate(s, ctx.returnValuesOnConditionCheckFailure(), region);
case Stmt.Delete s -> executeDelete(s, ctx.returnValuesOnConditionCheckFailure(), region);
case Stmt.Exists ignored -> throw new AwsException("ValidationException",
"EXISTS can only be used in ExecuteTransaction write requests.", 400);
};
@@ -562,7 +562,7 @@ class DynamoDbPartiQLHandler {
// --- UPDATE ---
private Result executeUpdate(Stmt.Update stmt, String region) {
private Result executeUpdate(Stmt.Update stmt, String returnValuesOnConditionCheckFailure, String region) {
requireNoIndexQualifier(stmt.index());
TableDefinition table = service.describeTable(stmt.table(), region);
requireOneValuePerKey(table, stmt.where());
@@ -576,8 +576,8 @@ class DynamoDbPartiQLHandler {
DynamoDbService.UpdateResult result = service.updateItem(stmt.table(), key, null, ue,
ean.isEmpty() ? null : ean.toNode(mapper),
eav.isEmpty() ? null : eav.toNode(mapper), returnValues, ce, region, "NONE",
UpdateSizeRule.FINISHED_ITEM);
eav.isEmpty() ? null : eav.toNode(mapper), returnValues, ce, region,
returnValuesOnConditionCheckFailure, UpdateSizeRule.FINISHED_ITEM);
return new Result(itemsResponse(returnedAttributes(stmt.returning(), result)),
writeCapacity(table, result.oldItem(), result.newItem()));
}
@@ -617,7 +617,7 @@ class DynamoDbPartiQLHandler {
// --- DELETE ---
private Result executeDelete(Stmt.Delete stmt, String region) {
private Result executeDelete(Stmt.Delete stmt, String returnValuesOnConditionCheckFailure, String region) {
requireNoIndexQualifier(stmt.index());
TableDefinition table = service.describeTable(stmt.table(), region);
requireOneValuePerKey(table, stmt.where());
@@ -629,7 +629,7 @@ class DynamoDbPartiQLHandler {
JsonNode oldItem = service.deleteItem(stmt.table(), key, ce,
ean.isEmpty() ? null : ean.toNode(mapper),
eav.isEmpty() ? null : eav.toNode(mapper), region, "NONE");
eav.isEmpty() ? null : eav.toNode(mapper), region, returnValuesOnConditionCheckFailure);
return new Result(itemsResponse(stmt.returning() == Returning.ALL_OLD ? oldItem : null),
writeCapacity(table, oldItem, null));
}
@@ -646,7 +646,7 @@ class DynamoDbPartiQLHandler {
}
}
TransactMember toTransactItem(Stmt stmt, String region) {
TransactMember toTransactItem(Stmt stmt, String returnValuesOnConditionCheckFailure, String region) {
requireNoIndexQualifier(switch (stmt) {
case Stmt.Update upd -> upd.index();
case Stmt.Delete del -> del.index();
@@ -687,6 +687,7 @@ class DynamoDbPartiQLHandler {
update.set("Key", key);
update.put("UpdateExpression", ue);
update.put("ConditionExpression", updateCondition(table, upd.where(), eav, ean));
update.put("ReturnValuesOnConditionCheckFailure", returnValuesOnConditionCheckFailure);
if (!eav.isEmpty()) update.set("ExpressionAttributeValues", eav.toNode(mapper));
if (!ean.isEmpty()) update.set("ExpressionAttributeNames", ean.toNode(mapper));
txItem.set("Update", update);
@@ -702,6 +703,7 @@ class DynamoDbPartiQLHandler {
if (ce != null) {
delete.put("ConditionExpression", ce);
}
delete.put("ReturnValuesOnConditionCheckFailure", returnValuesOnConditionCheckFailure);
if (!eav.isEmpty()) {
delete.set("ExpressionAttributeValues", eav.toNode(mapper));
}
@@ -3529,6 +3529,7 @@ public class NativeDynamoDbJsonHandler {
.limit(request.has("Limit") ? request.get("Limit").asInt() : null)
.nextToken(request.has("NextToken") ? request.get("NextToken").asText() : null)
.consistentRead(request.path("ConsistentRead").asBoolean(false))
.returnValuesOnConditionCheckFailure(request.path("ReturnValuesOnConditionCheckFailure").asText("NONE"))
.tokenBinding(partiQLHandler.tokenBinding(statement, parameters));
DynamoDbPartiQLHandler.Result result = partiQLHandler.execute(stmt, ctx, region);
ObjectNode response = result.body();
@@ -3569,7 +3570,10 @@ public class NativeDynamoDbJsonHandler {
List<DynamoDbPartiQLHandler.TransactMember> members = new ArrayList<>();
for (int i = 0; i < statements.size(); i++) {
DynamoDbPartiQLParser.Stmt stmt = statements.get(i);
members.add(inTransactStatement(i, () -> partiQLHandler.toTransactItem(stmt, region)));
String returnValuesOnConditionCheckFailure =
stmts.get(i).path("ReturnValuesOnConditionCheckFailure").asText("NONE");
members.add(inTransactStatement(i,
() -> partiQLHandler.toTransactItem(stmt, returnValuesOnConditionCheckFailure, region)));
}
List<JsonNode> transactItems = cancelOnMemberReasons(members);
DynamoDbService.TransactWriteResult result = dynamoDbService.transactWriteItems(transactItems, region,
@@ -3694,7 +3698,9 @@ public class NativeDynamoDbJsonHandler {
requireBatchSelectReadsByKey(select, region);
}
DynamoDbPartiQLHandler.Result result = partiQLHandler.execute(stmt, PartiQLExecuteContext.builder()
.consistentRead(statement.path("ConsistentRead").asBoolean(false)), region);
.consistentRead(statement.path("ConsistentRead").asBoolean(false))
.returnValuesOnConditionCheckFailure(
statement.path("ReturnValuesOnConditionCheckFailure").asText("NONE")), region);
JsonNode firstItem = result.body().path("Items").path(0);
if (!firstItem.isMissingNode()) {
slot.set("Item", firstItem);
@@ -3705,6 +3711,9 @@ public class NativeDynamoDbJsonHandler {
ObjectNode err = objectMapper.createObjectNode();
err.put("Code", batchMemberErrorCode(e.getErrorCode()));
err.put("Message", e.getMessage());
if (e instanceof ConditionalCheckFailedException failed && failed.getItem() != null) {
err.set("Item", failed.getItem());
}
slot.set("Error", err);
if (tableName != null && batchMemberReachedItsTable(e)) {
slot.put("TableName", tableName);
@@ -6,6 +6,7 @@ class PartiQLExecuteContext {
private String nextToken;
private String tokenBinding;
private boolean consistentRead;
private String returnValuesOnConditionCheckFailure;
private PartiQLExecuteContext() {
}
@@ -34,6 +35,11 @@ class PartiQLExecuteContext {
return this;
}
PartiQLExecuteContext returnValuesOnConditionCheckFailure(String returnValuesOnConditionCheckFailure) {
this.returnValuesOnConditionCheckFailure = returnValuesOnConditionCheckFailure;
return this;
}
boolean consistentRead() {
return consistentRead;
}
@@ -49,4 +55,8 @@ class PartiQLExecuteContext {
String tokenBinding() {
return tokenBinding;
}
String returnValuesOnConditionCheckFailure() {
return returnValuesOnConditionCheckFailure;
}
}
@@ -318,6 +318,82 @@ class DynamoDbPartiQLIntegrationTest {
.body("Responses[2].TableName", equalTo(TABLE));
}
@Test
@Order(19)
void aFailedConditionReturnsTheItemWithAllOld() {
putItem("rvocf-upd", "\"val\":{\"S\":\"here\"}");
putItem("rvocf-del", "\"val\":{\"S\":\"here\"}");
request("DynamoDB_20120810.ExecuteStatement", """
{"Statement":"UPDATE \\"%s\\" SET touched = 'yes' WHERE pk = 'rvocf-upd' AND val = 'wrong'",
"ReturnValuesOnConditionCheckFailure":"ALL_OLD"}
""".formatted(TABLE))
.statusCode(400)
.body("__type", equalTo("ConditionalCheckFailedException"))
.body("Item.val.S", equalTo("here"));
request("DynamoDB_20120810.ExecuteStatement", """
{"Statement":"DELETE FROM \\"%s\\" WHERE pk = 'rvocf-del' AND val = 'wrong'",
"ReturnValuesOnConditionCheckFailure":"ALL_OLD"}
""".formatted(TABLE))
.statusCode(400)
.body("Item.val.S", equalTo("here"));
request("DynamoDB_20120810.ExecuteStatement", """
{"Statement":"UPDATE \\"%s\\" SET touched = 'yes' WHERE pk = 'rvocf-upd' AND val = 'wrong'",
"ReturnValuesOnConditionCheckFailure":"NONE"}
""".formatted(TABLE))
.statusCode(400)
.body("Item", nullValue());
request("DynamoDB_20120810.ExecuteStatement", """
{"Statement":"UPDATE \\"%s\\" SET touched = 'yes' WHERE pk = 'rvocf-ghost'",
"ReturnValuesOnConditionCheckFailure":"ALL_OLD"}
""".formatted(TABLE))
.statusCode(400)
.body("Item", nullValue());
}
@Test
@Order(20)
void aFailedBatchMemberCarriesTheItemOnItsErrorWithAllOld() {
putItem("rvocf-batch", "\"val\":{\"S\":\"here\"}");
putItem("rvocf-batch-none", "\"val\":{\"S\":\"here\"}");
request("DynamoDB_20120810.BatchExecuteStatement", """
{"Statements":[
{"Statement":"UPDATE \\"%1$s\\" SET touched = 'yes' WHERE pk = 'rvocf-batch' AND val = 'wrong'",
"ReturnValuesOnConditionCheckFailure":"ALL_OLD"},
{"Statement":"DELETE FROM \\"%1$s\\" WHERE pk = 'rvocf-batch-none' AND val = 'wrong'"}]}
""".formatted(TABLE))
.statusCode(200)
.body("Responses[0].Error.Code", equalTo("ConditionalCheckFailed"))
.body("Responses[0].Error.Item.val.S", equalTo("here"))
.body("Responses[0].Item", nullValue())
.body("Responses[1].Error.Code", equalTo("ConditionalCheckFailed"))
.body("Responses[1].Error.Item", nullValue());
}
@Test
@Order(21)
void aCancelledTransactionCarriesTheItemOfAFailedWriteWithAllOld() {
putItem("rvocf-tx-upd", "\"val\":{\"S\":\"here\"}");
putItem("rvocf-tx-del", "\"val\":{\"S\":\"here\"}");
request("DynamoDB_20120810.ExecuteTransaction", """
{"TransactStatements":[
{"Statement":"UPDATE \\"%1$s\\" SET touched = 'yes' WHERE pk = 'rvocf-tx-upd' AND val = 'wrong'",
"ReturnValuesOnConditionCheckFailure":"ALL_OLD"},
{"Statement":"DELETE FROM \\"%1$s\\" WHERE pk = 'rvocf-tx-del' AND val = 'wrong'",
"ReturnValuesOnConditionCheckFailure":"ALL_OLD"},
{"Statement":"EXISTS(SELECT * FROM \\"%1$s\\" WHERE pk = 'rvocf-upd' AND val = 'wrong')",
"ReturnValuesOnConditionCheckFailure":"ALL_OLD"}]}
""".formatted(TABLE))
.statusCode(400)
.body("CancellationReasons[0].Code", equalTo("ConditionalCheckFailed"))
.body("CancellationReasons[0].Item.val.S", equalTo("here"))
.body("CancellationReasons[1].Item.val.S", equalTo("here"))
.body("CancellationReasons[2].Code", equalTo("ConditionalCheckFailed"))
.body("CancellationReasons[2].Item", nullValue());
}
private static void putItem(String pk, String attributes) {
request("DynamoDB_20120810.PutItem", """
{"TableName":"%s","Item":{"pk":{"S":"%s"},%s}}