From 29253ff7f89d4a461c734ba61b891df731f5774f Mon Sep 17 00:00:00 2001 From: Niels Kootstra <545768+nkootstra@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:43:29 +0100 Subject: [PATCH] fix(dynamodb): resume a Query after the ExclusiveStartKey position, whether or not an item has that key (#8021) Closes #7970 --- .../services/dynamodb/DynamoDbService.java | 84 +++++++++---------- .../dynamodb/DynamoDbServiceTest.java | 56 +++++++++++++ 2 files changed, 96 insertions(+), 44 deletions(-) diff --git a/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java b/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java index 81f22ae47f..0953fc489d 100644 --- a/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java +++ b/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java @@ -1614,54 +1614,29 @@ public class DynamoDbService { .toList(); } - // Sort by the full (possibly composite) sort key, comparing each attribute in key-schema - // order. Using only the first sort-key attribute would ignore the remaining components - // (e.g. a "requestStateQuery RANGE, createdAt RANGE" index would never order by createdAt), - // which in turn breaks ScanIndexForward=false. See floci-io/floci#1675. - if (!sortKeyNames.isEmpty()) { - List finalSortKeyNames = sortKeyNames; - results = new ArrayList<>(results); - results.sort((a, b) -> { - for (String name : finalSortKeyNames) { - JsonNode aAttr = a.get(name); - JsonNode bAttr = b.get(name); - int cmp; - if (aAttr == null && bAttr == null) cmp = 0; - else if (aAttr == null) cmp = -1; - else if (bAttr == null) cmp = 1; - else cmp = ExpressionEvaluator.compareAttributeValues(aAttr, bAttr); - if (cmp != 0) return cmp; - } - return 0; - }); - if (Boolean.FALSE.equals(scanIndexForward)) { - Collections.reverse(results); + // Order by the full (possibly composite) sort key, comparing each attribute in key-schema + // order (floci-io/floci#1675), then, on an index, by the table key, which is what + // tells apart items whose index keys tie. + List orderKeyNames = new ArrayList<>(sortKeyNames); + if (accessPath.isIndex()) { + orderKeyNames.add(table.getPartitionKeyName()); + if (table.getSortKeyName() != null) { + orderKeyNames.add(table.getSortKeyName()); } } + Comparator keyOrder = (a, b) -> compareKeyAttributes(a, b, orderKeyNames); + int direction = Boolean.FALSE.equals(scanIndexForward) ? -1 : 1; + if (!orderKeyNames.isEmpty()) { + results = new ArrayList<>(results); + results.sort((a, b) -> direction * keyOrder.compare(a, b)); + } - // Apply ExclusiveStartKey offset + // ExclusiveStartKey is a position in key order, not a pointer to an item, so the + // query resumes after it even when no item has that key any more. if (exclusiveStartKey != null) { - String tablePkName = table.getPartitionKeyName(); - String tableSkName = table.getSortKeyName(); - boolean hasTableKeys = exclusiveStartKey.has(tablePkName); - - String startItemKey = hasTableKeys - ? buildItemKeyFromNode(exclusiveStartKey, tablePkName, tableSkName) - : buildItemKeyFromNode(exclusiveStartKey, pkName, sortKeyNames); - - int startIdx = -1; - for (int i = 0; i < results.size(); i++) { - String thisKey = hasTableKeys - ? buildItemKeyFromNode(results.get(i), tablePkName, tableSkName) - : buildItemKeyFromNode(results.get(i), pkName, sortKeyNames); - if (thisKey.equals(startItemKey)) { - startIdx = i; - break; - } - } - if (startIdx >= 0) { - results = new ArrayList<>(results.subList(startIdx + 1, results.size())); - } + results = results.stream() + .filter(item -> direction * keyOrder.compare(item, exclusiveStartKey) > 0) + .toList(); } List evaluatedItems = results; @@ -5004,6 +4979,27 @@ public class DynamoDbService { }; } + private static int compareKeyAttributes(JsonNode a, JsonNode b, List keyNames) { + for (String name : keyNames) { + JsonNode aAttr = a.get(name); + JsonNode bAttr = b.get(name); + int cmp; + if (aAttr == null && bAttr == null) { + cmp = 0; + } else if (aAttr == null) { + cmp = -1; + } else if (bAttr == null) { + cmp = 1; + } else { + cmp = ExpressionEvaluator.compareAttributeValues(aAttr, bAttr); + } + if (cmp != 0) { + return cmp; + } + } + return 0; + } + private void validateExclusiveStartKeyWithinQuery(JsonNode exclusiveStartKey, TableDefinition table, DynamoDbAccessPath accessPath, JsonNode keyConditions, String keyConditionExpression, JsonNode expressionAttrValues, diff --git a/src/test/java/io/floci/aws/services/dynamodb/DynamoDbServiceTest.java b/src/test/java/io/floci/aws/services/dynamodb/DynamoDbServiceTest.java index a15f8e47ec..5b84af5bff 100644 --- a/src/test/java/io/floci/aws/services/dynamodb/DynamoDbServiceTest.java +++ b/src/test/java/io/floci/aws/services/dynamodb/DynamoDbServiceTest.java @@ -879,6 +879,62 @@ class DynamoDbServiceTest { .toList()); } + @Test + void queryResumesAfterTheStartKeyOfAnItemDeletedSinceThePreviousPage() { + createPagingTable(); + service.deleteItem("Paging", item("PK", "p", "SK", "s2"), "us-east-1"); + + assertEquals(List.of("s3", "s4", "s5"), + pagingQuery(null, true, item("PK", "p", "SK", "s2"))); + } + + @Test + void queryResumesAfterAStartKeyThatNeverExisted() { + createPagingTable(); + + assertEquals(List.of("s4", "s5"), pagingQuery(null, true, item("PK", "p", "SK", "s3x"))); + } + + @Test + void descendingQueryResumesBeforeAStartKeyThatNeverExisted() { + createPagingTable(); + + assertEquals(List.of("s3", "s2", "s1"), pagingQuery(null, false, item("PK", "p", "SK", "s3x"))); + } + + @Test + void indexQueryResumesAfterTheStartKeyOfAnItemReKeyedSinceThePreviousPage() { + createPagingTable(); + service.putItem("Paging", item("PK", "p", "SK", "s3", "G", "live", "GS", "t9"), "us-east-1"); + + assertEquals(List.of("s4", "s5", "s3"), + pagingQuery("gsi1", true, item("PK", "p", "SK", "s3", "G", "live", "GS", "t3"))); + } + + private void createPagingTable() { + GlobalSecondaryIndex gsi = new GlobalSecondaryIndex("gsi1", + List.of(new KeySchemaElement("G", "HASH"), new KeySchemaElement("GS", "RANGE")), + null, "ALL", null); + service.createTable("Paging", + List.of(new KeySchemaElement("PK", "HASH"), new KeySchemaElement("SK", "RANGE")), + List.of(new AttributeDefinition("PK", "S"), new AttributeDefinition("SK", "S"), + new AttributeDefinition("G", "S"), new AttributeDefinition("GS", "S")), + 5L, 5L, List.of(gsi), "us-east-1"); + for (int i = 1; i <= 5; i++) { + service.putItem("Paging", item("PK", "p", "SK", "s" + i, "G", "live", "GS", "t" + i), "us-east-1"); + } + } + + private List pagingQuery(String indexName, boolean forward, ObjectNode exclusiveStartKey) { + ObjectNode exprValues = mapper.createObjectNode(); + exprValues.set(":v", attributeValue("S", indexName == null ? "p" : "live")); + String keyCondition = indexName == null ? "PK = :v" : "G = :v"; + return service.query("Paging", null, exprValues, keyCondition, null, null, forward, indexName, + exclusiveStartKey, null, "us-east-1").items().stream() + .map(result -> result.get("SK").get("S").asText()) + .toList(); + } + private void createRequestsTableWithCompositeSortKeyGsi(String region) { GlobalSecondaryIndex gsi = new GlobalSecondaryIndex( "memberIndex",