fix(dynamodb): apply parenthesized and nested operands in an UpdateExpression (#8215)

DynamoDB reads an operand in parentheses like the bare operand, so
SET a = (:v) + (:w) adds two numbers and list_append((:l), :m) joins two
lists. It also reads the fallback of if_not_exists as an operand, so
if_not_exists(a, if_not_exists(b, :v)) gives the first of a, b and :v
that exists.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-13 15:52:40 -04:00
1 parent 44eb096ed5
commit 8c31942e84
2 files changed
+44 -35

No files matched your search

@@ -29,6 +29,8 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.ArgumentCaptor;
import java.io.ByteArrayInputStream;
@@ -4039,6 +4041,40 @@ class DynamoDbServiceTest {
assertEquals("10", stored.get("counter").get("N").asText());
}
@ParameterizedTest
@CsvSource(delimiter = '|', textBlock = """
SET a = (:v) + (:w) | a | {"N":"5"}
SET a = ((:v) + :w) | a | {"N":"5"}
SET a = :w - (:v) | a | {"N":"1"}
SET a = (n) + (:v) | a | {"N":"12"}
SET a = (k[0]) + (k[1]) | a | {"N":"9"}
SET a = (if_not_exists(n, :v)) + (:w) | a | {"N":"13"}
SET a = if_not_exists(z, if_not_exists(n, :v)) + :w | a | {"N":"13"}
SET a = if_not_exists((#n), :v) | a | {"N":"10"}
SET a = if_not_exists((z), (:v)) | a | {"N":"2"}
SET a = if_not_exists(z, (n)) | a | {"N":"10"}
SET a = if_not_exists(z, if_not_exists(n, :v)) | a | {"N":"10"}
SET a = if_not_exists(z, if_not_exists(y, :v)) | a | {"N":"2"}
SET a = if_not_exists(z, (if_not_exists((y), (:v)))) | a | {"N":"2"}
SET l = list_append((:l), (:m)) | l | {"L":[{"S":"x"},{"S":"y"}]}
SET l = list_append((if_not_exists(l, :l)), :m) | l | {"L":[{"S":"x"},{"S":"y"}]}
SET l = (list_append(:l, (:m))) | l | {"L":[{"S":"x"},{"S":"y"}]}
""")
void updateItemAppliesParenthesizedAndNestedOperands(String expression, String attribute, String expected)
throws IOException {
String region = "eu-west-1";
createUsersTable(region);
service.putItem("Users", (ObjectNode) mapper.readTree("""
{"userId":{"S":"u1"},"n":{"N":"10"},"k":{"L":[{"N":"4"},{"N":"5"}]}}"""), region);
ObjectNode names = mapper.createObjectNode().put("#n", "n");
ObjectNode values = (ObjectNode) mapper.readTree("""
{":v":{"N":"2"},":w":{"N":"3"},":l":{"L":[{"S":"x"}]},":m":{"L":[{"S":"y"}]}}""");
service.updateItem("Users", item("userId", "u1"), null, expression, names, values, null, region);
assertEquals(mapper.readTree(expected), service.getItem("Users", item("userId", "u1"), region).get(attribute));
}
@Test
void transactWriteItemsReplayWithSameTokenAndBodyIsNoOp() {
// ClientRequestToken contract: same token + same body → no re-application of writes.