feat(dynamodb): index-qualified PartiQL SELECT with ConsistentRead validation (#2953)

* feat(dynamodb): index-qualified PartiQL SELECT with ConsistentRead validation

ExecuteStatement parsed FROM "Table" only — a dot aborted the lexer
with 'Unexpected character', so PartiQL could not read through a
secondary index at all. Lex the qualifier, resolve it to an access
path, and route the read the way AWS does (characterised on real AWS,
eu-west-1, 2026-09-02):

- equality on the selected source's partition key queries it; any
  other WHERE shape becomes a full scan of the table or index with the
  remaining conditions as a filter
- ConsistentRead on a GSI-qualified statement is rejected with
  'Strongly consistent read is not supported on Global Secondary
  Indexes' — PartiQL's own wording, distinct from Query/Scan
- an unknown index omits the index name from the message, again
  unlike Query/Scan; index resolution also precedes the consistent-
  read check
- SELECT over an index surfaces only projected attributes, and an
  explicitly selected non-projected column is rejected
- BatchExecuteStatement SELECT statements must resolve through the
  table's primary key; index-qualified or partial-key statements get
  a per-statement ValidationError slot

Consistent reads on base tables and LSIs stay legal, NextToken/Limit
now paginate both read branches, and GetItem stays a base-table-only
shortcut.

Reproduced with paritysuite/dynamodb-conformance (new
tests/tier2/partiql/executeStatementIndex.test.ts plus ExecuteStatement
cases in tier3/error-messages/indexReads.test.ts): the qualified-read
and message cases fail against main and pass with this change.

Signed-off-by: Kévin Dehoux <dehoux.kevin@gmail.com>

* fix(dynamodb): bind the PartiQL NextToken to its issuing statement

A PartiQL NextToken carried only the last evaluated key, so replaying a
token against another table or access path with identical key names and
scalar types was accepted: Scan continued from the foreign key and
skipped items, Query restarted and duplicated them. Real AWS binds the
opaque token to the request: replaying it against any other statement,
table or access path is rejected with 'NextToken does not match request',
while a changed Limit or a dropped ConsistentRead is accepted and an
older token replays idempotently (characterised on real AWS, eu-west-1,
2026-09-03).

The token now embeds a SHA-256 digest of the statement text and
parameter values alongside the key, and decoding verifies it against the
current request. Wire integration tests cover statement, parameter,
index-qualified and cross-table replay plus the accepted Limit change;
the SDK twin asserts the exact message; the conformance suite gains the
base-table-to-index replay case.

Signed-off-by: Kévin Dehoux <dehoux.kevin@gmail.com>

---------

Signed-off-by: Kévin Dehoux <dehoux.kevin@gmail.com>
This commit is contained in:
Kévin Dehoux authored and GitHub committed 2026-09-04 09:25:46 -04:00
1 parent 7fdd145c0c
commit 84ec8ae8e7
8 files changed
+738 -97

No files matched your search

@@ -15,6 +15,7 @@ class DynamoDbPartiQLTest {
private static DynamoDbClient ddb;
private static final String TABLE = "partiql-test-table";
private static final String INDEX_TABLE = "partiql-index-table";
@BeforeAll
static void setup() {
@@ -34,6 +35,59 @@ class DynamoDbPartiQLTest {
)
.billingMode(BillingMode.PAY_PER_REQUEST)
.build());
try {
ddb.deleteTable(DeleteTableRequest.builder().tableName(INDEX_TABLE).build());
} catch (Exception ignored) {}
ddb.createTable(CreateTableRequest.builder()
.tableName(INDEX_TABLE)
.keySchema(
KeySchemaElement.builder().attributeName("pk").keyType(KeyType.HASH).build(),
KeySchemaElement.builder().attributeName("sk").keyType(KeyType.RANGE).build()
)
.attributeDefinitions(
AttributeDefinition.builder().attributeName("pk").attributeType(ScalarAttributeType.S).build(),
AttributeDefinition.builder().attributeName("sk").attributeType(ScalarAttributeType.S).build(),
AttributeDefinition.builder().attributeName("status").attributeType(ScalarAttributeType.S).build(),
AttributeDefinition.builder().attributeName("createdAt").attributeType(ScalarAttributeType.S).build(),
AttributeDefinition.builder().attributeName("alternate").attributeType(ScalarAttributeType.S).build()
)
.globalSecondaryIndexes(GlobalSecondaryIndex.builder()
.indexName("status-index")
.keySchema(
KeySchemaElement.builder().attributeName("status").keyType(KeyType.HASH).build(),
KeySchemaElement.builder().attributeName("createdAt").keyType(KeyType.RANGE).build()
)
.projection(Projection.builder().projectionType(ProjectionType.ALL).build())
.build())
.localSecondaryIndexes(LocalSecondaryIndex.builder()
.indexName("alternate-index")
.keySchema(
KeySchemaElement.builder().attributeName("pk").keyType(KeyType.HASH).build(),
KeySchemaElement.builder().attributeName("alternate").keyType(KeyType.RANGE).build()
)
.projection(Projection.builder().projectionType(ProjectionType.ALL).build())
.build())
.billingMode(BillingMode.PAY_PER_REQUEST)
.build());
ddb.putItem(PutItemRequest.builder()
.tableName(INDEX_TABLE)
.item(Map.of(
"pk", AttributeValue.builder().s("idxpk").build(),
"sk", AttributeValue.builder().s("s1").build(),
"status", AttributeValue.builder().s("active").build(),
"createdAt", AttributeValue.builder().s("2026-01-01").build(),
"alternate", AttributeValue.builder().s("alt-1").build()))
.build());
ddb.putItem(PutItemRequest.builder()
.tableName(INDEX_TABLE)
.item(Map.of(
"pk", AttributeValue.builder().s("idxpk").build(),
"sk", AttributeValue.builder().s("s2").build(),
"status", AttributeValue.builder().s("active").build(),
"createdAt", AttributeValue.builder().s("2026-01-02").build(),
"alternate", AttributeValue.builder().s("alt-2").build()))
.build());
}
@AfterAll
@@ -42,6 +96,9 @@ class DynamoDbPartiQLTest {
try {
ddb.deleteTable(DeleteTableRequest.builder().tableName(TABLE).build());
} catch (Exception ignored) {}
try {
ddb.deleteTable(DeleteTableRequest.builder().tableName(INDEX_TABLE).build());
} catch (Exception ignored) {}
ddb.close();
}
}
@@ -205,4 +262,91 @@ class DynamoDbPartiQLTest {
assertThat(resp.responses().get(0).item()).containsKey("hit");
assertThat(resp.responses().get(1).item()).isEmpty();
}
@Test
@Order(14)
void executeStatementOnGsiRejectsConsistentRead() {
assertThatThrownBy(() -> ddb.executeStatement(r -> r
.statement("SELECT * FROM \"" + INDEX_TABLE + "\".\"status-index\"")
.consistentRead(true)))
.isInstanceOf(DynamoDbException.class)
.hasMessageContaining("Strongly consistent read is not supported on Global Secondary Indexes");
}
@Test
@Order(15)
void executeStatementReadsThroughGsi() {
// Equality on the index partition key routes through an index query.
ExecuteStatementResponse resp = ddb.executeStatement(r -> r
.statement("SELECT * FROM \"" + INDEX_TABLE + "\".\"status-index\" WHERE status = 'active'"));
assertThat(resp.items()).hasSize(2);
// Without equality on the index partition key, the statement performs
// a full index scan and applies the remaining conditions as a filter.
ExecuteStatementResponse filtered = ddb.executeStatement(r -> r
.statement("SELECT * FROM \"" + INDEX_TABLE + "\".\"status-index\" WHERE sk = 's2'"));
assertThat(filtered.items()).hasSize(1);
assertThat(filtered.items().get(0).get("sk").s()).isEqualTo("s2");
}
@Test
@Order(16)
void executeStatementOnLsiAcceptsConsistentRead() {
ExecuteStatementResponse resp = ddb.executeStatement(r -> r
.statement("SELECT * FROM \"" + INDEX_TABLE + "\".\"alternate-index\" WHERE pk = 'idxpk'")
.consistentRead(true));
assertThat(resp.items()).hasSize(2);
}
@Test
@Order(17)
void executeStatementOnUnknownIndexFailsWithoutIndexName() {
// ExecuteStatement omits the index name from this message, unlike
// Query/Scan (characterised on real AWS, eu-west-1, 2026-09-02).
assertThatThrownBy(() -> ddb.executeStatement(r -> r
.statement("SELECT * FROM \"" + INDEX_TABLE + "\".\"no-such-index\"")))
.isInstanceOfSatisfying(DynamoDbException.class, e -> {
assertThat(e.awsErrorDetails().errorCode()).isEqualTo("ValidationException");
// ExecuteStatement omits the index name from this message,
// unlike Query/Scan (characterised on real AWS, eu-west-1,
// 2026-09-02).
assertThat(e.awsErrorDetails().errorMessage())
.isEqualTo("The table does not have the specified index");
});
}
@Test
@Order(18)
void batchExecuteStatementRejectsIndexQualifiedSelectPerSlot() {
BatchExecuteStatementResponse resp = ddb.batchExecuteStatement(r -> r
.statements(BatchStatementRequest.builder()
.statement("SELECT * FROM \"" + INDEX_TABLE + "\".\"status-index\" WHERE status = 'active'")
.build()));
assertThat(resp.responses()).hasSize(1);
assertThat(resp.responses().get(0).error().codeAsString()).isEqualTo("ValidationError");
assertThat(resp.responses().get(0).error().message()).isEqualTo(
"Select statements within BatchExecuteStatement must specify the primary key in the where clause.");
}
@Test
@Order(19)
void executeStatementRejectsForeignNextToken() {
ExecuteStatementResponse page = ddb.executeStatement(r -> r
.statement("SELECT * FROM \"" + INDEX_TABLE + "\" WHERE pk = 'idxpk'")
.limit(1));
assertThat(page.nextToken()).isNotBlank();
// Characterised on real AWS (eu-west-1, 2026-09-03): the token is bound
// to the issuing statement, so replaying it on another statement or
// access path is rejected even when the target key schema matches.
assertThatThrownBy(() -> ddb.executeStatement(r -> r
.statement("SELECT * FROM \"" + INDEX_TABLE + "\".\"status-index\" WHERE status = 'active'")
.limit(1)
.nextToken(page.nextToken())))
.isInstanceOfSatisfying(DynamoDbException.class, e -> {
assertThat(e.awsErrorDetails().errorCode()).isEqualTo("ValidationException");
assertThat(e.awsErrorDetails().errorMessage())
.isEqualTo("NextToken does not match request");
});
}
}