3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing the WITH clause.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set ::testprefix with3
24 # Test problems found by Kostya Serebryany using
25 # LibFuzzer. (http://llvm.org/docs/LibFuzzer.html)
27 do_catchsql_test 1.0 {
30 SELECT 5 FROM t0 UNION SELECT 8 FROM m
33 } {1 {no such table: m}}
35 # 2019-11-09 dbfuzzcheck find
36 do_catchsql_test 1.1 {
37 CREATE VIEW v1(x,y) AS
38 WITH t1(a,b) AS (VALUES(1,2))
39 SELECT * FROM nosuchtable JOIN t1;
41 } {1 {no such table: main.nosuchtable}}
43 # Additional test cases that came out of the work to
44 # fix for Kostya's problem.
51 SELECT * FROM x1 UNION ALL SELECT * FROM x2
66 x1(a) AS (values(100))
68 SELECT * FROM (WITH x2(y) AS (SELECT * FROM x1) SELECT y+a FROM x1, x2);
72 #-------------------------------------------------------------------------
73 # Test that the planner notices LIMIT clauses on recursive WITH queries.
77 do_execsql_test 3.1.1 {
78 CREATE TABLE y1(a, b);
79 CREATE INDEX y1a ON y1(a);
81 WITH cnt(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM cnt LIMIT 1000)
82 INSERT INTO y1 SELECT i%10, i FROM cnt;
88 WITH cnt(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM cnt LIMIT 1)
89 SELECT * FROM cnt, y1 WHERE i=a
90 } [string map {"\n " \n} {
94 | | `--SCAN CONSTANT ROW
98 `--SEARCH y1 USING INDEX y1a (a=?)
102 WITH cnt(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM cnt LIMIT 1000000)
103 SELECT * FROM cnt, y1 WHERE i=a
104 } [string map {"\n " \n} {
108 | | `--SCAN CONSTANT ROW
112 |--BLOOM FILTER ON cnt (i=?)
113 `--SEARCH cnt USING AUTOMATIC COVERING INDEX (i=?)
117 do_execsql_test 3.2.1 {
118 CREATE TABLE w1(pk INTEGER PRIMARY KEY, x INTEGER);
119 CREATE TABLE w2(pk INTEGER PRIMARY KEY);
123 WITH RECURSIVE c(w,id) AS (SELECT 0, (SELECT pk FROM w2 LIMIT 1)
124 UNION ALL SELECT c.w + 1, x FROM w1, c LIMIT 1)
125 SELECT * FROM c, w2, w1
126 WHERE c.id=w2.pk AND c.id=w1.pk;
131 | | |--SCAN CONSTANT ROW
132 | | `--SCALAR SUBQUERY xxxxxx
138 |--SEARCH w2 USING INTEGER PRIMARY KEY (rowid=?)
139 `--SEARCH w1 USING INTEGER PRIMARY KEY (rowid=?)
142 do_execsql_test 4.0 {
147 WITH t1 AS (SELECT 1 AS c1 GROUP BY 1)
148 SELECT a.c1 FROM t1 AS a, t1 AS b
151 SELECT (SELECT 1 FROM t2) FROM t2
153 SELECT t3col1 FROM t3 WHERE t3col1
154 ) FROM (SELECT 1 AS anoncol1)
156 SELECT t5col1, t5col1 FROM t5
158 do_execsql_test 4.1 {
160 WITH RECURSIVE Table0 AS (
161 WITH RECURSIVE Table0(Col0) AS (SELECT ALL 1 )
163 WITH RECURSIVE Table0 AS (
164 WITH RECURSIVE Table0 AS (
165 WITH RECURSIVE Table0 AS (SELECT DISTINCT 1 GROUP BY 1 )
166 SELECT DISTINCT * FROM Table0 NATURAL INNER JOIN Table0
169 SELECT ALL (SELECT DISTINCT * FROM Table0) FROM Table0 WHERE Col0 = 1
171 SELECT ALL * FROM Table0 NATURAL INNER JOIN Table0
173 SELECT DISTINCT * FROM Table0 NATURAL INNER JOIN Table0
177 # 2020-01-18 chrome ticket 1043236
178 # Correct handling of the sequence:
184 do_execsql_test 4.2 {
186 WITH t1(a) AS (VALUES(1))
190 WITH t4(d) AS (VALUES('elvis'))
191 SELECT t4a.d FROM t4 AS t4a JOIN t4 AS t4b LEFT JOIN t4 AS t4c
193 SELECT c FROM t3 WHERE a = 1
195 SELECT t2a.b FROM t2 AS t2a JOIN t2 AS t2x
203 # Avoid manifesting the same CTE multiple times.
206 WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<1)
207 SELECT x1.x||x2.x||x3.x||x4.x FROM c AS x1, c AS x2, c AS x3, c AS x4
213 | | `--SCAN CONSTANT ROW
220 `--USE TEMP B-TREE FOR ORDER BY
222 do_execsql_test 5.2 {
223 WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<1)
224 SELECT x1.x||x2.x||x3.x||x4.x FROM c AS x1, c AS x2, c AS x3, c AS x4
226 } {0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111}
228 #-------------------------------------------------------------------------
229 # At one point this would incorrectly report "circular reference: cte1"
231 do_catchsql_test 6.0 {
233 cte1(x, y) AS ( select 1, 2, 3 ),
234 cte2(z) as ( select 1 from cte1 )
235 select * from cte2, cte1;
236 } {1 {table cte1 has 3 values for 2 columns}}
238 do_catchsql_test 6.1 {
240 cte1(x, y) AS ( select 1, 2, 3 ),
241 cte2(z) as ( select 1 from cte1 UNION ALL SELECT z+1 FROM cte2 WHERE z<5)
242 select * from cte2, cte1;
243 } {1 {table cte1 has 3 values for 2 columns}}