join.sql 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. --
  2. -- JOIN
  3. -- Test JOIN clauses
  4. --
  5. CREATE TABLE J1_TBL (
  6. i integer,
  7. j integer,
  8. t text
  9. );
  10. CREATE TABLE J2_TBL (
  11. i integer,
  12. k integer
  13. );
  14. INSERT INTO J1_TBL VALUES (1, 4, 'one');
  15. INSERT INTO J1_TBL VALUES (2, 3, 'two');
  16. INSERT INTO J1_TBL VALUES (3, 2, 'three');
  17. INSERT INTO J1_TBL VALUES (4, 1, 'four');
  18. INSERT INTO J1_TBL VALUES (5, 0, 'five');
  19. INSERT INTO J1_TBL VALUES (6, 6, 'six');
  20. INSERT INTO J1_TBL VALUES (7, 7, 'seven');
  21. INSERT INTO J1_TBL VALUES (8, 8, 'eight');
  22. INSERT INTO J1_TBL VALUES (0, NULL, 'zero');
  23. INSERT INTO J1_TBL VALUES (NULL, NULL, 'null');
  24. INSERT INTO J1_TBL VALUES (NULL, 0, 'zero');
  25. INSERT INTO J2_TBL VALUES (1, -1);
  26. INSERT INTO J2_TBL VALUES (2, 2);
  27. INSERT INTO J2_TBL VALUES (3, -3);
  28. INSERT INTO J2_TBL VALUES (2, 4);
  29. INSERT INTO J2_TBL VALUES (5, -5);
  30. INSERT INTO J2_TBL VALUES (5, -5);
  31. INSERT INTO J2_TBL VALUES (0, NULL);
  32. INSERT INTO J2_TBL VALUES (NULL, NULL);
  33. INSERT INTO J2_TBL VALUES (NULL, 0);
  34. -- useful in some tests below
  35. create temp table onerow();
  36. --
  37. -- CORRELATION NAMES
  38. -- Make sure that table/column aliases are supported
  39. -- before diving into more complex join syntax.
  40. --
  41. SELECT *
  42. FROM J1_TBL AS tx;
  43. SELECT *
  44. FROM J1_TBL tx;
  45. SELECT *
  46. FROM J1_TBL AS t1 (a, b, c);
  47. SELECT *
  48. FROM J1_TBL t1 (a, b, c);
  49. SELECT *
  50. FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, d) USING (a)
  51. ORDER BY a, d;
  52. -- test join using aliases
  53. SELECT * FROM J1_TBL JOIN J2_TBL USING (i) WHERE J1_TBL.t = 'one'; -- ok
  54. SELECT *
  55. FROM J1_TBL LEFT JOIN J2_TBL USING (i) WHERE (k = 1);
  56. --
  57. -- More complicated constructs
  58. --
  59. --
  60. -- Multiway full join
  61. --
  62. CREATE TABLE t1 (name TEXT, n INTEGER);
  63. CREATE TABLE t2 (name TEXT, n INTEGER);
  64. CREATE TABLE t3 (name TEXT, n INTEGER);
  65. INSERT INTO t1 VALUES ( 'bb', 11 );
  66. INSERT INTO t2 VALUES ( 'bb', 12 );
  67. INSERT INTO t2 VALUES ( 'cc', 22 );
  68. INSERT INTO t2 VALUES ( 'ee', 42 );
  69. INSERT INTO t3 VALUES ( 'bb', 13 );
  70. INSERT INTO t3 VALUES ( 'cc', 23 );
  71. INSERT INTO t3 VALUES ( 'dd', 33 );
  72. -- Test for propagation of nullability constraints into sub-joins
  73. create temp table x (x1 int, x2 int);
  74. insert into x values (1,11);
  75. insert into x values (2,22);
  76. insert into x values (3,null);
  77. insert into x values (4,44);
  78. insert into x values (5,null);
  79. create temp table y (y1 int, y2 int);
  80. insert into y values (1,111);
  81. insert into y values (2,222);
  82. insert into y values (3,333);
  83. insert into y values (4,null);
  84. select * from x;
  85. select * from y;
  86. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  87. on (x1 = xx1);
  88. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  89. on (x1 = xx1 and xx2 is not null);
  90. -- these should NOT give the same answers as above
  91. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  92. on (x1 = xx1) where (x2 is not null);
  93. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  94. on (x1 = xx1) where (y2 is not null);
  95. select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
  96. on (x1 = xx1) where (xx2 is not null);
  97. --
  98. -- regression test: check for bug with propagation of implied equality
  99. -- to outside an IN
  100. --
  101. select count(*) from tenk1 a where unique1 in
  102. (select unique1 from tenk1 b join tenk1 c using (unique1)
  103. where b.unique2 = 42);
  104. -- try that with GEQO too
  105. begin;
  106. rollback;
  107. --
  108. -- regression test: check a case where join_clause_is_movable_into() gives
  109. -- an imprecise result, causing an assertion failure
  110. --
  111. select count(*)
  112. from
  113. (select t3.tenthous as x1, coalesce(t1.stringu1, t2.stringu1) as x2
  114. from tenk1 t1
  115. left join tenk1 t2 on t1.unique1 = t2.unique1
  116. join tenk1 t3 on t1.unique2 = t3.unique2) ss,
  117. tenk1 t4,
  118. tenk1 t5
  119. where t4.thousand = t5.unique1 and ss.x1 = t4.tenthous and ss.x2 = t5.stringu1;
  120. select count(*) from
  121. (select * from tenk1 x order by x.thousand, x.twothousand, x.fivethous) x
  122. left join
  123. (select * from tenk1 y order by y.unique2) y
  124. on x.thousand = y.unique2 and x.twothousand = y.hundred and x.fivethous = y.unique2;
  125. --
  126. -- Clean up
  127. --
  128. DROP TABLE t1;
  129. DROP TABLE t2;
  130. DROP TABLE t3;
  131. DROP TABLE J1_TBL;
  132. DROP TABLE J2_TBL;
  133. -- Both DELETE and UPDATE allow the specification of additional tables
  134. -- to "join" against to determine which rows should be modified.
  135. CREATE TEMP TABLE t1 (a int, b int);
  136. CREATE TEMP TABLE t2 (a int, b int);
  137. CREATE TEMP TABLE t3 (x int, y int);
  138. INSERT INTO t1 VALUES (5, 10);
  139. INSERT INTO t1 VALUES (15, 20);
  140. INSERT INTO t1 VALUES (100, 100);
  141. INSERT INTO t1 VALUES (200, 1000);
  142. INSERT INTO t2 VALUES (200, 2000);
  143. INSERT INTO t3 VALUES (5, 20);
  144. INSERT INTO t3 VALUES (6, 7);
  145. INSERT INTO t3 VALUES (7, 8);
  146. INSERT INTO t3 VALUES (500, 100);
  147. --
  148. -- regression test for 8.1 merge right join bug
  149. --
  150. CREATE TEMP TABLE tt1 ( tt1_id int4, joincol int4 );
  151. INSERT INTO tt1 VALUES (1, 11);
  152. INSERT INTO tt1 VALUES (2, NULL);
  153. CREATE TEMP TABLE tt2 ( tt2_id int4, joincol int4 );
  154. INSERT INTO tt2 VALUES (21, 11);
  155. INSERT INTO tt2 VALUES (22, 11);
  156. select count(*) from tenk1 a, tenk1 b
  157. where a.hundred = b.thousand and (b.fivethous % 10) < 10;
  158. --
  159. -- regression test for 8.2 bug with improper re-ordering of left joins
  160. --
  161. create temp table tt3(f1 int, f2 text);
  162. insert into tt3 select x, repeat('xyzzy', 100) from generate_series(1,10000) x;
  163. create index tt3i on tt3(f1);
  164. create temp table tt4(f1 int);
  165. insert into tt4 values (0),(1),(9999);
  166. --
  167. -- regression test for proper handling of outer joins within antijoins
  168. --
  169. create temp table tt4x(c1 int, c2 int, c3 int);
  170. --
  171. -- regression test for problems of the sort depicted in bug #3494
  172. --
  173. create temp table tt5(f1 int, f2 int);
  174. create temp table tt6(f1 int, f2 int);
  175. insert into tt5 values(1, 10);
  176. insert into tt5 values(1, 11);
  177. insert into tt6 values(1, 9);
  178. insert into tt6 values(1, 2);
  179. insert into tt6 values(2, 9);
  180. --
  181. -- regression test for problems of the sort depicted in bug #3588
  182. --
  183. create temp table xx (pkxx int);
  184. create temp table yy (pkyy int, pkxx int);
  185. insert into xx values (1);
  186. insert into xx values (2);
  187. insert into xx values (3);
  188. insert into yy values (101, 1);
  189. insert into yy values (201, 2);
  190. insert into yy values (301, NULL);
  191. --
  192. -- regression test for improper pushing of constants across outer-join clauses
  193. -- (as seen in early 8.2.x releases)
  194. --
  195. create temp table zt1 (f1 int primary key);
  196. create temp table zt2 (f2 int primary key);
  197. create temp table zt3 (f3 int primary key);
  198. insert into zt1 values(53);
  199. insert into zt2 values(53);
  200. select * from
  201. zt2 left join zt3 on (f2 = f3)
  202. left join zt1 on (f3 = f1)
  203. where f2 = 53;
  204. --
  205. -- test for sane behavior with noncanonical merge clauses, per bug #4926
  206. --
  207. begin;
  208. create temp table a (i integer);
  209. create temp table b (x integer, y integer);
  210. select * from a left join b on i = x and i = y and x = i;
  211. rollback;
  212. --
  213. -- test handling of merge clauses using record_ops
  214. --
  215. begin;
  216. create temp table tidv (idv mycomptype);
  217. create index on tidv (idv);
  218. rollback;
  219. --
  220. -- test incorrect failure to NULL pulled-up subexpressions
  221. --
  222. begin;
  223. create temp table a (
  224. code char not null,
  225. constraint a_pk primary key (code)
  226. );
  227. create temp table b (
  228. a char not null,
  229. num integer not null,
  230. constraint b_pk primary key (a, num)
  231. );
  232. create temp table c (
  233. name char not null,
  234. a char,
  235. constraint c_pk primary key (name)
  236. );
  237. insert into a (code) values ('p');
  238. insert into a (code) values ('q');
  239. insert into b (a, num) values ('p', 1);
  240. insert into b (a, num) values ('p', 2);
  241. insert into c (name, a) values ('A', 'p');
  242. insert into c (name, a) values ('B', 'q');
  243. insert into c (name, a) values ('C', null);
  244. rollback;
  245. --
  246. -- test incorrect handling of placeholders that only appear in targetlists,
  247. -- per bug #6154
  248. --
  249. SELECT * FROM
  250. ( SELECT 1 as key1 ) sub1
  251. LEFT JOIN
  252. ( SELECT sub3.key3, sub4.value2, COALESCE(sub4.value2, 66) as value3 FROM
  253. ( SELECT 1 as key3 ) sub3
  254. LEFT JOIN
  255. ( SELECT sub5.key5, COALESCE(sub6.value1, 1) as value2 FROM
  256. ( SELECT 1 as key5 ) sub5
  257. LEFT JOIN
  258. ( SELECT 2 as key6, 42 as value1 ) sub6
  259. ON sub5.key5 = sub6.key6
  260. ) sub4
  261. ON sub4.key5 = sub3.key3
  262. ) sub2
  263. ON sub1.key1 = sub2.key3;
  264. -- test the path using join aliases, too
  265. SELECT * FROM
  266. ( SELECT 1 as key1 ) sub1
  267. LEFT JOIN
  268. ( SELECT sub3.key3, value2, COALESCE(value2, 66) as value3 FROM
  269. ( SELECT 1 as key3 ) sub3
  270. LEFT JOIN
  271. ( SELECT sub5.key5, COALESCE(sub6.value1, 1) as value2 FROM
  272. ( SELECT 1 as key5 ) sub5
  273. LEFT JOIN
  274. ( SELECT 2 as key6, 42 as value1 ) sub6
  275. ON sub5.key5 = sub6.key6
  276. ) sub4
  277. ON sub4.key5 = sub3.key3
  278. ) sub2
  279. ON sub1.key1 = sub2.key3;
  280. --
  281. -- nested nestloops can require nested PlaceHolderVars
  282. --
  283. create temp table nt1 (
  284. id int primary key,
  285. a1 boolean,
  286. a2 boolean
  287. );
  288. insert into nt1 values (1,true,true);
  289. insert into nt1 values (2,true,false);
  290. insert into nt1 values (3,false,false);
  291. select * from
  292. int8_tbl t1 left join
  293. (select q1 as x, 42 as y from int8_tbl t2) ss
  294. on t1.q2 = ss.x
  295. where
  296. 1 = (select 1 from int8_tbl t3 where ss.y is not null limit 1)
  297. order by 1,2;
  298. select t1.unique2, t1.stringu1, t2.unique1, t2.stringu2 from
  299. tenk1 t1
  300. inner join int4_tbl i1
  301. left join (select v1.x2, v2.y1, 11 AS d1
  302. from (values(1,0)) v1(x1,x2)
  303. left join (values(3,1)) v2(y1,y2)
  304. on v1.x1 = v2.y2) subq1
  305. on (i1.f1 = subq1.x2)
  306. on (t1.unique2 = subq1.d1)
  307. left join tenk1 t2
  308. on (subq1.y1 = t2.unique1)
  309. where t1.unique2 < 42 and t1.stringu1 > t2.stringu2;
  310. select count(*) from
  311. tenk1 a join tenk1 b on a.unique1 = b.unique2
  312. left join tenk1 c on a.unique2 = b.unique1 and c.thousand = a.thousand
  313. join int4_tbl on b.thousand = f1;
  314. select f1, unique2, case when unique2 is null then f1 else 0 end
  315. from int4_tbl a left join tenk1 b on f1 = unique2
  316. where (case when unique2 is null then f1 else 0 end) = 0;
  317. --
  318. -- test join removal
  319. --
  320. begin;
  321. CREATE TEMP TABLE a (id int PRIMARY KEY, b_id int);
  322. CREATE TEMP TABLE b (id int PRIMARY KEY, c_id int);
  323. CREATE TEMP TABLE c (id int PRIMARY KEY);
  324. CREATE TEMP TABLE d (a int, b int);
  325. INSERT INTO b VALUES (0, 0), (1, NULL);
  326. INSERT INTO c VALUES (0), (1);
  327. INSERT INTO d VALUES (1,3), (2,2), (3,1);
  328. rollback;
  329. create temp table parent (k int primary key, pd int);
  330. create temp table child (k int unique, cd int);
  331. insert into parent values (1, 10), (2, 20), (3, 30);
  332. insert into child values (1, 100), (4, 400);
  333. -- check for a 9.0rc1 bug: join removal breaks pseudoconstant qual handling
  334. select p.* from
  335. parent p left join child c on (p.k = c.k)
  336. where p.k = 1 and p.k = 2;
  337. select p.* from
  338. (parent p left join child c on (p.k = c.k)) join parent x on p.k = x.k
  339. where p.k = 1 and p.k = 2;
  340. -- bug 5255: this is not optimizable by join removal
  341. begin;
  342. CREATE TEMP TABLE a (id int PRIMARY KEY);
  343. CREATE TEMP TABLE b (id int PRIMARY KEY, a_id int);
  344. INSERT INTO a VALUES (0), (1);
  345. INSERT INTO b VALUES (0, 0), (1, NULL);
  346. rollback;
  347. -- another join removal bug: this is not optimizable, either
  348. begin;
  349. create temp table innertab (id int8 primary key, dat1 int8);
  350. insert into innertab values(123, 42);
  351. rollback;
  352. -- another join removal bug: we must clean up correctly when removing a PHV
  353. begin;
  354. create temp table uniquetbl (f1 text unique);
  355. rollback;
  356. create table join_ut1 (a int, b int, c varchar);
  357. insert into join_ut1 values (101, 101, 'y'), (2, 2, 'z');
  358. drop table join_ut1;
  359. --
  360. -- test estimation behavior with multi-column foreign key and constant qual
  361. --
  362. begin;
  363. create table fkest (x integer, x10 integer, x10b integer, x100 integer);
  364. insert into fkest select x, x/10, x/10, x/100 from generate_series(1,1000) x;
  365. rollback;
  366. --
  367. -- test that foreign key join estimation performs sanely for outer joins
  368. --
  369. begin;
  370. create table fkest (a int, b int, c int unique, primary key(a,b));
  371. create table fkest1 (a int, b int, primary key(a,b));
  372. insert into fkest select x/10, x%10, x from generate_series(1,1000) x;
  373. insert into fkest1 select x/10, x%10 from generate_series(1,1000) x;
  374. rollback;
  375. --
  376. -- test planner's ability to mark joins as unique
  377. --
  378. create table j1 (id int primary key);
  379. create table j2 (id int primary key);
  380. create table j3 (id int);
  381. insert into j1 values(1),(2),(3);
  382. insert into j2 values(1),(2),(3);
  383. insert into j3 values(1),(1);
  384. drop table j1;
  385. drop table j2;
  386. drop table j3;
  387. -- test more complex permutations of unique joins
  388. create table j1 (id1 int, id2 int, primary key(id1,id2));
  389. create table j2 (id1 int, id2 int, primary key(id1,id2));
  390. create table j3 (id1 int, id2 int, primary key(id1,id2));
  391. insert into j1 values(1,1),(1,2);
  392. insert into j2 values(1,1);
  393. insert into j3 values(1,1);
  394. -- need an additional row in j2, if we want j2_id1_idx to be preferred
  395. insert into j2 values(1,2);
  396. drop table j1;
  397. drop table j2;
  398. drop table j3;