mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(dynamodb): lift the PartiQL overlap refusal when a branch tests a key outside a range (#6602)
* fix(dynamodb): lift the PartiQL overlap refusal when a branch tests a key outside a range DynamoDB refuses a SELECT whose OR branches overlap on the sort key, but not 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'. Every branch then runs and the statement returns the union, each item once. A NOT lifts the refusal only when it sits right on the test, and a NOT over size() never does. Floci refused all of these as overlapping. * chore(dynamodb): walk PartiQL condition leaves through one helper The key plan had two walks over And, Or and Not that differed only in the test on each leaf.
This commit is contained in:
1 parent
fb67a6fafe
commit
8b57546159
2 files changed
+64
-6
No files matched your search
+22
-6
@@ -17,6 +17,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -94,7 +95,7 @@ final class DynamoDbPartiQLKeyPlan {
|
||||
}
|
||||
|
||||
void requireNoOverlap() {
|
||||
if (!keyed()) {
|
||||
if (!keyed() || branches.stream().anyMatch(this::testsAKeyOutsideARange)) {
|
||||
return;
|
||||
}
|
||||
Map<Key, List<Range>> rangesByPartition = new TreeMap<>();
|
||||
@@ -115,6 +116,21 @@ final class DynamoDbPartiQLKeyPlan {
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
private boolean namesAKey(Cond.Leaf leaf) {
|
||||
return leaf.bareAttribute().filter(name -> name.equals(partitionKey) || name.equals(sortKey)).isPresent();
|
||||
}
|
||||
|
||||
List<String> unprojectedFilterAttributes(Set<String> projected) {
|
||||
if (!keyed()) {
|
||||
return List.of();
|
||||
@@ -198,7 +214,7 @@ final class DynamoDbPartiQLKeyPlan {
|
||||
}
|
||||
yield spread;
|
||||
}
|
||||
case Cond.Or or when namesKey(or) -> {
|
||||
case Cond.Or or when anyLeaf(or, this::isKeyCondition) -> {
|
||||
List<Spread> spread = or.operands().stream().flatMap(operand -> spread(operand).stream()).toList();
|
||||
tooManyBranches |= spread.size() > MAX_BRANCHES;
|
||||
yield spread;
|
||||
@@ -215,11 +231,11 @@ final class DynamoDbPartiQLKeyPlan {
|
||||
return left.stream().flatMap(l -> right.stream().map(l::and)).toList();
|
||||
}
|
||||
|
||||
private boolean namesKey(Cond cond) {
|
||||
private static boolean anyLeaf(Cond cond, Predicate<Cond.Leaf> test) {
|
||||
return switch (cond) {
|
||||
case Cond.Leaf leaf -> isKeyCondition(leaf);
|
||||
case Cond.And and -> and.operands().stream().anyMatch(this::namesKey);
|
||||
case Cond.Or or -> or.operands().stream().anyMatch(this::namesKey);
|
||||
case Cond.Leaf leaf -> test.test(leaf);
|
||||
case Cond.And and -> and.operands().stream().anyMatch(operand -> anyLeaf(operand, test));
|
||||
case Cond.Or or -> or.operands().stream().anyMatch(operand -> anyLeaf(operand, test));
|
||||
case Cond.Not ignored -> false;
|
||||
};
|
||||
}
|
||||
|
||||
+42
@@ -747,6 +747,48 @@ class DynamoDbPartiQLEdgeCaseIntegrationTest {
|
||||
.body("message", equalTo("Key attribute's data type should match its data type in table's schema: Key gsiPk"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(32)
|
||||
void liftsTheOverlapCheckWhenABranchTestsAKeyOutsideARange() {
|
||||
String select = "SELECT sk FROM " + QUOTED_TABLE + " WHERE ";
|
||||
String overlapping = "Overlapping conditions with range keys are not supported in where clause";
|
||||
|
||||
for (String where : List.of("pk='ranges' AND NOT sk='1' OR pk='ranges' AND sk='2'",
|
||||
"pk='ranges' AND (sk<>'1' OR flag='two') OR pk='ranges' AND sk='2'",
|
||||
"pk IN ['ranges','ranges'] AND NOT sk='1'")) {
|
||||
statement(select + where).statusCode(200).body("Items.sk.S", containsInAnyOrder("2"));
|
||||
}
|
||||
for (String where : List.of("pk='ranges' OR pk='ranges' AND NOT sk='1'",
|
||||
"pk='ranges' AND attribute_exists(sk) OR pk='ranges' AND sk='2'",
|
||||
"pk='ranges' AND size(sk) = 1 OR pk='ranges' AND sk='2'",
|
||||
"pk='ranges' AND NOT pk='ranges-other' OR pk='ranges' AND sk='2'")) {
|
||||
statement(select + where).statusCode(200).body("Items.sk.S", containsInAnyOrder("1", "2"));
|
||||
}
|
||||
for (String where : List.of("pk='ranges' AND sk='1' OR pk='ranges' AND sk='1' OR pk='ranges' AND NOT sk='2'",
|
||||
"pk='ranges' AND sk='1' OR pk='ranges' AND sk='1' OR pk='ranges-other' AND NOT sk='2'")) {
|
||||
statement(select + where).statusCode(200).body("Items.sk.S", containsInAnyOrder("1"));
|
||||
}
|
||||
for (String where : List.of("pk='ranges' AND NOT flag='one' OR pk='ranges' AND sk='2'",
|
||||
"pk='ranges' AND NOT (sk='1' AND flag='one') OR pk='ranges' AND sk='2'",
|
||||
"pk='ranges' AND NOT NOT sk='1' OR pk='ranges' AND sk='2'",
|
||||
"pk='ranges' AND NOT size(sk) = 1 OR pk='ranges' AND sk='2'",
|
||||
"pk='ranges' AND (NOT sk='1' OR flag='one') OR pk='ranges' AND sk='2'")) {
|
||||
statement(select + where).statusCode(400).body("message", equalTo(overlapping));
|
||||
}
|
||||
transaction(select + "pk='ranges' AND NOT sk='1' OR pk='ranges' AND sk='2'")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("Validation failed in TransactStatements[0]: "
|
||||
+ "Select statements within ExecuteTransaction must specify the primary key in the where clause."));
|
||||
|
||||
String index = "SELECT pk FROM \"partiql-edge-cases-index\".\"gsi-inc\" WHERE ";
|
||||
statement(index + "gsiPk='y' AND NOT gsiPk='z' OR gsiPk='y'")
|
||||
.statusCode(200)
|
||||
.body("Items.pk.S", containsInAnyOrder("p"));
|
||||
statement(index + "gsiPk='y' AND NOT pk='q' OR gsiPk='y'")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(overlapping));
|
||||
}
|
||||
|
||||
private static ValidatableResponse getItem(String pk) {
|
||||
return request("DynamoDB_20120810.GetItem", """
|
||||
{"TableName":"%s","Key":{"pk":{"S":"%s"},"sk":{"S":"1"}},"ConsistentRead":true}
|
||||
|
||||
Reference in new issue
Block a user