srfs.slt 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. # Copyright 2015 - 2019 The Cockroach Authors. All rights reserved.
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # This file is derived from the logic test suite in CockroachDB. The
  12. # original file was retrieved on June 10, 2019 from:
  13. #
  14. # https://github.com/cockroachdb/cockroach/blob/d2f7fbf5dd1fc1a099bbad790a2e1f7c60a66cc3/pkg/sql/logictest/testdata/logic_test/srfs
  15. #
  16. # The original source code is subject to the terms of the Apache
  17. # 2.0 license, a copy of which can be found in the LICENSE file at the
  18. # root of this repository.
  19. # not supported yet
  20. halt
  21. mode cockroach
  22. subtest generate_series
  23. query I
  24. SELECT * FROM generate_series(1, NULL)
  25. ----
  26. query I colnames
  27. SELECT * FROM generate_series(1, 3)
  28. ----
  29. generate_series
  30. 1
  31. 2
  32. 3
  33. query T colnames
  34. SELECT * FROM generate_series('2017-11-11 00:00:00'::TIMESTAMP, '2017-11-11 03:00:00'::TIMESTAMP, '1 hour')
  35. ----
  36. generate_series
  37. 2017-11-11 00:00:00 +0000 +0000
  38. 2017-11-11 01:00:00 +0000 +0000
  39. 2017-11-11 02:00:00 +0000 +0000
  40. 2017-11-11 03:00:00 +0000 +0000
  41. query T colnames
  42. SELECT * FROM generate_series('2017-11-11 03:00:00'::TIMESTAMP, '2017-11-11 00:00:00'::TIMESTAMP, '-1 hour')
  43. ----
  44. generate_series
  45. 2017-11-11 03:00:00 +0000 +0000
  46. 2017-11-11 02:00:00 +0000 +0000
  47. 2017-11-11 01:00:00 +0000 +0000
  48. 2017-11-11 00:00:00 +0000 +0000
  49. query T colnames
  50. SELECT * FROM generate_series('2017-11-11 03:00:00'::TIMESTAMP, '2017-11-15 00:00:00'::TIMESTAMP, '1 day')
  51. ----
  52. generate_series
  53. 2017-11-11 03:00:00 +0000 +0000
  54. 2017-11-12 03:00:00 +0000 +0000
  55. 2017-11-13 03:00:00 +0000 +0000
  56. 2017-11-14 03:00:00 +0000 +0000
  57. query T colnames
  58. SELECT * FROM generate_series('2017-01-15 03:00:00'::TIMESTAMP, '2017-12-15 00:00:00'::TIMESTAMP, '1 month')
  59. ----
  60. generate_series
  61. 2017-01-15 03:00:00 +0000 +0000
  62. 2017-02-15 03:00:00 +0000 +0000
  63. 2017-03-15 03:00:00 +0000 +0000
  64. 2017-04-15 03:00:00 +0000 +0000
  65. 2017-05-15 03:00:00 +0000 +0000
  66. 2017-06-15 03:00:00 +0000 +0000
  67. 2017-07-15 03:00:00 +0000 +0000
  68. 2017-08-15 03:00:00 +0000 +0000
  69. 2017-09-15 03:00:00 +0000 +0000
  70. 2017-10-15 03:00:00 +0000 +0000
  71. 2017-11-15 03:00:00 +0000 +0000
  72. # Check what happens when we step through February in a leap year, starting on Jan 31.
  73. # This output is consistent with PostgreSQL 10.
  74. query T colnames
  75. SELECT * FROM generate_series('2016-01-31 03:00:00'::TIMESTAMP, '2016-12-31 00:00:00'::TIMESTAMP, '1 month')
  76. ----
  77. generate_series
  78. 2016-01-31 03:00:00 +0000 +0000
  79. 2016-02-29 03:00:00 +0000 +0000
  80. 2016-03-29 03:00:00 +0000 +0000
  81. 2016-04-29 03:00:00 +0000 +0000
  82. 2016-05-29 03:00:00 +0000 +0000
  83. 2016-06-29 03:00:00 +0000 +0000
  84. 2016-07-29 03:00:00 +0000 +0000
  85. 2016-08-29 03:00:00 +0000 +0000
  86. 2016-09-29 03:00:00 +0000 +0000
  87. 2016-10-29 03:00:00 +0000 +0000
  88. 2016-11-29 03:00:00 +0000 +0000
  89. 2016-12-29 03:00:00 +0000 +0000
  90. # Similar to the previous, but we don't hit a 30-day month until July.
  91. query T colnames
  92. SELECT * FROM generate_series('2016-01-31 03:00:00'::TIMESTAMP, '2016-12-31 00:00:00'::TIMESTAMP, '2 month')
  93. ----
  94. generate_series
  95. 2016-01-31 03:00:00 +0000 +0000
  96. 2016-03-31 03:00:00 +0000 +0000
  97. 2016-05-31 03:00:00 +0000 +0000
  98. 2016-07-31 03:00:00 +0000 +0000
  99. 2016-09-30 03:00:00 +0000 +0000
  100. 2016-11-30 03:00:00 +0000 +0000
  101. # Verify rollover when we're adding by months, days, and hours
  102. query T colnames
  103. SELECT * FROM generate_series('2016-01-30 22:00:00'::TIMESTAMP, '2016-12-31 00:00:00'::TIMESTAMP, '1 month 1 day 1 hour')
  104. ----
  105. generate_series
  106. 2016-01-30 22:00:00 +0000 +0000
  107. 2016-03-01 23:00:00 +0000 +0000
  108. 2016-04-03 00:00:00 +0000 +0000
  109. 2016-05-04 01:00:00 +0000 +0000
  110. 2016-06-05 02:00:00 +0000 +0000
  111. 2016-07-06 03:00:00 +0000 +0000
  112. 2016-08-07 04:00:00 +0000 +0000
  113. 2016-09-08 05:00:00 +0000 +0000
  114. 2016-10-09 06:00:00 +0000 +0000
  115. 2016-11-10 07:00:00 +0000 +0000
  116. 2016-12-11 08:00:00 +0000 +0000
  117. query T colnames
  118. SELECT * FROM generate_series('1996-02-29 22:00:00'::TIMESTAMP, '2004-03-01 00:00:00'::TIMESTAMP, '4 year')
  119. ----
  120. generate_series
  121. 1996-02-29 22:00:00 +0000 +0000
  122. 2000-02-29 22:00:00 +0000 +0000
  123. 2004-02-29 22:00:00 +0000 +0000
  124. query T colnames
  125. SELECT * FROM generate_series('2017-11-11 00:00:00'::TIMESTAMP, '2017-11-11 03:00:00'::TIMESTAMP, '-1 hour')
  126. ----
  127. generate_series
  128. query II colnames,rowsort
  129. SELECT * FROM generate_series(1, 2), generate_series(1, 2)
  130. ----
  131. generate_series generate_series
  132. 1 1
  133. 1 2
  134. 2 1
  135. 2 2
  136. query I colnames
  137. SELECT * FROM generate_series(3, 1, -1)
  138. ----
  139. generate_series
  140. 3
  141. 2
  142. 1
  143. query I colnames
  144. SELECT * FROM generate_series(3, 1)
  145. ----
  146. generate_series
  147. query error step cannot be 0
  148. SELECT * FROM generate_series(1, 3, 0)
  149. query I colnames
  150. SELECT * FROM PG_CATALOG.generate_series(1, 3)
  151. ----
  152. generate_series
  153. 1
  154. 2
  155. 3
  156. query I colnames
  157. SELECT * FROM generate_series(1, 1) AS c(x)
  158. ----
  159. x
  160. 1
  161. query II colnames
  162. SELECT * FROM generate_series(1, 1) WITH ORDINALITY
  163. ----
  164. generate_series ordinality
  165. 1 1
  166. query II colnames
  167. SELECT * FROM generate_series(1, 1) WITH ORDINALITY AS c(x, y)
  168. ----
  169. x y
  170. 1 1
  171. query error generator functions are not allowed in LIMIT
  172. SELECT * FROM (VALUES (1)) LIMIT generate_series(1, 3)
  173. query I colnames
  174. SELECT generate_series(1, 2)
  175. ----
  176. generate_series
  177. 1
  178. 2
  179. subtest multiple_SRFs
  180. query II colnames
  181. SELECT generate_series(1, 2), generate_series(3, 4)
  182. ----
  183. generate_series generate_series
  184. 1 3
  185. 2 4
  186. query II
  187. SELECT generate_series(1, 2), generate_series(3, 4)
  188. ----
  189. 1 3
  190. 2 4
  191. statement ok
  192. CREATE TABLE t (a string)
  193. statement ok
  194. CREATE TABLE u (b string)
  195. statement ok
  196. INSERT INTO t VALUES ('cat')
  197. statement ok
  198. INSERT INTO u VALUES ('bird')
  199. query TTII colnames
  200. SELECT t.*, u.*, generate_series(1,2), generate_series(3, 4) FROM t, u
  201. ----
  202. a b generate_series generate_series
  203. cat bird 1 3
  204. cat bird 2 4
  205. query TTII colnames,rowsort
  206. SELECT t.*, u.*, a.*, b.* FROM t, u, generate_series(1, 2) AS a, generate_series(3, 4) AS b
  207. ----
  208. a b a b
  209. cat bird 1 3
  210. cat bird 1 4
  211. cat bird 2 3
  212. cat bird 2 4
  213. query I colnames
  214. SELECT 3 + x AS r FROM generate_series(1,2) AS a(x)
  215. ----
  216. r
  217. 4
  218. 5
  219. query I colnames
  220. SELECT 3 + generate_series(1,2) AS r
  221. ----
  222. r
  223. 4
  224. 5
  225. query I colnames
  226. SELECT 3 + (3 * generate_series(1,3)) AS r
  227. ----
  228. r
  229. 6
  230. 9
  231. 12
  232. subtest srf_ordering
  233. statement ok
  234. CREATE TABLE ordered_t(x INT PRIMARY KEY);
  235. INSERT INTO ordered_t VALUES (0), (1)
  236. query II colnames
  237. SELECT x, generate_series(3, x, -1) FROM ordered_t ORDER BY 1, 2;
  238. ----
  239. x generate_series
  240. 0 0
  241. 0 1
  242. 0 2
  243. 0 3
  244. 1 1
  245. 1 2
  246. 1 3
  247. subtest unnest
  248. statement error could not determine polymorphic type
  249. SELECT * FROM unnest(NULL)
  250. statement error could not determine polymorphic type
  251. SELECT unnest(NULL)
  252. query I colnames
  253. SELECT * from unnest(ARRAY[1,2])
  254. ----
  255. unnest
  256. 1
  257. 2
  258. query IT
  259. SELECT unnest(ARRAY[1,2]), unnest(ARRAY['a', 'b'])
  260. ----
  261. 1 a
  262. 2 b
  263. query I colnames
  264. SELECT unnest(ARRAY[3,4]) - 2 AS r
  265. ----
  266. r
  267. 1
  268. 2
  269. query II colnames
  270. SELECT 1 + generate_series(0, 1) AS r, unnest(ARRAY[2, 4]) - 1 AS t
  271. ----
  272. r t
  273. 1 1
  274. 2 3
  275. query II
  276. SELECT 1 + generate_series(0, 1), unnest(ARRAY[2, 4]) - 1
  277. ----
  278. 1 1
  279. 2 3
  280. query I colnames
  281. SELECT ascii(unnest(ARRAY['a', 'b', 'c']));
  282. ----
  283. ascii
  284. 97
  285. 98
  286. 99
  287. subtest nested_SRF
  288. # See materialize#20511
  289. query error unimplemented: nested set-returning functions
  290. SELECT generate_series(generate_series(1, 3), 3)
  291. query I
  292. SELECT generate_series(1, 3) + generate_series(1, 3)
  293. ----
  294. 2
  295. 4
  296. 6
  297. query error pq: column "generate_series" does not exist
  298. SELECT generate_series(1, 3) FROM t WHERE generate_series > 3
  299. # Regressions for materialize#15900: ensure that null parameters to generate_series don't
  300. # cause issues.
  301. query T colnames
  302. SELECT * from generate_series(1, (select * from generate_series(1, 0)))
  303. ----
  304. generate_series
  305. # The following query is designed to produce a null array argument to unnest
  306. # in a way that the type system can't detect before evaluation.
  307. query T colnames
  308. SELECT unnest((SELECT current_schemas((SELECT isnan((SELECT round(3.4, (SELECT generate_series(1, 0)))))))));
  309. ----
  310. unnest
  311. query T colnames
  312. SELECT information_schema._pg_expandarray((SELECT current_schemas((SELECT isnan((SELECT round(3.4, (SELECT generate_series(1, 0)))))))));
  313. ----
  314. information_schema._pg_expandarray
  315. # Regression for materialize#18021.
  316. query I colnames
  317. SELECT generate_series(9223372036854775807::int, -9223372036854775807::int, -9223372036854775807::int)
  318. ----
  319. generate_series
  320. 9223372036854775807
  321. 0
  322. -9223372036854775807
  323. subtest pg_get_keywords
  324. # pg_get_keywords for compatibility (materialize#10291)
  325. query TTT colnames
  326. SELECT * FROM pg_get_keywords() WHERE word IN ('alter', 'and', 'between', 'cross') ORDER BY word
  327. ----
  328. word catcode catdesc
  329. alter U unreserved
  330. and R reserved
  331. between C unreserved (cannot be function or type name)
  332. cross T reserved (can be function or type name)
  333. # Postgres enables renaming both the source and the column name for
  334. # single-column generators, but not for multi-column generators.
  335. query IITTT colnames
  336. SELECT a.*, b.*, c.* FROM generate_series(1,1) a, unnest(ARRAY[1]) b, pg_get_keywords() c LIMIT 0
  337. ----
  338. a b word catcode catdesc
  339. # Regression for cockroach#36501: the column from a single-column SRF should not be
  340. # renamed because of a higher-level table alias.
  341. query I colnames
  342. SELECT * FROM (SELECT * FROM generate_series(1, 2)) AS a
  343. ----
  344. generate_series
  345. 1
  346. 2
  347. query I colnames
  348. SELECT * FROM (SELECT unnest(ARRAY[1])) AS tablealias
  349. ----
  350. unnest
  351. 1
  352. query I colnames
  353. SELECT * FROM (SELECT unnest(ARRAY[1]) AS colalias) AS tablealias
  354. ----
  355. colalias
  356. 1
  357. query II
  358. SELECT * FROM
  359. (SELECT unnest(ARRAY[1]) AS filter_id2) AS uq
  360. JOIN
  361. (SELECT unnest(ARRAY[1]) AS filter_id) AS ab
  362. ON uq.filter_id2 = ab.filter_id
  363. ----
  364. 1 1
  365. # Beware of multi-valued SRFs in render position (database-issues#5675)
  366. query TTT colnames
  367. SELECT 'a' AS a, pg_get_keywords(), 'c' AS c LIMIT 1
  368. ----
  369. a pg_get_keywords c
  370. a (abort,U,unreserved) c
  371. query TTT colnames
  372. SELECT 'a' AS a, pg_get_keywords() AS b, 'c' AS c LIMIT 1
  373. ----
  374. a b c
  375. a (abort,U,unreserved) c
  376. subtest unary_table
  377. query TTT colnames
  378. SELECT 'a' AS a, crdb_internal.unary_table() AS b, 'c' AS c LIMIT 1
  379. ----
  380. a b c
  381. a () c
  382. subtest upper
  383. # Regular scalar functions can be used as functions too. materialize#22312
  384. query T colnames
  385. SELECT * FROM upper('abc')
  386. ----
  387. upper
  388. ABC
  389. subtest current_schema
  390. query TI colnames
  391. SELECT * FROM current_schema() WITH ORDINALITY AS a(b)
  392. ----
  393. b ordinality
  394. public 1
  395. subtest expandArray
  396. query error pq: unknown signature: information_schema._pg_expandarray()
  397. SELECT information_schema._pg_expandarray()
  398. query error pq: unknown signature: information_schema._pg_expandarray()
  399. SELECT * FROM information_schema._pg_expandarray()
  400. query error pq: information_schema\._pg_expandarray\(\): cannot determine type of empty array\. Consider annotating with the desired type, for example ARRAY\[\]:::int\[\]
  401. SELECT information_schema._pg_expandarray(ARRAY[])
  402. query error pq: information_schema\._pg_expandarray\(\): cannot determine type of empty array\. Consider annotating with the desired type, for example ARRAY\[\]:::int\[\]
  403. SELECT * FROM information_schema._pg_expandarray(ARRAY[])
  404. statement error could not determine polymorphic type
  405. SELECT * FROM information_schema._pg_expandarray(NULL)
  406. statement error could not determine polymorphic type
  407. SELECT information_schema._pg_expandarray(NULL)
  408. query I colnames
  409. SELECT information_schema._pg_expandarray(ARRAY[]:::int[])
  410. ----
  411. information_schema._pg_expandarray
  412. query II colnames
  413. SELECT * FROM information_schema._pg_expandarray(ARRAY[]:::int[])
  414. ----
  415. x n
  416. query T colnames
  417. SELECT information_schema._pg_expandarray(ARRAY[100])
  418. ----
  419. information_schema._pg_expandarray
  420. (100,1)
  421. query II colnames
  422. SELECT * FROM information_schema._pg_expandarray(ARRAY[100])
  423. ----
  424. x n
  425. 100 1
  426. query T colnames
  427. SELECT information_schema._pg_expandarray(ARRAY[2, 1])
  428. ----
  429. information_schema._pg_expandarray
  430. (2,1)
  431. (1,2)
  432. query II colnames
  433. SELECT * FROM information_schema._pg_expandarray(ARRAY[2, 1])
  434. ----
  435. x n
  436. 2 1
  437. 1 2
  438. query T colnames
  439. SELECT information_schema._pg_expandarray(ARRAY[3, 2, 1])
  440. ----
  441. information_schema._pg_expandarray
  442. (3,1)
  443. (2,2)
  444. (1,3)
  445. query II colnames
  446. SELECT * FROM information_schema._pg_expandarray(ARRAY[3, 2, 1])
  447. ----
  448. x n
  449. 3 1
  450. 2 2
  451. 1 3
  452. query T colnames
  453. SELECT information_schema._pg_expandarray(ARRAY['a'])
  454. ----
  455. information_schema._pg_expandarray
  456. (a,1)
  457. query TI colnames
  458. SELECT * FROM information_schema._pg_expandarray(ARRAY['a'])
  459. ----
  460. x n
  461. a 1
  462. query T colnames
  463. SELECT information_schema._pg_expandarray(ARRAY['b', 'a'])
  464. ----
  465. information_schema._pg_expandarray
  466. (b,1)
  467. (a,2)
  468. query TI colnames
  469. SELECT * FROM information_schema._pg_expandarray(ARRAY['b', 'a'])
  470. ----
  471. x n
  472. b 1
  473. a 2
  474. query T colnames
  475. SELECT information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])
  476. ----
  477. information_schema._pg_expandarray
  478. (c,1)
  479. (b,2)
  480. (a,3)
  481. query TI colnames
  482. SELECT * FROM information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])
  483. ----
  484. x n
  485. c 1
  486. b 2
  487. a 3
  488. subtest srf_accessor
  489. query error pq: type int is not composite
  490. SELECT (1).*
  491. query error pq: type int is not composite
  492. SELECT ((1)).*
  493. query error pq: type int is not composite
  494. SELECT (1).x
  495. query error pq: type int is not composite
  496. SELECT ((1)).x
  497. query error pq: type text is not composite
  498. SELECT ('a').*
  499. query error pq: type text is not composite
  500. SELECT (('a')).*
  501. query error pq: type text is not composite
  502. SELECT ('a').x
  503. query error pq: type text is not composite
  504. SELECT (('a')).x
  505. query error pq: unnest\(\): cannot determine type of empty array. Consider annotating with the desired type, for example ARRAY\[\]:::int\[\]
  506. SELECT (unnest(ARRAY[])).*
  507. query error type int is not composite
  508. SELECT (unnest(ARRAY[]:::INT[])).*
  509. subtest multi_column
  510. query TI colnames
  511. SELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).*
  512. ----
  513. x n
  514. c 1
  515. b 2
  516. a 3
  517. query T colnames
  518. SELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).x
  519. ----
  520. x
  521. c
  522. b
  523. a
  524. query I colnames
  525. SELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).n
  526. ----
  527. n
  528. 1
  529. 2
  530. 3
  531. query error pq: could not identify column "other" in tuple{string AS x, int AS n}
  532. SELECT (information_schema._pg_expandarray(ARRAY['c', 'b', 'a'])).other
  533. query T colnames
  534. SELECT temp.x from information_schema._pg_expandarray(array['c','b','a']) AS temp;
  535. ----
  536. x
  537. c
  538. b
  539. a
  540. query I colnames
  541. SELECT temp.n from information_schema._pg_expandarray(array['c','b','a']) AS temp;
  542. ----
  543. n
  544. 1
  545. 2
  546. 3
  547. query error pq: column "temp.other" does not exist
  548. SELECT temp.other from information_schema._pg_expandarray(array['c','b','a']) AS temp;
  549. query TI colnames
  550. SELECT temp.* from information_schema._pg_expandarray(array['c','b','a']) AS temp;
  551. ----
  552. x n
  553. c 1
  554. b 2
  555. a 3
  556. query TI colnames
  557. SELECT * from information_schema._pg_expandarray(array['c','b','a']) AS temp;
  558. ----
  559. x n
  560. c 1
  561. b 2
  562. a 3
  563. query I colnames
  564. SELECT (i.keys).n FROM (SELECT information_schema._pg_expandarray(ARRAY[3,2,1]) AS keys) AS i
  565. ----
  566. n
  567. 1
  568. 2
  569. 3
  570. query II colnames
  571. SELECT (i.keys).* FROM (SELECT information_schema._pg_expandarray(ARRAY[3,2,1]) AS keys) AS i
  572. ----
  573. x n
  574. 3 1
  575. 2 2
  576. 1 3
  577. query T
  578. SELECT ((i.keys).*, 123) FROM (SELECT information_schema._pg_expandarray(ARRAY[3,2,1]) AS keys) AS i
  579. ----
  580. ("(3,1)",123)
  581. ("(2,2)",123)
  582. ("(1,3)",123)
  583. subtest generate_subscripts
  584. # Basic use cases
  585. query I colnames
  586. SELECT * FROM generate_subscripts(ARRAY[3,2,1])
  587. ----
  588. generate_subscripts
  589. 1
  590. 2
  591. 3
  592. query I colnames
  593. SELECT * FROM generate_subscripts(ARRAY[3,2,1], 1)
  594. ----
  595. generate_subscripts
  596. 1
  597. 2
  598. 3
  599. query I colnames
  600. SELECT * FROM generate_subscripts(ARRAY[3,2,1], 1, false)
  601. ----
  602. generate_subscripts
  603. 1
  604. 2
  605. 3
  606. query I colnames
  607. SELECT * FROM generate_subscripts(ARRAY[3,2,1], 1, true)
  608. ----
  609. generate_subscripts
  610. 3
  611. 2
  612. 1
  613. query I colnames
  614. SELECT generate_subscripts('{NULL,1,NULL,2}'::int[], 1) AS s
  615. ----
  616. s
  617. 1
  618. 2
  619. 3
  620. 4
  621. query I colnames
  622. SELECT generate_subscripts('{NULL,1,NULL,2}'::int[], 1, true) AS s
  623. ----
  624. s
  625. 4
  626. 3
  627. 2
  628. 1
  629. # With a non-valid dimension (only 1 should return any rows)
  630. query I colnames
  631. SELECT * FROM generate_subscripts(ARRAY[3,2,1], 2)
  632. ----
  633. generate_subscripts
  634. query I colnames
  635. SELECT * FROM generate_subscripts(ARRAY[3,2,1], 2, false)
  636. ----
  637. generate_subscripts
  638. query I colnames
  639. SELECT * FROM generate_subscripts(ARRAY[3,2,1], 2, true)
  640. ----
  641. generate_subscripts
  642. query I colnames
  643. SELECT * FROM generate_subscripts(ARRAY[3,2,1], 0)
  644. ----
  645. generate_subscripts
  646. query I colnames
  647. SELECT * FROM generate_subscripts(ARRAY[3,2,1], 0, false)
  648. ----
  649. generate_subscripts
  650. query I colnames
  651. SELECT * FROM generate_subscripts(ARRAY[3,2,1], 0, true)
  652. ----
  653. generate_subscripts
  654. query I colnames
  655. SELECT * FROM generate_subscripts(ARRAY[3,2,1], -1)
  656. ----
  657. generate_subscripts
  658. query I colnames
  659. SELECT * FROM generate_subscripts(ARRAY[3,2,1], -1, false)
  660. ----
  661. generate_subscripts
  662. query I colnames
  663. SELECT * FROM generate_subscripts(ARRAY[3,2,1], -1, true)
  664. ----
  665. generate_subscripts
  666. # With an empty array
  667. query I colnames
  668. SELECT * FROM generate_subscripts(ARRAY[]:::int[])
  669. ----
  670. generate_subscripts
  671. query I colnames
  672. SELECT * FROM generate_subscripts(ARRAY[]:::int[], 1)
  673. ----
  674. generate_subscripts
  675. query I colnames
  676. SELECT * FROM generate_subscripts(ARRAY[]:::string[], 1, false)
  677. ----
  678. generate_subscripts
  679. query I colnames
  680. SELECT * FROM generate_subscripts(ARRAY[]:::bool[], 1, true)
  681. ----
  682. generate_subscripts
  683. query I colnames
  684. SELECT * FROM generate_subscripts(ARRAY[]:::int[], 0)
  685. ----
  686. generate_subscripts
  687. query I colnames
  688. SELECT * FROM generate_subscripts(ARRAY[]:::string[], -1, false)
  689. ----
  690. generate_subscripts
  691. query I colnames
  692. SELECT * FROM generate_subscripts(ARRAY[]:::bool[], 2, true)
  693. ----
  694. generate_subscripts
  695. # With an array with only one value
  696. query I colnames
  697. SELECT * FROM generate_subscripts(ARRAY[100])
  698. ----
  699. generate_subscripts
  700. 1
  701. query I colnames
  702. SELECT * FROM generate_subscripts(ARRAY[100], 1)
  703. ----
  704. generate_subscripts
  705. 1
  706. query I colnames
  707. SELECT * FROM generate_subscripts(ARRAY['b'], 1, false)
  708. ----
  709. generate_subscripts
  710. 1
  711. query I colnames
  712. SELECT * FROM generate_subscripts(ARRAY[true], 1, true)
  713. ----
  714. generate_subscripts
  715. 1
  716. subtest srf_errors
  717. query error generator functions are not allowed in ORDER BY
  718. SELECT * FROM t ORDER BY generate_series(1, 3)
  719. query error generator functions are not allowed in WHERE
  720. SELECT * FROM t WHERE generate_series(1, 3) < 3
  721. query error generator functions are not allowed in HAVING
  722. SELECT * FROM t HAVING generate_series(1, 3) < 3
  723. query error generator functions are not allowed in LIMIT
  724. SELECT * FROM t LIMIT generate_series(1, 3)
  725. query error generator functions are not allowed in OFFSET
  726. SELECT * FROM t OFFSET generate_series(1, 3)
  727. query error generator functions are not allowed in VALUES
  728. VALUES (generate_series(1,3))
  729. statement error generator functions are not allowed in DEFAULT
  730. CREATE TABLE uu (x INT DEFAULT generate_series(1, 3))
  731. statement error generator functions are not allowed in CHECK
  732. CREATE TABLE uu (x INT CHECK (generate_series(1, 3) < 3))
  733. statement error generator functions are not allowed in computed column
  734. CREATE TABLE uu (x INT AS (generate_series(1, 3)) STORED)
  735. subtest correlated_srf
  736. statement ok
  737. CREATE TABLE vals (x INT, y INT, INDEX woo (x, y));
  738. INSERT INTO vals VALUES (3, 4), (NULL, NULL), (5, 6);
  739. query III colnames
  740. SELECT x, generate_series(1,x), generate_series(1,2) FROM vals ORDER BY 1,2,3
  741. ----
  742. x generate_series generate_series
  743. NULL NULL 1
  744. NULL NULL 2
  745. 3 1 1
  746. 3 2 2
  747. 3 3 NULL
  748. 5 1 1
  749. 5 2 2
  750. 5 3 NULL
  751. 5 4 NULL
  752. 5 5 NULL
  753. # Check that the expression is still valid if the dependent name
  754. # is not otherwise rendered (needed column elision).
  755. query I colnames,rowsort
  756. SELECT generate_series(1,x) FROM vals
  757. ----
  758. generate_series
  759. 1
  760. 2
  761. 3
  762. 1
  763. 2
  764. 3
  765. 4
  766. 5
  767. # Check that the number of rows is still correct
  768. # even if the SRF is not needed.
  769. query I
  770. SELECT count(*) FROM (SELECT generate_series(1,x) FROM vals)
  771. ----
  772. 8
  773. query TI colnames
  774. SELECT relname, unnest(indkey) FROM pg_class, pg_index WHERE pg_class.oid = pg_index.indrelid ORDER BY relname, unnest
  775. ----
  776. relname unnest
  777. ordered_t 1
  778. t 2
  779. u 2
  780. vals 1
  781. vals 2
  782. vals 3
  783. query TT colnames
  784. SELECT relname, information_schema._pg_expandarray(indkey) FROM pg_class, pg_index WHERE pg_class.oid = pg_index.indrelid ORDER BY relname, x, n
  785. ----
  786. relname information_schema._pg_expandarray
  787. ordered_t (1,1)
  788. t (2,1)
  789. u (2,1)
  790. vals (1,1)
  791. vals (2,2)
  792. vals (3,1)
  793. # The following query needs indclass to become an oidvector.
  794. # See bug materialize#26504.
  795. # query III
  796. # SELECT
  797. # indexrelid,
  798. # (information_schema._pg_expandarray(indclass)).x AS operator_argument_type_oid,
  799. # (information_schema._pg_expandarray(indclass)).n AS operator_argument_position
  800. # FROM
  801. # pg_index
  802. # ----
  803. subtest correlated_json_object_keys
  804. statement ok
  805. CREATE TABLE j(x INT PRIMARY KEY, y JSON);
  806. INSERT INTO j VALUES
  807. (1, '{"a":123,"b":456}'),
  808. (2, '{"c":111,"d":222}')
  809. query IT rowsort
  810. SELECT x, y->>json_object_keys(y) FROM j
  811. ----
  812. 1 123
  813. 1 456
  814. 2 111
  815. 2 222
  816. subtest correlated_multi_column
  817. query TTI colnames
  818. SELECT tbl, idx, (i.keys).n
  819. FROM (SELECT ct.relname AS tbl, ct2.relname AS idx, information_schema._pg_expandarray(indkey) AS keys
  820. FROM pg_index ix
  821. JOIN pg_class ct ON ix.indrelid = ct.oid AND ct.relname = 'vals'
  822. JOIN pg_class ct2 ON ix.indexrelid = ct2.oid) AS i
  823. ORDER BY 1,2,3
  824. ----
  825. tbl idx n
  826. vals primary 1
  827. vals woo 1
  828. vals woo 2
  829. subtest dbviz_example_query
  830. # DbVisualizer query from materialize#24649 listed in materialize#16971.
  831. query TTI
  832. SELECT a.attname, a.atttypid, atttypmod
  833. FROM pg_catalog.pg_class ct
  834. JOIN pg_catalog.pg_attribute a ON (ct.oid = a.attrelid)
  835. JOIN pg_catalog.pg_namespace n ON (ct.relnamespace = n.oid)
  836. JOIN (
  837. SELECT i.indexrelid, i.indrelid, i.indisprimary,
  838. information_schema._pg_expandarray(i.indkey) AS keys
  839. FROM pg_catalog.pg_index i
  840. ) i ON (a.attnum = (i.keys).x AND a.attrelid = i.indrelid)
  841. WHERE true
  842. AND n.nspname = 'public'
  843. AND ct.relname = 'j'
  844. AND i.indisprimary
  845. ORDER BY a.attnum
  846. ----
  847. x 20 -1
  848. subtest metabase_confluent_example_query
  849. # Test from metabase listed on materialize#16971.
  850. # Also Kafka Confluent sink query from materialize#25854.
  851. query TTTTIT
  852. SELECT NULL AS TABLE_CAT,
  853. n.nspname AS TABLE_SCHEM,
  854. ct.relname AS TABLE_NAME,
  855. a.attname AS COLUMN_NAME,
  856. (i.keys).n AS KEY_SEQ,
  857. ci.relname AS PK_NAME
  858. FROM pg_catalog.pg_class ct
  859. JOIN pg_catalog.pg_attribute a ON (ct.oid = a.attrelid)
  860. JOIN pg_catalog.pg_namespace n ON (ct.relnamespace = n.oid)
  861. JOIN (SELECT i.indexrelid,
  862. i.indrelid,
  863. i.indisprimary,
  864. information_schema._pg_expandarray(i.indkey) AS keys
  865. FROM pg_catalog.pg_index i) i ON (a.attnum = (i.keys).x AND a.attrelid = i.indrelid)
  866. JOIN pg_catalog.pg_class ci ON (ci.oid = i.indexrelid)
  867. WHERE true AND ct.relname = 'j' AND i.indisprimary
  868. ORDER BY table_name, pk_name, key_seq
  869. ----
  870. NULL public j x 1 primary
  871. subtest liquibase_example_query
  872. # # Test from materialize#24713 (Liquibase) listed on materialize#16971.
  873. # # TODO(knz) Needs support for pg_get_indexdef with 3 arguments,
  874. # # see database-issues#7870.
  875. # query TTTBTTIITTTTT
  876. # SELECT NULL AS table_cat,
  877. # n.nspname AS table_schem,
  878. # ct.relname AS TABLE_NAME,
  879. # NOT i.indisunique AS non_unique,
  880. # NULL AS index_qualifier,
  881. # ci.relname AS index_name,
  882. # CASE i.indisclustered
  883. # WHEN TRUE THEN 1
  884. # ELSE CASE am.amname
  885. # WHEN 'hash' THEN 2
  886. # ELSE 3
  887. # END
  888. # END AS TYPE,
  889. # (i.KEYS).n AS ordinal_position,
  890. # trim(BOTH '"' FROM pg_catalog.pg_get_indexdef(ci.oid, (i.KEYS).n, FALSE)) AS COLUMN_NAME,
  891. # CASE am.amcanorder
  892. # WHEN TRUE THEN CASE i.indoption[(i.keys).n - 1] & 1
  893. # WHEN 1 THEN 'D'
  894. # ELSE 'A'
  895. # END
  896. # ELSE NULL
  897. # END AS asc_or_desc,
  898. # ci.reltuples AS CARDINALITY,
  899. # ci.relpages AS pages,
  900. # pg_catalog.pg_get_expr(i.indpred, i.indrelid) AS filter_condition
  901. # FROM pg_catalog.pg_class ct
  902. # JOIN pg_catalog.pg_namespace n ON (ct.relnamespace = n.oid)
  903. # JOIN (
  904. # SELECT i.indexrelid,
  905. # i.indrelid,
  906. # i.indoption,
  907. # i.indisunique,
  908. # i.indisclustered,
  909. # i.indpred,
  910. # i.indexprs,
  911. # information_schema._pg_expandarray(i.indkey) AS KEYS
  912. # FROM pg_catalog.pg_index i
  913. # ) i
  914. # ON (ct.oid = i.indrelid)
  915. # JOIN pg_catalog.pg_class ci ON (ci.oid = i.indexrelid)
  916. # JOIN pg_catalog.pg_am am ON (ci.relam = am.oid)
  917. # WHERE TRUE
  918. # AND n.nspname = 'public'
  919. # AND ct.relname = 'j'
  920. # ORDER BY non_unique,
  921. # TYPE,
  922. # index_name,
  923. # ordinal_position
  924. # ----
  925. subtest unnest_with_tuple_types
  926. query T colnames
  927. SELECT unnest(ARRAY[(1,2),(3,4)])
  928. ----
  929. unnest
  930. (1,2)
  931. (3,4)
  932. query error pq: type tuple{int, int} is not composite
  933. SELECT (unnest(ARRAY[(1,2),(3,4)])).*
  934. query T colnames
  935. SELECT * FROM unnest(ARRAY[(1,2),(3,4)])
  936. ----
  937. unnest
  938. (1,2)
  939. (3,4)
  940. query T colnames
  941. SELECT t.* FROM unnest(ARRAY[(1,2),(3,4)]) AS t
  942. ----
  943. t
  944. (1,2)
  945. (3,4)