From 1ddc8a32f7523d2c35b861e1987e39b2ff264e26 Mon Sep 17 00:00:00 2001 From: GitMarco Date: Wed, 8 Apr 2026 06:37:22 +0200 Subject: [PATCH] fix: dynamodb support add function multiple values (#263) * Fixes DynamoDB support for adding values to a list for the SET ... ADD query/command * Revert "Fixes DynamoDB support for adding values to a list for the SET ... ADD query/command" * Fixes DynamoDB support for adding values to a list for the SET ... ADD query/command --- pom.xml | 2 +- .../services/dynamodb/DynamoDbService.java | 81 ++++++++++++++++++- .../dynamodb/DynamoDbServiceTest.java | 49 +++++++++++ 3 files changed, 127 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index b6ca60806f..78e0a917a9 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.hectorvent floci - 1.0.11 + 1.3.0 jar Floci diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java index 84f17157b6..f9fbc8000c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java @@ -1010,7 +1010,7 @@ public class DynamoDbService { } private String applyAddClause(ObjectNode item, String clause, - JsonNode exprAttrNames, JsonNode exprAttrValues) { + JsonNode exprAttrNames, JsonNode exprAttrValues) { while (!clause.isEmpty()) { String upper = clause.toUpperCase(); if (upper.startsWith("SET ") || upper.startsWith("REMOVE ") || upper.startsWith("DELETE ")) { @@ -1025,9 +1025,11 @@ public class DynamoDbService { String valuePlaceholder = parts[1].replaceAll(",.*", "").trim(); if (valuePlaceholder.startsWith(":") && exprAttrValues != null) { - JsonNode value = exprAttrValues.get(valuePlaceholder); - if (value != null) { - item.set(attrName, value); + JsonNode addValue = exprAttrValues.get(valuePlaceholder); + if (addValue != null) { + JsonNode existingValue = item.get(attrName); + JsonNode newValue = applyAddOperation(existingValue, addValue); + item.set(attrName, newValue); } } @@ -1043,6 +1045,77 @@ public class DynamoDbService { return clause; } + /** + * Implements DynamoDB ADD operation semantics: + * - For numbers (N): adds the value to the existing number, or sets it if attribute doesn't exist + * - For sets (SS, NS, BS): adds elements to the existing set, or creates the set if it doesn't exist + */ + private JsonNode applyAddOperation(JsonNode existingValue, JsonNode addValue) { + ObjectNode result = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode(); + + // Handle number addition + if (addValue.has("N")) { + String addNumStr = addValue.get("N").asText(); + if (existingValue == null || !existingValue.has("N")) { + // Attribute doesn't exist — set to the add value + return addValue; + } + // Add the numbers + String existingNumStr = existingValue.get("N").asText(); + try { + java.math.BigDecimal existingNum = new java.math.BigDecimal(existingNumStr); + java.math.BigDecimal addNum = new java.math.BigDecimal(addNumStr); + result.put("N", existingNum.add(addNum).toPlainString()); + return result; + } catch (NumberFormatException e) { + // Fall back to just setting the value + return addValue; + } + } + + // Handle string set (SS) addition + if (addValue.has("SS")) { + if (existingValue == null || !existingValue.has("SS")) { + return addValue; + } + java.util.Set combined = new java.util.LinkedHashSet<>(); + existingValue.get("SS").forEach(n -> combined.add(n.asText())); + addValue.get("SS").forEach(n -> combined.add(n.asText())); + var arrayNode = result.putArray("SS"); + combined.forEach(arrayNode::add); + return result; + } + + // Handle number set (NS) addition + if (addValue.has("NS")) { + if (existingValue == null || !existingValue.has("NS")) { + return addValue; + } + java.util.Set combined = new java.util.LinkedHashSet<>(); + existingValue.get("NS").forEach(n -> combined.add(n.asText())); + addValue.get("NS").forEach(n -> combined.add(n.asText())); + var arrayNode = result.putArray("NS"); + combined.forEach(arrayNode::add); + return result; + } + + // Handle binary set (BS) addition + if (addValue.has("BS")) { + if (existingValue == null || !existingValue.has("BS")) { + return addValue; + } + java.util.Set combined = new java.util.LinkedHashSet<>(); + existingValue.get("BS").forEach(n -> combined.add(n.asText())); + addValue.get("BS").forEach(n -> combined.add(n.asText())); + var arrayNode = result.putArray("BS"); + combined.forEach(arrayNode::add); + return result; + } + + // Unsupported type for ADD — just set the value + return addValue; + } + String resolveAttributeName(String nameOrPlaceholder, JsonNode exprAttrNames) { nameOrPlaceholder = trimBalancedOuterParens(nameOrPlaceholder).trim(); if (nameOrPlaceholder.startsWith("#") && exprAttrNames != null) { diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java index 03f1ae7282..d850159425 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java @@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.Arrays; import java.util.List; import static org.junit.jupiter.api.Assertions.*; @@ -764,4 +765,52 @@ class DynamoDbServiceTest { DynamoDbService.ScanResult result = service.scan("Users", "contains(values, :v)", null, exprValues, null, null, null); assertEquals(1, result.items().size(), "contains() on List with N elements should use type-aware numeric comparison"); } + + @Test + void updateItemSetAddsToStringSet() { + createOrdersTable(); + + ObjectNode key = item("customerId", "1", "orderId", "sort1"); + + ObjectNode exprValues = mapper.createObjectNode(); + ObjectNode priceVal = mapper.createObjectNode(); + priceVal.put("N", "100"); + + // Use SS (String Set) type for ADD operation + ObjectNode tagVal = mapper.createObjectNode(); + var tagArray = tagVal.putArray("SS"); + tagArray.add("a"); + exprValues.set(":val", priceVal); + exprValues.set(":newTag", tagVal); + + service.updateItem("Orders", key, null, + "SET price = if_not_exists(price, :val) ADD tags :newTag", + null, exprValues, null); + + // And add another tag to the same item, to verify that the ADD works on existing items as well + ObjectNode tagVal2 = mapper.createObjectNode(); + var tagArray2 = tagVal2.putArray("SS"); + tagArray2.add("b"); + exprValues.set(":newTag", tagVal2); + DynamoDbService.UpdateResult updateResult = service.updateItem("Orders", key, null, + "SET price = if_not_exists(price, :val) ADD tags :newTag", + null, exprValues, null); + + JsonNode stored = service.getItem("Orders", key); + assertNotNull(stored, "item should have been created"); + assertTrue(stored.has("tags"), "tags attribute must be present on item after ADD"); + + // Verify tags is a String Set (SS) with both values + JsonNode tagsNode = stored.get("tags"); + assertTrue(tagsNode.has("SS"), "tags should be of type SS (String Set)"); + JsonNode ssArray = tagsNode.get("SS"); + assertEquals(2, ssArray.size(), "tags should have 2 elements"); + + // Verify values from the SS array + java.util.Set tagValues = new java.util.HashSet<>(); + ssArray.forEach(node -> tagValues.add(node.asText())); + assertEquals(2, tagValues.size()); + assertTrue(tagValues.containsAll(Arrays.asList("a", "b"))); + } + }