mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
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:
1 parent
c67216065f
commit
b899f177e4
4 files changed
+357
-5
No files matched your search
@@ -985,7 +985,32 @@ public class DynamoDbService {
|
||||
|
||||
|
||||
// Resolve the value
|
||||
if (valuePart.startsWith("if_not_exists(")) {
|
||||
// Check for arithmetic expressions (operand + operand, operand - operand)
|
||||
// before handling individual expression types, since the left operand can be
|
||||
// a function like if_not_exists(...).
|
||||
int arithmeticIdx = findArithmeticOperator(valuePart);
|
||||
if (arithmeticIdx >= 0) {
|
||||
String leftExpr = valuePart.substring(0, arithmeticIdx).trim();
|
||||
char operator = valuePart.charAt(arithmeticIdx);
|
||||
String rightExpr = valuePart.substring(arithmeticIdx + 1).trim();
|
||||
JsonNode leftVal = evaluateSetExpr(item, leftExpr, exprAttrNames, exprAttrValues);
|
||||
JsonNode rightVal = evaluateSetExpr(item, rightExpr, exprAttrNames, exprAttrValues);
|
||||
if (leftVal == null || rightVal == null || !leftVal.has("N") || !rightVal.has("N")) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid UpdateExpression: Incorrect operand type for operator or function", 400);
|
||||
}
|
||||
try {
|
||||
java.math.BigDecimal left = new java.math.BigDecimal(leftVal.get("N").asText());
|
||||
java.math.BigDecimal right = new java.math.BigDecimal(rightVal.get("N").asText());
|
||||
java.math.BigDecimal result = (operator == '+') ? left.add(right) : left.subtract(right);
|
||||
ObjectNode numNode = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
|
||||
numNode.put("N", result.toPlainString());
|
||||
setValueAtPath(item, attrPath, numNode, exprAttrNames);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("ValidationException",
|
||||
"The parameter cannot be converted to a numeric value", 400);
|
||||
}
|
||||
} else if (valuePart.startsWith("if_not_exists(")) {
|
||||
// if_not_exists(attrRef, fallbackExpr) evaluates to:
|
||||
// attrRef's current value — when attrRef exists in the item
|
||||
// fallbackExpr — otherwise
|
||||
@@ -1062,19 +1087,19 @@ public class DynamoDbService {
|
||||
if (args.length == 2) {
|
||||
String checkAttr = resolveAttributeName(args[0].trim(), exprAttrNames);
|
||||
String fallbackExpr = args[1].trim();
|
||||
if (item.has(checkAttr)) {
|
||||
return item.get(checkAttr);
|
||||
if (hasValueAtPath(item, checkAttr, exprAttrNames)) {
|
||||
return getValueAtPath(item, checkAttr, exprAttrNames);
|
||||
} else if (fallbackExpr.startsWith(":") && exprAttrValues != null) {
|
||||
return exprAttrValues.get(fallbackExpr);
|
||||
} else {
|
||||
return item.get(resolveAttributeName(fallbackExpr, exprAttrNames));
|
||||
return getValueAtPath(item, resolveAttributeName(fallbackExpr, exprAttrNames), exprAttrNames);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
} else if (expr.startsWith(":") && exprAttrValues != null) {
|
||||
return exprAttrValues.get(expr);
|
||||
} else {
|
||||
return item.get(resolveAttributeName(expr, exprAttrNames));
|
||||
return getValueAtPath(item, resolveAttributeName(expr, exprAttrNames), exprAttrNames);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1534,6 +1559,28 @@ public class DynamoDbService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the index of an arithmetic operator (+ or -) that is outside
|
||||
* function parentheses. Returns -1 if none found.
|
||||
*/
|
||||
private int findArithmeticOperator(String expr) {
|
||||
int depth = 0;
|
||||
for (int i = 0; i < expr.length(); i++) {
|
||||
char c = expr.charAt(i);
|
||||
if (c == '(') {
|
||||
depth++;
|
||||
} else if (c == ')') {
|
||||
depth--;
|
||||
} else if (depth == 0 && (c == '+' || c == '-')) {
|
||||
// Ensure this is a binary operator, not a sign at the start or after '('
|
||||
if (i > 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private String[] extractFunctionArgs(String funcCall) {
|
||||
int open = funcCall.indexOf('(');
|
||||
int close = funcCall.lastIndexOf(')');
|
||||
|
||||
Reference in new issue
Block a user