From a2e12ee004b6a8937e9d616108fdd967b5b68b07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Sat, 31 Oct 2026 18:33:47 +0900 Subject: [PATCH] fix(dynamodb): refuse a PartiQL statement that is one term as DynamoDB does (#7068) --- .../dynamodb/DynamoDbPartiQLParser.java | 129 +++++++++++++++++- .../DynamoDbPartiQLIntegrationTest.java | 20 +++ .../dynamodb/DynamoDbPartiQLParserTest.java | 47 +++++++ 3 files changed, 194 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParser.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParser.java index 71381016b4..5762ae7890 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParser.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParser.java @@ -527,11 +527,130 @@ public class DynamoDbPartiQLParser { case DELETE -> parseDelete(); default -> { requireTermStart(); - throw validationEx(NOT_WELL_FORMED + "Expected data manipulation"); + throw validationEx(expressionStatementRefusal()); } }; } + // DynamoDB reads any other statement as one expression and refuses it by the kind of that expression. + private String expressionStatementRefusal() { + String kind = parseTermKind(); + if (kind == null || continuesExpression(peek())) { + return NOT_WELL_FORMED + "Expected data manipulation"; + } + if (peek().type() == TType.SEMICOLON) { + advance(); + } + if (peek().type() != TType.EOF) { + return UNEXPECTED_TOKEN; + } + return OPERATOR.equals(kind) ? NOT_WELL_FORMED + "Expected data manipulation" : "Unsupported operation: " + kind; + } + + // A sign on anything but a number, or IS NOT. DynamoDB refuses it with a message of its own. + private static final String OPERATOR = "Operator"; + + // A sign folds into the number it applies to. It binds tighter than IS and looser than a path step. + private String parseTermKind() { + Deque signedGroups = new ArrayDeque<>(); + boolean signed = skipSigns(); + while (peek().type() == TType.LPAREN) { + advance(); + requireTermStart(); + signedGroups.push(signed); + signed = skipSigns(); + } + Token primary = peek(); + String kind = parsePrimaryKind(); + if (kind == null) { + return null; + } + boolean number = primary.type() == TType.NUMBER; + signed |= number && primary.value().startsWith("-"); + while (true) { + if (addPathSteps(new ArrayList<>())) { + kind = "Path"; + number = false; + } + if (signed) { + kind = number ? "Lit" : OPERATOR; + } + while (peek().type() == TType.IS) { + advance(); + boolean negated = peek().type() == TType.NOT; + if (negated) { + advance(); + } + parseTypeName(negated); + kind = negated ? OPERATOR : "IsType"; + number = false; + } + if (signedGroups.isEmpty() || peek().type() != TType.RPAREN) { + break; + } + advance(); + signed = signedGroups.pop(); + } + if (signedGroups.isEmpty()) { + return kind; + } + if (continuesExpression(peek())) { + return null; + } + throw validationEx(NOT_WELL_FORMED + "Expected RIGHT_PAREN"); + } + + private boolean skipSigns() { + boolean signed = false; + while (peek().type() == TType.PLUS || peek().type() == TType.MINUS) { + advance(); + signed = true; + } + return signed; + } + + private String parsePrimaryKind() { + Token t = peek(); + return switch (t.type()) { + case QUESTION -> { + advance(); + yield "Parameter"; + } + case MISSING -> { + advance(); + yield "Missing"; + } + case STRING, NUMBER, BOOL, NULL -> { + advance(); + yield "Lit"; + } + case LBRACKET, LBRACE, LBAG -> { + parseValue(); + yield switch (t.type()) { + case LBRACKET -> "List"; + case LBRACE -> "Struct"; + default -> "Bag"; + }; + } + case IDENT -> { + if (isBareWord(t) && KEYWORDS.contains(t.value().toLowerCase(Locale.ROOT)) + || tokens.get(pos + 1).type() == TType.LPAREN) { + yield null; + } + yield parsePath().segments().size() == 1 ? "Id" : "Path"; + } + default -> null; + }; + } + + private static final Set EXPRESSION_CONTINUATIONS = EnumSet.of(TType.EQ, TType.NE, TType.LT, TType.LE, + TType.GT, TType.GE, TType.AND, TType.OR, TType.NOT, TType.IN, TType.BETWEEN, TType.PLUS, TType.MINUS); + + private boolean continuesExpression(Token t) { + return EXPRESSION_CONTINUATIONS.contains(t.type()) || isStar(t) + || Stream.of("LIKE", "UNION", "INTERSECT", "EXCEPT").anyMatch(word -> isWord(t, word)); + } + // SELECT col [, col …] | * FROM "Table"["." "Index"] [WHERE cond [AND cond …]] private Stmt.Select parseSelect() { consume(TType.SELECT); @@ -1327,6 +1446,12 @@ public class DynamoDbPartiQLParser { requireTermStart(); List segments = new ArrayList<>(); segments.add(new Seg.Name(expectIdent())); + addPathSteps(segments); + return new Path(List.copyOf(segments)); + } + + private boolean addPathSteps(List segments) { + int start = segments.size(); while (true) { switch (peek().type()) { case DOT -> { @@ -1339,7 +1464,7 @@ public class DynamoDbPartiQLParser { consume(TType.RBRACKET); } default -> { - return new Path(List.copyOf(segments)); + return start < segments.size(); } } } diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLIntegrationTest.java index 6173246bbe..239c04374b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLIntegrationTest.java @@ -556,6 +556,26 @@ class DynamoDbPartiQLIntegrationTest { equalTo("Statement wasn't well formed, can't be processed: Unexpected token after expression")); } + @Test + @Order(27) + void aStatementThatIsOneTermIsRefusedByItsKind() { + statement("x IS INT4") + .statusCode(400) + .body("__type", equalTo("ValidationException")) + .body("message", equalTo("Unsupported operation: IsType")); + request("DynamoDB_20120810.ExecuteTransaction", """ + {"TransactStatements":[{"Statement":"?"}]} + """) + .statusCode(400) + .body("message", equalTo("Validation failed in TransactStatements[0]: Unsupported operation: Parameter")); + request("DynamoDB_20120810.BatchExecuteStatement", """ + {"Statements":[{"Statement":"x.y IS DOUBLE PRECISION"}]} + """) + .statusCode(200) + .body("Responses[0].Error.Code", equalTo("ValidationError")) + .body("Responses[0].Error.Message", equalTo("Unsupported operation: IsType")); + } + private static void putItem(String pk, String attributes) { request("DynamoDB_20120810.PutItem", """ {"TableName":"%s","Item":{"pk":{"S":"%s"},%s}} diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParserTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParserTest.java index 343b93678e..7facf7c55e 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParserTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParserTest.java @@ -553,6 +553,53 @@ class DynamoDbPartiQLParserTest { assertEquals(expected, refusal(statement)); } + @ParameterizedTest + @CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """ + x IS INT4 | Unsupported operation: IsType + x is character varying; | Unsupported operation: IsType + ((x) IS NULL) IS TIME WITH TIME ZONE | Unsupported operation: IsType + "size" | Unsupported operation: Id + begins_with | Unsupported operation: Id + x.y[0] | Unsupported operation: Path + (x).y | Unsupported operation: Path + 'a'[0] | Unsupported operation: Path + ?.x | Unsupported operation: Path + <<1>>[0] | Unsupported operation: Path + (-1).x | Unsupported operation: Path + (x IS NOT NULL).y | Unsupported operation: Path + - 1 | Unsupported operation: Lit + - + 1 | Unsupported operation: Lit + -((1)) | Unsupported operation: Lit + 1e126 | Unsupported operation: Lit + NULL | Unsupported operation: Lit + MISSING | Unsupported operation: Missing + ? | Unsupported operation: Parameter + [1e126] | Unsupported operation: List + {} | Unsupported operation: Struct + <<1, 1>> | Unsupported operation: Bag + x IS NOT NULL IS NULL | Unsupported operation: IsType + -x IS NULL | Unsupported operation: IsType + -(1) IS NULL | Unsupported operation: IsType + (x).y IS NULL | Unsupported operation: IsType + x IS FOO | NWF Expected type name + x IS NOT | NWF Expected type name + x IS INT4(1) | NWF is type argument integer4 must have arity of 0..0 + x IS INT4 y | NWF Unexpected token after expression + x AS y | NWF Unexpected token after expression + x IS NOT NULL y | NWF Unexpected token after expression + -x y | NWF Unexpected token after expression + x IS NULL.y | NWF Unexpected token after expression + (x) (1) | NWF Unexpected token after expression + () | NWF Unexpected term + (WHERE | NWF Unexpected keyword + ((x) | NWF Expected RIGHT_PAREN + (x IS NOT NULL | NWF Expected RIGHT_PAREN + SLECT * FROM "t" WHERE pk = 'a' | NWF Expected data manipulation + """) + void refusesAStatementThatIsOneTermByItsKind(String statement, String message) { + assertEquals(message.replace("NWF ", "Statement wasn't well formed, can't be processed: "), refusal(statement)); + } + private static String refusal(String statement) { return assertThrows(AwsException.class, () -> DynamoDbPartiQLParser.parse(statement, List.of())).getMessage(); }