mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(dynamodb): check and round PartiQL number literals as DynamoDB does (#6991)
* fix(dynamodb): check and round PartiQL number literals as DynamoDB does * fix(dynamodb): check the depth of a PartiQL literal once the statement parses * fix(dynamodb): stop a refused PartiQL bag from hiding the error DynamoDB reports
This commit is contained in:
1 parent
153eaa95c9
commit
21adc54a74
4 files changed
+312
-82
No files matched your search
@@ -17,6 +17,11 @@ final class DynamoDbNumberUtils {
|
||||
|
||||
private static final BigDecimal MAX_ABS = new BigDecimal("1E+126");
|
||||
private static final BigDecimal MIN_ABS_NONZERO = new BigDecimal("1E-130");
|
||||
static final String NUMBER_OVERFLOW =
|
||||
"Number overflow. Attempting to store a number with magnitude larger than supported range";
|
||||
static final String NUMBER_UNDERFLOW =
|
||||
"Number underflow. Attempting to store a number with magnitude smaller than supported range";
|
||||
static final String TOO_MANY_DIGITS = "Attempting to store more than 38 significant digits in a Number";
|
||||
|
||||
private DynamoDbNumberUtils() {}
|
||||
|
||||
@@ -104,16 +109,16 @@ final class DynamoDbNumberUtils {
|
||||
BigDecimal stripped = bd.stripTrailingZeros();
|
||||
if (stripped.precision() > 38) {
|
||||
throw new AwsException("ValidationException",
|
||||
prefix + "Attempting to store more than 38 significant digits in a Number", 400);
|
||||
prefix + TOO_MANY_DIGITS, 400);
|
||||
}
|
||||
BigDecimal abs = stripped.abs();
|
||||
if (abs.compareTo(MAX_ABS) >= 0) {
|
||||
throw new AwsException("ValidationException",
|
||||
prefix + "Number overflow. Attempting to store a number with magnitude larger than supported range", 400);
|
||||
prefix + NUMBER_OVERFLOW, 400);
|
||||
}
|
||||
if (abs.signum() > 0 && abs.compareTo(MIN_ABS_NONZERO) < 0) {
|
||||
throw new AwsException("ValidationException",
|
||||
prefix + "Number underflow. Attempting to store a number with magnitude smaller than supported range", 400);
|
||||
prefix + NUMBER_UNDERFLOW, 400);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,8 +129,7 @@ final class DynamoDbNumberUtils {
|
||||
BigDecimal abs = stripped.abs();
|
||||
if (stripped.precision() > 38 || abs.compareTo(MAX_ABS) >= 0
|
||||
|| (abs.signum() > 0 && abs.compareTo(MIN_ABS_NONZERO) < 0)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Number overflow. Attempting to store a number with magnitude larger than supported range", 400);
|
||||
throw new AwsException("ValidationException", NUMBER_OVERFLOW, 400);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+204
-77
@@ -5,6 +5,8 @@ import io.github.hectorvent.floci.core.common.AwsException;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -444,8 +446,8 @@ public class DynamoDbPartiQLParser {
|
||||
private final List<JsonNode> parameters;
|
||||
private int pos = 0;
|
||||
private int paramIdx = 0;
|
||||
private int literalDepth = 0;
|
||||
private String unsupportedIsType;
|
||||
// DynamoDB reports these only once the whole statement parses, and the first one in the statement wins.
|
||||
private String deferredError;
|
||||
|
||||
private DynamoDbPartiQLParser(String statement, List<JsonNode> parameters) {
|
||||
this.statement = statement;
|
||||
@@ -463,8 +465,8 @@ public class DynamoDbPartiQLParser {
|
||||
if (parser.peek().type() != TType.EOF) {
|
||||
throw validationEx("Statement wasn't well formed, can't be processed: Unexpected token after expression");
|
||||
}
|
||||
if (parser.unsupportedIsType != null) {
|
||||
throw validationEx(parser.unsupportedIsType);
|
||||
if (parser.deferredError != null) {
|
||||
throw validationEx(parser.deferredError);
|
||||
}
|
||||
stmt.where().stream()
|
||||
.flatMap(DynamoDbPartiQLParser::leaves)
|
||||
@@ -592,16 +594,14 @@ public class DynamoDbPartiQLParser {
|
||||
consume(TType.VALUE);
|
||||
requireTermStart();
|
||||
consume(TType.LBRACE);
|
||||
return new Stmt.Insert(table, parseTupleFields(null));
|
||||
return new Stmt.Insert(table, parseTupleFields());
|
||||
}
|
||||
|
||||
private Map<String, PVal> parseTupleFields(String literalPath) {
|
||||
private Map<String, PVal> parseTupleFields() {
|
||||
Map<String, PVal> fields = new LinkedHashMap<>();
|
||||
while (peek().type() != TType.RBRACE) {
|
||||
requireTermStart();
|
||||
String key = expectStringOrIdent();
|
||||
consume(TType.COLON);
|
||||
fields.put(key, parseValue(literalPath == null ? null : literalPath + "." + key));
|
||||
String key = parseFieldName();
|
||||
fields.put(key, parseValue(null));
|
||||
if (peek().type() == TType.COMMA) {
|
||||
advance();
|
||||
}
|
||||
@@ -617,8 +617,12 @@ public class DynamoDbPartiQLParser {
|
||||
String table = expectTableName();
|
||||
String index = parseIndexQualifier();
|
||||
Edits edits = parseEdits();
|
||||
// DynamoDB checks the WHERE clause before the SET values.
|
||||
String setError = deferredError;
|
||||
deferredError = null;
|
||||
consume(TType.WHERE);
|
||||
List<Cond> where = parseConditions();
|
||||
defer(setError);
|
||||
return new Stmt.Update(table, index, edits.sets(), edits.removes(), where, parseReturning());
|
||||
}
|
||||
|
||||
@@ -853,8 +857,8 @@ public class DynamoDbPartiQLParser {
|
||||
};
|
||||
}
|
||||
|
||||
private static void requireOrdered(String op, PVal val) {
|
||||
if (!ORDERED_TYPES.contains(typeCode(val))) {
|
||||
private void requireOrdered(String op, PVal val) {
|
||||
if (!literalErrorPending() && !ORDERED_TYPES.contains(typeCode(val))) {
|
||||
throw incorrectOperandType(op, val);
|
||||
}
|
||||
}
|
||||
@@ -959,10 +963,10 @@ public class DynamoDbPartiQLParser {
|
||||
advance();
|
||||
}
|
||||
PartiQLType type = parseTypeName(negated);
|
||||
if (!type.supported() && unsupportedIsType == null) {
|
||||
unsupportedIsType = "Unsupported data type for IS operator: " + type.className()
|
||||
if (!type.supported()) {
|
||||
defer("Unsupported data type for IS operator: " + type.className()
|
||||
+ ". Supported data types are [MISSING, NULL, BOOLEAN, BLOB, STRING, NUMERIC, STRUCT, TUPLE,"
|
||||
+ " LIST] at " + position(operand);
|
||||
+ " LIST] at " + position(operand));
|
||||
}
|
||||
return type == PartiQLType.MISSING ? new Cond.Missing(path, negated) : new Cond.IsType(path, type, negated);
|
||||
}
|
||||
@@ -1135,7 +1139,10 @@ public class DynamoDbPartiQLParser {
|
||||
private static final List<String> ATTRIBUTE_TYPE_NAMES =
|
||||
List.of("N", "BS", "L", "B", "NULL", "M", "S", "SS", "NS", "BOOL");
|
||||
|
||||
private static void requireAttributeTypeName(PVal type) {
|
||||
private void requireAttributeTypeName(PVal type) {
|
||||
if (literalErrorPending()) {
|
||||
return;
|
||||
}
|
||||
if (!(type instanceof PVal.Str name)) {
|
||||
throw incorrectOperandType("attribute_type", type);
|
||||
}
|
||||
@@ -1173,8 +1180,8 @@ public class DynamoDbPartiQLParser {
|
||||
return new Assign(path, left, null, null);
|
||||
}
|
||||
|
||||
private static void requireNumberLiteral(String op, Operand operand) {
|
||||
if (operand instanceof Operand.Value value && !(value.val() instanceof PVal.Num)) {
|
||||
private void requireNumberLiteral(String op, Operand operand) {
|
||||
if (!literalErrorPending() && operand instanceof Operand.Value value && !(value.val() instanceof PVal.Num)) {
|
||||
throw incorrectOperandType(op, value.val());
|
||||
}
|
||||
}
|
||||
@@ -1184,14 +1191,14 @@ public class DynamoDbPartiQLParser {
|
||||
String name = function.value().toUpperCase(Locale.ROOT);
|
||||
consume(TType.LPAREN);
|
||||
Token firstArgument = peek();
|
||||
if (!parsePath().equals(target)) {
|
||||
if (!parsePath().equals(target) && !literalErrorPending()) {
|
||||
throw validationEx("The first argument to " + name + " must equal the assignment value at "
|
||||
+ position(function));
|
||||
}
|
||||
consume(TType.COMMA);
|
||||
PVal bag = parseValue();
|
||||
consume(TType.RPAREN);
|
||||
if (!SET_TYPES.contains(typeCode(bag))) {
|
||||
if (!SET_TYPES.contains(typeCode(bag)) && !literalErrorPending()) {
|
||||
throw validationEx("The second argument to " + name + " must be a value with type SET at "
|
||||
+ position(firstArgument));
|
||||
}
|
||||
@@ -1274,40 +1281,154 @@ public class DynamoDbPartiQLParser {
|
||||
return parseValue(null);
|
||||
}
|
||||
|
||||
// Reads a literal with a stack of its open collections instead of recursion, so a deep literal
|
||||
// cannot exhaust the Java stack.
|
||||
private PVal parseValue(String literalPath) {
|
||||
if (literalDepth == DynamoDbAttributeValueValidator.MAX_NESTING_LEVELS) {
|
||||
throw validationEx(DynamoDbAttributeValueValidator.NESTING_EXCEEDED + " under " + literalPath);
|
||||
}
|
||||
requireTermStart();
|
||||
Token t = advance();
|
||||
String nested = literalPath == null ? "root" : literalPath;
|
||||
return switch (t.type()) {
|
||||
case STRING -> new PVal.Str(t.value());
|
||||
case NUMBER -> new PVal.Num(t.value());
|
||||
case BOOL -> new PVal.Bool(Boolean.parseBoolean(t.value()));
|
||||
case NULL -> new PVal.Null();
|
||||
case QUESTION -> {
|
||||
if (literalPath != null) {
|
||||
throw validationEx("Unsupported data type: Parameter under key " + literalPath + " at "
|
||||
+ position(t.start(), 1));
|
||||
}
|
||||
yield resolveParam();
|
||||
Deque<OpenCollection> open = new ArrayDeque<>();
|
||||
String path = literalPath;
|
||||
while (true) {
|
||||
if (open.size() == DynamoDbAttributeValueValidator.MAX_NESTING_LEVELS) {
|
||||
defer(DynamoDbAttributeValueValidator.NESTING_EXCEEDED + " under " + path);
|
||||
}
|
||||
case LBRACKET, LBRACE, LBAG -> parseCollection(t, nested);
|
||||
case PLUS, MINUS -> parseSignedNumber(t);
|
||||
default -> throw validationEx("Expected value literal or ?, got: " + t.value());
|
||||
requireTermStart();
|
||||
Token t = advance();
|
||||
String nested = path == null ? "root" : path;
|
||||
OpenCollection parent = open.peek();
|
||||
PVal value = switch (t.type()) {
|
||||
case STRING -> new PVal.Str(t.value());
|
||||
case NUMBER -> checkedNumber(new PVal.Num(t.value()), nested);
|
||||
case BOOL -> new PVal.Bool(Boolean.parseBoolean(t.value()));
|
||||
case NULL -> new PVal.Null();
|
||||
case QUESTION -> {
|
||||
if (path != null) {
|
||||
defer("Unsupported data type: Parameter under key " + path + " at "
|
||||
+ position(t.start(), 1));
|
||||
}
|
||||
yield resolveParam();
|
||||
}
|
||||
case LBRACKET, LBRACE, LBAG -> {
|
||||
open.push(new OpenCollection(t.type(), nested));
|
||||
yield null;
|
||||
}
|
||||
case PLUS, MINUS -> checkedNumber(parseSignedNumber(t), nested);
|
||||
default -> throw validationEx("Expected value literal or ?, got: " + t.value());
|
||||
};
|
||||
if (parent != null && parent.close == TType.RBAG) {
|
||||
checkBagMember(parent, value);
|
||||
}
|
||||
while (true) {
|
||||
OpenCollection top = open.peek();
|
||||
if (top == null) {
|
||||
return value;
|
||||
}
|
||||
if (value != null) {
|
||||
top.add(value);
|
||||
if (peek().type() == TType.COMMA) {
|
||||
advance();
|
||||
}
|
||||
}
|
||||
if (peek().type() == top.close) {
|
||||
advance();
|
||||
open.pop();
|
||||
value = closed(top);
|
||||
continue;
|
||||
}
|
||||
path = nextPath(top, open.size());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final class OpenCollection {
|
||||
private final TType close;
|
||||
private final String path;
|
||||
private final List<PVal> items = new ArrayList<>();
|
||||
private final Map<String, PVal> fields = new LinkedHashMap<>();
|
||||
private String key;
|
||||
private String memberType;
|
||||
private boolean invalid;
|
||||
|
||||
OpenCollection(TType open, String path) {
|
||||
this.close = switch (open) {
|
||||
case LBRACKET -> TType.RBRACKET;
|
||||
case LBRACE -> TType.RBRACE;
|
||||
default -> TType.RBAG;
|
||||
};
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
void add(PVal value) {
|
||||
if (close == TType.RBRACE) {
|
||||
fields.put(key, value);
|
||||
} else {
|
||||
items.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// No message names a path below the deepest level DynamoDB allows, so paths stop growing there.
|
||||
private String nextPath(OpenCollection collection, int depth) {
|
||||
String step = switch (collection.close) {
|
||||
case RBRACE -> {
|
||||
collection.key = parseFieldName();
|
||||
yield "." + collection.key;
|
||||
}
|
||||
case RBRACKET -> "[" + collection.items.size() + "]";
|
||||
default -> "";
|
||||
};
|
||||
return depth > DynamoDbAttributeValueValidator.MAX_NESTING_LEVELS ? collection.path : collection.path + step;
|
||||
}
|
||||
|
||||
private PVal closed(OpenCollection collection) {
|
||||
return switch (collection.close) {
|
||||
case RBRACKET -> new PVal.ListOf(List.copyOf(collection.items));
|
||||
case RBRACE -> new PVal.Tuple(collection.fields);
|
||||
default -> closedBag(collection);
|
||||
};
|
||||
}
|
||||
|
||||
private PVal parseCollection(Token open, String literalPath) {
|
||||
literalDepth++;
|
||||
PVal collection = switch (open.type()) {
|
||||
case LBRACKET -> new PVal.ListOf(parseListItems(literalPath));
|
||||
case LBRACE -> new PVal.Tuple(parseTupleFields(literalPath));
|
||||
default -> parseBag(literalPath);
|
||||
};
|
||||
literalDepth--;
|
||||
return collection;
|
||||
private String parseFieldName() {
|
||||
requireTermStart();
|
||||
String key = expectStringOrIdent();
|
||||
consume(TType.COLON);
|
||||
return key;
|
||||
}
|
||||
|
||||
private static final MathContext DECIMAL_LITERAL = new MathContext(38, RoundingMode.HALF_EVEN);
|
||||
|
||||
// DynamoDB rounds a literal written with a point or an exponent to 38 digits, and counts the
|
||||
// digits of one written without them.
|
||||
private PVal.Num checkedNumber(PVal.Num number, String literalPath) {
|
||||
if (deferredError != null) {
|
||||
return number;
|
||||
}
|
||||
BigDecimal value = new BigDecimal(number.v());
|
||||
boolean decimal = number.v().chars().anyMatch(c -> c == '.' || c == 'e' || c == 'E');
|
||||
if (decimal && DECIMAL_LITERAL.getPrecision() < value.precision()) {
|
||||
value = value.round(DECIMAL_LITERAL);
|
||||
number = new PVal.Num(value.toString());
|
||||
}
|
||||
String problem = numberLiteralProblem(value);
|
||||
if (problem != null) {
|
||||
defer(problem + " under " + literalPath);
|
||||
}
|
||||
return number;
|
||||
}
|
||||
|
||||
// DynamoDB measures a literal in 32-bit arithmetic, so an exponent near the limit wraps around
|
||||
// and reads as an underflow.
|
||||
private static String numberLiteralProblem(BigDecimal number) {
|
||||
int magnitude = number.precision() - number.scale() + 1;
|
||||
if (127 < magnitude) {
|
||||
return DynamoDbNumberUtils.NUMBER_OVERFLOW;
|
||||
}
|
||||
if (magnitude < -128) {
|
||||
return DynamoDbNumberUtils.NUMBER_UNDERFLOW;
|
||||
}
|
||||
if (38 < number.stripTrailingZeros().precision()) {
|
||||
return DynamoDbNumberUtils.TOO_MANY_DIGITS;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private PVal.Num parseSignedNumber(Token sign) {
|
||||
@@ -1323,41 +1444,47 @@ public class DynamoDbPartiQLParser {
|
||||
return new PVal.Num(number.v().startsWith("-") ? number.v().substring(1) : "-" + number.v());
|
||||
}
|
||||
|
||||
private List<PVal> parseListItems(String literalPath) {
|
||||
List<PVal> items = new ArrayList<>();
|
||||
while (peek().type() != TType.RBRACKET) {
|
||||
items.add(parseValue(literalPath + "[" + items.size() + "]"));
|
||||
if (peek().type() == TType.COMMA) {
|
||||
advance();
|
||||
}
|
||||
// Stands in for a bag DynamoDB refuses, so the statement can be read to its end.
|
||||
private static final PVal.Bag REFUSED_BAG = new PVal.Bag("SS", List.of());
|
||||
|
||||
// A collection member is refused before DynamoDB looks inside it.
|
||||
private void checkBagMember(OpenCollection bag, PVal member) {
|
||||
String type = member == null ? null : typeCode(member);
|
||||
if (bag.items.isEmpty() && ("S".equals(type) || "N".equals(type))) {
|
||||
bag.memberType = type;
|
||||
}
|
||||
if (type == null || !type.equals(bag.memberType)) {
|
||||
bag.invalid = true;
|
||||
defer("Unsupported data type in Bag. DynamoDB only supports either numbers or strings in bags");
|
||||
}
|
||||
consume(TType.RBRACKET);
|
||||
return List.copyOf(items);
|
||||
}
|
||||
|
||||
private PVal.Bag parseBag(String literalPath) {
|
||||
List<PVal> members = new ArrayList<>();
|
||||
while (peek().type() != TType.RBAG) {
|
||||
members.add(parseValue(literalPath));
|
||||
if (peek().type() == TType.COMMA) {
|
||||
advance();
|
||||
}
|
||||
private PVal closedBag(OpenCollection bag) {
|
||||
if (bag.items.isEmpty()) {
|
||||
defer("Empty bags are not supported");
|
||||
return REFUSED_BAG;
|
||||
}
|
||||
consume(TType.RBAG);
|
||||
if (members.isEmpty()) {
|
||||
throw validationEx("Empty bags are not supported");
|
||||
if (bag.invalid) {
|
||||
return REFUSED_BAG;
|
||||
}
|
||||
String memberType = typeCode(members.getFirst());
|
||||
boolean uniform = members.stream().allMatch(m -> typeCode(m).equals(memberType));
|
||||
if (!uniform || !(memberType.equals("S") || memberType.equals("N"))) {
|
||||
throw validationEx(
|
||||
"Unsupported data type in Bag. DynamoDB only supports either numbers or strings in bags");
|
||||
if (!literalErrorPending()) {
|
||||
checkDistinctMembers(bag.memberType, bag.items);
|
||||
}
|
||||
requireDistinctMembers(memberType, members);
|
||||
return new PVal.Bag(memberType + "S", List.copyOf(members));
|
||||
return new PVal.Bag(bag.memberType + "S", List.copyOf(bag.items));
|
||||
}
|
||||
|
||||
private static void requireDistinctMembers(String memberType, List<PVal> members) {
|
||||
// DynamoDB checks operand types only after every literal converts, so a literal error wins.
|
||||
private boolean literalErrorPending() {
|
||||
return deferredError != null;
|
||||
}
|
||||
|
||||
private void defer(String message) {
|
||||
if (deferredError == null) {
|
||||
deferredError = message;
|
||||
}
|
||||
}
|
||||
|
||||
private void checkDistinctMembers(String memberType, List<PVal> members) {
|
||||
boolean numbers = memberType.equals("N");
|
||||
List<String> texts = members.stream()
|
||||
.map(member -> member instanceof PVal.Num n ? n.v() : ((PVal.Str) member).v())
|
||||
@@ -1367,7 +1494,7 @@ public class DynamoDbPartiQLParser {
|
||||
.distinct()
|
||||
.count();
|
||||
if (distinct < texts.size()) {
|
||||
throw validationEx("One or more parameter values were invalid: Input collection ["
|
||||
defer("One or more parameter values were invalid: Input collection ["
|
||||
+ String.join(", ", texts) + "] contains duplicates" + (numbers ? "! under root" : "."));
|
||||
}
|
||||
}
|
||||
|
||||
+22
@@ -81,6 +81,28 @@ class DynamoDbPartiQLNumberLiteralIntegrationTest {
|
||||
.body("message", equalTo(NOT_WELL_FORMED));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void aDecimalIsRoundedAndALiteralOutOfRangeIsRefusedBeforeAnyTableIsRead() {
|
||||
statement("INSERT INTO \"%s\" VALUE {'pk': 0.123456789123456789123456789123456789125}".formatted(TABLE))
|
||||
.statusCode(200);
|
||||
getItem("0.12345678912345678912345678912345678912")
|
||||
.body("Item.pk.N", equalTo("0.12345678912345678912345678912345678912"));
|
||||
|
||||
statement("SELECT * FROM \"%s-never-created\" WHERE pk = 1e126".formatted(TABLE))
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo(
|
||||
"Number overflow. Attempting to store a number with magnitude larger than supported range under root"));
|
||||
String read = JsonNodeFactory.instance.textNode("SELECT * FROM \"%s\" WHERE pk = 1".formatted(TABLE)).toString();
|
||||
String write = JsonNodeFactory.instance.textNode(
|
||||
"INSERT INTO \"%s\" VALUE {'pk': 2, 'l': [1e-131]}".formatted(TABLE)).toString();
|
||||
ddb("ExecuteTransaction", "{\"TransactStatements\":[{\"Statement\":" + read + "},{\"Statement\":" + write + "}]}")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("Validation failed in TransactStatements[1]: Number underflow. "
|
||||
+ "Attempting to store a number with magnitude smaller than supported range under root[0]"));
|
||||
}
|
||||
|
||||
private static ValidatableResponse select(String literal) {
|
||||
return statement("SELECT * FROM \"%s\" WHERE pk = %s".formatted(TABLE, literal));
|
||||
}
|
||||
|
||||
+77
@@ -14,6 +14,7 @@ import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -452,6 +453,82 @@ class DynamoDbPartiQLParserTest {
|
||||
assertEquals("Statement wasn't well formed, can't be processed: ", e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
SELECT * FROM "t" WHERE pk = 1e126 | OVERFLOW under root
|
||||
SELECT * FROM "t" WHERE pk = -1e-131 | UNDERFLOW under root
|
||||
SELECT * FROM "t" WHERE pk = 0e126 | OVERFLOW under root
|
||||
SELECT * FROM "t" WHERE pk = 0e-131 | UNDERFLOW under root
|
||||
SELECT * FROM "t" WHERE pk = 1e2147483646 | UNDERFLOW under root
|
||||
SELECT * FROM "t" WHERE pk = 0.01e2147483647 | OVERFLOW under root
|
||||
SELECT * FROM "t" WHERE pk = 9.99999999999999999999999999999999999999e125 | OVERFLOW under root
|
||||
SELECT * FROM "t" WHERE pk = 123456789012345678901234567890123456789 | DIGITS under root
|
||||
SELECT * FROM "t" WHERE pk IN [1, 1e126] | OVERFLOW under root
|
||||
SELECT * FROM "t" WHERE a = [1, 1e126] | OVERFLOW under root[1]
|
||||
INSERT INTO "t" VALUE {'pk': 1, 'm': {'x': [1e126]}} | OVERFLOW under root.x[0]
|
||||
INSERT INTO "t" VALUE {'pk': 1, 'l': [{'x': <<1e126>>}]} | OVERFLOW under root[0].x
|
||||
SELECT * FROM "t" WHERE a = <<1e126, 1e126>> | OVERFLOW under root
|
||||
SELECT * FROM "t" WHERE pk = 1e126 AND a = <<1, 1>> | OVERFLOW under root
|
||||
SELECT * FROM "t" WHERE a = <<1, 1>> AND pk = 1e126 | DUPLICATES
|
||||
UPDATE "t" SET a = 1e126 WHERE pk = 1e-131 | UNDERFLOW under root
|
||||
UPDATE "t" SET a = <<1, 1>> WHERE pk = 1e126 | OVERFLOW under root
|
||||
UPDATE "t" SET a = set_add(a, <<true>>) WHERE pk = 1e126 | OVERFLOW under root
|
||||
UPDATE "t" SET a = set_add(a, <<true>>) WHERE pk = 1 AND | MISSING_RIGHT_HAND_SIDE
|
||||
SELECT * FROM "t" WHERE pk = 1e126 AND a < [1] | OVERFLOW under root
|
||||
SELECT * FROM "t" WHERE pk = 1e126 AND a = 1 AND | MISSING_RIGHT_HAND_SIDE
|
||||
SELECT * FROM "t" WHERE a = <<1, 1>> AND | MISSING_RIGHT_HAND_SIDE
|
||||
""")
|
||||
void refusesALiteralItCannotStoreOnceTheStatementParses(String statement, String message) {
|
||||
String expected = message
|
||||
.replace("UNDERFLOW", "Number underflow. Attempting to store a number with magnitude smaller than "
|
||||
+ "supported range")
|
||||
.replace("OVERFLOW", "Number overflow. Attempting to store a number with magnitude larger than "
|
||||
+ "supported range")
|
||||
.replace("DIGITS", "Attempting to store more than 38 significant digits in a Number")
|
||||
.replace("DUPLICATES", "One or more parameter values were invalid: Input collection [1, 1] "
|
||||
+ "contains duplicates! under root")
|
||||
.replace("MISSING_RIGHT_HAND_SIDE", "Statement wasn't well formed, can't be processed: "
|
||||
+ "Missing right-hand side expression of infix operator");
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> DynamoDbPartiQLParser.parse(statement, List.of()));
|
||||
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
assertEquals(expected, e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void checksTheDepthOfALiteralOnceTheStatementParses() {
|
||||
String deep = "[".repeat(40) + "1" + "]".repeat(40);
|
||||
|
||||
assertEquals("Statement wasn't well formed, can't be processed: Unexpected token after expression",
|
||||
refusal("INSERT INTO \"t\" VALUE {'pk': 1, 'a': " + deep + "} foo"));
|
||||
assertEquals("Number overflow. Attempting to store a number with magnitude larger than supported range under root",
|
||||
refusal("UPDATE \"t\" SET a = " + deep + " WHERE pk = 1e126"));
|
||||
assertEquals("Nesting Levels have exceeded supported limits under root" + "[0]".repeat(32),
|
||||
refusal("SELECT * FROM \"t\" WHERE a = " + "[".repeat(100_000) + "1" + "]".repeat(100_000)));
|
||||
assertTrue(refusal("SELECT * FROM \"t\" WHERE a = " + "<<".repeat(100_000) + "1" + ">>".repeat(100_000))
|
||||
.startsWith("Unsupported data type in Bag."));
|
||||
}
|
||||
|
||||
private static String refusal(String statement) {
|
||||
return assertThrows(AwsException.class, () -> DynamoDbPartiQLParser.parse(statement, List.of())).getMessage();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"0.123456789123456789123456789123456789125, 0.12345678912345678912345678912345678912",
|
||||
"0.123456789123456789123456789123456789135, 0.12345678912345678912345678912345678914",
|
||||
"12345678912345678912345678912345678917.5, 12345678912345678912345678912345678918",
|
||||
"123456789123456789123456789123456789123.1, 123456789123456789123456789123456789120",
|
||||
"-0.999999999999999999999999999999999999999, -1"
|
||||
})
|
||||
void roundsADecimalLiteralToThirtyEightDigits(String literal, String value) {
|
||||
Cond cond = firstCond(SELECT + "= " + literal);
|
||||
|
||||
PVal val = assertInstanceOf(Cond.Eq.class, cond).val();
|
||||
assertEquals(0, new BigDecimal(value).compareTo(new BigDecimal(assertInstanceOf(PVal.Num.class, val).v())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void readsASecondPointAsALeftoverToken() {
|
||||
AwsException e = assertThrows(AwsException.class, () -> firstCond(SELECT + "= 1.2.3"));
|
||||
|
||||
Reference in new issue
Block a user