Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(dynamodb): match AWS for unprojected GSI attributes in Query, Scan and PartiQL (#5911)

TATSUNO “Taz” Yasuhiro61102a7c4eed
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidator.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidator.java
index ef080efd3c..1306847f62 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidator.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidator.java
@@ -19,6 +19,7 @@ import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
@@ -71,11 +72,12 @@ final class DynamoDbAccessPathValidator {
return;
}
if ("ALL_ATTRIBUTES".equals(select)) {
- throw validationException("Select type ALL_ATTRIBUTES is not supported for global secondary index "
+ throw validationException("One or more parameter values were invalid: "
+ + "Select type ALL_ATTRIBUTES is not supported for global secondary index "
+ accessPath.indexName() + " because its projection type is not ALL");
}
- Set<String> requestedAttributes = new HashSet<>();
+ Set<String> requestedAttributes = new LinkedHashSet<>();
if (projectionExpression != null) {
requestedAttributes.addAll(ProjectionEvaluator.topLevelAttributes(
projectionExpression, expressionAttributeNames));
@@ -87,12 +89,15 @@ final class DynamoDbAccessPathValidator {
Set<String> projectedAttributes = accessPath.projectedAttributeNames(table);
requestedAttributes.removeAll(projectedAttributes);
if (!requestedAttributes.isEmpty()) {
- throw validationException("One or more parameter values were invalid: Global secondary index "
- + accessPath.indexName() + " does not project "
- + String.join(", ", requestedAttributes.stream().sorted().toList()));
+ throw unprojectedGsiAttributes(accessPath.indexName(), requestedAttributes);
}
}
+ 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) + "]");
+ }
+
private static void rejectWithSelect(String parameter, boolean present, String select) {
if (present && select != null && !"SPECIFIC_ATTRIBUTES".equals(select)) {
throw validationException("1 validation error detected: "
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 8e14c706cc..3142ef7537 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
@@ -66,31 +66,12 @@ class DynamoDbPartiQLHandler {
"Strongly consistent read is not supported on Global Secondary Indexes", 400);
}
- // A GSI-qualified statement can only select attributes the index
- // projects; the message preserves statement order (characterised on
- // real AWS, eu-west-1, 2026-09-02). An LSI read reaches the co-located
- // base item, so non-projected columns stay legal there.
- if (accessPath.isGlobalSecondaryIndex() && !"ALL".equals(accessPath.projectionType())
- && !stmt.columns().isEmpty()) {
- Set<String> projected = accessPath.projectedAttributeNames(table);
- List<String> unprojected = stmt.columns().stream()
- .map(Path::root)
- .filter(c -> !"*".equals(c))
- .filter(c -> !projected.contains(c))
- .toList();
- if (!unprojected.isEmpty()) {
- throw new AwsException("ValidationException",
- "One or more parameter values were invalid: Global secondary index "
- + accessPath.indexName() + " does not project ["
- + String.join(", ", unprojected) + "]", 400);
- }
- }
-
DynamoDbPartiQLKeyPlan keys = new DynamoDbPartiQLKeyPlan(stmt.where(), accessPath, table);
keys.requireKeyTypesMatchSchema();
keys.requireNoOverlap();
keys.requireReadsWithinLimit();
requireFilterAttributesProjected(keys, accessPath, table);
+ requireColumnsProjected(stmt, accessPath, table);
Routing routing = routingOf(stmt.where(), keys, accessPath.partitionKeyName(), accessPath.sortKeyName());
Supplier<JsonNode> startKey = () -> decodeNextToken(ctx.nextToken(), ctx.tokenBinding());
@@ -503,6 +484,25 @@ class DynamoDbPartiQLHandler {
}
}
+ // An LSI read reaches the co-located base item, so non-projected columns stay legal there.
+ // AWS lists each root once, in an order of its own that is not the statement order.
+ private static void requireColumnsProjected(Stmt.Select stmt, DynamoDbAccessPath accessPath,
+ TableDefinition table) {
+ if (!accessPath.isGlobalSecondaryIndex() || "ALL".equals(accessPath.projectionType())) {
+ return;
+ }
+ Set<String> projected = accessPath.projectedAttributeNames(table);
+ List<String> unprojected = stmt.columns().stream()
+ .map(Path::root)
+ .filter(c -> !"*".equals(c))
+ .filter(c -> !projected.contains(c))
+ .distinct()
+ .toList();
+ if (!unprojected.isEmpty()) {
+ throw DynamoDbAccessPathValidator.unprojectedGsiAttributes(accessPath.indexName(), unprojected);
+ }
+ }
+
private static boolean isKeyCondition(Cond cond, String keyName) {
return cond.bareAttribute().filter(keyName::equals).isPresent();
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/ProjectionEvaluator.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/ProjectionEvaluator.java
index 639cee7c55..081d36fc24 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/ProjectionEvaluator.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/ProjectionEvaluator.java
@@ -7,8 +7,9 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import java.util.ArrayList;
-import java.util.HashSet;
+import java.util.Collections;
import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -65,14 +66,19 @@ final class ProjectionEvaluator {
}
static Set<String> topLevelAttributes(String projectionExpression, JsonNode exprAttrNames) {
- HashSet<String> attributes = new HashSet<>();
+ Set<String> attributes = new LinkedHashSet<>();
for (String rawPath : splitProjectionPaths(projectionExpression)) {
List<PathSegment> segments = resolvePath(rawPath.trim(), exprAttrNames);
- if (!segments.isEmpty()) {
- attributes.add(segments.getFirst().name());
+ if (segments.isEmpty()) {
+ continue;
+ }
+ PathSegment root = segments.getFirst();
+ if (root.isIndex()) {
+ throw new IllegalArgumentException("Projection path starts with a list index: " + rawPath.trim());
}
+ attributes.add(root.name());
}
- return Set.copyOf(attributes);
+ return Collections.unmodifiableSet(attributes);
}
static void validateSyntax(String expression, String expressionType) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathIntegrationTest.java
index 14979bf648..49fbb4a58e 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathIntegrationTest.java
@@ -223,17 +223,34 @@ class DynamoDbAccessPathIntegrationTest {
""".formatted(TABLE);
request("DynamoDB_20120810.Query", query)
.statusCode(400)
- .body("__type", equalTo("ValidationException"));
+ .body("__type", equalTo("ValidationException"))
+ .body("message", equalTo("One or more parameter values were invalid: "
+ + "Global secondary index status-index does not project [details]"));
request("DynamoDB_20120810.Scan", """
{
"TableName":"%s",
"IndexName":"status-index",
- "ProjectionExpression":"details"
+ "ProjectionExpression":"zeta.a, details, zeta.b"
}
""".formatted(TABLE))
.statusCode(400)
- .body("__type", equalTo("ValidationException"));
+ .body("__type", equalTo("ValidationException"))
+ .body("message", equalTo("One or more parameter values were invalid: "
+ + "Global secondary index status-index does not project [zeta, details]"));
+
+ request("DynamoDB_20120810.Scan", """
+ {
+ "TableName":"%s",
+ "IndexName":"status-index",
+ "Select":"ALL_ATTRIBUTES"
+ }
+ """.formatted(TABLE))
+ .statusCode(400)
+ .body("__type", equalTo("ValidationException"))
+ .body("message", equalTo("One or more parameter values were invalid: "
+ + "Select type ALL_ATTRIBUTES is not supported for global secondary index status-index "
+ + "because its projection type is not ALL"));
}
@Test
diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidatorTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidatorTest.java
index 430cd092e1..fe2071f2bd 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidatorTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbAccessPathValidatorTest.java
@@ -336,13 +336,29 @@ class DynamoDbAccessPathValidatorTest {
table, gsiPath, "SPECIFIC_ATTRIBUTES", "zeta, details",
null, null));
assertEquals("One or more parameter values were invalid: Global secondary index "
- + "status-index does not project details, zeta", projectionError.getMessage());
- assertThrows(AwsException.class, () -> DynamoDbAccessPathValidator.validateGsiProjection(
- table, gsiPath, "ALL_ATTRIBUTES", null, null, null));
+ + "status-index does not project [zeta, details]", projectionError.getMessage());
+ AwsException allAttributes = assertThrows(AwsException.class,
+ () -> DynamoDbAccessPathValidator.validateGsiProjection(
+ table, gsiPath, "ALL_ATTRIBUTES", null, null, null));
+ assertEquals("One or more parameter values were invalid: Select type ALL_ATTRIBUTES is not supported "
+ + "for global secondary index status-index because its projection type is not ALL",
+ allAttributes.getMessage());
assertDoesNotThrow(() -> DynamoDbAccessPathValidator.validateGsiProjection(
table, lsiPath, "ALL_ATTRIBUTES", null, null, null));
}
+ @Test
+ void listsTheUnprojectedGsiAttributesOnceInRequestOrder() {
+ ObjectNode names = mapper.createObjectNode();
+ names.put("#q", "qq");
+
+ AwsException error = assertThrows(AwsException.class,
+ () -> DynamoDbAccessPathValidator.validateGsiProjection(
+ table, gsiPath, null, "zz.a, #q, zz.b, aa[0], pk", null, names));
+ assertEquals("One or more parameter values were invalid: Global secondary index "
+ + "status-index does not project [zz, qq, aa]", error.getMessage());
+ }
+
@Test
void rejectsTableAllProjectedAttributesAndInvalidSelectProjectionCombination() {
assertThrows(AwsException.class, () -> DynamoDbAccessPathValidator.validateSelection(
diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConditionKeysTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConditionKeysTest.java
index 6152f221cd..121536f8d1 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConditionKeysTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConditionKeysTest.java
@@ -205,6 +205,19 @@ class DynamoDbConditionKeysTest {
assertTrue(result.attributes().contains("PK"), result.attributes().toString());
}
+ @Test
+ void getItemLeavesAProjectionWithAListIndexRootUnresolved() {
+ DynamoDbConditionKeys.Result result = DynamoDbConditionKeys.extract(
+ "dynamodb:GetItem",
+ json("""
+ {"TableName":"FgacTable",
+ "Key":{"PK":{"S":"USER_alice"},"SK":{"S":"profile"}},
+ "ProjectionExpression":"email, [0]"}"""),
+ table);
+
+ assertEquals(List.of("PK", "SK"), result.attributes());
+ }
+
@Test
void putItemCollectsLiteralAttributeNamesFromAConditionExpression() {
DynamoDbConditionKeys.Result result = DynamoDbConditionKeys.extract(
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 f524e926a0..c9cac653fd 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
@@ -729,6 +729,24 @@ class DynamoDbPartiQLEdgeCaseIntegrationTest {
.body("Items.sk.S", containsInAnyOrder("s1"));
}
+ @Test
+ @Order(31)
+ void checksSelectedColumnsAgainstAGsiProjectionAfterTheWhereClause() {
+ String select = "SELECT zz.a, zz.b, aa FROM \"partiql-edge-cases-index\".\"gsi-inc\"";
+
+ statement(select)
+ .statusCode(400)
+ .body("message", equalTo("One or more parameter values were invalid: "
+ + "Global secondary index gsi-inc does not project [zz, aa]"));
+ statement(select + " WHERE gsiPk='y' AND yy='1'")
+ .statusCode(400)
+ .body("message", equalTo("One or more parameter values were invalid: Secondary index gsi-inc"
+ + " does not project one or more filter attributes: [yy]"));
+ statement(select + " WHERE gsiPk = 1")
+ .statusCode(400)
+ .body("message", equalTo("Key attribute's data type should match its data type in table's schema: Key gsiPk"));
+ }
+
private static ValidatableResponse getItem(String pk) {
return request("DynamoDB_20120810.GetItem", """
{"TableName":"%s","Key":{"pk":{"S":"%s"},"sk":{"S":"1"}},"ConsistentRead":true}