blob: 45ee7905c3adf01c4c2fbd29cc7310aada73a21f [file] [log] [blame]
drh7c2fbde2009-01-07 20:58:57 +00001# 2008 August 01
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11#
12# This test script checks malloc failures in WHERE clause analysis.
13#
drh103bd882009-01-08 21:00:02 +000014# $Id: mallocK.test,v 1.3 2009/01/08 21:00:03 drh Exp $
drh7c2fbde2009-01-07 20:58:57 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18source $testdir/malloc_common.tcl
dan6696ba32014-06-28 19:06:49 +000019set testprefix mallocK
drh7c2fbde2009-01-07 20:58:57 +000020
drh7c2fbde2009-01-07 20:58:57 +000021set sql {SELECT * FROM t1, t2 WHERE (a=1 OR a=2)}
22for {set x 1} {$x<5} {incr x} {
23 append sql " AND b=y"
24 do_malloc_test mallocK-1.$x -sqlbody $sql -sqlprep {
25 CREATE TABLE t1(a,b);
26 CREATE TABLE t2(x,y);
27 }
28}
29
drh103bd882009-01-08 21:00:02 +000030set sql {SELECT * FROM t1 WHERE a GLOB 'xyz*' AND (a=1 OR a=2)}
drh7c2fbde2009-01-07 20:58:57 +000031for {set x 1} {$x<5} {incr x} {
32 append sql " AND b!=$x"
33 do_malloc_test mallocK-2.$x -sqlbody $sql -sqlprep {
34 CREATE TABLE t1(a,b);
35 }
36}
37
38set sql {SELECT * FROM t1 WHERE a BETWEEN 5 AND 10}
39for {set x 1} {$x<5} {incr x} {
drh103bd882009-01-08 21:00:02 +000040 append sql " AND b=$x"
drh7c2fbde2009-01-07 20:58:57 +000041 do_malloc_test mallocK-3.$x -sqlbody $sql -sqlprep {
42 CREATE TABLE t1(a,b);
43 }
44}
45
drh103bd882009-01-08 21:00:02 +000046set sql {SELECT * FROM t1 WHERE b=0}
47for {set x 1} {$x<5} {incr x} {
48 set term "(b=$x"
49 for {set y 0} {$y<$x} {incr y} {
50 append term " AND a!=$y"
51 }
52 append sql " OR $term)"
53 do_malloc_test mallocK-4.$x -sqlbody $sql -sqlprep {
54 CREATE TABLE t1(a,b);
55 }
56}
57
drh7c2fbde2009-01-07 20:58:57 +000058ifcapable vtab {
59 set sql {SELECT * FROM t2 WHERE a MATCH 'xyz'}
60 for {set x 1} {$x<5} {incr x} {
61 append sql " AND b!=$x"
drh103bd882009-01-08 21:00:02 +000062 do_malloc_test mallocK-5.$x -sqlbody $sql -tclprep {
drh7c2fbde2009-01-07 20:58:57 +000063 register_echo_module [sqlite3_connection_pointer db]
64 db eval {
65 CREATE TABLE t1(a,b);
66 CREATE VIRTUAL TABLE t2 USING echo(t1);
67 }
68 }
69 }
70}
71
dan6696ba32014-06-28 19:06:49 +000072#-------------------------------------------------------------------------
73# Test that OOM errors are correctly handled by the code that uses stat4
74# data to estimate the number of rows visited by a skip-scan range query.
75#
76add_alignment_test_collations db
77do_execsql_test 6.0 {
78 CREATE TABLE t3(a TEXT, b TEXT COLLATE utf16_aligned, c);
79 INSERT INTO t3 VALUES('one', '.....', 0);
80 INSERT INTO t3 VALUES('one', '....x', 1);
81 INSERT INTO t3 VALUES('one', '...x.', 2);
82 INSERT INTO t3 VALUES('one', '...xx', 3);
83 INSERT INTO t3 VALUES('one', '..x..', 4);
84 INSERT INTO t3 VALUES('one', '..x.x', 5);
85 INSERT INTO t3 VALUES('one', '..xx.', 6);
86 INSERT INTO t3 VALUES('one', '..xxx', 7);
87 INSERT INTO t3 VALUES('one', '.x...', 8);
88 INSERT INTO t3 VALUES('one', '.x..x', 9);
89 INSERT INTO t3 VALUES('one', '.x.x.', 10);
90 INSERT INTO t3 VALUES('one', '.x.xx', 11);
91 INSERT INTO t3 VALUES('one', '.xx..', 12);
92 INSERT INTO t3 VALUES('one', '.xx.x', 13);
93 INSERT INTO t3 VALUES('one', '.xxx.', 14);
94 INSERT INTO t3 VALUES('one', '.xxxx', 15);
95
96 INSERT INTO t3 VALUES('two', 'x....', 16);
97 INSERT INTO t3 VALUES('two', 'x...x', 17);
98 INSERT INTO t3 VALUES('two', 'x..x.', 18);
99 INSERT INTO t3 VALUES('two', 'x..xx', 19);
100 INSERT INTO t3 VALUES('two', 'x.x..', 20);
101 INSERT INTO t3 VALUES('two', 'x.x.x', 21);
102 INSERT INTO t3 VALUES('two', 'x.xx.', 22);
103 INSERT INTO t3 VALUES('two', 'x.xxx', 23);
104 INSERT INTO t3 VALUES('two', 'xx...', 24);
105 INSERT INTO t3 VALUES('two', 'xx..x', 25);
106 INSERT INTO t3 VALUES('two', 'xx.x.', 26);
107 INSERT INTO t3 VALUES('two', 'xx.xx', 27);
108 INSERT INTO t3 VALUES('two', 'xxx..', 28);
109 INSERT INTO t3 VALUES('two', 'xxx.x', 29);
110 INSERT INTO t3 VALUES('two', 'xxxx.', 30);
111 INSERT INTO t3 VALUES('two', 'xxxxx', 31);
112
113 INSERT INTO t3 SELECT * FROM t3;
114
115 CREATE INDEX i3 ON t3(a, b);
116 ANALYZE;
117
118 SELECT 'x' > '.';
119} {1}
120
121ifcapable stat4 {
122 do_eqp_test 6.1 {
123 SELECT DISTINCT c FROM t3 WHERE b BETWEEN '.xx..' AND '.xxxx';
124 } {
125 0 0 0 {SEARCH TABLE t3 USING INDEX i3 (ANY(a) AND b>? AND b<?)}
126 0 0 0 {USE TEMP B-TREE FOR DISTINCT}
127 }
128}
129
130do_faultsim_test 6 -faults oom* -body {
131 db cache flush
132 db eval { SELECT DISTINCT c FROM t3 WHERE b BETWEEN '.xx..' AND '.xxxx' }
133} -test {
134 faultsim_test_result {0 {12 13 14 15}}
135}
drh7c2fbde2009-01-07 20:58:57 +0000136
dance8f53d2015-01-21 17:00:57 +0000137do_execsql_test 7.1 {
138 CREATE TABLE x1(a INTEGER PRIMARY KEY, b);
139}
140do_faultsim_test 7.2 -faults oom* -body {
141 execsql { SELECT * FROM x1 WHERE a = (SELECT 1) }
142} -test {
143 faultsim_test_result [list 0 {}]
144}
145
dan3df30592015-03-13 08:31:54 +0000146reset_db
147
148proc isqrt {i} { expr { int(sqrt($i)) } }
149db func isqrt isqrt
150
151do_execsql_test 8.0 {
152 PRAGMA encoding = 'utf-16';
153 CREATE TABLE x2(x TEXT, y TEXT);
154 WITH data(i) AS (
155 SELECT 1 UNION ALL SELECT i+1 FROM data
156 )
157 INSERT INTO x2 SELECT isqrt(i), isqrt(i) FROM data LIMIT 400;
158 CREATE INDEX x2x ON x2(x);
159 CREATE INDEX x2y ON x2(y);
160 ANALYZE;
161 DELETE FROM x2;
162}
163
164proc str {a} { return $a }
165db func str -deterministic str
166
167do_faultsim_test 8 -faults oom* -body {
168 execsql { SELECT * FROM x2 WHERE x = str('19') AND y = str('4') }
169} -test {
170 faultsim_test_result [list 0 {}]
171}
172
dance8f53d2015-01-21 17:00:57 +0000173
drh7c2fbde2009-01-07 20:58:57 +0000174finish_test