tkt2640.test 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # 2007 Sep 12
  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 file is to test that ticket #2640 has been fixed.
  13. #
  14. # $Id: tkt2640.test,v 1.3 2008/08/04 03:51:24 danielk1977 Exp $
  15. #
  16. # The problem in ticket #2640 was that the query optimizer was
  17. # not recognizing all uses of tables within subqueries in the
  18. # WHERE clause. If the subquery contained a compound SELECT,
  19. # then tables that were used by terms of the compound other than
  20. # the last term would not be recognized as dependencies.
  21. # So if one of the SELECT statements within a compound made
  22. # use of a table that occurs later in a join, the query
  23. # optimizer would not recognize this and would try to evaluate
  24. # the subquery too early, before that tables value had been
  25. # established.
  26. set testdir [file dirname $argv0]
  27. source $testdir/tester.tcl
  28. ifcapable !subquery||!compound {
  29. finish_test
  30. return
  31. }
  32. do_test tkt2640-1.1 {
  33. execsql {
  34. CREATE TABLE persons(person_id, name);
  35. INSERT INTO persons VALUES(1,'fred');
  36. INSERT INTO persons VALUES(2,'barney');
  37. INSERT INTO persons VALUES(3,'wilma');
  38. INSERT INTO persons VALUES(4,'pebbles');
  39. INSERT INTO persons VALUES(5,'bambam');
  40. CREATE TABLE directors(person_id);
  41. INSERT INTO directors VALUES(5);
  42. INSERT INTO directors VALUES(3);
  43. CREATE TABLE writers(person_id);
  44. INSERT INTO writers VALUES(2);
  45. INSERT INTO writers VALUES(3);
  46. INSERT INTO writers VALUES(4);
  47. SELECT DISTINCT p.name
  48. FROM persons p, directors d
  49. WHERE d.person_id=p.person_id
  50. AND NOT EXISTS (
  51. SELECT person_id FROM directors d1 WHERE d1.person_id=p.person_id
  52. EXCEPT
  53. SELECT person_id FROM writers w
  54. );
  55. }
  56. } {wilma}
  57. do_test tkt2640-1.2 {
  58. execsql {
  59. SELECT DISTINCT p.name
  60. FROM persons p CROSS JOIN directors d
  61. WHERE d.person_id=p.person_id
  62. AND NOT EXISTS (
  63. SELECT person_id FROM directors d1 WHERE d1.person_id=p.person_id
  64. EXCEPT
  65. SELECT person_id FROM writers w
  66. );
  67. }
  68. } {wilma}
  69. do_test tkt2640-1.3 {
  70. execsql {
  71. SELECT DISTINCT p.name
  72. FROM directors d CROSS JOIN persons p
  73. WHERE d.person_id=p.person_id
  74. AND NOT EXISTS (
  75. SELECT person_id FROM directors d1 WHERE d1.person_id=p.person_id
  76. EXCEPT
  77. SELECT person_id FROM writers w
  78. );
  79. }
  80. } {wilma}
  81. do_test tkt2640-1.4 {
  82. execsql {
  83. SELECT DISTINCT p.name
  84. FROM persons p, directors d
  85. WHERE d.person_id=p.person_id
  86. AND NOT EXISTS (
  87. SELECT person_id FROM directors d1 WHERE d1.person_id=d.person_id
  88. EXCEPT
  89. SELECT person_id FROM writers w
  90. );
  91. }
  92. } {wilma}
  93. do_test tkt2640-1.5 {
  94. execsql {
  95. SELECT DISTINCT p.name
  96. FROM persons p CROSS JOIN directors d
  97. WHERE d.person_id=p.person_id
  98. AND NOT EXISTS (
  99. SELECT person_id FROM directors d1 WHERE d1.person_id=d.person_id
  100. EXCEPT
  101. SELECT person_id FROM writers w
  102. );
  103. }
  104. } {wilma}
  105. do_test tkt2640-1.6 {
  106. execsql {
  107. SELECT DISTINCT p.name
  108. FROM directors d CROSS JOIN persons p
  109. WHERE d.person_id=p.person_id
  110. AND NOT EXISTS (
  111. SELECT person_id FROM directors d1 WHERE d1.person_id=d.person_id
  112. EXCEPT
  113. SELECT person_id FROM writers w
  114. );
  115. }
  116. } {wilma}
  117. finish_test