2017-04-28 22:59:55 +03:00
|
|
|
# 2017 April 29
|
|
|
|
#
|
|
|
|
# The author disclaims copyright to this source code. In place of
|
|
|
|
# a legal notice, here is a blessing:
|
|
|
|
#
|
|
|
|
# May you do good and not evil.
|
|
|
|
# May you find forgiveness for yourself and forgive others.
|
|
|
|
# May you share freely, never taking more than you give.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
|
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
source $testdir/tester.tcl
|
|
|
|
set testprefix pushdown
|
|
|
|
|
|
|
|
do_execsql_test 1.0 {
|
|
|
|
CREATE TABLE t1(a, b, c);
|
|
|
|
INSERT INTO t1 VALUES(1, 'b1', 'c1');
|
|
|
|
INSERT INTO t1 VALUES(2, 'b2', 'c2');
|
|
|
|
INSERT INTO t1 VALUES(3, 'b3', 'c3');
|
|
|
|
INSERT INTO t1 VALUES(4, 'b4', 'c4');
|
|
|
|
CREATE INDEX i1 ON t1(a, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
proc f {val} {
|
|
|
|
lappend ::L $val
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
db func f f
|
|
|
|
|
|
|
|
do_test 1.1 {
|
|
|
|
set L [list]
|
|
|
|
execsql { SELECT * FROM t1 WHERE a=2 AND f(b) AND f(c) }
|
|
|
|
set L
|
|
|
|
} {c2}
|
|
|
|
|
|
|
|
do_test 1.2 {
|
|
|
|
set L [list]
|
|
|
|
execsql { SELECT * FROM t1 WHERE a=3 AND f(c) AND f(b) }
|
|
|
|
set L
|
|
|
|
} {c3}
|
|
|
|
|
|
|
|
do_execsql_test 1.3 {
|
|
|
|
DROP INDEX i1;
|
|
|
|
CREATE INDEX i1 ON t1(a, b);
|
|
|
|
}
|
|
|
|
do_test 1.4 {
|
|
|
|
set L [list]
|
|
|
|
execsql { SELECT * FROM t1 WHERE a=2 AND f(b) AND f(c) }
|
|
|
|
set L
|
|
|
|
} {b2}
|
|
|
|
|
|
|
|
do_test 1.5 {
|
|
|
|
set L [list]
|
|
|
|
execsql { SELECT * FROM t1 WHERE a=3 AND f(c) AND f(b) }
|
|
|
|
set L
|
|
|
|
} {b3}
|
2017-07-10 18:17:30 +03:00
|
|
|
|
|
|
|
#-----------------------------------------------
|
|
|
|
|
|
|
|
do_execsql_test 2.0 {
|
|
|
|
CREATE TABLE u1(a, b, c);
|
|
|
|
CREATE TABLE u2(x, y, z);
|
|
|
|
|
|
|
|
INSERT INTO u1 VALUES('a1', 'b1', 'c1');
|
|
|
|
INSERT INTO u2 VALUES('a1', 'b1', 'c1');
|
|
|
|
}
|
|
|
|
|
|
|
|
do_test 2.1 {
|
|
|
|
set L [list]
|
|
|
|
execsql {
|
|
|
|
SELECT * FROM u1 WHERE f('one')=123 AND 123=(
|
|
|
|
SELECT x FROM u2 WHERE x=a AND f('two')
|
|
|
|
)
|
|
|
|
}
|
|
|
|
set L
|
|
|
|
} {one}
|
|
|
|
|
|
|
|
do_test 2.2 {
|
|
|
|
set L [list]
|
|
|
|
execsql {
|
|
|
|
SELECT * FROM u1 WHERE 123=(
|
|
|
|
SELECT x FROM u2 WHERE x=a AND f('two')
|
|
|
|
) AND f('three')=123
|
|
|
|
}
|
|
|
|
set L
|
|
|
|
} {three}
|
|
|
|
|
2022-11-25 18:52:00 +03:00
|
|
|
# 2022-11-25 dbsqlfuzz crash-3a548de406a50e896c1bf7142692d35d339d697f
|
|
|
|
# Disable the push-down optimization for compound subqueries if any
|
|
|
|
# arm of the compound has an incompatible affinity.
|
|
|
|
#
|
|
|
|
reset_db
|
|
|
|
do_execsql_test 3.1 {
|
|
|
|
CREATE TABLE t0(c0 INT);
|
|
|
|
INSERT INTO t0 VALUES(0);
|
|
|
|
CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT);
|
2022-12-14 12:06:45 +03:00
|
|
|
INSERT INTO t1_a VALUES(1,'one');
|
2022-11-25 18:52:00 +03:00
|
|
|
CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT);
|
2022-12-14 12:06:45 +03:00
|
|
|
INSERT INTO t1_b VALUES(2,'two');
|
2022-11-25 18:52:00 +03:00
|
|
|
CREATE VIEW v0 AS SELECT CAST(t0.c0 AS INTEGER) AS c0 FROM t0;
|
2022-12-14 17:41:35 +03:00
|
|
|
CREATE VIEW v1(a,b) AS SELECT a, b FROM t1_a UNION ALL SELECT c, 0 FROM t1_b;
|
|
|
|
SELECT v1.a, quote(v1.b), t0.c0 AS cd FROM t0 LEFT JOIN v0 ON v0.c0!=0,v1;
|
2022-11-25 18:52:00 +03:00
|
|
|
} {
|
|
|
|
1 'one' 0
|
2022-12-14 12:06:45 +03:00
|
|
|
2 0 0
|
2022-11-25 18:52:00 +03:00
|
|
|
}
|
|
|
|
do_execsql_test 3.2 {
|
|
|
|
SELECT a, quote(b), cd FROM (
|
2022-12-14 17:41:35 +03:00
|
|
|
SELECT v1.a, v1.b, t0.c0 AS cd FROM t0 LEFT JOIN v0 ON v0.c0!=0, v1
|
2022-11-25 18:52:00 +03:00
|
|
|
) WHERE a=2 AND b='0' AND cd=0;
|
2022-12-14 12:06:45 +03:00
|
|
|
} {}
|
|
|
|
do_execsql_test 3.3 {
|
|
|
|
SELECT a, quote(b), cd FROM (
|
2022-12-14 17:41:35 +03:00
|
|
|
SELECT v1.a, v1.b, t0.c0 AS cd FROM t0 LEFT JOIN v0 ON v0.c0!=0, v1
|
|
|
|
) WHERE a=1 AND b='one' AND cd=0;
|
|
|
|
} {1 'one' 0}
|
|
|
|
do_execsql_test 3.4 {
|
|
|
|
SELECT a, quote(b), cd FROM (
|
|
|
|
SELECT v1.a, v1.b, t0.c0 AS cd FROM t0 LEFT JOIN v0 ON v0.c0!=0, v1
|
2022-12-14 12:06:45 +03:00
|
|
|
) WHERE a=2 AND b=0 AND cd=0;
|
2022-11-25 18:52:00 +03:00
|
|
|
} {
|
2022-12-14 12:06:45 +03:00
|
|
|
2 0 0
|
2022-11-25 18:52:00 +03:00
|
|
|
}
|
2023-02-22 23:42:15 +03:00
|
|
|
|
|
|
|
# 2023-02-22 https://sqlite.org/forum/forumpost/bcc4375032
|
|
|
|
# Performance regression caused by check-in [1ad41840c5e0fa70] from 2022-11-25.
|
|
|
|
# That check-in added a new restriction on push-down. The new restriction is
|
|
|
|
# no longer necessary after check-in [27655c9353620aa5] from 2022-12-14.
|
|
|
|
#
|
|
|
|
do_execsql_test 3.5 {
|
|
|
|
DROP TABLE IF EXISTS t1;
|
|
|
|
CREATE TABLE t1(a INT, b INT, c TEXT, PRIMARY KEY(a,b)) WITHOUT ROWID;
|
|
|
|
INSERT INTO t1(a,b,c) VALUES
|
|
|
|
(1,100,'abc'),
|
|
|
|
(2,200,'def'),
|
|
|
|
(3,300,'abc');
|
|
|
|
DROP TABLE IF EXISTS t2;
|
|
|
|
CREATE TABLE t2(a INT, b INT, c TEXT, PRIMARY KEY(a,b)) WITHOUT ROWID;
|
|
|
|
INSERT INTO t2(a,b,c) VALUES
|
|
|
|
(1,110,'efg'),
|
|
|
|
(2,200,'hij'),
|
|
|
|
(3,330,'klm');
|
|
|
|
CREATE VIEW v3 AS
|
|
|
|
SELECT a, b, c FROM t1
|
|
|
|
UNION ALL
|
|
|
|
SELECT a, b, 'xyz' FROM t2;
|
|
|
|
SELECT * FROM v3 WHERE a=2 AND b=200;
|
|
|
|
} {2 200 def 2 200 xyz}
|
|
|
|
do_eqp_test 3.6 {
|
|
|
|
SELECT * FROM v3 WHERE a=2 AND b=200;
|
|
|
|
} {
|
|
|
|
QUERY PLAN
|
|
|
|
|--CO-ROUTINE v3
|
|
|
|
| `--COMPOUND QUERY
|
|
|
|
| |--LEFT-MOST SUBQUERY
|
|
|
|
| | `--SEARCH t1 USING PRIMARY KEY (a=? AND b=?)
|
|
|
|
| `--UNION ALL
|
|
|
|
| `--SEARCH t2 USING PRIMARY KEY (a=? AND b=?)
|
|
|
|
`--SCAN v3
|
|
|
|
}
|
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
# We want both arms of the compound subquery to use the
|
|
|
|
# primary key.
|
|
|
|
|
2017-04-28 22:59:55 +03:00
|
|
|
finish_test
|