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 flattening UNION ALL sub-queries.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set testprefix unionall
20 CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT);
21 CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT);
22 CREATE TABLE t1_c(e INTEGER PRIMARY KEY, f TEXT);
24 INSERT INTO t1_a VALUES(1, 'one'), (4, 'four');
25 INSERT INTO t1_b VALUES(2, 'two'), (5, 'five');
26 INSERT INTO t1_c VALUES(3, 'three'), (6, 'six');
29 SELECT a, b FROM t1_a UNION ALL
30 SELECT c, d FROM t1_b UNION ALL
31 SELECT e, f FROM t1_c;
34 INSERT INTO i1 VALUES(2), (5), (6), (1);
39 SELECT a, b FROM t1_a UNION ALL
40 SELECT c, d FROM t1_b UNION ALL
44 1 one 2 two 3 three 4 four 5 five 6 six
48 SELECT a, b FROM t1 ORDER BY a
50 1 one 2 two 3 three 4 four 5 five 6 six
54 SELECT a, b FROM i1, t1 WHERE a=x ORDER BY a
55 } {1 one 2 two 5 five 6 six}
58 # 2022-10-31 part of ticket 57c47526c34f01e8
59 # The queries below were causing an assertion fault in
60 # the comparison operators of the VDBE.
63 database_never_corrupt
64 optimization_control db all 0
65 do_execsql_test 1.10 {
66 CREATE TABLE t0(c0 INT);
67 INSERT INTO t0 VALUES(0);
68 CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT);
69 INSERT INTO t1_a VALUES(1,'one');
70 CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT);
71 INSERT INTO t1_b VALUES(2,'two');
72 CREATE VIEW t1 AS SELECT a, b FROM t1_a UNION ALL SELECT c, c FROM t1_b;
73 SELECT * FROM (SELECT t1.a, t1.b AS b, t0.c0 FROM t0, t1);
75 do_execsql_test 1.11 {
76 SELECT * FROM (SELECT t1.a, t1.b AS b, t0.c0 FROM t0, t1) WHERE b=2;
79 #-------------------------------------------------------------------------
82 do_execsql_test 2.1.0 {
83 CREATE TABLE t1(x, y);
84 INSERT INTO t1 VALUES(1, 'one');
85 INSERT INTO t1 VALUES(1, 'ONE');
86 INSERT INTO t1 VALUES(2, 'two');
87 INSERT INTO t1 VALUES(2, 'TWO');
88 INSERT INTO t1 VALUES(3, 'three');
89 INSERT INTO t1 VALUES(3, 'THREE');
92 do_execsql_test 2.1.1 {
94 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<3
97 SELECT 0 AS i UNION ALL SELECT i FROM s UNION ALL SELECT 0
100 1 1 one 1 1 ONE 2 2 two 2 2 TWO 3 3 three 3 3 THREE
103 do_catchsql_test 2.1.2 {
105 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<3 UNION ALL SELECT 4
107 SELECT * FROM s, t1 WHERE x=i;
108 } {1 {circular reference: s}}
110 do_execsql_test 2.2.0 {
111 CREATE TABLE t2_a(k INTEGER PRIMARY KEY, v TEXT);
112 CREATE TABLE t2_b(k INTEGER PRIMARY KEY, v TEXT);
119 CREATE TRIGGER t2_insert INSTEAD OF INSERT ON t2 BEGIN
120 INSERT INTO t2_a SELECT new.k, new.v WHERE (new.k%2)==0;
121 INSERT INTO t2_b SELECT new.k, new.v WHERE (new.k%2)==1;
124 INSERT INTO t2 VALUES(5, 'v'), (4, 'iv'), (3, 'iii'), (2, 'ii');
127 do_execsql_test 2.2.1 {
128 SELECT * FROM t1, t2 WHERE x=k;
130 2 two 2 ii 2 TWO 2 ii 3 three 3 iii 3 THREE 3 iii
133 do_execsql_test 2.2.2 {
134 SELECT * FROM t1 LEFT JOIN t2 ON (x=k);
138 2 two 2 ii 2 TWO 2 ii 3 three 3 iii 3 THREE 3 iii
141 do_execsql_test 2.2.3 {
142 SELECT x1.*, x2.* FROM t2 AS x1, t2 AS x2 WHERE x1.k=x2.k+1
149 do_execsql_test 2.2.4 {
150 SELECT * FROM t1, t2 WHERE x=k ORDER BY y;
157 do_execsql_test 2.2.5 {
158 SELECT * FROM t1, t2 WHERE x=k ORDER BY y||'';
165 do_execsql_test 2.2.6 {
166 SELECT * FROM t1, t2 WHERE x=k ORDER BY v
173 do_execsql_test 2.2.7 {
174 SELECT * FROM t1, t2 WHERE x=k ORDER BY v||''
181 do_execsql_test 2.2.8 {
182 SELECT * FROM t1, t2 WHERE x=k ORDER BY k,v||''
189 do_execsql_test 2.2.9a {
190 SELECT * FROM t1, t2 ORDER BY +k
192 1 one 2 ii 1 ONE 2 ii 2 two 2 ii
193 2 TWO 2 ii 3 three 2 ii 3 THREE 2 ii
195 1 one 3 iii 1 ONE 3 iii 2 two 3 iii
196 2 TWO 3 iii 3 three 3 iii 3 THREE 3 iii
198 1 one 4 iv 1 ONE 4 iv 2 two 4 iv
199 2 TWO 4 iv 3 three 4 iv 3 THREE 4 iv
201 1 one 5 v 1 ONE 5 v 2 two 5 v
202 2 TWO 5 v 3 three 5 v 3 THREE 5 v
205 do_execsql_test 2.2.9b {
206 SELECT * FROM t1, t2 ORDER BY k
208 1 one 2 ii 1 ONE 2 ii 2 two 2 ii
209 2 TWO 2 ii 3 three 2 ii 3 THREE 2 ii
211 1 one 3 iii 1 ONE 3 iii 2 two 3 iii
212 2 TWO 3 iii 3 three 3 iii 3 THREE 3 iii
214 1 one 4 iv 1 ONE 4 iv 2 two 4 iv
215 2 TWO 4 iv 3 three 4 iv 3 THREE 4 iv
217 1 one 5 v 1 ONE 5 v 2 two 5 v
218 2 TWO 5 v 3 three 5 v 3 THREE 5 v
221 #-------------------------------------------------------------------------
223 do_execsql_test 3.0 {
224 CREATE TABLE t1(c INTEGER PRIMARY KEY, d TEXT);
225 INSERT INTO t1 VALUES(1,2);
226 CREATE TABLE t3_a(k INTEGER PRIMARY KEY, v TEXT);
227 INSERT INTO t3_a VALUES(2,'ii');
228 CREATE TABLE t3_b(k INTEGER PRIMARY KEY, v TEXT);
235 do_execsql_test 3.1 {
236 SELECT * FROM t1, t3 ORDER BY k;
240 do_execsql_test 4.0 {
242 CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT);
243 INSERT INTO t1_a VALUES(123, 't1_a');
244 CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT);
247 SELECT a, b FROM t1_a
249 SELECT c, d FROM t1_b;
251 CREATE TABLE t3_a(k INTEGER PRIMARY KEY, v TEXT);
252 INSERT INTO t3_a VALUES(456, 't3_a');
253 CREATE TABLE t3_b(k INTEGER PRIMARY KEY, v TEXT);
261 do_execsql_test 4.1 {
262 SELECT * FROM t1, t3 ORDER BY k;
263 } {123 t1_a 456 t3_a}
265 do_execsql_test 4.2 {
266 SELECT * FROM (SELECT * FROM t1, t3) ORDER BY k;
267 } {123 t1_a 456 t3_a}
269 do_execsql_test 4.3 {
270 SELECT * FROM (SELECT * FROM t1, t3), (
271 SELECT max(a) OVER () FROM t1
273 SELECT min(a) OVER () FROM t1
277 123 t1_a 456 t3_a 123
278 123 t1_a 456 t3_a 123
281 do_execsql_test 4.3 {
282 SELECT * FROM (SELECT * FROM t1, t3), (
283 SELECT group_concat(a) OVER (ORDER BY a),
284 group_concat(a) OVER (ORDER BY a),
285 group_concat(a) OVER (ORDER BY a),
286 group_concat(a) OVER (ORDER BY a),
287 group_concat(a) OVER (ORDER BY a),
288 group_concat(a) OVER (ORDER BY a),
289 group_concat(a) OVER (ORDER BY a),
290 group_concat(a) OVER (ORDER BY a),
291 group_concat(a) OVER (ORDER BY a)
296 123 t1_a 456 t3_a 123 123 123 123 123 123 123 123 123
299 do_execsql_test 4.3 {
300 SELECT * FROM (SELECT * FROM t1, t3) AS o, (
301 SELECT * FROM t1 LEFT JOIN t3 ON a=k
304 123 t1_a 456 t3_a 123 t1_a {} {}
307 # 2020-12-30: dbsqlfuzz find
309 do_execsql_test 5.1 {
310 CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT);
311 INSERT INTO t1_a VALUES(1,'one');
312 INSERT INTO t1_a VALUES(0,NULL);
313 CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT);
314 INSERT INTO t1_b VALUES(2,'two');
315 INSERT INTO t1_b VALUES(5,'five');
316 CREATE TABLE t1_c(e INTEGER PRIMARY KEY, f TEXT);
317 INSERT INTO t1_c VALUES(3,'three');
318 INSERT INTO t1_c VALUES(6,'six');
319 CREATE TABLE t2(k,v);
320 INSERT INTO t2 VALUES(5,'v');
321 INSERT INTO t2 VALUES(4,'iv');
322 INSERT INTO t2 VALUES(3,'iii');
323 INSERT INTO t2 VALUES(2,'ii');
324 CREATE TABLE t3_a(k INTEGER PRIMARY KEY, v TEXT);
325 INSERT INTO t3_a VALUES(2,'ii');
326 INSERT INTO t3_a VALUES(4,'iv');
327 CREATE TABLE t3_b(k INTEG5R PRIMARY KEY, v TEXT);
328 INSERT INTO t3_b VALUES(NULL,'iii');
329 INSERT INTO t3_b VALUES(NULL,'v');
331 SELECT a, b FROM t1_a UNION ALL
332 SELECT c, d FROM t1_b UNION ALL
333 SELECT e, f FROM t1_c;
338 CREATE TRIGGER t3_insert INSTEAD OF INSERT ON t3 BEGIN
339 INSERT INTO t3_a SELECT new.k, new.v WHERE (new.k%2)==0;
340 INSERT INTO t3_b SELECT new.k, new.v WHERE (new.k%2)==1;
343 do_execsql_test 5.10 {
344 SELECT *, '+' FROM t1 LEFT JOIN t2 ON (a NOT IN(SELECT v FROM t1, t3 WHERE a=k)=NOT EXISTS(SELECT 1 FROM t1 LEFT JOIN t3 ON (a=k)));
345 } {0 {} {} {} + 1 one {} {} + 2 two {} {} + 5 five {} {} + 3 three {} {} + 6 six {} {} +}
346 do_execsql_test 5.20 {
347 SELECT *, '+' FROM t1 LEFT JOIN t3 ON (a NOT IN(SELECT v FROM t1 LEFT JOIN t2 ON (a=k))=k);
348 } {0 {} {} {} + 1 one {} {} + 2 two {} {} + 5 five {} {} + 3 three {} {} + 6 six {} {} +}
350 do_catchsql_test 5.30 {
351 SELECT * FROM (t1 NATURAL JOIN pragma_table_xinfo('t1_a') NATURAL JOIN t3) t1
352 NATURAL JOIN t2 NATURAL JOIN t3
353 WHERE rowid ISNULL>0 AND 0%y;
354 } {1 {ambiguous column name: rowid}}
358 do_execsql_test 6.0 {
359 CREATE TABLE t1(a,b);
360 INSERT INTO t1 VALUES(1,2);
361 CREATE TABLE t2(a,b);
362 INSERT INTO t2 VALUES(3,4);
364 CREATE TABLE t3(a,b);
365 INSERT INTO t3 VALUES(5,6);
366 CREATE TABLE t4(a,b);
367 INSERT INTO t4 VALUES(7,8);
369 CREATE TABLE t5(a,b);
370 INSERT INTO t5 VALUES(9,10);
373 do_execsql_test 6.1 {
375 SELECT 1000 FROM t1 UNION ALL SELECT 800 FROM t2
378 SELECT 100 FROM t3 UNION ALL SELECT 400 FROM t4
380 SELECT * FROM t5, x, y;
382 9 10 1000 100 9 10 1000 400
383 9 10 800 100 9 10 800 400
386 # 2021-04-26 dbsqlfuzz 88ed5c66789fced139d148aed823cba7c0926dd7
388 do_execsql_test 7.1 {
389 WITH c1(x) AS (VALUES(0) UNION ALL SELECT 100+x FROM c1 WHERE x<100 UNION ALL SELECT 1+x FROM c1 WHERE x<1)
391 FROM c1 AS x1, (SELECT x+1 AS y FROM c1 WHERE x<1 UNION ALL SELECT 1+x FROM c1 WHERE 1<x) AS x2
393 } {0 1 | 0 101 | 0 102 | 1 1 | 1 101 | 1 102 | 100 1 | 100 101 | 100 102 | 101 1 | 101 101 | 101 102 |}
395 # 2022-10-31 ticket https://sqlite.org/src/info/57c47526c34f01e8
396 # dbsqlfuzz 37230460b46b3b6049f0d768eb801f3428189382
397 # UNION ALL subqueries or views which have arms with different
398 # affinities should not be flattened.
401 do_execsql_test 8.1 {
402 CREATE TABLE t0(c0 INT);
403 INSERT INTO t0 VALUES(0);
404 CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT);
405 INSERT INTO t1_a VALUES(1,'one');
406 INSERT INTO t1_a VALUES(4,'four');
407 CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT);
408 INSERT INTO t1_b VALUES(2,'two');
409 INSERT INTO t1_b VALUES(5,'five');
410 CREATE TABLE t1_c(e INTEGER PRIMARY KEY, f TEXT);
411 INSERT INTO t1_c VALUES(3,'three');
412 INSERT INTO t1_c VALUES(6,'six');
413 CREATE VIEW v0(c0) AS SELECT CAST(t0.c0 AS INTEGER) FROM t0;
415 SELECT a, b FROM t1_a UNION ALL
416 SELECT c, c FROM t1_b UNION ALL
417 SELECT e, f FROM t1_c;
418 SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1;
419 } {1 one 0 {} 4 four 0 {} 2 2 0 {} 5 5 0 {} 3 three 0 {} 6 six 0 {}}
421 optimization_control db all 1
422 do_execsql_test 8.2 {
423 SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2;
425 do_execsql_test 8.3 {
426 SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2.0;
428 do_execsql_test 8.4 {
429 SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b='2';
431 optimization_control db query-flattener,push-down 0
432 do_execsql_test 8.5 {
433 SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2;
435 do_execsql_test 8.6 {
436 SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2.0;
438 do_execsql_test 8.7 {
439 SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b='2';
441 optimization_control db all 0
442 do_execsql_test 8.8 {
443 SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2;
445 do_execsql_test 8.9 {
446 SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2.0;
448 do_execsql_test 8.10 {
449 SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b='2';