tkt1443.test 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # 2005 September 17
  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.
  12. #
  13. # This file implements tests to verify that ticket #1433 has been
  14. # fixed.
  15. #
  16. # The problem in ticket #1433 was that the dependencies on the right-hand
  17. # side of an IN operator were not being checked correctly. So in an
  18. # expression of the form:
  19. #
  20. # t1.x IN (1,t2.b,3)
  21. #
  22. # the optimizer was missing the fact that the right-hand side of the IN
  23. # depended on table t2. It was checking dependencies based on the
  24. # Expr.pRight field rather than Expr.pList and Expr.pSelect.
  25. #
  26. # Such a bug could be verifed using a less elaborate test case. But
  27. # this test case (from the original bug poster) exercises so many different
  28. # parts of the system all at once, that it seemed like a good one to
  29. # include in the test suite.
  30. #
  31. # NOTE: Yes, in spite of the name of this file (tkt1443.test) this
  32. # test is for ticket #1433 not #1443. I mistyped the name when I was
  33. # creating the file and I had already checked in the file by the wrong
  34. # name be the time I noticed the error. With CVS it is a really hassle
  35. # to change filenames, so I'll just leave it as is. No harm done.
  36. #
  37. # $Id: tkt1443.test,v 1.4 2006/01/17 09:35:02 danielk1977 Exp $
  38. set testdir [file dirname $argv0]
  39. source $testdir/tester.tcl
  40. ifcapable !subquery||!memorydb {
  41. finish_test
  42. return
  43. }
  44. # Construct the sample database.
  45. #
  46. do_test tkt1443-1.0 {
  47. sqlite3 db :memory:
  48. execsql {
  49. CREATE TABLE Items(
  50. itemId integer primary key,
  51. item str unique
  52. );
  53. INSERT INTO "Items" VALUES(0, 'ALL');
  54. INSERT INTO "Items" VALUES(1, 'double:source');
  55. INSERT INTO "Items" VALUES(2, 'double');
  56. INSERT INTO "Items" VALUES(3, 'double:runtime');
  57. INSERT INTO "Items" VALUES(4, '.*:runtime');
  58. CREATE TABLE Labels(
  59. labelId INTEGER PRIMARY KEY,
  60. label STR UNIQUE
  61. );
  62. INSERT INTO "Labels" VALUES(0, 'ALL');
  63. INSERT INTO "Labels" VALUES(1, 'localhost@rpl:linux');
  64. INSERT INTO "Labels" VALUES(2, 'localhost@rpl:branch');
  65. CREATE TABLE LabelMap(
  66. itemId INTEGER,
  67. labelId INTEGER,
  68. branchId integer
  69. );
  70. INSERT INTO "LabelMap" VALUES(1, 1, 1);
  71. INSERT INTO "LabelMap" VALUES(2, 1, 1);
  72. INSERT INTO "LabelMap" VALUES(3, 1, 1);
  73. INSERT INTO "LabelMap" VALUES(1, 2, 2);
  74. INSERT INTO "LabelMap" VALUES(2, 2, 3);
  75. INSERT INTO "LabelMap" VALUES(3, 2, 3);
  76. CREATE TABLE Users (
  77. userId INTEGER PRIMARY KEY,
  78. user STRING UNIQUE,
  79. salt BINARY,
  80. password STRING
  81. );
  82. INSERT INTO "Users" VALUES(1, 'test', 'Šæ$d',
  83. '43ba0f45014306bd6df529551ffdb3df');
  84. INSERT INTO "Users" VALUES(2, 'limited', 'ªš>S',
  85. 'cf07c8348fdf675cc1f7696b7d45191b');
  86. CREATE TABLE UserGroups (
  87. userGroupId INTEGER PRIMARY KEY,
  88. userGroup STRING UNIQUE
  89. );
  90. INSERT INTO "UserGroups" VALUES(1, 'test');
  91. INSERT INTO "UserGroups" VALUES(2, 'limited');
  92. CREATE TABLE UserGroupMembers (
  93. userGroupId INTEGER,
  94. userId INTEGER
  95. );
  96. INSERT INTO "UserGroupMembers" VALUES(1, 1);
  97. INSERT INTO "UserGroupMembers" VALUES(2, 2);
  98. CREATE TABLE Permissions (
  99. userGroupId INTEGER,
  100. labelId INTEGER NOT NULL,
  101. itemId INTEGER NOT NULL,
  102. write INTEGER,
  103. capped INTEGER,
  104. admin INTEGER
  105. );
  106. INSERT INTO "Permissions" VALUES(1, 0, 0, 1, 0, 1);
  107. INSERT INTO "Permissions" VALUES(2, 2, 4, 0, 0, 0);
  108. }
  109. } {}
  110. # Run the query with an index
  111. #
  112. do_test tkt1443-1.1 {
  113. execsql {
  114. select distinct
  115. Items.Item as trove, UP.pattern as pattern
  116. from
  117. ( select
  118. Permissions.labelId as labelId,
  119. PerItems.item as pattern
  120. from
  121. Users, UserGroupMembers, Permissions
  122. left outer join Items as PerItems
  123. on Permissions.itemId = PerItems.itemId
  124. where
  125. Users.user = 'limited'
  126. and Users.userId = UserGroupMembers.userId
  127. and UserGroupMembers.userGroupId = Permissions.userGroupId
  128. ) as UP join LabelMap on ( UP.labelId = 0 or
  129. UP.labelId = LabelMap.labelId ),
  130. Labels, Items
  131. where
  132. Labels.label = 'localhost@rpl:branch'
  133. and Labels.labelId = LabelMap.labelId
  134. and LabelMap.itemId = Items.itemId
  135. ORDER BY +trove, +pattern
  136. }
  137. } {double .*:runtime double:runtime .*:runtime double:source .*:runtime}
  138. # Create an index and rerun the query.
  139. # Verify that the results are the same
  140. #
  141. do_test tkt1443-1.2 {
  142. execsql {
  143. CREATE UNIQUE INDEX PermissionsIdx
  144. ON Permissions(userGroupId, labelId, itemId);
  145. select distinct
  146. Items.Item as trove, UP.pattern as pattern
  147. from
  148. ( select
  149. Permissions.labelId as labelId,
  150. PerItems.item as pattern
  151. from
  152. Users, UserGroupMembers, Permissions
  153. left outer join Items as PerItems
  154. on Permissions.itemId = PerItems.itemId
  155. where
  156. Users.user = 'limited'
  157. and Users.userId = UserGroupMembers.userId
  158. and UserGroupMembers.userGroupId = Permissions.userGroupId
  159. ) as UP join LabelMap on ( UP.labelId = 0 or
  160. UP.labelId = LabelMap.labelId ),
  161. Labels, Items
  162. where
  163. Labels.label = 'localhost@rpl:branch'
  164. and Labels.labelId = LabelMap.labelId
  165. and LabelMap.itemId = Items.itemId
  166. ORDER BY +trove, +pattern
  167. }
  168. } {double .*:runtime double:runtime .*:runtime double:source .*:runtime}
  169. finish_test