fix(dynamodb): implement DELETE action for set attributes (#312) (#415)

The DELETE clause in UpdateExpression was stubbed out with a
break statement, silently ignoring the operation. This caused
DELETE on SS/NS/BS sets to be a no-op.

Add applyDeleteClause (clause parser) and applyDeleteOperation
(set subtraction) mirroring the existing ADD implementation.
Empty sets after removal trigger attribute deletion per the
DynamoDB spec.
This commit is contained in:
TiM authored and GitHub committed 2026-04-13 15:34:49 -05:00
1 parent cfe48dfabe
commit bffc374406
2 files changed
+298 -2

No files matched your search

@@ -885,6 +885,208 @@ class DynamoDbIntegrationTest {
.statusCode(200);
}
@Test
@Order(29)
void deleteElementsFromStringSet() {
// Create table
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "DeleteSetTable",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST"
}
""")
.when()
.post("/")
.then()
.statusCode(200);
// Put item with a String Set
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "DeleteSetTable",
"Item": {"pk": {"S": "k1"}, "tags": {"SS": ["a", "b", "c"]}}
}
""")
.when()
.post("/")
.then()
.statusCode(200);
// DELETE "a" from the set
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "DeleteSetTable",
"Key": {"pk": {"S": "k1"}},
"UpdateExpression": "DELETE tags :val",
"ExpressionAttributeValues": {":val": {"SS": ["a"]}}
}
""")
.when()
.post("/")
.then()
.statusCode(200);
// Verify "a" was removed, "b" and "c" remain
given()
.header("X-Amz-Target", "DynamoDB_20120810.GetItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "DeleteSetTable",
"Key": {"pk": {"S": "k1"}}
}
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("Item.tags.SS.size()", equalTo(2))
.body("Item.tags.SS", hasItems("b", "c"))
.body("Item.tags.SS", not(hasItem("a")));
// DELETE remaining elements to verify attribute removal on empty set
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "DeleteSetTable",
"Key": {"pk": {"S": "k1"}},
"UpdateExpression": "DELETE tags :val",
"ExpressionAttributeValues": {":val": {"SS": ["b", "c"]}}
}
""")
.when()
.post("/")
.then()
.statusCode(200);
// Verify attribute is removed entirely (DynamoDB doesn't allow empty sets)
given()
.header("X-Amz-Target", "DynamoDB_20120810.GetItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "DeleteSetTable",
"Key": {"pk": {"S": "k1"}}
}
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("Item.tags", nullValue());
// Cleanup
given()
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{"TableName": "DeleteSetTable"}
""")
.when()
.post("/")
.then()
.statusCode(200);
}
@Test
@Order(30)
void deleteFromSetWithAddInSameExpression() {
// Create table
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "DeleteAddComboTable",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST"
}
""")
.when()
.post("/")
.then()
.statusCode(200);
// Put item with a String Set
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "DeleteAddComboTable",
"Item": {"pk": {"S": "k1"}, "tags": {"SS": ["a", "b"]}}
}
""")
.when()
.post("/")
.then()
.statusCode(200);
// Combined: ADD "c" then DELETE "a" in the same expression
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "DeleteAddComboTable",
"Key": {"pk": {"S": "k1"}},
"UpdateExpression": "ADD tags :toAdd DELETE tags :toRemove",
"ExpressionAttributeValues": {
":toAdd": {"SS": ["c"]},
":toRemove": {"SS": ["a"]}
}
}
""")
.when()
.post("/")
.then()
.statusCode(200);
// Verify: should have "b" and "c", not "a"
given()
.header("X-Amz-Target", "DynamoDB_20120810.GetItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "DeleteAddComboTable",
"Key": {"pk": {"S": "k1"}}
}
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("Item.tags.SS.size()", equalTo(2))
.body("Item.tags.SS", hasItems("b", "c"))
.body("Item.tags.SS", not(hasItem("a")));
// Cleanup
given()
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{"TableName": "DeleteAddComboTable"}
""")
.when()
.post("/")
.then()
.statusCode(200);
}
@Test
void unsupportedOperation() {
given()