where6.test 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # 2007 June 8
  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. The
  12. # focus of this file is testing that terms in the ON clause of
  13. # a LEFT OUTER JOIN are not used with indices. See ticket #3015.
  14. #
  15. # $Id: where6.test,v 1.2 2008/04/17 19:14:02 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Build some test data
  19. #
  20. do_test where6-1.1 {
  21. execsql {
  22. CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c);
  23. INSERT INTO t1 VALUES(1,3,1);
  24. INSERT INTO t1 VALUES(2,4,2);
  25. CREATE TABLE t2(x INTEGER PRIMARY KEY);
  26. INSERT INTO t2 VALUES(3);
  27. SELECT * FROM t1 LEFT JOIN t2 ON b=x AND c=1;
  28. }
  29. } {1 3 1 3 2 4 2 {}}
  30. do_test where6-1.2 {
  31. execsql {
  32. SELECT * FROM t1 LEFT JOIN t2 ON x=b AND c=1;
  33. }
  34. } {1 3 1 3 2 4 2 {}}
  35. do_test where6-1.3 {
  36. execsql {
  37. SELECT * FROM t1 LEFT JOIN t2 ON x=b AND 1=c;
  38. }
  39. } {1 3 1 3 2 4 2 {}}
  40. do_test where6-1.4 {
  41. execsql {
  42. SELECT * FROM t1 LEFT JOIN t2 ON b=x AND 1=c;
  43. }
  44. } {1 3 1 3 2 4 2 {}}
  45. ifcapable explain {
  46. do_test where6-1.5 {
  47. explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON x=b AND 1=c}
  48. } [explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON b=x AND c=1}]
  49. do_test where6-1.6 {
  50. explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON x=b WHERE 1=c}
  51. } [explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE c=1}]
  52. }
  53. do_test where6-1.11 {
  54. execsql {
  55. SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE c=1;
  56. }
  57. } {1 3 1 3}
  58. do_test where6-1.12 {
  59. execsql {
  60. SELECT * FROM t1 LEFT JOIN t2 ON x=b WHERE c=1;
  61. }
  62. } {1 3 1 3}
  63. do_test where6-1.13 {
  64. execsql {
  65. SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE 1=c;
  66. }
  67. } {1 3 1 3}
  68. do_test where6-2.1 {
  69. execsql {
  70. CREATE INDEX i1 ON t1(c);
  71. SELECT * FROM t1 LEFT JOIN t2 ON b=x AND c=1;
  72. }
  73. } {1 3 1 3 2 4 2 {}}
  74. do_test where6-2.2 {
  75. execsql {
  76. SELECT * FROM t1 LEFT JOIN t2 ON x=b AND c=1;
  77. }
  78. } {1 3 1 3 2 4 2 {}}
  79. do_test where6-2.3 {
  80. execsql {
  81. SELECT * FROM t1 LEFT JOIN t2 ON x=b AND 1=c;
  82. }
  83. } {1 3 1 3 2 4 2 {}}
  84. do_test where6-2.4 {
  85. execsql {
  86. SELECT * FROM t1 LEFT JOIN t2 ON b=x AND 1=c;
  87. }
  88. } {1 3 1 3 2 4 2 {}}
  89. ifcapable explain {
  90. do_test where6-2.5 {
  91. explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON x=b AND 1=c}
  92. } [explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON b=x AND c=1}]
  93. do_test where6-2.6 {
  94. explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON x=b WHERE 1=c}
  95. } [explain_no_trace {SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE c=1}]
  96. }
  97. do_test where6-2.11 {
  98. execsql {
  99. SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE c=1;
  100. }
  101. } {1 3 1 3}
  102. do_test where6-2.12 {
  103. execsql {
  104. SELECT * FROM t1 LEFT JOIN t2 ON x=b WHERE c=1;
  105. }
  106. } {1 3 1 3}
  107. do_test where6-2.13 {
  108. execsql {
  109. SELECT * FROM t1 LEFT JOIN t2 ON x=b WHERE 1=c;
  110. }
  111. } {1 3 1 3}
  112. do_test where6-2.14 {
  113. execsql {
  114. SELECT * FROM t1 LEFT JOIN t2 ON b=x WHERE 1=c;
  115. }
  116. } {1 3 1 3}
  117. # Ticket [ebdbadade5b]:
  118. # If the ON close on a LEFT JOIN is of the form x=y where both x and y
  119. # are indexed columns on tables to left of the join, then do not use that
  120. # term with indices to either table.
  121. #
  122. do_test where6-3.1 {
  123. db eval {
  124. CREATE TABLE t4(x UNIQUE);
  125. INSERT INTO t4 VALUES('abc');
  126. INSERT INTO t4 VALUES('def');
  127. INSERT INTO t4 VALUES('ghi');
  128. CREATE TABLE t5(a, b, c, PRIMARY KEY(a,b));
  129. INSERT INTO t5 VALUES('abc','def',123);
  130. INSERT INTO t5 VALUES('def','ghi',456);
  131. SELECT t4a.x, t4b.x, t5.c, t6.v
  132. FROM t4 AS t4a
  133. INNER JOIN t4 AS t4b
  134. LEFT JOIN t5 ON t5.a=t4a.x AND t5.b=t4b.x
  135. LEFT JOIN (SELECT 1 AS v) AS t6 ON t4a.x=t4b.x
  136. ORDER BY 1, 2, 3;
  137. }
  138. } {abc abc {} 1 abc def 123 {} abc ghi {} {} def abc {} {} def def {} 1 def ghi 456 {} ghi abc {} {} ghi def {} {} ghi ghi {} 1}
  139. finish_test