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 70e6fad3c2..367ce55fe6 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 @@ -46,7 +46,7 @@ class DynamoDbPartiQLHandler { // --- SELECT --- private Result executeSelect(Stmt.Select stmt, PartiQLExecuteContext ctx, String region) { - TableDefinition table = service.describeTable(stmt.table(), region); + TableDefinition table = service.requireTableForItemCall(stmt.table(), region); // A vector index is reachable through SearchVectors alone, and PartiQL says so // rather than reporting the index as missing. if (table.findVectorIndex(stmt.index()).isPresent()) { @@ -544,7 +544,7 @@ class DynamoDbPartiQLHandler { // --- INSERT --- private Result executeInsert(Stmt.Insert stmt, String region) { - TableDefinition table = service.describeTable(stmt.table(), region); + TableDefinition table = service.requireTableForItemCall(stmt.table(), region); String pkName = table.getPartitionKeyName(); ObjectNode item = mapper.createObjectNode(); @@ -564,7 +564,7 @@ class DynamoDbPartiQLHandler { private Result executeUpdate(Stmt.Update stmt, String returnValuesOnConditionCheckFailure, String region) { requireNoIndexQualifier(stmt.index()); - TableDefinition table = service.describeTable(stmt.table(), region); + TableDefinition table = service.requireTableForItemCall(stmt.table(), region); requireOneValuePerKey(table, stmt.where()); ObjectNode key = buildKey(table, stmt.where()); @@ -619,7 +619,7 @@ class DynamoDbPartiQLHandler { private Result executeDelete(Stmt.Delete stmt, String returnValuesOnConditionCheckFailure, String region) { requireNoIndexQualifier(stmt.index()); - TableDefinition table = service.describeTable(stmt.table(), region); + TableDefinition table = service.requireTableForItemCall(stmt.table(), region); requireOneValuePerKey(table, stmt.where()); ObjectNode key = buildKey(table, stmt.where()); @@ -652,7 +652,7 @@ class DynamoDbPartiQLHandler { case Stmt.Delete del -> del.index(); default -> null; }); - Optional found = service.findTable(stmt.table(), region); + Optional found = service.findTableForItemCall(stmt.table(), region); if (found.isEmpty()) { return TransactMember.TABLE_NOT_FOUND; } @@ -767,7 +767,7 @@ class DynamoDbPartiQLHandler { } TransactMember toTransactGetItem(Stmt.Select stmt, String region) { - Optional found = service.findTable(stmt.table(), region); + Optional found = service.findTableForItemCall(stmt.table(), region); if (found.isEmpty()) { return TransactMember.TABLE_NOT_FOUND; } 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 a22f49ae7b..897d484156 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 @@ -4928,12 +4928,11 @@ public class DynamoDbService { } private TableDefinition requireActiveTable(String storageKey) { - TableDefinition table = tableStore.get(storageKey) - .orElseThrow(DynamoDbService::itemCallResourceNotFound); - if ("CREATING".equals(table.getTableStatus())) { - throw itemCallResourceNotFound(); - } - return table; + return findActiveTable(storageKey).orElseThrow(DynamoDbService::itemCallResourceNotFound); + } + + private Optional findActiveTable(String storageKey) { + return tableStore.get(storageKey).filter(table -> !"CREATING".equals(table.getTableStatus())); } /** Resolves a table for an item call, so a miss reports the message AWS uses there. */ @@ -4942,6 +4941,11 @@ public class DynamoDbService { return requireActiveTable(regionKey(region, canonicalTableName)); } + /** Finds a table for an item call, which treats a CREATING table as absent. */ + public Optional findTableForItemCall(String tableName, String region) { + return findActiveTable(regionKey(region, canonicalTableName(region, tableName))); + } + /** * Resolves the table for an item call and validates the key it names, without reading the * stored item, so a caller can report table and key errors before its own validation. diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java index 9d911c24b9..4838899cf7 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java @@ -3841,10 +3841,10 @@ public class NativeDynamoDbJsonHandler { // WHERE clauses are rejected per statement (characterised on real AWS, // eu-west-1, 2026-09-02). private void requireBatchSelectReadsByKey(DynamoDbPartiQLParser.Stmt.Select select, String region) { + TableDefinition table = dynamoDbService.requireTableForItemCall(select.table(), region); if (select.index() != null) { throw batchSelectNeedsKey(); } - TableDefinition table = dynamoDbService.requireTableForItemCall(select.table(), region); DynamoDbPartiQLHandler.requireReadsWithinLimit(table, select.where()); if (!DynamoDbPartiQLHandler.namesOnlyTheKey(table, select.where())) { throw batchSelectNeedsKey(); 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 index 3e7614476c..c7b280cc25 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLIntegrationTest.java @@ -8,6 +8,8 @@ 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 org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -599,6 +601,29 @@ class DynamoDbPartiQLIntegrationTest { .body("Responses[0].Error.Message", equalTo(notWellFormed)); } + @ParameterizedTest + @Order(29) + @ValueSource(strings = { + "SELECT * FROM \"%s\" WHERE pk = 'x'", + "SELECT * FROM \"%s\".\"gsi\" WHERE pk = 'x'", + "INSERT INTO \"%s\" VALUE {'pk': 'x'}", + "UPDATE \"%s\" SET a = 1 WHERE pk = 'x'", + "DELETE FROM \"%s\" WHERE pk = 'x'", + "FROM \"%s\" WHERE pk = 'x' SET c = 1"}) + void aStatementOnAMissingTableIsRefusedWithoutTheTableName(String statement) { + String partiql = statement.formatted(TABLE + "-never-created"); + + statement(partiql) + .statusCode(400) + .body("__type", equalTo("ResourceNotFoundException")) + .body("message", equalTo("Requested resource not found")); + request("DynamoDB_20120810.BatchExecuteStatement", + "{\"Statements\":[{\"Statement\":\"" + partiql.replace("\"", "\\\"") + "\"}]}") + .statusCode(200) + .body("Responses[0].Error.Code", equalTo("ResourceNotFound")) + .body("Responses[0].Error.Message", equalTo("Requested resource not found")); + } + private static void putItem(String pk, String attributes) { request("DynamoDB_20120810.PutItem", """ {"TableName":"%s","Item":{"pk":{"S":"%s"},%s}} diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandlerTest.java index 925bd4a079..8950484157 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandlerTest.java @@ -16,6 +16,8 @@ import jakarta.ws.rs.core.Response; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.nio.file.Path; import java.util.ArrayList; @@ -2533,6 +2535,39 @@ class NativeDynamoDbJsonHandlerTest { assertEquals("Requested resource not found", ex.getMessage()); } + @ParameterizedTest + @ValueSource(strings = { + "SELECT * FROM \"Users\".\"TitleIndex\" WHERE userId = 'u1'", + "INSERT INTO \"Users\" VALUE {'userId': 1}", + "UPDATE \"Users\" SET a = 1 WHERE userId = 1", + "DELETE FROM \"Users\" WHERE nope = 'u1'"}) + void executeStatementAnswersResourceNotFoundForACreatingTableBeforeTheKeyCheck(String statement) { + createUsersTable("eu-west-1").setTableStatus("CREATING"); + ObjectNode request = mapper.createObjectNode(); + request.put("Statement", statement); + + AwsException ex = assertThrows(AwsException.class, + () -> handler.handle("ExecuteStatement", request, "eu-west-1")); + assertEquals("ResourceNotFoundException", ex.getErrorCode()); + assertEquals("Requested resource not found", ex.getMessage()); + } + + @ParameterizedTest + @ValueSource(strings = { + "SELECT * FROM \"Users\" WHERE userId = 'u1'", + "UPDATE \"Users\" SET a = 1 WHERE userId = 'u1'"}) + void executeTransactionCancelsOnACreatingTable(String statement) throws Exception { + createUsersTable("eu-west-1").setTableStatus("CREATING"); + ObjectNode request = mapper.createObjectNode(); + request.putArray("TransactStatements").addObject().put("Statement", statement); + + Response response = handler.handle("ExecuteTransaction", request, "eu-west-1"); + assertEquals(400, response.getStatus()); + JsonNode reason = mapper.convertValue(response.getEntity(), JsonNode.class).get("CancellationReasons").get(0); + assertEquals("ResourceNotFound", reason.get("Code").asText()); + assertEquals("Requested resource not found", reason.get("Message").asText()); + } + @Test void transactWriteItemsFailsTheRequestOnAnEmptyKeyAheadOfAKeyMismatch() { createUsersTable("eu-west-1");