arrays.sql 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. select '[0:2][0:2]={{1,2,3},{4,5,6},{7,8,9}}'::int[];
  16. -- test slices with empty lower and/or upper index
  17. CREATE TEMP TABLE arrtest_s (
  18. a int2[],
  19. b int2[][]
  20. );
  21. INSERT INTO arrtest_s VALUES(NULL, NULL);
  22. --
  23. -- test array extension
  24. --
  25. CREATE TEMP TABLE arrtest1 (i int[], t text[]);
  26. --
  27. -- array expressions and operators
  28. --
  29. -- table creation and INSERTs
  30. CREATE TEMP TABLE arrtest2 (i integer ARRAY[4], f float8[], n numeric[], t text[], d timestamp[]);
  31. -- some more test data
  32. CREATE TEMP TABLE arrtest_f (f0 int, f1 text, f2 float8);
  33. insert into arrtest_f values(1,'cat1',1.21);
  34. insert into arrtest_f values(2,'cat1',1.24);
  35. insert into arrtest_f values(3,'cat1',1.18);
  36. insert into arrtest_f values(4,'cat1',1.26);
  37. insert into arrtest_f values(5,'cat1',1.15);
  38. insert into arrtest_f values(6,'cat2',1.15);
  39. insert into arrtest_f values(7,'cat2',1.26);
  40. insert into arrtest_f values(8,'cat2',1.32);
  41. insert into arrtest_f values(9,'cat2',1.30);
  42. CREATE TEMP TABLE arrtest_i (f0 int, f1 text, f2 int);
  43. insert into arrtest_i values(1,'cat1',21);
  44. insert into arrtest_i values(2,'cat1',24);
  45. insert into arrtest_i values(3,'cat1',18);
  46. insert into arrtest_i values(4,'cat1',26);
  47. insert into arrtest_i values(5,'cat1',15);
  48. insert into arrtest_i values(6,'cat2',15);
  49. insert into arrtest_i values(7,'cat2',26);
  50. insert into arrtest_i values(8,'cat2',32);
  51. insert into arrtest_i values(9,'cat2',30);
  52. SELECT ARRAY[[[[[['hello'],['world']]]]]];
  53. SELECT ARRAY[ARRAY['hello'],ARRAY['world']];
  54. -- with nulls
  55. SELECT '{1,null,3}'::int[];
  56. SELECT ARRAY[1,NULL,3];
  57. SELECT NOT ARRAY[1.1,1.2,1.3] = ARRAY[1.1,1.2,1.3] AS "FALSE";
  58. -- array casts
  59. SELECT ARRAY[1,2,3]::text[]::int[]::float8[] AS "{1,2,3}";
  60. SELECT pg_typeof(ARRAY[1,2,3]::text[]::int[]::float8[]) AS "double precision[]";
  61. SELECT ARRAY[['a','bc'],['def','hijk']]::text[]::varchar[] AS "{{a,bc},{def,hijk}}";
  62. SELECT pg_typeof(ARRAY[['a','bc'],['def','hijk']]::text[]::varchar[]) AS "character varying[]";
  63. SELECT CAST(ARRAY[[[[[['a','bb','ccc']]]]]] as text[]) as "{{{{{{a,bb,ccc}}}}}}";
  64. SELECT NULL::text[]::int[] AS "NULL";
  65. -- scalar op any/all (array)
  66. select 33 = any ('{1,2,3}');
  67. select 33 = any ('{1,2,33}');
  68. select 33 = all ('{1,2,33}');
  69. select 33 >= all ('{1,2,33}');
  70. -- boundary cases
  71. select null::int >= all ('{1,2,33}');
  72. select null::int >= all ('{}');
  73. select null::int >= any ('{}');
  74. -- cross-datatype
  75. select 33.4 = any (array[1,2,3]);
  76. select 33.4 > all (array[1,2,3]);
  77. -- nulls
  78. select 33 = any (null::int[]);
  79. select null::int = any ('{1,2,3}');
  80. select 33 = any ('{1,null,3}');
  81. select 33 = any ('{1,null,33}');
  82. select 33 = all (null::int[]);
  83. select null::int = all ('{1,2,3}');
  84. select 33 = all ('{1,null,3}');
  85. select 33 = all ('{33,null,33}');
  86. -- nulls later in the bitmap
  87. SELECT -1 != ALL(ARRAY(SELECT NULLIF(g.i, 900) FROM generate_series(1,1000) g(i)));
  88. -- test indexes on arrays
  89. create temp table arr_tbl (f1 int[] unique);
  90. -- test ON CONFLICT DO UPDATE with arrays
  91. create temp table arr_pk_tbl (pk int4 primary key, f1 int[]);
  92. -- test [not] (like|ilike) (any|all) (...)
  93. select 'foo' like any (array['%a', '%o']); -- t
  94. select 'foo' like any (array['%a', '%b']); -- f
  95. select 'foo' like all (array['f%', '%o']); -- t
  96. select 'foo' like all (array['f%', '%b']); -- f
  97. select 'foo' not like any (array['%a', '%b']); -- t
  98. select 'foo' not like all (array['%a', '%o']); -- f
  99. select 'foo' ilike any (array['%A', '%O']); -- t
  100. select 'foo' ilike all (array['F%', '%O']); -- t
  101. --
  102. -- General array parser tests
  103. --
  104. -- none of the following should be accepted
  105. select '{{1,{2}},{2,3}}'::text[];
  106. select '{{},{}}'::text[];
  107. select E'{{1,2},\\{2,3}}'::text[];
  108. select '{{"1 2" x},{3}}'::text[];
  109. select '{}}'::text[];
  110. select '{ }}'::text[];
  111. -- none of the above should be accepted
  112. -- all of the following should be accepted
  113. select '{}'::text[];
  114. select '{{{1,2,3,4},{2,3,4,5}},{{3,4,5,6},{4,5,6,7}}}'::text[];
  115. select '{ { "," } , { 3 } }'::text[];
  116. select ' { { " 0 second " , 0 second } }'::text[];
  117. select '[0:1]={1.1,2.2}'::float8[];
  118. -- all of the above should be accepted
  119. -- tests for array aggregates
  120. CREATE TEMP TABLE arraggtest ( f1 INT[], f2 TEXT[][], f3 FLOAT[]);
  121. create table comptable (c1 comptype, c2 comptype[]);
  122. drop table comptable;
  123. select string_to_array('1|2|3', '|');
  124. select string_to_array('1|2|3|', '|');
  125. select string_to_array('1||2|3||', '||');
  126. select string_to_array('1|2|3', '');
  127. select string_to_array('', '|');
  128. select string_to_array('1|2|3', NULL);
  129. select string_to_array(NULL, '|') IS NULL;
  130. select string_to_array('abc', '');
  131. select string_to_array('abc', '', 'abc');
  132. select string_to_array('abc', ',');
  133. select string_to_array('abc', ',', 'abc');
  134. select string_to_array('1,2,3,4,,6', ',');
  135. select string_to_array('1,2,3,4,,6', ',', '');
  136. select string_to_array('1,2,3,4,*,6', ',', '*');
  137. select v, v is null as "is null" from string_to_table('1|2|3', '|') g(v);
  138. select v, v is null as "is null" from string_to_table('1|2|3|', '|') g(v);
  139. select v, v is null as "is null" from string_to_table('1||2|3||', '||') g(v);
  140. select v, v is null as "is null" from string_to_table('1|2|3', '') g(v);
  141. select v, v is null as "is null" from string_to_table('', '|') g(v);
  142. select v, v is null as "is null" from string_to_table('1|2|3', NULL) g(v);
  143. select v, v is null as "is null" from string_to_table(NULL, '|') g(v);
  144. select v, v is null as "is null" from string_to_table('abc', '') g(v);
  145. select v, v is null as "is null" from string_to_table('abc', '', 'abc') g(v);
  146. select v, v is null as "is null" from string_to_table('abc', ',') g(v);
  147. select v, v is null as "is null" from string_to_table('abc', ',', 'abc') g(v);
  148. select v, v is null as "is null" from string_to_table('1,2,3,4,,6', ',') g(v);
  149. select v, v is null as "is null" from string_to_table('1,2,3,4,,6', ',', '') g(v);
  150. select v, v is null as "is null" from string_to_table('1,2,3,4,*,6', ',', '*') g(v);
  151. select array_to_string(NULL::int4[], ',') IS NULL;
  152. select array_to_string('{}'::int4[], ',');
  153. select array_to_string(array[1,2,3,4,NULL,6], ',');
  154. select array_to_string(array[1,2,3,4,NULL,6], ',', '*');
  155. select array_to_string(array[1,2,3,4,NULL,6], NULL);
  156. select array_to_string(array[1,2,3,4,NULL,6], ',', NULL);
  157. select array_to_string(string_to_array('1|2|3', '|'), '|');
  158. select array_length(array[1,2,3], 1);
  159. select array_length(array[[1,2,3], [4,5,6]], 0);
  160. select array_length(array[[1,2,3], [4,5,6]], 1);
  161. select array_length(array[[1,2,3], [4,5,6]], 2);
  162. select array_length(array[[1,2,3], [4,5,6]], 3);
  163. select cardinality(NULL::int[]);
  164. select cardinality('{}'::int[]);
  165. select cardinality(array[1,2,3]);
  166. select cardinality('[2:4]={5,6,7}'::int[]);
  167. select cardinality('{{1,2}}'::int[]);
  168. select cardinality('{{1,2},{3,4},{5,6}}'::int[]);
  169. select cardinality('{{{1,9},{5,6}},{{2,3},{3,4}}}'::int[]);
  170. select array_agg(unique1) from tenk1 where unique1 < -15;
  171. -- array_agg(anyarray)
  172. select array_agg(ar)
  173. from (values ('{1,2}'::int[]), ('{3,4}'::int[])) v(ar);
  174. select array_agg(ar)
  175. from (select array_agg(array[i, i+1, i-1])
  176. from generate_series(1,2) a(i)) b(ar);
  177. select array_agg(array[i+1.2, i+1.3, i+1.4]) from generate_series(1,3) g(i);
  178. select array_agg(array['Hello', i::text]) from generate_series(9,11) g(i);
  179. select array_agg(array[i, nullif(i, 3), i+1]) from generate_series(1,4) g(i);
  180. -- errors
  181. select array_agg('{}'::int[]) from generate_series(1,2);
  182. select array_agg(null::int[]) from generate_series(1,2);
  183. select array_agg(ar)
  184. from (values ('{1,2}'::int[]), ('{3}'::int[])) v(ar);
  185. select * from unnest(array[1,2,3]);
  186. -- array(select array-value ...)
  187. select array(select array[i,i/2] from generate_series(1,5) i);
  188. select array(select array['Hello', i::text] from generate_series(9,11) i);
  189. -- Insert/update on a column that is array of composite
  190. create temp table t1 (f1 int8_tbl[]);
  191. -- Check that arrays of composites are safely detoasted when needed
  192. create temp table src (f1 text);
  193. insert into src
  194. select string_agg(random()::text,'') from generate_series(1,10000);
  195. create temp table dest (f1 textandtext[]);
  196. drop table src;
  197. drop table dest;
  198. -- trim_array
  199. SELECT arr, trim_array(arr, 2)
  200. FROM
  201. (VALUES ('{1,2,3,4,5,6}'::bigint[]),
  202. ('{1,2}'),
  203. ('[10:16]={1,2,3,4,5,6,7}'),
  204. ('[-15:-10]={1,2,3,4,5,6}'),
  205. ('{{1,10},{2,20},{3,30},{4,40}}')) v(arr);
  206. SELECT trim_array(ARRAY[1, 2, 3], -1); -- fail
  207. SELECT trim_array(ARRAY[1, 2, 3], 10); -- fail