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.
13 # This file implements tests for joins, including outer joins.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
21 CREATE TABLE t1(a,b,c);
22 INSERT INTO t1 VALUES(1,2,3);
23 INSERT INTO t1 VALUES(2,3,4);
24 INSERT INTO t1 VALUES(3,4,5);
30 CREATE TABLE t2(b,c,d);
31 INSERT INTO t2 VALUES(1,2,3);
32 INSERT INTO t2 VALUES(2,3,4);
33 INSERT INTO t2 VALUES(3,4,5);
38 # A FROM clause of the form: "<table>, <table> ON <expr>" is not
39 # allowed by the SQLite syntax diagram, nor by any other SQL database
40 # engine that we are aware of. Nevertheless, historic versions of
41 # SQLite have allowed it. We need to continue to support it moving
42 # forward to prevent breakage of legacy applications. Though, we will
43 # not advertise it as being supported.
45 do_execsql_test join-1.2.1 {
46 SELECT t1.rowid, t2.rowid, '|' FROM t1, t2 ON t1.a=t2.b;
51 SELECT * FROM t1 NATURAL JOIN t2;
53 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
56 SELECT * FROM t2 NATURAL JOIN t1;
58 } {b 2 c 3 d 4 a 1 b 3 c 4 d 5 a 2}
61 SELECT * FROM t2 AS x NATURAL JOIN t1;
63 } {b 2 c 3 d 4 a 1 b 3 c 4 d 5 a 2}
66 SELECT * FROM t2 NATURAL JOIN t1 AS y;
68 } {b 2 c 3 d 4 a 1 b 3 c 4 d 5 a 2}
71 SELECT b FROM t1 NATURAL JOIN t2;
78 SELECT t2.* FROM t2 NATURAL JOIN t1
80 } {b 2 c 3 d 4 b 3 c 4 d 5}
83 SELECT xyzzy.* FROM t2 AS xyzzy NATURAL JOIN t1
85 } {b 2 c 3 d 4 b 3 c 4 d 5}
88 SELECT t1.* FROM t2 NATURAL JOIN t1
90 } {a 1 b 2 c 3 a 2 b 3 c 4}
93 SELECT xyzzy.* FROM t2 NATURAL JOIN t1 AS xyzzy
95 } {a 1 b 2 c 3 a 2 b 3 c 4}
98 SELECT aaa.*, bbb.* FROM t2 AS aaa NATURAL JOIN t1 AS bbb
100 } {b 2 c 3 d 4 a 1 b 2 c 3 b 3 c 4 d 5 a 2 b 3 c 4}
101 do_test join-1.3.10 {
103 SELECT t1.*, t2.* FROM t2 NATURAL JOIN t1
105 } {a 1 b 2 c 3 b 2 c 3 d 4 a 2 b 3 c 4 b 3 c 4 d 5}
110 SELECT * FROM t1 INNER JOIN t2 USING(b,c);
112 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
115 SELECT * FROM t1 AS x INNER JOIN t2 USING(b,c);
117 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
120 SELECT * FROM t1 INNER JOIN t2 AS y USING(b,c);
122 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
125 SELECT * FROM t1 AS x INNER JOIN t2 AS y USING(b,c);
127 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
130 SELECT b FROM t1 JOIN t2 USING(b);
137 SELECT t1.* FROM t1 JOIN t2 USING(b);
139 } {a 1 b 2 c 3 a 2 b 3 c 4}
142 SELECT t2.* FROM t1 JOIN t2 USING(b);
144 } {b 2 c 3 d 4 b 3 c 4 d 5}
148 SELECT * FROM t1 INNER JOIN t2 USING(b);
150 } {a 1 b 2 c 3 c 3 d 4 a 2 b 3 c 4 c 4 d 5}
153 SELECT * FROM t1 INNER JOIN t2 USING(c);
155 } {a 1 b 2 c 3 b 2 d 4 a 2 b 3 c 4 b 3 d 5}
158 SELECT * FROM t1 INNER JOIN t2 USING(c,b);
160 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
164 SELECT * FROM t1 NATURAL CROSS JOIN t2;
169 SELECT * FROM t1 CROSS JOIN t2 USING(b,c);
174 SELECT * FROM t1 NATURAL INNER JOIN t2;
179 SELECT * FROM t1 INNER JOIN t2 USING(b,c);
184 SELECT * FROM t1 natural inner join t2;
191 SELECT * FROM t1 NATURAL JOIN
192 (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as t3
194 } {a 1 b 2 c 3 d 4 e 5}
197 SELECT * FROM (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as 'tx'
200 } {c 3 d 4 e 5 a 1 b 2}
205 CREATE TABLE t3(c,d,e);
206 INSERT INTO t3 VALUES(2,3,4);
207 INSERT INTO t3 VALUES(3,4,5);
208 INSERT INTO t3 VALUES(4,5,6);
211 } {2 3 4 3 4 5 4 5 6}
214 SELECT * FROM t1 natural join t2 natural join t3;
216 } {1 2 3 4 5 2 3 4 5 6}
219 SELECT * FROM t1 natural join t2 natural join t3;
221 } {a 1 b 2 c 3 d 4 e 5 a 2 b 3 c 4 d 5 e 6}
224 CREATE TABLE t4(d,e,f);
225 INSERT INTO t4 VALUES(2,3,4);
226 INSERT INTO t4 VALUES(3,4,5);
227 INSERT INTO t4 VALUES(4,5,6);
230 } {2 3 4 3 4 5 4 5 6}
231 do_test join-1.19.1 {
233 SELECT * FROM t1 natural join t2 natural join t4;
236 do_test join-1.19.2 {
238 SELECT * FROM t1 natural join t2 natural join t4;
240 } {a 1 b 2 c 3 d 4 e 5 f 6}
243 SELECT * FROM t1 natural join t2 natural join t3 WHERE t1.a=1
249 SELECT * FROM t1 NATURAL LEFT JOIN t2;
251 } {1 2 3 4 2 3 4 5 3 4 5 {}}
256 SELECT * FROM t1 NATURAL LEFT JOIN t2;
258 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5 a 3 b 4 c 5 d {}}
261 SELECT t1.* FROM t1 NATURAL LEFT JOIN t2;
263 } {a 1 b 2 c 3 a 2 b 3 c 4 a 3 b 4 c 5}
266 SELECT t2.* FROM t1 NATURAL LEFT JOIN t2;
268 } {b 2 c 3 d 4 b 3 c 4 d 5 b {} c {} d {}}
272 SELECT * FROM t2 NATURAL LEFT OUTER JOIN t1;
274 } {1 2 3 {} 2 3 4 1 3 4 5 2}
277 SELECT * FROM t1 NATURAL RIGHT OUTER JOIN t2;
279 } {1 {RIGHT and FULL OUTER JOINs are not currently supported}}
282 SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d
284 } {1 2 3 {} {} {} 2 3 4 {} {} {} 3 4 5 1 2 3}
287 SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t1.a>1
289 } {2 3 4 {} {} {} 3 4 5 1 2 3}
292 SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t2.b IS NULL OR t2.b>1
294 } {1 2 3 {} {} {} 2 3 4 {} {} {}}
298 SELECT * FROM t1 NATURAL JOIN t2 ON t1.a=t2.b;
300 } {1 {a NATURAL join may not have an ON or USING clause}}
303 SELECT * FROM t1 NATURAL JOIN t2 USING(b);
305 } {1 {a NATURAL join may not have an ON or USING clause}}
308 SELECT * FROM t1 JOIN t2 ON t1.a=t2.b USING(b);
310 } {1 {cannot have both ON and USING clauses in the same join}}
313 SELECT * FROM t1 JOIN t2 USING(a);
315 } {1 {cannot join using column a - column not present in both tables}}
318 SELECT * FROM t1 JOIN t2 USING(d);
320 } {1 {cannot join using column d - column not present in both tables}}
322 catchsql { SELECT * FROM t1 USING(a) }
323 } {1 {a JOIN clause is required before USING}}
326 SELECT * FROM t1 JOIN t2 ON t3.a=t2.b;
328 } {1 {no such column: t3.a}}
331 SELECT * FROM t1 INNER OUTER JOIN t2;
333 } {1 {unknown or unsupported join type: INNER OUTER}}
336 SELECT * FROM t1 INNER OUTER CROSS JOIN t2;
338 } {1 {unknown or unsupported join type: INNER OUTER CROSS}}
341 SELECT * FROM t1 OUTER NATURAL INNER JOIN t2;
343 } {1 {unknown or unsupported join type: OUTER NATURAL INNER}}
346 SELECT * FROM t1 LEFT BOGUS JOIN t2;
348 } {1 {unknown or unsupported join type: LEFT BOGUS}}
351 SELECT * FROM t1 INNER BOGUS CROSS JOIN t2;
353 } {1 {unknown or unsupported join type: INNER BOGUS CROSS}}
356 SELECT * FROM t1 NATURAL AWK SED JOIN t2;
358 } {1 {unknown or unsupported join type: NATURAL AWK SED}}
363 CREATE TABLE t5(a INTEGER PRIMARY KEY);
364 CREATE TABLE t6(a INTEGER);
365 INSERT INTO t6 VALUES(NULL);
366 INSERT INTO t6 VALUES(NULL);
367 INSERT INTO t6 SELECT * FROM t6;
368 INSERT INTO t6 SELECT * FROM t6;
369 INSERT INTO t6 SELECT * FROM t6;
370 INSERT INTO t6 SELECT * FROM t6;
371 INSERT INTO t6 SELECT * FROM t6;
372 INSERT INTO t6 SELECT * FROM t6;
376 SELECT * FROM t6 NATURAL JOIN t5;
381 SELECT * FROM t6, t5 WHERE t6.a<t5.a;
386 SELECT * FROM t6, t5 WHERE t6.a>t5.a;
391 UPDATE t6 SET a='xyz';
392 SELECT * FROM t6 NATURAL JOIN t5;
397 SELECT * FROM t6, t5 WHERE t6.a<t5.a;
402 SELECT * FROM t6, t5 WHERE t6.a>t5.a;
408 SELECT * FROM t6 NATURAL JOIN t5;
413 SELECT * FROM t6, t5 WHERE t6.a<t5.a;
418 SELECT * FROM t6, t5 WHERE t6.a>t5.a;
425 create table centros (id integer primary key, centro);
426 INSERT INTO centros VALUES(1,'xxx');
427 create table usuarios (id integer primary key, nombre, apellidos,
429 INSERT INTO usuarios VALUES(1,'a','aa',1);
430 INSERT INTO usuarios VALUES(2,'b','bb',1);
431 INSERT INTO usuarios VALUES(3,'c','cc',NULL);
432 create index idcentro on usuarios (idcentro);
434 select usuarios.id, usuarios.nombre, centros.centro from
435 usuarios left outer join centros on usuarios.idcentro = centros.id;
437 } {1 a xxx 2 b xxx 3 c {}}
439 # A test for ticket #247.
442 sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1
444 CREATE TABLE t7 (x, y);
445 INSERT INTO t7 VALUES ("pa1", 1);
446 INSERT INTO t7 VALUES ("pa2", NULL);
447 INSERT INTO t7 VALUES ("pa3", NULL);
448 INSERT INTO t7 VALUES ("pa4", 2);
449 INSERT INTO t7 VALUES ("pa30", 131);
450 INSERT INTO t7 VALUES ("pa31", 130);
451 INSERT INTO t7 VALUES ("pa28", NULL);
453 CREATE TABLE t8 (a integer primary key, b);
454 INSERT INTO t8 VALUES (1, "pa1");
455 INSERT INTO t8 VALUES (2, "pa4");
456 INSERT INTO t8 VALUES (3, NULL);
457 INSERT INTO t8 VALUES (4, NULL);
458 INSERT INTO t8 VALUES (130, "pa31");
459 INSERT INTO t8 VALUES (131, "pa30");
461 SELECT coalesce(t8.a,999) from t7 LEFT JOIN t8 on y=a;
463 } {1 999 999 2 131 130 999}
465 # Make sure a left join where the right table is really a view that
466 # is itself a join works right. Ticket #306.
472 CREATE TABLE t9(a INTEGER PRIMARY KEY, b);
473 INSERT INTO t9 VALUES(1,11);
474 INSERT INTO t9 VALUES(2,22);
475 CREATE TABLE t10(x INTEGER PRIMARY KEY, y);
476 INSERT INTO t10 VALUES(1,2);
477 INSERT INTO t10 VALUES(3,3);
478 CREATE TABLE t11(p INTEGER PRIMARY KEY, q);
479 INSERT INTO t11 VALUES(2,111);
480 INSERT INTO t11 VALUES(3,333);
481 CREATE VIEW v10_11 AS SELECT x, q FROM t10, t11 WHERE t10.y=t11.p;
483 SELECT * FROM t9 LEFT JOIN v10_11 ON( a=x );
485 } {1 11 1 111 2 22 {} {}}
489 SELECT * FROM t9 LEFT JOIN (SELECT x, q FROM t10, t11 WHERE t10.y=t11.p)
492 } {1 11 1 111 2 22 {} {}}
496 SELECT * FROM v10_11 LEFT JOIN t9 ON( a=x );
498 } {1 111 1 11 3 333 {} {}}
500 # Constant expressions in a subquery that is the right element of a
501 # LEFT JOIN evaluate to NULL for rows where the LEFT JOIN does not
502 # match. Ticket #3300
505 SELECT * FROM t9 LEFT JOIN (SELECT 44, p, q FROM t11) AS sub1 ON p=a
507 } {1 11 {} {} {} 2 22 44 2 111}
511 # Ticket #350 describes a scenario where LEFT OUTER JOIN does not
512 # function correctly if the right table in the join is really
515 # To test the problem, we generate the same LEFT OUTER JOIN in two
516 # separate selects but with on using a subquery and the other calling
517 # the table directly. Then connect the two SELECTs using an EXCEPT.
518 # Both queries should generate the same results so the answer should
525 CREATE TABLE t12(a,b);
526 INSERT INTO t12 VALUES(1,11);
527 INSERT INTO t12 VALUES(2,22);
528 CREATE TABLE t13(b,c);
529 INSERT INTO t13 VALUES(22,222);
537 SELECT * FROM t12 NATURAL LEFT JOIN t13
539 SELECT * FROM t12 NATURAL LEFT JOIN (SELECT * FROM t13 WHERE b>0);
546 CREATE VIEW v13 AS SELECT * FROM t13 WHERE b>0;
547 SELECT * FROM t12 NATURAL LEFT JOIN t13
549 SELECT * FROM t12 NATURAL LEFT JOIN v13;
553 } ;# ifcapable compound
556 # Ticket #1697: Left Join WHERE clause terms that contain an
557 # aggregate subquery.
561 CREATE TABLE t21(a,b,c);
562 CREATE TABLE t22(p,q);
563 CREATE INDEX i22 ON t22(q);
564 SELECT a FROM t21 LEFT JOIN t22 ON b=p WHERE q=
565 (SELECT max(m.q) FROM t22 m JOIN t21 n ON n.b=m.p WHERE n.c=1);
569 # Test a LEFT JOIN when the right-hand side of hte join is an empty
570 # sub-query. Seems fine.
574 CREATE TABLE t23(a, b, c);
575 CREATE TABLE t24(a, b, c);
576 INSERT INTO t23 VALUES(1, 2, 3);
579 SELECT * FROM t23 LEFT JOIN t24;
584 SELECT * FROM t23 LEFT JOIN (SELECT * FROM t24);
588 } ;# ifcapable subquery
590 #-------------------------------------------------------------------------
591 # The following tests are to ensure that bug b73fb0bd64 is fixed.
596 CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT);
597 CREATE TABLE t2(a INTEGER PRIMARY KEY, b TEXT);
598 INSERT INTO t1 VALUES(1,'abc');
599 INSERT INTO t1 VALUES(2,'def');
600 INSERT INTO t2 VALUES(1,'abc');
601 INSERT INTO t2 VALUES(2,'def');
602 SELECT * FROM t1 NATURAL JOIN t2;
607 execsql { SELECT a FROM t1 JOIN t1 USING (a)}
610 execsql { SELECT a FROM t1 JOIN t1 AS t2 USING (a)}
613 execsql { SELECT * FROM t1 NATURAL JOIN t1 AS t2}
616 execsql { SELECT * FROM t1 NATURAL JOIN t1 }
622 CREATE TABLE t1(a COLLATE nocase, b);
623 CREATE TABLE t2(a, b);
624 INSERT INTO t1 VALUES('ONE', 1);
625 INSERT INTO t1 VALUES('two', 2);
626 INSERT INTO t2 VALUES('one', 1);
627 INSERT INTO t2 VALUES('two', 2);
631 execsql { SELECT * FROM t1 NATURAL JOIN t2 }
634 execsql { SELECT * FROM t2 NATURAL JOIN t1 }
640 CREATE TABLE t1(a, b TEXT);
641 CREATE TABLE t2(b INTEGER, a);
642 INSERT INTO t1 VALUES('one', '1.0');
643 INSERT INTO t1 VALUES('two', '2');
644 INSERT INTO t2 VALUES(1, 'one');
645 INSERT INTO t2 VALUES(2, 'two');
649 execsql { SELECT * FROM t1 NATURAL JOIN t2 }
652 execsql { SELECT * FROM t2 NATURAL JOIN t1 }
655 #-------------------------------------------------------------------------
656 # Test that at most 64 tables are allowed in a join.
658 do_execsql_test join-12.1 {
660 INSERT INTO t14 VALUES('abcdefghij');
663 proc jointest {tn nTbl res} {
664 set sql "SELECT 1 FROM [string repeat t14, [expr $nTbl-1]] t14;"
665 uplevel [list do_catchsql_test $tn $sql $res]
668 jointest join-12.2 30 {0 1}
669 jointest join-12.3 63 {0 1}
670 jointest join-12.4 64 {0 1}
671 jointest join-12.5 65 {1 {at most 64 tables in a join}}
672 jointest join-12.6 66 {1 {at most 64 tables in a join}}
673 jointest join-12.7 127 {1 {at most 64 tables in a join}}
674 jointest join-12.8 128 {1 {at most 64 tables in a join}}
676 # As of 2019-01-17, the number of elements in a SrcList is limited
677 # to 200. The following tests still run, but the answer is now
678 # an SQLITE_NOMEM error.
680 # jointest join-12.9 1000 {1 {at most 64 tables in a join}}
682 # If SQLite is built with SQLITE_MEMDEBUG, then the huge number of realloc()
683 # calls made by the following test cases are too time consuming to run.
684 # Without SQLITE_MEMDEBUG, realloc() is fast enough that these are not
687 # ifcapable pragma&&compileoption_diags {
688 # if {[lsearch [db eval {PRAGMA compile_options}] MEMDEBUG]<0} {
689 # jointest join-12.10 65534 {1 {at most 64 tables in a join}}
690 # jointest join-12.11 65535 {1 {too many references to "t14": max 65535}}
691 # jointest join-12.12 65536 {1 {too many references to "t14": max 65535}}
692 # jointest join-12.13 65537 {1 {too many references to "t14": max 65535}}
697 #-------------------------------------------------------------------------
698 # Test a problem with reordering tables following a LEFT JOIN.
700 do_execsql_test join-13.0 {
705 INSERT INTO aa VALUES(45);
706 INSERT INTO cc VALUES(45);
707 INSERT INTO cc VALUES(45);
710 do_execsql_test join-13.1 {
711 SELECT * FROM aa LEFT JOIN bb, cc WHERE cc.c=aa.a;
712 } {45 {} 45 45 {} 45}
714 # In the following, the order of [cc] and [bb] must not be exchanged, even
715 # though this would be helpful if the query used an inner join.
716 do_execsql_test join-13.2 {
717 CREATE INDEX ccc ON cc(c);
718 SELECT * FROM aa LEFT JOIN bb, cc WHERE cc.c=aa.a;
719 } {45 {} 45 45 {} 45}
721 # Verify that that iTable attributes the TK_IF_NULL_ROW operators in the
722 # expression tree are correctly updated by the query flattener. This was
723 # a bug discovered on 2017-05-22 by Mark Brand.
725 do_execsql_test join-14.1 {
727 FROM (SELECT 1 a) AS x
728 LEFT JOIN (SELECT 1, * FROM (SELECT * FROM (SELECT 1)));
730 do_execsql_test join-14.2 {
732 FROM (SELECT 1 a) AS x
733 LEFT JOIN (SELECT 1, * FROM (SELECT * FROM (SELECT * FROM (SELECT 1)))) AS y
734 JOIN (SELECT * FROM (SELECT 9)) AS z;
736 do_execsql_test join-14.3 {
739 LEFT JOIN (SELECT cc+222, * FROM (SELECT * FROM (SELECT 333 cc)));
742 do_execsql_test join-14.4 {
743 DROP TABLE IF EXISTS t1;
744 CREATE TABLE t1(c PRIMARY KEY, a TEXT(10000), b TEXT(10000));
745 SELECT * FROM (SELECT 111) LEFT JOIN (SELECT c+222 FROM t1) GROUP BY 1;
747 do_execsql_test join-14.5 {
748 DROP TABLE IF EXISTS t1;
749 CREATE TABLE t1(c PRIMARY KEY) WITHOUT ROWID;
750 SELECT * FROM (SELECT 111) LEFT JOIN (SELECT c+222 FROM t1) GROUP BY 1;
753 # Verify the fix to ticket
754 # https://www.sqlite.org/src/tktview/7fde638e94287d2c948cd9389
758 do_execsql_test join-14.10 {
760 INSERT INTO t1 VALUES(1),(2),(3);
761 CREATE VIEW v2 AS SELECT a, 1 AS b FROM t1;
763 INSERT INTO t3 VALUES(2),(4);
764 SELECT *, '|' FROM t3 LEFT JOIN v2 ON a=x WHERE b=1;
766 do_execsql_test join-14.11 {
767 SELECT *, '|' FROM t3 LEFT JOIN v2 ON a=x WHERE b+1=x;
769 do_execsql_test join-14.12 {
770 SELECT *, '|' FROM t3 LEFT JOIN v2 ON a=x ORDER BY b;
771 } {4 {} {} | 2 2 1 |}
773 # Verify the fix for ticket
774 # https://www.sqlite.org/src/info/892fc34f173e99d8
778 do_execsql_test join-14.20 {
779 CREATE TABLE t1(id INTEGER PRIMARY KEY);
780 CREATE TABLE t2(id INTEGER PRIMARY KEY, c2 INTEGER);
781 CREATE TABLE t3(id INTEGER PRIMARY KEY, c3 INTEGER);
782 INSERT INTO t1(id) VALUES(456);
783 INSERT INTO t3(id) VALUES(1),(2);
784 SELECT t1.id, x2.id, x3.id
786 LEFT JOIN (SELECT * FROM t2) AS x2 ON t1.id=x2.c2
787 LEFT JOIN t3 AS x3 ON x2.id=x3.c3;
791 # E.Pasma discovered that the LEFT JOIN strength reduction optimization
792 # was misbehaving. The problem turned out to be that the
793 # sqlite3ExprImpliesNotNull() routine was saying that CASE expressions
796 # CASE WHEN true THEN true ELSE x=0 END
798 # could never be true if x is NULL. The following test cases verify
799 # that this error has been resolved.
803 do_execsql_test join-15.100 {
804 CREATE TABLE t1(a INT, b INT);
805 INSERT INTO t1 VALUES(1,2),(3,4);
806 CREATE TABLE t2(x INT, y INT);
809 WHERE CASE WHEN FALSE THEN a=x ELSE 1 END;
810 } {1 2 {} {} x 3 4 {} {} x}
811 do_execsql_test join-15.105 {
814 WHERE a IN (1,3,x,y);
815 } {1 2 {} {} x 3 4 {} {} x}
816 do_execsql_test join-15.106a {
819 WHERE NOT ( 'x'='y' AND t2.y=1 );
820 } {1 2 {} {} x 3 4 {} {} x}
821 do_execsql_test join-15.106b {
824 WHERE ~ ( 'x'='y' AND t2.y=1 );
825 } {1 2 {} {} x 3 4 {} {} x}
826 do_execsql_test join-15.107 {
829 WHERE t2.y IS NOT 'abc'
830 } {1 2 {} {} x 3 4 {} {} x}
831 do_execsql_test join-15.110 {
834 CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER);
835 INSERT INTO t1(a,b) VALUES(1,0),(11,1),(12,1),(13,1),(121,12);
836 CREATE INDEX t1b ON t1(b);
837 CREATE TABLE t2(x INTEGER PRIMARY KEY);
838 INSERT INTO t2(x) VALUES(0),(1);
839 SELECT a1, a2, a3, a4, a5
840 FROM (SELECT a AS a1 FROM t1 WHERE b=0)
841 JOIN (SELECT x AS x1 FROM t2)
842 LEFT JOIN (SELECT a AS a2, b AS b2 FROM t1)
843 ON x1 IS TRUE AND b2=a1
844 JOIN (SELECT x AS x2 FROM t2)
845 ON x2<=CASE WHEN x1 THEN CASE WHEN a2 THEN 1 ELSE -1 END ELSE 0 END
846 LEFT JOIN (SELECT a AS a3, b AS b3 FROM t1)
847 ON x2 IS TRUE AND b3=a2
848 JOIN (SELECT x AS x3 FROM t2)
849 ON x3<=CASE WHEN x2 THEN CASE WHEN a3 THEN 1 ELSE -1 END ELSE 0 END
850 LEFT JOIN (SELECT a AS a4, b AS b4 FROM t1)
851 ON x3 IS TRUE AND b4=a3
852 JOIN (SELECT x AS x4 FROM t2)
853 ON x4<=CASE WHEN x3 THEN CASE WHEN a4 THEN 1 ELSE -1 END ELSE 0 END
854 LEFT JOIN (SELECT a AS a5, b AS b5 FROM t1)
855 ON x4 IS TRUE AND b5=a4
856 ORDER BY a1, a2, a3, a4, a5;
857 } {1 {} {} {} {} 1 11 {} {} {} 1 12 {} {} {} 1 12 121 {} {} 1 13 {} {} {}}
859 # 2019-02-05 Ticket https://www.sqlite.org/src/tktview/5948e09b8c415bc45da5c
860 # Error in join due to the LEFT JOIN strength reduction optimization.
862 do_execsql_test join-16.100 {
863 DROP TABLE IF EXISTS t1;
864 DROP TABLE IF EXISTS t2;
865 CREATE TABLE t1(a INT);
866 INSERT INTO t1(a) VALUES(1);
867 CREATE TABLE t2(b INT);
869 FROM t1 LEFT JOIN t2 ON 0
870 WHERE (b IS NOT NULL)=0;
873 # 2019-08-17 ticket https://sqlite.org/src/tktview/6710d2f7a13a299728ab
874 # Ensure that constants that derive from the right-hand table of a LEFT JOIN
875 # are never factored out, since they are not really constant.
877 do_execsql_test join-17.100 {
878 DROP TABLE IF EXISTS t1;
880 INSERT INTO t1(x) VALUES(0),(1);
881 SELECT * FROM t1 LEFT JOIN (SELECT abs(1) AS y FROM t1) ON x WHERE NOT(y='a');
883 do_execsql_test join-17.110 {
884 SELECT * FROM t1 LEFT JOIN (SELECT abs(1)+2 AS y FROM t1) ON x
888 #-------------------------------------------------------------------------
890 do_execsql_test join-18.1 {
893 CREATE VIEW v0 AS SELECT a FROM t1 LEFT JOIN t0;
894 INSERT INTO t1 VALUES (1);
897 do_execsql_test join-18.2 {
898 SELECT * FROM v0 WHERE NOT(v0.a IS FALSE);
901 do_execsql_test join-18.3 {
902 SELECT * FROM t1 LEFT JOIN t0 WHERE NOT(a IS FALSE);
905 do_execsql_test join-18.4 {
906 SELECT NOT(v0.a IS FALSE) FROM v0
909 #-------------------------------------------------------------------------
911 do_execsql_test join-19.0 {
914 INSERT INTO t1(a) VALUES(0);
915 CREATE VIEW v0(c) AS SELECT t2.b FROM t1 LEFT JOIN t2;
918 do_execsql_test join-19.1 {
919 SELECT * FROM v0 WHERE v0.c NOTNULL NOTNULL;
922 do_execsql_test join-19.2 {
923 SELECT * FROM t1 LEFT JOIN t2
926 do_execsql_test join-19.3 {
927 SELECT * FROM t1 LEFT JOIN t2 WHERE (b IS NOT NULL) IS NOT NULL;
930 do_execsql_test join-19.4 {
931 SELECT (b IS NOT NULL) IS NOT NULL FROM t1 LEFT JOIN t2
934 do_execsql_test join-19.5 {
935 SELECT * FROM t1 LEFT JOIN t2 WHERE
936 (b IS NOT NULL AND b IS NOT NULL) IS NOT NULL;
939 # 2019-11-02 ticket 623eff57e76d45f6
940 # The optimization of exclusing the WHERE expression of a partial index
941 # from the WHERE clause of the query if the index is used does not work
942 # of the table of the index is the right-hand table of a LEFT JOIN.
946 do_execsql_test join-20.1 {
949 INSERT INTO t0(c0) VALUES (0);
950 SELECT * FROM t0 LEFT JOIN t1 WHERE NULL IN (c1);
952 do_execsql_test join-20.2 {
953 CREATE INDEX t1x ON t1(0) WHERE NULL IN (c1);
954 SELECT * FROM t0 LEFT JOIN t1 WHERE NULL IN (c1);
957 # 2019-11-30 ticket 7f39060a24b47353
958 # Do not allow a WHERE clause term to qualify a partial index on the
959 # right table of a LEFT JOIN.
961 do_execsql_test join-21.10 {
966 INSERT INTO t0(aa) VALUES (1);
967 INSERT INTO t1(bb) VALUES (1);
968 SELECT 11, * FROM t1 LEFT JOIN t0 WHERE aa ISNULL;
969 SELECT 12, * FROM t1 LEFT JOIN t0 WHERE +aa ISNULL;
970 SELECT 13, * FROM t1 LEFT JOIN t0 ON aa ISNULL;
971 SELECT 14, * FROM t1 LEFT JOIN t0 ON +aa ISNULL;
972 CREATE INDEX i0 ON t0(aa) WHERE aa ISNULL;
973 SELECT 21, * FROM t1 LEFT JOIN t0 WHERE aa ISNULL;
974 SELECT 22, * FROM t1 LEFT JOIN t0 WHERE +aa ISNULL;
975 SELECT 23, * FROM t1 LEFT JOIN t0 ON aa ISNULL;
976 SELECT 24, * FROM t1 LEFT JOIN t0 ON +aa ISNULL;
977 } {13 1 {} 14 1 {} 23 1 {} 24 1 {}}
979 # 2019-12-18 problem with a LEFT JOIN where the RHS is a view.
980 # Detected by Yongheng and Rui.
981 # Follows from the optimization attempt of check-in 41c27bc0ff1d3135
985 do_execsql_test join-22.10 {
986 CREATE TABLE t0(a, b);
987 CREATE INDEX t0a ON t0(a);
988 INSERT INTO t0 VALUES(10,10),(10,11),(10,12);
989 SELECT DISTINCT c FROM t0 LEFT JOIN (SELECT a+1 AS c FROM t0) ORDER BY c ;
992 # 2019-12-22 ticket 7929c1efb2d67e98
996 do_execsql_test join-23.10 {
998 INSERT INTO t0(c0) VALUES(123);
999 CREATE VIEW v0(c0) AS SELECT 0 GROUP BY 1;
1000 SELECT t0.c0, v0.c0, vt0.name
1001 FROM v0, t0 LEFT JOIN pragma_table_info('t0') AS vt0
1002 ON vt0.name LIKE 'c0'
1007 #-------------------------------------------------------------------------
1009 do_execsql_test join-24.1 {
1010 CREATE TABLE t1(a PRIMARY KEY, x);
1011 CREATE TABLE t2(b INT);
1012 CREATE INDEX t1aa ON t1(a, a);
1014 INSERT INTO t1 VALUES('abc', 'def');
1015 INSERT INTO t2 VALUES(1);
1018 do_execsql_test join-24.2 {
1019 SELECT * FROM t2 JOIN t1 WHERE a='abc' AND x='def';
1021 do_execsql_test join-24.3 {
1022 SELECT * FROM t2 JOIN t1 WHERE a='abc' AND x='abc';
1025 do_execsql_test join-24.2 {
1026 SELECT * FROM t2 LEFT JOIN t1 ON a=0 WHERE (x='x' OR x IS NULL);
1029 # 2020-09-30 ticket 66e4b0e271c47145
1030 # The query flattener inserts an "expr AND expr" expression as a substitution
1031 # for the column of a view where that view column is part of an ON expression
1035 do_execsql_test join-25.1 {
1036 CREATE TABLE t0(c0 INT);
1037 CREATE VIEW v0 AS SELECT (NULL AND 5) as c0 FROM t0;
1038 INSERT INTO t0(c0) VALUES (NULL);
1039 SELECT count(*) FROM v0 LEFT JOIN t0 ON v0.c0;