fix(dynamodb): refuse BETWEEN bounds of different types (#9351)

A BETWEEN whose bounds have different types is now refused with DynamoDB's ValidationException in a FilterExpression, KeyConditionExpression, ConditionExpression and PartiQL statement, and PartiQL also refuses reversed bounds. Each check runs with the operand type checks, so the first failing condition in written order wins.

The one-violation envelope moves to SmithyValidationErrors.inEnvelope. PutItem, UpdateItem and DeleteItem condition errors carry it, through the Step Functions tasks too, and TransactWriteItems ones no longer do, as on DynamoDB.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-24 11:09:30 -04:00
1 parent 67a619d58e
commit 2f4b2c492a
9 files changed
+272 -32

No files matched your search

@@ -6036,6 +6036,16 @@ given()
PutItem | {"TableName": "ConditionOrderTable", "Item": {"pk": {"S": "a"}}, "ConditionExpression": "nope(x)"} | 1 validation error detected: Invalid ConditionExpression: Invalid function name; function: nope
PutItem | {"TableName": "ConditionOrderTable", "Item": {"pk": {"S": "a"}}, "ConditionExpression": "ATTRIBUTE_EXISTS(x)"} | 1 validation error detected: Invalid ConditionExpression: Invalid function name; function: ATTRIBUTE_EXISTS
Query | {"TableName": "ConditionOrderTable", "KeyConditionExpression": "pk = :k AND BEGINS_WITH(sk, :c)", "ExpressionAttributeValues": {":k": {"S": "a"}, ":c": {"S": "b"}}} | Invalid KeyConditionExpression: Invalid function name; function: BEGINS_WITH
Scan | {"TableName": "ConditionOrderTable", "FilterExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"S": "3"}}} | Invalid FilterExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
Scan | {"TableName": "NoSuchTable", "FilterExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"S": "3"}}} | Invalid FilterExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
Query | {"TableName": "ConditionOrderTable", "KeyConditionExpression": "pk = :k AND sk BETWEEN :a AND :b", "ExpressionAttributeValues": {":k": {"S": "k"}, ":a": {"N": "3"}, ":b": {"S": "3"}}} | Invalid KeyConditionExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
PutItem | {"TableName": "ConditionOrderTable", "Item": {"pk": {"S": "a"}}, "ConditionExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"S": "3"}}} | 1 validation error detected: Invalid ConditionExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
UpdateItem | {"TableName": "ConditionOrderTable", "Key": {"pk": {"S": "a"}}, "UpdateExpression": "SET q = :a", "ConditionExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"S": "3"}}} | 1 validation error detected: Invalid ConditionExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
DeleteItem | {"TableName": "ConditionOrderTable", "Key": {"pk": {"S": "a"}}, "ConditionExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"S": "3"}}} | 1 validation error detected: Invalid ConditionExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
TransactWriteItems | {"TransactItems": [{"ConditionCheck": {"TableName": "ConditionOrderTable", "Key": {"pk": {"S": "a"}}, "ConditionExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"S": "3"}}}}]} | Invalid ConditionExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
TransactWriteItems | {"TransactItems": [{"ConditionCheck": {"TableName": "ConditionOrderTable", "Key": {"pk": {"S": "a"}}, "ConditionExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"N": "1"}}}}]} | Invalid ConditionExpression: The BETWEEN operator requires upper bound to be greater than or equal to lower bound; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {N:1}
TransactWriteItems | {"TransactItems": [{"Put": {"TableName": "ConditionOrderTable", "Item": {"pk": {"S": "a"}}, "ConditionExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"S": "3"}}}}]} | Invalid ConditionExpression: The BETWEEN operator requires same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {S:3}
TransactWriteItems | {"TransactItems": [{"Put": {"TableName": "ConditionOrderTable", "Item": {"pk": {"S": "a"}}, "ConditionExpression": "x BETWEEN :a AND :b", "ExpressionAttributeValues": {":a": {"N": "3"}, ":b": {"N": "1"}}}}]} | Invalid ConditionExpression: The BETWEEN operator requires upper bound to be greater than or equal to lower bound; lower bound operand: AttributeValue: {N:3}, upper bound operand: AttributeValue: {N:1}
""")
void expressionErrorsComeFromTheFirstFailingConditionInWrittenOrder(String operation, String body, String message) {
createOnDemandTableForGsiUpdates("ConditionOrderTable");
@@ -19,6 +19,7 @@ import static io.floci.aws.testing.DynamoDbRequests.nestedMap;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
@@ -1052,6 +1053,130 @@ class DynamoDbPartiQLEdgeCaseIntegrationTest {
getItem("grouped-or").body("Item", nullValue());
}
private static final String NUMERIC_TABLE = "partiql-between-numeric-sk";
private static final String MIXED_BOUNDS = "The BETWEEN operator requires same data type for lower and upper "
+ "bounds; lower bound operand: ";
private static final String REVERSED_BOUNDS = "The BETWEEN operator requires upper bound to be greater than "
+ "or equal to lower bound; lower bound operand: ";
private static final String BETWEEN_WHERE = " WHERE pk='between' AND sk='1' AND ";
@Test
@Order(39)
void refusesUpdateWithBetweenBoundsOfDifferentTypesOrReversed() {
putItem("between", "\"y\":{\"N\":\"2\"}");
String update = "UPDATE " + QUOTED_TABLE + " SET a=1" + BETWEEN_WHERE;
statement(update + "y BETWEEN 1 AND 'b'")
.statusCode(400)
.body("message", equalTo(MIXED_BOUNDS + "AttributeValue: {N:1}, upper bound operand: AttributeValue: {S:b}"));
statement(update + "y BETWEEN 3 AND 1")
.statusCode(400)
.body("message", equalTo(REVERSED_BOUNDS + "AttributeValue: {N:3}, upper bound operand: AttributeValue: {N:1}"));
statement(update + "NOT (y BETWEEN 3 AND 1)").statusCode(400);
statement(update + "(y = 2 OR y BETWEEN 1 AND 'b')").statusCode(400);
getItem("between").body("Item.a", nullValue());
statement(update + "y BETWEEN 2 AND 10").statusCode(200);
statement(update + "y BETWEEN 2.0 AND 2").statusCode(200);
getItem("between").body("Item.a.N", equalTo("1"));
}
@Test
@Order(40)
void refusesSelectDeleteAndTheOtherPartiQLApisWithBetweenBounds() {
String mixed = MIXED_BOUNDS + "AttributeValue: {N:1}, upper bound operand: AttributeValue: {S:b}";
String select = "SELECT * FROM " + QUOTED_TABLE + " WHERE y BETWEEN 1 AND 'b'";
String delete = "DELETE FROM " + QUOTED_TABLE + BETWEEN_WHERE + "y BETWEEN 1 AND 'b'";
statement(select).statusCode(400).body("message", equalTo(mixed));
statement(delete).statusCode(400).body("message", equalTo(mixed));
transaction(delete)
.statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", equalTo("Validation failed in TransactStatements[0]: " + mixed));
request("DynamoDB_20120810.BatchExecuteStatement", "{\"Statements\":[{\"Statement\":" + json(delete) + "}]}")
.statusCode(200)
.body("Responses[0].Error.Code", equalTo("ValidationError"))
.body("Responses[0].Error.Message", equalTo(mixed));
getItem("between").body("Item.y.N", equalTo("2"));
}
@Test
@Order(41)
void showsBetweenBoundsTheWayDynamoDbDoes() {
String update = "UPDATE " + QUOTED_TABLE + " SET a=2" + BETWEEN_WHERE;
statement(update + "y BETWEEN 1e2 AND 1")
.statusCode(400)
.body("message", equalTo(REVERSED_BOUNDS + "AttributeValue: {N:1E+2}, upper bound operand: AttributeValue: {N:1}"));
statement(update + "y BETWEEN ? AND ?", "[{\"N\":\"3.50\"},{\"N\":\"1\"}]")
.statusCode(400)
.body("message", equalTo(REVERSED_BOUNDS + "AttributeValue: {N:3.50}, upper bound operand: AttributeValue: {N:1}"));
statement(update + "y BETWEEN ? AND ?", "[{\"B\":\"AQI=\"},{\"S\":\"3\"}]")
.statusCode(400)
.body("message", equalTo(MIXED_BOUNDS + "AttributeValue: {B:[AQI=]}, upper bound operand: AttributeValue: {S:3}"));
statement(update + "y BETWEEN ? AND ?", "[{\"B\":\"AQI=\"},{\"B\":\"AQ==\"}]")
.statusCode(400)
.body("message", equalTo(REVERSED_BOUNDS + "AttributeValue: {B:[AQI=]}, upper bound operand: AttributeValue: {B:[AQ==]}"));
statement(update + "y BETWEEN 1 AND true")
.statusCode(400)
.body("message", equalTo("Incorrect operand type for operator or function; "
+ "operator or function: BETWEEN, operand type: BOOL"));
getItem("between").body("Item.a.N", equalTo("1"));
}
@Test
@Order(42)
void reportsTheFirstFailingConditionInWrittenOrder() {
String update = "UPDATE " + QUOTED_TABLE + " SET a=3" + BETWEEN_WHERE;
String mixed = MIXED_BOUNDS + "AttributeValue: {N:1}, upper bound operand: AttributeValue: {S:b}";
String reversed = REVERSED_BOUNDS + "AttributeValue: {N:3}, upper bound operand: AttributeValue: {N:1}";
String beginsWith = "Incorrect operand type for operator or function; "
+ "operator or function: begins_with, operand type: N";
statement(update + "y BETWEEN 1 AND 'b' AND y BETWEEN 3 AND 1").body("message", equalTo(mixed));
statement(update + "y BETWEEN 3 AND 1 AND y BETWEEN 1 AND 'b'").body("message", equalTo(reversed));
statement(update + "begins_with(z, 1) AND y BETWEEN 1 AND 'b'").body("message", equalTo(beginsWith));
statement(update + "y BETWEEN 1 AND 'b' AND begins_with(z, 1)").body("message", equalTo(mixed));
statement(update + "y BETWEEN 1 AND 'b' AND y = 1e999")
.body("message", containsString("Number overflow"));
statement(update + "y BETWEEN 1 AND 'b' AND nope(z)")
.body("message", containsString("Unrecognized function: nope"));
statement("UPDATE " + QUOTED_TABLE + " SET x = set_add(z, 1)" + BETWEEN_WHERE + "y BETWEEN 1 AND 'b'")
.statusCode(400)
.body("message", equalTo(mixed));
getItem("between").body("Item.a.N", equalTo("1"));
}
@Test
@Order(43)
void refusesBetweenBoundsOfASortKeyCondition() {
request("DynamoDB_20120810.CreateTable", """
{
"TableName": "%s",
"AttributeDefinitions": [
{"AttributeName":"pk","AttributeType":"S"},
{"AttributeName":"sk","AttributeType":"N"}
],
"KeySchema": [
{"AttributeName":"pk","KeyType":"HASH"},
{"AttributeName":"sk","KeyType":"RANGE"}
],
"BillingMode": "PAY_PER_REQUEST"
}
""".formatted(NUMERIC_TABLE))
.statusCode(200);
String select = "SELECT * FROM \"" + NUMERIC_TABLE + "\" WHERE pk = 'a' AND sk BETWEEN ";
statement(select + "1 AND 'b'")
.statusCode(400)
.body("message", equalTo(MIXED_BOUNDS + "AttributeValue: {N:1}, upper bound operand: AttributeValue: {S:b}"));
statement(select + "3 AND 1")
.statusCode(400)
.body("message", equalTo(REVERSED_BOUNDS + "AttributeValue: {N:3}, upper bound operand: AttributeValue: {N:1}"));
statement(select + "1 AND 3").statusCode(200);
}
private static void putItem(String pk, String attributes) {
putItem(pk, "1", attributes);
}
@@ -706,11 +706,43 @@ class ExpressionEvaluatorTest {
ExpressionEvaluator.validateExpression("n BETWEEN :hi AND :lo",
"ConditionExpression", null, values));
assertEquals("ValidationException", e.getErrorCode());
assertEquals("1 validation error detected: Invalid ConditionExpression: The BETWEEN operator "
assertEquals("Invalid ConditionExpression: The BETWEEN operator "
+ "requires upper bound to be greater than or equal to lower bound; lower bound operand: "
+ "AttributeValue: {N:10}, upper bound operand: AttributeValue: {N:1}", e.getMessage());
}
@ParameterizedTest
@CsvSource(delimiter = '|', textBlock = """
ConditionExpression | {"N":"3"} | {"S":"3"} | {N:3} | {S:3}
FilterExpression | {"S":"a"} | {"B":"AQI="} | {S:a} | {B:AQI=}
KeyConditionExpression | {"B":"AQI="} | {"N":"1"} | {B:AQI=} | {N:1}
""")
void betweenBoundsOfDifferentTypesAreRejected(String exprType, String low, String high,
String lowShown, String highShown) throws Exception {
ObjectMapper mapper = new ObjectMapper();
ObjectNode values = mapper.createObjectNode();
values.set(":lo", mapper.readTree(low));
values.set(":hi", mapper.readTree(high));
AwsException e = assertThrows(AwsException.class, () ->
ExpressionEvaluator.validateExpression("n BETWEEN :lo AND :hi", exprType, null, values));
assertEquals("ValidationException", e.getErrorCode());
assertEquals("Invalid " + exprType + ": The BETWEEN operator requires same data type for lower and "
+ "upper bounds; lower bound operand: AttributeValue: " + lowShown
+ ", upper bound operand: AttributeValue: " + highShown, e.getMessage());
}
@Test
void betweenWithAPathBoundIsNotCheckedForTypes() {
ObjectMapper mapper = new ObjectMapper();
ObjectNode values = mapper.createObjectNode();
values.set(":lo", mapper.createObjectNode().put("N", "3"));
assertDoesNotThrow(() -> ExpressionEvaluator.validateExpression("n BETWEEN :lo AND other",
"ConditionExpression", null, values));
}
// Checked against real DynamoDB (us-east-1, 2026-09-07): U+E000 as the lower bound
// and U+10000 as the upper bound is accepted, because DynamoDB orders strings by
// their UTF-8 bytes. Java's UTF-16 ordering puts them the other way round.
@@ -204,8 +204,22 @@ class DynamoDbNativeBackendTest {
});
assertEquals("ValidationException", refused.getErrorCode());
assertEquals("Invalid ConditionExpression: Invalid function name; function: " + function,
refused.getMessage());
assertEquals("1 validation error detected: Invalid ConditionExpression: Invalid function name; function: "
+ function, refused.getMessage());
}
@Test
void typedPutRefusesBetweenBoundsOfDifferentTypes() throws Exception {
createTable("");
JsonNode key = json("{\"id\":{\"S\":\"k\"}}");
AwsException refused = assertThrows(AwsException.class, () -> backend.putItem(SCOPE, TABLE, key,
"n BETWEEN :a AND :b", null, json("{\":a\":{\"N\":\"3\"},\":b\":{\"S\":\"3\"}}")));
assertEquals("ValidationException", refused.getErrorCode());
assertEquals("1 validation error detected: Invalid ConditionExpression: The BETWEEN operator requires "
+ "same data type for lower and upper bounds; lower bound operand: AttributeValue: {N:3}, "
+ "upper bound operand: AttributeValue: {S:3}", refused.getMessage());
}
@Test