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
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set ::testprefix whereL
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;
26 SELECT * FROM t1, v4 WHERE t1.a=?1 AND v4.a=t1.a;
31 | |--SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (a=?)
32 | `--SEARCH t2 USING INDEX sqlite_autoindex_t2_1 (a=?)
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.
43 SELECT * FROM t1, t2, t3
44 WHERE t1.a=t2.a AND t2.a=t3.j AND t3.j=5
48 |--SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (a=?)
49 |--SEARCH t2 USING INDEX sqlite_autoindex_t2_1 (a=?)
53 SELECT * FROM t1, t2, t3
54 WHERE t1.a=t2.a AND t2.a=t3.j AND t3.j=abs(5)
58 |--SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (a=?)
59 |--SEARCH t2 USING INDEX sqlite_autoindex_t2_1 (a=?)
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.
67 sqlite3_create_function db
69 SELECT * FROM t1, t2, t3
70 WHERE t1.a=t2.a AND t2.a=t3.j AND t3.j=coalesce(5,random())
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:
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';
89 # If the constants are blindly propagated, as shown in the following
90 # query, the wrong answer results:
93 SELECT * FROM c3 WHERE x='abc' AND y='abc' AND z='abc';
96 # Constant propagation caused an incorrect answer in the following
97 # query. (Reported by Bentley system on 2018-08-09.)
100 CREATE TABLE A(id INTEGER PRIMARY KEY, label TEXT);
101 CREATE TABLE B(id INTEGER PRIMARY KEY, label TEXT, Aid INTEGER);
103 id INTEGER PRIMARY KEY,
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);
115 (SELECT id,xx,yy,zz FROM C) subq,
121 do_execsql_test 301 {
124 (SELECT id,xx,yy,zz FROM C) subq,
130 do_execsql_test 302 {
133 (SELECT id,yy,zz FROM C) subq,
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;
151 # 2020-01-07: ticket 82ac75ba0093e5dc
152 # Incorrect join result due to mishandling of affinity in constant
156 do_execsql_test 500 {
157 PRAGMA automatic_index=OFF;
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;
163 do_execsql_test 510 {
164 SELECT 200, * FROM t0, v0 WHERE t0.c0 = 0 AND t0.c0 = v0.c0;
166 do_execsql_test 520 {
167 SELECT 200, * FROM t0, v0 WHERE 0 = t0.c0 AND v0.c0 = t0.c0;
169 do_execsql_test 530 {
170 SELECT 200, * FROM t0, v0 WHERE t0.c0 = 0 AND v0.c0 = t0.c0;
173 # 2020-02-13: ticket 1dcb4d44964846ad
174 # A problem introduced while making optimizations on the fixes above.
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';
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 {
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 (
202 INSERT INTO tableB VALUES(1,202004),(2,202004),(3,202004),(4,202004),
208 WHERE RunYearMonth = 202004
213 WHERE RunYearMonth = 202004
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.
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;
233 SELECT v FROM t1 WHERE abs(v)=1 and v=1;
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.
245 do_execsql_test 800 {
246 CREATE TABLE t0(c0, c1);
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}