/sql/test/BugTracker-2016/Tests/dce.Bug-6114.sql

https://github.com/MonetDB/MonetDB · SQL · 264 lines · 234 code · 28 blank · 2 comment · 0 complexity · 12bff02cc055e87a31f623511c04813a MD5 · raw file

  1. CREATE SCHEMA foo;
  2. SET SCHEMA foo;
  3. /* Relations */
  4. CREATE TABLE comments(
  5. id BIGINT NOT NULL PRIMARY KEY,
  6. creationDate TIMESTAMP(3) WITH TIME ZONE NOT NULL,
  7. locationIP VARCHAR(40) NOT NULL,
  8. browserUsed VARCHAR(40) NOT NULL,
  9. content VARCHAR(2000) NOT NULL,
  10. length INT NOT NULL,
  11. creator BIGINT NOT NULL,
  12. place_id BIGINT NOT NULL,
  13. replyOfPost BIGINT,
  14. replyOfComment BIGINT
  15. );
  16. CREATE TABLE comment_tags(
  17. comment_id BIGINT NOT NULL,
  18. tag_id BIGINT NOT NULL,
  19. PRIMARY KEY(comment_id, tag_id)
  20. );
  21. CREATE TABLE forums(
  22. id BIGINT NOT NULL PRIMARY KEY,
  23. title VARCHAR(80) NOT NULL,
  24. creationDate TIMESTAMP(3) WITH TIME ZONE NOT NULL,
  25. moderator BIGINT /*though it seems that all generated tuples have always a moderator */
  26. );
  27. CREATE TABLE forum_persons(
  28. forum_id BIGINT NOT NULL,
  29. person_id BIGINT NOT NULL,
  30. joinDate TIMESTAMP(3) WITH TIME ZONE NOT NULL,
  31. PRIMARY KEY(forum_id, person_id)
  32. );
  33. CREATE TABLE forum_tags(
  34. forum_id BIGINT NOT NULL,
  35. tag_id BIGINT NOT NULL,
  36. PRIMARY KEY(forum_id, tag_id)
  37. );
  38. CREATE TABLE friends(
  39. src BIGINT NOT NULL,
  40. dst BIGINT NOT NULL,
  41. creationDate TIMESTAMP(3) WITH TIME ZONE NOT NULL,
  42. PRIMARY KEY(src, dst)
  43. );
  44. CREATE TABLE organisations(
  45. id BIGINT NOT NULL PRIMARY KEY,
  46. "type" VARCHAR(40) NOT NULL, /* university or company */
  47. name VARCHAR(160) NOT NULL,
  48. url VARCHAR(2000) NOT NULL,
  49. place_id BIGINT NOT NULL
  50. );
  51. CREATE TABLE persons(
  52. id BIGINT NOT NULL PRIMARY KEY,
  53. firstName VARCHAR(40) NOT NULL ,
  54. lastName VARCHAR(40) NOT NULL,
  55. gender VARCHAR(40) NOT NULL,
  56. birthDay DATE NOT NULL,
  57. creationDate TIMESTAMP(3) WITH TIME ZONE NOT NULL,
  58. locationIP VARCHAR(40) NOT NULL,
  59. browserUsed VARCHAR(40) NOT NULL,
  60. place_id BIGINT NOT NULL
  61. );
  62. CREATE TABLE person_comments(
  63. person_id BIGINT NOT NULL,
  64. comment_id BIGINT NOT NULL,
  65. creationDate TIMESTAMP(3) WITH TIME ZONE NOT NULL,
  66. PRIMARY KEY(person_id, comment_id)
  67. );
  68. CREATE TABLE person_emails(
  69. id BIGINT NOT NULL,
  70. email VARCHAR(120) NOT NULL,
  71. PRIMARY KEY(id, email)
  72. );
  73. CREATE TABLE person_languages(
  74. id BIGINT NOT NULL,
  75. "language" VARCHAR(40) NOT NULL,
  76. PRIMARY KEY(id, "language")
  77. );
  78. CREATE TABLE person_tags(
  79. person_id BIGINT NOT NULL,
  80. tag_id BIGINT NOT NULL,
  81. PRIMARY KEY(person_id, tag_id)
  82. );
  83. CREATE TABLE person_posts(
  84. person_id BIGINT NOT NULL,
  85. post_id BIGINT NOT NULL,
  86. creationDate TIMESTAMP(3) WITH TIME ZONE NOT NULL,
  87. PRIMARY KEY(person_id, post_id)
  88. );
  89. CREATE TABLE person_studyAt_organisations(
  90. person_id BIGINT NOT NULL,
  91. organisation_id BIGINT NOT NULL,
  92. classYear INT NOT NULL,
  93. PRIMARY KEY(person_id, organisation_id)
  94. );
  95. CREATE TABLE person_workAt_organisations(
  96. person_id BIGINT NOT NULL,
  97. organisation_id BIGINT NOT NULL,
  98. workFrom INT NOT NULL,
  99. PRIMARY KEY(person_id, organisation_id)
  100. );
  101. CREATE TABLE places(
  102. id BIGINT NOT NULL PRIMARY KEY,
  103. name VARCHAR(200) NOT NULL,
  104. url VARCHAR(2000) NOT NULL, /* suspecting this is a left-over for RDF systems where this field replaces the ID */
  105. "type" VARCHAR(40) NOT NULL,
  106. isPartOf BIGINT
  107. );
  108. CREATE TABLE posts(
  109. id BIGINT NOT NULL PRIMARY KEY,
  110. imageFile VARCHAR(40),
  111. creationDate TIMESTAMP(3) WITH TIME ZONE NOT NULL,
  112. locationIP VARCHAR(40) NOT NULL,
  113. browserUsed VARCHAR(40) NOT NULL,
  114. "language" VARCHAR(40),
  115. content VARCHAR(2000),
  116. length INT NOT NULL,
  117. creator BIGINT NOT NULL,
  118. forum_id BIGINT NOT NULL,
  119. place_id BIGINT NOT NULL
  120. );
  121. CREATE TABLE post_tags(
  122. post_id BIGINT NOT NULL,
  123. tag_id BIGINT NOT NULL,
  124. PRIMARY KEY(post_id, tag_id)
  125. );
  126. CREATE TABLE tags(
  127. id BIGINT NOT NULL PRIMARY KEY,
  128. name VARCHAR(160) NOT NULL,
  129. url VARCHAR(2000) NOT NULL /* is this field mandatory ? */
  130. );
  131. CREATE TABLE tag_tagclasses(
  132. tag_id BIGINT NOT NULL,
  133. tagclass_id BIGINT NOT NULL,
  134. PRIMARY KEY(tag_id, tagclass_id)
  135. );
  136. CREATE TABLE tagclasses(
  137. id BIGINT NOT NULL PRIMARY KEY,
  138. name VARCHAR(40) NOT NULL,
  139. url VARCHAR(2000) NOT NULL /* is this field mandatory ? */
  140. );
  141. CREATE TABLE tagclass_inheritance(
  142. subclass_id BIGINT NOT NULL,
  143. superclass_id BIGINT NOT NULL,
  144. PRIMARY KEY(subclass_id, superclass_id)
  145. );
  146. /* Foreign keys */
  147. ALTER TABLE comments ADD FOREIGN KEY(creator) REFERENCES persons(id);
  148. ALTER TABLE comments ADD FOREIGN KEY(place_id) REFERENCES places(id);
  149. ALTER TABLE comments ADD FOREIGN KEY(replyOfPost) REFERENCES posts(id);
  150. ALTER TABLE comments ADD FOREIGN KEY(replyOfComment) REFERENCES comments(id);
  151. ALTER TABLE comment_tags ADD FOREIGN KEY(comment_id) REFERENCES comments(id);
  152. ALTER TABLE comment_tags ADD FOREIGN KEY(tag_id) REFERENCES tags(id);
  153. ALTER TABLE forums ADD FOREIGN KEY(moderator) REFERENCES persons(id);
  154. ALTER TABLE forum_persons ADD FOREIGN KEY(forum_id) REFERENCES forums(id);
  155. ALTER TABLE forum_persons ADD FOREIGN KEY(person_id) REFERENCES persons(id);
  156. ALTER TABLE forum_tags ADD FOREIGN KEY(forum_id) REFERENCES forums(id);
  157. ALTER TABLE forum_tags ADD FOREIGN KEY(tag_id) REFERENCES tags(id);
  158. ALTER TABLE friends ADD FOREIGN KEY(src) REFERENCES persons(id);
  159. ALTER TABLE friends ADD FOREIGN KEY(dst) REFERENCES persons(id);
  160. ALTER TABLE organisations ADD FOREIGN KEY(place_id) REFERENCES places(id);
  161. ALTER TABLE persons ADD FOREIGN KEY(place_id) REFERENCES places(id);
  162. ALTER TABLE person_comments ADD FOREIGN KEY(person_id) REFERENCES persons(id);
  163. ALTER TABLE person_comments ADD FOREIGN KEY(comment_id) REFERENCES comments(id);
  164. ALTER TABLE person_emails ADD FOREIGN KEY(id) REFERENCES persons(id);
  165. ALTER TABLE person_languages ADD FOREIGN KEY(id) REFERENCES persons(id);
  166. ALTER TABLE person_posts ADD FOREIGN KEY(person_id) REFERENCES persons(id);
  167. ALTER TABLE person_posts ADD FOREIGN KEY(post_id) REFERENCES posts(id);
  168. ALTER TABLE person_studyAt_organisations ADD FOREIGN KEY(person_id) REFERENCES persons(id);
  169. ALTER TABLE person_studyAt_organisations ADD FOREIGN KEY(organisation_id) REFERENCES organisations(id);
  170. ALTER TABLE person_tags ADD FOREIGN KEY(person_id) REFERENCES persons(id);
  171. ALTER TABLE person_tags ADD FOREIGN KEY(tag_id) REFERENCES tags(id);
  172. ALTER TABLE person_workAt_organisations ADD FOREIGN KEY(person_id) REFERENCES persons(id);
  173. ALTER TABLE person_workAt_organisations ADD FOREIGN KEY(organisation_id) REFERENCES organisations(id);
  174. ALTER TABLE places ADD FOREIGN KEY(isPartOf) REFERENCES places(id);
  175. ALTER TABLE posts ADD FOREIGN KEY(creator) REFERENCES persons(id);
  176. ALTER TABLE posts ADD FOREIGN KEY(forum_id) REFERENCES forums(id);
  177. ALTER TABLE posts ADD FOREIGN KEY(place_id) REFERENCES places(id);
  178. ALTER TABLE post_tags ADD FOREIGN KEY(post_id) REFERENCES posts(id);
  179. ALTER TABLE post_tags ADD FOREIGN KEY(tag_id) REFERENCES tags(id);
  180. ALTER TABLE tag_tagclasses ADD FOREIGN KEY(tag_id) REFERENCES tags(id);
  181. ALTER TABLE tag_tagclasses ADD FOREIGN KEY(tagclass_id) REFERENCES tagclasses(id);
  182. ALTER TABLE tagclass_inheritance ADD FOREIGN KEY(subclass_id) REFERENCES tagclasses(id);
  183. ALTER TABLE tagclass_inheritance ADD FOREIGN KEY(superclass_id) REFERENCES tagclasses(id);
  184. WITH
  185. params AS ( /* input parameters */
  186. SELECT
  187. 2199023260527 AS id,
  188. CAST('Lin' AS VARCHAR(40)) AS name
  189. ),
  190. friends_1 AS (
  191. SELECT 1 as "distance", p1."id", p1.firstName, p1.lastName, p1.birthDay, p1.creationDate, p1."gender", p1.browserUsed, p1.locationIP
  192. FROM persons p1, friends f, params p
  193. WHERE p.id = f."src" AND f."dst" = p1."id"
  194. ),
  195. friends_2 AS (
  196. SELECT * FROM friends_1
  197. UNION
  198. SELECT 2 as "distance", p2."id", p2.firstName, p2.lastName, p2.birthDay, p2.creationDate, p2."gender", p2.browserUsed, p2.locationIP
  199. FROM persons p2, friends f
  200. WHERE f."src" IN ( SELECT "id" FROM friends_1 ) AND f."dst" = p2."id" AND p2.id NOT IN ( SELECT "id" FROM friends_1 )
  201. ),
  202. friends_3 AS (
  203. SELECT * FROM friends_2 f WHERE f.firstName = (SELECT name FROM params)
  204. UNION
  205. SELECT 3 as "distance", p3."id", p3.firstName, p3.lastName, p3.birthDay, p3.creationDate, p3."gender", p3.browserUsed, p3.locationIP
  206. FROM persons p3, friends f
  207. WHERE f."src" IN ( SELECT "id" FROM friends_2 ) AND f."dst" = p3."id" AND p3.id NOT IN ( SELECT "id" FROM friends_2 ) AND p3.firstName = (SELECT name FROM params)
  208. ),
  209. filter AS (
  210. SELECT * FROM friends_3 f WHERE f.firstName = (SELECT name FROM params)
  211. )
  212. SELECT * FROM filter ORDER BY "distance";
  213. DROP TABLE comment_tags;
  214. DROP TABLE forum_persons;
  215. DROP TABLE forum_tags;
  216. DROP TABLE friends;
  217. DROP TABLE person_emails;
  218. DROP TABLE person_tags;
  219. DROP TABLE person_languages;
  220. DROP TABLE person_comments;
  221. DROP TABLE person_posts;
  222. DROP TABLE person_studyAt_organisations;
  223. DROP TABLE person_workAt_organisations;
  224. DROP TABLE post_tags;
  225. DROP TABLE tag_tagclasses;
  226. DROP TABLE tagclass_inheritance;
  227. DROP TABLE organisations;
  228. DROP TABLE comments;
  229. DROP TABLE posts;
  230. DROP TABLE forums;
  231. DROP TABLE persons;
  232. DROP TABLE places;
  233. DROP TABLE tags;
  234. DROP TABLE tagclasses;
  235. SET SCHEMA sys;
  236. DROP SCHEMA foo;