diff --git a/README.md b/README.md index a3d272b..0d017f9 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ This package provides an implementation for the `@aws-appsync/utils` package tha ## Changelog: +- v0.1.11: `rds.select()` renders joins (`join`, `leftJoin` and the other kinds, with `on` or `using`), which were previously dropped without an error, subqueries aliased in `from` and aliased columns; add `rds.agg` aggregates, the `groupBy` and `having` select clauses and `not` conditions; `and`/`or`/`not` are recognised in any casing, and an `or` item with several columns now ANDs them instead of ORing them - v0.1.10: `util.authType()` now reports `User Pool Authorization` for the user pool identity of an Event API request, which carries no `defaultAuthStrategy` unlike the GraphQL one - v0.1.9: add `runtime.earlyReturn()`, importable from `@aws-appsync/utils`; it validates the `skipTo` return option and hands the value and target to the host, which decides which handlers still run - v0.1.8: `util.authType()` now reports `Lambda Authorization` for the identity of a Lambda-authorized Event API request, which carries what the authorizer returned as `handlerContext` rather than the `resolverContext` a GraphQL API uses diff --git a/__tests__/__snapshots__/rds-aggregates.test.js.snap b/__tests__/__snapshots__/rds-aggregates.test.js.snap new file mode 100644 index 0000000..4c0555a --- /dev/null +++ b/__tests__/__snapshots__/rds-aggregates.test.js.snap @@ -0,0 +1,810 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`agg imported by name 1`] = ` +{ + "statements": [ + "SELECT COUNT(*) FROM "t"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`aggregate arguments rejects a boolean 1`] = ` +{ + "message": "java.lang.ClassCastException: class java.lang.Boolean cannot be cast to class java.lang.String (java.lang.Boolean and java.lang.String are in module java.base of loader 'bootstrap')", +} +`; + +exports[`aggregate arguments rejects a fractional number 1`] = ` +{ + "message": "java.lang.ClassCastException: class java.lang.Double cannot be cast to class java.lang.String (java.lang.Double and java.lang.String are in module java.base of loader 'bootstrap')", +} +`; + +exports[`aggregate arguments rejects a missing argument 1`] = ` +{ + "message": "A single argument is expected to be passed to aggregates.", +} +`; + +exports[`aggregate arguments rejects a star in a distinct aggregate 1`] = ` +{ + "message": "* is an invalid argument for distinct aggregates.", +} +`; + +exports[`aggregate arguments rejects a star in a distinct aggregate other than count 1`] = ` +{ + "message": "* is an invalid argument for distinct aggregates.", +} +`; + +exports[`aggregate arguments rejects a star in an aggregate other than count 1`] = ` +{ + "message": "* is an invalid argument for all aggregates, except COUNT", +} +`; + +exports[`aggregate arguments rejects an aggregate as a compared value 1`] = ` +{ + "message": "Only primitive values are supported as parameter.", +} +`; + +exports[`aggregate arguments rejects an aggregate as a from source 1`] = ` +{ + "message": "Object value must be a SQL statement or string.", +} +`; + +exports[`aggregate arguments rejects an aggregate as a statement 1`] = ` +{ + "message": "Unsupported type is passed as argument to createPgStatement", +} +`; + +exports[`aggregate arguments rejects an aggregate as an inserted value 1`] = ` +{ + "message": "Only primitive values are supported as parameter.", +} +`; + +exports[`aggregate arguments rejects an array 1`] = ` +{ + "message": "java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class java.lang.String (java.util.ArrayList and java.lang.String are in module java.base of loader 'bootstrap')", +} +`; + +exports[`aggregate arguments rejects an integer 1`] = ` +{ + "message": "java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')", +} +`; + +exports[`aggregate arguments rejects an integer beyond 32 bits 1`] = ` +{ + "message": "java.lang.ClassCastException: class java.lang.Long cannot be cast to class java.lang.String (java.lang.Long and java.lang.String are in module java.base of loader 'bootstrap')", +} +`; + +exports[`aggregate arguments rejects an object 1`] = ` +{ + "message": "java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class java.lang.String (java.util.LinkedHashMap and java.lang.String are in module java.base of loader 'bootstrap')", +} +`; + +exports[`aggregate columns a null column 1`] = ` +{ + "statements": [ + "SELECT MAX() FROM "t"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`aggregate columns a null column in a distinct aggregate 1`] = ` +{ + "statements": [ + "SELECT COUNT(DISTINCT ) FROM "t"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`aggregate columns a qualified column 1`] = ` +{ + "statements": [ + "SELECT SUM(DISTINCT "x"."y") FROM "t"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`aggregate columns a qualified star in a distinct count 1`] = ` +{ + "statements": [ + "SELECT COUNT(DISTINCT "t"."*") FROM "t"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`aggregate columns an aggregate in a subquery 1`] = ` +{ + "statements": [ + "SELECT * FROM (SELECT MAX("a") as "m" FROM "u" GROUP BY "b") as "s"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`aggregate columns an aggregate in using 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" LEFT JOIN "u" USING (MAX("a"))", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`aggregate columns an aliased aggregate 1`] = ` +{ + "statements": [ + "SELECT MAX("a") as "m", "b" FROM "t"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`aggregate columns an aliased aggregate in mysql 1`] = ` +{ + "statements": [ + "SELECT AVG(\`db\`.\`a\`) as \`m\` FROM \`t\`", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`aggregate columns an aliased aggregate in returning 1`] = ` +{ + "statements": [ + "DELETE FROM "t" RETURNING MAX("a") as "m"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`aggregate columns an aliased aggregate in using 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" LEFT JOIN "u" USING (MAX("a") as "k")", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`aggregate columns an empty column 1`] = ` +{ + "statements": [ + "SELECT COUNT(DISTINCT "") FROM "t"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`aggregate columns an undefined column 1`] = ` +{ + "statements": [ + "SELECT MAX() FROM "t"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`aggregate columns count of every row 1`] = ` +{ + "statements": [ + "SELECT COUNT(*) FROM "t"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`aggregate columns every aggregate 1`] = ` +{ + "statements": [ + "SELECT MAX("a"), MAX(DISTINCT "a"), MIN("a"), MIN(DISTINCT "a"), COUNT("a"), COUNT(DISTINCT "a"), AVG("a"), AVG(DISTINCT "a"), SUM("a"), SUM(DISTINCT "a") FROM "t"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`aggregate columns rejects an aggregate in returning 1`] = ` +{ + "message": "Only columns in SELECT statements can include aggregates.", +} +`; + +exports[`aggregate columns rejects an aggregate in returning in mysql 1`] = ` +{ + "message": "Only columns in SELECT statements can include aggregates.", +} +`; + +exports[`aggregate conditions outside having rejects a malformed one in where 1`] = ` +{ + "message": "Aggregates are not valid for WHERE conditions.", +} +`; + +exports[`aggregate conditions outside having rejects a null one in where 1`] = ` +{ + "message": "Value for a.count can't be null.", +} +`; + +exports[`aggregate conditions outside having rejects one in a remove 1`] = ` +{ + "message": "Aggregates are not valid for WHERE conditions.", +} +`; + +exports[`aggregate conditions outside having rejects one in an update 1`] = ` +{ + "message": "Aggregates are not valid for WHERE conditions.", +} +`; + +exports[`aggregate conditions outside having rejects one in on 1`] = ` +{ + "message": "Aggregates are not valid for ON conditions.", +} +`; + +exports[`aggregate conditions outside having rejects one in where 1`] = ` +{ + "message": "Aggregates are not valid for WHERE conditions.", +} +`; + +exports[`aggregate conditions outside having rejects one nested in where 1`] = ` +{ + "message": "Aggregates are not valid for WHERE conditions.", +} +`; + +exports[`aggregate conditions outside having rejects one with a number in on 1`] = ` +{ + "message": "Aggregates are not valid for ON conditions.", +} +`; + +exports[`aggregate counts grouped by a column 1`] = ` +{ + "statements": [ + "SELECT "customer", COUNT(*) as "orders", SUM("price") as "total" FROM "orders" WHERE "status" = :P0 GROUP BY "customer" HAVING SUM("price") > :P1 ORDER BY "customer" ASC", + ], + "variableMap": { + ":P0": "PAID", + ":P1": 100, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`group by a null group by renders nothing 1`] = ` +{ + "statements": [ + "SELECT * FROM "t"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`group by column positions 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" GROUP BY 1, "a", 0, -2, 2", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`group by columns 1`] = ` +{ + "statements": [ + "SELECT "a", COUNT(*) as "n" FROM "t" GROUP BY "a", "b"."c"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`group by group by is validated before having 1`] = ` +{ + "message": "Column values are expected for GROUP BY.", +} +`; + +exports[`group by in mysql 1`] = ` +{ + "statements": [ + "SELECT * FROM \`t\` GROUP BY \`db\`.\`a\`, 2", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`group by insert ignores group by and having 1`] = ` +{ + "statements": [ + "INSERT INTO "t" ("a") VALUES (:P0)", + ], + "variableMap": { + ":P0": 1, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`group by rejects NaN 1`] = ` +{ + "message": "Invalid type in GROUP BY column array.", +} +`; + +exports[`group by rejects a boolean 1`] = ` +{ + "message": "Invalid type in GROUP BY column array.", +} +`; + +exports[`group by rejects a fractional number 1`] = ` +{ + "message": "Invalid type in GROUP BY column array.", +} +`; + +exports[`group by rejects a string 1`] = ` +{ + "message": "groupBy expects an array.", +} +`; + +exports[`group by rejects an aggregate 1`] = ` +{ + "message": "Invalid type in GROUP BY column array.", +} +`; + +exports[`group by rejects an alias object 1`] = ` +{ + "message": "Invalid type in GROUP BY column array.", +} +`; + +exports[`group by rejects an empty list 1`] = ` +{ + "message": "Column values are expected for GROUP BY.", +} +`; + +exports[`group by rejects an integer beyond 32 bits 1`] = ` +{ + "message": "Invalid type in GROUP BY column array.", +} +`; + +exports[`group by rejects null 1`] = ` +{ + "message": "Invalid type in GROUP BY column array.", +} +`; + +exports[`group by remove ignores group by 1`] = ` +{ + "statements": [ + "DELETE FROM "t"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`group by stars are quoted 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" GROUP BY "*", "a"."*"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`group by where is validated before group by 1`] = ` +{ + "message": "WHERE values are expected to be SQL templates or a condition object.", +} +`; + +exports[`having a count of every row 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING COUNT(*) > :P0", + ], + "variableMap": { + ":P0": 1, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having a null having renders nothing 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" GROUP BY "a"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`having a null value 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" GROUP BY "a" HAVING COUNT("a") > NULL", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`having a qualified column 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING SUM("x"."y") > :P0", + ], + "variableMap": { + ":P0": 1, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having a sql template 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" GROUP BY "a" HAVING count(*) > :P0", + ], + "variableMap": { + ":P0": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having a string value 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING MAX("a") > :P0", + ], + "variableMap": { + ":P0": "z", + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having a type hinted value 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING SUM("a") > :P0", + ], + "variableMap": { + ":P0": "1.5", + }, + "variableTypeHintMap": { + ":P0": "DECIMAL", + }, +} +`; + +exports[`having an aggregate condition 1`] = ` +{ + "statements": [ + "SELECT "a" FROM "t" GROUP BY "a" HAVING COUNT("a") > :P0", + ], + "variableMap": { + ":P0": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having an and group 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING (COUNT("a") > :P0) AND (SUM("b") < :P1)", + ], + "variableMap": { + ":P0": 1, + ":P1": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having an empty aggregate condition renders nothing 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" GROUP BY "a"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`having an empty condition renders nothing 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" GROUP BY "a"", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`having avg 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING AVG("a") >= :P0", + ], + "variableMap": { + ":P0": 3, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having avgDistinct 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING AVG(DISTINCT "a") = :P0", + ], + "variableMap": { + ":P0": 8, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having count 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING COUNT("a") = :P0", + ], + "variableMap": { + ":P0": 5, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having countDistinct 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING COUNT(DISTINCT "a") = :P0", + ], + "variableMap": { + ":P0": 10, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having in mysql 1`] = ` +{ + "statements": [ + "SELECT * FROM \`t\` GROUP BY \`db\`.\`a\` HAVING COUNT(\`db\`.\`a\`) > :P0", + ], + "variableMap": { + ":P0": 1, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having max 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING MAX("a") > :P0 AND MAX("a") < :P1", + ], + "variableMap": { + ":P0": 1, + ":P1": 9, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having maxDistinct 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING MAX(DISTINCT "a") != :P0", + ], + "variableMap": { + ":P0": 6, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having min 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING MIN("a") < :P0", + ], + "variableMap": { + ":P0": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having minDistinct 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING MIN(DISTINCT "a") = :P0", + ], + "variableMap": { + ":P0": 7, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having plain and aggregate conditions on one column 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING COUNT("a") > :P0 AND "a" = :P1 AND MAX("a") < :P2", + ], + "variableMap": { + ":P0": 1, + ":P1": "x", + ":P2": 3, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having plain conditions alongside aggregate ones 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" GROUP BY "a" HAVING "a" = :P0 AND (COUNT("b") > :P1) OR (LENGTH ("c") < :P2)", + ], + "variableMap": { + ":P0": "x", + ":P1": 1, + ":P2": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having rejects a condition operator 1`] = ` +{ + "message": "a.count has invalid aggregate operator.", +} +`; + +exports[`having rejects a distinct count of every row 1`] = ` +{ + "message": "* is an invalid argument for distinct aggregates.", +} +`; + +exports[`having rejects a max of every row 1`] = ` +{ + "message": "* is an invalid argument for all aggregates, except COUNT", +} +`; + +exports[`having rejects a null aggregate condition 1`] = ` +{ + "message": "Value for a.count can't be null.", +} +`; + +exports[`having rejects a string 1`] = ` +{ + "message": "HAVING values are expected to be SQL templates or a condition object.", +} +`; + +exports[`having rejects a value that is not an object 1`] = ` +{ + "message": "Expected a.count to be an Object.", +} +`; + +exports[`having rejects an array 1`] = ` +{ + "message": "HAVING values are expected to be SQL templates or a condition object.", +} +`; + +exports[`having rejects between 1`] = ` +{ + "message": "a.count has invalid aggregate operator.", +} +`; + +exports[`having sum 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING SUM("a") <= :P0", + ], + "variableMap": { + ":P0": 4, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having sumDistinct 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING SUM(DISTINCT "a") = :P0", + ], + "variableMap": { + ":P0": 9, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having with every other select clause 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" LEFT JOIN "u" ON x = :P0 WHERE "w" = :P1 GROUP BY "a" HAVING COUNT("a") > :P2 ORDER BY "a" ASC LIMIT :P3 OFFSET :P4", + ], + "variableMap": { + ":P0": "j", + ":P1": "w", + ":P2": "h", + ":P3": 1, + ":P4": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`having without a group by 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" HAVING COUNT("a") > :P0", + ], + "variableMap": { + ":P0": 2, + }, + "variableTypeHintMap": {}, +} +`; diff --git a/__tests__/__snapshots__/rds-conditions.test.js.snap b/__tests__/__snapshots__/rds-conditions.test.js.snap new file mode 100644 index 0000000..d6d28f5 --- /dev/null +++ b/__tests__/__snapshots__/rds-conditions.test.js.snap @@ -0,0 +1,397 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`group items with several columns an empty item in an and group 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE AND ("a" = :P0)", + ], + "variableMap": { + ":P0": 1, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`group items with several columns in an and group 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE ("a" = :P0 AND "b" > :P1)", + ], + "variableMap": { + ":P0": 1, + ":P1": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`group items with several columns in an or group 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE ("a" = :P0 AND "b" > :P1) OR ("c" = :P2)", + ], + "variableMap": { + ":P0": 1, + ":P1": 2, + ":P2": 3, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`logical key casing mixed case and 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE ("a" = :P0) AND ("b" = :P1)", + ], + "variableMap": { + ":P0": 1, + ":P1": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`logical key casing rejects a string item in an upper case or 1`] = ` +{ + "message": "Expected OR to be an Object.", +} +`; + +exports[`logical key casing rejects an upper case aggregate 1`] = ` +{ + "message": "Unsupported condition a.COUNT.", +} +`; + +exports[`logical key casing rejects an upper case not that is not an array 1`] = ` +{ + "message": "NOT expects conditions to be an array", +} +`; + +exports[`logical key casing rejects an upper case operator 1`] = ` +{ + "message": "Unsupported condition a.EQ.", +} +`; + +exports[`logical key casing rejects an upper case or that is not an array 1`] = ` +{ + "message": "OR expects conditions to be an array", +} +`; + +exports[`logical key casing upper case not 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE NOT (("a" = :P0))", + ], + "variableMap": { + ":P0": 1, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`logical key casing upper case or 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE ("a" = :P0) OR ("b" = :P1)", + ], + "variableMap": { + ":P0": 1, + ":P1": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not a not inside a not 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE NOT ((NOT (("a" = :P0))))", + ], + "variableMap": { + ":P0": 1, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not a single condition 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE NOT (("a" = :P0))", + ], + "variableMap": { + ":P0": 1, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not after an or group 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE ("a" = :P0) OR ("b" = :P1) AND NOT (("c" = :P2))", + ], + "variableMap": { + ":P0": 1, + ":P1": 2, + ":P2": 3, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not an empty item 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE NOT ()", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`not an empty item beside a condition 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE NOT ( AND ("a" = :P0))", + ], + "variableMap": { + ":P0": 1, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not an empty not 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE NOT ()", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`not an empty not beside a condition 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE NOT () AND "a" = :P0", + ], + "variableMap": { + ":P0": 1, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not an empty not in on 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" LEFT JOIN "u" ON NOT ()", + ], + "variableMap": {}, + "variableTypeHintMap": {}, +} +`; + +exports[`not an item with several columns 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE NOT (("a" = :P0 AND "b" > :P1))", + ], + "variableMap": { + ":P0": 1, + ":P1": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not an item with several conditions on one column 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE NOT (("a" > :P0 AND "a" < :P1))", + ], + "variableMap": { + ":P0": 1, + ":P1": 5, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not attributeExists, size and between 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE NOT (("a" IS NOT NULL) AND (LENGTH ("b") > :P0) AND ("c" BETWEEN :P1 AND :P2))", + ], + "variableMap": { + ":P0": 1, + ":P1": 1, + ":P2": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not between sibling conditions 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE "x" = :P0 AND NOT (("a" = :P1)) AND "y" = :P2", + ], + "variableMap": { + ":P0": "first", + ":P1": "second", + ":P2": "third", + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not holding an or group 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE NOT ((("a" = :P0) OR ("b" = :P1)))", + ], + "variableMap": { + ":P0": 1, + ":P1": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not in a remove in mysql 1`] = ` +{ + "statements": [ + "DELETE FROM \`t\` WHERE NOT ((\`b\` = :P0))", + ], + "variableMap": { + ":P0": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not in an update 1`] = ` +{ + "statements": [ + "UPDATE "t" SET "a" = :P0 WHERE NOT (("b" = :P1))", + ], + "variableMap": { + ":P0": 1, + ":P1": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not in having 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" GROUP BY "a" HAVING NOT ((COUNT("a") > :P0))", + ], + "variableMap": { + ":P0": 1, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not in on 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" LEFT JOIN "u" ON NOT (("a" = :P0))", + ], + "variableMap": { + ":P0": "b", + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not nested in an and group 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE (NOT (("a" = :P0) AND ("b" = :P1)))", + ], + "variableMap": { + ":P0": 1, + ":P1": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not nested in an or group 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE (NOT (("a" = :P0))) OR ("b" = :P1)", + ], + "variableMap": { + ":P0": 1, + ":P1": 2, + }, + "variableTypeHintMap": {}, +} +`; + +exports[`not rejects a null item 1`] = ` +{ + "message": "Expected not to be an Object.", +} +`; + +exports[`not rejects a number in on 1`] = ` +{ + "message": "ON expects a string value comparisons.", +} +`; + +exports[`not rejects a sql template item 1`] = ` +{ + "message": "Expected not to be an Object.", +} +`; + +exports[`not rejects a string item 1`] = ` +{ + "message": "Expected not to be an Object.", +} +`; + +exports[`not rejects an aggregate in where 1`] = ` +{ + "message": "Aggregates are not valid for WHERE conditions.", +} +`; + +exports[`not rejects an object 1`] = ` +{ + "message": "not expects conditions to be an array", +} +`; + +exports[`not rejects null 1`] = ` +{ + "message": "not expects conditions to be an array", +} +`; + +exports[`not several conditions 1`] = ` +{ + "statements": [ + "SELECT * FROM "t" WHERE NOT (("a" = :P0) AND ("b" > :P1))", + ], + "variableMap": { + ":P0": 1, + ":P1": 2, + }, + "variableTypeHintMap": {}, +} +`; diff --git a/__tests__/rds-aggregates.test.js b/__tests__/rds-aggregates.test.js new file mode 100644 index 0000000..a536762 --- /dev/null +++ b/__tests__/rds-aggregates.test.js @@ -0,0 +1,444 @@ +/* rds.agg aggregates, and the select clauses that use them: groupBy and having */ + +import { dirname, resolve } from "path"; +import { fileURLToPath, pathToFileURL } from "url"; +import { checkResolverValid } from "./helpers"; + +const pg = (expr) => ` + export function request(ctx) { + return rds.createPgStatement(${expr}); + } + export function response(ctx) {} +`; + +const mysql = (expr) => ` + export function request(ctx) { + return rds.createMySQLStatement(${expr}); + } + export function response(ctx) {} +`; + +const evaluate = (expr) => ` + export function request(ctx) { + return ${expr}; + } + export function response(ctx) {} +`; + +test("aggregate counts grouped by a column", async () => { + await checkResolverValid(pg(`rds.select({ + table: "orders", + columns: ["customer", { orders: rds.agg.count("*") }, { total: rds.agg.sum("price") }], + where: { status: { eq: "PAID" } }, + groupBy: ["customer"], + having: { price: { sum: { gt: 100 } } }, + orderBy: [{ column: "customer" }], + })`), {}, "request"); +}); + +test("agg imported by name", async () => { + const code = ` + import { agg, select, createPgStatement } from '@aws-appsync/utils/rds'; + export function request(ctx) { + return createPgStatement(select({ table: "t", columns: [agg.count("*")] })); + } + export function response(ctx) {} + `; + // a local run evaluates the code from a data URI, so the named import needs an absolute URL + let resolverCode = code; + if (process.env.TEST_TARGET !== "AWS_CLOUD") { + const rdsModule = pathToFileURL(resolve(dirname(fileURLToPath(import.meta.url)), "../rds/index.js")); + resolverCode = code.replace("'@aws-appsync/utils/rds'", `'${rdsModule}'`); + } + await checkResolverValid(resolverCode, {}, "request"); +}); + +describe("aggregate columns", () => { + test("every aggregate", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", columns: [ + rds.agg.max("a"), rds.agg.maxDistinct("a"), rds.agg.min("a"), rds.agg.minDistinct("a"), + rds.agg.count("a"), rds.agg.countDistinct("a"), rds.agg.avg("a"), rds.agg.avgDistinct("a"), + rds.agg.sum("a"), rds.agg.sumDistinct("a"), + ] })`), {}, "request"); + }); + + test("count of every row", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", columns: [rds.agg.count("*")] })`), {}, "request"); + }); + + test("a qualified column", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", columns: [rds.agg.sumDistinct("x.y")] })`), {}, "request"); + }); + + // only the bare star is special: a qualified one is quoted, even in a distinct aggregate + test("a qualified star in a distinct count", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", columns: [rds.agg.countDistinct("t.*")] })`), {}, "request"); + }); + + test("an aliased aggregate", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", columns: [{ m: rds.agg.max("a") }, "b"] })`), {}, "request"); + }); + + test("an aliased aggregate in mysql", async () => { + await checkResolverValid(mysql(`rds.select({ table: "t", columns: [{ m: rds.agg.avg("db.a") }] })`), {}, "request"); + }); + + test("an aggregate in a subquery", async () => { + await checkResolverValid(pg(`rds.select({ from: { s: rds.select({ table: "u", columns: [{ m: rds.agg.max("a") }], groupBy: ["b"] }) } })`), {}, "request"); + }); + + // a nullish column renders an empty argument list rather than being rejected + test("a null column", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", columns: [rds.agg.max(null)] })`), {}, "request"); + }); + + test("an undefined column", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", columns: [rds.agg.max(undefined)] })`), {}, "request"); + }); + + test("a null column in a distinct aggregate", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", columns: [rds.agg.countDistinct(null)] })`), {}, "request"); + }); + + test("an empty column", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", columns: [rds.agg.countDistinct("")] })`), {}, "request"); + }); + + // `using` shares the column list, aggregates included + test("an aggregate in using", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", leftJoin: [{ table: "u", using: [rds.agg.max("a")] }] })`), {}, "request"); + }); + + test("an aliased aggregate in using", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", leftJoin: [{ table: "u", using: [{ k: rds.agg.max("a") }] }] })`), {}, "request"); + }); + + // returning refuses a bare aggregate, but an aliased one gets through + test("an aliased aggregate in returning", async () => { + await checkResolverValid(pg(`rds.remove({ table: "t", returning: [{ m: rds.agg.max("a") }] })`), {}, "request"); + }); + + test("rejects an aggregate in returning", async () => { + await checkResolverValid(pg(`rds.insert({ table: "t", values: { a: 1 }, returning: [rds.agg.max("a")] })`), {}, "request"); + }); + + test("rejects an aggregate in returning in mysql", async () => { + await checkResolverValid(mysql(`rds.insert({ table: "t", values: { a: 1 }, returning: [rds.agg.max("a")] })`), {}, "request"); + }); +}); + +describe("aggregate arguments", () => { + // the argument is validated when the aggregate is created, whether or not it is ever rendered + test("rejects a missing argument", async () => { + await checkResolverValid(evaluate(`rds.agg.max()`), {}, "request"); + }); + + test("rejects a star in a distinct aggregate", async () => { + await checkResolverValid(evaluate(`rds.agg.countDistinct("*")`), {}, "request"); + }); + + test("rejects a star in an aggregate other than count", async () => { + await checkResolverValid(evaluate(`rds.agg.sum("*")`), {}, "request"); + }); + + test("rejects a star in a distinct aggregate other than count", async () => { + await checkResolverValid(evaluate(`rds.agg.maxDistinct("*")`), {}, "request"); + }); + + // AWS leaks the exception its own implementation raises for a non-string column + test("rejects an integer", async () => { + await checkResolverValid(evaluate(`rds.agg.max(-1)`), {}, "request"); + }); + + test("rejects an integer beyond 32 bits", async () => { + await checkResolverValid(evaluate(`rds.agg.max(3000000000)`), {}, "request"); + }); + + test("rejects a fractional number", async () => { + await checkResolverValid(evaluate(`rds.agg.max(1.5)`), {}, "request"); + }); + + test("rejects a boolean", async () => { + await checkResolverValid(evaluate(`rds.agg.max(true)`), {}, "request"); + }); + + test("rejects an array", async () => { + await checkResolverValid(evaluate(`rds.agg.max(["a"])`), {}, "request"); + }); + + test("rejects an object", async () => { + await checkResolverValid(evaluate(`rds.agg.max({ a: "b" })`), {}, "request"); + }); + + test("rejects an aggregate as a statement", async () => { + await checkResolverValid(pg(`rds.agg.max("a")`), {}, "request"); + }); + + test("rejects an aggregate as a from source", async () => { + await checkResolverValid(pg(`rds.select({ from: { s: rds.agg.max("a") } })`), {}, "request"); + }); + + test("rejects an aggregate as an inserted value", async () => { + await checkResolverValid(pg(`rds.insert({ table: "t", values: { a: rds.agg.max("a") } })`), {}, "request"); + }); + + test("rejects an aggregate as a compared value", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { a: { eq: rds.agg.max("a") } } })`), {}, "request"); + }); +}); + +describe("group by", () => { + test("columns", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", columns: ["a", { n: rds.agg.count("*") }], groupBy: ["a", "b.c"] })`), {}, "request"); + }); + + // integers are column positions and render unquoted + test("column positions", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: [1, "a", 0, -2, 2.0] })`), {}, "request"); + }); + + // unlike in a column list, a star is quoted + test("stars are quoted", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: ["*", "a.*"] })`), {}, "request"); + }); + + test("in mysql", async () => { + await checkResolverValid(mysql(`rds.select({ table: "t", groupBy: ["db.a", 2] })`), {}, "request"); + }); + + test("a null group by renders nothing", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: null })`), {}, "request"); + }); + + // only select knows the clause + test("remove ignores group by", async () => { + await checkResolverValid(pg(`rds.remove({ table: "t", groupBy: "x" })`), {}, "request"); + }); + + test("insert ignores group by and having", async () => { + await checkResolverValid(pg(`rds.insert({ table: "t", values: { a: 1 }, groupBy: "bad", having: "bad" })`), {}, "request"); + }); + + test("rejects a string", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: "a" })`), {}, "request"); + }); + + test("rejects an empty list", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: [] })`), {}, "request"); + }); + + test("rejects a fractional number", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: [1.5] })`), {}, "request"); + }); + + test("rejects an integer beyond 32 bits", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: [3000000000] })`), {}, "request"); + }); + + test("rejects NaN", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: [NaN] })`), {}, "request"); + }); + + test("rejects a boolean", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: [true] })`), {}, "request"); + }); + + test("rejects null", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: [null] })`), {}, "request"); + }); + + test("rejects an alias object", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: [{ a: "b" }] })`), {}, "request"); + }); + + test("rejects an aggregate", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: [rds.agg.max("a")] })`), {}, "request"); + }); + + // the clauses are validated in the order they render + test("where is validated before group by", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: "x", groupBy: [] })`), {}, "request"); + }); + + test("group by is validated before having", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: [], having: "x" })`), {}, "request"); + }); +}); + +describe("having", () => { + test("an aggregate condition", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", columns: ["a"], groupBy: ["a"], having: { a: { count: { gt: 2 } } } })`), {}, "request"); + }); + + test("max", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { max: { gt: 1, lt: 9 } } } })`), {}, "request"); + }); + + test("min", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { min: { lt: 2 } } } })`), {}, "request"); + }); + + test("avg", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { avg: { ge: 3 } } } })`), {}, "request"); + }); + + test("sum", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { sum: { le: 4 } } } })`), {}, "request"); + }); + + test("count", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { count: { eq: 5 } } } })`), {}, "request"); + }); + + test("maxDistinct", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { maxDistinct: { ne: 6 } } } })`), {}, "request"); + }); + + test("minDistinct", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { minDistinct: { eq: 7 } } } })`), {}, "request"); + }); + + test("avgDistinct", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { avgDistinct: { eq: 8 } } } })`), {}, "request"); + }); + + test("sumDistinct", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { sumDistinct: { eq: 9 } } } })`), {}, "request"); + }); + + test("countDistinct", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { countDistinct: { eq: 10 } } } })`), {}, "request"); + }); + + test("a count of every row", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { "*": { count: { gt: 1 } } } })`), {}, "request"); + }); + + test("a qualified column", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { "x.y": { sum: { gt: 1 } } } })`), {}, "request"); + }); + + test("a string value", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { max: { gt: "z" } } } })`), {}, "request"); + }); + + test("a type hinted value", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { sum: { gt: rds.typeHint.DECIMAL("1.5") } } } })`), {}, "request"); + }); + + // unlike a direct comparison, a nullish value is inlined + test("a null value", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: ["a"], having: { a: { count: { gt: null } } } })`), {}, "request"); + }); + + test("plain conditions alongside aggregate ones", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: ["a"], having: { a: { eq: "x" }, or: [{ b: { count: { gt: 1 } } }, { c: { size: { lt: 2 } } }] } })`), {}, "request"); + }); + + test("plain and aggregate conditions on one column", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { count: { gt: 1 }, eq: "x", max: { lt: 3 } } } })`), {}, "request"); + }); + + test("an and group", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { and: [{ a: { count: { gt: 1 } } }, { b: { sum: { lt: 2 } } }] } })`), {}, "request"); + }); + + test("a sql template", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: ["a"], having: rds.sql\`count(*) > \${2}\` })`), {}, "request"); + }); + + test("in mysql", async () => { + await checkResolverValid(mysql(`rds.select({ table: "t", groupBy: ["db.a"], having: { "db.a": { count: { gt: 1 } } } })`), {}, "request"); + }); + + // having renders after group by and before order by, and binds its variables in that order + test("with every other select clause", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { w: { eq: "w" } }, groupBy: ["a"], having: { a: { count: { gt: "h" } } }, orderBy: [{ column: "a" }], limit: 1, offset: 2, leftJoin: [{ table: "u", on: rds.sql\`x = \${"j"}\` }] })`), {}, "request"); + }); + + test("without a group by", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { count: { gt: 2 } } } })`), {}, "request"); + }); + + test("an empty condition renders nothing", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: ["a"], having: {} })`), {}, "request"); + }); + + test("an empty aggregate condition renders nothing", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: ["a"], having: { a: { count: {} } } })`), {}, "request"); + }); + + test("a null having renders nothing", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: ["a"], having: null })`), {}, "request"); + }); + + test("rejects a string", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: ["a"], having: "x" })`), {}, "request"); + }); + + test("rejects an array", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: ["a"], having: [] })`), {}, "request"); + }); + + // the typings allow between, but AWS refuses it + test("rejects between", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { count: { between: [1, 2] } } } })`), {}, "request"); + }); + + test("rejects a condition operator", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: ["a"], having: { a: { count: { contains: "x" } } } })`), {}, "request"); + }); + + test("rejects a value that is not an object", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: ["a"], having: { a: { count: 5 } } })`), {}, "request"); + }); + + test("rejects a null aggregate condition", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { count: null } } })`), {}, "request"); + }); + + test("rejects a distinct count of every row", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { "*": { countDistinct: { gt: 1 } } } })`), {}, "request"); + }); + + test("rejects a max of every row", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { "*": { max: { gt: 1 } } } })`), {}, "request"); + }); +}); + +describe("aggregate conditions outside having", () => { + test("rejects one in where", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { a: { count: { gt: 2 } } } })`), {}, "request"); + }); + + test("rejects one nested in where", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { or: [{ a: { count: { gt: 1 } } }] } })`), {}, "request"); + }); + + // the clause is checked before the condition's shape + test("rejects a malformed one in where", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { a: { count: "x" } } })`), {}, "request"); + }); + + // but after its nullness + test("rejects a null one in where", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { a: { count: null } } })`), {}, "request"); + }); + + test("rejects one in an update", async () => { + await checkResolverValid(pg(`rds.update({ table: "t", values: { a: 1 }, where: { b: { max: { gt: 1 } } } })`), {}, "request"); + }); + + test("rejects one in a remove", async () => { + await checkResolverValid(pg(`rds.remove({ table: "t", where: { a: { avg: { gt: 1 } } } })`), {}, "request"); + }); + + test("rejects one in on", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", leftJoin: [{ table: "u", on: { a: { count: { gt: "x" } } } }] })`), {}, "request"); + }); + + // ahead of on's own check that values are strings + test("rejects one with a number in on", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", leftJoin: [{ table: "u", on: { a: { count: { gt: 1 } } } }] })`), {}, "request"); + }); +}); diff --git a/__tests__/rds-conditions.test.js b/__tests__/rds-conditions.test.js new file mode 100644 index 0000000..b3626ff --- /dev/null +++ b/__tests__/rds-conditions.test.js @@ -0,0 +1,182 @@ +/* rds condition objects: not groups, the logical keys' casing and multi-column group items */ + +import { checkResolverValid } from "./helpers"; + +const pg = (expr) => ` + export function request(ctx) { + return rds.createPgStatement(${expr}); + } + export function response(ctx) {} +`; + +const mysql = (expr) => ` + export function request(ctx) { + return rds.createMySQLStatement(${expr}); + } + export function response(ctx) {} +`; + +describe("not", () => { + test("a single condition", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: [{ a: { eq: 1 } }] } })`), {}, "request"); + }); + + // the negated items are ANDed together + test("several conditions", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: [{ a: { eq: 1 } }, { b: { gt: 2 } }] } })`), {}, "request"); + }); + + test("an item with several columns", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: [{ a: { eq: 1 }, b: { gt: 2 } }] } })`), {}, "request"); + }); + + test("an item with several conditions on one column", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: [{ a: { gt: 1, lt: 5 } }] } })`), {}, "request"); + }); + + test("attributeExists, size and between", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: [{ a: { attributeExists: true } }, { b: { size: { gt: 1 } } }, { c: { between: [1, 2] } }] } })`), {}, "request"); + }); + + // variables are numbered in key order + test("between sibling conditions", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { x: { eq: "first" }, not: [{ a: { eq: "second" } }], y: { eq: "third" } } })`), {}, "request"); + }); + + test("after an or group", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { or: [{ a: { eq: 1 } }, { b: { eq: 2 } }], not: [{ c: { eq: 3 } }] } })`), {}, "request"); + }); + + test("nested in an or group", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { or: [{ not: [{ a: { eq: 1 } }] }, { b: { eq: 2 } }] } })`), {}, "request"); + }); + + test("nested in an and group", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { and: [{ not: [{ a: { eq: 1 } }, { b: { eq: 2 } }] }] } })`), {}, "request"); + }); + + test("holding an or group", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: [{ or: [{ a: { eq: 1 } }, { b: { eq: 2 } }] }] } })`), {}, "request"); + }); + + test("a not inside a not", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: [{ not: [{ a: { eq: 1 } }] }] } })`), {}, "request"); + }); + + // unlike an empty and/or, an empty not still renders, leaving SQL the engine rejects + test("an empty not", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: [] } })`), {}, "request"); + }); + + test("an empty not beside a condition", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: [], a: { eq: 1 } } })`), {}, "request"); + }); + + test("an empty item", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: [{}] } })`), {}, "request"); + }); + + test("an empty item beside a condition", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: [{}, { a: { eq: 1 } }] } })`), {}, "request"); + }); + + test("in an update", async () => { + await checkResolverValid(pg(`rds.update({ table: "t", values: { a: 1 }, where: { not: [{ b: { eq: 2 } }] } })`), {}, "request"); + }); + + test("in a remove in mysql", async () => { + await checkResolverValid(mysql(`rds.remove({ table: "t", where: { not: [{ b: { eq: 2 } }] } })`), {}, "request"); + }); + + test("in on", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", leftJoin: [{ table: "u", on: { not: [{ a: { eq: "b" } }] } }] })`), {}, "request"); + }); + + test("an empty not in on", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", leftJoin: [{ table: "u", on: { not: [] } }] })`), {}, "request"); + }); + + test("in having", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", groupBy: ["a"], having: { not: [{ a: { count: { gt: 1 } } }] } })`), {}, "request"); + }); + + // the clause rules reach into the group + test("rejects a number in on", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", leftJoin: [{ table: "u", on: { not: [{ a: { eq: 1 } }] } }] })`), {}, "request"); + }); + + test("rejects an aggregate in where", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: [{ a: { count: { gt: 1 } } }] } })`), {}, "request"); + }); + + test("rejects an object", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: { a: { eq: 1 } } } })`), {}, "request"); + }); + + test("rejects null", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: null } })`), {}, "request"); + }); + + test("rejects a string item", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: ["x"] } })`), {}, "request"); + }); + + test("rejects a null item", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: [null] } })`), {}, "request"); + }); + + test("rejects a sql template item", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { not: [rds.sql\`a = \${1}\`] } })`), {}, "request"); + }); +}); + +// AWS matches and/or/not in any casing, and names the key as written in its messages +describe("logical key casing", () => { + test("upper case not", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { NOT: [{ a: { eq: 1 } }] } })`), {}, "request"); + }); + + test("upper case or", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { OR: [{ a: { eq: 1 } }, { b: { eq: 2 } }] } })`), {}, "request"); + }); + + test("mixed case and", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { And: [{ a: { eq: 1 } }, { b: { eq: 2 } }] } })`), {}, "request"); + }); + + test("rejects an upper case not that is not an array", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { NOT: {} } })`), {}, "request"); + }); + + test("rejects an upper case or that is not an array", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { OR: {} } })`), {}, "request"); + }); + + test("rejects a string item in an upper case or", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { OR: ["x"] } })`), {}, "request"); + }); + + // the operators are not case-insensitive + test("rejects an upper case operator", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { a: { EQ: 1 } } })`), {}, "request"); + }); + + test("rejects an upper case aggregate", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", having: { a: { COUNT: { GT: 1 } } } })`), {}, "request"); + }); +}); + +// an item's columns are ANDed together and the item is grouped as a whole, whatever group it is in +describe("group items with several columns", () => { + test("in an and group", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { and: [{ a: { eq: 1 }, b: { gt: 2 } }] } })`), {}, "request"); + }); + + test("in an or group", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { or: [{ a: { eq: 1 }, b: { gt: 2 } }, { c: { eq: 3 } }] } })`), {}, "request"); + }); + + test("an empty item in an and group", async () => { + await checkResolverValid(pg(`rds.select({ table: "t", where: { and: [{}, { a: { eq: 1 } }] } })`), {}, "request"); + }); +}); diff --git a/package.json b/package.json index 4aee4c8..e24b71e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@localstack/appsync-utils", - "version": "0.1.10", + "version": "0.1.11", "description": "Implementation of the AppSync utils helpers", "type": "module", "main": "index.js", diff --git a/rds/index.js b/rds/index.js index 644e1ee..ccb7474 100644 --- a/rds/index.js +++ b/rds/index.js @@ -127,6 +127,74 @@ export function remove(s) { return { type: "REMOVE", properties: statementPayload(s, 'remove') }; } +// the `rds.agg` helpers, which double as the aggregate condition keys of a `having` clause +const AGGREGATE_FUNCTIONS = { + max: ['MAX', false], + maxDistinct: ['MAX', true], + min: ['MIN', false], + minDistinct: ['MIN', true], + count: ['COUNT', false], + countDistinct: ['COUNT', true], + avg: ['AVG', false], + avgDistinct: ['AVG', true], + sum: ['SUM', false], + sumDistinct: ['SUM', true], +}; + +// opaque to resolver code, like the value AWS returns from `rds.agg` +class Aggregate { + constructor(sqlFunction, distinct, column) { + this.sqlFunction = sqlFunction; + this.distinct = distinct; + this.column = column; + } +} + +/** + * Each helper validates its column when it is called, whether or not the aggregate is ever + * rendered. A nullish column is accepted and renders as an empty argument list, like on AWS. + */ +export const agg = Object.fromEntries(Object.entries(AGGREGATE_FUNCTIONS).map( + ([name, [sqlFunction, distinct]]) => [name, (...args) => { + if (args.length !== 1) { + throw codeError("A single argument is expected to be passed to aggregates."); + } + + const [column] = args; + if ((column != null) && (typeof column !== 'string')) { + // the exception AWS's own implementation leaks for a non-string column, reproduced verbatim + // with the Java class the value arrives as + let javaClass = 'java.util.LinkedHashMap'; + if (typeof column === 'boolean') { + javaClass = 'java.lang.Boolean'; + } else if (typeof column === 'number') { + if (!Number.isInteger(column)) { + javaClass = 'java.lang.Double'; + } else if ((column >= -(2 ** 31)) && (column < 2 ** 31)) { + javaClass = 'java.lang.Integer'; + } else { + javaClass = 'java.lang.Long'; + } + } else if (Array.isArray(column)) { + javaClass = 'java.util.ArrayList'; + } + throw codeError(`java.lang.ClassCastException: class ${javaClass} cannot be cast to class java.lang.String (${javaClass} and java.lang.String are in module java.base of loader 'bootstrap')`); + } + + if (column === '*') { + if (distinct) { + throw codeError("* is an invalid argument for distinct aggregates."); + } + + if (sqlFunction !== 'COUNT') { + throw codeError("* is an invalid argument for all aggregates, except COUNT"); + } + } + + return new Aggregate(sqlFunction, distinct, column); + }], +)); + // SQL operators for the simple comparison conditions, shared by the direct form (`{ eq: 1 }`) and // the `size` form (`{ size: { eq: 1 } }`) const COMPARISON_OPERATORS = { @@ -171,8 +239,9 @@ class StatementBuilder { }; this.variableIndex = 0; - // set while an `on` condition object renders, where AWS binds only strings and type hints - this.bindingOnCondition = false; + // the clause (WHERE, ON or HAVING) whose condition object is rendering: AWS accepts aggregate + // conditions in HAVING only, and binds only strings and type hints in ON + this.conditionClause = null; } render(statements) { @@ -246,7 +315,7 @@ class StatementBuilder { const { where, returning, } = properties; const parts = [`DELETE FROM ${this.resolveTableName(properties)}`]; - parts.push(...this.buildWhereParts(where)); + parts.push(...this.buildConditionParts('WHERE', where)); if (returning) { parts.push('RETURNING', this.renderReturning(returning)); @@ -286,7 +355,7 @@ class StatementBuilder { } parts.push(columnDefinitionItems.join(', ')); - parts.push(...this.buildWhereParts(where)); + parts.push(...this.buildConditionParts('WHERE', where)); this.result.statements.push(this.joinClauses(parts)); break; @@ -301,16 +370,44 @@ class StatementBuilder { * text rather than pushing it, and a subquery binds its variables before the outer statement's. */ renderSelect(properties) { - const { columns, where, orderBy, limit, offset } = properties; + const { columns, where, groupBy, having, orderBy, limit, offset } = properties; const parts = ["SELECT"]; // an absent column list means every column. Anything else is validated, so `columns: []` // still leaves a dangling `SELECT`, like AWS - parts.push(columns == null ? '*' : this.renderColumnList(columns)); + parts.push(columns == null ? '*' : this.renderColumnList(columns, true)); parts.push(`FROM ${this.resolveTableName(properties, true)}`); parts.push(...this.buildJoinParts(properties)); - parts.push(...this.buildWhereParts(where)); + parts.push(...this.buildConditionParts('WHERE', where)); + + if (groupBy != null) { + if (!Array.isArray(groupBy)) { + throw codeError("groupBy expects an array."); + } + + if (groupBy.length === 0) { + throw codeError("Column values are expected for GROUP BY."); + } + + const items = groupBy.map(item => { + // a 32-bit integer is a column position and renders as is + if (Number.isInteger(item) && (item >= -(2 ** 31)) && (item < 2 ** 31)) { + return String(item); + } + + if (typeof item !== 'string') { + throw codeError("Invalid type in GROUP BY column array."); + } + + // unlike in a column list, a star is quoted + return this.quoteSegments(item); + }); + parts.push('GROUP BY', items.join(', ')); + } + + // AWS renders HAVING whether or not there is a GROUP BY + parts.push(...this.buildConditionParts('HAVING', having)); if (orderBy != null) { if (!Array.isArray(orderBy)) { @@ -363,56 +460,48 @@ class StatementBuilder { throw codeError("'on' and 'using' keys cannot be used together"); } - if (isSqlTemplate(on)) { - parts.push('ON', this.renderTemplate(on.strings, on.keys)); - } else if (on != null) { - if ((typeof on !== 'object') || Array.isArray(on)) { - throw codeError("ON values are expected to be SQL templates or a condition object."); - } - - // the condition renders like a WHERE clause, its values bound as variables rather than - // read as column references, but AWS binds only strings and type hints here. An empty - // condition drops the ON keyword too. - this.bindingOnCondition = true; - try { - const condition = this.buildWhereClause(on); - if (condition) { - parts.push('ON', condition); - } - } finally { - this.bindingOnCondition = false; - } - } + // a condition object renders like a WHERE clause, its values bound as variables rather than + // read as column references + parts.push(...this.buildConditionParts('ON', on)); if (using != null) { // an empty list still renders, as `USING ()`, like AWS - parts.push(`USING (${this.renderColumnList(using)})`); + parts.push(`USING (${this.renderColumnList(using, true)})`); } } return parts; } - buildWhereParts(where) { - if (where == null) { + /** + * A WHERE, ON or HAVING clause, which AWS builds the same way: from a `sql` tagged template or a + * condition object. + */ + buildConditionParts(keyword, condition) { + if (condition == null) { return []; } // the whole clause may be a `sql` tagged template instead of a condition object, sharing the // statement's variable numbering - if (isSqlTemplate(where)) { - const rendered = this.renderTemplate(where.strings, where.keys); - return rendered ? ['WHERE', rendered] : []; + if (isSqlTemplate(condition)) { + const rendered = this.renderTemplate(condition.strings, condition.keys); + return rendered ? [keyword, rendered] : []; } - if ((typeof where !== 'object') || Array.isArray(where)) { - throw codeError("WHERE values are expected to be SQL templates or a condition object."); + if ((typeof condition !== 'object') || Array.isArray(condition)) { + throw codeError(`${keyword} values are expected to be SQL templates or a condition object.`); } - // a `where` that renders to nothing - `{}`, `{ and: [] }`, or a column with no condition - - // drops the WHERE keyword along with its body - const clause = this.buildWhereClause(where); - return clause ? ['WHERE', clause] : []; + // a condition that renders to nothing - `{}`, `{ and: [] }`, or a column with no condition - + // drops the keyword along with its body + this.conditionClause = keyword; + try { + const clause = this.buildWhereClause(condition); + return clause ? [keyword, clause] : []; + } finally { + this.conditionClause = null; + } } renderOrderByItem(item) { @@ -439,7 +528,7 @@ class StatementBuilder { renderReturning(returning) { // the column list is validated before the dialect is considered: a malformed `returning` on // MySQL is reported as malformed, not as unsupported - const columns = this.renderColumnList(returning); + const columns = this.renderColumnList(returning, false); // MySQL has no RETURNING clause and AWS refuses the key outright rather than emitting SQL the // engine would reject @@ -454,7 +543,7 @@ class StatementBuilder { * Validate and quote a list of column names. AWS applies the same rules, and raises the same * messages, for a select's `columns` and an insert or delete's `returning`. */ - renderColumnList(columns) { + renderColumnList(columns, allowAggregates) { // AWS accepts either the bare string `*` or an array of column names if (columns === '*') { return columns; @@ -469,6 +558,15 @@ class StatementBuilder { return this.quoteIdentifier(column); } + if (column instanceof Aggregate) { + // `returning` refuses a bare aggregate, though an aliased one gets through + if (!allowAggregates) { + throw codeError("Only columns in SELECT statements can include aggregates."); + } + + return this.renderAggregate(column); + } + if ((column == null) || (typeof column !== 'object') || Array.isArray(column)) { throw codeError("Invalid type in column array."); } @@ -476,15 +574,26 @@ class StatementBuilder { // a single-entry alias object, read like a table alias: `{ n: "p.name" }` becomes // `"p"."name" as "n"` const [alias, name] = this.aliasEntry(column, "column"); + const quotedAlias = this.quoteIdentifier(alias); + if (name instanceof Aggregate) { + return `${this.renderAggregate(name)} as ${quotedAlias}`; + } + if (typeof name !== 'string') { throw codeError("Column alias value is expected to be a string or rds.agg."); } // unlike a bare `*`, an aliased one is quoted - return `${this.quoteSegments(name)} as ${this.quoteIdentifier(alias)}`; + return `${this.quoteSegments(name)} as ${quotedAlias}`; }).join(', '); } + renderAggregate({ sqlFunction, distinct, column }) { + // a nullish column leaves the argument list empty, as `MAX()` or `COUNT(DISTINCT )` + const argument = column == null ? '' : this.quoteIdentifier(column); + return `${sqlFunction}(${distinct ? 'DISTINCT ' : ''}${argument})`; + } + resolveValues(properties, verb) { const { values } = properties; if (values == null) { @@ -523,7 +632,11 @@ class StatementBuilder { } newVariable(value, addTypeHint = true) { - if (this.bindingOnCondition && (typeof value !== 'string') && !value.type) { + if (value instanceof Aggregate) { + throw codeError("Only primitive values are supported as parameter."); + } + + if ((this.conditionClause === 'ON') && (typeof value !== 'string') && !value.type) { throw codeError("ON expects a string value comparisons."); } @@ -540,44 +653,56 @@ class StatementBuilder { return name; } - buildWhereClause(where, startGrouping = "", endGrouping = "", default_operator="AND") { - let blocks = []; + /** + * Render a condition object: its entries are ANDed together, each either a column's conditions + * or an `and`/`or`/`not` group, which AWS recognises in any casing. + */ + buildWhereClause(where) { + const blocks = []; for (const key in where) { - if ( ["or", "and"].includes(key)) { - const ops = key.toUpperCase(); - if (!Array.isArray(where[key])) { - throw codeError(`${key} expects conditions to be an array`); + const logicalOperator = key.toLowerCase(); + if (!["and", "or", "not"].includes(logicalOperator)) { + blocks.push(this.buildWhereStatement(key, where[key])); + continue; + } + + // the messages name the key as it was written + if (!Array.isArray(where[key])) { + throw codeError(`${key} expects conditions to be an array`); + } + + const items = where[key].map(item => { + // only a condition object is accepted here - unlike a top-level `where`, a nested `sql` + // template is refused + if ((item == null) || (typeof item !== 'object') || Array.isArray(item) || isSqlTemplate(item)) { + throw codeError(`Expected ${key} to be an Object.`); } - const parts = where[key].map(part => { - // only a condition object is accepted here - unlike a top-level `where`, a nested `sql` - // template is refused - if ((part == null) || (typeof part !== 'object') || Array.isArray(part) || isSqlTemplate(part)) { - throw codeError(`Expected ${key} to be an Object.`); - } - return this.buildWhereClause(part, "(", ")", ops); - }); - const group = parts.join(` ${ops} `); + // an item is grouped as a whole, so the columns of `{ a: ..., b: ... }` stay ANDed together + // inside an `or`. An item that renders nothing contributes an empty string, which still + // takes its place in the list, like on AWS. + const clause = this.buildWhereClause(item); + return clause === "" ? "" : `(${clause})`; + }); + + if (logicalOperator === "not") { + // the negated items are ANDed together, and unlike an empty `and`/`or`, an empty `not` + // still renders, as `NOT ()` + blocks.push(`NOT (${items.join(" AND ")})`); + } else { // an `and`/`or` holding no conditions contributes nothing at all: emitting the grouping on // its own would produce `WHERE ()` + const group = items.join(` ${logicalOperator.toUpperCase()} `); if (group !== "") { - blocks.push(`${startGrouping}${group}${endGrouping}`); + blocks.push(group); } - } else { - // implicit single clause - const block = {}; - block[key] = where[key]; - blocks.push(this.buildWhereStatement(block, startGrouping, endGrouping)); } } - return blocks.join(` ${default_operator} `); + return blocks.join(" AND "); } - buildWhereStatement(defn, startGrouping = "(", endGrouping = ")") { - const columnName = Object.keys(defn)[0]; - const condition = defn[columnName]; - + buildWhereStatement(columnName, condition) { if ((condition == null) || (typeof condition !== 'object') || Array.isArray(condition)) { throw codeError("Expected condition to be an Object."); } @@ -587,17 +712,39 @@ class StatementBuilder { conditionType => this.buildCondition(columnName, condition[conditionType], conditionType) ); - // a column that renders nothing - carrying no condition at all, e.g. an optional filter the - // resolver left empty, or only an empty `size` - contributes nothing, and must not pick up the - // grouping on its way out or it would produce `WHERE ()` - const joined = statements.join(" AND "); - return joined === "" ? "" : `${startGrouping}${joined}${endGrouping}`; + return statements.join(" AND "); } buildCondition(columnName, rawValue, conditionType) { const column = this.quoteIdentifier(columnName); const path = `${columnName}.${conditionType}`; + if (Object.hasOwn(AGGREGATE_FUNCTIONS, conditionType)) { + // nullness is checked first, then the clause, then the condition's shape + this.requireNonNull(rawValue, path); + if (this.conditionClause !== 'HAVING') { + throw codeError(`Aggregates are not valid for ${this.conditionClause} conditions.`); + } + + if ((typeof rawValue !== 'object') || Array.isArray(rawValue)) { + throw codeError(`Expected ${path} to be an Object.`); + } + + // built through `rds.agg` so a star gets the same validation, e.g. `{ "*": { count: ... } }` + // is `COUNT(*)` while `{ "*": { max: ... } }` is rejected + const target = this.renderAggregate(agg[conditionType](columnName)); + return Object.keys(rawValue).map(operator => { + // unlike `size`, an aggregate condition takes no `between` + const comparison = COMPARISON_OPERATORS[operator]; + if (!comparison) { + throw codeError(`${path} has invalid aggregate operator.`); + } + + // a nullish value is inlined, as in a `size` condition + return `${target} ${comparison} ${this.renderValue(rawValue[operator])}`; + }).join(" AND "); + } + switch (conditionType) { case "size": return this.buildSizeCondition(column, rawValue, path);