xref: /sqlite-3.40.0/test/mallocK.test (revision 8210233c)
17c2fbdebSdrh# 2008 August 01
27c2fbdebSdrh#
37c2fbdebSdrh# The author disclaims copyright to this source code.  In place of
47c2fbdebSdrh# a legal notice, here is a blessing:
57c2fbdebSdrh#
67c2fbdebSdrh#    May you do good and not evil.
77c2fbdebSdrh#    May you find forgiveness for yourself and forgive others.
87c2fbdebSdrh#    May you share freely, never taking more than you give.
97c2fbdebSdrh#
107c2fbdebSdrh#***********************************************************************
117c2fbdebSdrh#
127c2fbdebSdrh# This test script checks malloc failures in WHERE clause analysis.
137c2fbdebSdrh#
14103bd88cSdrh# $Id: mallocK.test,v 1.3 2009/01/08 21:00:03 drh Exp $
157c2fbdebSdrh
167c2fbdebSdrhset testdir [file dirname $argv0]
177c2fbdebSdrhsource $testdir/tester.tcl
187c2fbdebSdrhsource $testdir/malloc_common.tcl
196696ba3eSdanset testprefix mallocK
207c2fbdebSdrh
217c2fbdebSdrhset sql {SELECT * FROM t1, t2 WHERE (a=1 OR a=2)}
227c2fbdebSdrhfor {set x 1} {$x<5} {incr x} {
237c2fbdebSdrh  append sql " AND b=y"
247c2fbdebSdrh  do_malloc_test mallocK-1.$x -sqlbody $sql -sqlprep {
257c2fbdebSdrh    CREATE TABLE t1(a,b);
267c2fbdebSdrh    CREATE TABLE t2(x,y);
277c2fbdebSdrh  }
287c2fbdebSdrh}
297c2fbdebSdrh
30103bd88cSdrhset sql {SELECT * FROM t1 WHERE a GLOB 'xyz*' AND (a=1 OR a=2)}
317c2fbdebSdrhfor {set x 1} {$x<5} {incr x} {
327c2fbdebSdrh  append sql " AND b!=$x"
337c2fbdebSdrh  do_malloc_test mallocK-2.$x -sqlbody $sql -sqlprep {
347c2fbdebSdrh    CREATE TABLE t1(a,b);
357c2fbdebSdrh  }
367c2fbdebSdrh}
377c2fbdebSdrh
387c2fbdebSdrhset sql {SELECT * FROM t1 WHERE a BETWEEN 5 AND 10}
397c2fbdebSdrhfor {set x 1} {$x<5} {incr x} {
40103bd88cSdrh  append sql " AND b=$x"
417c2fbdebSdrh  do_malloc_test mallocK-3.$x -sqlbody $sql -sqlprep {
427c2fbdebSdrh    CREATE TABLE t1(a,b);
437c2fbdebSdrh  }
447c2fbdebSdrh}
457c2fbdebSdrh
46103bd88cSdrhset sql {SELECT * FROM t1 WHERE b=0}
47103bd88cSdrhfor {set x 1} {$x<5} {incr x} {
48103bd88cSdrh  set term "(b=$x"
49103bd88cSdrh  for {set y 0} {$y<$x} {incr y} {
50103bd88cSdrh    append term " AND a!=$y"
51103bd88cSdrh  }
52103bd88cSdrh  append sql " OR $term)"
53103bd88cSdrh  do_malloc_test mallocK-4.$x -sqlbody $sql -sqlprep {
54103bd88cSdrh    CREATE TABLE t1(a,b);
55103bd88cSdrh  }
56103bd88cSdrh}
57103bd88cSdrh
587c2fbdebSdrhifcapable vtab {
597c2fbdebSdrh  set sql {SELECT * FROM t2 WHERE a MATCH 'xyz'}
607c2fbdebSdrh  for {set x 1} {$x<5} {incr x} {
617c2fbdebSdrh    append sql " AND b!=$x"
62103bd88cSdrh    do_malloc_test mallocK-5.$x -sqlbody $sql -tclprep {
637c2fbdebSdrh      register_echo_module [sqlite3_connection_pointer db]
647c2fbdebSdrh      db eval {
657c2fbdebSdrh        CREATE TABLE t1(a,b);
667c2fbdebSdrh        CREATE VIRTUAL TABLE t2 USING echo(t1);
677c2fbdebSdrh      }
687c2fbdebSdrh    }
697c2fbdebSdrh  }
707c2fbdebSdrh}
717c2fbdebSdrh
726696ba3eSdan#-------------------------------------------------------------------------
736696ba3eSdan# Test that OOM errors are correctly handled by the code that uses stat4
746696ba3eSdan# data to estimate the number of rows visited by a skip-scan range query.
756696ba3eSdan#
766696ba3eSdanadd_alignment_test_collations db
776696ba3eSdando_execsql_test 6.0 {
786696ba3eSdan  CREATE TABLE t3(a TEXT, b TEXT COLLATE utf16_aligned, c);
796696ba3eSdan  INSERT INTO t3 VALUES('one', '.....', 0);
806696ba3eSdan  INSERT INTO t3 VALUES('one', '....x', 1);
816696ba3eSdan  INSERT INTO t3 VALUES('one', '...x.', 2);
826696ba3eSdan  INSERT INTO t3 VALUES('one', '...xx', 3);
836696ba3eSdan  INSERT INTO t3 VALUES('one', '..x..', 4);
846696ba3eSdan  INSERT INTO t3 VALUES('one', '..x.x', 5);
856696ba3eSdan  INSERT INTO t3 VALUES('one', '..xx.', 6);
866696ba3eSdan  INSERT INTO t3 VALUES('one', '..xxx', 7);
876696ba3eSdan  INSERT INTO t3 VALUES('one', '.x...', 8);
886696ba3eSdan  INSERT INTO t3 VALUES('one', '.x..x', 9);
896696ba3eSdan  INSERT INTO t3 VALUES('one', '.x.x.', 10);
906696ba3eSdan  INSERT INTO t3 VALUES('one', '.x.xx', 11);
916696ba3eSdan  INSERT INTO t3 VALUES('one', '.xx..', 12);
926696ba3eSdan  INSERT INTO t3 VALUES('one', '.xx.x', 13);
936696ba3eSdan  INSERT INTO t3 VALUES('one', '.xxx.', 14);
946696ba3eSdan  INSERT INTO t3 VALUES('one', '.xxxx', 15);
956696ba3eSdan
966696ba3eSdan  INSERT INTO t3 VALUES('two', 'x....', 16);
976696ba3eSdan  INSERT INTO t3 VALUES('two', 'x...x', 17);
986696ba3eSdan  INSERT INTO t3 VALUES('two', 'x..x.', 18);
996696ba3eSdan  INSERT INTO t3 VALUES('two', 'x..xx', 19);
1006696ba3eSdan  INSERT INTO t3 VALUES('two', 'x.x..', 20);
1016696ba3eSdan  INSERT INTO t3 VALUES('two', 'x.x.x', 21);
1026696ba3eSdan  INSERT INTO t3 VALUES('two', 'x.xx.', 22);
1036696ba3eSdan  INSERT INTO t3 VALUES('two', 'x.xxx', 23);
1046696ba3eSdan  INSERT INTO t3 VALUES('two', 'xx...', 24);
1056696ba3eSdan  INSERT INTO t3 VALUES('two', 'xx..x', 25);
1066696ba3eSdan  INSERT INTO t3 VALUES('two', 'xx.x.', 26);
1076696ba3eSdan  INSERT INTO t3 VALUES('two', 'xx.xx', 27);
1086696ba3eSdan  INSERT INTO t3 VALUES('two', 'xxx..', 28);
1096696ba3eSdan  INSERT INTO t3 VALUES('two', 'xxx.x', 29);
1106696ba3eSdan  INSERT INTO t3 VALUES('two', 'xxxx.', 30);
1116696ba3eSdan  INSERT INTO t3 VALUES('two', 'xxxxx', 31);
1126696ba3eSdan
1136696ba3eSdan  INSERT INTO t3 SELECT * FROM t3;
1146696ba3eSdan
1156696ba3eSdan  CREATE INDEX i3 ON t3(a, b);
1166696ba3eSdan  ANALYZE;
1176696ba3eSdan
1186696ba3eSdan  SELECT 'x' > '.';
1196696ba3eSdan} {1}
1206696ba3eSdan
1216696ba3eSdanifcapable stat4 {
1226696ba3eSdan  do_eqp_test 6.1 {
1236696ba3eSdan    SELECT DISTINCT c FROM t3 WHERE b BETWEEN '.xx..' AND '.xxxx';
124b3f0276bSdrh  } [string map {"\n  " \n} {
125b3f0276bSdrh    QUERY PLAN
126*8210233cSdrh    |--SEARCH t3 USING INDEX i3 (ANY(a) AND b>? AND b<?)
127b3f0276bSdrh    `--USE TEMP B-TREE FOR DISTINCT
128b3f0276bSdrh  }]
1296696ba3eSdan}
1306696ba3eSdan
1316696ba3eSdando_faultsim_test 6 -faults oom* -body {
1326696ba3eSdan  db cache flush
1336696ba3eSdan  db eval { SELECT DISTINCT c FROM t3 WHERE b BETWEEN '.xx..' AND '.xxxx' }
1346696ba3eSdan} -test {
1356696ba3eSdan  faultsim_test_result {0 {12 13 14 15}}
1366696ba3eSdan}
1377c2fbdebSdrh
138ce8f53d4Sdando_execsql_test 7.1 {
139ce8f53d4Sdan  CREATE TABLE x1(a INTEGER PRIMARY KEY, b);
140ce8f53d4Sdan}
141ce8f53d4Sdando_faultsim_test 7.2 -faults oom* -body {
142ce8f53d4Sdan  execsql { SELECT * FROM x1 WHERE a = (SELECT 1) }
143ce8f53d4Sdan} -test {
144ce8f53d4Sdan  faultsim_test_result [list 0 {}]
145ce8f53d4Sdan}
146ce8f53d4Sdan
1473df30592Sdanreset_db
1483df30592Sdan
1493df30592Sdanproc isqrt {i} { expr { int(sqrt($i)) } }
1503df30592Sdandb func isqrt isqrt
1513df30592Sdan
1523df30592Sdando_execsql_test 8.0 {
1533df30592Sdan  PRAGMA encoding = 'utf-16';
1543df30592Sdan  CREATE TABLE x2(x TEXT, y TEXT);
1553df30592Sdan  WITH data(i) AS (
1563df30592Sdan    SELECT 1 UNION ALL SELECT i+1 FROM data
1573df30592Sdan  )
1583df30592Sdan  INSERT INTO x2 SELECT isqrt(i), isqrt(i) FROM data LIMIT 400;
1593df30592Sdan  CREATE INDEX x2x ON x2(x);
1603df30592Sdan  CREATE INDEX x2y ON x2(y);
1613df30592Sdan  ANALYZE;
1623df30592Sdan  DELETE FROM x2;
1633df30592Sdan}
1643df30592Sdan
1653df30592Sdanproc str {a} { return $a }
1663df30592Sdandb func str -deterministic str
1673df30592Sdan
1683df30592Sdando_faultsim_test 8 -faults oom* -body {
1693df30592Sdan  execsql { SELECT * FROM x2 WHERE x = str('19') AND y = str('4') }
1703df30592Sdan} -test {
1713df30592Sdan  faultsim_test_result [list 0 {}]
1723df30592Sdan}
1733df30592Sdan
174ce8f53d4Sdan
1757c2fbdebSdrhfinish_test
176