fix(dynamoDB): support arithmetic in DynamoDB SET expressions (#480)

* fix: support arithmetic in DynamoDB SET expressions

SET expressions like `SET cnt = cnt + :inc` or
`SET cnt = if_not_exists(cnt, :start) + :inc` silently ignored
the arithmetic operator - only the left operand was assigned.

Add `findArithmeticOperator()` to locate `+`/`-` outside function
parentheses, and evaluate both operands via `evaluateSetExpr` with
BigDecimal arithmetic.

Closes #479

* fix: address review - path-aware evaluateSetExpr, proper error handling

- Make evaluateSetExpr use hasValueAtPath/getValueAtPath for nested
  path support (e.g. SET a.b = a.b + :inc)
- Throw ValidationException for non-numeric operands instead of
  silent no-op
- Catch NumberFormatException on BigDecimal parsing

* fix: add integration and compatibility tests for SET arithmetic

- Add DynamoDbIntegrationTest for if_not_exists + arithmetic (repro from #479)
- Add Java SDK compatibility test covering increment, consecutive
  increments, and subtraction
This commit is contained in:
Stas Burnashev authored and GitHub committed 2026-04-17 18:17:02 -05:00
1 parent c67216065f
commit b899f177e4
4 files changed
+357 -5

No files matched your search

@@ -1323,6 +1323,111 @@ class DynamoDbIntegrationTest {
.statusCode(200);
}
@Test
@Order(31)
void updateItemSetArithmeticIncrement() {
// Create table
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "ArithmeticTable",
"KeySchema": [{"AttributeName": "PK", "KeyType": "HASH"}],
"AttributeDefinitions": [{"AttributeName": "PK", "AttributeType": "S"}],
"BillingMode": "PAY_PER_REQUEST"
}
""")
.when()
.post("/")
.then()
.statusCode(200);
// First call: if_not_exists(counter, :start) + :inc → 60000001
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "ArithmeticTable",
"Key": {"PK": {"S": "LastId"}},
"UpdateExpression": "SET customerId = if_not_exists(customerId, :start) + :inc",
"ExpressionAttributeValues": {
":start": {"N": "60000000"},
":inc": {"N": "1"}
}
}
""")
.when()
.post("/")
.then()
.statusCode(200);
// Verify first increment
given()
.header("X-Amz-Target", "DynamoDB_20120810.GetItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "ArithmeticTable",
"Key": {"PK": {"S": "LastId"}}
}
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("Item.customerId.N", equalTo("60000001"));
// Second call: existing (60000001) + 1 → 60000002
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "ArithmeticTable",
"Key": {"PK": {"S": "LastId"}},
"UpdateExpression": "SET customerId = if_not_exists(customerId, :start) + :inc",
"ExpressionAttributeValues": {
":start": {"N": "60000000"},
":inc": {"N": "1"}
}
}
""")
.when()
.post("/")
.then()
.statusCode(200);
// Verify second increment
given()
.header("X-Amz-Target", "DynamoDB_20120810.GetItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "ArithmeticTable",
"Key": {"PK": {"S": "LastId"}}
}
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("Item.customerId.N", equalTo("60000002"));
// Cleanup
given()
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{"TableName": "ArithmeticTable"}
""")
.when()
.post("/")
.then()
.statusCode(200);
}
@Test
void unsupportedOperation() {
given()
@@ -557,6 +557,140 @@ class DynamoDbServiceTest {
"target should receive the fallback value when source is absent");
}
@Test
void updateItemSetArithmeticIncrement() {
createUsersTable();
// Put an item with counter = 100
ObjectNode existing = mapper.createObjectNode();
existing.set("userId", attributeValue("S", "u1"));
ObjectNode counterVal = mapper.createObjectNode();
counterVal.put("N", "100");
existing.set("counter", counterVal);
service.putItem("Users", existing);
ObjectNode key = item("userId", "u1");
ObjectNode exprValues = mapper.createObjectNode();
ObjectNode incVal = mapper.createObjectNode();
incVal.put("N", "1");
exprValues.set(":inc", incVal);
service.updateItem("Users", key, null,
"SET counter = counter + :inc",
null, exprValues, null);
JsonNode stored = service.getItem("Users", key);
assertNotNull(stored);
assertEquals("101", stored.get("counter").get("N").asText(),
"counter should be incremented from 100 to 101");
}
@Test
void updateItemSetArithmeticDecrement() {
createUsersTable();
ObjectNode existing = mapper.createObjectNode();
existing.set("userId", attributeValue("S", "u1"));
ObjectNode counterVal = mapper.createObjectNode();
counterVal.put("N", "50");
existing.set("counter", counterVal);
service.putItem("Users", existing);
ObjectNode key = item("userId", "u1");
ObjectNode exprValues = mapper.createObjectNode();
ObjectNode decVal = mapper.createObjectNode();
decVal.put("N", "3");
exprValues.set(":dec", decVal);
service.updateItem("Users", key, null,
"SET counter = counter - :dec",
null, exprValues, null);
JsonNode stored = service.getItem("Users", key);
assertNotNull(stored);
assertEquals("47", stored.get("counter").get("N").asText(),
"counter should be decremented from 50 to 47");
}
@Test
void updateItemSetIfNotExistsWithArithmeticOnNewItem() {
createUsersTable();
ObjectNode key = item("userId", "u1");
ObjectNode exprValues = mapper.createObjectNode();
ObjectNode startVal = mapper.createObjectNode();
startVal.put("N", "60000000");
ObjectNode incVal = mapper.createObjectNode();
incVal.put("N", "1");
exprValues.set(":start", startVal);
exprValues.set(":inc", incVal);
service.updateItem("Users", key, null,
"SET counter = if_not_exists(counter, :start) + :inc",
null, exprValues, null);
JsonNode stored = service.getItem("Users", key);
assertNotNull(stored);
assertEquals("60000001", stored.get("counter").get("N").asText(),
"counter should be if_not_exists default (60000000) + 1 = 60000001");
}
@Test
void updateItemSetIfNotExistsWithArithmeticOnExistingItem() {
createUsersTable();
ObjectNode existing = mapper.createObjectNode();
existing.set("userId", attributeValue("S", "u1"));
ObjectNode counterVal = mapper.createObjectNode();
counterVal.put("N", "60000005");
existing.set("counter", counterVal);
service.putItem("Users", existing);
ObjectNode key = item("userId", "u1");
ObjectNode exprValues = mapper.createObjectNode();
ObjectNode startVal = mapper.createObjectNode();
startVal.put("N", "60000000");
ObjectNode incVal = mapper.createObjectNode();
incVal.put("N", "1");
exprValues.set(":start", startVal);
exprValues.set(":inc", incVal);
service.updateItem("Users", key, null,
"SET counter = if_not_exists(counter, :start) + :inc",
null, exprValues, null);
JsonNode stored = service.getItem("Users", key);
assertNotNull(stored);
assertEquals("60000006", stored.get("counter").get("N").asText(),
"counter should be existing (60000005) + 1 = 60000006");
}
@Test
void updateItemSetArithmeticConsecutiveIncrements() {
createUsersTable();
ObjectNode key = item("userId", "u1");
ObjectNode exprValues = mapper.createObjectNode();
ObjectNode startVal = mapper.createObjectNode();
startVal.put("N", "0");
ObjectNode incVal = mapper.createObjectNode();
incVal.put("N", "1");
exprValues.set(":start", startVal);
exprValues.set(":inc", incVal);
// Three consecutive increments
for (int i = 0; i < 3; i++) {
service.updateItem("Users", key, null,
"SET counter = if_not_exists(counter, :start) + :inc",
null, exprValues, null);
}
JsonNode stored = service.getItem("Users", key);
assertNotNull(stored);
assertEquals("3", stored.get("counter").get("N").asText(),
"counter should be 3 after three increments starting from 0");
}
@Test
void scanWithBoolFilterExpression() {
createUsersTable();