fix(dynamodb): refuse a PartiQL key read whose filter names a key (#8559)

A PartiQL read by key now refuses the statement once the filter of
every read names a key of the table or index read, as DynamoDB does,
answering "Filter Expression can only contain non-primary key
attributes" with a key from the last read in key order. A statement
that tests a key in a way no key condition can hold is still read as a
scan and accepted, and size() on a key counts only on its own. Query
and PartiQL build that message in one place.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-16 09:09:46 -04:00
1 parent a115078c48
commit 651ac1cbe7
4 files changed
+71 -5

No files matched your search

@@ -131,6 +131,11 @@ final class DynamoDbAccessPathValidator {
+ String.join(", ", attributes) + "]");
}
static AwsException keyAttributeInFilter(String keyAttribute) {
return validationException("Filter Expression can only contain non-primary key attributes: "
+ "Primary key attribute: " + keyAttribute);
}
static AwsException unprojectedGsiAttributes(String indexName, Collection<String> attributes) {
return validationException("One or more parameter values were invalid: Global secondary index "
+ indexName + " does not project [" + String.join(", ", attributes) + "]");
@@ -448,8 +453,7 @@ final class DynamoDbAccessPathValidator {
collectAttributes(root, names, referenced);
for (String keyAttribute : accessPath.keyAttributeNames()) {
if (referenced.contains(keyAttribute)) {
throw validationException("Filter Expression can only contain non-primary key attributes: "
+ "Primary key attribute: " + keyAttribute);
throw keyAttributeInFilter(keyAttribute);
}
}
}
@@ -90,6 +90,7 @@ class DynamoDbPartiQLHandler {
DynamoDbPartiQLKeyPlan keys = new DynamoDbPartiQLKeyPlan(stmt.where(), accessPath, table);
keys.requireKeyTypesMatchSchema();
keys.requireNoOverlap();
keys.requireFilterWithoutKeys();
keys.requireReadsWithinLimit();
requireFilterAttributesProjected(keys, accessPath, table);
requireColumnsProjected(stmt, accessPath, table);
@@ -44,7 +44,11 @@ final class DynamoDbPartiQLKeyPlan {
}
}
private record Branch(Key partition, PVal partitionValue, Range sortRange, List<Cond> filters) {}
private record Branch(Key partition, PVal partitionValue, Range sortRange, List<Cond> filters) {
static final Comparator<Branch> IN_KEY_ORDER = Comparator.comparing(Branch::partition)
.thenComparing(Branch::sortRange, Range.BY_LOWER);
}
DynamoDbPartiQLKeyPlan(List<Cond> where, DynamoDbAccessPath accessPath, TableDefinition table) {
this.partitionKey = accessPath.partitionKeyName();
@@ -116,14 +120,38 @@ final class DynamoDbPartiQLKeyPlan {
}
}
/**
* Each key read leaves the rest of its branch to a filter, and DynamoDB refuses the statement
* once every such filter names a key of the access path. A statement that tests a key outside
* a range is read as a scan, whose filter may name the keys.
*/
void requireFilterWithoutKeys() {
if (!keyed() || branches.stream().anyMatch(this::testsAKeyOutsideARange)) {
return;
}
// DynamoDB names a key the last read in key order filters on, the sort key first.
List<String> keys = sortKey == null ? List.of(partitionKey) : List.of(sortKey, partitionKey);
Optional<String> named = Optional.empty();
for (Branch branch : branches.stream().sorted(Branch.IN_KEY_ORDER).toList()) {
named = keys.stream().filter(key -> branch.filters().stream()
.flatMap(DynamoDbPartiQLParser::leaves)
.anyMatch(leaf -> leaf.path().root().equals(key))).findFirst();
if (named.isEmpty()) {
return;
}
}
throw DynamoDbAccessPathValidator.keyAttributeInFilter(named.get());
}
// DynamoDB drops the overlap check once a branch tests a key in a way no key condition can
// hold, such as sk <> 'a', attribute_exists(sk) or NOT sk = 'a'. A NOT counts only when it
// sits right on the test, and a NOT over size() never counts.
// sits right on the test, a NOT over size() never counts, and size() counts only on its own.
private boolean testsAKeyOutsideARange(Branch branch) {
return branch.filters().stream().anyMatch(filter -> switch (filter) {
case Cond.Not not -> not.operand() instanceof Cond.Leaf leaf
&& !(leaf instanceof Cond.SizeCmp) && namesAKey(leaf);
default -> anyLeaf(filter, this::namesAKey);
case Cond.Leaf leaf -> namesAKey(leaf);
default -> anyLeaf(filter, leaf -> !(leaf instanceof Cond.SizeCmp) && namesAKey(leaf));
});
}
@@ -858,6 +858,39 @@ class DynamoDbPartiQLEdgeCaseIntegrationTest {
.statusCode(200);
}
@Test
@Order(34)
void refusesAKeyReadWhoseFilterNamesAKey() {
String select = "SELECT sk FROM " + QUOTED_TABLE + " WHERE ";
String refused = "Filter Expression can only contain non-primary key attributes: Primary key attribute: ";
for (String where : List.of("pk='ranges' AND (NOT sk='1' OR flag='one')",
"pk='ranges' AND NOT (sk='1' AND flag='one')",
"pk='ranges' AND NOT NOT sk='1'",
"pk='ranges' AND NOT size(sk) = 1",
"pk='ranges' AND (size(sk) = 1 OR flag='one')",
"pk IN ['ranges','ranges-other'] AND (NOT sk='1' OR flag='one')",
"pk='ranges' AND (NOT pk='ranges-other' OR NOT sk='1')",
"pk='ranges' AND ((sk='1' AND NOT size(sk) = 1) OR (sk='2' AND NOT size(sk) = 1))",
"(pk='ranges' AND NOT size(pk) = 1) OR (pk='ranges-other' AND NOT size(sk) = 1)")) {
statement(select + where).statusCode(400).body("message", equalTo(refused + "sk"));
}
for (String where : List.of("pk='ranges' AND (NOT pk='ranges-other' OR flag='one')",
"pk='ranges' AND ((sk='1' AND NOT size(sk) = 1) OR (sk='2' AND NOT size(pk) = 1))",
"pk='ranges' AND ((sk='2' AND NOT size(pk) = 1) OR (sk='1' AND NOT size(sk) = 1))")) {
statement(select + where).statusCode(400).body("message", equalTo(refused + "pk"));
}
for (String where : List.of("pk='ranges' AND NOT sk='1'",
"pk='ranges' AND size(sk) = 1",
"pk='ranges' AND (sk<>'1' OR flag='one')",
"(pk='ranges' AND (NOT sk='1' OR flag='one')) OR (pk='ranges-other' AND flag='two')",
"pk='ranges' AND ((sk='1' AND NOT size(sk) = 1) OR sk='2')",
"pk='ranges' AND ((sk='1' AND flag='one') OR (sk='2' AND NOT size(sk) = 1))",
"NOT sk='1' OR flag='one'")) {
statement(select + where).statusCode(200);
}
}
private static void putItem(String pk, String attributes) {
putItem(pk, "1", attributes);
}