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

@@ -3654,27 +3654,9 @@ public class DynamoDbService {
"The parameter cannot be converted to a numeric value", 400);
}
} else if (startsWithFunctionCall(valuePart, "if_not_exists")) {
// if_not_exists(attrRef, fallbackExpr) evaluates to:
// attrRef's current value — when attrRef exists in the item
// fallbackExpr — otherwise
// The result is always assigned to attrPath.
String[] args = extractFunctionArgs(valuePart);
if (args.length == 2) {
String checkAttr = resolveAttributeName(args[0].trim(), exprAttrNames);
String fallbackExpr = args[1].trim();
JsonNode resolved;
if (hasValueAtPath(snapshot, checkAttr, exprAttrNames)) {
// attrRef exists — evaluate to its current value
resolved = getValueAtPath(snapshot, checkAttr, exprAttrNames);
} else if (fallbackExpr.startsWith(":") && exprAttrValues != null) {
resolved = exprAttrValues.get(fallbackExpr);
} else {
// fallback is itself an attribute reference
resolved = getValueAtPath(snapshot, resolveAttributeName(fallbackExpr, exprAttrNames), exprAttrNames);
}
if (resolved != null) {
setValueAtPath(item, attrPath, resolved, exprAttrNames);
}
JsonNode resolved = evaluateSetExpr(snapshot, valuePart, exprAttrNames, exprAttrValues);
if (resolved != null) {
setValueAtPath(item, attrPath, resolved, exprAttrNames);
}
} else if (startsWithFunctionCall(valuePart.toLowerCase(), "list_append")) {
int open = valuePart.indexOf('(');
@@ -3729,13 +3711,7 @@ public class DynamoDbService {
return clause;
}
/**
* Strip balanced outer parentheses from a SET-action RHS expression, repeatedly,
* so that producers wrapping arithmetic or function calls in parens (e.g.
* "(c - :v)" or "((if_not_exists(c, :d) - :v))") parse identically to the
* unwrapped form. Rejects forms like "(a) - (b)" where the outer parens do
* not actually enclose the whole expression.
*/
/** Removes parentheses around the whole operand, so "(c - :v)" reads as "c - :v" and "(a) - (b)" stays. */
static String stripOuterParens(String expr) {
String s = expr.trim();
while (s.length() >= 2 && s.charAt(0) == '(' && s.charAt(s.length() - 1) == ')') {
@@ -3752,20 +3728,17 @@ public class DynamoDbService {
return s;
}
private JsonNode evaluateSetExpr(ObjectNode item, String expr,
private JsonNode evaluateSetExpr(ObjectNode item, String operand,
JsonNode exprAttrNames, JsonNode exprAttrValues) {
String expr = stripOuterParens(operand);
if (startsWithFunctionCall(expr.toLowerCase(), "if_not_exists")) {
String[] args = extractFunctionArgs(expr);
if (args.length == 2) {
String checkAttr = resolveAttributeName(args[0].trim(), exprAttrNames);
String fallbackExpr = args[1].trim();
String checkAttr = resolveAttributeName(stripOuterParens(args[0]), exprAttrNames);
if (hasValueAtPath(item, checkAttr, exprAttrNames)) {
return getValueAtPath(item, checkAttr, exprAttrNames);
} else if (fallbackExpr.startsWith(":") && exprAttrValues != null) {
return exprAttrValues.get(fallbackExpr);
} else {
return getValueAtPath(item, resolveAttributeName(fallbackExpr, exprAttrNames), exprAttrNames);
}
return evaluateSetExpr(item, args[1], exprAttrNames, exprAttrValues);
}
return null;
} else if (expr.startsWith(":") && exprAttrValues != null) {
@@ -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.