Fix a problem causing the recovery extension to use excessive memory and CPU time...
[sqlite.git] / test / whereL.test
blob2e9ae219e1013944a73e21cfd9cc73aa38bcc16e
1 # 2018-07-26
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 WHERE-clause constant propagation
13 # optimization.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set ::testprefix whereL
19 do_execsql_test 100 {
20   CREATE TABLE t1(a INT PRIMARY KEY, b, c, d, e);
21   CREATE TABLE t2(a INT PRIMARY KEY, f, g, h, i);
22   CREATE TABLE t3(a INT PRIMARY KEY, j, k, l, m);
23   CREATE VIEW v4 AS SELECT * FROM t2 UNION ALL SELECT * FROM t3;
25 do_eqp_test 110 {
26   SELECT * FROM t1, v4 WHERE t1.a=?1 AND v4.a=t1.a;
27 } {
28   QUERY PLAN
29   `--COMPOUND QUERY
30      |--LEFT-MOST SUBQUERY
31      |  |--SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (a=?)
32      |  `--SEARCH t2 USING INDEX sqlite_autoindex_t2_1 (a=?)
33      `--UNION ALL
34         |--SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (a=?)
35         `--SEARCH t3 USING INDEX sqlite_autoindex_t3_1 (a=?)
38 # The scan of the t1 table goes first since that enables the ORDER BY
39 # sort to be omitted.  This would not be possible without constant
40 # propagation because without it the t1 table would depend on t3.
42 do_eqp_test 120 {
43   SELECT * FROM t1, t2, t3
44    WHERE t1.a=t2.a AND t2.a=t3.j AND t3.j=5
45   ORDER BY t1.a;
46 } {
47   QUERY PLAN
48   |--SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (a=?)
49   |--SEARCH t2 USING INDEX sqlite_autoindex_t2_1 (a=?)
50   `--SCAN t3
52 do_eqp_test 121 {
53   SELECT * FROM t1, t2, t3
54    WHERE t1.a=t2.a AND t2.a=t3.j AND t3.j=abs(5)
55   ORDER BY t1.a;
56 } {
57   QUERY PLAN
58   |--SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (a=?)
59   |--SEARCH t2 USING INDEX sqlite_autoindex_t2_1 (a=?)
60   `--SCAN t3
63 # The sqlite3ExprIsConstant() routine does not believe that
64 # the expression "coalesce(5,random())" is constant.  So the
65 # optimization does not apply in this case.
66
67 sqlite3_create_function db
68 do_eqp_test 122 {
69   SELECT * FROM t1, t2, t3
70    WHERE t1.a=t2.a AND t2.a=t3.j AND t3.j=coalesce(5,random())
71   ORDER BY t1.a;
72 } {
73   QUERY PLAN
74   |--SCAN t3
75   |--SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (a=?)
76   |--SEARCH t2 USING INDEX sqlite_autoindex_t2_1 (a=?)
77   `--USE TEMP B-TREE FOR ORDER BY
80 # Constant propagation in the face of collating sequences:
82 do_execsql_test 200 {
83   CREATE TABLE c3(x COLLATE binary, y COLLATE nocase, z COLLATE binary);
84   CREATE INDEX c3x ON c3(x);
85   INSERT INTO c3 VALUES('ABC', 'ABC', 'abc');
86   SELECT * FROM c3 WHERE x=y AND y=z AND z='abc';
87 } {ABC ABC abc}
89 # If the constants are blindly propagated, as shown in the following
90 # query, the wrong answer results:
92 do_execsql_test 201 {
93   SELECT * FROM c3 WHERE x='abc' AND y='abc' AND z='abc';
94 } {}
96 # Constant propagation caused an incorrect answer in the following
97 # query.  (Reported by Bentley system on 2018-08-09.)
99 do_execsql_test 300 {
100   CREATE TABLE A(id INTEGER PRIMARY KEY, label TEXT);
101   CREATE TABLE B(id INTEGER PRIMARY KEY, label TEXT, Aid INTEGER);
102   CREATE TABLE C(
103     id INTEGER PRIMARY KEY,
104     xx INTEGER NOT NULL,
105     yy INTEGER,
106     zz INTEGER
107   );
108   CREATE UNIQUE INDEX x2 ON C(yy);
109   CREATE UNIQUE INDEX x4 ON C(yy, zz);
110   INSERT INTO A(id) VALUES(1);
111   INSERT INTO B(id) VALUES(2);
112   INSERT INTO C(id,xx,yy,zz) VALUES(99,50,1,2);
113   SELECT 1
114     FROM A,
115          (SELECT id,xx,yy,zz FROM C) subq,
116          B
117    WHERE A.id='1'
118      AND A.id=subq.yy
119      AND B.id=subq.zz;
120 } {1}  
121 do_execsql_test 301 {
122   SELECT 1
123     FROM A,
124          (SELECT id,xx,yy,zz FROM C) subq,
125          B
126    WHERE A.id=1
127      AND A.id=subq.yy
128      AND B.id=subq.zz;
129 } {1}  
130 do_execsql_test 302 {
131   SELECT 1
132     FROM A,
133          (SELECT id,yy,zz FROM C) subq,
134          B
135    WHERE A.id='1'
136      AND A.id=subq.yy
137      AND B.id=subq.zz;
138 } {1}  
140 # 2018-10-25: Ticket [cf5ed20f]
141 # Incorrect join result with duplicate WHERE clause constraint.
143 do_execsql_test 400 {
144   CREATE TABLE x(a, b, c);
145   CREATE TABLE y(a, b);
146   INSERT INTO x VALUES (1, 0, 1);
147   INSERT INTO y VALUES (1, 2);
148   SELECT x.a FROM x JOIN y ON x.c = y.a WHERE x.b = 1 AND x.b = 1;
149 } {}
151 # 2020-01-07: ticket 82ac75ba0093e5dc
152 # Incorrect join result due to mishandling of affinity in constant
153 # propagation.
155 reset_db
156 do_execsql_test 500 {
157   PRAGMA automatic_index=OFF;
158   CREATE TABLE t0(c0);
159   INSERT INTO t0 VALUES('0');
160   CREATE VIEW v0(c0) AS SELECT CAST(0 AS INT) FROM t0;
161   SELECT 200, * FROM t0, v0 WHERE 0 = t0.c0 AND t0.c0 = v0.c0;
162 } {}
163 do_execsql_test 510 {
164   SELECT 200, * FROM t0, v0 WHERE t0.c0 = 0 AND t0.c0 = v0.c0;
165 } {}
166 do_execsql_test 520 {
167   SELECT 200, * FROM t0, v0 WHERE 0 = t0.c0 AND v0.c0 = t0.c0;
168 } {}
169 do_execsql_test 530 {
170   SELECT 200, * FROM t0, v0 WHERE t0.c0 = 0 AND v0.c0 = t0.c0;
171 } {}
173 # 2020-02-13: ticket 1dcb4d44964846ad
174 # A problem introduced while making optimizations on the fixes above.
176 reset_db
177 do_execsql_test 600 {
178   CREATE TABLE t1(x TEXT);
179   CREATE TABLE t2(y TEXT);
180   INSERT INTO t1 VALUES('good'),('bad');
181   INSERT INTO t2 VALUES('good'),('bad');
182   SELECT * FROM t1 JOIN t2 ON x=y
183    WHERE x='good' AND y='good';
184 } {good good}
186 # 2020-04-24: Another test case for the previous (1dcb4d44964846ad)
187 # ticket.  The test case comes from 
188 # https://stackoverflow.com/questions/61399253/sqlite3-different-result-in-console-compared-to-python-script/
189 # Output verified against postgresql.
191 do_execsql_test 610 {
192   CREATE TABLE tableA(
193     ID           int,
194     RunYearMonth int
195   );
196   INSERT INTO tableA VALUES(1,202003),(2,202003),(3,202003),(4,202004),
197     (5,202004),(6,202004),(7,202004),(8,202004);
198   CREATE TABLE tableB (
199     ID           int,
200     RunYearMonth int
201   );
202   INSERT INTO tableB VALUES(1,202004),(2,202004),(3,202004),(4,202004),
203   (5,202004);
204   SELECT *
205    FROM (
206         SELECT *
207         FROM tableA
208         WHERE RunYearMonth = 202004
209     ) AS A
210     INNER JOIN (
211         SELECT *
212         FROM tableB
213         WHERE RunYearMonth = 202004
214     ) AS B
215     ON A.ID = B.ID
216     AND A.RunYearMonth = B.RunYearMonth;
217 } {4 202004 4 202004 5 202004 5 202004}
219 # 2023-02-10 https://sqlite.org/forum/forumpost/0a539c76db3b9e29
220 # The original constant propagation implementation caused a performance
221 # regression.  Because "abs(v)" was rewritten into "abs(1)" it no longer
222 # matches the indexed column and the index is not used.
224 reset_db
225 do_execsql_test 700 {
226   CREATE TABLE t1(v INTEGER);
227   WITH RECURSIVE c(x) AS (VALUES(-10) UNION ALL SELECT x+1 FROM c WHERE x<10)
228     INSERT INTO t1(v) SELECT x FROM c;
229   CREATE INDEX idx ON t1( abs(v) );
230   SELECT v FROM t1 WHERE abs(v)=1 and v=1;
231 } 1
232 do_eqp_test 710 {
233   SELECT v FROM t1 WHERE abs(v)=1 and v=1;
234 } {
235   QUERY PLAN
236   `--SEARCH t1 USING INDEX idx (<expr>=?)
239 # 2024-03-07 https://sqlite.org/forum/forumpost/ecdfc02339
240 # A refinement is needed to the enhancements tested by the prior test case
241 # to avoid another problem with indexes on constant expressions.
243 reset_db
244 db null NULL
245 do_execsql_test 800 {
246   CREATE TABLE t0(c0, c1);
247   CREATE TABLE t1(c2);
248   CREATE INDEX i0 ON t1(NULL);
249   INSERT INTO t1(c2) VALUES (0.2);
250   CREATE VIEW v0(c3) AS SELECT DISTINCT c2 FROM t1;
251   SELECT * FROM v0 LEFT JOIN t0 ON c3<NULL LEFT JOIN t1 ON 1;
252 } {0.2 NULL NULL 0.2}
253 do_execsql_test 810 {
254   SELECT * FROM v0 LEFT JOIN t0 ON c3<NULL LEFT JOIN t1 ON 1 WHERE c2/0.1;
255 } {0.2 NULL NULL 0.2}
257 finish_test