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

* fix(dynamodb): use the AWS wording for unprojected GSI attributes in Query and Scan

* refactor(dynamodb): share the unprojected GSI attribute error with PartiQL

* fix(dynamodb): check PartiQL GSI columns after the WHERE clause

AWS reports a key type mismatch, overlapping ranges, too many reads and an
unprojected filter attribute before an unprojected column. It also lists
each column root once.

* fix(dynamodb): keep rejecting a projection path that starts with a list index
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-10-18 00:55:47 -04:00
1 parent b55014ddef
commit 61102a7c4e
7 files changed
+111 -36

No files matched your search

@@ -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: "
@@ -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();
}
@@ -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) {
@@ -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
@@ -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(
@@ -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(
@@ -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}