in3.test 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. # 2007 November 29
  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. # This file tests the optimisations made in November 2007 of expressions
  12. # of the following form:
  13. #
  14. # <value> IN (SELECT <column> FROM <table>)
  15. #
  16. # $Id: in3.test,v 1.5 2008/08/04 03:51:24 danielk1977 Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. ifcapable !subquery {
  20. finish_test
  21. return
  22. }
  23. # Return the number of OpenEphemeral instructions used in the
  24. # implementation of the sql statement passed as a an argument.
  25. #
  26. proc nEphemeral {sql} {
  27. set nEph 0
  28. foreach op [execsql "EXPLAIN $sql"] {
  29. if {$op eq "OpenEphemeral"} {incr nEph}
  30. }
  31. set nEph
  32. }
  33. # This proc works the same way as execsql, except that the number
  34. # of OpenEphemeral instructions used in the implementation of the
  35. # statement is inserted into the start of the returned list.
  36. #
  37. proc exec_neph {sql} {
  38. return [concat [nEphemeral $sql] [execsql $sql]]
  39. }
  40. do_test in3-1.1 {
  41. execsql {
  42. CREATE TABLE t1(a PRIMARY KEY, b);
  43. INSERT INTO t1 VALUES(1, 2);
  44. INSERT INTO t1 VALUES(3, 4);
  45. INSERT INTO t1 VALUES(5, 6);
  46. }
  47. } {}
  48. # All of these queries should avoid using a temp-table:
  49. #
  50. do_test in3-1.2 {
  51. exec_neph { SELECT rowid FROM t1 WHERE rowid IN (SELECT rowid FROM t1); }
  52. } {0 1 2 3}
  53. do_test in3-1.3 {
  54. exec_neph { SELECT a FROM t1 WHERE a IN (SELECT a FROM t1); }
  55. } {0 1 3 5}
  56. do_test in3-1.4 {
  57. exec_neph { SELECT rowid FROM t1 WHERE rowid+0 IN (SELECT rowid FROM t1); }
  58. } {0 1 2 3}
  59. do_test in3-1.5 {
  60. exec_neph { SELECT a FROM t1 WHERE a+0 IN (SELECT a FROM t1); }
  61. } {0 1 3 5}
  62. # Because none of the sub-select queries in the following statements
  63. # match the pattern ("SELECT <column> FROM <table>"), the following do
  64. # require a temp table.
  65. #
  66. do_test in3-1.6 {
  67. exec_neph { SELECT rowid FROM t1 WHERE rowid IN (SELECT rowid+0 FROM t1); }
  68. } {1 1 2 3}
  69. do_test in3-1.7 {
  70. exec_neph { SELECT a FROM t1 WHERE a IN (SELECT a+0 FROM t1); }
  71. } {1 1 3 5}
  72. do_test in3-1.8 {
  73. exec_neph { SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 WHERE 1); }
  74. } {1 1 3 5}
  75. do_test in3-1.9 {
  76. exec_neph { SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 GROUP BY a); }
  77. } {1 1 3 5}
  78. # This should not use a temp-table. Even though the sub-select does
  79. # not exactly match the pattern "SELECT <column> FROM <table>", in
  80. # this case the ORDER BY is a no-op and can be ignored.
  81. do_test in3-1.10 {
  82. exec_neph { SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 ORDER BY a); }
  83. } {0 1 3 5}
  84. # These do use the temp-table. Adding the LIMIT clause means the
  85. # ORDER BY cannot be ignored.
  86. do_test in3-1.11 {
  87. exec_neph {SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 ORDER BY a LIMIT 1)}
  88. } {1 1}
  89. do_test in3-1.12 {
  90. exec_neph {
  91. SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 ORDER BY a LIMIT 1 OFFSET 1)
  92. }
  93. } {1 3}
  94. # Has to use a temp-table because of the compound sub-select.
  95. #
  96. ifcapable compound {
  97. do_test in3-1.13 {
  98. exec_neph {
  99. SELECT a FROM t1 WHERE a IN (
  100. SELECT a FROM t1 UNION ALL SELECT a FROM t1
  101. )
  102. }
  103. } {1 1 3 5}
  104. }
  105. # The first of these queries has to use the temp-table, because the
  106. # collation sequence used for the index on "t1.a" does not match the
  107. # collation sequence used by the "IN" comparison. The second does not
  108. # require a temp-table, because the collation sequences match.
  109. #
  110. do_test in3-1.14 {
  111. exec_neph { SELECT a FROM t1 WHERE a COLLATE nocase IN (SELECT a FROM t1) }
  112. } {1 1 3 5}
  113. do_test in3-1.15 {
  114. exec_neph { SELECT a FROM t1 WHERE a COLLATE binary IN (SELECT a FROM t1) }
  115. } {0 1 3 5}
  116. # Neither of these queries require a temp-table. The collation sequence
  117. # makes no difference when using a rowid.
  118. #
  119. do_test in3-1.16 {
  120. exec_neph {SELECT a FROM t1 WHERE a COLLATE nocase IN (SELECT rowid FROM t1)}
  121. } {0 1 3}
  122. do_test in3-1.17 {
  123. exec_neph {SELECT a FROM t1 WHERE a COLLATE binary IN (SELECT rowid FROM t1)}
  124. } {0 1 3}
  125. # The following tests - in3.2.* - test a bug that was difficult to track
  126. # down during development. They are not particularly well focused.
  127. #
  128. do_test in3-2.1 {
  129. execsql {
  130. DROP TABLE IF EXISTS t1;
  131. CREATE TABLE t1(w int, x int, y int);
  132. CREATE TABLE t2(p int, q int, r int, s int);
  133. }
  134. for {set i 1} {$i<=100} {incr i} {
  135. set w $i
  136. set x [expr {int(log($i)/log(2))}]
  137. set y [expr {$i*$i + 2*$i + 1}]
  138. execsql "INSERT INTO t1 VALUES($w,$x,$y)"
  139. }
  140. set maxy [execsql {select max(y) from t1}]
  141. db eval { INSERT INTO t2 SELECT 101-w, x, $maxy+1-y, y FROM t1 }
  142. } {}
  143. do_test in3-2.2 {
  144. execsql {
  145. SELECT rowid
  146. FROM t1
  147. WHERE rowid IN (SELECT rowid FROM t1 WHERE rowid IN (1, 2));
  148. }
  149. } {1 2}
  150. do_test in3-2.3 {
  151. execsql {
  152. select rowid from t1 where rowid IN (-1,2,4)
  153. }
  154. } {2 4}
  155. do_test in3-2.4 {
  156. execsql {
  157. SELECT rowid FROM t1 WHERE rowid IN
  158. (select rowid from t1 where rowid IN (-1,2,4))
  159. }
  160. } {2 4}
  161. #-------------------------------------------------------------------------
  162. # This next block of tests - in3-3.* - verify that column affinity is
  163. # correctly handled in cases where an index might be used to optimise
  164. # an IN (SELECT) expression.
  165. #
  166. do_test in3-3.1 {
  167. catch {execsql {
  168. DROP TABLE t1;
  169. DROP TABLE t2;
  170. }}
  171. execsql {
  172. CREATE TABLE t1(a BLOB, b NUMBER ,c TEXT);
  173. CREATE UNIQUE INDEX t1_i1 ON t1(a); /* no affinity */
  174. CREATE UNIQUE INDEX t1_i2 ON t1(b); /* numeric affinity */
  175. CREATE UNIQUE INDEX t1_i3 ON t1(c); /* text affinity */
  176. CREATE TABLE t2(x BLOB, y NUMBER, z TEXT);
  177. CREATE UNIQUE INDEX t2_i1 ON t2(x); /* no affinity */
  178. CREATE UNIQUE INDEX t2_i2 ON t2(y); /* numeric affinity */
  179. CREATE UNIQUE INDEX t2_i3 ON t2(z); /* text affinity */
  180. INSERT INTO t1 VALUES(1, 1, 1);
  181. INSERT INTO t2 VALUES('1', '1', '1');
  182. }
  183. } {}
  184. do_test in3-3.2 {
  185. # No affinity is applied before comparing "x" and "a". Therefore
  186. # the index can be used (the comparison is false, text!=number).
  187. exec_neph { SELECT x IN (SELECT a FROM t1) FROM t2 }
  188. } {0 0}
  189. do_test in3-3.3 {
  190. # Logically, numeric affinity is applied to both sides before
  191. # the comparison. Therefore it is possible to use index t1_i2.
  192. exec_neph { SELECT x IN (SELECT b FROM t1) FROM t2 }
  193. } {0 1}
  194. do_test in3-3.4 {
  195. # No affinity is applied before the comparison takes place. Making
  196. # it possible to use index t1_i3.
  197. exec_neph { SELECT x IN (SELECT c FROM t1) FROM t2 }
  198. } {0 1}
  199. do_test in3-3.5 {
  200. # Numeric affinity should be applied to each side before the comparison
  201. # takes place. Therefore we cannot use index t1_i1, which has no affinity.
  202. exec_neph { SELECT y IN (SELECT a FROM t1) FROM t2 }
  203. } {1 1}
  204. do_test in3-3.6 {
  205. # Numeric affinity is applied to both sides before
  206. # the comparison. Therefore it is possible to use index t1_i2.
  207. exec_neph { SELECT y IN (SELECT b FROM t1) FROM t2 }
  208. } {0 1}
  209. do_test in3-3.7 {
  210. # Numeric affinity is applied before the comparison takes place.
  211. # Making it impossible to use index t1_i3.
  212. exec_neph { SELECT y IN (SELECT c FROM t1) FROM t2 }
  213. } {1 1}
  214. #---------------------------------------------------------------------
  215. #
  216. # Test using a multi-column index.
  217. #
  218. do_test in3-4.1 {
  219. execsql {
  220. CREATE TABLE t3(a, b, c);
  221. CREATE UNIQUE INDEX t3_i ON t3(b, a);
  222. }
  223. execsql {
  224. INSERT INTO t3 VALUES(1, 'numeric', 2);
  225. INSERT INTO t3 VALUES(2, 'text', 2);
  226. INSERT INTO t3 VALUES(3, 'real', 2);
  227. INSERT INTO t3 VALUES(4, 'none', 2);
  228. }
  229. } {}
  230. do_test in3-4.2 {
  231. exec_neph { SELECT 'text' IN (SELECT b FROM t3) }
  232. } {0 1}
  233. do_test in3-4.3 {
  234. exec_neph { SELECT 'TEXT' COLLATE nocase IN (SELECT b FROM t3) }
  235. } {1 1}
  236. do_test in3-4.4 {
  237. # A temp table must be used because t3_i.b is not guaranteed to be unique.
  238. exec_neph { SELECT b FROM t3 WHERE b IN (SELECT b FROM t3) }
  239. } {1 none numeric real text}
  240. do_test in3-4.5 {
  241. execsql { CREATE UNIQUE INDEX t3_i2 ON t3(b) }
  242. exec_neph { SELECT b FROM t3 WHERE b IN (SELECT b FROM t3) }
  243. } {0 none numeric real text}
  244. do_test in3-4.6 {
  245. execsql { DROP INDEX t3_i2 }
  246. } {}
  247. # The following two test cases verify that ticket #2991 has been fixed.
  248. #
  249. do_test in3-5.1 {
  250. execsql {
  251. CREATE TABLE Folders(
  252. folderid INTEGER PRIMARY KEY,
  253. parentid INTEGER,
  254. rootid INTEGER,
  255. path VARCHAR(255)
  256. );
  257. }
  258. } {}
  259. do_test in3-5.2 {
  260. catchsql {
  261. DELETE FROM Folders WHERE folderid IN
  262. (SELECT folderid FROM Folder WHERE path LIKE 'C:\MP3\Albums\' || '%');
  263. }
  264. } {1 {no such table: Folder}}
  265. finish_test