mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): check the arguments of PartiQL set_add, set_delete and list_append (#7737)
* fix(dynamodb): check the arguments of PartiQL set_add, set_delete and list_append * test(dynamodb): cover a list_append with a wrong operand count after the INSERT table name
This commit is contained in:
1 parent
46a8cbe51d
commit
5af751cec8
3 files changed
+273
-55
No files matched your search
@@ -16,6 +16,7 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
@@ -780,6 +781,38 @@ class DynamoDbPartiQLIntegrationTest {
|
||||
+ "operator or function; operator: contains, first operand: [a]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(37)
|
||||
void anUpdateFunctionChecksItsArguments() {
|
||||
putItem("update-function", "\"tags\":{\"SS\":[\"a\"]},\"items\":{\"L\":[{\"S\":\"a\"}]}");
|
||||
String update = "UPDATE \"" + TABLE + "\" SET ";
|
||||
String key = " WHERE pk = 'update-function'";
|
||||
int function = update.length() + "tags = ".length() + 1;
|
||||
|
||||
statement(update + "tags = set_add(tags)" + key)
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("SET_ADD must have exactly two arguments at 1:" + function + ":7"));
|
||||
statement(update + "tags = set_add(<<'a'>>, <<'b'>>)" + key)
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("The first argument to SET_ADD must equal the assignment value at 1:"
|
||||
+ function + ":7"));
|
||||
statement(update + "items = list_append(items)" + key)
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("Incorrect number of operands for operator or function; "
|
||||
+ "operator or function: list_append, number of operands: 1"));
|
||||
statement(update + "items = list_append(items, 1)" + key)
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("Incorrect operand type for operator or function; "
|
||||
+ "operator or function: list_append, operand type: N"));
|
||||
|
||||
statement(update + "tags = set_add(tags, <<'b'>>), items = list_append(items, ['b'])" + key)
|
||||
.statusCode(200);
|
||||
statement("SELECT * FROM \"" + TABLE + "\"" + key)
|
||||
.statusCode(200)
|
||||
.body("Items[0].tags.SS", contains("a", "b"))
|
||||
.body("Items[0].items.L.S", contains("a", "b"));
|
||||
}
|
||||
|
||||
private static void putItem(String pk, String attributes) {
|
||||
request("DynamoDB_20120810.PutItem", """
|
||||
{"TableName":"%s","Item":{"pk":{"S":"%s"},%s}}
|
||||
|
||||
@@ -697,6 +697,175 @@ class DynamoDbPartiQLParserTest {
|
||||
assertEquals(message, e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
UPDATE "t" SET a = set_add() WHERE pk = 1 | SET_ADD must have exactly two arguments at 1:20:7
|
||||
UPDATE "t" SET a = set_add(a) WHERE pk = 1 | SET_ADD must have exactly two arguments at 1:20:7
|
||||
UPDATE "t" SET a = set_delete(a, <<1>>, <<2>>) WHERE pk = 1 | SET_DELETE must have exactly two arguments at 1:20:10
|
||||
UPDATE "t" SET a = "SET_ADD"(a) WHERE pk = 1 | SET_ADD must have exactly two arguments at 1:20:9
|
||||
UPDATE "t" SET a = set_add(foo(1)) WHERE pk = 1 | SET_ADD must have exactly two arguments at 1:20:7
|
||||
UPDATE "t" SET a = set_add(a, <<>>, 1) WHERE pk = 1 | SET_ADD must have exactly two arguments at 1:20:7
|
||||
UPDATE "t" SET a = 'x' + 1, b = set_add(b) WHERE pk = 1 | SET_ADD must have exactly two arguments at 1:33:7
|
||||
UPDATE "t" SET a = list_append(b), c = set_add(c) WHERE pk = 1 | SET_ADD must have exactly two arguments at 1:40:7
|
||||
UPDATE "t" SET a = foo(1), b = set_add(b) WHERE pk = 1 | Unrecognized function: foo at 1:20:3
|
||||
UPDATE "t" SET a = set_add(a), b = foo(1) WHERE pk = 1 | SET_ADD must have exactly two arguments at 1:20:7
|
||||
UPDATE "t" SET a = set_add(b, 1), c = set_add(c) WHERE pk = 1 | The first argument to SET_ADD must equal the assignment value at 1:20:7
|
||||
UPDATE "t" SET a = set_add(a) WHERE foo(1) | Unrecognized function: foo at 1:37:3
|
||||
FROM "t" WHERE attribute_exists('x') SET a = set_add(a) | Operator or function requires a document path; operator or function: attribute_exists
|
||||
UPDATE "t" SET a = set_add(a).b WHERE pk = 1 | Unexpected path component at 1:20:7
|
||||
UPDATE "t" SET a = set_add(foo(1))[0] WHERE pk = 1 | Unexpected path component at 1:20:7
|
||||
UPDATE "t" SET a = set_add(*) WHERE pk = 1 | Statement wasn't well formed, can't be processed: set_add(*) is not allowed
|
||||
UPDATE "t" SET a = set_add(a WHERE pk = 1 | Statement wasn't well formed, can't be processed: Expected RIGHT_PAREN
|
||||
""")
|
||||
void refusesTheWrongNumberOfArgumentsToASetFunction(String statement, String message) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> DynamoDbPartiQLParser.parse(statement, List.of()));
|
||||
|
||||
assertEquals(message, e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
UPDATE "t" SET a = set_add(<<'a'>>, <<'b'>>) WHERE pk = 1 | The first argument to SET_ADD must equal the assignment value at 1:20:7
|
||||
UPDATE "t" SET a = set_delete('a', 'b') WHERE pk = 1 | The first argument to SET_DELETE must equal the assignment value at 1:20:10
|
||||
UPDATE "t" SET a = set_add(1, 2) WHERE pk = 1 | The first argument to SET_ADD must equal the assignment value at 1:20:7
|
||||
UPDATE "t" SET a = set_add(list_append(a, [1]), 1) WHERE pk = 1 | The first argument to SET_ADD must equal the assignment value at 1:20:7
|
||||
UPDATE "t" SET a = set_add(list_append(1, 2), 1) WHERE pk = 1 | The first argument to SET_ADD must equal the assignment value at 1:20:7
|
||||
UPDATE "t" SET a = set_add(foo(1), <<'b'>>) WHERE pk = 1 | Unrecognized function: foo at 1:28:3
|
||||
UPDATE "t" SET a = set_add(a, foo(1)) WHERE pk = 1 | Unrecognized function: foo at 1:31:3
|
||||
UPDATE "t" SET a = set_add(b, foo(1)) WHERE pk = 1 | Unrecognized function: foo at 1:31:3
|
||||
UPDATE "t" SET a = set_add(<<>>, <<'b'>>) WHERE pk = 1 | Empty bags are not supported
|
||||
UPDATE "t" SET a = set_add(b, <<>>) WHERE pk = 1 | Empty bags are not supported
|
||||
UPDATE "t" SET a = set_add(set_add(a, <<'a'>>), <<'b'>>) WHERE pk = 1 | Unrecognized function: set_add at 1:28:7
|
||||
UPDATE "t" SET a = set_add(size(a), <<'b'>>) WHERE pk = 1 | The function is not allowed in an update expression; function: size
|
||||
UPDATE "t" SET a = set_add(a, size(b)) WHERE pk = 1 | The function is not allowed in an update expression; function: size
|
||||
UPDATE "t" SET a = set_add(a, list_append(b, [1])) WHERE pk = 1 | The second argument to SET_ADD must be a value with type SET at 1:28:1
|
||||
UPDATE "t" SET a = set_add(a, list_append(1, 2)) WHERE pk = 1 | The second argument to SET_ADD must be a value with type SET at 1:28:1
|
||||
UPDATE "t" SET a = set_add(a, b) WHERE pk = 1 | The second argument to SET_ADD must be a value with type SET at 1:28:1
|
||||
UPDATE "t" SET a = set_add(a, b.c) WHERE pk = 1 | The second argument to SET_ADD must be a value with type SET at 1:28:1
|
||||
""")
|
||||
void refusesASetFunctionArgumentByItsKind(String statement, String message) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> DynamoDbPartiQLParser.parse(statement, List.of()));
|
||||
|
||||
assertEquals(message, e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
UPDATE "t" SET a = set_add(a) + 1 WHERE pk = 1 | Unrecognized function: set_add at 1:20:7
|
||||
UPDATE "t" SET a = set_delete(a, <<'b'>>) - 1 WHERE pk = 1 | Unrecognized function: set_delete at 1:20:10
|
||||
UPDATE "t" SET a = set_add(<<>>) + 1 WHERE pk = 1 | Empty bags are not supported
|
||||
UPDATE "t" SET a = set_add(foo(1)) + 1 WHERE pk = 1 | Unrecognized function: foo at 1:28:3
|
||||
UPDATE "t" SET a = set_add(a) + foo(1) WHERE pk = 1 | Unrecognized function: set_add at 1:20:7
|
||||
UPDATE "t" SET a = set_add(b, 1) + 'x' WHERE pk = 1 | Unrecognized function: set_add at 1:20:7
|
||||
UPDATE "t" SET a = 1 - set_add(a) WHERE pk = 1 | Unrecognized function: set_add at 1:24:7
|
||||
""")
|
||||
void readsASetFunctionBesideAnOperatorAsAnUnknownFunction(String statement, String message) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> DynamoDbPartiQLParser.parse(statement, List.of()));
|
||||
|
||||
assertEquals(message, e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
list_append() | 0
|
||||
list_append(a) | 1
|
||||
list_append([1]) | 1
|
||||
LIST_APPEND(1) | 1
|
||||
"list_append"('a') | 1
|
||||
list_append(a [1]) | 1
|
||||
list_append(a, [1], [2]) | 3
|
||||
list_append(1, 2, 3, 4) | 4
|
||||
""")
|
||||
void refusesTheWrongNumberOfListAppendOperands(String call, String count) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> DynamoDbPartiQLParser.parse(
|
||||
"UPDATE \"t\" SET a = " + call + " WHERE pk = 1", List.of()));
|
||||
|
||||
assertEquals("Incorrect number of operands for operator or function; operator or function: list_append, "
|
||||
+ "number of operands: " + count, e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
list_append(1, 2) | N
|
||||
list_append(a, 'x') | S
|
||||
list_append('x', a) | S
|
||||
list_append(true, a) | BOOL
|
||||
list_append(a, null) | NULL
|
||||
list_append(<<'x'>>, a) | SS
|
||||
list_append(a, <<1>>) | NS
|
||||
list_append({'x': 1}, a) | M
|
||||
LIST_APPEND(1, 2) | N
|
||||
list_append(list_append(1, 2)) | N
|
||||
list_append(a, b, list_append(1, 2)) | N
|
||||
list_append(list_append(a, [1]), 1) | N
|
||||
""")
|
||||
void refusesAListAppendOperandThatIsNotAList(String call, String type) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> DynamoDbPartiQLParser.parse(
|
||||
"UPDATE \"t\" SET a = " + call + " WHERE pk = 1", List.of()));
|
||||
|
||||
assertEquals("Incorrect operand type for operator or function; operator or function: list_append, "
|
||||
+ "operand type: " + type, e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
UPDATE "t" SET a = list_append(foo(1)) WHERE pk = 1 | Unrecognized function: foo at 1:32:3
|
||||
UPDATE "t" SET a = list_append(a, foo(1), 1) WHERE pk = 1 | Unrecognized function: foo at 1:35:3
|
||||
UPDATE "t" SET a = list_append(<<>>) WHERE pk = 1 | Empty bags are not supported
|
||||
UPDATE "t" SET a = list_append(size(b)) WHERE pk = 1 | The function is not allowed in an update expression; function: size
|
||||
UPDATE "t" SET a = list_append(l[99999999999]) WHERE pk = 1 | List index is not within the allowable range; index: [99999999999] at 1:34:11
|
||||
UPDATE "t" SET a = list_append(b), c = foo(1) WHERE pk = 1 | Unrecognized function: foo at 1:40:3
|
||||
UPDATE "t" SET a = list_append(1, 2), c = <<>> WHERE pk = 1 | Empty bags are not supported
|
||||
UPDATE "t" SET a = list_append(b) WHERE foo(1) | Unrecognized function: foo at 1:41:3
|
||||
FROM "t" WHERE a < true SET a = list_append(b) | Incorrect operand type for operator or function; operator or function: <, operand type: BOOL
|
||||
UPDATE "t" SET a = 'x' + 1, b = list_append(c) WHERE pk = 1 | Incorrect operand type for operator or function; operator or function: +, operand type: S
|
||||
UPDATE "t" SET a = list_append(b), c = 'x' + 1 WHERE pk = 1 | Incorrect number of operands for operator or function; operator or function: list_append, number of operands: 1
|
||||
UPDATE "t" SET a = list_append(1, 2), b = list_append(c) WHERE pk = 1 | Incorrect operand type for operator or function; operator or function: list_append, operand type: N
|
||||
UPDATE "t" SET a = list_append(list_append(b)) WHERE pk = 1 | Incorrect number of operands for operator or function; operator or function: list_append, number of operands: 1
|
||||
UPDATE "t" SET a = list_append(1, list_append(b)) WHERE pk = 1 | Incorrect number of operands for operator or function; operator or function: list_append, number of operands: 1
|
||||
UPDATE "t" SET a = list_append(b).c WHERE pk = 1 | Unexpected path component at 1:20:11
|
||||
UPDATE "t" SET a = list_append(foo(1))[0] WHERE pk = 1 | Unexpected path component at 1:20:11
|
||||
UPDATE "t" SET a = list_append(*) WHERE pk = 1 | Statement wasn't well formed, can't be processed: list_append(*) is not allowed
|
||||
UPDATE "t" SET a = list_append(b WHERE pk = 1 | Statement wasn't well formed, can't be processed: Expected RIGHT_PAREN
|
||||
""")
|
||||
void reportsAListAppendOperandCheckInItsStage(String statement, String message) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> DynamoDbPartiQLParser.parse(statement, List.of()));
|
||||
|
||||
assertEquals(message, e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
UPDATE "t" SET a = list_append(b, [1]) + 1 WHERE pk = 1 | +, operand type: L
|
||||
UPDATE "t" SET a = 1 - list_append(b, [1]) WHERE pk = 1 | -, operand type: L
|
||||
UPDATE "t" SET a = list_append(b, [1]) + 'x' WHERE pk = 1 | +, operand type: L
|
||||
UPDATE "t" SET a = 'x' + list_append(b, [1]) WHERE pk = 1 | +, operand type: S
|
||||
UPDATE "t" SET a = list_append(1, 2) + 1 WHERE pk = 1 | list_append, operand type: N
|
||||
""")
|
||||
void refusesAListAppendBesideAnOperator(String statement, String operand) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> DynamoDbPartiQLParser.parse(statement, List.of()));
|
||||
|
||||
assertEquals("Incorrect operand type for operator or function; operator or function: " + operand,
|
||||
e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void checksAParameterAsTheOperandOfAnUpdateFunction() {
|
||||
List<JsonNode> string = List.of(json("{\"S\":\"x\"}"));
|
||||
List<JsonNode> list = List.of(json("{\"L\":[]}"));
|
||||
List<JsonNode> stringSet = List.of(json("{\"SS\":[\"x\"]}"));
|
||||
String append = "UPDATE \"t\" SET a = list_append(a, ?) WHERE pk = 1";
|
||||
String add = "UPDATE \"t\" SET a = set_add(a, ?) WHERE pk = 1";
|
||||
|
||||
assertDoesNotThrow(() -> DynamoDbPartiQLParser.parse(append, list));
|
||||
assertDoesNotThrow(() -> DynamoDbPartiQLParser.parse(add, stringSet));
|
||||
assertEquals("Incorrect operand type for operator or function; operator or function: list_append, "
|
||||
+ "operand type: S",
|
||||
assertThrows(AwsException.class, () -> DynamoDbPartiQLParser.parse(append, string)).getMessage());
|
||||
assertEquals("The second argument to SET_ADD must be a value with type SET at 1:28:1",
|
||||
assertThrows(AwsException.class, () -> DynamoDbPartiQLParser.parse(add, string)).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void countsInOperandsWithTheOperandChecks() {
|
||||
String tooMany = "The IN operator is provided with too many operands; number of operands: 101";
|
||||
@@ -909,6 +1078,7 @@ class DynamoDbPartiQLParserTest {
|
||||
DELETE FROM "t" WHERE pk = 1 RETURNING ALL NEW * x | LEFTOVER
|
||||
INSERT INTO "t" a VALUE {'pk': 1} | LEFTOVER
|
||||
INSERT INTO "t" {'pk': 1e126}; | MULTIPLE_ITEMS
|
||||
INSERT INTO "t" list_append(a) | MULTIPLE_ITEMS
|
||||
INSERT INTO "t" AS a VALUE {'pk': 1} | NWF Unexpected term
|
||||
SELECT * FROM "t" WHERE at = 1 | NWF Unexpected term
|
||||
UPDATE "t" SET as = 1 WHERE pk = 1 | NWF Expected identifier for simple path
|
||||
|
||||
Reference in new issue
Block a user