xref: /sqlite-3.40.0/test/autoindex4.test (revision c1085ea4)
1059b2d50Sdrh# 2014-10-24
2059b2d50Sdrh#
3059b2d50Sdrh# The author disclaims copyright to this source code.  In place of
4059b2d50Sdrh# a legal notice, here is a blessing:
5059b2d50Sdrh#
6059b2d50Sdrh#    May you do good and not evil.
7059b2d50Sdrh#    May you find forgiveness for yourself and forgive others.
8059b2d50Sdrh#    May you share freely, never taking more than you give.
9059b2d50Sdrh#
10059b2d50Sdrh#*************************************************************************
11059b2d50Sdrh#
12059b2d50Sdrh# This file implements regression tests for SQLite library.  The
13059b2d50Sdrh# focus of this script is testing automatic index creation logic,
14059b2d50Sdrh# and specifically creation of automatic partial indexes.
15059b2d50Sdrh#
16059b2d50Sdrh
17059b2d50Sdrhset testdir [file dirname $argv0]
18059b2d50Sdrhsource $testdir/tester.tcl
19059b2d50Sdrh
20059b2d50Sdrhdo_execsql_test autoindex4-1.0 {
21059b2d50Sdrh  CREATE TABLE t1(a,b);
22059b2d50Sdrh  INSERT INTO t1 VALUES(123,'abc'),(234,'def'),(234,'ghi'),(345,'jkl');
23059b2d50Sdrh  CREATE TABLE t2(x,y);
24059b2d50Sdrh  INSERT INTO t2 VALUES(987,'zyx'),(654,'wvu'),(987,'rqp');
25059b2d50Sdrh
26059b2d50Sdrh  SELECT *, '|' FROM t1, t2 WHERE a=234 AND x=987 ORDER BY +b;
27059b2d50Sdrh} {234 def 987 rqp | 234 def 987 zyx | 234 ghi 987 rqp | 234 ghi 987 zyx |}
28059b2d50Sdrhdo_execsql_test autoindex4-1.1 {
29059b2d50Sdrh  SELECT *, '|' FROM t1, t2 WHERE a=234 AND x=555;
30059b2d50Sdrh} {}
31059b2d50Sdrh
32059b2d50Sdrhdo_execsql_test autoindex4-1.2 {
33059b2d50Sdrh  SELECT *, '|' FROM t1 LEFT JOIN t2 ON a=234 AND x=555;
34059b2d50Sdrh} {123 abc {} {} | 234 def {} {} | 234 ghi {} {} | 345 jkl {} {} |}
35059b2d50Sdrhdo_execsql_test autoindex4-1.3 {
36059b2d50Sdrh  SELECT *, '|' FROM t1 LEFT JOIN t2 ON x=555 WHERE a=234;
37059b2d50Sdrh} {234 def {} {} | 234 ghi {} {} |}
38059b2d50Sdrhdo_execsql_test autoindex4-1.4 {
39059b2d50Sdrh  SELECT *, '|' FROM t1 LEFT JOIN t2 WHERE a=234 AND x=555;
40059b2d50Sdrh} {}
41059b2d50Sdrh
42059b2d50Sdrh
43059b2d50Sdrhdo_execsql_test autoindex4-2.0 {
44059b2d50Sdrh  CREATE TABLE t3(e,f);
45059b2d50Sdrh  INSERT INTO t3 VALUES(123,654),(555,444),(234,987);
46059b2d50Sdrh
47059b2d50Sdrh  SELECT (SELECT count(*) FROM t1, t2 WHERE a=e AND x=f), e, f, '|'
48059b2d50Sdrh    FROM t3
49059b2d50Sdrh   ORDER BY rowid;
50059b2d50Sdrh} {1 123 654 | 0 555 444 | 4 234 987 |}
51059b2d50Sdrh
5236f65bafSdrh# Ticket [2326c258d02ead33d]
5336f65bafSdrh# Two joins, one with and the other without an ORDER BY clause.
5436f65bafSdrh# The one without ORDER BY correctly returns two rows of result.
5536f65bafSdrh# The one with ORDER BY returns no rows.
5636f65bafSdrh#
5736f65bafSdrhdo_execsql_test autoindex4-3.0 {
5836f65bafSdrh  CREATE TABLE A(Name text);
5936f65bafSdrh  CREATE TABLE Items(ItemName text , Name text);
6036f65bafSdrh  INSERT INTO Items VALUES('Item1','Parent');
6136f65bafSdrh  INSERT INTO Items VALUES('Item2','Parent');
6236f65bafSdrh  CREATE TABLE B(Name text);
6336f65bafSdrh
6436f65bafSdrh  SELECT Items.ItemName
6536f65bafSdrh    FROM Items
6636f65bafSdrh      LEFT JOIN A ON (A.Name = Items.ItemName and Items.ItemName = 'dummy')
6736f65bafSdrh      LEFT JOIN B ON (B.Name = Items.ItemName)
6836f65bafSdrh    WHERE Items.Name = 'Parent'
6936f65bafSdrh    ORDER BY Items.ItemName;
7036f65bafSdrh} {Item1 Item2}
71077f06edSdrhdo_execsql_test autoindex4-3.1 {
72077f06edSdrh  CREATE INDEX Items_x1 ON Items(ItemName,Name) WHERE ItemName = 'dummy';
73077f06edSdrh
74077f06edSdrh  SELECT Items.ItemName
75077f06edSdrh    FROM Items
76077f06edSdrh      LEFT JOIN A ON (A.Name = Items.ItemName and Items.ItemName = 'dummy')
77077f06edSdrh      LEFT JOIN B ON (B.Name = Items.ItemName)
78077f06edSdrh    WHERE Items.Name = 'Parent'
79077f06edSdrh    ORDER BY Items.ItemName;
80077f06edSdrh} {Item1 Item2}
8136f65bafSdrh
82*c1085ea4Sdrh# 2021-11-30 - Enhancement to help the automatic index mechanism to
83*c1085ea4Sdrh# create a partial index more often.
84*c1085ea4Sdrh#
85*c1085ea4Sdrhunset -nocomplain id data1 data2 jointype onclause whereclause answer
86*c1085ea4Sdrhforeach {id data1 data2 jointype onclause whereclause answer} {
87*c1085ea4Sdrh  1
88*c1085ea4Sdrh  VALUES(1,2),(3,4)
89*c1085ea4Sdrh  VALUES(1,2),(3,4)
90*c1085ea4Sdrh  {LEFT JOIN}
91*c1085ea4Sdrh  a=x
92*c1085ea4Sdrh  {y=4 OR y IS NULL}
93*c1085ea4Sdrh  {3 4 3 4}
94*c1085ea4Sdrh
95*c1085ea4Sdrh  2
96*c1085ea4Sdrh  VALUES(1,2),(3,4)
97*c1085ea4Sdrh  VALUES(1,2),(3,4)
98*c1085ea4Sdrh  {LEFT JOIN}
99*c1085ea4Sdrh  {a=x AND y=4}
100*c1085ea4Sdrh  {coalesce(y,4)==4}
101*c1085ea4Sdrh  {1 2 {} {} 3 4 3 4}
102*c1085ea4Sdrh
103*c1085ea4Sdrh  3
104*c1085ea4Sdrh  VALUES(1,2),(3,4)
105*c1085ea4Sdrh  VALUES(1,2),(3,4)
106*c1085ea4Sdrh  {JOIN}
107*c1085ea4Sdrh  {a=x}
108*c1085ea4Sdrh  {y=4 OR y IS NULL}
109*c1085ea4Sdrh  {3 4 3 4}
110*c1085ea4Sdrh
111*c1085ea4Sdrh  4
112*c1085ea4Sdrh  VALUES(1,2),(3,4)
113*c1085ea4Sdrh  VALUES(1,2),(3,4)
114*c1085ea4Sdrh  {JOIN}
115*c1085ea4Sdrh  {a=x AND y=4}
116*c1085ea4Sdrh  {coalesce(y,4)==4}
117*c1085ea4Sdrh  {3 4 3 4}
118*c1085ea4Sdrh
119*c1085ea4Sdrh  5
120*c1085ea4Sdrh  VALUES(1,2),(3,4),(NULL,4)
121*c1085ea4Sdrh  VALUES(1,2),(3,4)
122*c1085ea4Sdrh  {LEFT JOIN}
123*c1085ea4Sdrh  a=x
124*c1085ea4Sdrh  {y=4 OR y IS NULL}
125*c1085ea4Sdrh  {3 4 3 4 {} 4 {} {}}
126*c1085ea4Sdrh
127*c1085ea4Sdrh  6
128*c1085ea4Sdrh  VALUES(1,2),(3,4)
129*c1085ea4Sdrh  VALUES(1,2),(3,4),(NULL,4)
130*c1085ea4Sdrh  {LEFT JOIN}
131*c1085ea4Sdrh  {a=x AND y=4}
132*c1085ea4Sdrh  {coalesce(y,4)==4}
133*c1085ea4Sdrh  {1 2 {} {} 3 4 3 4}
134*c1085ea4Sdrh
135*c1085ea4Sdrh  7
136*c1085ea4Sdrh  VALUES(1,2),(3,4),(NULL,4)
137*c1085ea4Sdrh  VALUES(1,2),(3,4),(NULL,4)
138*c1085ea4Sdrh  {JOIN}
139*c1085ea4Sdrh  {a=x}
140*c1085ea4Sdrh  {y=4 OR y IS NULL}
141*c1085ea4Sdrh  {3 4 3 4}
142*c1085ea4Sdrh
143*c1085ea4Sdrh  8
144*c1085ea4Sdrh  VALUES(1,2),(3,4)
145*c1085ea4Sdrh  VALUES(1,2),(3,4)
146*c1085ea4Sdrh  {JOIN}
147*c1085ea4Sdrh  {a=x AND y=4}
148*c1085ea4Sdrh  {coalesce(y,4)==4}
149*c1085ea4Sdrh  {3 4 3 4}
150*c1085ea4Sdrh} {
151*c1085ea4Sdrh  do_test autoindex4-4.$id.0 {
152*c1085ea4Sdrh    db eval {
153*c1085ea4Sdrh       DROP TABLE IF EXISTS t1;
154*c1085ea4Sdrh       CREATE TABLE t1(a INT, b INT);
155*c1085ea4Sdrh       DROP TABLE IF EXISTS t2;
156*c1085ea4Sdrh       CREATE TABLE t2(x INT, y INT);
157*c1085ea4Sdrh    }
158*c1085ea4Sdrh    db eval "INSERT INTO t1(a,b) $data1;"
159*c1085ea4Sdrh    db eval "INSERT INTO t2(x,y) $data2;"
160*c1085ea4Sdrh  } {}
161*c1085ea4Sdrh  set sql "SELECT * FROM t1 $jointype t2 ON $onclause WHERE $whereclause"
162*c1085ea4Sdrh  # puts "sql = $sql"
163*c1085ea4Sdrh  do_test autoindex4-4.$id.1 {
164*c1085ea4Sdrh    db eval {PRAGMA automatic_index=ON;}
165*c1085ea4Sdrh    db eval $sql
166*c1085ea4Sdrh  } $answer
167*c1085ea4Sdrh  do_test autoindex4-4.$id.2 {
168*c1085ea4Sdrh    db eval {PRAGMA automatic_index=OFF;}
169*c1085ea4Sdrh    db eval $sql
170*c1085ea4Sdrh  } $answer
171*c1085ea4Sdrh}
172*c1085ea4Sdrh
173*c1085ea4Sdrh
174*c1085ea4Sdrh
17536f65bafSdrh
176059b2d50Sdrhfinish_test
177