arrays.out 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. --
  2. -- ARRAYS
  3. --
  4. CREATE TABLE arrtest (
  5. a int2[],
  6. b int4[][][],
  7. c name[],
  8. d text[][],
  9. e float8[],
  10. f char(5)[],
  11. g varchar(5)[]
  12. );
  13. -- test mixed slice/scalar subscripting
  14. select '{{1,2,3},{4,5,6},{7,8,9}}'::int[];
  15. int4
  16. ---------------------------
  17. {{1,2,3},{4,5,6},{7,8,9}}
  18. (1 row)
  19. select '[0:2][0:2]={{1,2,3},{4,5,6},{7,8,9}}'::int[];
  20. int4
  21. --------------------------------------
  22. [0:2][0:2]={{1,2,3},{4,5,6},{7,8,9}}
  23. (1 row)
  24. -- test slices with empty lower and/or upper index
  25. CREATE TEMP TABLE arrtest_s (
  26. a int2[],
  27. b int2[][]
  28. );
  29. INSERT INTO arrtest_s VALUES(NULL, NULL);
  30. --
  31. -- test array extension
  32. --
  33. CREATE TEMP TABLE arrtest1 (i int[], t text[]);
  34. --
  35. -- array expressions and operators
  36. --
  37. -- table creation and INSERTs
  38. CREATE TEMP TABLE arrtest2 (i integer ARRAY[4], f float8[], n numeric[], t text[], d timestamp[]);
  39. -- some more test data
  40. CREATE TEMP TABLE arrtest_f (f0 int, f1 text, f2 float8);
  41. insert into arrtest_f values(1,'cat1',1.21);
  42. insert into arrtest_f values(2,'cat1',1.24);
  43. insert into arrtest_f values(3,'cat1',1.18);
  44. insert into arrtest_f values(4,'cat1',1.26);
  45. insert into arrtest_f values(5,'cat1',1.15);
  46. insert into arrtest_f values(6,'cat2',1.15);
  47. insert into arrtest_f values(7,'cat2',1.26);
  48. insert into arrtest_f values(8,'cat2',1.32);
  49. insert into arrtest_f values(9,'cat2',1.30);
  50. CREATE TEMP TABLE arrtest_i (f0 int, f1 text, f2 int);
  51. insert into arrtest_i values(1,'cat1',21);
  52. insert into arrtest_i values(2,'cat1',24);
  53. insert into arrtest_i values(3,'cat1',18);
  54. insert into arrtest_i values(4,'cat1',26);
  55. insert into arrtest_i values(5,'cat1',15);
  56. insert into arrtest_i values(6,'cat2',15);
  57. insert into arrtest_i values(7,'cat2',26);
  58. insert into arrtest_i values(8,'cat2',32);
  59. insert into arrtest_i values(9,'cat2',30);
  60. SELECT ARRAY[[[[[['hello'],['world']]]]]];
  61. array
  62. ---------------------------
  63. {{{{{{hello},{world}}}}}}
  64. (1 row)
  65. SELECT ARRAY[ARRAY['hello'],ARRAY['world']];
  66. array
  67. -------------------
  68. {{hello},{world}}
  69. (1 row)
  70. -- with nulls
  71. SELECT '{1,null,3}'::int[];
  72. int4
  73. ------------
  74. {1,NULL,3}
  75. (1 row)
  76. SELECT ARRAY[1,NULL,3];
  77. array
  78. ------------
  79. {1,NULL,3}
  80. (1 row)
  81. SELECT NOT ARRAY[1.1,1.2,1.3] = ARRAY[1.1,1.2,1.3] AS "FALSE";
  82. FALSE
  83. -------
  84. f
  85. (1 row)
  86. -- array casts
  87. SELECT ARRAY[1,2,3]::text[]::int[]::float8[] AS "{1,2,3}";
  88. {1,2,3}
  89. ---------
  90. {1,2,3}
  91. (1 row)
  92. SELECT pg_typeof(ARRAY[1,2,3]::text[]::int[]::float8[]) AS "double precision[]";
  93. double precision[]
  94. --------------------
  95. double precision[]
  96. (1 row)
  97. SELECT ARRAY[['a','bc'],['def','hijk']]::text[]::varchar[] AS "{{a,bc},{def,hijk}}";
  98. {{a,bc},{def,hijk}}
  99. ---------------------
  100. {{a,bc},{def,hijk}}
  101. (1 row)
  102. SELECT pg_typeof(ARRAY[['a','bc'],['def','hijk']]::text[]::varchar[]) AS "character varying[]";
  103. character varying[]
  104. ---------------------
  105. character varying[]
  106. (1 row)
  107. SELECT CAST(ARRAY[[[[[['a','bb','ccc']]]]]] as text[]) as "{{{{{{a,bb,ccc}}}}}}";
  108. {{{{{{a,bb,ccc}}}}}}
  109. ----------------------
  110. {{{{{{a,bb,ccc}}}}}}
  111. (1 row)
  112. SELECT NULL::text[]::int[] AS "NULL";
  113. NULL
  114. ------
  115. (1 row)
  116. -- scalar op any/all (array)
  117. select 33 = any ('{1,2,3}');
  118. ?column?
  119. ----------
  120. f
  121. (1 row)
  122. select 33 = any ('{1,2,33}');
  123. ?column?
  124. ----------
  125. t
  126. (1 row)
  127. select 33 = all ('{1,2,33}');
  128. ?column?
  129. ----------
  130. f
  131. (1 row)
  132. select 33 >= all ('{1,2,33}');
  133. ?column?
  134. ----------
  135. t
  136. (1 row)
  137. -- boundary cases
  138. select null::int >= all ('{1,2,33}');
  139. ?column?
  140. ----------
  141. (1 row)
  142. select null::int >= all ('{}');
  143. ?column?
  144. ----------
  145. t
  146. (1 row)
  147. select null::int >= any ('{}');
  148. ?column?
  149. ----------
  150. f
  151. (1 row)
  152. -- cross-datatype
  153. select 33.4 = any (array[1,2,3]);
  154. ?column?
  155. ----------
  156. f
  157. (1 row)
  158. select 33.4 > all (array[1,2,3]);
  159. ?column?
  160. ----------
  161. t
  162. (1 row)
  163. -- nulls
  164. select 33 = any (null::int[]);
  165. ?column?
  166. ----------
  167. (1 row)
  168. select null::int = any ('{1,2,3}');
  169. ?column?
  170. ----------
  171. (1 row)
  172. select 33 = any ('{1,null,3}');
  173. ?column?
  174. ----------
  175. (1 row)
  176. select 33 = any ('{1,null,33}');
  177. ?column?
  178. ----------
  179. t
  180. (1 row)
  181. select 33 = all (null::int[]);
  182. ?column?
  183. ----------
  184. (1 row)
  185. select null::int = all ('{1,2,3}');
  186. ?column?
  187. ----------
  188. (1 row)
  189. select 33 = all ('{1,null,3}');
  190. ?column?
  191. ----------
  192. f
  193. (1 row)
  194. select 33 = all ('{33,null,33}');
  195. ?column?
  196. ----------
  197. (1 row)
  198. -- nulls later in the bitmap
  199. SELECT -1 != ALL(ARRAY(SELECT NULLIF(g.i, 900) FROM generate_series(1,1000) g(i)));
  200. ?column?
  201. ----------
  202. (1 row)
  203. -- test indexes on arrays
  204. create temp table arr_tbl (f1 int[] unique);
  205. -- test ON CONFLICT DO UPDATE with arrays
  206. create temp table arr_pk_tbl (pk int4 primary key, f1 int[]);
  207. -- test [not] (like|ilike) (any|all) (...)
  208. select 'foo' like any (array['%a', '%o']); -- t
  209. ?column?
  210. ----------
  211. t
  212. (1 row)
  213. select 'foo' like any (array['%a', '%b']); -- f
  214. ?column?
  215. ----------
  216. f
  217. (1 row)
  218. select 'foo' like all (array['f%', '%o']); -- t
  219. ?column?
  220. ----------
  221. t
  222. (1 row)
  223. select 'foo' like all (array['f%', '%b']); -- f
  224. ?column?
  225. ----------
  226. f
  227. (1 row)
  228. select 'foo' not like any (array['%a', '%b']); -- t
  229. ?column?
  230. ----------
  231. t
  232. (1 row)
  233. select 'foo' not like all (array['%a', '%o']); -- f
  234. ?column?
  235. ----------
  236. f
  237. (1 row)
  238. select 'foo' ilike any (array['%A', '%O']); -- t
  239. ?column?
  240. ----------
  241. t
  242. (1 row)
  243. select 'foo' ilike all (array['F%', '%O']); -- t
  244. ?column?
  245. ----------
  246. t
  247. (1 row)
  248. --
  249. -- General array parser tests
  250. --
  251. -- none of the following should be accepted
  252. select '{{1,{2}},{2,3}}'::text[];
  253. ERROR: malformed array literal: "{{1,{2}},{2,3}}"
  254. LINE 1: select '{{1,{2}},{2,3}}'::text[];
  255. ^
  256. DETAIL: Unexpected "{" character.
  257. select '{{},{}}'::text[];
  258. ERROR: malformed array literal: "{{},{}}"
  259. LINE 1: select '{{},{}}'::text[];
  260. ^
  261. DETAIL: Unexpected "}" character.
  262. select E'{{1,2},\\{2,3}}'::text[];
  263. ERROR: malformed array literal: "{{1,2},\{2,3}}"
  264. LINE 1: select E'{{1,2},\\{2,3}}'::text[];
  265. ^
  266. DETAIL: Unexpected "\" character.
  267. select '{{"1 2" x},{3}}'::text[];
  268. ERROR: malformed array literal: "{{"1 2" x},{3}}"
  269. LINE 1: select '{{"1 2" x},{3}}'::text[];
  270. ^
  271. DETAIL: Unexpected array element.
  272. select '{}}'::text[];
  273. ERROR: malformed array literal: "{}}"
  274. LINE 1: select '{}}'::text[];
  275. ^
  276. DETAIL: Junk after closing right brace.
  277. select '{ }}'::text[];
  278. ERROR: malformed array literal: "{ }}"
  279. LINE 1: select '{ }}'::text[];
  280. ^
  281. DETAIL: Junk after closing right brace.
  282. -- none of the above should be accepted
  283. -- all of the following should be accepted
  284. select '{}'::text[];
  285. text
  286. ------
  287. {}
  288. (1 row)
  289. select '{{{1,2,3,4},{2,3,4,5}},{{3,4,5,6},{4,5,6,7}}}'::text[];
  290. text
  291. -----------------------------------------------
  292. {{{1,2,3,4},{2,3,4,5}},{{3,4,5,6},{4,5,6,7}}}
  293. (1 row)
  294. select '{ { "," } , { 3 } }'::text[];
  295. text
  296. -------------
  297. {{","},{3}}
  298. (1 row)
  299. select ' { { " 0 second " , 0 second } }'::text[];
  300. text
  301. -------------------------------
  302. {{" 0 second ","0 second"}}
  303. (1 row)
  304. select '[0:1]={1.1,2.2}'::float8[];
  305. float8
  306. -----------------
  307. [0:1]={1.1,2.2}
  308. (1 row)
  309. -- all of the above should be accepted
  310. -- tests for array aggregates
  311. CREATE TEMP TABLE arraggtest ( f1 INT[], f2 TEXT[][], f3 FLOAT[]);
  312. create table comptable (c1 comptype, c2 comptype[]);
  313. drop table comptable;
  314. select string_to_array('1|2|3', '|');
  315. string_to_array
  316. -----------------
  317. {1,2,3}
  318. (1 row)
  319. select string_to_array('1|2|3|', '|');
  320. string_to_array
  321. -----------------
  322. {1,2,3,""}
  323. (1 row)
  324. select string_to_array('1||2|3||', '||');
  325. string_to_array
  326. -----------------
  327. {1,2|3,""}
  328. (1 row)
  329. select string_to_array('1|2|3', '');
  330. string_to_array
  331. -----------------
  332. {1|2|3}
  333. (1 row)
  334. select string_to_array('', '|');
  335. string_to_array
  336. -----------------
  337. {}
  338. (1 row)
  339. select string_to_array('1|2|3', NULL);
  340. string_to_array
  341. -----------------
  342. {1,|,2,|,3}
  343. (1 row)
  344. select string_to_array(NULL, '|') IS NULL;
  345. ?column?
  346. ----------
  347. t
  348. (1 row)
  349. select string_to_array('abc', '');
  350. string_to_array
  351. -----------------
  352. {abc}
  353. (1 row)
  354. select string_to_array('abc', '', 'abc');
  355. string_to_array
  356. -----------------
  357. {NULL}
  358. (1 row)
  359. select string_to_array('abc', ',');
  360. string_to_array
  361. -----------------
  362. {abc}
  363. (1 row)
  364. select string_to_array('abc', ',', 'abc');
  365. string_to_array
  366. -----------------
  367. {NULL}
  368. (1 row)
  369. select string_to_array('1,2,3,4,,6', ',');
  370. string_to_array
  371. -----------------
  372. {1,2,3,4,"",6}
  373. (1 row)
  374. select string_to_array('1,2,3,4,,6', ',', '');
  375. string_to_array
  376. ------------------
  377. {1,2,3,4,NULL,6}
  378. (1 row)
  379. select string_to_array('1,2,3,4,*,6', ',', '*');
  380. string_to_array
  381. ------------------
  382. {1,2,3,4,NULL,6}
  383. (1 row)
  384. select v, v is null as "is null" from string_to_table('1|2|3', '|') g(v);
  385. v | is null
  386. ---+---------
  387. 1 | f
  388. 2 | f
  389. 3 | f
  390. (3 rows)
  391. select v, v is null as "is null" from string_to_table('1|2|3|', '|') g(v);
  392. v | is null
  393. ---+---------
  394. 1 | f
  395. 2 | f
  396. 3 | f
  397. | f
  398. (4 rows)
  399. select v, v is null as "is null" from string_to_table('1||2|3||', '||') g(v);
  400. v | is null
  401. -----+---------
  402. 1 | f
  403. 2|3 | f
  404. | f
  405. (3 rows)
  406. select v, v is null as "is null" from string_to_table('1|2|3', '') g(v);
  407. v | is null
  408. -------+---------
  409. 1|2|3 | f
  410. (1 row)
  411. select v, v is null as "is null" from string_to_table('', '|') g(v);
  412. v | is null
  413. ---+---------
  414. (0 rows)
  415. select v, v is null as "is null" from string_to_table('1|2|3', NULL) g(v);
  416. v | is null
  417. ---+---------
  418. 1 | f
  419. | | f
  420. 2 | f
  421. | | f
  422. 3 | f
  423. (5 rows)
  424. select v, v is null as "is null" from string_to_table(NULL, '|') g(v);
  425. v | is null
  426. ---+---------
  427. (0 rows)
  428. select v, v is null as "is null" from string_to_table('abc', '') g(v);
  429. v | is null
  430. -----+---------
  431. abc | f
  432. (1 row)
  433. select v, v is null as "is null" from string_to_table('abc', '', 'abc') g(v);
  434. v | is null
  435. ---+---------
  436. | t
  437. (1 row)
  438. select v, v is null as "is null" from string_to_table('abc', ',') g(v);
  439. v | is null
  440. -----+---------
  441. abc | f
  442. (1 row)
  443. select v, v is null as "is null" from string_to_table('abc', ',', 'abc') g(v);
  444. v | is null
  445. ---+---------
  446. | t
  447. (1 row)
  448. select v, v is null as "is null" from string_to_table('1,2,3,4,,6', ',') g(v);
  449. v | is null
  450. ---+---------
  451. 1 | f
  452. 2 | f
  453. 3 | f
  454. 4 | f
  455. | f
  456. 6 | f
  457. (6 rows)
  458. select v, v is null as "is null" from string_to_table('1,2,3,4,,6', ',', '') g(v);
  459. v | is null
  460. ---+---------
  461. 1 | f
  462. 2 | f
  463. 3 | f
  464. 4 | f
  465. | t
  466. 6 | f
  467. (6 rows)
  468. select v, v is null as "is null" from string_to_table('1,2,3,4,*,6', ',', '*') g(v);
  469. v | is null
  470. ---+---------
  471. 1 | f
  472. 2 | f
  473. 3 | f
  474. 4 | f
  475. | t
  476. 6 | f
  477. (6 rows)
  478. select array_to_string(NULL::int4[], ',') IS NULL;
  479. ?column?
  480. ----------
  481. t
  482. (1 row)
  483. select array_to_string('{}'::int4[], ',');
  484. array_to_string
  485. -----------------
  486. (1 row)
  487. select array_to_string(array[1,2,3,4,NULL,6], ',');
  488. array_to_string
  489. -----------------
  490. 1,2,3,4,6
  491. (1 row)
  492. select array_to_string(array[1,2,3,4,NULL,6], ',', '*');
  493. array_to_string
  494. -----------------
  495. 1,2,3,4,*,6
  496. (1 row)
  497. select array_to_string(array[1,2,3,4,NULL,6], NULL);
  498. array_to_string
  499. -----------------
  500. (1 row)
  501. select array_to_string(array[1,2,3,4,NULL,6], ',', NULL);
  502. array_to_string
  503. -----------------
  504. 1,2,3,4,6
  505. (1 row)
  506. select array_to_string(string_to_array('1|2|3', '|'), '|');
  507. array_to_string
  508. -----------------
  509. 1|2|3
  510. (1 row)
  511. select array_length(array[1,2,3], 1);
  512. array_length
  513. --------------
  514. 3
  515. (1 row)
  516. select array_length(array[[1,2,3], [4,5,6]], 0);
  517. array_length
  518. --------------
  519. (1 row)
  520. select array_length(array[[1,2,3], [4,5,6]], 1);
  521. array_length
  522. --------------
  523. 2
  524. (1 row)
  525. select array_length(array[[1,2,3], [4,5,6]], 2);
  526. array_length
  527. --------------
  528. 3
  529. (1 row)
  530. select array_length(array[[1,2,3], [4,5,6]], 3);
  531. array_length
  532. --------------
  533. (1 row)
  534. select cardinality(NULL::int[]);
  535. cardinality
  536. -------------
  537. (1 row)
  538. select cardinality('{}'::int[]);
  539. cardinality
  540. -------------
  541. 0
  542. (1 row)
  543. select cardinality(array[1,2,3]);
  544. cardinality
  545. -------------
  546. 3
  547. (1 row)
  548. select cardinality('[2:4]={5,6,7}'::int[]);
  549. cardinality
  550. -------------
  551. 3
  552. (1 row)
  553. select cardinality('{{1,2}}'::int[]);
  554. cardinality
  555. -------------
  556. 2
  557. (1 row)
  558. select cardinality('{{1,2},{3,4},{5,6}}'::int[]);
  559. cardinality
  560. -------------
  561. 6
  562. (1 row)
  563. select cardinality('{{{1,9},{5,6}},{{2,3},{3,4}}}'::int[]);
  564. cardinality
  565. -------------
  566. 8
  567. (1 row)
  568. select array_agg(unique1) from tenk1 where unique1 < -15;
  569. array_agg
  570. -----------
  571. (1 row)
  572. -- array_agg(anyarray)
  573. select array_agg(ar)
  574. from (values ('{1,2}'::int[]), ('{3,4}'::int[])) v(ar);
  575. array_agg
  576. ---------------
  577. {{1,2},{3,4}}
  578. (1 row)
  579. select array_agg(ar)
  580. from (select array_agg(array[i, i+1, i-1])
  581. from generate_series(1,2) a(i)) b(ar);
  582. array_agg
  583. ---------------------
  584. {{{1,2,0},{2,3,1}}}
  585. (1 row)
  586. select array_agg(array[i+1.2, i+1.3, i+1.4]) from generate_series(1,3) g(i);
  587. array_agg
  588. ---------------------------------------------
  589. {{2.2,2.3,2.4},{3.2,3.3,3.4},{4.2,4.3,4.4}}
  590. (1 row)
  591. select array_agg(array['Hello', i::text]) from generate_series(9,11) g(i);
  592. array_agg
  593. -----------------------------------
  594. {{Hello,9},{Hello,10},{Hello,11}}
  595. (1 row)
  596. select array_agg(array[i, nullif(i, 3), i+1]) from generate_series(1,4) g(i);
  597. array_agg
  598. --------------------------------------
  599. {{1,1,2},{2,2,3},{3,NULL,4},{4,4,5}}
  600. (1 row)
  601. -- errors
  602. select array_agg('{}'::int[]) from generate_series(1,2);
  603. ERROR: cannot accumulate empty arrays
  604. select array_agg(null::int[]) from generate_series(1,2);
  605. ERROR: cannot accumulate null arrays
  606. select array_agg(ar)
  607. from (values ('{1,2}'::int[]), ('{3}'::int[])) v(ar);
  608. ERROR: cannot accumulate arrays of different dimensionality
  609. select * from unnest(array[1,2,3]);
  610. unnest
  611. --------
  612. 1
  613. 2
  614. 3
  615. (3 rows)
  616. -- array(select array-value ...)
  617. select array(select array[i,i/2] from generate_series(1,5) i);
  618. array
  619. ---------------------------------
  620. {{1,0},{2,1},{3,1},{4,2},{5,2}}
  621. (1 row)
  622. select array(select array['Hello', i::text] from generate_series(9,11) i);
  623. array
  624. -----------------------------------
  625. {{Hello,9},{Hello,10},{Hello,11}}
  626. (1 row)
  627. -- Insert/update on a column that is array of composite
  628. create temp table t1 (f1 int8_tbl[]);
  629. -- Check that arrays of composites are safely detoasted when needed
  630. create temp table src (f1 text);
  631. insert into src
  632. select string_agg(random()::text,'') from generate_series(1,10000);
  633. create temp table dest (f1 textandtext[]);
  634. drop table src;
  635. drop table dest;
  636. -- trim_array
  637. SELECT arr, trim_array(arr, 2)
  638. FROM
  639. (VALUES ('{1,2,3,4,5,6}'::bigint[]),
  640. ('{1,2}'),
  641. ('[10:16]={1,2,3,4,5,6,7}'),
  642. ('[-15:-10]={1,2,3,4,5,6}'),
  643. ('{{1,10},{2,20},{3,30},{4,40}}')) v(arr);
  644. arr | trim_array
  645. -------------------------------+-----------------
  646. {1,2,3,4,5,6} | {1,2,3,4}
  647. {1,2} | {}
  648. [10:16]={1,2,3,4,5,6,7} | {1,2,3,4,5}
  649. [-15:-10]={1,2,3,4,5,6} | {1,2,3,4}
  650. {{1,10},{2,20},{3,30},{4,40}} | {{1,10},{2,20}}
  651. (5 rows)
  652. SELECT trim_array(ARRAY[1, 2, 3], -1); -- fail
  653. ERROR: number of elements to trim must be between 0 and 3
  654. SELECT trim_array(ARRAY[1, 2, 3], 10); -- fail
  655. ERROR: number of elements to trim must be between 0 and 3