mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): hold a PartiQL Limit to the int range (#8694)
This commit is contained in:
1 parent
f96cd91c8b
commit
0e5785f6ec
3 files changed
+144
-6
No files matched your search
@@ -383,7 +383,7 @@ class DynamoDbPartiQLHandler {
|
||||
start++;
|
||||
}
|
||||
}
|
||||
int end = limit == null ? rows.size() : Math.min(rows.size(), start + limit);
|
||||
int end = limit == null ? rows.size() : start + Math.min(rows.size() - start, limit);
|
||||
JsonNode resumeAfter = start < end && end < rows.size() ? orderingKey(rows.get(end - 1), accessPath, table) : null;
|
||||
return new Page(new ArrayList<>(rows.subList(start, end)), resumeAfter, List.of(scannedBytes), scannedItems);
|
||||
}
|
||||
|
||||
@@ -4284,10 +4284,15 @@ public class NativeDynamoDbJsonHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private static void addLimitError(JsonNode request, List<String> validationErrors) {
|
||||
// DynamoDB drops the fraction of Limit and holds it to the int range.
|
||||
private static Integer partiQLLimit(JsonNode request) {
|
||||
JsonNode limit = request.get("Limit");
|
||||
if (limit != null && !limit.isNull() && limit.asLong() < 1) {
|
||||
validationErrors.add(SmithyValidationErrors.valueTooSmall("limit", limit.asText(), 1));
|
||||
return limit == null || limit.isNull() ? null : (int) limit.doubleValue();
|
||||
}
|
||||
|
||||
private static void addLimitError(Integer limit, List<String> validationErrors) {
|
||||
if (limit != null && limit < 1) {
|
||||
validationErrors.add(SmithyValidationErrors.valueTooSmall("limit", String.valueOf(limit), 1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4333,7 +4338,8 @@ public class NativeDynamoDbJsonHandler {
|
||||
List<String> validationErrors = new ArrayList<>();
|
||||
addPartiQLReturnValuesError(request, "returnValuesOnConditionCheckFailure", validationErrors);
|
||||
addLengthError(request, "NextToken", "nextToken", MAX_NEXT_TOKEN_LENGTH, validationErrors);
|
||||
addLimitError(request, validationErrors);
|
||||
Integer limit = partiQLLimit(request);
|
||||
addLimitError(limit, validationErrors);
|
||||
addStatementError(request, "statement", validationErrors);
|
||||
addReturnConsumedCapacityError(request, validationErrors);
|
||||
addParametersError(request, "parameters", validationErrors);
|
||||
@@ -4342,7 +4348,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
List<JsonNode> parameters = toPartiQLParams(request.path("Parameters"));
|
||||
DynamoDbPartiQLParser.Stmt stmt = parsePartiQLStatement(statement, parameters, region);
|
||||
PartiQLExecuteContext ctx = PartiQLExecuteContext.builder()
|
||||
.limit(request.has("Limit") ? request.get("Limit").asInt() : null)
|
||||
.limit(limit)
|
||||
.nextToken(request.has("NextToken") ? request.get("NextToken").asText() : null)
|
||||
.consistentRead(request.path("ConsistentRead").asBoolean(false))
|
||||
.returnValuesOnConditionCheckFailure(request.path("ReturnValuesOnConditionCheckFailure").asText("NONE"))
|
||||
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
package io.floci.aws.services.dynamodb;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import io.floci.aws.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static io.floci.aws.testing.DynamoDbRequests.ddb;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
/**
|
||||
* ExecuteStatement reads a Limit written past the int range, with an exponent or with a fraction.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class DynamoDbPartiQLLimitIntegrationTest {
|
||||
|
||||
private static final String TABLE = "partiql-limit";
|
||||
private static final String PARTITION = "SELECT * FROM \"" + TABLE + "\" WHERE pk = 'p'";
|
||||
private static final String WHOLE_TABLE = "SELECT * FROM \"" + TABLE + "\"";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTable() {
|
||||
ddb("CreateTable", """
|
||||
{
|
||||
"TableName": "%s",
|
||||
"AttributeDefinitions": [
|
||||
{"AttributeName":"pk","AttributeType":"S"},
|
||||
{"AttributeName":"sk","AttributeType":"S"}
|
||||
],
|
||||
"KeySchema": [
|
||||
{"AttributeName":"pk","KeyType":"HASH"},
|
||||
{"AttributeName":"sk","KeyType":"RANGE"}
|
||||
],
|
||||
"BillingMode": "PAY_PER_REQUEST"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.statusCode(200);
|
||||
for (int row = 1; row <= 4; row++) {
|
||||
ddb("PutItem", """
|
||||
{"TableName":"%s","Item":{"pk":{"S":"p"},"sk":{"S":"row-%d"}}}
|
||||
""".formatted(TABLE, row))
|
||||
.statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@Order(2)
|
||||
@ValueSource(strings = {
|
||||
"2147483648", "4294967297", "9223372036854775808", "18446744073709551617",
|
||||
"100000000000000000000", "1E+2", "1e+20", "1e400",
|
||||
})
|
||||
void aLimitPastTheIntRangeReadsTheWholePartition(String limit) {
|
||||
readsEveryRow(PARTITION, limit);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@Order(3)
|
||||
@ValueSource(strings = {"2147483648", "4294967297", "9223372036854775808", "18446744073709551617", "1e+20"})
|
||||
void aLimitPastTheIntRangeReadsTheWholeTable(String limit) {
|
||||
readsEveryRow(WHOLE_TABLE, limit);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@Order(4)
|
||||
@CsvSource({"2.9, 2", "1.5, 1", "1.5e0, 1", "10e-1, 1"})
|
||||
void aLimitWithAFractionReadsItsWholePart(String limit, int rows) {
|
||||
read(PARTITION, limit)
|
||||
.statusCode(200)
|
||||
.body("Items.size()", equalTo(rows))
|
||||
.body("NextToken", notNullValue());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@Order(5)
|
||||
@CsvSource({
|
||||
"0.5, 0", "0.999, 0", "1e-400, 0", "-0.5, 0", "-1, -1", "-2147483649, -2147483648",
|
||||
"-18446744073709551615, -2147483648", "-1e+20, -2147483648",
|
||||
})
|
||||
void aLimitBelowOneIsReportedAsTheIntItReadsAs(String limit, String reported) {
|
||||
read(PARTITION, limit)
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo("1 validation error detected: Value '" + reported + "' at 'limit' "
|
||||
+ "failed to satisfy constraint: Member must have value greater than or equal to 1"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@Order(6)
|
||||
@ValueSource(strings = {"2147483647", "4294967297", "9223372036854775808"})
|
||||
void aLimitPastTheIntRangeReadsTheRestOfAnOrderedRead(String limit) {
|
||||
String ordered = PARTITION + " ORDER BY sk DESC";
|
||||
String token = read(ordered, "1")
|
||||
.statusCode(200)
|
||||
.body("Items.sk.S", contains("row-4"))
|
||||
.extract().path("NextToken");
|
||||
ddb("ExecuteStatement", "{\"Statement\":" + JsonNodeFactory.instance.textNode(ordered)
|
||||
+ ",\"Limit\":" + limit + ",\"NextToken\":" + JsonNodeFactory.instance.textNode(token) + "}")
|
||||
.statusCode(200)
|
||||
.body("Items.sk.S", contains("row-3", "row-2", "row-1"))
|
||||
.body("NextToken", nullValue());
|
||||
}
|
||||
|
||||
private static void readsEveryRow(String statement, String limit) {
|
||||
read(statement, limit)
|
||||
.statusCode(200)
|
||||
.body("Items.sk.S", contains("row-1", "row-2", "row-3", "row-4"))
|
||||
.body("NextToken", nullValue());
|
||||
}
|
||||
|
||||
private static ValidatableResponse read(String statement, String limit) {
|
||||
return ddb("ExecuteStatement", "{\"Statement\":" + JsonNodeFactory.instance.textNode(statement)
|
||||
+ ",\"Limit\":" + limit + "}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user