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 is on testing that cursor-hints are correct for queries
13 # involving LEFT JOIN.
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19 set ::testprefix cursorhint2
21 ifcapable !cursorhints {
26 proc extract_hints {sql} {
28 db eval "SELECT tbl_name, rootpage FROM sqlite_master where rootpage" {
29 set lookup($rootpage) $tbl_name
33 db eval "EXPLAIN $sql" a {
34 switch -- $a(opcode) {
36 set csr($a(p1)) $lookup($a(p2))
39 lappend ret $csr($a(p1)) $a(p4)
47 proc do_extract_hints_test {tn sql ret} {
48 uplevel [list do_test $tn [list extract_hints $sql] [list {*}$ret]]
52 PRAGMA automatic_index = 0;
53 CREATE TABLE t1(a, b);
54 CREATE TABLE t2(c, d);
55 CREATE TABLE t3(e, f);
58 do_extract_hints_test 1.1 {
59 SELECT * FROM t1 WHERE a=1;
64 do_extract_hints_test 1.2 {
65 SELECT * FROM t1 CROSS JOIN t2 ON (a=c) WHERE d IS NULL;
67 t2 {AND(ISNULL(c1),EQ(r[1],c0))}
70 do_extract_hints_test 1.3 {
71 SELECT * FROM t1 LEFT JOIN t2 ON (a=c) WHERE d IS NULL;
76 do_extract_hints_test 1.4 {
77 SELECT * FROM t1 LEFT JOIN t2 ON (a=c AND a=10) WHERE d IS NULL;
79 t2 {AND(EQ(r[2],c0),EQ(r[3],10))}
82 do_extract_hints_test 1.5 {
83 SELECT * FROM t1 CROSS JOIN t2 ON (a=c AND a=10) WHERE d IS NULL;
85 t1 EQ(c0,10) t2 {AND(ISNULL(c1),EQ(r[3],c0))}
88 do_extract_hints_test 1.6 {
89 SELECT * FROM t1 LEFT JOIN t2 ON (a=c) LEFT JOIN t3 ON (d=f);
91 t2 {EQ(r[2],c0)} t3 {EQ(r[6],c1)}
94 do_extract_hints_test 1.7 {
95 SELECT * FROM t1 LEFT JOIN t2 ON (a=c AND d=e) LEFT JOIN t3 ON (d=f);
97 t2 {EQ(r[2],c0)} t3 {AND(EQ(r[6],c0),EQ(r[7],c1))}
100 #-------------------------------------------------------------------------
102 do_execsql_test 2.0 {
103 CREATE TABLE x1(x, y);
104 CREATE TABLE x2(a, b);
107 do_extract_hints_test 2.1 {
108 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE b IS NULL;
113 do_extract_hints_test 2.2 {
114 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE b IS +NULL;
119 do_extract_hints_test 2.3 {
120 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 1 = (b IS NULL)
125 do_extract_hints_test 2.4 {
126 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 1 = coalesce(b, 1)
131 do_extract_hints_test 2.5 {
132 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 1 = coalesce(b, 1)
137 do_extract_hints_test 2.6 {
138 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 0 = (b IS NOT NULL)
143 do_extract_hints_test 2.7 {
144 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 0 = (b IS NOT +NULL)
149 do_extract_hints_test 2.8 {
150 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE b IS NOT +NULL
155 do_extract_hints_test 2.9 {
156 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE CASE b WHEN 0 THEN 0 ELSE 1 END;
161 do_extract_hints_test 2.10 {
162 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE x2.b = 32+32
164 x2 {AND(EQ(c1,ADD(32,32)),EQ(c0,r[2]))}
167 do_extract_hints_test 2.11 {
168 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE x2.b LIKE 'abc%'
170 x2 {AND(expr,EQ(c0,r[2]))}
173 do_extract_hints_test 2.11 {
174 SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE coalesce(x2.b, 1)