fix(dynamodb): count the hash key conditions of every OR branch for a PartiQL ORDER BY (#8558)

A PartiQL ORDER BY now counts the hash key conditions inside every OR
branch. An OR whose branches each name the partition key, such as
(pk = 'a' AND x = 1) OR (pk = 'b' AND x = 2), reads more than one
partition, so it needs the partition key in ORDER BY, and an OR with a
branch that names none still counts no hash key condition.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-16 07:16:52 -04:00
1 parent e47747845e
commit 18d04ded17
2 files changed
+23 -2

No files matched your search

@@ -343,12 +343,23 @@ class DynamoDbPartiQLHandler {
}
}
// An OR reads one partition per branch only when every branch names one.
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();
case Cond.And and -> and.operands().stream().mapToInt(o -> hashKeyConditionCount(o, pkName)).sum();
case Cond.Or or -> {
int total = 0;
for (Cond operand : or.operands()) {
int count = hashKeyConditionCount(operand, pkName);
if (count == 0) {
yield 0;
}
total += count;
}
yield total;
}
default -> 0;
};
}
@@ -264,6 +264,16 @@ class DynamoDbPartiQLDocumentedSyntaxIntegrationTest {
.statusCode(400)
.body("message", equalTo("Must have hash key in ORDER BY clause when more than one hash key condition"
+ " specified in WHERE clause."));
statement(select + "WHERE (pk = 'o' AND Total = 1) OR (pk = 'p' AND Total = 2) ORDER BY sk")
.statusCode(400)
.body("message", equalTo("Must have hash key in ORDER BY clause when more than one hash key condition"
+ " specified in WHERE clause."));
statement(select + "WHERE (pk = 'o' AND Total = 1) OR Total = 2 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 = 'o' AND Total = 1) OR (pk = 'p' AND Total = 2) ORDER BY pk")
.statusCode(200);
}
@Test