From 82a3c1dbacd822ea604dd2619c4001697c5e0513 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Sat, 19 Sep 2026 11:04:16 +0900 Subject: [PATCH] feat(dynamodb): improve PartiQL grammar coverage (#3798) * feat(dynamodb): improve PartiQL grammar coverage An attribute reference is now a path, so a.b and a[0] work in SELECT columns, WHERE conditions and UPDATE SET and REMOVE targets. SELECT reports a path from its last named component on, so SELECT a.b[0] answers b[0]. Values take map, list and set literals, exponent numbers and a doubled single quote. An empty set, a mixed set and a set with a repeated member are refused in the AWS words. WHERE takes OR, NOT, parentheses, IN with a list literal, IS MISSING and IS NULL with their negations, and the contains, attribute_type and size functions. Each becomes the matching filter or condition expression, so the existing evaluator does the work. UPDATE SET takes a sum or difference, list_append, set_add and set_delete. A predicate beside the key equality becomes a condition on the write, so a false one answers ConditionalCheckFailedException and leaves the item. UPDATE is not an upsert and needs the key to hold an item already, while a DELETE on a key holding no item stays a no-op. A clause omitting a key attribute says so. RETURNING runs all four variants on UPDATE and ALL OLD * on DELETE. It answers one row on Items, and no row when the projection is empty. DELETE refuses the other three and ExecuteTransaction refuses the clause outright. SELECT takes ORDER BY on the key, pages the sorted rows, and refuses the shapes AWS refuses. A FROM path takes at most two components and no empty one. INSERT refuses an index qualifier as a parse error, UPDATE and DELETE refuse the operation on an index, and a keyed read filtering on an attribute the index does not project is refused. ExecuteTransaction runs a transaction of nothing but SELECT as a read, runs EXISTS as a condition check, refuses a mix of reads and writes, and no longer applies twice on a replayed ClientRequestToken. A BatchExecuteStatement member carries the short error code and echoes TableName when it reached its table. Two update engine bugs came out of this and are fixed for UpdateItem as well. list_append on a nested path wrote a new top-level attribute, and ADD or DELETE with an operand of another type replaced or ignored the stored value. * fix(dynamodb): read key equalities inside a grouped AND in PartiQL * fix(dynamodb): subtract without spaces and refuse trailing tokens in PartiQL * fix(dynamodb): refuse a negative list index in PartiQL * fix(dynamodb): refuse a non-string type in PartiQL attribute_type * fix(dynamodb): refuse a value that is not a set in PartiQL set_add and set_delete * fix(dynamodb): refuse duplicate members in a PartiQL bag * fix(dynamodb): check PartiQL parameters before reading the statement * fix(dynamodb): refuse two PartiQL reads of one item in a transaction * fix(dynamodb): read only by the key in a PartiQL transaction SELECT * fix(dynamodb): check unprojected attributes inside grouped PartiQL conditions * fix(dynamodb): refuse an ADD or DELETE operand type before reading the item * fix(dynamodb): skip line and block comments in PartiQL * fix(dynamodb): apply a sign to a PartiQL number literal * fix(dynamodb): refuse a parameter inside a PartiQL collection literal * test(dynamodb): read a parameter in IN on a non-key attribute * fix(dynamodb): refuse a PartiQL list index above the integer range * fix(dynamodb): refuse a PartiQL literal nested 32 levels deep * fix(dynamodb): refuse a non-number literal in PartiQL arithmetic * feat(dynamodb): read attribute_exists and attribute_not_exists in PartiQL * feat(dynamodb): compare a PartiQL condition with a boolean * fix(dynamodb): match nothing when ordering a value against another type * fix(dynamodb): read PartiQL key conditions spread over OR and IN like AWS * fix(dynamodb): read a PartiQL transaction or batch SELECT only by its key * fix(dynamodb): refuse two values for one key on a PartiQL write * fix(dynamodb): cancel a PartiQL transaction that names a missing table * fix(dynamodb): cancel a PartiQL transaction whose statement does not name one item * fix(dynamodb): refuse a write that leaves an item nested too deep * fix(dynamodb): refuse a PartiQL parameter only past the nesting limit AWS uses * chore(dynamodb): drop comments that restate the code or a test * perf(dynamodb): work out each PartiQL key branch once and cap how far it spreads * chore(dynamodb): share one first key value helper across PartiQL key checks * chore(dynamodb): throw the ADD or DELETE operand error and merge member loops * chore(dynamodb): track PartiQL literal depth in one place and share truth helpers * test(dynamodb): name the quoted table once and add statement and putItem overloads * fix(dynamodb): cap IN operands and decomposed reads in PartiQL --- .../dynamodb/DynamoDbAccessPathValidator.java | 2 +- .../DynamoDbAttributeValueValidator.java | 6 +- .../dynamodb/DynamoDbJsonHandler.java | 216 +++-- .../dynamodb/DynamoDbPartiQLHandler.java | 778 +++++++++++++--- .../dynamodb/DynamoDbPartiQLKeyPlan.java | 342 +++++++ .../dynamodb/DynamoDbPartiQLParser.java | 840 ++++++++++++++++-- .../services/dynamodb/DynamoDbService.java | 68 +- .../dynamodb/ExpressionEvaluator.java | 8 +- .../ItemNestingExceededException.java | 10 + ...namoDbAddDeleteOperandIntegrationTest.java | 115 +++ .../dynamodb/DynamoDbJsonHandlerTest.java | 37 +- ...artiQLDocumentedSyntaxIntegrationTest.java | 372 ++++++++ ...ynamoDbPartiQLEdgeCaseIntegrationTest.java | 732 +++++++++++++++ .../DynamoDbPartiQLIntegrationTest.java | 342 +++++++ .../dynamodb/DynamoDbPartiQLParserTest.java | 109 +++ .../dynamodb/ExpressionEvaluatorTest.java | 8 + 16 files changed, 3700 insertions(+), 285 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLKeyPlan.java create mode 100644 src/main/java/io/github/hectorvent/floci/services/dynamodb/ItemNestingExceededException.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAddDeleteOperandIntegrationTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLDocumentedSyntaxIntegrationTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLEdgeCaseIntegrationTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLIntegrationTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidator.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidator.java index 45a5d77d75..e512097e8c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidator.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidator.java @@ -306,7 +306,7 @@ final class DynamoDbAccessPathValidator { validateCompositeSortKeyConditions(sortKeys, sortKeyEqualities); } - private static String attributeType(TableDefinition table, String attribute) { + static String attributeType(TableDefinition table, String attribute) { List definitions = table.getAttributeDefinitions(); if (definitions == null) { throw validationException(KEY_TYPE_MISMATCH); diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAttributeValueValidator.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAttributeValueValidator.java index b2cb835ed0..81dfce134b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAttributeValueValidator.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAttributeValueValidator.java @@ -53,13 +53,11 @@ final class DynamoDbAttributeValueValidator { } } - // PartiQL reports the limit with this short wording, and a transaction turns it into a - // cancellation reason. static final String NESTING_EXCEEDED = "Nesting Levels have exceeded supported limits"; static void requireParameterNestingWithinLimit(JsonNode value) { - if (!valueNestingWithinLimit(value)) { - throw validationEx(NESTING_EXCEEDED); + if (depthOf(value) > MAX_NESTING_LEVELS + 1) { + throw validationEx(NESTING_EXCEEDED + ": Attributes in the item have nested levels beyond supported limit"); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java index b8995fbb33..03ae79aa88 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java @@ -15,6 +15,7 @@ import jakarta.inject.Inject; import jakarta.ws.rs.core.Response; import java.util.*; +import java.util.function.Supplier; /** * DynamoDB JSON protocol handler. @@ -676,6 +677,7 @@ public class DynamoDbJsonHandler { checkUnusedEan(exprAttrNames, hashTokens); Set colonTokensAll = extractColonTokens(updateExpression, conditionExpression); checkUnusedEav(exprAttrValues, colonTokensAll); + dynamoDbService.requireAddOrDeleteOperandTypes(updateExpression, exprAttrValues, true); } if (expectedUpd != null) { @@ -1696,6 +1698,8 @@ public class DynamoDbJsonHandler { op.get("ExpressionAttributeNames"), op.get("ExpressionAttributeValues")); DynamoDbAttributeValueValidator.requireNestingWithinLimit(op.get("Item"), false); + dynamoDbService.requireAddOrDeleteOperandTypes(op.path("UpdateExpression").textValue(), + op.get("ExpressionAttributeValues"), false); } } @@ -2674,15 +2678,32 @@ public class DynamoDbJsonHandler { if (stmts.isMissingNode() || !stmts.isArray() || stmts.isEmpty()) { throw new AwsException("ValidationException", "TransactStatements must not be empty", 400); } - List transactItems = new ArrayList<>(); try { - cancelOnTooDeepParameters(stmts); - for (JsonNode s : stmts) { - DynamoDbPartiQLParser.Stmt stmt = DynamoDbPartiQLParser.parse( - s.path("Statement").asText(), toPartiQLParams(s.path("Parameters"))); - transactItems.add(partiQLHandler.toTransactItem(stmt, region)); + List statements = new ArrayList<>(); + for (int i = 0; i < stmts.size(); i++) { + JsonNode s = stmts.get(i); + List parameters = toPartiQLParams(s.path("Parameters")); + statements.add(inTransactStatement(i, + () -> DynamoDbPartiQLParser.parse(s.path("Statement").asText(), parameters))); } - dynamoDbService.transactWriteItems(transactItems, region); + long reads = statements.stream().filter(DynamoDbPartiQLParser.Stmt.Select.class::isInstance).count(); + if (reads > 0 && reads < statements.size()) { + throw new AwsException("ValidationException", + "ExecuteTransaction API does not support both read and write operations in the same request.", 400); + } + if (reads == statements.size()) { + return executeTransactionReads(statements.stream() + .map(DynamoDbPartiQLParser.Stmt.Select.class::cast) + .toList(), region); + } + List members = new ArrayList<>(); + for (int i = 0; i < statements.size(); i++) { + DynamoDbPartiQLParser.Stmt stmt = statements.get(i); + members.add(inTransactStatement(i, () -> partiQLHandler.toTransactItem(stmt, region))); + } + List transactItems = cancelOnMemberReasons(members); + dynamoDbService.transactWriteItems(transactItems, region, + request.path("ClientRequestToken").asText(null), request); ObjectNode resp = objectMapper.createObjectNode(); resp.set("Responses", objectMapper.createArrayNode()); return Response.ok(resp).build(); @@ -2694,8 +2715,9 @@ public class DynamoDbJsonHandler { for (TransactionCanceledException.CancellationReason reason : e.getCancellationReasons()) { ObjectNode r = objectMapper.createObjectNode(); r.put("Code", reason.code().isEmpty() ? "None" : reason.code()); - r.put("Message", reason.code().isEmpty() ? "" - : reason.message() != null ? reason.message() : "The conditional request failed"); + if (!reason.code().isEmpty()) { + r.put("Message", reason.message() != null ? reason.message() : "The conditional request failed"); + } if (reason.item() != null) { r.set("Item", reason.item()); } @@ -2705,24 +2727,69 @@ public class DynamoDbJsonHandler { } } - // AWS cancels the transaction, with that statement's reason, when a parameter is too deep. - private void cancelOnTooDeepParameters(JsonNode stmts) { - var reasons = new ArrayList(); - var cancelled = false; - for (JsonNode s : stmts) { - var tooDeep = false; - for (var parameter : s.path("Parameters")) { - tooDeep |= !DynamoDbAttributeValueValidator.valueNestingWithinLimit(parameter); + private Response executeTransactionReads(List selects, String region) { + List members = new ArrayList<>(); + for (int i = 0; i < selects.size(); i++) { + DynamoDbPartiQLParser.Stmt.Select select = selects.get(i); + members.add(inTransactStatement(i, () -> partiQLHandler.toTransactGetItem(select, region))); + } + List getItems = cancelOnMemberReasons(members); + + List results = dynamoDbService.transactGetItems(getItems, region); + requireOneReadPerItem(getItems, region); + ArrayNode responses = objectMapper.createArrayNode(); + for (int i = 0; i < results.size(); i++) { + JsonNode item = partiQLHandler.projectSelected(selects.get(i), results.get(i)); + ObjectNode entry = objectMapper.createObjectNode(); + if (item != null) { + entry.set("Item", item); } - cancelled |= tooDeep; - reasons.add(tooDeep - ? new TransactionCanceledException.CancellationReason("ValidationError", null, - DynamoDbAttributeValueValidator.NESTING_EXCEEDED) - : new TransactionCanceledException.CancellationReason("", null)); + responses.add(entry); + } + ObjectNode resp = objectMapper.createObjectNode(); + resp.set("Responses", responses); + return Response.ok(resp).build(); + } + + private static List cancelOnMemberReasons(List members) { + List items = new ArrayList<>(); + List reasons = new ArrayList<>(); + boolean cancelled = false; + for (DynamoDbPartiQLHandler.TransactMember member : members) { + cancelled |= member.reason() != null; + items.add(member.item()); + reasons.add(member.reason() != null ? member.reason() : new TransactionCanceledException.CancellationReason("", null)); } if (cancelled) { throw new TransactionCanceledException(reasons); } + return items; + } + + private void requireOneReadPerItem(List getItems, String region) { + Set> items = new HashSet<>(); + for (JsonNode getItem : getItems) { + JsonNode get = getItem.path("Get"); + String tableName = get.path("TableName").asText(); + TableDefinition table = dynamoDbService.findTable(tableName, region).orElseThrow(); + String itemKey = dynamoDbService.buildItemKey(table, get.path("Key"), true); + if (!items.add(List.of(tableName, itemKey))) { + throw new AwsException("ValidationException", + "Transaction request cannot include multiple operations on one item", 400); + } + } + } + + private T inTransactStatement(int index, Supplier member) { + try { + return member.get(); + } catch (AwsException e) { + if (!"ValidationException".equals(e.getErrorCode())) { + throw e; + } + throw new AwsException("ValidationException", + "Validation failed in TransactStatements[" + index + "]: " + e.getMessage(), 400); + } } private Response handleBatchExecuteStatement(JsonNode request, String region) { @@ -2732,60 +2799,79 @@ public class DynamoDbJsonHandler { } ArrayNode responses = objectMapper.createArrayNode(); for (JsonNode s : stmts) { - try { - DynamoDbPartiQLParser.Stmt stmt = DynamoDbPartiQLParser.parse( - s.path("Statement").asText(), toPartiQLParams(s.path("Parameters"))); - if (stmt instanceof DynamoDbPartiQLParser.Stmt.Select select - && !batchSelectResolvesThroughPrimaryKey(select, region)) { - throw new AwsException("ValidationError", - "Select statements within BatchExecuteStatement must specify the primary key " - + "in the where clause.", 400); - } - JsonNode result = partiQLHandler.execute(stmt, PartiQLExecuteContext.builder() - .consistentRead(s.path("ConsistentRead").asBoolean(false)), region); - ObjectNode slot = objectMapper.createObjectNode(); - JsonNode firstItem = result.path("Items").path(0); - if (!firstItem.isMissingNode()) { - slot.set("Item", firstItem); - } - responses.add(slot); - } catch (AwsException e) { - ObjectNode slot = objectMapper.createObjectNode(); - ObjectNode err = objectMapper.createObjectNode(); - err.put("Code", e.getErrorCode()); - err.put("Message", e.getMessage()); - slot.set("Error", err); - responses.add(slot); - } + responses.add(batchMemberResponse(s, region)); } ObjectNode resp = objectMapper.createObjectNode(); resp.set("Responses", responses); return Response.ok(resp).build(); } + private ObjectNode batchMemberResponse(JsonNode statement, String region) { + ObjectNode slot = objectMapper.createObjectNode(); + String tableName = null; + try { + DynamoDbPartiQLParser.Stmt stmt = parsePartiQLStatement(statement); + tableName = stmt.table(); + if (stmt instanceof DynamoDbPartiQLParser.Stmt.Select select) { + requireBatchSelectReadsByKey(select, region); + } + JsonNode result = partiQLHandler.execute(stmt, PartiQLExecuteContext.builder() + .consistentRead(statement.path("ConsistentRead").asBoolean(false)), region); + JsonNode firstItem = result.path("Items").path(0); + if (!firstItem.isMissingNode()) { + slot.set("Item", firstItem); + } + slot.put("TableName", tableName); + } catch (AwsException e) { + ObjectNode err = objectMapper.createObjectNode(); + err.put("Code", batchMemberErrorCode(e.getErrorCode())); + err.put("Message", e.getMessage()); + slot.set("Error", err); + if (tableName != null && batchMemberReachedItsTable(e)) { + slot.put("TableName", tableName); + } + } + return slot; + } + + private static String batchMemberErrorCode(String errorCode) { + if ("ValidationException".equals(errorCode)) { + return "ValidationError"; + } + return errorCode.endsWith("Exception") + ? errorCode.substring(0, errorCode.length() - "Exception".length()) + : errorCode; + } + + private static boolean batchMemberReachedItsTable(AwsException e) { + return e instanceof ItemNestingExceededException + || "ConditionalCheckFailedException".equals(e.getErrorCode()) + || "DuplicateItemException".equals(e.getErrorCode()); + } + // BatchExecuteStatement only runs SELECT statements that resolve through // the table's primary key: index-qualified statements and partial-key // WHERE clauses are rejected per statement (characterised on real AWS, // eu-west-1, 2026-09-02). - private boolean batchSelectResolvesThroughPrimaryKey(DynamoDbPartiQLParser.Stmt.Select select, String region) { + private void requireBatchSelectReadsByKey(DynamoDbPartiQLParser.Stmt.Select select, String region) { if (select.index() != null) { - return false; + throw batchSelectNeedsKey(); } TableDefinition table = dynamoDbService.describeTable(select.table(), region); - String pkName = table.getPartitionKeyName(); - String skName = table.getSortKeyName(); - boolean pkEq = false; - boolean skEq = skName == null; - for (DynamoDbPartiQLParser.Cond c : select.where()) { - if (c instanceof DynamoDbPartiQLParser.Cond.Eq eq) { - if (eq.attr().equals(pkName)) { - pkEq = true; - } else if (skName != null && eq.attr().equals(skName)) { - skEq = true; - } - } + DynamoDbPartiQLHandler.requireReadsWithinLimit(table, select.where()); + if (!DynamoDbPartiQLHandler.namesOnlyTheKey(table, select.where())) { + throw batchSelectNeedsKey(); } - return pkEq && skEq; + } + + private static AwsException batchSelectNeedsKey() { + return new AwsException("ValidationException", + "Select statements within BatchExecuteStatement must specify the primary key in the where clause.", 400); + } + + private DynamoDbPartiQLParser.Stmt parsePartiQLStatement(JsonNode statement) { + return DynamoDbPartiQLParser.parse(statement.path("Statement").asText(), + toPartiQLParams(statement.path("Parameters"))); } private List toPartiQLParams(JsonNode node) { @@ -2793,7 +2879,11 @@ public class DynamoDbJsonHandler { return Collections.emptyList(); } List params = new ArrayList<>(); - node.forEach(params::add); + for (JsonNode parameter : node) { + DynamoDbAttributeValueValidator.requireParameterNestingWithinLimit(parameter); + checkAttrSets(parameter); + params.add(parameter); + } return params; } } diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java index 7f0b73fb89..84f03fb608 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.dynamodb.DynamoDbPartiQLParser.*; import io.github.hectorvent.floci.services.dynamodb.model.ConditionalCheckFailedException; +import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement; import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition; import java.nio.charset.StandardCharsets; @@ -15,6 +16,8 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.*; import java.util.Base64; +import java.util.function.Supplier; +import java.util.stream.Collectors; class DynamoDbPartiQLHandler { @@ -32,6 +35,8 @@ class DynamoDbPartiQLHandler { case Stmt.Insert s -> executeInsert(s, region); case Stmt.Update s -> executeUpdate(s, region); case Stmt.Delete s -> executeDelete(s, region); + case Stmt.Exists ignored -> throw new AwsException("ValidationException", + "EXISTS can only be used in ExecuteTransaction write requests.", 400); }; } @@ -56,9 +61,11 @@ class DynamoDbPartiQLHandler { // projects; the message preserves statement order (characterised on // real AWS, eu-west-1, 2026-09-02). An LSI read reaches the co-located // base item, so non-projected columns stay legal there. - if (accessPath.isGlobalSecondaryIndex() && !"ALL".equals(accessPath.projectionType())) { + if (accessPath.isGlobalSecondaryIndex() && !"ALL".equals(accessPath.projectionType()) + && !stmt.columns().isEmpty()) { Set projected = accessPath.projectedAttributeNames(table); List unprojected = stmt.columns().stream() + .map(Path::root) .filter(c -> !"*".equals(c)) .filter(c -> !projected.contains(c)) .toList(); @@ -70,84 +77,27 @@ class DynamoDbPartiQLHandler { } } - String pkName = accessPath.partitionKeyName(); - String skName = accessPath.sortKeyName(); + DynamoDbPartiQLKeyPlan keys = new DynamoDbPartiQLKeyPlan(stmt.where(), accessPath, table); + keys.requireKeyTypesMatchSchema(); + keys.requireNoOverlap(); + keys.requireReadsWithinLimit(); + requireFilterAttributesProjected(keys, accessPath, table); - Cond.Eq pkEq = null; - Cond skCond = null; - List filterConds = new ArrayList<>(); - - for (Cond c : stmt.where()) { - String attr = condAttr(c); - if (pkName.equals(attr)) { - if (c instanceof Cond.Eq eq) { - pkEq = eq; - } else { - filterConds.add(c); - } - } else if (skName != null && skName.equals(attr)) { - skCond = c; - } else { - filterConds.add(c); - } - } - - List items; - JsonNode lastEvaluatedKey = null; - - if (pkEq == null) { - // No equality on the selected source's partition key: AWS performs - // a full scan of the table or index and applies the remaining - // conditions as a filter (characterised on real AWS, eu-west-1, - // 2026-09-02). - ExprAttrBuilder eav = new ExprAttrBuilder(); - ExprAttrNameBuilder ean = new ExprAttrNameBuilder(); - List scanFilters = new ArrayList<>(filterConds); - if (skCond != null) { - scanFilters.add(skCond); - } - String fe = buildFe(scanFilters, eav, ean); - JsonNode exclusiveStartKey = decodeNextToken(ctx.nextToken(), ctx.tokenBinding()); - DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, table, accessPath, true); - DynamoDbService.ScanResult result = service.scan(stmt.table(), fe, - ean.isEmpty() ? null : ean.toNode(mapper), - eav.isEmpty() ? null : eav.toNode(mapper), - null, ctx.limit(), exclusiveStartKey, accessPath.indexName(), region); - items = result.items(); - lastEvaluatedKey = result.lastEvaluatedKey(); - } else if (accessPath.kind() == DynamoDbAccessPath.Kind.TABLE && skName == null - && skCond == null && filterConds.isEmpty()) { - ObjectNode key = mapper.createObjectNode(); - key.set(pkName, toTypedNode(pkEq.val())); - JsonNode item = service.getItem(stmt.table(), key, region); - items = item != null ? List.of(item) : Collections.emptyList(); - } else { - ExprAttrBuilder eav = new ExprAttrBuilder(); - ExprAttrNameBuilder ean = new ExprAttrNameBuilder(); - String kce = buildKce(pkEq, skCond, pkName, skName, eav, ean); - String fe = buildFe(filterConds, eav, ean); - JsonNode exclusiveStartKey = decodeNextToken(ctx.nextToken(), ctx.tokenBinding()); - DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, table, accessPath, false); - DynamoDbService.QueryResult result = service.query( - stmt.table(), null, eav.toNode(mapper), kce, fe, - ctx.limit(), null, accessPath.indexName(), exclusiveStartKey, - ean.isEmpty() ? null : ean.toNode(mapper), region); - items = result.items(); - lastEvaluatedKey = result.lastEvaluatedKey(); - } + Routing routing = routingOf(stmt.where(), keys, accessPath.partitionKeyName(), accessPath.sortKeyName()); + Supplier startKey = () -> decodeNextToken(ctx.nextToken(), ctx.tokenBinding()); + Page page = stmt.orderBy().isEmpty() + ? readPage(stmt, table, accessPath, routing, startKey, ctx.limit(), region) + : readOrdered(stmt, table, accessPath, routing, startKey.get(), ctx.limit(), region); + List items = page.items(); + JsonNode lastEvaluatedKey = page.lastEvaluatedKey(); // Apply column projection for non-* SELECT. Projects against the full // item: an LSI read can reach attributes outside the index projection // (characterised on real AWS, eu-west-1, 2026-09-02). if (!stmt.columns().isEmpty()) { - ExprAttrNameBuilder projEan = new ExprAttrNameBuilder(); - String proj = stmt.columns().stream() - .map(projEan::alias) - .reduce((a, b) -> a + "," + b) - .orElse(""); - ObjectNode eanNode = projEan.isEmpty() ? null : projEan.toNode(mapper); + List columns = selectedColumns(stmt); items = items.stream() - .map(item -> (JsonNode) ProjectionEvaluator.project(item, proj, eanNode)) + .map(item -> (JsonNode) selectColumns(item, columns)) .toList(); } @@ -171,6 +121,203 @@ class DynamoDbPartiQLHandler { return resp; } + private record Routing(Cond.Eq pkEq, Cond skCond, List filterConds, PVal partition) {} + + private static Routing routingOf(List where, DynamoDbPartiQLKeyPlan keys, String pkName, String skName) { + Optional partition = keys.singlePartition(); + if (partition.isEmpty()) { + return new Routing(null, null, where, null); + } + Cond.Eq pkEq = null; + Cond skCond = null; + List filterConds = new ArrayList<>(); + for (Cond c : where) { + if (c instanceof Cond.Eq eq && isKeyCondition(eq, pkName)) { + pkEq = eq; + } else if (skCond == null && DynamoDbPartiQLKeyPlan.isSortKeyRange(c, skName)) { + skCond = c; + } else if (DynamoDbPartiQLKeyPlan.attributePaths(c).map(Path::root) + .anyMatch(root -> root.equals(pkName) || root.equals(skName))) { + return new Routing(null, null, where, partition.get()); + } else { + filterConds.add(c); + } + } + return new Routing(pkEq, skCond, filterConds, null); + } + + private record Page(List items, JsonNode lastEvaluatedKey) {} + + private Page readPage(Stmt.Select stmt, TableDefinition table, DynamoDbAccessPath accessPath, + Routing routing, Supplier startKey, Integer limit, String region) { + if (routing.partition() != null) { + return readPartition(stmt, table, accessPath, routing, startKey, limit, region); + } + String pkName = accessPath.partitionKeyName(); + String skName = accessPath.sortKeyName(); + Cond.Eq pkEq = routing.pkEq(); + Cond skCond = routing.skCond(); + List filterConds = routing.filterConds(); + + if (pkEq == null) { + // No equality on the selected source's partition key: AWS performs + // a full scan of the table or index and applies the remaining + // conditions as a filter (characterised on real AWS, eu-west-1, + // 2026-09-02). + ExprAttrBuilder eav = new ExprAttrBuilder(); + ExprAttrNameBuilder ean = new ExprAttrNameBuilder(); + List scanFilters = new ArrayList<>(filterConds); + if (skCond != null) { + scanFilters.add(skCond); + } + String fe = buildFe(scanFilters, eav, ean); + JsonNode exclusiveStartKey = startKey.get(); + DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, table, accessPath, true); + DynamoDbService.ScanResult result = service.scan(stmt.table(), fe, + ean.isEmpty() ? null : ean.toNode(mapper), + eav.isEmpty() ? null : eav.toNode(mapper), + null, limit, exclusiveStartKey, accessPath.indexName(), region); + return new Page(result.items(), result.lastEvaluatedKey()); + } + if (accessPath.kind() == DynamoDbAccessPath.Kind.TABLE && skName == null + && skCond == null && filterConds.isEmpty()) { + ObjectNode key = mapper.createObjectNode(); + key.set(pkName, toTypedNode(pkEq.val())); + JsonNode item = service.getItem(stmt.table(), key, region); + return new Page(item != null ? List.of(item) : Collections.emptyList(), null); + } + ExprAttrBuilder eav = new ExprAttrBuilder(); + ExprAttrNameBuilder ean = new ExprAttrNameBuilder(); + String kce = buildKce(pkEq, skCond, pkName, skName, eav, ean); + String fe = buildFe(filterConds, eav, ean); + JsonNode exclusiveStartKey = startKey.get(); + DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, table, accessPath, false); + DynamoDbService.QueryResult result = service.query( + stmt.table(), null, eav.toNode(mapper), kce, fe, + limit, null, accessPath.indexName(), exclusiveStartKey, + ean.isEmpty() ? null : ean.toNode(mapper), region); + return new Page(result.items(), result.lastEvaluatedKey()); + } + + private Page readPartition(Stmt.Select stmt, TableDefinition table, DynamoDbAccessPath accessPath, + Routing routing, Supplier startKey, Integer limit, String region) { + ExprAttrBuilder eav = new ExprAttrBuilder(); + ExprAttrNameBuilder ean = new ExprAttrNameBuilder(); + String kce = ean.alias(accessPath.partitionKeyName()) + " = " + eav.add(toTypedNode(routing.partition())); + JsonNode exclusiveStartKey = startKey.get(); + DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, table, accessPath, false); + DynamoDbService.QueryResult result = service.query(stmt.table(), null, eav.toNode(mapper), kce, null, + limit, null, accessPath.indexName(), exclusiveStartKey, ean.toNode(mapper), region); + ExprAttrBuilder filterValues = new ExprAttrBuilder(); + ExprAttrNameBuilder filterNames = new ExprAttrNameBuilder(); + ExpressionEvaluator.Expr filter = ExpressionEvaluator.parse(buildFe(routing.filterConds(), filterValues, filterNames)); + JsonNode names = filterNames.isEmpty() ? null : filterNames.toNode(mapper); + JsonNode values = filterValues.isEmpty() ? null : filterValues.toNode(mapper); + List matching = result.items().stream() + .filter(item -> ExpressionEvaluator.evaluate(filter, item, names, values)) + .toList(); + return new Page(matching, result.lastEvaluatedKey()); + } + + private Page readOrdered(Stmt.Select stmt, TableDefinition table, DynamoDbAccessPath accessPath, + Routing routing, JsonNode after, Integer limit, String region) { + requireOrderByShape(stmt, accessPath); + List rows = new ArrayList<>(); + JsonNode lastKey = null; + do { + JsonNode from = lastKey; + Page page = readPage(stmt, table, accessPath, routing, () -> from, null, region); + rows.addAll(page.items()); + lastKey = page.lastEvaluatedKey(); + } while (lastKey != null); + + Comparator order = orderByComparator(stmt.orderBy(), accessPath, table); + rows.sort(order); + int start = 0; + if (after != null) { + while (start < rows.size() && order.compare(rows.get(start), after) <= 0) { + start++; + } + } + int end = limit == null ? rows.size() : Math.min(rows.size(), start + limit); + JsonNode resumeAfter = start < end && end < rows.size() ? orderingKey(rows.get(end - 1), accessPath, table) : null; + return new Page(new ArrayList<>(rows.subList(start, end)), resumeAfter); + } + + private static void requireOrderByShape(Stmt.Select stmt, DynamoDbAccessPath accessPath) { + if (stmt.where().isEmpty()) { + throw new AwsException("ValidationException", + "Must have WHERE clause in the statement when using ORDER BY clause.", 400); + } + Set keys = accessPath.keyAttributeNames(); + for (OrderTerm term : stmt.orderBy()) { + if (!keys.contains(term.attribute())) { + throw new AwsException("ValidationException", + "Attribute " + term.attribute() + " in ORDER BY clause must be part of the primary key", 400); + } + } + String pkName = accessPath.partitionKeyName(); + int hashKeyConditions = stmt.where().stream().mapToInt(c -> hashKeyConditionCount(c, pkName)).sum(); + if (hashKeyConditions == 0) { + throw new AwsException("ValidationException", + "Must have at least one non-optional hash key condition in WHERE clause when using ORDER BY clause.", + 400); + } + boolean ordersByHashKey = stmt.orderBy().stream().anyMatch(term -> term.attribute().equals(pkName)); + if (hashKeyConditions > 1 && !ordersByHashKey) { + throw new AwsException("ValidationException", + "Must have hash key in ORDER BY clause when more than one hash key condition specified in WHERE clause.", + 400); + } + } + + private static int hashKeyConditionCount(Cond cond, String pkName) { + return switch (cond) { + case Cond.Eq eq when isKeyCondition(eq, pkName) -> 1; + case Cond.In in when isKeyCondition(in, pkName) -> in.values().size(); + case Cond.Or or when or.operands().stream() + .allMatch(o -> o instanceof Cond.Eq && isKeyCondition(o, pkName)) -> or.operands().size(); + default -> 0; + }; + } + + private static Comparator orderByComparator(List terms, DynamoDbAccessPath accessPath, + TableDefinition table) { + String pkName = accessPath.partitionKeyName(); + String skName = accessPath.sortKeyName(); + boolean pkDescending = directionOf(terms, pkName).orElse(false); + Comparator order = byAttribute(pkName, pkDescending); + if (skName != null) { + order = order.thenComparing(byAttribute(skName, directionOf(terms, skName).orElse(true))); + } + for (String tableKey : List.of(table.getPartitionKeyName(), Objects.toString(table.getSortKeyName(), ""))) { + if (!tableKey.isEmpty() && !tableKey.equals(pkName) && !tableKey.equals(skName)) { + order = order.thenComparing(byAttribute(tableKey, false)); + } + } + return order; + } + + private static Optional directionOf(List terms, String attribute) { + return terms.stream().filter(term -> term.attribute().equals(attribute)).findFirst().map(OrderTerm::descending); + } + + private static Comparator byAttribute(String attribute, boolean descending) { + Comparator ascending = (a, b) -> ExpressionEvaluator.compareAttributeValues(a.path(attribute), b.path(attribute)); + return descending ? ascending.reversed() : ascending; + } + + private JsonNode orderingKey(JsonNode row, DynamoDbAccessPath accessPath, TableDefinition table) { + ObjectNode key = mapper.createObjectNode(); + Set names = new LinkedHashSet<>(accessPath.keyAttributeNames()); + names.add(table.getPartitionKeyName()); + if (table.getSortKeyName() != null) { + names.add(table.getSortKeyName()); + } + names.forEach(name -> key.set(name, row.get(name))); + return key; + } + // NextToken is opaque to the client, so it carries a digest binding it to // the statement text and parameter values that minted it. Real AWS rejects // a token replayed against any other statement, table or access path with @@ -239,22 +386,89 @@ class DynamoDbPartiQLHandler { private String buildCondExpr(Cond c, ExprAttrBuilder eav, ExprAttrNameBuilder ean) { return switch (c) { - case Cond.Eq eq -> ean.alias(eq.attr()) + " = " + eav.add(toTypedNode(eq.val())); - case Cond.Cmp cmp -> ean.alias(cmp.attr()) + " " + cmp.op() + " " + eav.add(toTypedNode(cmp.val())); + case Cond.Eq eq -> ean.alias(eq.path()) + " = " + eav.add(toTypedNode(eq.val())); + case Cond.Cmp cmp -> ean.alias(cmp.path()) + " " + cmp.op() + " " + eav.add(toTypedNode(cmp.val())); case Cond.Between b -> - ean.alias(b.attr()) + " BETWEEN " + eav.add(toTypedNode(b.lo())) + " AND " + eav.add(toTypedNode(b.hi())); + ean.alias(b.path()) + " BETWEEN " + eav.add(toTypedNode(b.lo())) + " AND " + eav.add(toTypedNode(b.hi())); case Cond.BeginsWith bw -> - "begins_with(" + ean.alias(bw.attr()) + ", " + eav.add(toTypedNode(bw.prefix())) + ")"; + "begins_with(" + ean.alias(bw.path()) + ", " + eav.add(toTypedNode(bw.prefix())) + ")"; + case Cond.In in -> ean.alias(in.path()) + " IN (" + in.values().stream() + .map(v -> eav.add(toTypedNode(v))) + .collect(Collectors.joining(", ")) + ")"; + case Cond.Missing m -> + (m.negated() ? "attribute_exists(" : "attribute_not_exists(") + ean.alias(m.path()) + ")"; + case Cond.Contains ct -> + "contains(" + ean.alias(ct.path()) + ", " + eav.add(toTypedNode(ct.operand())) + ")"; + case Cond.AttributeType at -> + "attribute_type(" + ean.alias(at.path()) + ", " + eav.add(toTypedNode(at.type())) + ")"; + case Cond.SizeCmp sc -> + "size(" + ean.alias(sc.path()) + ") " + sc.op() + " " + eav.add(toTypedNode(sc.val())); + case Cond.IsNull isNull -> (isNull.negated() ? "NOT " : "") + "attribute_type(" + ean.alias(isNull.path()) + + ", " + eav.add(mapper.createObjectNode().put("S", "NULL")) + ")"; + case Cond.And and -> joinOperands(and.operands(), " AND ", eav, ean); + case Cond.Or or -> joinOperands(or.operands(), " OR ", eav, ean); + case Cond.Not not -> "NOT (" + buildCondExpr(not.operand(), eav, ean) + ")"; }; } - private static String condAttr(Cond c) { - return switch (c) { - case Cond.Eq eq -> eq.attr(); - case Cond.Cmp cmp -> cmp.attr(); - case Cond.Between b -> b.attr(); - case Cond.BeginsWith bw -> bw.attr(); - }; + private String joinOperands(List operands, String separator, + ExprAttrBuilder eav, ExprAttrNameBuilder ean) { + return "(" + operands.stream() + .map(operand -> buildCondExpr(operand, eav, ean)) + .collect(Collectors.joining(separator)) + ")"; + } + + private static void requireFilterAttributesProjected(DynamoDbPartiQLKeyPlan keys, DynamoDbAccessPath accessPath, + TableDefinition table) { + if (!accessPath.isIndex() || "ALL".equals(accessPath.projectionType())) { + return; + } + List unprojected = keys.unprojectedFilterAttributes(accessPath.projectedAttributeNames(table)); + if (!unprojected.isEmpty()) { + throw new AwsException("ValidationException", + "One or more parameter values were invalid: Secondary index " + + accessPath.indexName() + + " does not project one or more filter attributes: [" + + String.join(", ", unprojected) + "]", 400); + } + } + + private static boolean isKeyCondition(Cond cond, String keyName) { + return cond.bareAttribute().filter(keyName::equals).isPresent(); + } + + private record SelectedColumn(Path path, String reportedName) {} + + private static List selectedColumns(Stmt.Select stmt) { + return stmt.columns().stream() + .map(column -> new SelectedColumn(column, column.leafName())) + .toList(); + } + + private ObjectNode selectColumns(JsonNode item, List columns) { + ObjectNode projected = mapper.createObjectNode(); + for (SelectedColumn column : columns) { + JsonNode value = resolvePath(item, column.path()); + if (value != null && !value.isMissingNode()) { + projected.set(column.reportedName(), value); + } + } + return projected; + } + + private JsonNode resolvePath(JsonNode item, Path path) { + List segments = path.segments(); + JsonNode current = item.get(((Seg.Name) segments.getFirst()).value()); + for (int i = 1; i < segments.size() && current != null; i++) { + current = switch (segments.get(i)) { + case Seg.Name name -> current.path("M").get(name.value()); + case Seg.Index index -> { + JsonNode list = current.path("L"); + yield index.value() < list.size() ? list.get((int) index.value()) : null; + } + }; + } + return current; } // --- INSERT --- @@ -271,54 +485,108 @@ class DynamoDbPartiQLHandler { } catch (ConditionalCheckFailedException e) { throw new AwsException("DuplicateItemException", "Duplicate primary key exists in table", 400); } - return emptyItemsResponse(); + return itemsResponse(null); } // --- UPDATE --- private JsonNode executeUpdate(Stmt.Update stmt, String region) { + requireNoIndexQualifier(stmt.index()); TableDefinition table = service.describeTable(stmt.table(), region); + requireOneValuePerKey(table, stmt.where()); ObjectNode key = buildKey(table, stmt.where()); ExprAttrBuilder eav = new ExprAttrBuilder(); ExprAttrNameBuilder ean = new ExprAttrNameBuilder(); - StringBuilder ue = new StringBuilder(); - if (!stmt.sets().isEmpty()) { - ue.append("SET "); - List parts = new ArrayList<>(); - for (Assign a : stmt.sets()) { - parts.add(ean.alias(a.attr()) + " = " + eav.add(toTypedNode(a.val()))); - } - ue.append(String.join(", ", parts)); - } - if (!stmt.removes().isEmpty()) { - if (!ue.isEmpty()) ue.append(" "); - ue.append("REMOVE "); - ue.append(stmt.removes().stream().map(ean::alias).reduce((a, b) -> a + ", " + b).orElse("")); - } + String ue = buildUpdateExpression(stmt.sets(), stmt.removes(), eav, ean); + String ce = updateCondition(table, stmt.where(), eav, ean); + String returnValues = stmt.returning().returnValues(); - service.updateItem(stmt.table(), key, null, ue.toString(), + DynamoDbService.UpdateResult result = service.updateItem(stmt.table(), key, null, ue, ean.isEmpty() ? null : ean.toNode(mapper), - eav.isEmpty() ? null : eav.toNode(mapper), "NONE", region); - return emptyItemsResponse(); + eav.isEmpty() ? null : eav.toNode(mapper), returnValues, ce, region, "NONE"); + return itemsResponse(returnedAttributes(stmt.returning(), result)); + } + + private static void requireNoIndexQualifier(String index) { + if (index != null) { + throw new AwsException("ValidationException", + "This operation is not supported on an index", 400); + } + } + + private static Returning returningOf(Stmt stmt) { + return switch (stmt) { + case Stmt.Update upd -> upd.returning(); + case Stmt.Delete del -> del.returning(); + case Stmt.Select ignored -> Returning.NONE; + case Stmt.Insert ignored -> Returning.NONE; + case Stmt.Exists ignored -> Returning.NONE; + }; + } + + private JsonNode returnedAttributes(Returning returning, DynamoDbService.UpdateResult result) { + return switch (returning) { + case NONE -> null; + case ALL_OLD -> result.oldItem(); + case ALL_NEW -> result.newItem(); + case MODIFIED_OLD -> DynamoDbUpdatedAttributes.collect(result.touched(), false); + case MODIFIED_NEW -> DynamoDbUpdatedAttributes.collect(result.touched(), true); + }; } // --- DELETE --- private JsonNode executeDelete(Stmt.Delete stmt, String region) { + requireNoIndexQualifier(stmt.index()); TableDefinition table = service.describeTable(stmt.table(), region); + requireOneValuePerKey(table, stmt.where()); ObjectNode key = buildKey(table, stmt.where()); - service.deleteItem(stmt.table(), key, null, null, null, region, "NONE"); - return emptyItemsResponse(); + + ExprAttrBuilder eav = new ExprAttrBuilder(); + ExprAttrNameBuilder ean = new ExprAttrNameBuilder(); + String ce = deleteCondition(table, stmt.where(), eav, ean); + + JsonNode oldItem = service.deleteItem(stmt.table(), key, ce, + ean.isEmpty() ? null : ean.toNode(mapper), + eav.isEmpty() ? null : eav.toNode(mapper), region, "NONE"); + return itemsResponse(stmt.returning() == Returning.ALL_OLD ? oldItem : null); } // --- Transaction item builder --- - JsonNode toTransactItem(Stmt stmt, String region) { + record TransactMember(JsonNode item, TransactionCanceledException.CancellationReason reason) { + + static final TransactMember TABLE_NOT_FOUND = cancels("ResourceNotFound", "Requested resource not found"); + static final TransactMember KEY_MISMATCH = cancels("ValidationError", "The provided key element does not match the schema"); + + static TransactMember cancels(String code, String message) { + return new TransactMember(null, new TransactionCanceledException.CancellationReason(code, null, message)); + } + } + + TransactMember toTransactItem(Stmt stmt, String region) { + requireNoIndexQualifier(switch (stmt) { + case Stmt.Update upd -> upd.index(); + case Stmt.Delete del -> del.index(); + default -> null; + }); + Optional found = service.findTable(stmt.table(), region); + if (found.isEmpty()) { + return TransactMember.TABLE_NOT_FOUND; + } + TableDefinition table = found.get(); + if (returningOf(stmt) != Returning.NONE) { + throw new AwsException("ValidationException", + "RETURNING clause is not supported in ExecuteTransaction.", 400); + } + Optional keyProblem = keyProblemOf(stmt, table); + if (keyProblem.isPresent()) { + return keyProblem.get(); + } ObjectNode txItem = mapper.createObjectNode(); switch (stmt) { case Stmt.Insert ins -> { - TableDefinition table = service.describeTable(ins.table(), region); String pkName = table.getPartitionKeyName(); ObjectNode item = mapper.createObjectNode(); ins.item().forEach((k, v) -> item.set(k, toTypedNode(v))); @@ -329,67 +597,265 @@ class DynamoDbPartiQLHandler { txItem.set("Put", put); } case Stmt.Update upd -> { - TableDefinition table = service.describeTable(upd.table(), region); ObjectNode key = buildKey(table, upd.where()); ExprAttrBuilder eav = new ExprAttrBuilder(); ExprAttrNameBuilder ean = new ExprAttrNameBuilder(); - StringBuilder ue = new StringBuilder(); - if (!upd.sets().isEmpty()) { - ue.append("SET "); - List parts = new ArrayList<>(); - for (Assign a : upd.sets()) { - parts.add(ean.alias(a.attr()) + " = " + eav.add(toTypedNode(a.val()))); - } - ue.append(String.join(", ", parts)); - } - if (!upd.removes().isEmpty()) { - if (!ue.isEmpty()) ue.append(" "); - ue.append("REMOVE "); - ue.append(upd.removes().stream().map(ean::alias).reduce((a, b) -> a + ", " + b).orElse("")); - } + String ue = buildUpdateExpression(upd.sets(), upd.removes(), eav, ean); ObjectNode update = mapper.createObjectNode(); update.put("TableName", upd.table()); update.set("Key", key); - update.put("UpdateExpression", ue.toString()); + update.put("UpdateExpression", ue); + update.put("ConditionExpression", updateCondition(table, upd.where(), eav, ean)); if (!eav.isEmpty()) update.set("ExpressionAttributeValues", eav.toNode(mapper)); if (!ean.isEmpty()) update.set("ExpressionAttributeNames", ean.toNode(mapper)); txItem.set("Update", update); } case Stmt.Delete del -> { - TableDefinition table = service.describeTable(del.table(), region); ObjectNode key = buildKey(table, del.where()); + ExprAttrBuilder eav = new ExprAttrBuilder(); + ExprAttrNameBuilder ean = new ExprAttrNameBuilder(); + String ce = deleteCondition(table, del.where(), eav, ean); ObjectNode delete = mapper.createObjectNode(); delete.put("TableName", del.table()); delete.set("Key", key); + if (ce != null) { + delete.put("ConditionExpression", ce); + } + if (!eav.isEmpty()) { + delete.set("ExpressionAttributeValues", eav.toNode(mapper)); + } + if (!ean.isEmpty()) { + delete.set("ExpressionAttributeNames", ean.toNode(mapper)); + } txItem.set("Delete", delete); } case Stmt.Select ignored -> throw new AwsException("ValidationException", "SELECT is not supported inside ExecuteTransaction", 400); + case Stmt.Exists exists -> txItem.set("ConditionCheck", conditionCheck(exists.select(), table)); } - return txItem; + return new TransactMember(txItem, null); + } + + private ObjectNode conditionCheck(Stmt.Select select, TableDefinition table) { + List conditions = nonKeyConditions(table, select.where()); + if (!pinsFullKey(table, select.where()) || conditions.isEmpty()) { + throw new AwsException("ValidationException", + "EXISTS() must contain a single item read with additional condition", 400); + } + ExprAttrBuilder eav = new ExprAttrBuilder(); + ExprAttrNameBuilder ean = new ExprAttrNameBuilder(); + ObjectNode check = mapper.createObjectNode(); + check.put("TableName", select.table()); + check.set("Key", buildKey(table, select.where())); + check.put("ConditionExpression", joinOperands(conditions, " AND ", eav, ean)); + if (!eav.isEmpty()) { + check.set("ExpressionAttributeValues", eav.toNode(mapper)); + } + if (!ean.isEmpty()) { + check.set("ExpressionAttributeNames", ean.toNode(mapper)); + } + return check; + } + + private static Optional keyProblemOf(Stmt stmt, TableDefinition table) { + return switch (stmt) { + case Stmt.Insert ins -> table.getKeySchema().stream() + .map(KeySchemaElement::getAttributeName) + .filter(name -> !ins.item().containsKey(name)) + .findFirst() + .map(name -> TransactMember.cancels("ValidationError", + "One or more parameter values were invalid: Missing the key " + name + " in the item")); + case Stmt.Update upd -> wrongKey(table, upd.where()); + case Stmt.Delete del -> wrongKey(table, del.where()); + default -> Optional.empty(); + }; + } + + private static Optional wrongKey(TableDefinition table, List where) { + boolean namesOneItem = secondKeyValue(table, where).isEmpty() && pinsFullKey(table, where) + && keyTypesMatch(table, where); + return namesOneItem ? Optional.empty() : Optional.of(TransactMember.KEY_MISMATCH); + } + + private static boolean keyTypesMatch(TableDefinition table, List where) { + return firstKeyValues(table, where).entrySet().stream() + .allMatch(key -> DynamoDbPartiQLKeyPlan.matchesKeyType(table, key.getKey(), key.getValue())); + } + + TransactMember toTransactGetItem(Stmt.Select stmt, String region) { + Optional found = service.findTable(stmt.table(), region); + if (found.isEmpty()) { + return TransactMember.TABLE_NOT_FOUND; + } + TableDefinition table = found.get(); + if (stmt.index() != null) { + throw new AwsException("ValidationException", + "Reads on indices are not supported within transactions.", 400); + } + new DynamoDbPartiQLKeyPlan(stmt.where(), DynamoDbAccessPath.resolve(table, null), table).requireNoOverlap(); + if (!namesOnlyTheKey(table, stmt.where())) { + throw new AwsException("ValidationException", + "Select statements within ExecuteTransaction must specify the primary key in the where clause.", 400); + } + List equalities = asEqualities(stmt.where()); + if (!keyTypesMatch(table, equalities)) { + return TransactMember.KEY_MISMATCH; + } + ObjectNode get = mapper.createObjectNode(); + get.put("TableName", stmt.table()); + get.set("Key", buildKey(table, equalities)); + ObjectNode txItem = mapper.createObjectNode(); + txItem.set("Get", get); + return new TransactMember(txItem, null); + } + + JsonNode projectSelected(Stmt.Select stmt, JsonNode item) { + if (item == null || stmt.columns().isEmpty()) { + return item; + } + return selectColumns(item, selectedColumns(stmt)); } // --- Shared helpers --- - private ObjectNode buildKey(TableDefinition table, List where) { - String pkName = table.getPartitionKeyName(); - String skName = table.getSortKeyName(); - ObjectNode key = mapper.createObjectNode(); - for (Cond c : where) { - if (c instanceof Cond.Eq eq) { - if (pkName.equals(eq.attr()) || (skName != null && skName.equals(eq.attr()))) { - key.set(eq.attr(), toTypedNode(eq.val())); - } + private String buildUpdateExpression(List sets, List removes, + ExprAttrBuilder eav, ExprAttrNameBuilder ean) { + List assignments = new ArrayList<>(); + List adds = new ArrayList<>(); + List deletes = new ArrayList<>(); + for (SetClause clause : sets) { + switch (clause) { + case Assign assign -> assignments.add(assignExpr(assign, eav, ean)); + case SetAdd add -> adds.add(ean.alias(add.path()) + " " + eav.add(toTypedNode(add.bag()))); + case SetDelete delete -> deletes.add(ean.alias(delete.path()) + " " + eav.add(toTypedNode(delete.bag()))); } } - if (!key.has(pkName)) { - throw new AwsException("ValidationException", - "WHERE clause must include an equality condition on the partition key", 400); + List clauses = new ArrayList<>(); + if (!assignments.isEmpty()) { + clauses.add("SET " + String.join(", ", assignments)); } + if (!removes.isEmpty()) { + clauses.add("REMOVE " + removes.stream() + .map(ean::alias) + .collect(Collectors.joining(", "))); + } + if (!adds.isEmpty()) { + clauses.add("ADD " + String.join(", ", adds)); + } + if (!deletes.isEmpty()) { + clauses.add("DELETE " + String.join(", ", deletes)); + } + return String.join(" ", clauses); + } + + private String assignExpr(Assign assign, ExprAttrBuilder eav, ExprAttrNameBuilder ean) { + String assignment = ean.alias(assign.path()) + " = " + operandExpr(assign.left(), eav, ean); + if (assign.op() == null) { + return assignment; + } + return assignment + " " + assign.op() + " " + operandExpr(assign.right(), eav, ean); + } + + private String operandExpr(Operand operand, ExprAttrBuilder eav, ExprAttrNameBuilder ean) { + return switch (operand) { + case Operand.Value value -> eav.add(toTypedNode(value.val())); + case Operand.Attribute attribute -> ean.alias(attribute.path()); + case Operand.ListAppend append -> "list_append(" + operandExpr(append.first(), eav, ean) + + ", " + operandExpr(append.second(), eav, ean) + ")"; + }; + } + + private ObjectNode buildKey(TableDefinition table, List where) { + if (!pinsFullKey(table, where)) { + throw new AwsException("ValidationException", + "Where clause does not contain a mandatory equality on all key attributes", 400); + } + ObjectNode key = mapper.createObjectNode(); + firstKeyValues(table, where).forEach((name, value) -> key.set(name, toTypedNode(value))); return key; } + private static boolean pinsFullKey(TableDefinition table, List where) { + return firstKeyValues(table, where).size() == keyAttributeNames(table).size(); + } + + static void requireReadsWithinLimit(TableDefinition table, List where) { + new DynamoDbPartiQLKeyPlan(where, DynamoDbAccessPath.resolve(table, null), table).requireReadsWithinLimit(); + } + + static boolean namesOnlyTheKey(TableDefinition table, List where) { + List equalities = asEqualities(where); + return nonKeyConditions(table, equalities).isEmpty() && pinsFullKey(table, equalities); + } + + private static List asEqualities(List where) { + return where.stream() + .map(c -> c instanceof Cond.In in && in.values().size() == 1 ? new Cond.Eq(in.path(), in.values().getFirst()) : c) + .toList(); + } + + private static Set keyAttributeNames(TableDefinition table) { + return DynamoDbAccessPath.resolve(table, null).keyAttributeNames(); + } + + private static boolean isKeyEquality(Cond cond, Set keyNames) { + return cond instanceof Cond.Eq eq && eq.bareAttribute().filter(keyNames::contains).isPresent(); + } + + private static Map firstKeyValues(TableDefinition table, List where) { + Set keyNames = keyAttributeNames(table); + Map firstValues = new LinkedHashMap<>(); + for (Cond c : where) { + if (c instanceof Cond.Eq eq && isKeyEquality(eq, keyNames)) { + firstValues.putIfAbsent(eq.path().root(), eq.val()); + } + } + return firstValues; + } + + private static Optional secondKeyValue(TableDefinition table, List where) { + Map firstValues = firstKeyValues(table, where); + return where.stream() + .filter(c -> c instanceof Cond.Eq eq + && eq.bareAttribute().map(firstValues::get).filter(first -> !first.equals(eq.val())).isPresent()) + .map(Cond.Eq.class::cast) + .findFirst(); + } + + private static List nonKeyConditions(TableDefinition table, List where) { + Set keyNames = keyAttributeNames(table); + Map firstValues = firstKeyValues(table, where); + return where.stream() + .filter(c -> !(c instanceof Cond.Eq eq && isKeyEquality(eq, keyNames) + && firstValues.get(eq.path().root()).equals(eq.val()))) + .toList(); + } + + private static void requireOneValuePerKey(TableDefinition table, List where) { + secondKeyValue(table, where).ifPresent(eq -> { + throw new AwsException("ValidationException", "Multiple conditions on same key " + eq.path().root() + + ". Only single item Update/Insert/Delete are supported", 400); + }); + } + + private String updateCondition(TableDefinition table, List where, + ExprAttrBuilder eav, ExprAttrNameBuilder ean) { + String exists = "attribute_exists(" + ean.alias(table.getPartitionKeyName()) + ")"; + List filters = nonKeyConditions(table, where); + return filters.isEmpty() ? exists : exists + " AND " + joinOperands(filters, " AND ", eav, ean); + } + + private String deleteCondition(TableDefinition table, List where, + ExprAttrBuilder eav, ExprAttrNameBuilder ean) { + List filters = nonKeyConditions(table, where); + if (filters.isEmpty()) { + return null; + } + return "attribute_not_exists(" + ean.alias(table.getPartitionKeyName()) + ") OR " + + joinOperands(filters, " AND ", eav, ean); + } + JsonNode toTypedNode(PVal val) { return switch (val) { case PVal.Str s -> mapper.createObjectNode().put("S", s.v()); @@ -397,12 +863,34 @@ class DynamoDbPartiQLHandler { case PVal.Bool b -> mapper.createObjectNode().put("BOOL", b.v()); case PVal.Null ignored -> mapper.createObjectNode().put("NULL", true); case PVal.Av av -> av.node(); + case PVal.ListOf list -> { + ObjectNode node = mapper.createObjectNode(); + ArrayNode items = node.putArray("L"); + list.items().forEach(item -> items.add(toTypedNode(item))); + yield node; + } + case PVal.Tuple tuple -> { + ObjectNode node = mapper.createObjectNode(); + ObjectNode fields = node.putObject("M"); + tuple.fields().forEach((name, value) -> fields.set(name, toTypedNode(value))); + yield node; + } + case PVal.Bag bag -> { + ObjectNode node = mapper.createObjectNode(); + ArrayNode members = node.putArray(bag.type()); + bag.members().forEach(member -> members.add(toTypedNode(member).path(bag.type().substring(0, 1)))); + yield node; + } }; } - private ObjectNode emptyItemsResponse() { + private ObjectNode itemsResponse(JsonNode returned) { ObjectNode resp = mapper.createObjectNode(); - resp.set("Items", mapper.createArrayNode()); + ArrayNode items = mapper.createArrayNode(); + if (returned != null && !returned.isEmpty()) { + items.add(returned); + } + resp.set("Items", items); return resp; } @@ -440,6 +928,22 @@ class DynamoDbPartiQLHandler { }); } + String alias(Path path) { + StringBuilder expression = new StringBuilder(); + for (Seg segment : path.segments()) { + switch (segment) { + case Seg.Name name -> { + if (!expression.isEmpty()) { + expression.append('.'); + } + expression.append(alias(name.value())); + } + case Seg.Index index -> expression.append('[').append(index.value()).append(']'); + } + } + return expression.toString(); + } + boolean isEmpty() { return aliasToName.isEmpty(); } ObjectNode toNode(ObjectMapper mapper) { diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLKeyPlan.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLKeyPlan.java new file mode 100644 index 0000000000..3bddca8e33 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLKeyPlan.java @@ -0,0 +1,342 @@ +package io.github.hectorvent.floci.services.dynamodb; + +import io.github.hectorvent.floci.services.dynamodb.DynamoDbPartiQLParser.Cond; +import io.github.hectorvent.floci.services.dynamodb.DynamoDbPartiQLParser.PVal; +import io.github.hectorvent.floci.services.dynamodb.DynamoDbPartiQLParser.Path; +import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition; + +import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Base64; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.TreeMap; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +final class DynamoDbPartiQLKeyPlan { + + private static final int MAX_BRANCHES = 1000; + private static final int MAX_DECOMPOSED_READS = 50; + + private final String partitionKey; + private final String sortKey; + private final TableDefinition table; + private final boolean everyBranchPinsPartition; + private final List keyConditions = new ArrayList<>(); + private final List branches; + private boolean tooManyBranches; + + private record Spread(List keyConditions, List filters) { + + static final Spread EMPTY = new Spread(List.of(), List.of()); + + Spread and(Spread other) { + return new Spread(Stream.concat(keyConditions.stream(), other.keyConditions.stream()).toList(), + Stream.concat(filters.stream(), other.filters.stream()).toList()); + } + } + + private record Branch(Key partition, PVal partitionValue, Range sortRange, List filters) {} + + DynamoDbPartiQLKeyPlan(List where, DynamoDbAccessPath accessPath, TableDefinition table) { + this.partitionKey = accessPath.partitionKeyName(); + this.sortKey = accessPath.sortKeyName(); + this.table = table; + Cond conditions = new Cond.And(where); + this.everyBranchPinsPartition = everyBranchPinsPartition(conditions); + List spread = spread(conditions); + this.branches = tooManyBranches + ? List.of(new Branch(null, null, Range.ALL, where)) + : spread.stream().map(this::branchOf).toList(); + } + + void requireKeyTypesMatchSchema() { + for (Cond.Leaf condition : keyConditions) { + String name = condition.path().root(); + for (PVal value : values(condition)) { + String type = DynamoDbPartiQLParser.typeCode(value); + if (!DynamoDbPartiQLParser.ORDERED_TYPES.contains(type)) { + throw DynamoDbPartiQLParser.validationEx( + "Key value must be of type S, N, or B. Key name: " + name + ", Key type: " + type); + } + if (!matchesKeyType(table, name, value)) { + throw DynamoDbPartiQLParser.validationEx( + "Key attribute's data type should match its data type in table's schema: Key " + name); + } + } + } + } + + static boolean matchesKeyType(TableDefinition table, String keyName, PVal value) { + return DynamoDbAccessPathValidator.attributeType(table, keyName).equals(DynamoDbPartiQLParser.typeCode(value)); + } + + void requireReadsWithinLimit() { + if (everyBranchPinsPartition && (tooManyBranches || branches.size() > MAX_DECOMPOSED_READS)) { + throw DynamoDbPartiQLParser.validationEx("Too many decomposed read operations for a given query."); + } + } + + private boolean everyBranchPinsPartition(Cond cond) { + return switch (cond) { + case Cond.Leaf leaf -> pinsPartition(leaf); + case Cond.And and -> and.operands().stream().anyMatch(this::everyBranchPinsPartition); + case Cond.Or or -> or.operands().stream().allMatch(this::everyBranchPinsPartition); + case Cond.Not ignored -> false; + }; + } + + void requireNoOverlap() { + if (!keyed()) { + return; + } + Map> rangesByPartition = new TreeMap<>(); + for (Branch branch : branches) { + rangesByPartition.computeIfAbsent(branch.partition(), ignored -> new ArrayList<>()).add(branch.sortRange()); + } + for (List ranges : rangesByPartition.values()) { + Range reach = null; + for (Range range : ranges.stream().filter(range -> !range.isEmpty()).sorted(Range.BY_LOWER).toList()) { + if (reach != null && range.startsBefore(reach)) { + throw DynamoDbPartiQLParser.validationEx( + "Overlapping conditions with range keys are not supported in where clause"); + } + if (reach == null || range.endsAfter(reach)) { + reach = range; + } + } + } + } + + List unprojectedFilterAttributes(Set projected) { + if (!keyed()) { + return List.of(); + } + Set last = Set.of(); + for (Branch branch : branches.stream().sorted(Comparator.comparing(Branch::partition)).toList()) { + Set names = branch.filters().stream() + .flatMap(DynamoDbPartiQLKeyPlan::attributePaths) + .map(Path::root) + .filter(root -> !projected.contains(root)) + .collect(Collectors.toCollection(HashSet::new)); + if (names.isEmpty()) { + return List.of(); + } + last = names; + } + return List.copyOf(last); + } + + Optional singlePartition() { + Key first = branches.getFirst().partition(); + if (!keyed() || branches.stream().anyMatch(branch -> branch.partition().compareTo(first) != 0)) { + return Optional.empty(); + } + return Optional.of(branches.getFirst().partitionValue()); + } + + static Stream attributePaths(Cond cond) { + return DynamoDbPartiQLParser.leaves(cond).map(Cond.Leaf::path); + } + + static boolean isSortKeyRange(Cond cond, String sortKey) { + if (sortKey == null || cond.bareAttribute().filter(sortKey::equals).isEmpty()) { + return false; + } + return cond instanceof Cond.Cmp cmp ? !"<>".equals(cmp.op()) + : cond instanceof Cond.Eq || cond instanceof Cond.Between || cond instanceof Cond.BeginsWith; + } + + private boolean keyed() { + return branches.stream().allMatch(branch -> branch.partition() != null); + } + + private Branch branchOf(Spread spread) { + Key partition = null; + PVal partitionValue = null; + boolean conflicting = false; + Range sortRange = Range.ALL; + for (Cond.Leaf condition : spread.keyConditions()) { + if (condition.path().root().equals(partitionKey)) { + PVal value = ((Cond.Eq) condition).val(); + Key key = Key.of(value); + conflicting |= partition != null && partition.compareTo(key) != 0; + if (partition == null) { + partition = key; + partitionValue = value; + } + } else { + sortRange = sortRange.intersect(rangeOf(condition)); + } + } + return new Branch(conflicting ? null : partition, partitionValue, sortRange, spread.filters()); + } + + private List spread(Cond cond) { + return switch (cond) { + case Cond.In in when isKeyCondition(in) -> { + keyConditions.add(in); + yield in.values().stream() + .map(value -> new Spread(List.of(new Cond.Eq(in.path(), value)), List.of())) + .toList(); + } + case Cond.Leaf leaf when isKeyCondition(leaf) -> { + keyConditions.add(leaf); + yield List.of(new Spread(List.of(leaf), List.of())); + } + case Cond.And and -> { + List spread = List.of(Spread.EMPTY); + for (Cond operand : and.operands()) { + spread = and(spread, spread(operand)); + } + yield spread; + } + case Cond.Or or when namesKey(or) -> { + List spread = or.operands().stream().flatMap(operand -> spread(operand).stream()).toList(); + tooManyBranches |= spread.size() > MAX_BRANCHES; + yield spread; + } + default -> List.of(new Spread(List.of(), List.of(cond))); + }; + } + + private List and(List left, List right) { + if ((long) left.size() * right.size() > MAX_BRANCHES) { + tooManyBranches = true; + return left; + } + return left.stream().flatMap(l -> right.stream().map(l::and)).toList(); + } + + private boolean namesKey(Cond cond) { + return switch (cond) { + case Cond.Leaf leaf -> isKeyCondition(leaf); + case Cond.And and -> and.operands().stream().anyMatch(this::namesKey); + case Cond.Or or -> or.operands().stream().anyMatch(this::namesKey); + case Cond.Not ignored -> false; + }; + } + + private boolean pinsPartition(Cond.Leaf leaf) { + return leaf.bareAttribute().filter(partitionKey::equals).isPresent() + && (leaf instanceof Cond.Eq || leaf instanceof Cond.In); + } + + private boolean isKeyCondition(Cond.Leaf leaf) { + if (leaf.bareAttribute().filter(partitionKey::equals).isPresent()) { + return pinsPartition(leaf); + } + return isSortKeyRange(leaf, sortKey) + || (leaf instanceof Cond.In && leaf.bareAttribute().filter(name -> name.equals(sortKey)).isPresent()); + } + + private static List values(Cond.Leaf condition) { + return switch (condition) { + case Cond.Eq eq -> List.of(eq.val()); + case Cond.Cmp cmp -> List.of(cmp.val()); + case Cond.Between between -> List.of(between.lo(), between.hi()); + case Cond.BeginsWith beginsWith -> List.of(beginsWith.prefix()); + case Cond.In in -> in.values(); + default -> List.of(); + }; + } + + private static Range rangeOf(Cond.Leaf condition) { + return switch (condition) { + case Cond.Eq eq -> new Range(Key.of(eq.val()), true, Key.of(eq.val()), true); + case Cond.Between between -> new Range(Key.of(between.lo()), true, Key.of(between.hi()), true); + case Cond.BeginsWith beginsWith -> new Range(Key.of(beginsWith.prefix()), true, prefixEnd(beginsWith.prefix()), false); + case Cond.Cmp cmp -> switch (cmp.op()) { + case "<" -> new Range(null, false, Key.of(cmp.val()), false); + case "<=" -> new Range(null, false, Key.of(cmp.val()), true); + case ">" -> new Range(Key.of(cmp.val()), false, null, false); + default -> new Range(Key.of(cmp.val()), true, null, false); + }; + default -> Range.ALL; + }; + } + + private static Key prefixEnd(PVal prefix) { + byte[] bytes = Key.of(prefix).bytes(); + for (int i = bytes.length - 1; i >= 0; i--) { + if (bytes[i] != (byte) 0xFF) { + byte[] end = Arrays.copyOf(bytes, i + 1); + end[i]++; + return new Key(null, end); + } + } + return null; + } + + private record Key(BigDecimal number, byte[] bytes) implements Comparable { + + static Key of(PVal value) { + return switch (value) { + case PVal.Num number -> new Key(new BigDecimal(number.v()), "N".getBytes(StandardCharsets.UTF_8)); + case PVal.Str text -> new Key(null, text.v().getBytes(StandardCharsets.UTF_8)); + case PVal.Av av when av.node().has("B") -> new Key(null, Base64.getDecoder().decode(av.node().get("B").asText())); + default -> new Key(null, DynamoDbPartiQLParser.typeCode(value).getBytes(StandardCharsets.UTF_8)); + }; + } + + @Override + public int compareTo(Key other) { + if (number != null && other.number != null) { + return number.compareTo(other.number); + } + return Arrays.compareUnsigned(bytes, other.bytes); + } + } + + private record Range(Key lower, boolean lowerInclusive, Key upper, boolean upperInclusive) { + + static final Range ALL = new Range(null, false, null, false); + + static final Comparator BY_LOWER = Comparator + .comparing(Range::lower, Comparator.nullsFirst(Comparator.naturalOrder())) + .thenComparing(range -> !range.lowerInclusive()); + + Range intersect(Range other) { + boolean raisesLower = other.lower != null && (lower == null || other.lower.compareTo(lower) > 0 + || (other.lower.compareTo(lower) == 0 && !other.lowerInclusive)); + boolean dropsUpper = other.upper != null && (upper == null || other.upper.compareTo(upper) < 0 + || (other.upper.compareTo(upper) == 0 && !other.upperInclusive)); + return new Range(raisesLower ? other.lower : lower, raisesLower ? other.lowerInclusive : lowerInclusive, + dropsUpper ? other.upper : upper, dropsUpper ? other.upperInclusive : upperInclusive); + } + + boolean isEmpty() { + if (lower == null || upper == null) { + return false; + } + int order = lower.compareTo(upper); + return order > 0 || (order == 0 && !(lowerInclusive && upperInclusive)); + } + + boolean startsBefore(Range reach) { + if (reach.upper == null || lower == null) { + return true; + } + int order = lower.compareTo(reach.upper); + return order < 0 || (order == 0 && lowerInclusive && reach.upperInclusive); + } + + boolean endsAfter(Range reach) { + if (reach.upper == null) { + return false; + } + if (upper == null) { + return true; + } + int order = upper.compareTo(reach.upper); + return order > 0 || (order == 0 && upperInclusive && !reach.upperInclusive); + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParser.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParser.java index a8aa444e06..6c8feeff5b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParser.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParser.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode; import io.github.hectorvent.floci.core.common.AwsException; import java.util.*; +import java.util.stream.Stream; public class DynamoDbPartiQLParser { @@ -13,19 +14,98 @@ public class DynamoDbPartiQLParser { IDENT, STRING, NUMBER, BOOL, NULL, QUESTION, EQ, NE, LT, LE, GT, GE, LPAREN, RPAREN, LBRACE, RBRACE, COMMA, COLON, DOT, + OR, NOT, IN, IS, MISSING, RETURNING, ALL, MODIFIED, OLD, NEW, + PLUS, MINUS, LBRACKET, RBRACKET, + LBAG, RBAG, SEMICOLON, + ORDER, BY, ASC, DESC, EOF } - record Token(TType type, String value) {} + record Token(TType type, String value, int start) { + Token(TType type, String value) { + this(type, value, -1); + } + } // --- AST node types --- - public sealed interface Stmt permits Stmt.Select, Stmt.Insert, Stmt.Update, Stmt.Delete { + public sealed interface Seg permits Seg.Name, Seg.Index { + record Name(String value) implements Seg {} + record Index(long value) implements Seg {} + } + + public record Path(List segments) { + + public String root() { + return ((Seg.Name) segments.getFirst()).value(); + } + + public boolean isRootOnly() { + return segments.size() == 1; + } + + String leafName() { + int last = segments.size() - 1; + while (segments.get(last) instanceof Seg.Index) { + last--; + } + StringBuilder name = new StringBuilder(((Seg.Name) segments.get(last)).value()); + for (Seg trailing : segments.subList(last + 1, segments.size())) { + name.append('[').append(((Seg.Index) trailing).value()).append(']'); + } + return name.toString(); + } + } + + public enum Returning { + NONE("NONE"), + ALL_OLD("ALL_OLD"), + ALL_NEW("ALL_NEW"), + MODIFIED_OLD("UPDATED_OLD"), + MODIFIED_NEW("UPDATED_NEW"); + + private final String returnValues; + + Returning(String returnValues) { + this.returnValues = returnValues; + } + + String returnValues() { + return returnValues; + } + + String clause() { + return "RETURNING " + name().replace('_', ' ') + " *"; + } + } + + record OrderTerm(String attribute, boolean descending) {} + + public sealed interface Stmt permits Stmt.Select, Stmt.Insert, Stmt.Update, Stmt.Delete, Stmt.Exists { String table(); - record Select(String table, String index, List columns, List where) implements Stmt {} - record Insert(String table, Map item) implements Stmt {} - record Update(String table, List sets, List removes, List where) implements Stmt {} - record Delete(String table, List where) implements Stmt {} + List where(); + record Select(String table, String index, List columns, List where, + List orderBy) implements Stmt {} + record Insert(String table, Map item) implements Stmt { + @Override + public List where() { + return List.of(); + } + } + record Update(String table, String index, List sets, List removes, + List where, Returning returning) implements Stmt {} + record Delete(String table, String index, List where, Returning returning) implements Stmt {} + record Exists(Select select) implements Stmt { + @Override + public String table() { + return select.table(); + } + + @Override + public List where() { + return select.where(); + } + } } /** @@ -58,23 +138,63 @@ public class DynamoDbPartiQLParser { } } - sealed interface PVal permits PVal.Str, PVal.Num, PVal.Bool, PVal.Null, PVal.Av { + sealed interface PVal permits PVal.Str, PVal.Num, PVal.Bool, PVal.Null, PVal.Av, + PVal.ListOf, PVal.Tuple, PVal.Bag { record Str(String v) implements PVal {} record Num(String v) implements PVal {} record Bool(boolean v) implements PVal {} record Null() implements PVal {} // A parameter of a type with no literal syntax, kept as its wire node. record Av(String type, JsonNode node) implements PVal {} + record ListOf(List items) implements PVal {} + record Tuple(Map fields) implements PVal {} + record Bag(String type, List members) implements PVal {} } - sealed interface Cond permits Cond.Eq, Cond.Cmp, Cond.Between, Cond.BeginsWith { - record Eq(String attr, PVal val) implements Cond {} - record Cmp(String attr, String op, PVal val) implements Cond {} - record Between(String attr, PVal lo, PVal hi) implements Cond {} - record BeginsWith(String attr, PVal prefix) implements Cond {} + sealed interface Cond permits Cond.Leaf, Cond.And, Cond.Or, Cond.Not { + + default Optional bareAttribute() { + return Optional.empty(); + } + + sealed interface Leaf extends Cond permits Eq, Cmp, Between, BeginsWith, In, Missing, + Contains, AttributeType, SizeCmp, IsNull { + Path path(); + + @Override + default Optional bareAttribute() { + return path().isRootOnly() ? Optional.of(path().root()) : Optional.empty(); + } + } + + record Eq(Path path, PVal val) implements Leaf {} + record Cmp(Path path, String op, PVal val) implements Leaf {} + record Between(Path path, PVal lo, PVal hi) implements Leaf {} + record BeginsWith(Path path, PVal prefix) implements Leaf {} + record In(Path path, List values) implements Leaf {} + record Missing(Path path, boolean negated) implements Leaf {} + record Contains(Path path, PVal operand) implements Leaf {} + record AttributeType(Path path, PVal type) implements Leaf {} + record SizeCmp(Path path, String op, PVal val) implements Leaf {} + record IsNull(Path path, boolean negated) implements Leaf {} + record And(List operands) implements Cond {} + record Or(List operands) implements Cond {} + record Not(Cond operand) implements Cond {} } - record Assign(String attr, PVal val) {} + sealed interface Operand permits Operand.Value, Operand.Attribute, Operand.ListAppend { + record Value(PVal val) implements Operand {} + record Attribute(Path path) implements Operand {} + record ListAppend(Operand first, Operand second) implements Operand {} + } + + sealed interface SetClause permits Assign, SetAdd, SetDelete { + Path path(); + } + + record Assign(Path path, Operand left, String op, Operand right) implements SetClause {} + record SetAdd(Path path, PVal bag) implements SetClause {} + record SetDelete(Path path, PVal bag) implements SetClause {} // --- Tokenizer --- @@ -84,23 +204,42 @@ public class DynamoDbPartiQLParser { while (i < n) { char c = input.charAt(i); if (Character.isWhitespace(c)) { i++; continue; } + if (input.startsWith("--", i)) { + while (i < n && input.charAt(i) != '\n' && input.charAt(i) != '\r') i++; + continue; + } + if (input.startsWith("/*", i)) { + int end = input.indexOf("*/", i + 2); + if (end < 0) { + throw validationEx("Statement wasn't well formed, can't be processed: "); + } + i = end + 2; + continue; + } if (c == '\'') { - int start = ++i; - while (i < n && input.charAt(i) != '\'') i++; - tokens.add(new Token(TType.STRING, input.substring(start, i))); + StringBuilder text = new StringBuilder(); + i++; + while (i < n && (input.charAt(i) != '\'' || (i + 1 < n && input.charAt(i + 1) == '\''))) { + text.append(input.charAt(i)); + i += input.charAt(i) == '\'' ? 2 : 1; + } + tokens.add(new Token(TType.STRING, text.toString())); i++; continue; } if (c == '"') { int start = ++i; while (i < n && input.charAt(i) != '"') i++; - tokens.add(new Token(TType.IDENT, input.substring(start, i))); + tokens.add(new Token(TType.IDENT, input.substring(start, i), start - 1)); i++; continue; } - if (c == '?') { tokens.add(new Token(TType.QUESTION, "?")); i++; continue; } + if (c == '?') { tokens.add(new Token(TType.QUESTION, "?", i)); i++; continue; } if (c == '*') { tokens.add(new Token(TType.IDENT, "*")); i++; continue; } if (c == '=') { tokens.add(new Token(TType.EQ, "=")); i++; continue; } + if (c == '!' && i + 1 < n && input.charAt(i + 1) == '=') { tokens.add(new Token(TType.NE, "!=")); i += 2; continue; } + if (c == '<' && i + 1 < n && input.charAt(i + 1) == '<') { tokens.add(new Token(TType.LBAG, "<<")); i += 2; continue; } + if (c == '>' && i + 1 < n && input.charAt(i + 1) == '>') { tokens.add(new Token(TType.RBAG, ">>")); i += 2; continue; } if (c == '<') { if (i + 1 < n && input.charAt(i + 1) == '>') { tokens.add(new Token(TType.NE, "<>")); i += 2; } else if (i + 1 < n && input.charAt(i + 1) == '=') { tokens.add(new Token(TType.LE, "<=")); i += 2; } @@ -116,16 +255,23 @@ public class DynamoDbPartiQLParser { if (c == ')') { tokens.add(new Token(TType.RPAREN, ")")); i++; continue; } if (c == '{') { tokens.add(new Token(TType.LBRACE, "{")); i++; continue; } if (c == '}') { tokens.add(new Token(TType.RBRACE, "}")); i++; continue; } + if (c == '[') { tokens.add(new Token(TType.LBRACKET, "[")); i++; continue; } + if (c == ']') { tokens.add(new Token(TType.RBRACKET, "]")); i++; continue; } if (c == ',') { tokens.add(new Token(TType.COMMA, ",")); i++; continue; } if (c == ':') { tokens.add(new Token(TType.COLON, ":")); i++; continue; } + if (c == ';') { tokens.add(new Token(TType.SEMICOLON, ";")); i++; continue; } if (c == '.') { tokens.add(new Token(TType.DOT, ".")); i++; continue; } - if (Character.isDigit(c) || (c == '-' && i + 1 < n && Character.isDigit(input.charAt(i + 1)))) { + if (Character.isDigit(c) + || (c == '-' && i + 1 < n && Character.isDigit(input.charAt(i + 1)) && !endsOperand(tokens))) { int start = i; if (c == '-') i++; while (i < n && (Character.isDigit(input.charAt(i)) || input.charAt(i) == '.')) i++; - tokens.add(new Token(TType.NUMBER, input.substring(start, i))); + i = skipExponent(input, i); + tokens.add(new Token(TType.NUMBER, input.substring(start, i), start)); continue; } + if (c == '+') { tokens.add(new Token(TType.PLUS, "+")); i++; continue; } + if (c == '-') { tokens.add(new Token(TType.MINUS, "-")); i++; continue; } if (Character.isLetter(c) || c == '_') { int start = i; while (i < n && (Character.isLetterOrDigit(input.charAt(i)) || input.charAt(i) == '_')) i++; @@ -142,12 +288,26 @@ public class DynamoDbPartiQLParser { case "REMOVE" -> TType.REMOVE; case "DELETE" -> TType.DELETE; case "AND" -> TType.AND; + case "OR" -> TType.OR; + case "NOT" -> TType.NOT; + case "IN" -> TType.IN; + case "IS" -> TType.IS; + case "MISSING" -> TType.MISSING; case "BETWEEN" -> TType.BETWEEN; + case "RETURNING" -> TType.RETURNING; + case "ALL" -> TType.ALL; + case "MODIFIED" -> TType.MODIFIED; + case "OLD" -> TType.OLD; + case "NEW" -> TType.NEW; + case "ORDER" -> TType.ORDER; + case "BY" -> TType.BY; + case "ASC" -> TType.ASC; + case "DESC" -> TType.DESC; case "TRUE", "FALSE" -> TType.BOOL; case "NULL" -> TType.NULL; default -> TType.IDENT; }; - tokens.add(new Token(type, word)); + tokens.add(new Token(type, word, start)); continue; } throw validationEx("Unexpected character '" + c + "' in PartiQL statement"); @@ -156,80 +316,171 @@ public class DynamoDbPartiQLParser { return tokens; } + private static int skipExponent(String input, int i) { + int n = input.length(); + if (i >= n || (input.charAt(i) != 'e' && input.charAt(i) != 'E')) { + return i; + } + int digits = i + 1; + if (digits < n && (input.charAt(digits) == '+' || input.charAt(digits) == '-')) { + digits++; + } + if (digits >= n || !Character.isDigit(input.charAt(digits))) { + return i; + } + while (digits < n && Character.isDigit(input.charAt(digits))) { + digits++; + } + return digits; + } + // --- Recursive-descent parser --- + private final String statement; private final List tokens; private final List parameters; private int pos = 0; private int paramIdx = 0; + private int literalDepth = 0; - private DynamoDbPartiQLParser(List tokens, List parameters) { - this.tokens = tokens; + private DynamoDbPartiQLParser(String statement, List parameters) { + this.statement = statement; + this.tokens = tokenize(statement); this.parameters = parameters; } static Stmt parse(String statement, List parameters) { parameters.forEach(DynamoDbAttributeValueValidator::validate); - parameters.forEach(DynamoDbAttributeValueValidator::requireParameterNestingWithinLimit); - return new DynamoDbPartiQLParser(tokenize(statement.trim()), parameters).parseStmt(); + DynamoDbPartiQLParser parser = new DynamoDbPartiQLParser(statement, parameters); + Stmt stmt = parser.parseStmt(); + if (parser.peek().type() == TType.SEMICOLON) { + parser.advance(); + } + if (parser.peek().type() != TType.EOF) { + throw validationEx("Statement wasn't well formed, can't be processed: Unexpected token after expression"); + } + stmt.where().stream() + .flatMap(DynamoDbPartiQLParser::leaves) + .filter(Cond.In.class::isInstance) + .map(Cond.In.class::cast) + .filter(in -> in.values().size() > MAX_IN_OPERANDS) + .findFirst() + .ifPresent(in -> { + throw validationEx("The IN operator is provided with too many operands; number of operands: " + + in.values().size()); + }); + return stmt; + } + + private static final int MAX_IN_OPERANDS = 100; + + static Stream leaves(Cond cond) { + return switch (cond) { + case Cond.Leaf leaf -> Stream.of(leaf); + case Cond.Not not -> leaves(not.operand()); + case Cond.And and -> and.operands().stream().flatMap(DynamoDbPartiQLParser::leaves); + case Cond.Or or -> or.operands().stream().flatMap(DynamoDbPartiQLParser::leaves); + }; + } + + private static final Set OPERAND_ENDS = EnumSet.of(TType.IDENT, TType.NUMBER, TType.STRING, + TType.BOOL, TType.NULL, TType.QUESTION, TType.RPAREN, TType.RBRACKET, TType.RBRACE, TType.RBAG); + + private static boolean endsOperand(List tokens) { + return !tokens.isEmpty() && OPERAND_ENDS.contains(tokens.getLast().type()); } private Stmt parseStmt() { + if (peekFunction("exists")) { + advance(); + consume(TType.LPAREN); + Stmt.Select select = parseSelect(); + consume(TType.RPAREN); + return new Stmt.Exists(select); + } return switch (peek().type()) { case SELECT -> parseSelect(); case INSERT -> parseInsert(); case UPDATE -> parseUpdate(); case DELETE -> parseDelete(); - default -> throw validationEx("Unsupported PartiQL statement: " + peek().value()); + default -> throw validationEx( + "Statement wasn't well formed, can't be processed: Expected data manipulation"); }; } // SELECT col [, col …] | * FROM "Table"["." "Index"] [WHERE cond [AND cond …]] private Stmt.Select parseSelect() { consume(TType.SELECT); - List cols = new ArrayList<>(); + List cols = new ArrayList<>(); if (peek().type() == TType.IDENT && "*".equals(peek().value())) { advance(); } else { - cols.add(expectIdent()); - while (peek().type() == TType.COMMA) { advance(); cols.add(expectIdent()); } + cols.add(parsePath()); + while (peek().type() == TType.COMMA) { advance(); cols.add(parsePath()); } } consume(TType.FROM); - String table = expectIdent(); - String index = null; - if (peek().type() == TType.DOT) { - advance(); - index = expectIdent(); - } + String table = expectTableName(); + String index = parseIndexQualifier(); List where = new ArrayList<>(); if (peek().type() == TType.WHERE) { advance(); where = parseConditions(); } - return new Stmt.Select(table, index, cols, where); + return new Stmt.Select(table, index, cols, where, parseOrderBy()); + } + + private List parseOrderBy() { + if (peek().type() != TType.ORDER) { + return List.of(); + } + advance(); + consume(TType.BY); + List terms = new ArrayList<>(); + do { + if (!terms.isEmpty()) { + advance(); + } + String attribute = expectIdent(); + boolean descending = peek().type() == TType.DESC; + if (descending || peek().type() == TType.ASC) { + advance(); + } + terms.add(new OrderTerm(attribute, descending)); + } while (peek().type() == TType.COMMA); + return List.copyOf(terms); } // INSERT INTO "Table" VALUE {'key': val, …} private Stmt.Insert parseInsert() { consume(TType.INSERT); consume(TType.INTO); - String table = expectIdent(); + String table = expectTableName(); + if (peek().type() == TType.DOT) { + throw validationEx("FROM clause may only contain a single table name"); + } consume(TType.VALUE); consume(TType.LBRACE); - Map item = new LinkedHashMap<>(); + return new Stmt.Insert(table, parseTupleFields(null)); + } + + private Map parseTupleFields(String literalPath) { + Map fields = new LinkedHashMap<>(); while (peek().type() != TType.RBRACE && peek().type() != TType.EOF) { String key = expectStringOrIdent(); consume(TType.COLON); - item.put(key, parseValue()); - if (peek().type() == TType.COMMA) advance(); + fields.put(key, parseValue(literalPath == null ? null : literalPath + "." + key)); + if (peek().type() == TType.COMMA) { + advance(); + } } consume(TType.RBRACE); - return new Stmt.Insert(table, item); + return fields; } // UPDATE "Table" SET attr=val [, …] [REMOVE attr [, …]] WHERE … private Stmt.Update parseUpdate() { consume(TType.UPDATE); - String table = expectIdent(); - List sets = new ArrayList<>(); - List removes = new ArrayList<>(); + String table = expectTableName(); + String index = parseIndexQualifier(); + List sets = new ArrayList<>(); + List removes = new ArrayList<>(); while (peek().type() == TType.SET || peek().type() == TType.REMOVE) { if (peek().type() == TType.SET) { advance(); @@ -237,62 +488,275 @@ public class DynamoDbPartiQLParser { while (peek().type() == TType.COMMA) { advance(); sets.add(parseAssign()); } } else { advance(); - removes.add(expectIdent()); - while (peek().type() == TType.COMMA) { advance(); removes.add(expectIdent()); } + removes.add(parsePath()); + while (peek().type() == TType.COMMA) { advance(); removes.add(parsePath()); } } } consume(TType.WHERE); - return new Stmt.Update(table, sets, removes, parseConditions()); + List where = parseConditions(); + return new Stmt.Update(table, index, sets, removes, where, parseReturning()); } - // DELETE FROM "Table" WHERE … private Stmt.Delete parseDelete() { consume(TType.DELETE); consume(TType.FROM); - String table = expectIdent(); + String table = expectTableName(); + String index = parseIndexQualifier(); consume(TType.WHERE); - return new Stmt.Delete(table, parseConditions()); + List where = parseConditions(); + Returning returning = parseReturning(); + if (returning != Returning.NONE && returning != Returning.ALL_OLD) { + throw validationEx("Invalid returning clause: " + returning.clause() + + ". Only RETURNING ALL OLD * is allowed in DELETE statements."); + } + return new Stmt.Delete(table, index, where, returning); + } + + private String expectTableName() { + String table = expectIdent(); + requireNonEmptyPathComponent(table); + return table; + } + + private String parseIndexQualifier() { + if (peek().type() != TType.DOT) { + return null; + } + advance(); + String index = expectIdent(); + requireNonEmptyPathComponent(index); + if (peek().type() == TType.DOT) { + throw validationEx("A path may contain at most 2 components in the FROM clause"); + } + return index; + } + + private static void requireNonEmptyPathComponent(String component) { + if (component.isEmpty()) { + throw validationEx("Path component cannot be an empty string"); + } + } + + private Returning parseReturning() { + if (peek().type() != TType.RETURNING) { + return Returning.NONE; + } + advance(); + TType scope = advance().type(); + TType age = advance().type(); + Returning returning = returningOf(scope, age); + Token star = advance(); + if (!"*".equals(star.value())) { + throw validationEx("Expected * in the RETURNING clause, got: '" + star.value() + "'"); + } + return returning; + } + + private static Returning returningOf(TType scope, TType age) { + if (scope == TType.ALL && age == TType.OLD) { + return Returning.ALL_OLD; + } + if (scope == TType.ALL && age == TType.NEW) { + return Returning.ALL_NEW; + } + if (scope == TType.MODIFIED && age == TType.OLD) { + return Returning.MODIFIED_OLD; + } + if (scope == TType.MODIFIED && age == TType.NEW) { + return Returning.MODIFIED_NEW; + } + throw validationEx("Expected ALL or MODIFIED and OLD or NEW in the RETURNING clause"); } private List parseConditions() { - List conds = new ArrayList<>(); - conds.add(parseCond()); - while (peek().type() == TType.AND) { advance(); conds.add(parseCond()); } - return conds; + Cond cond = parseOr(); + return cond instanceof Cond.And and ? and.operands() : List.of(cond); + } + + private Cond parseOr() { + List operands = new ArrayList<>(); + operands.add(parseAnd()); + while (peek().type() == TType.OR) { + advance(); + operands.add(parseAnd()); + } + return operands.size() == 1 ? operands.getFirst() : new Cond.Or(List.copyOf(operands)); + } + + private Cond parseAnd() { + List operands = new ArrayList<>(); + addAndOperand(operands, parseNot()); + while (peek().type() == TType.AND) { + advance(); + addAndOperand(operands, parseNot()); + } + return operands.size() == 1 ? operands.getFirst() : new Cond.And(List.copyOf(operands)); + } + + private static void addAndOperand(List operands, Cond operand) { + if (operand instanceof Cond.And and) { + operands.addAll(and.operands()); + } else { + operands.add(operand); + } + } + + private Cond parseNot() { + if (peek().type() == TType.NOT) { + advance(); + return new Cond.Not(parseNot()); + } + return parseComparison(); + } + + private static final Set COMPARISONS = EnumSet.of(TType.EQ, TType.NE, TType.LT, TType.LE, TType.GT, TType.GE); + + private static final List PREDICATE_FUNCTIONS = + List.of("begins_with", "contains", "attribute_type", "attribute_exists", "attribute_not_exists"); + + private Cond parseComparison() { + if (peek().type() == TType.BOOL + && (tokens.get(pos + 1).type() == TType.EQ || tokens.get(pos + 1).type() == TType.NE)) { + boolean flag = Boolean.parseBoolean(advance().value()); + boolean equal = advance().type() == TType.EQ; + Cond predicate = parseCond(); + return flag == equal ? predicate : new Cond.Not(predicate); + } + Cond cond = parseCond(); + if (peek().type() == TType.BETWEEN) { + throw incorrectOperandType("BETWEEN", "BOOL"); + } + if (peek().type() == TType.IN) { + advance(); + consume(TType.LBRACKET); + List matches = new ArrayList<>(); + matches.add(conditionEquals(cond)); + while (peek().type() == TType.COMMA) { + advance(); + matches.add(conditionEquals(cond)); + } + consume(TType.RBRACKET); + return matches.size() == 1 ? matches.getFirst() : new Cond.Or(List.copyOf(matches)); + } + if (!COMPARISONS.contains(peek().type())) { + return cond; + } + String op = parseOp(); + if (!"=".equals(op) && !"<>".equals(op)) { + throw incorrectOperandType(op, "BOOL"); + } + Cond equal = conditionEquals(cond); + return "=".equals(op) ? equal : new Cond.Not(equal); + } + + private Cond conditionEquals(Cond cond) { + boolean predicateNext = peek().type() == TType.LPAREN + || PREDICATE_FUNCTIONS.stream().anyMatch(this::peekFunction); + if (predicateNext) { + Cond other = parseCond(); + return sameTruth(cond, other, new Cond.Not(other)); + } + if (peek().type() == TType.IDENT) { + Path path = parsePath(); + return sameTruth(cond, new Cond.Eq(path, new PVal.Bool(true)), new Cond.Eq(path, new PVal.Bool(false))); + } + if (parseValue() instanceof PVal.Bool flag) { + return flag.v() ? cond : new Cond.Not(cond); + } + return new Cond.And(List.of(cond, new Cond.Not(cond))); + } + + private static Cond sameTruth(Cond cond, Cond whenTrue, Cond whenFalse) { + return new Cond.Or(List.of(new Cond.And(List.of(cond, whenTrue)), + new Cond.And(List.of(new Cond.Not(cond), whenFalse)))); } // S, N and B are the only types DynamoDB gives an ordering. - private static final Set ORDERED_TYPES = Set.of("S", "N", "B"); + static final Set ORDERED_TYPES = Set.of("S", "N", "B"); - private static String typeCode(PVal val) { + static String typeCode(PVal val) { return switch (val) { case PVal.Str ignored -> "S"; case PVal.Num ignored -> "N"; case PVal.Bool ignored -> "BOOL"; case PVal.Null ignored -> "NULL"; case PVal.Av av -> av.type(); + case PVal.ListOf ignored -> "L"; + case PVal.Tuple ignored -> "M"; + case PVal.Bag bag -> bag.type(); }; } private static void requireOrdered(String op, PVal val) { - String type = typeCode(val); - if (!ORDERED_TYPES.contains(type)) { - throw validationEx("Incorrect operand type for operator or function; " - + "operator or function: " + op + ", operand type: " + type); + if (!ORDERED_TYPES.contains(typeCode(val))) { + throw incorrectOperandType(op, val); } } + private static AwsException incorrectOperandType(String op, PVal val) { + return incorrectOperandType(op, typeCode(val)); + } + + private static AwsException incorrectOperandType(String op, String type) { + return validationEx("Incorrect operand type for operator or function; " + + "operator or function: " + op + ", operand type: " + type); + } + private Cond parseCond() { + if (peek().type() == TType.LPAREN) { + advance(); + Cond grouped = parseOr(); + consume(TType.RPAREN); + return grouped; + } if (peek().type() == TType.IDENT && "begins_with".equalsIgnoreCase(peek().value())) { advance(); consume(TType.LPAREN); - String attr = expectIdent(); + Path path = parsePath(); consume(TType.COMMA); PVal prefix = parseValue(); consume(TType.RPAREN); - return new Cond.BeginsWith(attr, prefix); + return new Cond.BeginsWith(path, prefix); } - String attr = expectIdent(); + if (peekFunction("contains")) { + advance(); + consume(TType.LPAREN); + Path path = parsePath(); + consume(TType.COMMA); + PVal operand = parseValue(); + consume(TType.RPAREN); + return new Cond.Contains(path, operand); + } + if (peekFunction("attribute_type")) { + advance(); + consume(TType.LPAREN); + Path path = parsePath(); + consume(TType.COMMA); + PVal type = parseValue(); + consume(TType.RPAREN); + requireAttributeTypeName(type); + return new Cond.AttributeType(path, type); + } + if (peekFunction("attribute_exists") || peekFunction("attribute_not_exists")) { + boolean exists = "attribute_exists".equalsIgnoreCase(advance().value()); + consume(TType.LPAREN); + Path path = parsePath(); + consume(TType.RPAREN); + return new Cond.Missing(path, exists); + } + if (peekFunction("size")) { + advance(); + consume(TType.LPAREN); + Path path = parsePath(); + consume(TType.RPAREN); + String op = parseOp(); + PVal val = parseValue(); + if (!"=".equals(op) && !"<>".equals(op)) { + requireOrdered(op, val); + } + return new Cond.SizeCmp(path, op, val); + } + Path path = parsePath(); if (peek().type() == TType.BETWEEN) { advance(); PVal lo = parseValue(); @@ -300,17 +764,62 @@ public class DynamoDbPartiQLParser { PVal hi = parseValue(); requireOrdered("BETWEEN", lo); requireOrdered("BETWEEN", hi); - return new Cond.Between(attr, lo, hi); + return new Cond.Between(path, lo, hi); + } + if (peek().type() == TType.IN) { + advance(); + consume(TType.LBRACKET); + List values = new ArrayList<>(); + values.add(parseValue()); + while (peek().type() == TType.COMMA) { + advance(); + values.add(parseValue()); + } + consume(TType.RBRACKET); + return new Cond.In(path, List.copyOf(values)); + } + if (peek().type() == TType.IS) { + advance(); + boolean negated = peek().type() == TType.NOT; + if (negated) { + advance(); + } + if (peek().type() == TType.NULL) { + advance(); + return new Cond.IsNull(path, negated); + } + consume(TType.MISSING); + return new Cond.Missing(path, negated); } String op = parseOp(); PVal val = parseValue(); if ("=".equals(op)) { - return new Cond.Eq(attr, val); + return new Cond.Eq(path, val); } if (!"<>".equals(op)) { requireOrdered(op, val); } - return new Cond.Cmp(attr, op, val); + return new Cond.Cmp(path, op, val); + } + + private boolean peekFunction(String name) { + return peek().type() == TType.IDENT && name.equalsIgnoreCase(peek().value()) + && tokens.get(pos + 1).type() == TType.LPAREN; + } + + private static final Set SET_TYPES = Set.of("SS", "NS", "BS"); + + private static final List ATTRIBUTE_TYPE_NAMES = + List.of("N", "BS", "L", "B", "NULL", "M", "S", "SS", "NS", "BOOL"); + + private static void requireAttributeTypeName(PVal type) { + if (!(type instanceof PVal.Str name)) { + throw incorrectOperandType("attribute_type", type); + } + if (!ATTRIBUTE_TYPE_NAMES.contains(name.v())) { + throw validationEx("Invalid attribute type name found; type: " + name.v() + + ", valid types: {" + String.join(",", ATTRIBUTE_TYPE_NAMES) + "}"); + } } private String parseOp() { @@ -323,24 +832,215 @@ public class DynamoDbPartiQLParser { }; } - private Assign parseAssign() { - String attr = expectIdent(); + private SetClause parseAssign() { + Path path = parsePath(); consume(TType.EQ); - return new Assign(attr, parseValue()); + if (peekFunction("set_add") || peekFunction("set_delete")) { + return parseSetMutation(path); + } + Operand left = parseOperand(); + if (peek().type() == TType.PLUS || peek().type() == TType.MINUS) { + String op = advance().value(); + Operand right = parseOperand(); + requireNumberLiteral(op, left); + requireNumberLiteral(op, right); + return new Assign(path, left, op, right); + } + return new Assign(path, left, null, null); + } + + private static void requireNumberLiteral(String op, Operand operand) { + if (operand instanceof Operand.Value value && !(value.val() instanceof PVal.Num)) { + throw incorrectOperandType(op, value.val()); + } + } + + private SetClause parseSetMutation(Path target) { + Token function = advance(); + String name = function.value().toUpperCase(Locale.ROOT); + consume(TType.LPAREN); + Token firstArgument = peek(); + if (!parsePath().equals(target)) { + throw validationEx("The first argument to " + name + " must equal the assignment value at " + + position(function)); + } + consume(TType.COMMA); + PVal bag = parseValue(); + consume(TType.RPAREN); + if (!SET_TYPES.contains(typeCode(bag))) { + throw validationEx("The second argument to " + name + " must be a value with type SET at " + + position(firstArgument)); + } + return "set_add".equalsIgnoreCase(function.value()) ? new SetAdd(target, bag) : new SetDelete(target, bag); + } + + private String position(Token token) { + boolean quoted = statement.charAt(token.start()) == '"'; + return position(token.start(), token.value().length() + (quoted ? 2 : 0)); + } + + private String position(int start, int length) { + int lineStart = statement.lastIndexOf('\n', start - 1) + 1; + long line = statement.substring(0, start).chars().filter(c -> c == '\n').count() + 1; + return line + ":" + (start - lineStart + 1) + ":" + length; + } + + private Operand parseOperand() { + if (peekFunction("list_append")) { + advance(); + consume(TType.LPAREN); + Operand first = parseOperand(); + consume(TType.COMMA); + Operand second = parseOperand(); + consume(TType.RPAREN); + return new Operand.ListAppend(first, second); + } + if (peek().type() == TType.IDENT) { + return new Operand.Attribute(parsePath()); + } + return new Operand.Value(parseValue()); + } + + private Path parsePath() { + List segments = new ArrayList<>(); + segments.add(new Seg.Name(expectIdent())); + while (true) { + switch (peek().type()) { + case DOT -> { + advance(); + segments.add(new Seg.Name(expectIdent())); + } + case LBRACKET -> { + advance(); + segments.add(new Seg.Index(expectListIndex())); + consume(TType.RBRACKET); + } + default -> { + return new Path(List.copyOf(segments)); + } + } + } + } + + private long expectListIndex() { + Token t = advance(); + if (t.type() == TType.NUMBER) { + try { + long index = Long.parseLong(t.value()); + if (index < 0 || index > Integer.MAX_VALUE) { + int sign = index < 0 ? 1 : 0; + throw validationEx("List index is not within the allowable range; index: [" + t.value() + "] at " + + position(t.start() + sign, t.value().length() - sign)); + } + return index; + } catch (NumberFormatException expected) { + // Falls through to the shared rejection below. + } + } + throw validationEx("Expected a list index, got: '" + t.value() + "'"); } private PVal parseValue() { + return parseValue(null); + } + + private PVal parseValue(String literalPath) { + if (literalDepth == DynamoDbAttributeValueValidator.MAX_NESTING_LEVELS) { + throw validationEx(DynamoDbAttributeValueValidator.NESTING_EXCEEDED + " under " + literalPath); + } Token t = advance(); + String nested = literalPath == null ? "root" : literalPath; return switch (t.type()) { case STRING -> new PVal.Str(t.value()); case NUMBER -> new PVal.Num(t.value()); case BOOL -> new PVal.Bool(Boolean.parseBoolean(t.value())); case NULL -> new PVal.Null(); - case QUESTION -> resolveParam(); + case QUESTION -> { + if (literalPath != null) { + throw validationEx("Unsupported data type: Parameter under key " + literalPath + " at " + + position(t.start(), 1)); + } + yield resolveParam(); + } + case LBRACKET, LBRACE, LBAG -> parseCollection(t, nested); + case PLUS, MINUS -> parseSignedNumber(t); default -> throw validationEx("Expected value literal or ?, got: " + t.value()); }; } + private PVal parseCollection(Token open, String literalPath) { + literalDepth++; + PVal collection = switch (open.type()) { + case LBRACKET -> new PVal.ListOf(parseListItems(literalPath)); + case LBRACE -> new PVal.Tuple(parseTupleFields(literalPath)); + default -> parseBag(literalPath); + }; + literalDepth--; + return collection; + } + + private PVal.Num parseSignedNumber(Token sign) { + Token operand = advance(); + PVal.Num number = switch (operand.type()) { + case NUMBER -> new PVal.Num(operand.value()); + case PLUS, MINUS -> parseSignedNumber(operand); + default -> throw validationEx("Unsupported operator in Condition Expression. Operator: " + sign.value()); + }; + if (sign.type() == TType.PLUS) { + return number; + } + return new PVal.Num(number.v().startsWith("-") ? number.v().substring(1) : "-" + number.v()); + } + + private List parseListItems(String literalPath) { + List items = new ArrayList<>(); + while (peek().type() != TType.RBRACKET && peek().type() != TType.EOF) { + items.add(parseValue(literalPath + "[" + items.size() + "]")); + if (peek().type() == TType.COMMA) { + advance(); + } + } + consume(TType.RBRACKET); + return List.copyOf(items); + } + + private PVal.Bag parseBag(String literalPath) { + List members = new ArrayList<>(); + while (peek().type() != TType.RBAG && peek().type() != TType.EOF) { + members.add(parseValue(literalPath)); + if (peek().type() == TType.COMMA) { + advance(); + } + } + consume(TType.RBAG); + if (members.isEmpty()) { + throw validationEx("Empty bags are not supported"); + } + String memberType = typeCode(members.getFirst()); + boolean uniform = members.stream().allMatch(m -> typeCode(m).equals(memberType)); + if (!uniform || !(memberType.equals("S") || memberType.equals("N"))) { + throw validationEx( + "Unsupported data type in Bag. DynamoDB only supports either numbers or strings in bags"); + } + requireDistinctMembers(memberType, members); + return new PVal.Bag(memberType + "S", List.copyOf(members)); + } + + private static void requireDistinctMembers(String memberType, List members) { + boolean numbers = memberType.equals("N"); + List texts = members.stream() + .map(member -> member instanceof PVal.Num n ? n.v() : ((PVal.Str) member).v()) + .toList(); + long distinct = texts.stream() + .map(text -> numbers ? DynamoDbNumberUtils.validateAndNormalize(text) : text) + .distinct() + .count(); + if (distinct < texts.size()) { + throw validationEx("One or more parameter values were invalid: Input collection [" + + String.join(", ", texts) + "] contains duplicates" + (numbers ? "! under root" : ".")); + } + } + private PVal resolveParam() { if (paramIdx >= parameters.size()) { throw validationEx("Not enough parameters supplied for ? placeholders"); 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 539a017f15..566aa28625 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 @@ -633,6 +633,7 @@ public class DynamoDbService implements ResourceProvider { if (conditionExpression != null) { evaluateCondition(existing, conditionExpression, exprAttrNames, exprAttrValues, returnValuesOnConditionCheckFailure); } + requireItemNestingWithinLimit(normalizedItem); tableItems.put(itemKey, normalizedItem); if (shouldPersist) { @@ -922,6 +923,7 @@ public class DynamoDbService implements ResourceProvider { // Reject any attempt to modify a key attribute validateKeyNotModified(table, key, item); + requireItemNestingWithinLimit(item); // AWS validates index key values against the item the update produces. validateIndexKeyTypes(table, item, true); @@ -1466,7 +1468,7 @@ public class DynamoDbService implements ResourceProvider { for (int i = 0; i < transactItems.size(); i++) { try { validateTransactItem(transactItems.get(i), region, staged); - } catch (KeySchemaMismatchException e) { + } catch (KeySchemaMismatchException | ItemNestingExceededException e) { throw cancelledByMember(transactItems.size(), i, e.getMessage()); } } @@ -1610,6 +1612,12 @@ public class DynamoDbService implements ResourceProvider { } } + private static void requireItemNestingWithinLimit(JsonNode item) { + if (!DynamoDbAttributeValueValidator.nestingWithinLimit(item)) { + throw new ItemNestingExceededException(); + } + } + private static TransactionCanceledException cancelledByMember(int memberCount, int failedMember, String message) { List reasons = new ArrayList<>(); @@ -1712,6 +1720,7 @@ public class DynamoDbService implements ResourceProvider { DynamoDbItemSize.validateSize(normalizedItem); buildItemKey(table, normalizedItem); validateIndexKeyTypes(table, normalizedItem, false); + requireItemNestingWithinLimit(normalizedItem); } else if (transactItem.has("Delete")) { JsonNode del = transactItem.get("Delete"); String tableName = canonicalTableName(region, del.path("TableName").asText()); @@ -1743,6 +1752,7 @@ public class DynamoDbService implements ResourceProvider { applyUpdateExpression(item, updateExpression, exprAttrNames, exprAttrValues); } validateKeyNotModified(table, key, item); + requireItemNestingWithinLimit(item); validateIndexKeyTypes(table, item, true); } } @@ -2271,7 +2281,6 @@ public class DynamoDbService implements ResourceProvider { String attrPath = clause.substring(0, eqIdx).trim(); touched.add(attrPath); - String attrName = resolveAttributeName(attrPath, exprAttrNames); String rest = clause.substring(eqIdx + 1).trim(); @@ -2335,7 +2344,7 @@ public class DynamoDbService implements ResourceProvider { // 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 attrName. + // The result is always assigned to attrPath. String[] args = extractFunctionArgs(valuePart); if (args.length == 2) { String checkAttr = resolveAttributeName(args[0].trim(), exprAttrNames); @@ -2380,7 +2389,7 @@ public class DynamoDbService implements ResourceProvider { ObjectNode result = JsonNodeFactory.instance.objectNode(); result.set("L", merged); - item.set(attrName, result); + setValueAtPath(item, attrPath, result, exprAttrNames); } } } else if (valuePart.startsWith(":") && exprAttrValues != null) { @@ -2532,6 +2541,7 @@ public class DynamoDbService implements ResourceProvider { * - 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) { + requireSameTypeAsOperand(existingValue, addValue, List.of("N", "SS", "NS", "BS")); ObjectNode result = JsonNodeFactory.instance.objectNode(); // Handle number addition @@ -2646,13 +2656,61 @@ public class DynamoDbService implements ResourceProvider { return clause; } + private static final Map OPERAND_TYPE_NAMES = Map.of( + "S", "STRING", "N", "NUMBER", "B", "Binary", "BOOL", "BOOL", "NULL", "NULL", "L", "LIST", "M", "MAP"); + + void requireAddOrDeleteOperandTypes(String updateExpression, JsonNode exprAttrValues, boolean inValidationEnvelope) { + if (updateExpression == null || exprAttrValues == null) { + return; + } + String remaining = updateExpression.trim().replaceAll("\\s+", " "); + while (!remaining.isEmpty()) { + String keyword = remaining.substring(0, Math.max(remaining.indexOf(' '), 0)).toUpperCase(); + String body = remaining.substring(keyword.length()).trim(); + int nextClause = findNextClauseKeyword(body); + String actions = nextClause < 0 ? body : body.substring(0, nextClause); + remaining = nextClause < 0 ? "" : body.substring(nextClause); + Set allowed = switch (keyword) { + case "ADD" -> Set.of("N", "SS", "NS", "BS"); + case "DELETE" -> Set.of("SS", "NS", "BS"); + default -> null; + }; + while (allowed != null && !actions.isBlank()) { + int comma = findNextComma(actions); + String[] words = (comma < 0 ? actions : actions.substring(0, comma)).trim().split(" "); + actions = comma < 0 ? "" : actions.substring(comma + 1); + JsonNode operand = exprAttrValues.get(words[words.length - 1]); + if (operand == null) { + continue; + } + String type = DynamoDbAttributeValueValidator.typeOf(operand); + if (!allowed.contains(type)) { + throw new AwsException("ValidationException", (inValidationEnvelope ? "1 validation error detected: " : "") + + "Invalid UpdateExpression: Incorrect operand type for operator or function;" + + " operator: " + keyword + ", operand type: " + OPERAND_TYPE_NAMES.get(type) + + ", typeSet: ALLOWED_FOR_ADD_OPERAND", 400); + } + } + } + } + + private static void requireSameTypeAsOperand(JsonNode existingValue, JsonNode operand, List types) { + for (String type : types) { + if (operand.has(type) && existingValue != null && !existingValue.has(type)) { + throw new AwsException("ValidationException", + "An operand in the update expression has an incorrect data type", 400); + } + } + } + /** * Implements DynamoDB DELETE operation semantics: * removes the specified elements from a set attribute (SS, NS, BS). * Returns null if the resulting set is empty (caller should remove the attribute). - * Returns the existing value unchanged if types don't match or the value isn't a set. + * Returns the existing value unchanged if the value to delete isn't a set. */ private JsonNode applyDeleteOperation(JsonNode existingValue, JsonNode deleteValue) { + requireSameTypeAsOperand(existingValue, deleteValue, List.of("SS", "NS", "BS")); ObjectNode result = JsonNodeFactory.instance.objectNode(); if (deleteValue.has("SS") && existingValue.has("SS")) { diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java index 762cc00919..61e9fa2234 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluator.java @@ -711,7 +711,7 @@ final class ExpressionEvaluator { JsonNode leftNode = resolveAttributeValue(cmp.left(), item, exprAttrNames, exprAttrValues); JsonNode rightNode = resolveAttributeValue(cmp.right(), item, exprAttrNames, exprAttrValues); - if (leftNode == null || rightNode == null) return false; + if (leftNode == null || rightNode == null || !sameType(leftNode, rightNode)) return false; int cmpResult = compareAttributeValues(leftNode, rightNode); return switch (cmp.op()) { case LT -> cmpResult < 0; @@ -727,10 +727,14 @@ final class ExpressionEvaluator { JsonNode val = resolveAttributeValue(bet.value(), item, exprAttrNames, exprAttrValues); JsonNode low = resolveAttributeValue(bet.low(), item, exprAttrNames, exprAttrValues); JsonNode high = resolveAttributeValue(bet.high(), item, exprAttrNames, exprAttrValues); - if (val == null || low == null || high == null) return false; + if (val == null || low == null || high == null || !sameType(val, low) || !sameType(val, high)) return false; return compareAttributeValues(val, low) >= 0 && compareAttributeValues(val, high) <= 0; } + private static boolean sameType(JsonNode left, JsonNode right) { + return left.fieldNames().next().equals(right.fieldNames().next()); + } + private static boolean evaluateIn(InExpr in, JsonNode item, JsonNode exprAttrNames, JsonNode exprAttrValues) { // For IN, we use type-aware equality via the raw attribute value nodes diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/ItemNestingExceededException.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/ItemNestingExceededException.java new file mode 100644 index 0000000000..f2415cd79c --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/ItemNestingExceededException.java @@ -0,0 +1,10 @@ +package io.github.hectorvent.floci.services.dynamodb; + +import io.github.hectorvent.floci.core.common.AwsException; + +public class ItemNestingExceededException extends AwsException { + + public ItemNestingExceededException() { + super("ValidationException", DynamoDbAttributeValueValidator.NESTING_EXCEEDED, 400); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAddDeleteOperandIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAddDeleteOperandIntegrationTest.java new file mode 100644 index 0000000000..02a05d789e --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAddDeleteOperandIntegrationTest.java @@ -0,0 +1,115 @@ +package io.github.hectorvent.floci.services.dynamodb; + +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.nullValue; + +/** + * ADD and DELETE operand types, with every expectation captured from real DynamoDB (eu-west-2, 2026-09-17). + */ +@QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class DynamoDbAddDeleteOperandIntegrationTest { + + private static final String CONTENT_TYPE = "application/x-amz-json-1.0"; + private static final String TABLE = "add-delete-operands"; + private static final String KEY = "{\"pk\":{\"S\":\"a\"}}"; + private static final String REFUSED = "Invalid UpdateExpression: Incorrect operand type for operator or function;" + + " operator: %s, operand type: %s, typeSet: ALLOWED_FOR_ADD_OPERAND"; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + @Order(1) + void createTable() { + request("DynamoDB_20120810.CreateTable", """ + { + "TableName": "%s", + "AttributeDefinitions": [{"AttributeName":"pk","AttributeType":"S"}], + "KeySchema": [{"AttributeName":"pk","KeyType":"HASH"}], + "BillingMode": "PAY_PER_REQUEST" + } + """.formatted(TABLE)) + .statusCode(200); + request("DynamoDB_20120810.PutItem", """ + {"TableName":"%s","Item":{"pk":{"S":"a"},"n":{"N":"5"},"s":{"SS":["m"]}}} + """.formatted(TABLE)) + .statusCode(200); + } + + @Test + @Order(2) + void updateItemRefusesAnOperandBeforeReadingTheItem() { + updateItem("\"UpdateExpression\":\"ADD n :v\",\"ExpressionAttributeValues\":{\":v\":{\"S\":\"x\"}}") + .statusCode(400) + .body("message", equalTo("1 validation error detected: " + REFUSED.formatted("ADD", "STRING"))); + updateItem("\"UpdateExpression\":\"SET t = :t ADD n :v\"," + + "\"ExpressionAttributeValues\":{\":t\":{\"S\":\"t\"},\":v\":{\"BOOL\":true}}") + .statusCode(400) + .body("message", equalTo("1 validation error detected: " + REFUSED.formatted("ADD", "BOOL"))); + updateItem("\"UpdateExpression\":\"DELETE nope :v\",\"ConditionExpression\":\"attribute_exists(nope)\"," + + "\"ExpressionAttributeValues\":{\":v\":{\"L\":[{\"S\":\"m\"}]}}") + .statusCode(400) + .body("message", equalTo("1 validation error detected: " + REFUSED.formatted("DELETE", "LIST"))); + updateItem("\"UpdateExpression\":\"DELETE s :v\",\"ExpressionAttributeValues\":{\":v\":{\"N\":\"1\"}}") + .statusCode(400) + .body("message", equalTo("1 validation error detected: " + REFUSED.formatted("DELETE", "NUMBER"))); + request("DynamoDB_20120810.UpdateItem", """ + {"TableName":"add-delete-operands-missing","Key":%s,"UpdateExpression":"ADD n :v", + "ExpressionAttributeValues":{":v":{"S":"x"}}} + """.formatted(KEY)) + .statusCode(400) + .body("message", equalTo("1 validation error detected: " + REFUSED.formatted("ADD", "STRING"))); + assertItemUnchanged(); + } + + @Test + @Order(3) + void transactWriteItemsRefusesAnOperandWithoutTheErrorCount() { + request("DynamoDB_20120810.TransactWriteItems", """ + {"TransactItems":[{"Update":{"TableName":"%s","Key":%s,"UpdateExpression":"ADD n :v", + "ExpressionAttributeValues":{":v":{"S":"x"}}}}]} + """.formatted(TABLE, KEY)) + .statusCode(400) + .body("message", equalTo(REFUSED.formatted("ADD", "STRING"))); + assertItemUnchanged(); + } + + private static void assertItemUnchanged() { + request("DynamoDB_20120810.GetItem", """ + {"TableName":"%s","Key":%s,"ConsistentRead":true} + """.formatted(TABLE, KEY)) + .statusCode(200) + .body("Item.n.N", equalTo("5")) + .body("Item.s.SS", contains("m")) + .body("Item.t", nullValue()); + } + + private static ValidatableResponse updateItem(String expression) { + return request("DynamoDB_20120810.UpdateItem", + "{\"TableName\":\"" + TABLE + "\",\"Key\":" + KEY + "," + expression + "}"); + } + + private static ValidatableResponse request(String target, String body) { + return given() + .header("X-Amz-Target", target) + .contentType(CONTENT_TYPE) + .body(body) + .when() + .post("/") + .then(); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java index 631c18fa6e..4c35d41f0b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java @@ -1322,6 +1322,24 @@ class DynamoDbJsonHandlerTest { assertEquals(NESTING_MESSAGE, ex.getMessage()); } + @Test + void updateItemRejectsAValueThatLeavesALeafAtLevel33UnderANestedPath() throws Exception { + createUsersTable("eu-west-1"); + ObjectNode create = updateUserRequest(); + create.put("UpdateExpression", "SET parent = :empty"); + ObjectNode emptyMap = mapper.createObjectNode(); + emptyMap.putObject("M"); + create.set("ExpressionAttributeValues", mapper.createObjectNode().set(":empty", emptyMap)); + handler.handle("UpdateItem", create, "eu-west-1"); + + ObjectNode request = updateUserRequest(); + request.put("UpdateExpression", "SET parent.deep = :deep"); + request.set("ExpressionAttributeValues", mapper.createObjectNode().set(":deep", nestedMaps(31))); + AwsException ex = assertThrows(AwsException.class, + () -> handler.handle("UpdateItem", request, "eu-west-1")); + assertEquals("Nesting Levels have exceeded supported limits", ex.getMessage()); + } + private ObjectNode transactWrite(String action, ObjectNode op) { op.put("TableName", "Users"); var member = mapper.createObjectNode(); @@ -1662,17 +1680,30 @@ class DynamoDbJsonHandlerTest { createUsersTable("eu-west-1"); var request = mapper.createObjectNode(); request.put("Statement", "UPDATE \"Users\" SET deep=? WHERE userId=?"); - request.set("Parameters", mapper.createArrayNode().add(nestedMaps(32)).add(attributeValue("S", "u1"))); + request.set("Parameters", mapper.createArrayNode().add(nestedMaps(33)).add(attributeValue("S", "u1"))); var ex = assertThrows(AwsException.class, () -> handler.handle("ExecuteStatement", request, "eu-west-1")); assertEquals("ValidationException", ex.getErrorCode()); - assertEquals("Nesting Levels have exceeded supported limits", ex.getMessage()); + assertEquals("Nesting Levels have exceeded supported limits: " + + "Attributes in the item have nested levels beyond supported limit", ex.getMessage()); } @Test - void executeTransactionCancelsOnATooDeepParameterWithThatStatementsReason() throws Exception { + void executeStatementReadsAParameterWithALeafAtLevel33() throws Exception { createUsersTable("eu-west-1"); + ObjectNode request = mapper.createObjectNode(); + request.put("Statement", "SELECT * FROM \"Users\" WHERE userId=? AND deep=?"); + request.set("Parameters", mapper.createArrayNode().add(attributeValue("S", "u1")).add(nestedMaps(32))); + + Response response = handler.handle("ExecuteStatement", request, "eu-west-1"); + assertEquals(200, response.getStatus()); + } + + @Test + void executeTransactionCancelsOnAnItemLeftTooDeepWithThatStatementsReason() throws Exception { + createUsersTable("eu-west-1"); + service.putItem("Users", item("userId", "u1"), "eu-west-1"); var fine = mapper.createObjectNode(); fine.put("Statement", "UPDATE \"Users\" SET x=? WHERE userId=?"); fine.set("Parameters", mapper.createArrayNode().add(attributeValue("S", "x")).add(attributeValue("S", "u1"))); diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLDocumentedSyntaxIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLDocumentedSyntaxIntegrationTest.java new file mode 100644 index 0000000000..e55cb7ce4d --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLDocumentedSyntaxIntegrationTest.java @@ -0,0 +1,372 @@ +package io.github.hectorvent.floci.services.dynamodb; + +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import java.util.Arrays; +import java.util.Base64; +import java.util.List; +import java.util.stream.Collectors; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; + +/** + * The PartiQL forms the AWS developer guide shows, with every expectation captured from real + * DynamoDB (eu-west-2, 2026-09-17). + */ +@QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class DynamoDbPartiQLDocumentedSyntaxIntegrationTest { + + private static final String CONTENT_TYPE = "application/x-amz-json-1.0"; + private static final String TABLE = "partiql-documented-syntax"; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + @Order(1) + void createTable() { + request("DynamoDB_20120810.CreateTable", """ + { + "TableName": "%s", + "AttributeDefinitions": [ + {"AttributeName":"pk","AttributeType":"S"}, + {"AttributeName":"sk","AttributeType":"S"} + ], + "KeySchema": [ + {"AttributeName":"pk","KeyType":"HASH"}, + {"AttributeName":"sk","KeyType":"RANGE"} + ], + "BillingMode": "PAY_PER_REQUEST" + } + """.formatted(TABLE)) + .statusCode(200); + } + + @Test + @Order(2) + void insertsMapsListsAndSets() { + statement("INSERT INTO \"" + TABLE + "\" value {'pk':'dt', 'sk':'1', 'NumberType':1," + + " 'MapType' : {'entryname1': 'value', 'entryname2': 4}," + + " 'ListType': [1,'stringval']," + + " 'NumberSetType':<<1,34,32,4.5>>," + + " 'StringSetType':<<'stringval','stringval2'>>}") + .statusCode(200); + + getItem("dt") + .body("Item.NumberType.N", equalTo("1")) + .body("Item.MapType.M.entryname1.S", equalTo("value")) + .body("Item.MapType.M.entryname2.N", equalTo("4")) + .body("Item.ListType.L[0].N", equalTo("1")) + .body("Item.ListType.L[1].S", equalTo("stringval")) + .body("Item.NumberSetType.NS", containsInAnyOrder("1", "34", "32", "4.5")) + .body("Item.StringSetType.SS", containsInAnyOrder("stringval", "stringval2")); + } + + @Test + @Order(3) + void updatesWithListAppendAndSetAdd() { + statement("UPDATE \"" + TABLE + "\"" + + " SET NumberType=NumberType + 100" + + " SET MapType.NewMapEntry=[2020, 'stringvalue', 2.4]" + + " SET ListType = LIST_APPEND(ListType, [4, <<'string1', 'string2'>>])" + + " SET NumberSetType= SET_ADD(NumberSetType, <<345, 48.4>>)" + + " SET StringSetType = SET_ADD(StringSetType, <<'stringsetvalue1', 'stringsetvalue2'>>)" + + " WHERE pk='dt' AND sk='1'") + .statusCode(200); + + getItem("dt") + .body("Item.NumberType.N", equalTo("101")) + .body("Item.MapType.M.NewMapEntry.L[0].N", equalTo("2020")) + .body("Item.MapType.M.NewMapEntry.L[1].S", equalTo("stringvalue")) + .body("Item.MapType.M.NewMapEntry.L[2].N", equalTo("2.4")) + .body("Item.ListType.L.size()", equalTo(4)) + .body("Item.ListType.L[3].SS", containsInAnyOrder("string1", "string2")) + .body("Item.NumberSetType.NS", containsInAnyOrder("1", "4.5", "32", "34", "48.4", "345")) + .body("Item.StringSetType.SS", + containsInAnyOrder("stringsetvalue1", "stringsetvalue2", "stringval", "stringval2")); + } + + @Test + @Order(4) + void updatesWithRemoveAndSetDelete() { + statement("UPDATE \"" + TABLE + "\"" + + " SET NumberType=NumberType - 1" + + " REMOVE ListType[1]" + + " REMOVE MapType.NewMapEntry" + + " SET NumberSetType = SET_DELETE( NumberSetType, <<345>>)" + + " SET StringSetType = SET_DELETE( StringSetType, <<'stringsetvalue1'>>)" + + " WHERE pk='dt' AND sk='1'") + .statusCode(200); + + getItem("dt") + .body("Item.NumberType.N", equalTo("100")) + .body("Item.ListType.L.size()", equalTo(3)) + .body("Item.ListType.L[1].N", equalTo("4")) + .body("Item.MapType.M.NewMapEntry", nullValue()) + .body("Item.NumberSetType.NS", containsInAnyOrder("1", "4.5", "32", "34", "48.4")) + .body("Item.StringSetType.SS", containsInAnyOrder("stringsetvalue2", "stringval", "stringval2")); + } + + @Test + @Order(5) + void readsExponentsAndDoubledQuotes() { + statement("INSERT INTO \"" + TABLE + "\" VALUE {'pk':'num', 'sk':'1'," + + " 'a':1E3, 'b':-1.5e-3, 'c':2.50, 'd':-0, 'q':'it''s here'}") + .statusCode(200); + + getItem("num") + .body("Item.a.N", equalTo("1000")) + .body("Item.b.N", equalTo("-0.0015")) + .body("Item.c.N", equalTo("2.5")) + .body("Item.d.N", equalTo("0")) + .body("Item.q.S", equalTo("it's here")); + statement("SELECT sk FROM \"" + TABLE + "\" WHERE pk = 'num' AND q = 'it''s here' AND a = 1e3") + .statusCode(200) + .body("Items.size()", equalTo(1)); + } + + @Test + @Order(6) + void refusesABagDynamoDbCannotStore() { + statement("INSERT INTO \"" + TABLE + "\" VALUE {'pk':'bag', 'sk':'1', 's':<<>>}") + .statusCode(400) + .body("message", equalTo("Empty bags are not supported")); + statement("INSERT INTO \"" + TABLE + "\" VALUE {'pk':'bag', 'sk':'1', 's':<<1, 'a'>>}") + .statusCode(400) + .body("message", equalTo( + "Unsupported data type in Bag. DynamoDB only supports either numbers or strings in bags")); + statement("INSERT INTO \"" + TABLE + "\" VALUE {'pk':'bag', 'sk':'1', 's':<<'a', 'a'>>}") + .statusCode(400) + .body("message", equalTo( + "One or more parameter values were invalid: Input collection [a, a] contains duplicates.")); + } + + @Test + @Order(7) + void appendsToListsAndChangesSetMembers() { + putItem("sets", "\"AwardDetail\":{\"M\":{\"Grammys\":{\"L\":[{\"N\":\"2020\"},{\"N\":\"2018\"}]}}}," + + "\"BandMembers\":{\"SS\":[\"member1\",\"member2\"]},\"Only\":{\"SS\":[\"only\"]}"); + + update("sets", "SET AwardDetail.Grammys = list_append(AwardDetail.Grammys, [2016])"); + update("sets", "SET AwardDetail.Grammys = list_append([2010], AwardDetail.Grammys)"); + update("sets", "SET BandMembers = set_add(BandMembers, <<'newbandmember'>>)"); + update("sets", "SET BandMembers = set_delete(BandMembers, <<'member1'>>)"); + update("sets", "SET Fresh = set_add(Fresh, <<'m'>>)"); + update("sets", "SET Only = set_delete(Only, <<'only'>>)"); + + getItem("sets") + .body("Item.AwardDetail.M.Grammys.L.N", equalTo(List.of("2010", "2020", "2018", "2016"))) + .body("Item.'#n0.#n1'", nullValue()) + .body("Item.BandMembers.SS", containsInAnyOrder("member2", "newbandmember")) + .body("Item.Fresh.SS", containsInAnyOrder("m")) + .body("Item.Only", nullValue()); + } + + @Test + @Order(8) + void refusesMisusedUpdateFunctions() { + String prefix = "UPDATE \"" + TABLE + "\" SET Other = "; + statement(prefix + "set_add(BandMembers, <<'x'>>) WHERE pk='sets' AND sk='1'") + .statusCode(400) + .body("message", equalTo("The first argument to SET_ADD must equal the assignment value at 1:" + + (prefix.length() + 1) + ":7")); + statement("UPDATE \"" + TABLE + "\" SET BandMembers = set_add(BandMembers, <<1>>) WHERE pk='sets' AND sk='1'") + .statusCode(400) + .body("message", equalTo("An operand in the update expression has an incorrect data type")); + statement("UPDATE \"" + TABLE + "\" SET NoList = list_append(NoList, [1]) WHERE pk='sets' AND sk='1'") + .statusCode(400) + .body("message", equalTo("The provided expression refers to an attribute that does not exist in the item")); + } + + @Test + @Order(9) + void filtersWithFunctionsAndNullChecks() { + putItem("fn-1", "\"Address\":{\"S\":\"7834 24th Kirkland\"},\"tags\":{\"SS\":[\"a\",\"b\"]}," + + "\"Total\":{\"N\":\"400\"},\"nul\":{\"NULL\":true}," + + "\"Image\":{\"B\":\"" + Base64.getEncoder().encodeToString(new byte[400]) + "\"}"); + putItem("fn-2", "\"Total\":{\"N\":\"550\"}"); + + assertKeys("pk IN ['fn-1', 'fn-2'] AND contains(\"Address\", 'Kirkland')", "fn-1"); + assertKeys("pk IN ['fn-1', 'fn-2'] AND contains(tags, 'a')", "fn-1"); + assertKeys("pk IN ['fn-1', 'fn-2'] AND attribute_type(\"Total\", 'N')", "fn-1", "fn-2"); + assertKeys("pk IN ['fn-1', 'fn-2'] AND size(\"Image\") > 300", "fn-1"); + assertKeys("pk IN ['fn-1', 'fn-2'] AND Total != 400", "fn-2"); + assertKeys("pk IN ['fn-1', 'fn-2'] AND nul IS NULL", "fn-1"); + assertKeys("pk IN ['fn-1', 'fn-2'] AND nul IS NOT NULL", "fn-2"); + assertKeys("pk IN ['fn-1', 'fn-2'] AND absent IS NULL"); + + statement("SELECT pk FROM \"" + TABLE + "\" WHERE pk = 'fn-1' AND attribute_type(Total, 'X')") + .statusCode(400) + .body("message", equalTo( + "Invalid attribute type name found; type: X, valid types: {N,BS,L,B,NULL,M,S,SS,NS,BOOL}")); + } + + @Test + @Order(10) + void ordersByTheKey() { + for (String pk : List.of("o", "p")) { + for (String sk : List.of("1", "2", "3")) { + request("DynamoDB_20120810.PutItem", """ + {"TableName":"%s","Item":{"pk":{"S":"%s"},"sk":{"S":"%s"}}} + """.formatted(TABLE, pk, sk)) + .statusCode(200); + } + } + + assertOrder("pk = 'o' ORDER BY sk DESC", "o/3", "o/2", "o/1"); + assertOrder("pk = 'o' ORDER BY sk", "o/1", "o/2", "o/3"); + assertOrder("pk = 'o' ORDER BY pk ASC", "o/3", "o/2", "o/1"); + assertOrder("pk IN ['o', 'p'] ORDER BY pk DESC, sk ASC", "p/1", "p/2", "p/3", "o/1", "o/2", "o/3"); + + String firstToken = request("DynamoDB_20120810.ExecuteStatement", + orderedBody("pk IN ['o', 'p'] ORDER BY pk DESC", 2, null)) + .statusCode(200) + .body("Items.sk.S", equalTo(List.of("3", "2"))) + .body("NextToken", notNullValue()) + .extract().path("NextToken"); + request("DynamoDB_20120810.ExecuteStatement", orderedBody("pk IN ['o', 'p'] ORDER BY pk DESC", 2, firstToken)) + .statusCode(200) + .body("Items.pk.S", equalTo(List.of("p", "o"))) + .body("Items.sk.S", equalTo(List.of("1", "3"))); + } + + @Test + @Order(11) + void refusesAnOrderByItCannotServe() { + String select = "SELECT sk FROM \"" + TABLE + "\" "; + statement(select + "ORDER BY sk") + .statusCode(400) + .body("message", equalTo("Must have WHERE clause in the statement when using ORDER BY clause.")); + statement(select + "WHERE pk = 'o' ORDER BY Total DESC") + .statusCode(400) + .body("message", equalTo("Attribute Total in ORDER BY clause must be part of the primary key")); + statement(select + "WHERE pk > 'a' ORDER BY sk") + .statusCode(400) + .body("message", equalTo("Must have at least one non-optional hash key condition in WHERE clause" + + " when using ORDER BY clause.")); + statement(select + "WHERE pk IN ['o', 'p'] ORDER BY sk DESC") + .statusCode(400) + .body("message", equalTo("Must have hash key in ORDER BY clause when more than one hash key condition" + + " specified in WHERE clause.")); + } + + @Test + @Order(12) + void checksAConditionWithExists() { + putItem("x1", "\"free\":{\"S\":\"x\"}"); + putItem("x2", "\"free\":{\"S\":\"x\"}"); + putItem("x3", "\"Awards\":{\"N\":\"1\"}"); + + transaction("EXISTS(SELECT * FROM \"" + TABLE + "\" WHERE pk = 'x1' AND sk = '1' AND Awards IS MISSING)", + "UPDATE \"" + TABLE + "\" SET AwardsWon=1 WHERE pk = 'x2' AND sk = '1'") + .statusCode(200); + getItem("x2").body("Item.AwardsWon.N", equalTo("1")); + + transaction("EXISTS(SELECT * FROM \"" + TABLE + "\" WHERE pk = 'x3' AND sk = '1' AND Awards IS MISSING)", + "UPDATE \"" + TABLE + "\" SET Touched=1 WHERE pk = 'x1' AND sk = '1'") + .statusCode(400) + .body("__type", equalTo("TransactionCanceledException")) + .body("message", equalTo("Transaction cancelled, please refer cancellation reasons for specific reasons" + + " [ConditionalCheckFailed, None]")) + .body("CancellationReasons[0].Code", equalTo("ConditionalCheckFailed")) + .body("CancellationReasons[0].Message", equalTo("The conditional request failed")) + .body("CancellationReasons[1].Code", equalTo("None")) + .body("CancellationReasons[1]", not(hasKey("Message"))); + + transaction("EXISTS(SELECT * FROM \"" + TABLE + "\" WHERE pk = 'x1' AND sk = '1')", + "UPDATE \"" + TABLE + "\" SET Touched=1 WHERE pk = 'x2' AND sk = '1'") + .statusCode(400) + .body("message", equalTo("Validation failed in TransactStatements[0]:" + + " EXISTS() must contain a single item read with additional condition")); + + statement("EXISTS(SELECT * FROM \"" + TABLE + "\" WHERE pk = 'x1' AND sk = '1' AND Awards IS MISSING)") + .statusCode(400) + .body("message", equalTo("EXISTS can only be used in ExecuteTransaction write requests.")); + + transaction("SELECT * FROM \"" + TABLE + "\" WHERE pk = 'x3' AND sk = '1'", + "UPDATE \"" + TABLE + "\" SET Touched=1 WHERE pk = 'x3' AND sk = '1'") + .statusCode(400) + .body("message", equalTo( + "ExecuteTransaction API does not support both read and write operations in the same request.")); + } + + private void assertKeys(String where, String... pks) { + statement("SELECT pk FROM \"" + TABLE + "\" WHERE " + where) + .statusCode(200) + .body("Items.pk.S", containsInAnyOrder((Object[]) pks)); + } + + private void assertOrder(String whereAndOrder, String... rows) { + List expected = List.of(rows); + request("DynamoDB_20120810.ExecuteStatement", orderedBody(whereAndOrder, null, null)) + .statusCode(200) + .body("Items.pk.S", equalTo(expected.stream().map(r -> r.split("/")[0]).toList())) + .body("Items.sk.S", equalTo(expected.stream().map(r -> r.split("/")[1]).toList())); + } + + private static String orderedBody(String whereAndOrder, Integer limit, String nextToken) { + String statement = "SELECT pk, sk FROM \"" + TABLE + "\" WHERE " + whereAndOrder; + return "{\"Statement\":" + json(statement) + + (limit == null ? "" : ",\"Limit\":" + limit) + + (nextToken == null ? "" : ",\"NextToken\":" + json(nextToken)) + "}"; + } + + private static void update(String pk, String action) { + statement("UPDATE \"" + TABLE + "\" " + action + " WHERE pk='" + pk + "' AND sk='1'").statusCode(200); + } + + private static ValidatableResponse getItem(String pk) { + return request("DynamoDB_20120810.GetItem", """ + {"TableName":"%s","Key":{"pk":{"S":"%s"},"sk":{"S":"1"}},"ConsistentRead":true} + """.formatted(TABLE, pk)) + .statusCode(200); + } + + private static void putItem(String pk, String attributes) { + request("DynamoDB_20120810.PutItem", """ + {"TableName":"%s","Item":{"pk":{"S":"%s"},"sk":{"S":"1"},%s}} + """.formatted(TABLE, pk, attributes)) + .statusCode(200); + } + + private static ValidatableResponse statement(String partiql) { + return request("DynamoDB_20120810.ExecuteStatement", "{\"Statement\":" + json(partiql) + "}"); + } + + private static ValidatableResponse transaction(String... statements) { + String members = Arrays.stream(statements) + .map(s -> "{\"Statement\":" + json(s) + "}") + .collect(Collectors.joining(",")); + return request("DynamoDB_20120810.ExecuteTransaction", "{\"TransactStatements\":[" + members + "]}"); + } + + private static String json(String text) { + return "\"" + text.replace("\\", "\\\\").replace("\"", "\\\"") + "\""; + } + + private static ValidatableResponse request(String target, String body) { + return given() + .header("X-Amz-Target", target) + .contentType(CONTENT_TYPE) + .body(body) + .when() + .post("/") + .then(); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLEdgeCaseIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLEdgeCaseIntegrationTest.java new file mode 100644 index 0000000000..08907415d7 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLEdgeCaseIntegrationTest.java @@ -0,0 +1,732 @@ +package io.github.hectorvent.floci.services.dynamodb; + +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; + +/** + * PartiQL edge cases, with every expectation captured from real DynamoDB (eu-west-2, 2026-09-17). + */ +@QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class DynamoDbPartiQLEdgeCaseIntegrationTest { + + private static final String CONTENT_TYPE = "application/x-amz-json-1.0"; + private static final String TABLE = "partiql-edge-cases"; + private static final String QUOTED_TABLE = "\"" + TABLE + "\""; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + @Order(1) + void createTable() { + request("DynamoDB_20120810.CreateTable", """ + { + "TableName": "%s", + "AttributeDefinitions": [ + {"AttributeName":"pk","AttributeType":"S"}, + {"AttributeName":"sk","AttributeType":"S"} + ], + "KeySchema": [ + {"AttributeName":"pk","KeyType":"HASH"}, + {"AttributeName":"sk","KeyType":"RANGE"} + ], + "BillingMode": "PAY_PER_REQUEST" + } + """.formatted(TABLE)) + .statusCode(200); + } + + @Test + @Order(2) + void readsKeyEqualitiesInsideAGroupedAnd() { + putItem("grouped", "\"flag\":{\"S\":\"x\"}"); + putItem("grouped-other", "\"flag\":{\"S\":\"x\"}"); + + statement("UPDATE " + QUOTED_TABLE + " SET a=2 WHERE (pk='grouped' AND sk='1') AND flag='x'") + .statusCode(200); + getItem("grouped").body("Item.a.N", equalTo("2")); + + transaction("EXISTS(SELECT * FROM " + QUOTED_TABLE + " WHERE (pk='grouped' AND sk='1') AND nope IS MISSING)", + "UPDATE " + QUOTED_TABLE + " SET b=1 WHERE pk='grouped-other' AND sk='1'") + .statusCode(200); + getItem("grouped-other").body("Item.b.N", equalTo("1")); + + statement("DELETE FROM " + QUOTED_TABLE + " WHERE (pk='grouped' AND sk='1') AND flag='x'") + .statusCode(200); + getItem("grouped").body("Item", nullValue()); + } + + @Test + @Order(3) + void subtractsWithoutSpacesAndRefusesTrailingTokens() { + putItem("minus", "\"n\":{\"N\":\"5\"}"); + + statement("UPDATE " + QUOTED_TABLE + " SET n=n-1 WHERE pk='minus' AND sk='1'").statusCode(200); + getItem("minus").body("Item.n.N", equalTo("4")); + + statement("SELECT sk FROM " + QUOTED_TABLE + " WHERE pk='minus' AND n>-5;") + .statusCode(200) + .body("Items.size()", equalTo(1)); + statement("UPDATE " + QUOTED_TABLE + " SET t=1 WHERE pk='minus' AND sk='1' garbage") + .statusCode(400) + .body("message", equalTo( + "Statement wasn't well formed, can't be processed: Unexpected token after expression")); + getItem("minus").body("Item.t", nullValue()); + } + + @Test + @Order(4) + void refusesANegativeListIndex() { + putItem("index", "\"l\":{\"L\":[{\"S\":\"a\"},{\"S\":\"b\"}]}"); + String select = "SELECT l[-1] FROM " + QUOTED_TABLE + " WHERE pk='index' AND sk='1'"; + + statement(select) + .statusCode(400) + .body("message", equalTo("List index is not within the allowable range; index: [-1] at 1:11:1")); + statement("SELECT l[-0] FROM " + QUOTED_TABLE + " WHERE pk='index' AND sk='1'") + .statusCode(200) + .body("Items[0].'l[0]'.S", equalTo("a")); + } + + @Test + @Order(15) + void refusesAListIndexAboveTheIntegerRange() { + statement("SELECT l[2147483648] FROM " + QUOTED_TABLE + " WHERE pk='index' AND sk='1'") + .statusCode(400) + .body("message", equalTo("List index is not within the allowable range; index: [2147483648] at 1:10:10")); + statement("UPDATE " + QUOTED_TABLE + " REMOVE l[2147483648] WHERE pk='index' AND sk='1'") + .statusCode(400); + statement("SELECT l[2147483647] FROM " + QUOTED_TABLE + " WHERE pk='index' AND sk='1'") + .statusCode(200); + getItem("index").body("Item.l.L.size()", equalTo(2)); + } + + @Test + @Order(5) + void refusesANonStringTypeInAttributeType() { + statement("SELECT sk FROM " + QUOTED_TABLE + " WHERE pk='index' AND attribute_type(l, 1)") + .statusCode(400) + .body("message", equalTo("Incorrect operand type for operator or function;" + + " operator or function: attribute_type, operand type: N")); + statement("SELECT sk FROM " + QUOTED_TABLE + " WHERE pk='index' AND attribute_type(l, true)") + .statusCode(400) + .body("message", equalTo("Incorrect operand type for operator or function;" + + " operator or function: attribute_type, operand type: BOOL")); + } + + @Test + @Order(6) + void refusesAValueThatIsNotASetInSetAdd() { + putItem("sets", "\"BandMembers\":{\"SS\":[\"member1\",\"member2\"]}"); + String prefix = "UPDATE " + QUOTED_TABLE + "\nSET BandMembers = set_add("; + + statement(prefix + "BandMembers, 'x')\nWHERE pk='sets' AND sk='1'") + .statusCode(400) + .body("message", equalTo("The second argument to SET_ADD must be a value with type SET at 2:27:11")); + String quotedPrefix = "UPDATE " + QUOTED_TABLE + " SET \"BandMembers\" = set_delete("; + statement(quotedPrefix + "\"BandMembers\", ['x']) WHERE pk='sets' AND sk='1'") + .statusCode(400) + .body("message", equalTo("The second argument to SET_DELETE must be a value with type SET at 1:" + + (quotedPrefix.length() + 1) + ":13")); + getItem("sets").body("Item.BandMembers.SS", containsInAnyOrder("member1", "member2")); + } + + @Test + @Order(7) + void refusesDuplicateMembersInABag() { + putItem("bags", "\"s\":{\"SS\":[\"a\"]},\"n\":{\"NS\":[\"1\"]}"); + String update = "UPDATE " + QUOTED_TABLE + " SET "; + String where = " WHERE pk='bags' AND sk='1'"; + + statement(update + "s = set_add(s, <<'b','b'>>)" + where) + .statusCode(400) + .body("message", equalTo( + "One or more parameter values were invalid: Input collection [b, b] contains duplicates.")); + statement(update + "n = set_add(n, <<2, 2.0>>)" + where) + .statusCode(400) + .body("message", equalTo( + "One or more parameter values were invalid: Input collection [2, 2.0] contains duplicates! under root")); + statement("SELECT sk FROM " + QUOTED_TABLE + where + " AND s = <<'a','a'>>") + .statusCode(400) + .body("message", equalTo( + "One or more parameter values were invalid: Input collection [a, a] contains duplicates.")); + transaction("INSERT INTO " + QUOTED_TABLE + " VALUE {'pk':'bags-tx','sk':'1','x':[<<'a','a'>>]}") + .statusCode(400) + .body("message", equalTo("Validation failed in TransactStatements[0]:" + + " One or more parameter values were invalid: Input collection [a, a] contains duplicates.")); + getItem("bags-tx").body("Item", nullValue()); + + statement(update + "n = <<1, 10>>" + where).statusCode(200); + getItem("bags") + .body("Item.s.SS", containsInAnyOrder("a")) + .body("Item.n.NS", containsInAnyOrder("1", "10")); + } + + @Test + @Order(8) + void refusesAnInvalidParameterBeforeReadingTheStatement() { + String duplicates = "One or more parameter values were invalid: Input collection [a, a] contains duplicates."; + String insert = "INSERT INTO " + QUOTED_TABLE + " VALUE {'pk':'params','sk':'1','x':?}"; + String update = "UPDATE " + QUOTED_TABLE + " SET y = ? WHERE pk='params' AND sk='1'"; + + statement("UPDATE " + QUOTED_TABLE + " SET WHERE", "[{\"SS\":[\"a\",\"a\"]}]") + .statusCode(400) + .body("message", equalTo(duplicates)); + statement(insert, "[{\"NULL\":false}]") + .statusCode(400) + .body("message", equalTo( + "One or more parameter values were invalid: Null attribute value types must have the value of true")); + request("DynamoDB_20120810.ExecuteTransaction", "{\"TransactStatements\":[" + + member(update, "[{\"N\":\"1\"}]") + "," + member(insert, "[{\"SS\":[\"a\",\"a\"]}]") + "]}") + .statusCode(400) + .body("message", equalTo(duplicates)); + request("DynamoDB_20120810.BatchExecuteStatement", + "{\"Statements\":[" + member(update, "[{\"SS\":[]}]") + "]}") + .statusCode(200) + .body("Responses[0].Error.Code", equalTo("ValidationError")) + .body("Responses[0].Error.Message", equalTo( + "One or more parameter values were invalid: An string set may not be empty")); + getItem("params").body("Item", nullValue()); + } + + @Test + @Order(9) + void refusesTwoReadsOfOneItemInATransaction() { + putItem("reads", "\"a\":{\"S\":\"x\"}"); + String select = "SELECT * FROM " + QUOTED_TABLE + " WHERE pk='reads' AND sk='1'"; + + transaction(select, "SELECT a FROM " + QUOTED_TABLE + " WHERE sk='1' AND pk='reads'") + .statusCode(400) + .body("message", equalTo("Transaction request cannot include multiple operations on one item")); + transaction(select, select, "SELECT * FROM " + QUOTED_TABLE + " WHERE pk=1 AND sk='1'") + .statusCode(400) + .body("message", equalTo("Transaction cancelled, please refer cancellation reasons for specific reasons" + + " [None, None, ValidationError]")); + transaction(select, "SELECT * FROM " + QUOTED_TABLE + " WHERE pk='reads' AND sk='2'") + .statusCode(200) + .body("Responses[0].Item.a.S", equalTo("x")); + } + + @Test + @Order(10) + void readsOnlyByTheKeyInATransaction() { + putItem("keyed", "\"flag\":{\"S\":\"right\"}"); + String select = "SELECT * FROM " + QUOTED_TABLE + " WHERE "; + String refused = "Validation failed in TransactStatements[0]:" + + " Select statements within ExecuteTransaction must specify the primary key in the where clause."; + + transaction(select + "pk='keyed' AND sk='1' AND flag='right'") + .statusCode(400) + .body("message", equalTo(refused)); + transaction(select + "pk='keyed' AND sk='1' AND pk='reads'") + .statusCode(400) + .body("message", equalTo(refused)); + transaction(select + "pk='keyed'") + .statusCode(400) + .body("message", equalTo(refused)); + transaction(select + "(sk='1') AND pk='keyed' AND pk='keyed'") + .statusCode(200) + .body("Responses[0].Item.flag.S", equalTo("right")); + } + + @Test + @Order(11) + void refusesUnprojectedAttributesInsideGroupedConditionsOnAKeyedIndexRead() { + request("DynamoDB_20120810.CreateTable", """ + { + "TableName": "partiql-edge-cases-index", + "AttributeDefinitions": [ + {"AttributeName":"pk","AttributeType":"S"}, + {"AttributeName":"gsiPk","AttributeType":"S"} + ], + "KeySchema": [{"AttributeName":"pk","KeyType":"HASH"}], + "GlobalSecondaryIndexes": [{ + "IndexName": "gsi-inc", + "KeySchema": [{"AttributeName":"gsiPk","KeyType":"HASH"}], + "Projection": {"ProjectionType":"INCLUDE","NonKeyAttributes":["projattr"]} + }], + "BillingMode": "PAY_PER_REQUEST" + } + """) + .statusCode(200); + request("DynamoDB_20120810.PutItem", """ + {"TableName":"partiql-edge-cases-index", + "Item":{"pk":{"S":"p"},"gsiPk":{"S":"y"},"projattr":{"S":"proj1"},"nonproj":{"S":"np1"}}} + """) + .statusCode(200); + String select = "SELECT pk FROM \"partiql-edge-cases-index\".\"gsi-inc\" WHERE gsiPk='y' AND "; + String refused = "One or more parameter values were invalid: Secondary index gsi-inc" + + " does not project one or more filter attributes: "; + + statement(select + "NOT (nonproj='np1' OR projattr='x')") + .statusCode(400) + .body("message", equalTo(refused + "[nonproj]")); + statement(select + "b='1' AND (a='2' OR projattr='3')") + .statusCode(400) + .body("message", equalTo(refused + "[a, b]")); + statement(select + "(zz='1' OR aa='2' OR mm='3')") + .statusCode(400) + .body("message", equalTo(refused + "[zz, aa, mm]")); + statement(select + "(projattr='proj1' OR projattr='proj2')") + .statusCode(200) + .body("Items[0].pk.S", equalTo("p")); + } + + @Test + @Order(12) + void skipsLineAndBlockComments() { + putItem("comments", "\"n\":{\"N\":\"1\"},\"note\":{\"S\":\"a--b /*c*/\"}"); + String key = "pk='comments' AND sk='1'"; + + statement("SELECT/**/* FROM " + QUOTED_TABLE + " -- the table\nWHERE pk/* key */='comments' AND sk='1' --") + .statusCode(200) + .body("Items[0].note.S", equalTo("a--b /*c*/")); + statement("SELECT note FROM " + QUOTED_TABLE + " WHERE " + key + " AND note = 'a--b /*c*/'; -- done") + .statusCode(200) + .body("Items.size()", equalTo(1)); + statement("UPDATE " + QUOTED_TABLE + " SET n=n--1 WHERE " + key) + .statusCode(400); + statement("SELECT * FROM " + QUOTED_TABLE + " WHERE " + key + " /* open") + .statusCode(400); + getItem("comments").body("Item.n.N", equalTo("1")); + } + + @Test + @Order(13) + void appliesASignToANumberLiteral() { + putItem("signs", "\"n\":{\"N\":\"1\"}"); + String select = "SELECT sk FROM " + QUOTED_TABLE + " WHERE pk='signs' AND sk='1' AND "; + + statement(select + "n = - - 1").statusCode(200).body("Items.size()", equalTo(1)); + statement(select + "n > -/* c */-1").statusCode(200).body("Items.size()", equalTo(0)); + statement(select + "n BETWEEN - 2 AND +5 AND n IN [- 1, 1]").statusCode(200).body("Items.size()", equalTo(1)); + statement(select + "n > -?").statusCode(400); + statement(select + "n > -n").statusCode(400); + + statement("UPDATE " + QUOTED_TABLE + " SET n = n - - 2, l = [- 1] WHERE pk='signs' AND sk='1'").statusCode(200); + getItem("signs") + .body("Item.n.N", equalTo("3")) + .body("Item.l.L[0].N", equalTo("-1")); + } + + @Test + @Order(14) + void refusesAParameterInsideACollectionLiteral() { + String prefix = "UPDATE " + QUOTED_TABLE + " SET x = "; + + statement(prefix + "[?] WHERE pk='literals' AND sk='1'", "[{\"S\":\"v\"}]") + .statusCode(400) + .body("message", equalTo("Unsupported data type: Parameter under key root[0] at 1:" + + (prefix.length() + 2) + ":1")); + String insert = "INSERT INTO " + QUOTED_TABLE + " VALUE {'pk':'literals','sk':'1','m':{'k':?}}"; + statement(insert, "[{\"S\":\"v\"}]") + .statusCode(400) + .body("message", equalTo("Unsupported data type: Parameter under key root.k at 1:" + + (insert.indexOf('?') + 1) + ":1")); + getItem("literals").body("Item", nullValue()); + + statement("INSERT INTO " + QUOTED_TABLE + " VALUE {'pk':?,'sk':'1','n':1}", "[{\"S\":\"literals\"}]") + .statusCode(200); + statement("SELECT sk FROM " + QUOTED_TABLE + " WHERE pk='literals' AND n IN [?, 2]", "[{\"N\":\"1\"}]") + .statusCode(200) + .body("Items.size()", equalTo(1)); + } + + @Test + @Order(16) + void refusesALiteralNested32LevelsDeep() { + String update = "UPDATE " + QUOTED_TABLE + " SET d = "; + String where = " WHERE pk='nested' AND sk='1'"; + + statement(update + "{'a':".repeat(32) + "1" + "}".repeat(32) + where) + .statusCode(400) + .body("message", equalTo("Nesting Levels have exceeded supported limits under root" + ".a".repeat(32))); + statement(update + "{'a':" + "[".repeat(31) + "1" + "]".repeat(31) + "}" + where) + .statusCode(400) + .body("message", equalTo("Nesting Levels have exceeded supported limits under root.a" + "[0]".repeat(31))); + transaction(update + "[".repeat(32) + "1" + "]".repeat(32) + where) + .statusCode(400) + .body("message", equalTo("Validation failed in TransactStatements[0]:" + + " Nesting Levels have exceeded supported limits under root" + "[0]".repeat(32))); + + statement("INSERT INTO " + QUOTED_TABLE + " VALUE {'pk':'nested','sk':'1','d':" + + "{'a':".repeat(31) + "1" + "}".repeat(31) + "}") + .statusCode(200); + getItem("nested").body("Item.d.M.a.M.a.M", notNullValue()); + } + + @Test + @Order(17) + void refusesANonNumberLiteralInArithmeticBeforeTheCondition() { + putItem("arithmetic", "\"n\":{\"N\":\"1\"}"); + String update = "UPDATE " + QUOTED_TABLE + " SET n = "; + String failingCondition = " WHERE pk='arithmetic' AND sk='1' AND nope='x'"; + + statement(update + "'a' + 1" + failingCondition) + .statusCode(400) + .body("message", equalTo("Incorrect operand type for operator or function; operator or function: +, operand type: S")); + statement(update + "n - null" + failingCondition) + .statusCode(400) + .body("message", equalTo("Incorrect operand type for operator or function; operator or function: -, operand type: NULL")); + transaction(update + "n + [1] WHERE pk='arithmetic' AND sk='1'") + .statusCode(400) + .body("message", equalTo("Validation failed in TransactStatements[0]:" + + " Incorrect operand type for operator or function; operator or function: +, operand type: L")); + getItem("arithmetic").body("Item.n.N", equalTo("1")); + } + + @Test + @Order(18) + void readsAttributeExistsAndAttributeNotExists() { + putItem("exists", "\"m\":{\"M\":{\"x\":{\"S\":\"y\"}}},\"l\":{\"L\":[{\"S\":\"a\"}]}"); + String select = "SELECT sk FROM " + QUOTED_TABLE + " WHERE pk='exists' AND sk='1' AND "; + + statement(select + "attribute_exists(m.x) AND ATTRIBUTE_NOT_EXISTS (nope) AND attribute_exists(l[0])") + .statusCode(200) + .body("Items.size()", equalTo(1)); + statement(select + "(attribute_exists(nope) OR NOT attribute_exists(\"m\"))") + .statusCode(200) + .body("Items.size()", equalTo(0)); + statement(select + "attribute_exists('m')").statusCode(400); + + statement("UPDATE " + QUOTED_TABLE + " SET t=1 WHERE pk='exists' AND sk='1' AND attribute_exists(nope)") + .statusCode(400) + .body("__type", equalTo("ConditionalCheckFailedException")); + getItem("exists").body("Item.t", nullValue()); + } + + @Test + @Order(19) + void comparesAConditionWithABoolean() { + putItem("booleans", "\"n\":{\"N\":\"1\"},\"flag\":{\"S\":\"right\"},\"yes\":{\"BOOL\":true}"); + String select = "SELECT sk FROM " + QUOTED_TABLE + " WHERE pk='booleans' AND sk='1' AND "; + + for (String holds : List.of("attribute_exists(nope) = false", "true = attribute_exists(n)", + "attribute_exists(n) <> 1", "contains(nope, 'z') = false", "(n BETWEEN 0 AND 5) = true", + "(n = 5 OR flag = 'x') = false", "attribute_exists(nope) IN [true, false]", + "attribute_exists(n) = attribute_exists(flag)", "attribute_exists(n) = yes")) { + statement(select + holds).statusCode(200).body("Items.size()", equalTo(1)); + } + for (String fails : List.of("attribute_exists(n) = 1", "NOT attribute_exists(n) = true", + "(attribute_exists(n) = true) = false", "attribute_exists(n) = flag")) { + statement(select + fails).statusCode(200).body("Items.size()", equalTo(0)); + } + statement(select + "attribute_exists(n) > false") + .statusCode(400) + .body("message", equalTo("Incorrect operand type for operator or function; operator or function: >, operand type: BOOL")); + } + + @Test + @Order(20) + void readsSortKeyConditionsThatAQueryKeyCannotHold() { + putItem("ranges", "\"flag\":{\"S\":\"one\"}"); + putItem("ranges", "2", "\"flag\":{\"S\":\"two\"}"); + String select = "SELECT sk FROM " + QUOTED_TABLE + " WHERE pk='ranges' AND "; + + for (String both : List.of("(sk='1' OR sk='2')", "sk IN ['1','2']", "(sk < '1' OR sk > '1' OR sk='1')", + "attribute_exists(pk)")) { + statement(select + both).statusCode(200).body("Items.sk.S", containsInAnyOrder("1", "2")); + } + for (String second : List.of("NOT sk='1'", "sk <> '1'", "sk > '0' AND sk <> '1'")) { + statement(select + second).statusCode(200).body("Items.sk.S", containsInAnyOrder("2")); + } + statement(select + "sk='1' AND sk > '0'").statusCode(200).body("Items.sk.S", containsInAnyOrder("1")); + } + + @Test + @Order(21) + void refusesOverlappingKeyConditionsAndMismatchedKeyTypes() { + String select = "SELECT sk FROM " + QUOTED_TABLE + " WHERE "; + String overlapping = "Overlapping conditions with range keys are not supported in where clause"; + + for (String where : List.of("pk IN ['ranges','ranges']", "pk='ranges' OR pk='ranges'", + "pk='ranges' AND (sk > '0' OR sk < '5')", "pk='ranges' AND (sk='1' OR flag='two')", + "(pk='ranges' AND flag='one') OR (pk='ranges' AND flag='two')")) { + statement(select + where).statusCode(400).body("message", equalTo(overlapping)); + } + transaction(select + "pk IN ['ranges','ranges'] AND sk='1'") + .statusCode(400) + .body("message", equalTo("Validation failed in TransactStatements[0]: " + overlapping)); + statement(select + "pk='ranges' OR sk='1'").statusCode(200); + + statement(select + "pk = 1 OR pk = 'ranges'") + .statusCode(400) + .body("message", equalTo("Key attribute's data type should match its data type in table's schema: Key pk")); + statement(select + "pk = 'ranges' AND sk IN ['1', 2]") + .statusCode(400) + .body("message", equalTo("Key attribute's data type should match its data type in table's schema: Key sk")); + statement(select + "pk > 1").statusCode(200).body("Items.size()", equalTo(0)); + } + + @Test + @Order(22) + void checksProjectionOnEachKeyedBranchOfAnIndexRead() { + String select = "SELECT pk FROM \"partiql-edge-cases-index\".\"gsi-inc\" WHERE "; + String refused = "One or more parameter values were invalid: Secondary index gsi-inc" + + " does not project one or more filter attributes: "; + + statement(select + "(gsiPk='y' OR gsiPk='z') AND zz='1'") + .statusCode(400) + .body("message", equalTo(refused + "[zz]")); + statement(select + "(gsiPk='b' AND u1='1') OR (gsiPk='q' AND u2='1')") + .statusCode(400) + .body("message", equalTo(refused + "[u2]")); + statement(select + "(gsiPk='q' AND u1='1') OR (gsiPk='b' AND u2='1')") + .statusCode(400) + .body("message", equalTo(refused + "[u1]")); + for (String accepted : List.of("(gsiPk='y' AND projattr='1') OR (gsiPk='z' AND aa='1')", + "gsiPk > 'a' AND zz='1'", "gsiPk='y' AND (gsiPk='z' OR zz='1')", "(gsiPk='y' AND zz='1') OR aa='1'")) { + statement(select + accepted).statusCode(200); + } + statement(select + "(gsiPk='y' AND zz='1') OR (gsiPk='y' AND aa='1')") + .statusCode(400) + .body("message", equalTo("Overlapping conditions with range keys are not supported in where clause")); + } + + @Test + @Order(23) + void readsByAOneValueInAndRefusesAnythingBesideTheKeyInTransactionAndBatchReads() { + String select = "SELECT sk FROM " + QUOTED_TABLE + " WHERE "; + String batchRefused = "Select statements within BatchExecuteStatement must specify the primary key in the where clause."; + + transaction(select + "pk IN ['ranges'] AND sk IN ['1']") + .statusCode(200) + .body("Responses[0].Item.sk.S", equalTo("1")); + transaction(select + "(pk IN ['ranges']) AND pk='ranges' AND sk='2'") + .statusCode(200) + .body("Responses[0].Item.sk.S", equalTo("2")); + transaction(select + "pk IN ['other'] AND pk='ranges' AND sk='1'") + .statusCode(400) + .body("message", equalTo("Validation failed in TransactStatements[0]:" + + " Select statements within ExecuteTransaction must specify the primary key in the where clause.")); + transaction("EXISTS(" + select + "pk IN ['ranges'] AND sk='1' AND flag='one')", + "UPDATE " + QUOTED_TABLE + " SET b=1 WHERE pk='ranges' AND sk='2'") + .statusCode(400) + .body("message", equalTo("Validation failed in TransactStatements[0]:" + + " EXISTS() must contain a single item read with additional condition")); + + request("DynamoDB_20120810.BatchExecuteStatement", "{\"Statements\":[" + + "{\"Statement\":" + json(select + "pk IN ['ranges'] AND sk='1'") + "}," + + "{\"Statement\":" + json(select + "pk='ranges' AND sk='1' AND flag='one'") + "}," + + "{\"Statement\":" + json(select + "pk='ranges' AND sk='1' AND pk='other'") + "}]}") + .statusCode(200) + .body("Responses[0].Item.sk.S", equalTo("1")) + .body("Responses[1].Error.Message", equalTo(batchRefused)) + .body("Responses[2].Error.Message", equalTo(batchRefused)); + } + + @Test + @Order(24) + void refusesTwoValuesForOneKeyOnAWriteAndReadsTheFirstInExists() { + putItem("conflict", "\"flag\":{\"S\":\"right\"}"); + putItem("other", "\"flag\":{\"S\":\"right\"}"); + String multiple = "Multiple conditions on same key pk. Only single item Update/Insert/Delete are supported"; + + statement("UPDATE " + QUOTED_TABLE + " SET a=1 WHERE pk='conflict' AND sk='1' AND pk='other'") + .statusCode(400) + .body("message", equalTo(multiple)); + statement("DELETE FROM " + QUOTED_TABLE + " WHERE pk='conflict' AND sk='1' AND pk='other'") + .statusCode(400) + .body("message", equalTo(multiple)); + statement("UPDATE " + QUOTED_TABLE + " SET a=2 WHERE pk='conflict' AND sk='1' AND pk='conflict'") + .statusCode(200); + getItem("conflict").body("Item.a.N", equalTo("2")); + + transaction("EXISTS(SELECT * FROM " + QUOTED_TABLE + " WHERE pk='conflict' AND sk='1' AND pk='other' AND flag='right')", + "UPDATE " + QUOTED_TABLE + " SET b=1 WHERE pk='other' AND sk='1'") + .statusCode(400) + .body("CancellationReasons[0].Code", equalTo("ConditionalCheckFailed")) + .body("CancellationReasons[1].Code", equalTo("None")); + } + + @Test + @Order(25) + void cancelsATransactionThatNamesAMissingTable() { + putItem("missing", "\"flag\":{\"S\":\"right\"}"); + String missingUpdate = "UPDATE \"partiql-edge-cases-missing\" SET a=1 WHERE pk='x' AND sk='1'"; + String select = "SELECT * FROM " + QUOTED_TABLE + " WHERE pk='missing' AND sk='1'"; + + transaction(missingUpdate + " RETURNING ALL NEW *", "DELETE FROM \"partiql-edge-cases-missing\" WHERE pk='x' AND sk='1'") + .statusCode(400) + .body("__type", equalTo("TransactionCanceledException")) + .body("CancellationReasons.Code", contains("ResourceNotFound", "ResourceNotFound")) + .body("CancellationReasons[0].Message", equalTo("Requested resource not found")); + transaction(select, "SELECT * FROM \"partiql-edge-cases-missing\".\"idx\" WHERE pk='x' AND sk='1'") + .statusCode(400) + .body("CancellationReasons.Code", contains("None", "ResourceNotFound")) + .body("CancellationReasons[0].Message", nullValue()); + + transaction(missingUpdate, "UPDATE " + QUOTED_TABLE + " SET a=1 WHERE pk='missing' AND sk='1' RETURNING ALL NEW *") + .statusCode(400) + .body("message", equalTo("Validation failed in TransactStatements[1]: RETURNING clause is not supported in ExecuteTransaction.")); + transaction("SELECT * FROM \"partiql-edge-cases-missing\" WHERE pk='x' AND sk='1'", "SELECT * FROM " + QUOTED_TABLE + ".\"idx\" WHERE pk='x' AND sk='1'") + .statusCode(400) + .body("message", equalTo("Validation failed in TransactStatements[1]: Reads on indices are not supported within transactions.")); + getItem("missing").body("Item.a", nullValue()); + } + + @Test + @Order(26) + void cancelsATransactionWhoseWriteDoesNotNameOneItem() { + putItem("keys", "\"flag\":{\"S\":\"right\"}"); + String update = "UPDATE " + QUOTED_TABLE + " SET a=1 WHERE "; + String mismatch = "The provided key element does not match the schema"; + + transaction(update + "pk='keys'", update + "pk IN ['keys'] AND sk='1'", update + "pk='keys' AND sk='1' AND pk='other'", + update + "pk='keys' AND sk=1", update + "pk='keys' AND sk='1'") + .statusCode(400) + .body("CancellationReasons.Code", contains("ValidationError", "ValidationError", "ValidationError", + "ValidationError", "None")) + .body("CancellationReasons[0].Message", equalTo(mismatch)) + .body("CancellationReasons[3].Message", equalTo(mismatch)); + transaction("INSERT INTO " + QUOTED_TABLE + " VALUE {'pk':'keys-insert'}", "DELETE FROM " + QUOTED_TABLE + " WHERE sk='1'") + .statusCode(400) + .body("CancellationReasons[0].Message", equalTo("One or more parameter values were invalid: Missing the key sk in the item")) + .body("CancellationReasons[1].Message", equalTo(mismatch)); + transaction("SELECT * FROM " + QUOTED_TABLE + " WHERE pk='keys' AND sk=1") + .statusCode(400) + .body("CancellationReasons[0].Message", equalTo(mismatch)); + getItem("keys").body("Item.a", nullValue()); + } + + @Test + @Order(27) + void refusesAWriteThatLeavesTheItemTooDeep() { + putItem("deep-path", "\"m\":{\"M\":{}}"); + String update = "UPDATE " + QUOTED_TABLE + " SET m.deep = ? WHERE pk='deep-path' AND sk='1'"; + String parameters = "[" + "{\"M\":{\"a\":".repeat(31) + "{\"S\":\"x\"}" + "}}".repeat(31) + "]"; + String tooDeep = "Nesting Levels have exceeded supported limits"; + + statement(update, parameters) + .statusCode(400) + .body("message", equalTo(tooDeep)); + request("DynamoDB_20120810.ExecuteTransaction", "{\"TransactStatements\":[" + member(update, parameters) + "]}") + .statusCode(400) + .body("CancellationReasons[0].Code", equalTo("ValidationError")) + .body("CancellationReasons[0].Message", equalTo(tooDeep)); + request("DynamoDB_20120810.BatchExecuteStatement", "{\"Statements\":[" + member(update, parameters) + "]}") + .statusCode(200) + .body("Responses[0].Error.Message", equalTo(tooDeep)) + .body("Responses[0].TableName", equalTo(TABLE)); + getItem("deep-path").body("Item.m.M.deep", nullValue()); + } + + @Test + @Order(28) + void namesAPathColumnFromItsLastNamedComponent() { + putItem("columns", "\"m\":{\"M\":{\"k\":{\"L\":[{\"S\":\"c\"}]}}}"); + + statement("SELECT m.k[0] FROM " + QUOTED_TABLE + " WHERE pk='columns' AND sk='1'") + .statusCode(200) + .body("Items[0].'k[0]'.S", equalTo("c")); + } + + @Test + @Order(29) + void refusesTooManyInOperandsAndTooManyDecomposedReads() { + String select = "SELECT sk FROM " + QUOTED_TABLE + " WHERE "; + String tooManyReads = "Too many decomposed read operations for a given query."; + String tooManyOperands = "The IN operator is provided with too many operands; number of operands: 101"; + + for (String where : List.of("pk IN [" + inValues(51) + "]", + "pk='ranges' AND sk IN [" + inValues(51) + "]", + "pk IN [" + inValues(26) + "] AND sk IN ['1','2']")) { + statement(select + where).statusCode(400).body("message", equalTo(tooManyReads)); + } + for (String where : List.of("pk IN [" + inValues(50) + "] AND flag IN [" + inValues(100) + "]", + "pk IN [" + inValues(51) + "] OR flag='one'")) { + statement(select + where).statusCode(200); + } + request("DynamoDB_20120810.BatchExecuteStatement", + "{\"Statements\":[{\"Statement\":" + json(select + "pk IN [" + inValues(51) + "] AND sk='1'") + "}]}") + .statusCode(200) + .body("Responses[0].Error.Message", equalTo(tooManyReads)); + + statement(select + "pk='ranges' AND flag IN [" + inValues(101) + "]") + .statusCode(400) + .body("message", equalTo(tooManyOperands)); + statement("UPDATE " + QUOTED_TABLE + " SET t=1 WHERE pk='ranges' AND sk='1' AND flag IN [" + inValues(101) + "]") + .statusCode(400) + .body("message", equalTo(tooManyOperands)); + transaction("SELECT * FROM " + QUOTED_TABLE + " WHERE pk IN [" + inValues(101) + "] AND sk='1'") + .statusCode(400) + .body("message", equalTo("Validation failed in TransactStatements[0]: " + tooManyOperands)); + getItem("ranges").body("Item.t", nullValue()); + } + + private static ValidatableResponse getItem(String pk) { + return request("DynamoDB_20120810.GetItem", """ + {"TableName":"%s","Key":{"pk":{"S":"%s"},"sk":{"S":"1"}},"ConsistentRead":true} + """.formatted(TABLE, pk)) + .statusCode(200); + } + + private static void putItem(String pk, String attributes) { + putItem(pk, "1", attributes); + } + + private static void putItem(String pk, String sk, String attributes) { + request("DynamoDB_20120810.PutItem", """ + {"TableName":"%s","Item":{"pk":{"S":"%s"},"sk":{"S":"%s"},%s}} + """.formatted(TABLE, pk, sk, attributes)) + .statusCode(200); + } + + private static ValidatableResponse statement(String partiql) { + return request("DynamoDB_20120810.ExecuteStatement", "{\"Statement\":" + json(partiql) + "}"); + } + + private static ValidatableResponse statement(String partiql, String parameters) { + return request("DynamoDB_20120810.ExecuteStatement", member(partiql, parameters)); + } + + private static ValidatableResponse transaction(String... statements) { + String members = Arrays.stream(statements) + .map(s -> "{\"Statement\":" + json(s) + "}") + .collect(Collectors.joining(",")); + return request("DynamoDB_20120810.ExecuteTransaction", "{\"TransactStatements\":[" + members + "]}"); + } + + private static String member(String partiql, String parameters) { + return "{\"Statement\":" + json(partiql) + ",\"Parameters\":" + parameters + "}"; + } + + private static String inValues(int count) { + return IntStream.range(0, count).mapToObj(i -> "'v" + i + "'").collect(Collectors.joining(",")); + } + + private static String json(String text) { + return "\"" + text.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n") + "\""; + } + + private static ValidatableResponse request(String target, String body) { + return given() + .header("X-Amz-Target", target) + .contentType(CONTENT_TYPE) + .body(body) + .when() + .post("/") + .then(); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLIntegrationTest.java new file mode 100644 index 0000000000..a50ed67916 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLIntegrationTest.java @@ -0,0 +1,342 @@ +package io.github.hectorvent.floci.services.dynamodb; + +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.nullValue; + +@QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class DynamoDbPartiQLIntegrationTest { + + private static final String CONTENT_TYPE = "application/x-amz-json-1.0"; + private static final String TABLE = "partiql-statements"; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + @Order(1) + void createTable() { + request("DynamoDB_20120810.CreateTable", """ + { + "TableName": "%s", + "AttributeDefinitions": [{"AttributeName":"pk","AttributeType":"S"}], + "KeySchema": [{"AttributeName":"pk","KeyType":"HASH"}], + "BillingMode": "PAY_PER_REQUEST" + } + """.formatted(TABLE)) + .statusCode(200); + } + + @Test + @Order(2) + void selectReportsADocumentPathUnderItsLeafName() { + putItem("path-select", """ + "profile":{"M":{"sub":{"S":"deep"},"sib":{"S":"keep"}}}, + "tags":{"L":[{"S":"a"},{"S":"b"}]} + """); + + statement("SELECT profile.sub, tags[1] FROM \"" + TABLE + "\" WHERE pk = 'path-select'") + .statusCode(200) + .body("Items.size()", equalTo(1)) + .body("Items[0].sub.S", equalTo("deep")) + .body("Items[0].'tags[1]'.S", equalTo("b")) + .body("Items[0].profile", nullValue()); + } + + @Test + @Order(3) + void updateWritesThroughADocumentPath() { + putItem("path-update", "\"profile\":{\"M\":{\"sub\":{\"S\":\"old\"}}}"); + + statement("UPDATE \"" + TABLE + "\" SET profile.sub = 'new' WHERE pk = 'path-update'") + .statusCode(200); + + statement("SELECT profile.sub FROM \"" + TABLE + "\" WHERE pk = 'path-update'") + .statusCode(200) + .body("Items[0].sub.S", equalTo("new")); + } + + @Test + @Order(4) + void whereTakesInNotAndTheMissingPredicate() { + putItem("grammar-a", "\"kind\":{\"S\":\"alpha\"}"); + putItem("grammar-b", "\"kind\":{\"S\":\"beta\"}"); + + statement("SELECT pk FROM \"" + TABLE + "\" WHERE pk IN ['grammar-a','grammar-b']" + + " AND NOT begins_with(\"kind\", 'al')") + .statusCode(200) + .body("Items.size()", equalTo(1)) + .body("Items[0].pk.S", equalTo("grammar-b")); + + statement("SELECT pk FROM \"" + TABLE + "\" WHERE pk = 'grammar-a' AND \"kind\" IS NOT MISSING") + .statusCode(200) + .body("Items.size()", equalTo(1)); + + statement("SELECT pk FROM \"" + TABLE + "\" WHERE pk = 'grammar-a' AND \"kind\" IS MISSING") + .statusCode(200) + .body("Items.size()", equalTo(0)); + } + + @Test + @Order(5) + void updateOnAKeyHoldingNoItemFailsTheConditionalCheck() { + statement("UPDATE \"" + TABLE + "\" SET data = 'new' WHERE pk = 'write-ghost'") + .statusCode(400) + .body("__type", equalTo("ConditionalCheckFailedException")); + + request("DynamoDB_20120810.GetItem", """ + {"TableName":"%s","Key":{"pk":{"S":"write-ghost"}}} + """.formatted(TABLE)) + .statusCode(200) + .body("Item", nullValue()); + } + + @Test + @Order(6) + void aFalsePredicateBesideTheKeyLeavesTheItem() { + putItem("write-pred", "\"name\":{\"S\":\"alpha\"},\"n\":{\"N\":\"5\"}"); + + statement("UPDATE \"" + TABLE + "\" SET n = 9 WHERE pk = 'write-pred' AND \"name\" = 'beta'") + .statusCode(400) + .body("__type", equalTo("ConditionalCheckFailedException")); + + statement("DELETE FROM \"" + TABLE + "\" WHERE pk = 'write-pred' AND \"name\" = 'beta'") + .statusCode(400) + .body("__type", equalTo("ConditionalCheckFailedException")); + + request("DynamoDB_20120810.GetItem", """ + {"TableName":"%s","Key":{"pk":{"S":"write-pred"}}} + """.formatted(TABLE)) + .statusCode(200) + .body("Item.n.N", equalTo("5")); + } + + @Test + @Order(7) + void deleteOnAKeyHoldingNoItemIsANoOpEvenWithAPredicate() { + statement("DELETE FROM \"" + TABLE + "\" WHERE pk = 'write-absent' AND \"name\" = 'x'") + .statusCode(200); + } + + @Test + @Order(8) + void aWriteWithoutTheKeyEqualityIsRejected() { + statement("DELETE FROM \"" + TABLE + "\" WHERE \"name\" = 'alpha'") + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo( + "Where clause does not contain a mandatory equality on all key attributes")); + } + + @Test + @Order(9) + void updateReturningReportsTheRowOnTheRequestedSide() { + putItem("ret-update", "\"data\":{\"S\":\"old\"},\"keep\":{\"S\":\"same\"}"); + + statement("UPDATE \"" + TABLE + "\" SET data = 'new' WHERE pk = 'ret-update' RETURNING ALL OLD *") + .statusCode(200) + .body("Items.size()", equalTo(1)) + .body("Items[0].data.S", equalTo("old")) + .body("Items[0].keep.S", equalTo("same")); + + statement("UPDATE \"" + TABLE + "\" SET data = 'newer' WHERE pk = 'ret-update'" + + " RETURNING MODIFIED NEW *") + .statusCode(200) + .body("Items.size()", equalTo(1)) + .body("Items[0].data.S", equalTo("newer")) + .body("Items[0].keep", nullValue()) + .body("Items[0].pk", nullValue()); + } + + @Test + @Order(10) + void anEmptyModifiedProjectionAnswersNoRow() { + putItem("ret-removed", "\"data\":{\"S\":\"old\"}"); + + statement("UPDATE \"" + TABLE + "\" REMOVE data WHERE pk = 'ret-removed'" + + " RETURNING MODIFIED NEW *") + .statusCode(200) + .body("Items.size()", equalTo(0)); + } + + @Test + @Order(11) + void deleteTakesOnlyReturningAllOld() { + putItem("ret-delete", "\"data\":{\"S\":\"gone\"}"); + + statement("DELETE FROM \"" + TABLE + "\" WHERE pk = 'ret-delete' RETURNING MODIFIED OLD *") + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("Invalid returning clause: RETURNING MODIFIED OLD *." + + " Only RETURNING ALL OLD * is allowed in DELETE statements.")); + + statement("DELETE FROM \"" + TABLE + "\" WHERE pk = 'ret-delete' RETURNING ALL OLD *") + .statusCode(200) + .body("Items.size()", equalTo(1)) + .body("Items[0].data.S", equalTo("gone")); + + statement("DELETE FROM \"" + TABLE + "\" WHERE pk = 'ret-delete' RETURNING ALL OLD *") + .statusCode(200) + .body("Items.size()", equalTo(0)); + } + + @Test + @Order(12) + void aTransactionMemberTakesNoReturningClause() { + request("DynamoDB_20120810.ExecuteTransaction", """ + {"TransactStatements":[{"Statement":"UPDATE \\"%s\\" SET data = 'v' WHERE pk = 'ret-txn' RETURNING ALL NEW *"}]} + """.formatted(TABLE)) + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("Validation failed in TransactStatements[0]:" + + " RETURNING clause is not supported in ExecuteTransaction.")); + } + + @Test + @Order(13) + void updateSetsFromAnAttributeAndALiteral() { + putItem("arith", "\"n\":{\"N\":\"5\"}"); + + statement("UPDATE \"" + TABLE + "\" SET n = n + 1 WHERE pk = 'arith' RETURNING MODIFIED NEW *") + .statusCode(200) + .body("Items[0].n.N", equalTo("6")); + + statement("UPDATE \"" + TABLE + "\" SET n = n - 2 WHERE pk = 'arith' RETURNING MODIFIED NEW *") + .statusCode(200) + .body("Items[0].n.N", equalTo("4")); + } + + @Test + @Order(14) + void aReplayedTransactionTokenDoesNotApplyTwice() { + putItem("txn-idem", "\"n\":{\"N\":\"0\"}"); + String body = """ + {"ClientRequestToken":"partiql-idem-token","TransactStatements":[ + {"Statement":"UPDATE \\"%s\\" SET n = n + 1 WHERE pk = 'txn-idem'"}]} + """.formatted(TABLE); + + request("DynamoDB_20120810.ExecuteTransaction", body).statusCode(200); + request("DynamoDB_20120810.ExecuteTransaction", body).statusCode(200); + + request("DynamoDB_20120810.GetItem", """ + {"TableName":"%s","Key":{"pk":{"S":"txn-idem"}}} + """.formatted(TABLE)) + .statusCode(200) + .body("Item.n.N", equalTo("1")); + } + + @Test + @Order(15) + void aWriteStatementRejectsAnIndexQualifier() { + statement("INSERT INTO \"" + TABLE + "\".\"an-index\" VALUE {'pk': 'qualified'}") + .statusCode(400) + .body("message", equalTo("FROM clause may only contain a single table name")); + + statement("UPDATE \"" + TABLE + "\".\"an-index\" SET data = 'x' WHERE pk = 'qualified'") + .statusCode(400) + .body("message", equalTo("This operation is not supported on an index")); + + statement("DELETE FROM \"" + TABLE + "\".\"an-index\" WHERE pk = 'qualified'") + .statusCode(400) + .body("message", equalTo("This operation is not supported on an index")); + + request("DynamoDB_20120810.ExecuteTransaction", """ + {"TransactStatements":[{"Statement":"UPDATE \\"%s\\".\\"an-index\\" SET data = 'x' WHERE pk = 'qualified'"}]} + """.formatted(TABLE)) + .statusCode(400) + .body("message", equalTo("Validation failed in TransactStatements[0]:" + + " This operation is not supported on an index")); + } + + @Test + @Order(16) + void theFromClauseTakesAtMostTwoNonEmptyComponents() { + statement("SELECT * FROM \"\" WHERE pk = 'x'") + .statusCode(400) + .body("message", equalTo("Path component cannot be an empty string")); + + statement("SELECT * FROM \"" + TABLE + "\".\"\" WHERE pk = 'x'") + .statusCode(400) + .body("message", equalTo("Path component cannot be an empty string")); + + statement("SELECT * FROM \"" + TABLE + "\".\"an-index\".\"extra\" WHERE pk = 'x'") + .statusCode(400) + .body("message", equalTo("A path may contain at most 2 components in the FROM clause")); + } + + @Test + @Order(17) + void aTransactionOfSelectsReadsTheItems() { + putItem("txn-read", "\"data\":{\"S\":\"here\"}"); + + request("DynamoDB_20120810.ExecuteTransaction", """ + {"TransactStatements":[{"Statement":"SELECT * FROM \\"%s\\" WHERE pk = 'txn-read'"}]} + """.formatted(TABLE)) + .statusCode(200) + .body("Responses.size()", equalTo(1)) + .body("Responses[0].Item.data.S", equalTo("here")); + + request("DynamoDB_20120810.ExecuteTransaction", """ + {"TransactStatements":[{"Statement":"SELECT * FROM \\"%s\\".\\"an-index\\" WHERE pk = 'txn-read'"}]} + """.formatted(TABLE)) + .statusCode(400) + .body("message", equalTo("Validation failed in TransactStatements[0]:" + + " Reads on indices are not supported within transactions.")); + } + + @Test + @Order(18) + void aBatchMemberCarriesTheShortErrorCode() { + putItem("batch-dup", "\"val\":{\"S\":\"here\"}"); + + request("DynamoDB_20120810.BatchExecuteStatement", """ + {"Statements":[ + {"Statement":"SLECT * FROM \\"%1$s\\" WHERE pk = 'batch-dup'"}, + {"Statement":"INSERT INTO \\"%1$s\\" VALUE {'pk': 'batch-dup'}"}, + {"Statement":"UPDATE \\"%1$s\\" SET touched = 'yes' WHERE pk = 'batch-dup' AND val = 'wrong'"}]} + """.formatted(TABLE)) + .statusCode(200) + .body("Responses[0].Error.Code", equalTo("ValidationError")) + .body("Responses[0].Error.Message", equalTo( + "Statement wasn't well formed, can't be processed: Expected data manipulation")) + .body("Responses[0].TableName", nullValue()) + .body("Responses[1].Error.Code", equalTo("DuplicateItem")) + .body("Responses[1].TableName", equalTo(TABLE)) + .body("Responses[2].Error.Code", equalTo("ConditionalCheckFailed")) + .body("Responses[2].TableName", equalTo(TABLE)); + } + + private static void putItem(String pk, String attributes) { + request("DynamoDB_20120810.PutItem", """ + {"TableName":"%s","Item":{"pk":{"S":"%s"},%s}} + """.formatted(TABLE, pk, attributes)) + .statusCode(200); + } + + private static ValidatableResponse statement(String partiql) { + return request("DynamoDB_20120810.ExecuteStatement", + "{\"Statement\":\"" + partiql.replace("\"", "\\\"") + "\"}"); + } + + private static ValidatableResponse request(String target, String body) { + return given() + .header("X-Amz-Target", target) + .contentType(CONTENT_TYPE) + .body(body) + .when() + .post("/") + .then(); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParserTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParserTest.java index 9aa3f32faa..b37f980df1 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParserTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParserTest.java @@ -5,18 +5,26 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.dynamodb.DynamoDbPartiQLParser.Cond; import io.github.hectorvent.floci.services.dynamodb.DynamoDbPartiQLParser.PVal; +import io.github.hectorvent.floci.services.dynamodb.DynamoDbPartiQLParser.Path; +import io.github.hectorvent.floci.services.dynamodb.DynamoDbPartiQLParser.Seg; import io.github.hectorvent.floci.services.dynamodb.DynamoDbPartiQLParser.Stmt; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + import java.util.Arrays; +import java.util.List; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; class DynamoDbPartiQLParserTest { @@ -160,4 +168,105 @@ class DynamoDbPartiQLParserTest { assertEquals("<", assertInstanceOf(Cond.Cmp.class, cond).op()); } + + @Test + void parsesADocumentPathInASelectColumn() { + Stmt stmt = DynamoDbPartiQLParser.parse("SELECT a.b[2].c FROM \"t\" WHERE pk = 'p'", List.of()); + + Path column = assertInstanceOf(Stmt.Select.class, stmt).columns().getFirst(); + assertEquals(List.of(new Seg.Name("a"), new Seg.Name("b"), new Seg.Index(2), new Seg.Name("c")), + column.segments()); + assertEquals("a", column.root()); + assertEquals("c", column.leafName()); + } + + @Test + void parsesADocumentPathInAnUpdateTarget() { + Stmt stmt = DynamoDbPartiQLParser.parse( + "UPDATE \"t\" SET profile.sub = 'x' REMOVE tags[0] WHERE pk = 'p'", List.of()); + + Stmt.Update update = assertInstanceOf(Stmt.Update.class, stmt); + assertEquals(List.of(new Seg.Name("profile"), new Seg.Name("sub")), + update.sets().getFirst().path().segments()); + assertEquals(List.of(new Seg.Name("tags"), new Seg.Index(0)), + update.removes().getFirst().segments()); + } + + @Test + void keepsABareAttributeNameAsARootOnlyPath() { + Path path = assertInstanceOf(Cond.Eq.class, firstCond(SELECT + "= 'x'")).path(); + + assertTrue(path.isRootOnly()); + assertEquals("val", path.root()); + } + + @Test + void parsesAnInListAsOneCondition() { + Cond.In in = assertInstanceOf(Cond.In.class, firstCond(SELECT + "IN ['a', 'b']")); + + assertEquals(2, in.values().size()); + assertEquals("val", in.path().root()); + } + + @ParameterizedTest + @CsvSource({"IS MISSING, false", "IS NOT MISSING, true"}) + void parsesTheMissingPredicateWithItsNegation(String predicate, boolean negated) { + Cond.Missing missing = assertInstanceOf(Cond.Missing.class, firstCond(SELECT + predicate)); + + assertEquals(negated, missing.negated()); + } + + @Test + void parsesNotAheadOfThePredicateItNegates() { + Stmt stmt = DynamoDbPartiQLParser.parse( + "SELECT pk FROM \"t\" WHERE NOT begins_with(val, 'a')", List.of()); + + Cond.Not not = assertInstanceOf(Cond.Not.class, + assertInstanceOf(Stmt.Select.class, stmt).where().getFirst()); + assertInstanceOf(Cond.BeginsWith.class, not.operand()); + } + + @Test + void parsesAGroupedOrAsASingleTopLevelCondition() { + Stmt stmt = DynamoDbPartiQLParser.parse( + "SELECT pk FROM \"t\" WHERE (pk = 'a' AND v = 1) OR (pk = 'b')", List.of()); + + List where = assertInstanceOf(Stmt.Select.class, stmt).where(); + assertEquals(1, where.size()); + Cond.Or or = assertInstanceOf(Cond.Or.class, where.getFirst()); + assertEquals(2, or.operands().size()); + assertInstanceOf(Cond.And.class, or.operands().getFirst()); + } + + @Test + void rejectsAListIndexThatIsNotAWholeNumber() { + AwsException e = assertThrows(AwsException.class, + () -> DynamoDbPartiQLParser.parse("SELECT a[1.5] FROM \"t\" WHERE pk = 'p'", List.of())); + + assertEquals("ValidationException", e.getErrorCode()); + } + + @Test + void rejectsAnInOperatorWithMoreThan100Operands() { + AwsException e = assertThrows(AwsException.class, + () -> DynamoDbPartiQLParser.parse("SELECT a FROM \"t\" WHERE pk IN [" + inValues(101) + "]", List.of())); + + assertEquals("ValidationException", e.getErrorCode()); + assertEquals("The IN operator is provided with too many operands; number of operands: 101", e.getMessage()); + assertDoesNotThrow(() -> DynamoDbPartiQLParser.parse( + "SELECT a FROM \"t\" WHERE pk IN [" + inValues(100) + "]", List.of())); + } + + @Test + void readsTheWholeStatementBeforeItCountsInOperands() { + AwsException e = assertThrows(AwsException.class, + () -> DynamoDbPartiQLParser.parse("SELECT a FROM \"t\" WHERE pk IN [" + inValues(101) + "] AND", List.of())); + + assertEquals("ValidationException", e.getErrorCode()); + assertNotEquals("The IN operator is provided with too many operands; number of operands: 101", e.getMessage()); + } + + private static String inValues(int count) { + return IntStream.range(0, count).mapToObj(i -> "'v" + i + "'").collect(Collectors.joining(",")); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluatorTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluatorTest.java index e2c83b63a4..aed6eae6ee 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluatorTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/ExpressionEvaluatorTest.java @@ -267,6 +267,14 @@ class ExpressionEvaluatorTest { return mapper.readTree(json); } + @Test + void orderingAgainstAnotherTypeMatchesNothing() throws Exception { + JsonNode i = item("{\"a\": {\"S\": \"5\"}}"); + JsonNode v = values("{\":low\": {\"N\": \"1\"}, \":high\": {\"N\": \"9\"}}"); + assertFalse(ExpressionEvaluator.matches("a > :low", i, null, v)); + assertFalse(ExpressionEvaluator.matches("a BETWEEN :low AND :high", i, null, v)); + } + // AND, OR, NOT logic @Test