diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java b/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java index 54dffa52af..0e80c5f78e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java +++ b/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java @@ -391,9 +391,25 @@ public class GlueService { String prefix = tableKey(databaseName, tableName) + ":"; return partitionStore.scan(k -> k.startsWith(prefix)).stream() .filter(partition -> matchesPartitionExpression(table, partition, expression)) + .sorted(GlueService::comparePartitionValues) .toList(); } + // The partition store scan returns values in storage iteration order, which is not stable. + // Return partitions ordered by their values so GetPartitions is deterministic across calls. + private static int comparePartitionValues(Partition a, Partition b) { + List aValues = a.getValues() == null ? List.of() : a.getValues(); + List bValues = b.getValues() == null ? List.of() : b.getValues(); + int shared = Math.min(aValues.size(), bValues.size()); + for (int i = 0; i < shared; i++) { + int comparison = Comparator.nullsFirst(String::compareTo).compare(aValues.get(i), bValues.get(i)); + if (comparison != 0) { + return comparison; + } + } + return Integer.compare(aValues.size(), bValues.size()); + } + public void deletePartition(String databaseName, String tableName, List partitionValues) { getPartition(databaseName, tableName, partitionValues); String key = partitionKey(databaseName, tableName, partitionValues); diff --git a/src/test/java/io/github/hectorvent/floci/services/glue/GlueServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/glue/GlueServiceTest.java index 5099d47f85..379781b0ca 100644 --- a/src/test/java/io/github/hectorvent/floci/services/glue/GlueServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/glue/GlueServiceTest.java @@ -386,6 +386,25 @@ class GlueServiceTest { assertEquals("EntityNotFoundException", ex.getErrorCode()); } + @Test + void getPartitionsReturnsPartitionsSortedByValuesRegardlessOfCreationOrder() { + Table table = new Table(); + table.setName("plain"); + table.setPartitionKeys(List.of(new Column("year", "string"))); + glueService.createTable("db1", table); + for (String value : List.of("2028", "2026", "2027")) { + Partition partition = new Partition(); + partition.setValues(List.of(value)); + glueService.createPartition("db1", "plain", partition); + } + + // Deterministic ordering: returned in sorted order, not storage-scan order. No re-sort here. + assertEquals(List.of(List.of("2026"), List.of("2027"), List.of("2028")), + glueService.getPartitions("db1", "plain").stream() + .map(Partition::getValues) + .toList()); + } + @Test void partitionKeysDoNotCollideForCommaSeparatedValues() { Table table = new Table();