Skip to content

Commit

Permalink
add complext test queries
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zhengda committed Nov 7, 2023
1 parent 30aed55 commit d1090ea
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"expected": "DELETE FROM users u USING orders o, order_items oi, products p WHERE u.id = o.user_id AND o.id = oi.order_id AND oi.product_id = p.id AND p.category = ? AND o.order_date < NOW ( ) - INTERVAL ?",
"statement_metadata": {
"size": 11,
"tables": [
"users"
],
"commands": [
"DELETE"
],
"comments": [],
"procedures": []
}
}
]
12 changes: 12 additions & 0 deletions testdata/postgresql/complex/delete-complex-subqueries-joins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DELETE FROM
users u
USING
orders o,
order_items oi,
products p
WHERE
u.id = o.user_id
AND o.id = oi.order_id
AND oi.product_id = p.id
AND p.category = 'obsolete'
AND o.order_date < NOW() - INTERVAL '5 years';
21 changes: 21 additions & 0 deletions testdata/postgresql/complex/insert-complex-select-joins.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"expected": "INSERT INTO order_summaries ( order_id, product_count, total_amount, average_product_price ) SELECT o.id, COUNT ( p.id ), SUM ( oi.amount ), AVG ( p.price ) FROM orders o JOIN order_items oi ON o.id = oi.order_id JOIN products p ON oi.product_id = p.id GROUP BY o.id HAVING SUM ( oi.amount ) > ?",
"statement_metadata": {
"size": 56,
"tables": [
"order_summaries",
"orders",
"order_items",
"products"
],
"commands": [
"INSERT",
"SELECT",
"JOIN"
],
"comments": [],
"procedures": []
}
}
]
14 changes: 14 additions & 0 deletions testdata/postgresql/complex/insert-complex-select-joins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
INSERT INTO order_summaries (order_id, product_count, total_amount, average_product_price)
SELECT
o.id,
COUNT(p.id),
SUM(oi.amount),
AVG(p.price)
FROM
orders o
JOIN order_items oi ON o.id = oi.order_id
JOIN products p ON oi.product_id = p.id
GROUP BY
o.id
HAVING
SUM(oi.amount) > 1000;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"expected": "SELECT u.id, u.name, ( SELECT COUNT ( * ) FROM orders o WHERE o.user_id = u.id ), ( SELECT SUM ( amount ) FROM payments p WHERE p.user_id = u.id ), ( SELECT AVG ( rating ) FROM reviews r WHERE r.user_id = u.id ) FROM users u WHERE EXISTS ( SELECT ? FROM logins l WHERE l.user_id = u.id AND l.time > NOW ( ) - INTERVAL ? ) AND u.status = ? ORDER BY total_payments DESC, average_rating DESC, order_count DESC LIMIT ?",
"statement_metadata": {
"size": 38,
"tables": [
"orders",
"payments",
"reviews",
"users",
"logins"
],
"commands": [
"SELECT"
],
"comments": [],
"procedures": []
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
SELECT
u.id,
u.name,
(SELECT COUNT(*) FROM orders o WHERE o.user_id = u.id) AS order_count,
(SELECT SUM(amount) FROM payments p WHERE p.user_id = u.id) AS total_payments,
(SELECT AVG(rating) FROM reviews r WHERE r.user_id = u.id) AS average_rating
FROM
users u
WHERE
EXISTS (
SELECT 1 FROM logins l WHERE l.user_id = u.id AND l.time > NOW() - INTERVAL '1 month'
)
AND u.status = 'active'
ORDER BY
total_payments DESC, average_rating DESC, order_count DESC
LIMIT 10;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"expected": "SELECT e?.name, e?.salary, e?.name, AVG ( e?.salary ) OVER ( PARTITION BY e?.manager_id ), RANK ( ) OVER ( ORDER BY e?.salary DESC ) FROM employees e? LEFT JOIN employees e? ON e?.manager_id = e?.id WHERE e?.department_id IN ( SELECT id FROM departments WHERE name LIKE ? ) AND e?.hire_date > ? ORDER BY salary_rank, avg_manager_salary DESC",
"statement_metadata": {
"size": 30,
"tables": [
"employees",
"departments"
],
"commands": [
"SELECT",
"JOIN"
],
"comments": [],
"procedures": []
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SELECT
e1.name AS employee_name,
e1.salary,
e2.name AS manager_name,
AVG(e2.salary) OVER (PARTITION BY e1.manager_id) AS avg_manager_salary,
RANK() OVER (ORDER BY e1.salary DESC) AS salary_rank
FROM
employees e1
LEFT JOIN employees e2 ON e1.manager_id = e2.id
WHERE
e1.department_id IN (SELECT id FROM departments WHERE name LIKE 'IT%')
AND
e1.hire_date > '2020-01-01'
ORDER BY
salary_rank, avg_manager_salary DESC;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"expected": "SELECT user_id, order_id, order_total, user_total FROM ( SELECT o.user_id, o.id, o.total, ( SELECT SUM ( total ) FROM orders WHERE user_id = o.user_id ), RANK ( ) OVER ( PARTITION BY o.user_id ORDER BY o.total DESC ) FROM orders o ) sub WHERE sub.rnk = ? AND user_total > ( SELECT AVG ( total ) * ? FROM orders )",
"statement_metadata": {
"size": 12,
"tables": [
"orders"
],
"commands": [
"SELECT"
],
"comments": [],
"procedures": []
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
SELECT
user_id,
order_id,
order_total,
user_total
FROM (
SELECT
o.user_id,
o.id AS order_id,
o.total AS order_total,
(SELECT SUM(total) FROM orders WHERE user_id = o.user_id) AS user_total,
RANK() OVER (PARTITION BY o.user_id ORDER BY o.total DESC) AS rnk
FROM
orders o
) sub
WHERE
sub.rnk = 1
AND user_total > (
SELECT
AVG(total) * 2
FROM orders
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"expected": "UPDATE products p SET price = CASE WHEN p.stock < ? THEN p.price * ? WHEN p.stock BETWEEN ? AND ? THEN p.price ELSE p.price * ? END, last_updated = NOW ( ) FROM ( SELECT product_id, SUM ( quantity ) FROM inventory GROUP BY product_id ) WHERE sub.product_id = p.id",
"statement_metadata": {
"size": 29,
"tables": [
"products",
"inventory"
],
"commands": [
"UPDATE",
"SELECT"
],
"comments": [],
"procedures": []
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
UPDATE
products p
SET
price = CASE
WHEN p.stock < 10 THEN p.price * 1.10
WHEN p.stock BETWEEN 10 AND 50 THEN p.price
ELSE p.price * 0.90
END,
last_updated = NOW()
FROM (
SELECT
product_id,
SUM(quantity) AS stock
FROM
inventory
GROUP BY
product_id
) AS sub
WHERE
sub.product_id = p.id;

0 comments on commit d1090ea

Please sign in to comment.