join-lateral.slt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. # Copyright 1994, Regents of the University of California.
  2. # Copyright 1996-2019 PostgreSQL Global Development Group.
  3. # Copyright Materialize, Inc. and contributors. All rights reserved.
  4. #
  5. # Use of this software is governed by the Business Source License
  6. # included in the LICENSE file at the root of this repository.
  7. #
  8. # As of the Change Date specified in that file, in accordance with
  9. # the Business Source License, use of this software will be governed
  10. # by the Apache License, Version 2.0.
  11. #
  12. # This file is derived from the regression test suite in PostgreSQL.
  13. # The original file was retrieved on July 23, 2020 from:
  14. #
  15. # https://github.com/postgres/postgres/blob/5940ffb221316ab73e6fdc780dfe9a07d4221ebb/src/test/regress/expected/join.out
  16. #
  17. # The original source code is subject to the terms of the PostgreSQL
  18. # license, a copy of which can be found in the LICENSE file at the
  19. # root of this repository.
  20. mode cockroach
  21. statement ok
  22. CREATE TABLE int2_tbl (f1 smallint)
  23. statement ok
  24. INSERT INTO int2_tbl (f1) VALUES (0), (1234), (-1234), (32767), (-32767);
  25. statement ok
  26. CREATE TABLE int4_tbl (f1 int)
  27. statement ok
  28. INSERT INTO int4_tbl (f1) VALUES (0), (123456), (-123456), (2147483647), (-2147483647)
  29. statement ok
  30. CREATE TABLE int8_tbl (q1 bigint, q2 bigint)
  31. statement ok
  32. INSERT INTO int8_tbl VALUES
  33. (123, 456),
  34. (123, 4567890123456789),
  35. (4567890123456789, 123),
  36. (4567890123456789, 4567890123456789),
  37. (4567890123456789, -4567890123456789)
  38. statement ok
  39. CREATE TABLE tenk1 (
  40. unique1 int,
  41. unique2 int,
  42. two int
  43. )
  44. statement ok
  45. INSERT INTO tenk1 VALUES
  46. (2, 0, 0),
  47. (1, 3, 1),
  48. (6, 9, 0),
  49. (5, 8, 0),
  50. (0, 4, 0),
  51. (7, 7, 1),
  52. (9, 2, 1),
  53. (4, 6, 1),
  54. (8, 1, 0),
  55. (3, 5, 1)
  56. # NOTE(benesch): The tenk1 table is named as such because it is meant to contain
  57. # 10k rows. We include only 10 rows here because 10k rows causes Materialize to
  58. # absolutely fall over since the plans for a lot of these lateral joins are
  59. # absolutely horrible.
  60. statement ok
  61. CREATE TABLE onerow ()
  62. statement ok
  63. INSERT INTO onerow DEFAULT VALUES
  64. query II colnames
  65. select unique2, x.*
  66. from tenk1 a, lateral (select * from int4_tbl b where f1 = a.unique1) x;
  67. ----
  68. unique2 f1
  69. 4 0
  70. query II colnames
  71. select unique2, x.*
  72. from int4_tbl x, lateral (select unique2 from tenk1 where f1 = unique1) ss;
  73. ----
  74. unique2 f1
  75. 4 0
  76. query II colnames,rowsort
  77. select unique2, x.*
  78. from int4_tbl x left join lateral (select unique1, unique2 from tenk1 where f1 = unique1) ss on true;
  79. ----
  80. unique2 f1
  81. 4 0
  82. NULL -123456
  83. NULL -2147483647
  84. NULL 123456
  85. NULL 2147483647
  86. # check scoping of lateral versus parent references
  87. # the first of these should return int8_tbl.q2, the second int8_tbl.q1
  88. query III colnames,rowsort
  89. select *, (select r from (select q1 as q2) x, (select q2 as r) y) from int8_tbl
  90. ----
  91. q1 q2 r
  92. 123 456 456
  93. 123 4567890123456789 4567890123456789
  94. 4567890123456789 123 123
  95. 4567890123456789 4567890123456789 4567890123456789
  96. 4567890123456789 -4567890123456789 -4567890123456789
  97. query III colnames,rowsort
  98. select *, (select r from (select q1 as q2) x, lateral (select q2 as r) y) from int8_tbl;
  99. ----
  100. q1 q2 r
  101. 123 456 123
  102. 123 4567890123456789 123
  103. 4567890123456789 123 4567890123456789
  104. 4567890123456789 4567890123456789 4567890123456789
  105. 4567890123456789 -4567890123456789 4567890123456789
  106. # lateral with function in FROM
  107. query I colnames
  108. select count(*) from tenk1 a, lateral generate_series(1,two) g;
  109. ----
  110. count
  111. 5
  112. query III colnames,rowsort
  113. select * from generate_series(100,200) g (g),
  114. lateral (select * from int8_tbl a where g = q1 union all
  115. select * from int8_tbl b where g = q2) ss
  116. ----
  117. g q1 q2
  118. 123 123 456
  119. 123 123 4567890123456789
  120. 123 4567890123456789 123
  121. query I colnames
  122. select count(*) from tenk1 a,
  123. tenk1 b join lateral (values(a.unique1)) ss(x) on b.unique2 = ss.x;
  124. ----
  125. count
  126. 10
  127. query I colnames
  128. select count(*) from tenk1 a,
  129. tenk1 b join lateral (values(a.unique1),(-1)) ss(x) on b.unique2 = ss.x;
  130. ----
  131. count
  132. 10
  133. # lateral injecting a strange outer join condition
  134. query IIIII colnames
  135. select * from int8_tbl a,
  136. int8_tbl x left join lateral (select a.q1 from int4_tbl y) ss(z)
  137. on x.q2 = ss.z
  138. order by a.q1, a.q2, x.q1, x.q2, ss.z;
  139. ----
  140. q1 q2 q1 q2 z
  141. 123 456 123 456 NULL
  142. 123 456 123 4567890123456789 NULL
  143. 123 456 4567890123456789 -4567890123456789 NULL
  144. 123 456 4567890123456789 123 123
  145. 123 456 4567890123456789 123 123
  146. 123 456 4567890123456789 123 123
  147. 123 456 4567890123456789 123 123
  148. 123 456 4567890123456789 123 123
  149. 123 456 4567890123456789 4567890123456789 NULL
  150. 123 4567890123456789 123 456 NULL
  151. 123 4567890123456789 123 4567890123456789 NULL
  152. 123 4567890123456789 4567890123456789 -4567890123456789 NULL
  153. 123 4567890123456789 4567890123456789 123 123
  154. 123 4567890123456789 4567890123456789 123 123
  155. 123 4567890123456789 4567890123456789 123 123
  156. 123 4567890123456789 4567890123456789 123 123
  157. 123 4567890123456789 4567890123456789 123 123
  158. 123 4567890123456789 4567890123456789 4567890123456789 NULL
  159. 4567890123456789 -4567890123456789 123 456 NULL
  160. 4567890123456789 -4567890123456789 123 4567890123456789 4567890123456789
  161. 4567890123456789 -4567890123456789 123 4567890123456789 4567890123456789
  162. 4567890123456789 -4567890123456789 123 4567890123456789 4567890123456789
  163. 4567890123456789 -4567890123456789 123 4567890123456789 4567890123456789
  164. 4567890123456789 -4567890123456789 123 4567890123456789 4567890123456789
  165. 4567890123456789 -4567890123456789 4567890123456789 -4567890123456789 NULL
  166. 4567890123456789 -4567890123456789 4567890123456789 123 NULL
  167. 4567890123456789 -4567890123456789 4567890123456789 4567890123456789 4567890123456789
  168. 4567890123456789 -4567890123456789 4567890123456789 4567890123456789 4567890123456789
  169. 4567890123456789 -4567890123456789 4567890123456789 4567890123456789 4567890123456789
  170. 4567890123456789 -4567890123456789 4567890123456789 4567890123456789 4567890123456789
  171. 4567890123456789 -4567890123456789 4567890123456789 4567890123456789 4567890123456789
  172. 4567890123456789 123 123 456 NULL
  173. 4567890123456789 123 123 4567890123456789 4567890123456789
  174. 4567890123456789 123 123 4567890123456789 4567890123456789
  175. 4567890123456789 123 123 4567890123456789 4567890123456789
  176. 4567890123456789 123 123 4567890123456789 4567890123456789
  177. 4567890123456789 123 123 4567890123456789 4567890123456789
  178. 4567890123456789 123 4567890123456789 -4567890123456789 NULL
  179. 4567890123456789 123 4567890123456789 123 NULL
  180. 4567890123456789 123 4567890123456789 4567890123456789 4567890123456789
  181. 4567890123456789 123 4567890123456789 4567890123456789 4567890123456789
  182. 4567890123456789 123 4567890123456789 4567890123456789 4567890123456789
  183. 4567890123456789 123 4567890123456789 4567890123456789 4567890123456789
  184. 4567890123456789 123 4567890123456789 4567890123456789 4567890123456789
  185. 4567890123456789 4567890123456789 123 456 NULL
  186. 4567890123456789 4567890123456789 123 4567890123456789 4567890123456789
  187. 4567890123456789 4567890123456789 123 4567890123456789 4567890123456789
  188. 4567890123456789 4567890123456789 123 4567890123456789 4567890123456789
  189. 4567890123456789 4567890123456789 123 4567890123456789 4567890123456789
  190. 4567890123456789 4567890123456789 123 4567890123456789 4567890123456789
  191. 4567890123456789 4567890123456789 4567890123456789 -4567890123456789 NULL
  192. 4567890123456789 4567890123456789 4567890123456789 123 NULL
  193. 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  194. 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  195. 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  196. 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  197. 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  198. # lateral reference to a join alias variable
  199. query III colnames
  200. select * from (select f1/2 as x from int4_tbl) ss1 join int4_tbl i4 on x = f1,
  201. lateral (select x) ss2(y);
  202. ----
  203. x f1 y
  204. 0 0 0
  205. query III colnames,rowsort
  206. select * from (select f1 as x from int4_tbl) ss1 join int4_tbl i4 on x = f1,
  207. lateral (values(x)) ss2(y);
  208. ----
  209. x f1 y
  210. 0 0 0
  211. 123456 123456 123456
  212. -123456 -123456 -123456
  213. 2147483647 2147483647 2147483647
  214. -2147483647 -2147483647 -2147483647
  215. query III colnames
  216. select * from ((select f1/2 as x from int4_tbl) ss1 join int4_tbl i4 on x = f1) j,
  217. lateral (select x) ss2(y);
  218. ----
  219. x f1 y
  220. 0 0 0
  221. # lateral references requiring pullup
  222. query II rowsort
  223. select * from (values(1)) x(lb),
  224. lateral generate_series(lb,4) x4;
  225. ----
  226. 1 1
  227. 1 2
  228. 1 3
  229. 1 4
  230. query II rowsort
  231. select * from (select f1/1000000000 from int4_tbl) x(lb),
  232. lateral generate_series(lb,4) x4;
  233. ----
  234. -2 0
  235. -2 1
  236. -2 2
  237. -2 3
  238. -2 4
  239. -2 -1
  240. -2 -2
  241. 0 0
  242. 0 0
  243. 0 0
  244. 0 1
  245. 0 1
  246. 0 1
  247. 0 2
  248. 0 2
  249. 0 2
  250. 0 3
  251. 0 3
  252. 0 3
  253. 0 4
  254. 0 4
  255. 0 4
  256. 2 2
  257. 2 3
  258. 2 4
  259. query II colnames
  260. select * from (values(1)) x(lb),
  261. lateral (values(lb)) y(lbcopy)
  262. ----
  263. lb lbcopy
  264. 1 1
  265. query II colnames
  266. select * from (values(1)) x(lb),
  267. lateral (select lb from int4_tbl) y(lbcopy);
  268. ----
  269. lb lbcopy
  270. 1 1
  271. 1 1
  272. 1 1
  273. 1 1
  274. 1 1
  275. query IIIIIII colnames,rowsort
  276. select * from
  277. int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1,
  278. lateral (values(x.q1,y.q1,y.q2)) v(xq1,yq1,yq2);
  279. ----
  280. q1 q2 q1 q2 xq1 yq1 yq2
  281. 4567890123456789 -4567890123456789 NULL NULL 4567890123456789 NULL NULL
  282. 4567890123456789 123 123 456 4567890123456789 123 456
  283. 4567890123456789 123 123 4567890123456789 4567890123456789 123 4567890123456789
  284. 123 456 NULL NULL 123 NULL NULL
  285. 123 4567890123456789 4567890123456789 123 123 4567890123456789 123
  286. 123 4567890123456789 4567890123456789 4567890123456789 123 4567890123456789 4567890123456789
  287. 123 4567890123456789 4567890123456789 -4567890123456789 123 4567890123456789 -4567890123456789
  288. 4567890123456789 4567890123456789 4567890123456789 123 4567890123456789 4567890123456789 123
  289. 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  290. 4567890123456789 4567890123456789 4567890123456789 -4567890123456789 4567890123456789 4567890123456789 -4567890123456789
  291. query IIIIIII colnames,rowsort
  292. select * from
  293. int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1,
  294. lateral (select x.q1,y.q1,y.q2) v(xq1,yq1,yq2);
  295. ----
  296. q1 q2 q1 q2 xq1 yq1 yq2
  297. 4567890123456789 -4567890123456789 NULL NULL 4567890123456789 NULL NULL
  298. 4567890123456789 123 123 456 4567890123456789 123 456
  299. 4567890123456789 123 123 4567890123456789 4567890123456789 123 4567890123456789
  300. 123 456 NULL NULL 123 NULL NULL
  301. 123 4567890123456789 4567890123456789 123 123 4567890123456789 123
  302. 123 4567890123456789 4567890123456789 4567890123456789 123 4567890123456789 4567890123456789
  303. 123 4567890123456789 4567890123456789 -4567890123456789 123 4567890123456789 -4567890123456789
  304. 4567890123456789 4567890123456789 4567890123456789 123 4567890123456789 4567890123456789 123
  305. 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  306. 4567890123456789 4567890123456789 4567890123456789 -4567890123456789 4567890123456789 4567890123456789 -4567890123456789
  307. query II colnames,rowsort
  308. select x.* from
  309. int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1,
  310. lateral (select x.q1,y.q1,y.q2) v(xq1,yq1,yq2);
  311. ----
  312. q1 q2
  313. 123 456
  314. 123 4567890123456789
  315. 123 4567890123456789
  316. 123 4567890123456789
  317. 4567890123456789 123
  318. 4567890123456789 123
  319. 4567890123456789 4567890123456789
  320. 4567890123456789 4567890123456789
  321. 4567890123456789 4567890123456789
  322. 4567890123456789 -4567890123456789
  323. query II colnames,rowsort
  324. select v.* from
  325. (int8_tbl x left join (select q1,coalesce(q2,0) q2 from int8_tbl) y on x.q2 = y.q1)
  326. left join int4_tbl z on z.f1 = x.q2,
  327. lateral (select x.q1,y.q1 union all select x.q2,y.q2) v(vx,vy);
  328. ----
  329. vx vy
  330. 123 NULL
  331. 456 NULL
  332. 123 4567890123456789
  333. 4567890123456789 -4567890123456789
  334. 123 4567890123456789
  335. 4567890123456789 4567890123456789
  336. 123 4567890123456789
  337. 4567890123456789 123
  338. 4567890123456789 123
  339. 123 4567890123456789
  340. 4567890123456789 123
  341. 123 456
  342. 4567890123456789 4567890123456789
  343. 4567890123456789 -4567890123456789
  344. 4567890123456789 4567890123456789
  345. 4567890123456789 4567890123456789
  346. 4567890123456789 4567890123456789
  347. 4567890123456789 123
  348. 4567890123456789 NULL
  349. -4567890123456789 NULL
  350. query II colnames,rowsort
  351. select v.* from
  352. (int8_tbl x left join (select q1,(select coalesce(q2,0)) q2 from int8_tbl) y on x.q2 = y.q1)
  353. left join int4_tbl z on z.f1 = x.q2,
  354. lateral (select x.q1,y.q1 union all select x.q2,y.q2) v(vx,vy);
  355. ----
  356. vx vy
  357. 4567890123456789 NULL
  358. -4567890123456789 NULL
  359. 4567890123456789 123
  360. 123 456
  361. 4567890123456789 123
  362. 123 4567890123456789
  363. 123 NULL
  364. 456 NULL
  365. 123 4567890123456789
  366. 4567890123456789 123
  367. 123 4567890123456789
  368. 4567890123456789 4567890123456789
  369. 123 4567890123456789
  370. 4567890123456789 -4567890123456789
  371. 4567890123456789 4567890123456789
  372. 4567890123456789 123
  373. 4567890123456789 4567890123456789
  374. 4567890123456789 4567890123456789
  375. 4567890123456789 4567890123456789
  376. 4567890123456789 -4567890123456789
  377. query II colnames,rowsort
  378. select v.* from
  379. (int8_tbl x left join (select q1,(select coalesce(q2,0)) q2 from int8_tbl) y on x.q2 = y.q1)
  380. left join int4_tbl z on z.f1 = x.q2,
  381. lateral (select x.q1,y.q1 from onerow union all select x.q2,y.q2 from onerow) v(vx,vy);
  382. ----
  383. vx vy
  384. 4567890123456789 123
  385. 123 456
  386. 4567890123456789 123
  387. 123 4567890123456789
  388. 4567890123456789 4567890123456789
  389. 4567890123456789 123
  390. 123 4567890123456789
  391. 4567890123456789 123
  392. 4567890123456789 4567890123456789
  393. 4567890123456789 4567890123456789
  394. 123 4567890123456789
  395. 4567890123456789 4567890123456789
  396. 4567890123456789 4567890123456789
  397. 4567890123456789 -4567890123456789
  398. 123 4567890123456789
  399. 4567890123456789 -4567890123456789
  400. 123 NULL
  401. 456 NULL
  402. 4567890123456789 NULL
  403. -4567890123456789 NULL
  404. query IIIII rowsort
  405. select * from
  406. int8_tbl a left join
  407. lateral (select *, a.q2 as x from int8_tbl b) ss on a.q2 = ss.q1;
  408. ----
  409. 123 456 NULL NULL NULL
  410. 123 4567890123456789 4567890123456789 -4567890123456789 4567890123456789
  411. 123 4567890123456789 4567890123456789 123 4567890123456789
  412. 123 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  413. 4567890123456789 -4567890123456789 NULL NULL NULL
  414. 4567890123456789 123 123 456 123
  415. 4567890123456789 123 123 4567890123456789 123
  416. 4567890123456789 4567890123456789 4567890123456789 -4567890123456789 4567890123456789
  417. 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  418. 4567890123456789 4567890123456789 4567890123456789 123 4567890123456789
  419. query IIIII colnames,rowsort
  420. select * from
  421. int8_tbl a left join
  422. lateral (select *, coalesce(a.q2, 42) as x from int8_tbl b) ss on a.q2 = ss.q1;
  423. ----
  424. q1 q2 q1 q2 x
  425. 123 456 NULL NULL NULL
  426. 123 4567890123456789 4567890123456789 123 4567890123456789
  427. 123 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  428. 123 4567890123456789 4567890123456789 -4567890123456789 4567890123456789
  429. 4567890123456789 123 123 456 123
  430. 4567890123456789 123 123 4567890123456789 123
  431. 4567890123456789 4567890123456789 4567890123456789 123 4567890123456789
  432. 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  433. 4567890123456789 4567890123456789 4567890123456789 -4567890123456789 4567890123456789
  434. 4567890123456789 -4567890123456789 NULL NULL NULL
  435. # lateral can result in join conditions appearing below their
  436. # real semantic level
  437. query II colnames,rowsort
  438. select * from int4_tbl i left join
  439. lateral (select * from int2_tbl j where i.f1 = j.f1) k on true;
  440. ----
  441. f1 f1
  442. 0 0
  443. 123456 NULL
  444. -123456 NULL
  445. 2147483647 NULL
  446. -2147483647 NULL
  447. query IT colnames,rowsort
  448. select * from int4_tbl left join
  449. lateral (select coalesce(int4_tbl) from int2_tbl j where int4_tbl.f1 = j.f1) k on true;
  450. ----
  451. f1 coalesce
  452. 0 (0)
  453. 123456 NULL
  454. -123456 NULL
  455. 2147483647 NULL
  456. -2147483647 NULL
  457. query IIII colnames,rowsort
  458. select * from int4_tbl a,
  459. lateral (
  460. select * from int4_tbl b left join int8_tbl c on (b.f1 = q1 and a.f1 = q2)
  461. ) ss;
  462. ----
  463. f1 f1 q1 q2
  464. 0 0 NULL NULL
  465. 0 123456 NULL NULL
  466. 0 -123456 NULL NULL
  467. 0 2147483647 NULL NULL
  468. 0 -2147483647 NULL NULL
  469. 123456 0 NULL NULL
  470. 123456 123456 NULL NULL
  471. 123456 -123456 NULL NULL
  472. 123456 2147483647 NULL NULL
  473. 123456 -2147483647 NULL NULL
  474. -123456 0 NULL NULL
  475. -123456 123456 NULL NULL
  476. -123456 -123456 NULL NULL
  477. -123456 2147483647 NULL NULL
  478. -123456 -2147483647 NULL NULL
  479. 2147483647 0 NULL NULL
  480. 2147483647 123456 NULL NULL
  481. 2147483647 -123456 NULL NULL
  482. 2147483647 2147483647 NULL NULL
  483. 2147483647 -2147483647 NULL NULL
  484. -2147483647 0 NULL NULL
  485. -2147483647 123456 NULL NULL
  486. -2147483647 -123456 NULL NULL
  487. -2147483647 2147483647 NULL NULL
  488. -2147483647 -2147483647 NULL NULL
  489. # TODO(benesch): least function is unsupported.
  490. #
  491. # lateral reference in a PlaceHolderVar evaluated at join level
  492. # query IIIII colnames,rowsort
  493. # select * from
  494. # int8_tbl a left join lateral
  495. # (select b.q1 as bq1, c.q1 as cq1, least(a.q1,b.q1,c.q1) from
  496. # int8_tbl b cross join int8_tbl c) ss
  497. # on a.q2 = ss.bq1;
  498. # ----
  499. # q1 q2 bq1 cq1 least
  500. # 123 456 NULL NULL NULL
  501. # 123 4567890123456789 4567890123456789 123 123
  502. # 123 4567890123456789 4567890123456789 123 123
  503. # 123 4567890123456789 4567890123456789 123 123
  504. # 123 4567890123456789 4567890123456789 123 123
  505. # 123 4567890123456789 4567890123456789 123 123
  506. # 123 4567890123456789 4567890123456789 123 123
  507. # 123 4567890123456789 4567890123456789 4567890123456789 123
  508. # 123 4567890123456789 4567890123456789 4567890123456789 123
  509. # 123 4567890123456789 4567890123456789 4567890123456789 123
  510. # 123 4567890123456789 4567890123456789 4567890123456789 123
  511. # 123 4567890123456789 4567890123456789 4567890123456789 123
  512. # 123 4567890123456789 4567890123456789 4567890123456789 123
  513. # 123 4567890123456789 4567890123456789 4567890123456789 123
  514. # 123 4567890123456789 4567890123456789 4567890123456789 123
  515. # 123 4567890123456789 4567890123456789 4567890123456789 123
  516. # 4567890123456789 123 123 123 123
  517. # 4567890123456789 123 123 123 123
  518. # 4567890123456789 123 123 123 123
  519. # 4567890123456789 123 123 123 123
  520. # 4567890123456789 123 123 4567890123456789 123
  521. # 4567890123456789 123 123 4567890123456789 123
  522. # 4567890123456789 123 123 4567890123456789 123
  523. # 4567890123456789 123 123 4567890123456789 123
  524. # 4567890123456789 123 123 4567890123456789 123
  525. # 4567890123456789 123 123 4567890123456789 123
  526. # 4567890123456789 4567890123456789 4567890123456789 123 123
  527. # 4567890123456789 4567890123456789 4567890123456789 123 123
  528. # 4567890123456789 4567890123456789 4567890123456789 123 123
  529. # 4567890123456789 4567890123456789 4567890123456789 123 123
  530. # 4567890123456789 4567890123456789 4567890123456789 123 123
  531. # 4567890123456789 4567890123456789 4567890123456789 123 123
  532. # 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  533. # 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  534. # 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  535. # 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  536. # 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  537. # 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  538. # 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  539. # 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  540. # 4567890123456789 4567890123456789 4567890123456789 4567890123456789 4567890123456789
  541. # 4567890123456789 -4567890123456789 NULL NULL NULL
  542. # check handling of nested appendrels inside LATERAL
  543. query II colnames,rowsort
  544. select * from
  545. ((select 2 as v) union all (select 3 as v)) as q1
  546. cross join lateral
  547. ((select * from
  548. ((select 4 as v) union all (select 5 as v)) as q3)
  549. union all
  550. (select q1.v)
  551. ) as q2;
  552. ----
  553. v v
  554. 2 4
  555. 2 5
  556. 2 2
  557. 3 4
  558. 3 5
  559. 3 3
  560. # check we don't try to do a unique-ified semijoin with LATERAL
  561. query III colnames
  562. select * from
  563. (values (0,4), (1,1000)) v(id,x),
  564. lateral (select f1 from int4_tbl
  565. where f1 = any (select unique1 from tenk1
  566. where unique2 = v.x offset 0)) ss;
  567. ----
  568. id x f1
  569. 0 4 0
  570. query error column "f1" does not exist
  571. select f1,g from int4_tbl a, (select f1 as g) ss;
  572. query error column "a.f1" does not exist
  573. select f1,g from int4_tbl a, (select a.f1 as g) ss;
  574. query error column "f1" does not exist
  575. select f1,g from int4_tbl a cross join (select f1 as g) ss;
  576. query error column "a.f1" does not exist
  577. select f1,g from int4_tbl a cross join (select a.f1 as g) ss;
  578. # SQL:2008 says the left table is in scope but illegal to access here
  579. query error the combining JOIN type must be INNER or LEFT for a LATERAL reference
  580. select f1,g from int4_tbl a right join lateral generate_series(0, a.f1) g on true;
  581. query error the combining JOIN type must be INNER or LEFT for a LATERAL reference
  582. select f1,g from int4_tbl a full join lateral generate_series(0, a.f1) g on true;
  583. # check we complain about ambiguous table references
  584. # query error table reference "x" is ambiguous
  585. # select * from
  586. # int8_tbl x cross join (int4_tbl x cross join lateral (select x.f1) ss);
  587. # LATERAL can be used to put an aggregate into the FROM clause of its query
  588. #
  589. # TODO(benesch): when we support aggregates that refer exclusively to outer
  590. # columns, this case should still be disallowed, but with the following error
  591. # message instead:
  592. #
  593. # aggregate functions are not allowed in FROM clause of their own query level
  594. #
  595. # See: https://www.postgresql.org/message-id/1375925710.17807.13.camel%40vanquo.pezone.net
  596. query error aggregate functions that refer exclusively to outer columns not yet supported
  597. select 1 from tenk1 a, lateral (select max(a.unique1) from int4_tbl b) ss;