Skip to content
Open
2 changes: 1 addition & 1 deletion crates/engine/src/partiql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mod returning;
mod select;
mod snapshot;
#[cfg(test)]
mod test_context;
pub(crate) mod test_context;
mod transaction;
mod write;

Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/partiql/test_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub(super) fn block_on<F: Future>(future: F) -> F::Output {
}

/// A context whose item authorizer is `authorizer`.
pub(super) async fn context(authorizer: Option<Arc<dyn ItemAuthorizer>>) -> OperationContext {
pub(crate) async fn context(authorizer: Option<Arc<dyn ItemAuthorizer>>) -> OperationContext {
let storage = SqliteEngine::new(":memory:", 1, "us-east-1", 409_600)
.await
.expect("an in-memory database opens");
Expand Down
38 changes: 35 additions & 3 deletions crates/engine/src/streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,61 @@ use crate::OperationContext;
use crate::serialize_output;
use crate::streams_iterator_params as iterator_params;

/// Largest `Limit` `DescribeStream` accepts. The API Reference gives the
/// range 1 to 100 ("The upper limit is 100", minimum value 1,
/// <https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_DescribeStream.html>);
/// the service's refusals are quoted in `streams/describe_limit_tests.rs`.
const DESCRIBE_STREAM_LIMIT_MAX: i64 = 100;

/// Handle `DescribeStream`.
///
/// # Errors
///
/// Returns [`DynamoDbError::ResourceNotFoundException`] if the stream does not exist.
/// Returns [`DynamoDbError::ValidationException`] on invalid input.
/// Returns [`DynamoDbError::ValidationException`] on invalid input, including
/// a `Limit` below 1 or above 100.
pub async fn handle_describe_stream(
body: Value,
ctx: &OperationContext,
) -> Result<Value, DynamoDbError> {
let input: DescribeStreamInput = serde_json::from_value(body)
.map_err(|e| DynamoDbError::SerializationException(e.to_string()))?;
// The service names every failed member constraint in one message before
// it looks the stream up, and refuses a Limit above 100 only once it has
// found the stream (measured in us-east-1; the answers are quoted in
// streams/describe_limit_tests.rs).
let mut clauses = Vec::new();
if let Some(start) = &input.exclusive_start_shard_id
&& start.chars().count() < SHARD_ID_MIN_LEN
{
return Err(DynamoDbError::ValidationException(constraint(
clauses.push(clause(
"exclusiveStartShardId",
"have length greater than or equal to 28",
)));
));
}
if input.limit.is_some_and(|limit| limit < 1) {
clauses.push(clause("limit", "have value greater than or equal to 1"));
}
if !clauses.is_empty() {
return Err(DynamoDbError::ValidationException(
extenddb_core::error::validation_errors_detected(&clauses),
));
}

let desc = ctx
.storage
.describe_stream(&ctx.account_id, &input)
.await
.map_err(storage_to_dynamo)?;
if input
.limit
.is_some_and(|limit| limit > DESCRIBE_STREAM_LIMIT_MAX)
{
return Err(DynamoDbError::ValidationException(format!(
"One or more parameter values were invalid: Limit cannot be greater than \
the maximum: {DESCRIBE_STREAM_LIMIT_MAX}"
)));
}

let output = DescribeStreamOutput {
stream_description: desc,
Expand Down Expand Up @@ -554,3 +583,6 @@ fn storage_to_dynamo(e: StorageError) -> DynamoDbError {
#[cfg(test)]
#[path = "streams_tests.rs"]
mod tests;

#[cfg(all(test, feature = "partiql"))]
mod describe_limit_tests;
138 changes: 138 additions & 0 deletions crates/engine/src/streams/describe_limit_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright 2026 ExtendDB contributors
// SPDX-License-Identifier: Apache-2.0

//! `DescribeStream` refuses a `Limit` below 1 together with the other member
//! constraints before it looks the stream up, and a `Limit` above 100 only
//! after it has found the stream.
//!
//! Measured in us-east-1 with the SDK's own parameter validation switched
//! off, so the requests reached the service. On 2026-10-22, on an existing
//! stream:
//!
//! ```text
//! Limit 0 and Limit -1:
//! 400 {"__type":"com.amazon.coral.validate#ValidationException","message":"1 validation error detected: Value at 'limit' failed to satisfy constraint: Member must have value greater than or equal to 1"}
//! Limit 101 and Limit 1000:
//! 400 {"__type":"com.amazon.coral.validate#ValidationException","message":"One or more parameter values were invalid: Limit cannot be greater than the maximum: 100"}
//! ```
//!
//! On 2026-11-25, each request twice, on a stream of a table that does not
//! exist: Limit 0 and -1 got the first body above; Limit 1, 101 and 1000, and
//! Limit 101 with a 36-character `ExclusiveStartShardId`, got 400

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional. VALID_ID is 37 characters, so this line and the PR body's table say 36 for a 37-character id. The answer is the same at either length.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, you're right. The measured id was shardId-00000001700000000000-00000000, which is 37 characters, and the body's table now says 37. The test comment stays as it is, because the answer is the same at either length.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same note as the one above, answered there: the body's table now says 37, and the test comment stays as it is.

//! `ResourceNotFoundException` "Requested resource not found: Stream: <arn>
//! not found"; Limit 0 with a 13-character `ExclusiveStartShardId` got the
//! two clauses in one message ([`SHORT_START_AND_BELOW_ONE`]); Limit 101 with
//! that start got the start clause alone ([`SHORT_START`]).
//!
//! The tests run the handler on the engine's test context over a migrated
//! in-memory engine, so a request that reaches storage for a missing stream
//! gets `ResourceNotFoundException`.

use std::sync::Arc;

use serde_json::json;

use extenddb_core::error::DynamoDbError;

use super::handle_describe_stream;
use crate::OperationContext;
use crate::partiql::test_context::context;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new tests do not compile without the engine's partiql feature: cargo test -p extenddb-engine --no-default-features --lib --no-run fails with E0433 on this import, and exits 0 with the new module removed. CI does not build that configuration.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0c97f41. streams.rs declares the module under cfg(all(test, feature = "partiql")). cargo test -p extenddb-engine --no-default-features --lib --no-run builds with the gate and fails with E0433 without it.


const MISSING: &str =
"arn:aws:dynamodb:us-east-1:000000000000:table/Missing/stream/2026-10-22T00:00:00.000";
const SHORT_ID: &str = "shardId-short";
const VALID_ID: &str = "shardId-00000001700000000000-00000000";

const BELOW_ONE: &str = "1 validation error detected: Value at 'limit' failed to satisfy \
constraint: Member must have value greater than or equal to 1";
const ABOVE_MAX: &str =
"One or more parameter values were invalid: Limit cannot be greater than the maximum: 100";
const SHORT_START: &str = "1 validation error detected: Value at 'exclusiveStartShardId' \
failed to satisfy constraint: Member must have length greater \
than or equal to 28";
const SHORT_START_AND_BELOW_ONE: &str = "2 validation errors detected: Value at \
'exclusiveStartShardId' failed to satisfy constraint: Member must have length greater \
than or equal to 28; Value at 'limit' failed to satisfy constraint: Member must have \
value greater than or equal to 1";

/// The answer a row expects.
enum Answer {
Refused(&'static str),
NotFound,
Served,
}

/// The test context over a migrated in-memory engine holding the table
/// `Orders` with a stream. Returns the context and the stream's ARN.
async fn context_with_stream() -> (OperationContext, String) {
let mut ctx = context(None).await;
let storage =
extenddb_storage_sqlite::SqliteEngine::new_in_memory("us-east-1", &ctx.account_id, 409_600)
.await
.expect("a migrated in-memory engine opens");
ctx.storage = Arc::new(storage);
let created = crate::handle_create_table(
json!({
"TableName": "Orders",
"AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"BillingMode": "PAY_PER_REQUEST",
"StreamSpecification": {"StreamEnabled": true, "StreamViewType": "KEYS_ONLY"}
}),
&ctx,
)
.await
.expect("the table is created");
let arn = created["TableDescription"]["LatestStreamArn"]
.as_str()
.expect("the table has a stream")
.to_owned();
(ctx, arn)
}

#[tokio::test]
async fn describe_stream_checks_limit_where_the_service_does() {
let (ctx, existing) = context_with_stream().await;
let not_found = format!("Requested resource not found: Stream: {MISSING} not found");
let rows: [(&str, i64, Option<&str>, Answer); 14] = [
(existing.as_str(), 0, None, Answer::Refused(BELOW_ONE)),
(existing.as_str(), -1, None, Answer::Refused(BELOW_ONE)),
(existing.as_str(), 101, None, Answer::Refused(ABOVE_MAX)),
(existing.as_str(), 1000, None, Answer::Refused(ABOVE_MAX)),
(existing.as_str(), 1, None, Answer::Served),
(existing.as_str(), 100, None, Answer::Served),
(MISSING, 0, None, Answer::Refused(BELOW_ONE)),
(MISSING, -1, None, Answer::Refused(BELOW_ONE)),
(MISSING, 1, None, Answer::NotFound),
(MISSING, 101, None, Answer::NotFound),
(MISSING, 1000, None, Answer::NotFound),
(
MISSING,
0,
Some(SHORT_ID),
Answer::Refused(SHORT_START_AND_BELOW_ONE),
),
(MISSING, 101, Some(SHORT_ID), Answer::Refused(SHORT_START)),
(MISSING, 101, Some(VALID_ID), Answer::NotFound),
];
for (arn, limit, start, expected) in rows {
let mut body = json!({"StreamArn": arn, "Limit": limit});
if let Some(start) = start {
body["ExclusiveStartShardId"] = json!(start);
}
let row = format!("{arn} Limit {limit} start {start:?}");
let answer = handle_describe_stream(body, &ctx).await;
match (answer, expected) {
(Err(DynamoDbError::ValidationException(m)), Answer::Refused(e)) => {
assert_eq!(m, e, "{row}");
}
(Err(DynamoDbError::ResourceNotFoundException(m)), Answer::NotFound) => {
assert_eq!(m, not_found, "{row}");
}
(Ok(v), Answer::Served) => {
assert_eq!(v["StreamDescription"]["StreamArn"], json!(arn), "{row}");
}
(other, _) => panic!("{row}: got {other:?}"),
}
}
}
16 changes: 4 additions & 12 deletions crates/wasm-adapter/parity/corpus.json
Original file line number Diff line number Diff line change
Expand Up @@ -2858,25 +2858,17 @@
}
},
{
"name": "DescribeStream with a Limit above 2^32 returns all four shards and no LastEvaluatedShardId",
"name": "DescribeStream with a Limit above 2^32 is refused as a Limit above 100, not read as a Limit of 1",
"op": "DescribeStream",
"body": {
"StreamArn": "${strmArn}",
"Limit": 4294967297
},
"error": null,
"error": "ValidationException",
"checks": [
{
"path": "/StreamDescription/Shards/3/ShardId",
"present": true
},
{
"path": "/StreamDescription/Shards/4",
"absent": true
},
{
"path": "/StreamDescription/LastEvaluatedShardId",
"absent": true
"path": "/message",
"equals": "One or more parameter values were invalid: Limit cannot be greater than the maximum: 100"
}
]
},
Expand Down
Loading