Skip to content

Commit cb7b0bd

Browse files
authored
Merge 'tests: add CTE regression tests for issue #4637' from Jussi Saurio
Add 10 regression tests covering previously failing CTE cases: - CTE referenced in WHERE IN subquery with real table data - Multiple CTEs cross-referenced via scalar subquery - CTE self-join via explicit JOIN ON - CTE used twice via comma-join with alias - CTE in scalar subquery alongside FROM reference - CTE in WHERE with DISTINCT and IN subquery - CTE referenced three times in cross-join - CTE with LEFT JOIN on self - CTE in EXISTS/NOT EXISTS subqueries All these cases were reported as broken in #4637 but have since been fixed by prior work. These tests prevent regressions. Closes #4637 Closes #5479
2 parents adb8204 + b076459 commit cb7b0bd

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

testing/runner/tests/cte.sqltest

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,3 +1247,103 @@ test cte-alias-scalar-subquery-multiple-ctes {
12471247
expect {
12481248
1|2
12491249
}
1250+
1251+
# =============================================================================
1252+
# Regression tests for issue #4637 — previously failing CTE cases
1253+
# =============================================================================
1254+
1255+
# CTE referenced in WHERE IN subquery with real table data
1256+
test cte-where-in-subquery {
1257+
CREATE TABLE orders(id INT, amount INT);
1258+
INSERT INTO orders VALUES(1,100),(2,200),(3,150);
1259+
WITH high_orders AS (SELECT * FROM orders WHERE amount > 100)
1260+
SELECT * FROM orders WHERE id IN (SELECT id FROM high_orders) ORDER BY id;
1261+
}
1262+
expect {
1263+
2|200
1264+
3|150
1265+
}
1266+
1267+
# Multiple CTEs cross-referenced via scalar subquery
1268+
test cte-cross-ref-scalar-subquery {
1269+
WITH t1 AS (SELECT 1 as x), t2 AS (SELECT 2 as x)
1270+
SELECT * FROM t1 WHERE x < (SELECT x FROM t2);
1271+
}
1272+
expect {
1273+
1
1274+
}
1275+
1276+
# CTE self-join via explicit JOIN ON
1277+
test cte-self-join {
1278+
WITH t AS (SELECT 1 as x UNION SELECT 2)
1279+
SELECT * FROM t JOIN t as t2 ON t.x = t2.x ORDER BY 1;
1280+
}
1281+
expect {
1282+
1|1
1283+
2|2
1284+
}
1285+
1286+
# CTE used twice via comma-join with alias
1287+
test cte-comma-join-alias {
1288+
WITH t AS (SELECT 1 as x) SELECT * FROM t, t as t2;
1289+
}
1290+
expect {
1291+
1|1
1292+
}
1293+
1294+
# CTE in scalar subquery alongside FROM reference
1295+
test cte-scalar-and-from-ref {
1296+
WITH t AS (SELECT 1 as x) SELECT t.x, (SELECT MAX(x) FROM t) as mx FROM t;
1297+
}
1298+
expect {
1299+
1|1
1300+
}
1301+
1302+
# CTE in WHERE with DISTINCT and IN subquery
1303+
test cte-where-distinct-in {
1304+
CREATE TABLE items(id INT, cat TEXT);
1305+
INSERT INTO items VALUES(1,'a'),(2,'b'),(3,'a');
1306+
WITH cats AS (SELECT DISTINCT cat FROM items WHERE cat = 'a')
1307+
SELECT * FROM items WHERE cat IN (SELECT cat FROM cats) ORDER BY id;
1308+
}
1309+
expect {
1310+
1|a
1311+
3|a
1312+
}
1313+
1314+
# CTE referenced three times in cross-join
1315+
test cte-triple-ref {
1316+
WITH t AS (SELECT 1 as x UNION SELECT 2 UNION SELECT 3)
1317+
SELECT a.x, b.x, c.x FROM t a, t b, t c WHERE a.x = 1 AND b.x = 2 AND c.x = 3;
1318+
}
1319+
expect {
1320+
1|2|3
1321+
}
1322+
1323+
# CTE with LEFT JOIN on self
1324+
test cte-left-join-self {
1325+
WITH t AS (SELECT 1 as id, 'root' as name UNION SELECT 2, 'child')
1326+
SELECT a.name, b.name FROM t a LEFT JOIN t b ON a.id <> b.id ORDER BY 1, 2;
1327+
}
1328+
expect {
1329+
child|root
1330+
root|child
1331+
}
1332+
1333+
# CTE in EXISTS subquery
1334+
test cte-exists-self-ref {
1335+
WITH t AS (SELECT 1 as x) SELECT * FROM t WHERE EXISTS (SELECT 1 FROM t WHERE x = 1);
1336+
}
1337+
expect {
1338+
1
1339+
}
1340+
1341+
# CTE in NOT EXISTS subquery
1342+
test cte-not-exists-self-ref {
1343+
WITH t AS (SELECT 1 as x UNION SELECT 2)
1344+
SELECT * FROM t WHERE NOT EXISTS (SELECT 1 FROM t WHERE x = 3) ORDER BY 1;
1345+
}
1346+
expect {
1347+
1
1348+
2
1349+
}

0 commit comments

Comments
 (0)