From 616d9b29ff105df867d95c85d6dc10e1a95afe23 Mon Sep 17 00:00:00 2001 From: Harshit Gangal Date: Thu, 6 Jun 2024 16:07:33 +0530 Subject: [PATCH] added more tests Signed-off-by: Harshit Gangal --- t/info_schema.test | 1 + t/subqueries.test | 458 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 459 insertions(+) create mode 100644 t/info_schema.test create mode 100644 t/subqueries.test diff --git a/t/info_schema.test b/t/info_schema.test new file mode 100644 index 0000000..12df60d --- /dev/null +++ b/t/info_schema.test @@ -0,0 +1 @@ +describe information_schema.administrable_role_authorizations; \ No newline at end of file diff --git a/t/subqueries.test b/t/subqueries.test new file mode 100644 index 0000000..a0182a4 --- /dev/null +++ b/t/subqueries.test @@ -0,0 +1,458 @@ +CREATE TABLE user ( + id INT PRIMARY KEY, + name VARCHAR(100) +); + +CREATE TABLE user_extra ( + user_id INT, + extra_info VARCHAR(100), + PRIMARY KEY (user_id, extra_info) +); + +# Inserts for user table +INSERT INTO user (id, name) VALUES +(1, 'Alice'), +(2, 'Bob'), +(3, 'Charlie'), +(4, 'David'), +(5, 'Eve'), +(6, 'Frank'), +(7, 'Grace'), +(8, 'Hannah'), +(9, 'Ivy'), +(10, 'Jack'); + +# Inserts for user_extra table +INSERT INTO user_extra (user_id, extra_info) VALUES +(1, 'info1'), +(1, 'info2'), +(2, 'info1'), +(3, 'info1'), +(3, 'info2'), +(4, 'info1'), +(5, 'info1'), +(6, 'info1'), +(7, 'info1'), +(8, 'info1'); + +# uncorrelated subqueries +# subquery in SELECT and WHERE +SELECT + (SELECT COUNT(*) FROM user_extra) AS order_count, + id +FROM + user +WHERE + id = (SELECT COUNT(*) FROM user_extra) + +# subquery in SELECT and GROUP BY +--skip not supported +SELECT + id, + (SELECT COUNT(*) FROM user_extra) AS order_count +FROM + user +GROUP BY + id, + (SELECT COUNT(*) FROM user_extra); + +# subquery in SELECT and ORDER BY +SELECT + id, + (SELECT COUNT(*) FROM user_extra) AS order_count +FROM + user +ORDER BY + (SELECT COUNT(*) FROM user_extra); + +# subquery in WHERE and ORDER BY +SELECT + id +FROM + user +WHERE + id = (SELECT COUNT(*) FROM user_extra) +ORDER BY + (SELECT COUNT(*) FROM user_extra); + +# subquery in WHERE and GROUP BY +--skip not supported +SELECT + id +FROM + user +WHERE + id = (SELECT COUNT(*) FROM user_extra) +GROUP BY + id, + (SELECT COUNT(*) FROM user_extra); + +# subquery in GROUP BY and ORDER BY +--skip not supported +SELECT + id +FROM + user +GROUP BY + id, + (SELECT COUNT(*) FROM user_extra) +ORDER BY + (SELECT COUNT(*) FROM user_extra); + +# subquery in HAVING and ORDER BY +--skip not supported +SELECT + id, + COUNT(*) +FROM + user +GROUP BY + id +HAVING + COUNT(*) > (SELECT COUNT(*) FROM user_extra) +ORDER BY + (SELECT COUNT(*) FROM user_extra); + +# subquery in HAVING and SELECT +SELECT + id, + (SELECT COUNT(*) FROM user_extra) AS order_count +FROM + user +GROUP BY + id +HAVING + COUNT(*) > (SELECT COUNT(*) FROM user_extra); + +# subquery in WHERE and HAVING +-- skip not supported +SELECT + id, + COUNT(*) +FROM + user +WHERE + id IN (SELECT id FROM user_extra) +GROUP BY + id +HAVING + COUNT(*) > (SELECT COUNT(*) FROM user_extra); + +# correlated subqueries that can be merged +# correlated subquery in SELECT and WHERE +SELECT + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id) AS extra_count, + id, + name +FROM + user +WHERE + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id) > 0; + +# correlated subquery in SELECT and GROUP BY +--skip not supported +SELECT + id, + name, + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id) AS extra_count +FROM + user +GROUP BY + id, + name, + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id); + +# correlated subquery in SELECT and ORDER BY +SELECT + id, + name, + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id) AS extra_count +FROM + user +ORDER BY + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id); + +# correlated subquery in WHERE and ORDER BY +SELECT + id, + name +FROM + user +WHERE + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id) > 0 +ORDER BY + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id); + +# correlated subquery in WHERE and GROUP BY +--skip not supported +SELECT + id, + name +FROM + user +WHERE + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id) > 0 +GROUP BY + id, + name, + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id); + +# correlated subquery in GROUP BY and ORDER BY +--skip not supported +SELECT + id, + name +FROM + user +GROUP BY + id, + name, + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id) +ORDER BY + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id); + +# correlated subquery in HAVING and ORDER BY +--skip not supported +SELECT + id, + name, + COUNT(*) +FROM + user +GROUP BY + id, + name +HAVING + COUNT(*) > (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id) +ORDER BY + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id); + +# correlated subquery in HAVING and SELECT +SELECT + id, + name, + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id) AS extra_count +FROM + user +GROUP BY + id, + name +HAVING + COUNT(*) > (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id); + +# correlated subquery in WHERE and HAVING +SELECT + id, + name, + COUNT(*) +FROM + user +WHERE + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id) > 0 +GROUP BY + id, + name +HAVING + COUNT(*) > (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id); + +# correlated subqueries that can be merged +SELECT + id, + round(MAX(id + (SELECT COUNT(*) FROM user_extra where user_id = 42))) as r +FROM user +WHERE id = 42 +GROUP BY id +ORDER BY r; + +# Subquery in SELECT with Complex Expression +SELECT + id, + name, + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id) * 2 AS double_extra_count +FROM + user; + +# Subquery in WHERE with Complex Expression +SELECT + id, + name +FROM + user +WHERE + id IN (SELECT user_id FROM user_extra WHERE LENGTH(extra_info) > 4); + +# Subquery in HAVING with Complex Expression +SELECT + id, + COUNT(*) +FROM + user +GROUP BY + id +HAVING + COUNT(*) > (SELECT COUNT(*) FROM user_extra WHERE user_extra.user_id = user.id) + 1; + +# Subquery in ORDER BY with Complex Expression +SELECT + id, + name +FROM + user +ORDER BY + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id) * id; + +# Correlated Subquery in SELECT with Arithmetic Expression +SELECT + id, + name, + (SELECT COUNT(*) FROM user_extra WHERE user.id = user_extra.user_id) + id AS extra_count_plus_id +FROM + user; + +# Subquery in WHERE with Logical Expression +SELECT + id, + name +FROM + user +WHERE + id IN (SELECT user_id FROM user_extra WHERE extra_info = 'info1') + OR id IN (SELECT user_id FROM user_extra WHERE extra_info = 'info2'); + +# Subquery with COUNT aggregation in SELECT, used in ORDER BY +SELECT + id, + name, + (SELECT COUNT(*) FROM user_extra) AS total_extra_count, + SUM(id) AS sum_ids +FROM + user +GROUP BY + id, name +ORDER BY + (SELECT COUNT(*) FROM user_extra); + +# Subquery with SUM aggregation in SELECT, used in HAVING +SELECT + id, + name, + (SELECT SUM(LENGTH(extra_info)) FROM user_extra) AS total_length_extra_info, + AVG(id) AS avg_ids +FROM + user +GROUP BY + id, name +HAVING + (SELECT SUM(LENGTH(extra_info)) FROM user_extra) > 10; + +# Subquery with AVG aggregation in SELECT, used in WHERE +SELECT + id, + name, + (SELECT AVG(LENGTH(extra_info)) FROM user_extra) AS avg_length_extra_info, + MAX(id) AS max_id +FROM + user +WHERE + id IN (SELECT user_id FROM user_extra) +GROUP BY + id, name; + +# Subquery with MAX aggregation in SELECT, used in ORDER BY +SELECT + id, + name, + (SELECT MAX(LENGTH(extra_info)) FROM user_extra) AS max_length_extra_info, + MIN(id) AS min_id +FROM + user +GROUP BY + id, name +ORDER BY + (SELECT MAX(LENGTH(extra_info)) FROM user_extra); + +# Subquery with MIN aggregation in SELECT, used in HAVING +SELECT + id, + name, + (SELECT MIN(LENGTH(extra_info)) FROM user_extra) AS min_length_extra_info, + SUM(id) AS sum_ids +FROM + user +GROUP BY + id, name +HAVING + (SELECT MIN(LENGTH(extra_info)) FROM user_extra) < 5; + +# Subquery with COUNT aggregation in SELECT, used in WHERE +SELECT + id, + name, + (SELECT COUNT(*) FROM user_extra) AS total_extra_count, + AVG(id) AS avg_ids +FROM + user +WHERE + id > (SELECT COUNT(*) FROM user_extra) +GROUP BY + id, name; + +# Subquery with SUM aggregation in SELECT, used in ORDER BY +SELECT + id, + name, + (SELECT SUM(LENGTH(extra_info)) FROM user_extra) AS total_length_extra_info, + COUNT(id) AS count_ids +FROM + user +GROUP BY + id, name +ORDER BY + (SELECT SUM(LENGTH(extra_info)) FROM user_extra); + +# Subquery with Multiple Aggregations in SELECT, used in HAVING +SELECT + id, + name, + (SELECT COUNT(*) FROM user_extra) AS total_extra_count, + (SELECT SUM(LENGTH(extra_info)) FROM user_extra) AS total_length_extra_info, + (SELECT AVG(LENGTH(extra_info)) FROM user_extra) AS avg_length_extra_info, + (SELECT MAX(LENGTH(extra_info)) FROM user_extra) AS max_length_extra_info, + (SELECT MIN(LENGTH(extra_info)) FROM user_extra) AS min_length_extra_info, + SUM(id) AS sum_ids +FROM + user +GROUP BY + id, name +HAVING + (SELECT AVG(LENGTH(extra_info)) FROM user_extra) > 2; + +# Subquery in SELECT with Aggregation, used in WHERE +SELECT + id, + name, + (SELECT COUNT(*) FROM user_extra) + id AS total_extra_count_plus_id, + AVG(id) AS avg_ids +FROM + user +WHERE + id < (SELECT MAX(user_id) FROM user_extra) +GROUP BY + id, name; + +# Complex Subquery with Aggregation in SELECT, used in ORDER BY +SELECT + id, + name, + (SELECT COUNT(*) FROM user_extra WHERE user_extra.user_id = user.id) AS extra_count, + (SELECT SUM(LENGTH(extra_info)) FROM user_extra WHERE user_extra.user_id = user.id) AS total_length_extra_info, + SUM(id) AS sum_ids, + AVG(id) AS avg_ids, + MAX(id) AS max_id, + MIN(id) AS min_id +FROM + user +GROUP BY + id, name +ORDER BY + (SELECT SUM(LENGTH(extra_info)) FROM user_extra WHERE user_extra.user_id = user.id); \ No newline at end of file