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:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-10-25 21:25:38 -04:00
1 parent c7b3d04e5b
commit 17191a9d7b
3 files changed
+192 -6

No files matched your search

@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.dynamodb;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.core.common.AwsException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Stream;
@@ -223,6 +224,8 @@ public class DynamoDbPartiQLParser {
// --- Tokenizer ---
private static final String NOT_WELL_FORMED = "Statement wasn't well formed, can't be processed: ";
static List<Token> tokenize(String input) {
List<Token> tokens = new ArrayList<>();
int i = 0, n = input.length();
@@ -236,7 +239,7 @@ public class DynamoDbPartiQLParser {
if (input.startsWith("/*", i)) {
int end = input.indexOf("*/", i + 2);
if (end < 0) {
throw validationEx("Statement wasn't well formed, can't be processed: ");
throw validationEx(NOT_WELL_FORMED);
}
i = end + 2;
continue;
@@ -285,16 +288,21 @@ public class DynamoDbPartiQLParser {
if (c == ',') { tokens.add(new Token(TType.COMMA, ",")); i++; continue; }
if (c == ':') { tokens.add(new Token(TType.COLON, ":")); i++; continue; }
if (c == ';') { tokens.add(new Token(TType.SEMICOLON, ";")); i++; continue; }
if (c == '.') { tokens.add(new Token(TType.DOT, ".")); i++; continue; }
if (Character.isDigit(c)
|| (c == '-' && i + 1 < n && Character.isDigit(input.charAt(i + 1)) && !endsOperand(tokens))) {
if (startsNumber(input, i, tokens)) {
int start = i;
if (c == '-') i++;
while (i < n && (Character.isDigit(input.charAt(i)) || input.charAt(i) == '.')) i++;
boolean pointSeen = false;
while (i < n && (Character.isDigit(input.charAt(i)) || (input.charAt(i) == '.' && !pointSeen))) {
pointSeen |= input.charAt(i) == '.';
i++;
}
i = skipExponent(input, i);
tokens.add(new Token(TType.NUMBER, input.substring(start, i), start));
String number = input.substring(start, i);
requireRepresentable(number);
tokens.add(new Token(TType.NUMBER, number, start));
continue;
}
if (c == '.') { tokens.add(new Token(TType.DOT, ".")); i++; continue; }
if (c == '+') { tokens.add(new Token(TType.PLUS, "+")); i++; continue; }
if (c == '-') { tokens.add(new Token(TType.MINUS, "-")); i++; continue; }
if (Character.isLetter(c) || c == '_') {
@@ -359,6 +367,42 @@ public class DynamoDbPartiQLParser {
return digits;
}
// A literal may start with its point (.5) or its sign (-5, -.5). After an operand a
// point is a path step and a minus is an operator.
private static boolean startsNumber(String input, int i, List<Token> tokens) {
char c = input.charAt(i);
if (Character.isDigit(c)) {
return true;
}
if (endsOperand(tokens)) {
return false;
}
if (c == '-') {
return i + 1 < input.length() && startsUnsignedNumber(input, i + 1);
}
return startsUnsignedNumber(input, i);
}
private static boolean startsUnsignedNumber(String input, int i) {
char c = input.charAt(i);
return Character.isDigit(c)
|| (c == '.' && i + 1 < input.length() && Character.isDigit(input.charAt(i + 1)));
}
// 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.
private static void requireRepresentable(String number) {
int exponentAt = Math.max(number.indexOf('e'), number.indexOf('E'));
try {
if (exponentAt >= 0) {
Integer.parseInt(number.substring(exponentAt + 1));
}
new BigDecimal(number);
} catch (NumberFormatException expected) {
throw validationEx(NOT_WELL_FORMED);
}
}
// --- Recursive-descent parser ---
private final String statement;
@@ -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);
}
}
@@ -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,