mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): read PartiQL number literals as DynamoDB does (#6567)
A literal may begin with its point, with or without a sign: .5, -.5 and +.5e1 are numbers. DynamoDB reads the exponent as a 32-bit integer and refuses a literal whose exponent, or whose scale once the exponent is applied, does not fit one, with the "not well formed" error. Floci answered those with a 500. A second point ends the number, so 1.2.3 leaves a token over instead of failing with a 500.
This commit is contained in:
1 parent
c7b3d04e5b
commit
17191a9d7b
3 files changed
+192
-6
No files matched your search
+99
@@ -0,0 +1,99 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import io.github.hectorvent.floci.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 static io.github.hectorvent.floci.testing.DynamoDbRequests.ddb;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* PartiQL number literals as DynamoDB reads them: a point may lead, an exponent may follow,
|
||||
* and an exponent outside a 32-bit integer makes the statement ill formed.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class DynamoDbPartiQLNumberLiteralIntegrationTest {
|
||||
|
||||
private static final String TABLE = "partiql-number-literals";
|
||||
private static final String NOT_WELL_FORMED = "Statement wasn't well formed, can't be processed: ";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void aLiteralWithNoLeadingDigitMatchesTheNumberItDenotes() {
|
||||
ddb("CreateTable", """
|
||||
{
|
||||
"TableName": "%s",
|
||||
"AttributeDefinitions": [{"AttributeName":"pk","AttributeType":"N"}],
|
||||
"KeySchema": [{"AttributeName":"pk","KeyType":"HASH"}],
|
||||
"BillingMode": "PAY_PER_REQUEST"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.statusCode(200);
|
||||
for (String pk : new String[] {"7.3", "-7.3", "7310000"}) {
|
||||
ddb("PutItem", """
|
||||
{"TableName":"%s","Item":{"pk":{"N":"%s"}}}
|
||||
""".formatted(TABLE, pk))
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
select(".73e1").body("Items.pk.N", contains("7.3"));
|
||||
select("-.73e1").body("Items.pk.N", contains("-7.3"));
|
||||
select("+.73e1").body("Items.pk.N", contains("7.3"));
|
||||
select("731e4").body("Items.pk.N", contains("7310000"));
|
||||
select("73100000e-1").body("Items.pk.N", contains("7310000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void anInsertStoresTheNumberALiteralDenotes() {
|
||||
statement("INSERT INTO \"%s\" VALUE {'pk': .7325e1, 'tag': 'x'}".formatted(TABLE)).statusCode(200);
|
||||
statement("INSERT INTO \"%s\" VALUE {'pk': -.5, 'tag': 'x'}".formatted(TABLE)).statusCode(200);
|
||||
|
||||
getItem("7.325").body("Item.tag.S", equalTo("x"));
|
||||
getItem("-0.5").body("Item.tag.S", equalTo("x"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void anExponentOutsideAnIntIsNotWellFormed() {
|
||||
for (String literal : new String[] {"1e2147483648", "1e-2147483648", "1.5e-2147483647", "1e99999999999"}) {
|
||||
select(literal)
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo(NOT_WELL_FORMED));
|
||||
}
|
||||
statement("INSERT INTO \"%s\" VALUE {'pk': 1e2147483648}".formatted(TABLE))
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo(NOT_WELL_FORMED));
|
||||
}
|
||||
|
||||
private static ValidatableResponse select(String literal) {
|
||||
return statement("SELECT * FROM \"%s\" WHERE pk = %s".formatted(TABLE, literal));
|
||||
}
|
||||
|
||||
private static ValidatableResponse statement(String partiql) {
|
||||
String quoted = JsonNodeFactory.instance.textNode(partiql).toString();
|
||||
return ddb("ExecuteStatement", "{\"Statement\":" + quoted + ",\"ConsistentRead\":true}");
|
||||
}
|
||||
|
||||
private static ValidatableResponse getItem(String pk) {
|
||||
return ddb("GetItem", """
|
||||
{"TableName":"%s","Key":{"pk":{"N":"%s"}},"ConsistentRead":true}
|
||||
""".formatted(TABLE, pk))
|
||||
.statusCode(200);
|
||||
}
|
||||
}
|
||||
+43
@@ -245,6 +245,49 @@ class DynamoDbPartiQLParserTest {
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
".73e1, .73e1",
|
||||
"-.73e1, -.73e1",
|
||||
"+.73e1, .73e1",
|
||||
"5., 5.",
|
||||
"5.e1, 5.e1",
|
||||
"7.31E+6, 7.31E+6",
|
||||
"-7, -7"
|
||||
})
|
||||
void readsANumberLiteralWithALeadingPointOrAnExponent(String literal, String value) {
|
||||
Cond cond = firstCond(SELECT + "= " + literal);
|
||||
|
||||
PVal val = assertInstanceOf(Cond.Eq.class, cond).val();
|
||||
assertEquals(value, assertInstanceOf(PVal.Num.class, val).v());
|
||||
}
|
||||
|
||||
@Test
|
||||
void keepsAPointAfterANameAsAPathStep() {
|
||||
Cond cond = firstCond("SELECT pk FROM \"t\" WHERE a.b = 1");
|
||||
|
||||
Path path = assertInstanceOf(Cond.Eq.class, cond).path();
|
||||
assertEquals(List.of(new Seg.Name("a"), new Seg.Name("b")), path.segments());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"1e2147483648", "1e-2147483648", "1.5e-2147483647", "1e99999999999"})
|
||||
void refusesAnExponentThatDoesNotFitAnInt(String literal) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> firstCond(SELECT + "= " + literal));
|
||||
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
assertEquals("Statement wasn't well formed, can't be processed: ", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void readsASecondPointAsALeftoverToken() {
|
||||
AwsException e = assertThrows(AwsException.class, () -> firstCond(SELECT + "= 1.2.3"));
|
||||
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
assertEquals("Statement wasn't well formed, can't be processed: Unexpected token after expression",
|
||||
e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsAnInOperatorWithMoreThan100Operands() {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
|
||||
Reference in new issue
Block a user