Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(dynamodb): filter an unkeyed PartiQL index read on the index view (#4319)

TATSUNO “Taz” Yasuhiro9454ceed21d3
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java
index 2fb950bdc0..0120944881 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLHandler.java
@@ -17,6 +17,7 @@ import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.Base64;
import java.util.function.Supplier;
+import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
class DynamoDbPartiQLHandler {
@@ -212,14 +213,18 @@ class DynamoDbPartiQLHandler {
if (skCond != null) {
scanFilters.add(skCond);
}
- String fe = buildFe(scanFilters, eav, ean);
+ boolean partialProjection = !"ALL".equals(accessPath.projectionType());
+ String fe = partialProjection ? null : buildFe(scanFilters, eav, ean);
JsonNode exclusiveStartKey = startKey.get();
DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, table, accessPath, true);
DynamoDbService.ScanResult result = service.scan(stmt.table(), fe,
ean.isEmpty() ? null : ean.toNode(mapper),
eav.isEmpty() ? null : eav.toNode(mapper),
null, limit, exclusiveStartKey, accessPath.indexName(), region);
- return new Page(result.items(), result.lastEvaluatedKey(), result.scannedBytes(), result.scannedItems());
+ List<JsonNode> matching = partialProjection
+ ? matchingIndexView(result.items(), scanFilters, accessPath, table)
+ : result.items();
+ return new Page(matching, result.lastEvaluatedKey(), result.scannedBytes(), result.scannedItems());
}
if (accessPath.kind() == DynamoDbAccessPath.Kind.TABLE && skName == null
&& skCond == null && filterConds.isEmpty()) {
@@ -253,15 +258,31 @@ class DynamoDbPartiQLHandler {
DynamoDbAccessPathValidator.validateExclusiveStartKey(exclusiveStartKey, table, accessPath, false);
DynamoDbService.QueryResult result = service.query(stmt.table(), null, eav.toNode(mapper), kce, null,
limit, null, accessPath.indexName(), exclusiveStartKey, ean.toNode(mapper), region);
+ List<JsonNode> matching = matchingIndexView(result.items(), routing.filterConds(), accessPath, table);
+ return new Page(matching, result.lastEvaluatedKey(), result.scannedBytes(), result.items());
+ }
+
+ // The WHERE clause of an index read sees only what the index stores, so an
+ // attribute it does not project is missing even though the base item has it.
+ private List<JsonNode> matchingIndexView(List<JsonNode> items, List<Cond> conds,
+ DynamoDbAccessPath accessPath, TableDefinition table) {
ExprAttrBuilder filterValues = new ExprAttrBuilder();
ExprAttrNameBuilder filterNames = new ExprAttrNameBuilder();
- ExpressionEvaluator.Expr filter = ExpressionEvaluator.parse(buildFe(routing.filterConds(), filterValues, filterNames));
+ ExpressionEvaluator.Expr filter = ExpressionEvaluator.parse(buildFe(conds, filterValues, filterNames));
JsonNode names = filterNames.isEmpty() ? null : filterNames.toNode(mapper);
JsonNode values = filterValues.isEmpty() ? null : filterValues.toNode(mapper);
- List<JsonNode> matching = result.items().stream()
- .filter(item -> ExpressionEvaluator.evaluate(filter, item, names, values))
+ UnaryOperator<JsonNode> view = indexView(accessPath, table);
+ return items.stream()
+ .filter(item -> ExpressionEvaluator.evaluate(filter, view.apply(item), names, values))
.toList();
- return new Page(matching, result.lastEvaluatedKey(), result.scannedBytes(), result.items());
+ }
+
+ private static UnaryOperator<JsonNode> indexView(DynamoDbAccessPath accessPath, TableDefinition table) {
+ if ("ALL".equals(accessPath.projectionType())) {
+ return UnaryOperator.identity();
+ }
+ Set<String> projected = accessPath.projectedAttributeNames(table);
+ return item -> ProjectionEvaluator.trimToAttributes((ObjectNode) item, projected);
}
private Page readOrdered(Stmt.Select stmt, TableDefinition table, DynamoDbAccessPath accessPath,
diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLEdgeCaseIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLEdgeCaseIntegrationTest.java
index 08907415d7..f524e926a0 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLEdgeCaseIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLEdgeCaseIntegrationTest.java
@@ -675,6 +675,60 @@ class DynamoDbPartiQLEdgeCaseIntegrationTest {
getItem("ranges").body("Item.t", nullValue());
}
+ @Test
+ @Order(30)
+ void filtersAnUnkeyedIndexReadOnTheIndexView() {
+ request("DynamoDB_20120810.CreateTable", """
+ {
+ "TableName": "partiql-edge-cases-index-view",
+ "AttributeDefinitions": [
+ {"AttributeName":"pk","AttributeType":"S"},
+ {"AttributeName":"sk","AttributeType":"S"},
+ {"AttributeName":"gsiPk","AttributeType":"S"},
+ {"AttributeName":"lsiSk","AttributeType":"S"}
+ ],
+ "KeySchema": [
+ {"AttributeName":"pk","KeyType":"HASH"},
+ {"AttributeName":"sk","KeyType":"RANGE"}
+ ],
+ "GlobalSecondaryIndexes": [{
+ "IndexName": "gsi-inc",
+ "KeySchema": [{"AttributeName":"gsiPk","KeyType":"HASH"}],
+ "Projection": {"ProjectionType":"INCLUDE","NonKeyAttributes":["projattr"]}
+ }],
+ "LocalSecondaryIndexes": [{
+ "IndexName": "lsi-keys",
+ "KeySchema": [
+ {"AttributeName":"pk","KeyType":"HASH"},
+ {"AttributeName":"lsiSk","KeyType":"RANGE"}
+ ],
+ "Projection": {"ProjectionType":"KEYS_ONLY"}
+ }],
+ "BillingMode": "PAY_PER_REQUEST"
+ }
+ """)
+ .statusCode(200);
+ for (String item : List.of(
+ "\"sk\":{\"S\":\"s1\"},\"gsiPk\":{\"S\":\"y\"},\"lsiSk\":{\"S\":\"k1\"},\"nonproj\":{\"S\":\"np1\"}",
+ "\"sk\":{\"S\":\"s2\"},\"gsiPk\":{\"S\":\"y2\"},\"lsiSk\":{\"S\":\"k2\"},\"nonproj\":{\"S\":\"np2\"}")) {
+ request("DynamoDB_20120810.PutItem",
+ "{\"TableName\":\"partiql-edge-cases-index-view\",\"Item\":{\"pk\":{\"S\":\"p\"}," + item + "}}")
+ .statusCode(200);
+ }
+ String gsi = "SELECT sk FROM \"partiql-edge-cases-index-view\".\"gsi-inc\" WHERE ";
+ String lsi = "SELECT sk FROM \"partiql-edge-cases-index-view\".\"lsi-keys\" WHERE ";
+
+ statement(gsi + "nonproj='np1'").statusCode(200).body("Items.size()", equalTo(0));
+ statement(lsi + "nonproj='np1'").statusCode(200).body("Items.size()", equalTo(0));
+ statement(gsi + "NOT nonproj='np1'").statusCode(200).body("Items.sk.S", containsInAnyOrder("s1", "s2"));
+ statement(gsi + "(gsiPk='y' AND nonproj='np1') OR (gsiPk='y2')")
+ .statusCode(200)
+ .body("Items.sk.S", containsInAnyOrder("s2"));
+ statement("SELECT sk FROM \"partiql-edge-cases-index-view\" WHERE nonproj='np1'")
+ .statusCode(200)
+ .body("Items.sk.S", containsInAnyOrder("s1"));
+ }
+
private static ValidatableResponse getItem(String pk) {
return request("DynamoDB_20120810.GetItem", """
{"TableName":"%s","Key":{"pk":{"S":"%s"},"sk":{"S":"1"}},"ConsistentRead":true}