tkt-80ba201079.test 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # 2010 December 6
  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 implements regression tests for SQLite library. Specifically,
  12. # it tests that ticket [80ba201079ea608071d22a57856b940ea3ac53ce] is
  13. # resolved. That ticket is about an incorrect result that appears when
  14. # an index is added. The root cause is that a constant is being used
  15. # without initialization when the OR optimization applies in the WHERE clause.
  16. #
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. set ::testprefix tkt-80ba201079
  20. do_test tkt-80ba2-100 {
  21. db eval {
  22. CREATE TABLE t1(a);
  23. INSERT INTO t1 VALUES('A');
  24. CREATE TABLE t2(b);
  25. INSERT INTO t2 VALUES('B');
  26. CREATE TABLE t3(c);
  27. INSERT INTO t3 VALUES('C');
  28. SELECT * FROM t1, t2
  29. WHERE (a='A' AND b='X')
  30. OR (a='A' AND EXISTS (SELECT * FROM t3 WHERE c='C'));
  31. }
  32. } {A B}
  33. do_test tkt-80ba2-101 {
  34. db eval {
  35. CREATE INDEX i1 ON t1(a);
  36. SELECT * FROM t1, t2
  37. WHERE (a='A' AND b='X')
  38. OR (a='A' AND EXISTS (SELECT * FROM t3 WHERE c='C'));
  39. }
  40. } {A B}
  41. do_test tkt-80ba2-102 {
  42. optimization_control db factor-constants 0
  43. db cache flush
  44. db eval {
  45. SELECT * FROM t1, t2
  46. WHERE (a='A' AND b='X')
  47. OR (a='A' AND EXISTS (SELECT * FROM t3 WHERE c='C'));
  48. }
  49. } {A B}
  50. optimization_control db all 1
  51. # Verify that the optimization_control command is actually working
  52. #
  53. do_test tkt-80ba2-150 {
  54. optimization_control db factor-constants 1
  55. db cache flush
  56. set x1 [db eval {EXPLAIN
  57. SELECT * FROM t1, t2
  58. WHERE (a='A' AND b='X')
  59. OR (a='A' AND EXISTS (SELECT * FROM t3 WHERE c='C'));}]
  60. optimization_control db factor-constants 0
  61. db cache flush
  62. set x2 [db eval {EXPLAIN
  63. SELECT * FROM t1, t2
  64. WHERE (a='A' AND b='X')
  65. OR (a='A' AND EXISTS (SELECT * FROM t3 WHERE c='C'));}]
  66. expr {$x1==$x2}
  67. } {0}
  68. do_test tkt-80ba2-200 {
  69. db eval {
  70. CREATE TABLE entry_types (
  71. id integer primary key,
  72. name text
  73. );
  74. INSERT INTO "entry_types" VALUES(100,'cli_command');
  75. INSERT INTO "entry_types" VALUES(300,'object_change');
  76. CREATE TABLE object_changes (
  77. change_id integer primary key,
  78. system_id int,
  79. obj_id int,
  80. obj_context text,
  81. change_type int,
  82. command_id int
  83. );
  84. INSERT INTO "object_changes" VALUES(1551,1,114608,'exported_pools',1,2114);
  85. INSERT INTO "object_changes" VALUES(2048,1,114608,'exported_pools',2,2319);
  86. CREATE TABLE timeline (
  87. rowid integer primary key,
  88. timestamp text,
  89. system_id int,
  90. entry_type int,
  91. entry_id int
  92. );
  93. INSERT INTO "timeline" VALUES(6735,'2010-11-21 17:08:27.000',1,300,2048);
  94. INSERT INTO "timeline" VALUES(6825,'2010-11-21 17:09:21.000',1,300,2114);
  95. SELECT entry_type,
  96. entry_types.name,
  97. entry_id
  98. FROM timeline JOIN entry_types ON entry_type = entry_types.id
  99. WHERE (entry_types.name = 'cli_command' AND entry_id=2114)
  100. OR (entry_types.name = 'object_change'
  101. AND entry_id IN (SELECT change_id
  102. FROM object_changes
  103. WHERE obj_context = 'exported_pools'));
  104. }
  105. } {300 object_change 2048}
  106. do_test tkt-80ba2-201 {
  107. db eval {
  108. CREATE INDEX timeline_entry_id_idx on timeline(entry_id);
  109. SELECT entry_type,
  110. entry_types.name,
  111. entry_id
  112. FROM timeline JOIN entry_types ON entry_type = entry_types.id
  113. WHERE (entry_types.name = 'cli_command' AND entry_id=2114)
  114. OR (entry_types.name = 'object_change'
  115. AND entry_id IN (SELECT change_id
  116. FROM object_changes
  117. WHERE obj_context = 'exported_pools'));
  118. }
  119. } {300 object_change 2048}
  120. do_test tkt-80ba2-202 {
  121. optimization_control db factor-constants 0
  122. db cache flush
  123. db eval {
  124. SELECT entry_type,
  125. entry_types.name,
  126. entry_id
  127. FROM timeline JOIN entry_types ON entry_type = entry_types.id
  128. WHERE (entry_types.name = 'cli_command' AND entry_id=2114)
  129. OR (entry_types.name = 'object_change'
  130. AND entry_id IN (SELECT change_id
  131. FROM object_changes
  132. WHERE obj_context = 'exported_pools'));
  133. }
  134. } {300 object_change 2048}
  135. #-------------------------------------------------------------------------
  136. #
  137. drop_all_tables
  138. do_execsql_test 301 {
  139. CREATE TABLE t1(a, b, c);
  140. CREATE INDEX i1 ON t1(a);
  141. CREATE INDEX i2 ON t1(b);
  142. CREATE TABLE t2(d, e);
  143. INSERT INTO t1 VALUES('A', 'B', 'C');
  144. INSERT INTO t2 VALUES('D', 'E');
  145. }
  146. do_execsql_test 302 {
  147. SELECT * FROM t1, t2 WHERE
  148. (a='A' AND d='E') OR
  149. (b='B' AND c IN ('C', 'D', 'E'))
  150. } {A B C D E}
  151. do_execsql_test 303 {
  152. SELECT * FROM t1, t2 WHERE
  153. (a='A' AND d='E') OR
  154. (b='B' AND c IN (SELECT c FROM t1))
  155. } {A B C D E}
  156. ifcapable compound {
  157. do_execsql_test 304 {
  158. SELECT * FROM t1, t2 WHERE
  159. (a='A' AND d='E') OR
  160. (b='B' AND c IN (SELECT 'B' UNION SELECT 'C' UNION SELECT 'D'))
  161. } {A B C D E}
  162. }
  163. do_execsql_test 305 {
  164. SELECT * FROM t1, t2 WHERE
  165. (b='B' AND c IN ('C', 'D', 'E')) OR
  166. (a='A' AND d='E')
  167. } {A B C D E}
  168. do_execsql_test 306 {
  169. SELECT * FROM t1, t2 WHERE
  170. (b='B' AND c IN (SELECT c FROM t1)) OR
  171. (a='A' AND d='E')
  172. } {A B C D E}
  173. ifcapable compound {
  174. do_execsql_test 307 {
  175. SELECT * FROM t1, t2 WHERE
  176. (b='B' AND c IN (SELECT 'B' UNION SELECT 'C' UNION SELECT 'D')) OR
  177. (a='A' AND d='E')
  178. } {A B C D E}
  179. }
  180. finish_test