1 CREATE OR REPLACE FUNCTION plperl_sum_array(INTEGER[]) RETURNS text AS $$
6 push @arrays, @$array_arg;
9 my $el = shift @arrays;
10 if (is_array_ref($el)) {
16 return $result.' '.$array_arg;
18 select plperl_sum_array('{1,2,NULL}');
24 select plperl_sum_array('{}');
30 select plperl_sum_array('{{1,2,3}, {4,5,6}}');
32 ----------------------
36 select plperl_sum_array('{{{1,2,3}, {4,5,6}}, {{7,8,9}, {10,11,12}}}');
38 ---------------------------------------------
39 78 {{{1,2,3},{4,5,6}},{{7,8,9},{10,11,12}}}
42 -- check whether we can handle arrays of maximum dimension (6)
43 select plperl_sum_array(ARRAY[[[[[[1,2],[3,4]],[[5,6],[7,8]]],[[[9,10],[11,12]],
45 [[[[17,18],[19,20]],[[21,22],[23,24]]],[[[25,26],[27,28]],[[29,30],[31,32]]]]],
46 [[[[[1,2],[3,4]],[[5,6],[7,8]]],[[[9,10],[11,12]],[[13,14],[15,16]]]],
47 [[[[17,18],[19,20]],[[21,22],[23,24]]],[[[25,26],[27,28]],[[29,30],[31,32]]]]]]);
49 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
50 1056 {{{{{{1,2},{3,4}},{{5,6},{7,8}}},{{{9,10},{11,12}},{{13,14},{15,16}}}},{{{{17,18},{19,20}},{{21,22},{23,24}}},{{{25,26},{27,28}},{{29,30},{31,32}}}}},{{{{{1,2},{3,4}},{{5,6},{7,8}}},{{{9,10},{11,12}},{{13,14},{15,16}}}},{{{{17,18},{19,20}},{{21,22},{23,24}}},{{{25,26},{27,28}},{{29,30},{31,32}}}}}}
53 -- what would we do with the arrays exceeding maximum dimension (7)
54 select plperl_sum_array('{{{{{{{1,2},{3,4}},{{5,6},{7,8}}},{{{9,10},{11,12}},
56 {{{{17,18},{19,20}},{{21,22},{23,24}}},{{{25,26},{27,28}},{{29,30},{31,32}}}}},
57 {{{{{1,2},{3,4}},{{5,6},{7,8}}},{{{9,10},{11,12}},{{13,14},{15,16}}}},
58 {{{{17,18},{19,20}},{{21,22},{23,24}}},{{{25,26},{27,28}},{{29,30},{31,32}}}}}},
59 {{{{{{1,2},{3,4}},{{5,6},{7,8}}},{{{9,10},{11,12}},{{13,14},{15,16}}}},
60 {{{{17,18},{19,20}},{{21,22},{23,24}}},{{{25,26},{27,28}},{{29,30},{31,32}}}}},
61 {{{{{1,2},{3,4}},{{5,6},{7,8}}},{{{9,10},{11,12}},{{13,14},{15,16}}}},
62 {{{{17,18},{19,20}},{{21,22},{23,24}}},{{{25,26},{27,28}},{{29,30},{31,32}}}}}}}'
64 ERROR: number of array dimensions (7) exceeds the maximum allowed (6)
65 LINE 1: select plperl_sum_array('{{{{{{{1,2},{3,4}},{{5,6},{7,8}}},{...
67 select plperl_sum_array('{{{1,2,3}, {4,5,6,7}}, {{7,8,9}, {10, 11, 12}}}');
68 ERROR: malformed array literal: "{{{1,2,3}, {4,5,6,7}}, {{7,8,9}, {10, 11, 12}}}"
69 LINE 1: select plperl_sum_array('{{{1,2,3}, {4,5,6,7}}, {{7,8,9}, {1...
71 DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
72 CREATE OR REPLACE FUNCTION plperl_concat(TEXT[]) RETURNS TEXT AS $$
73 my $array_arg = shift;
77 push @arrays, @$array_arg;
79 my $el = shift @arrays;
80 if (is_array_ref($el)) {
86 return $result.' '.$array_arg;
88 select plperl_concat('{"NULL","NULL","NULL''"}');
90 -------------------------------------
91 NULLNULLNULL' {"NULL","NULL",NULL'}
94 select plperl_concat('{{NULL,NULL,NULL}}');
100 select plperl_concat('{"hello"," ","world!"}');
102 ---------------------------------
103 hello world! {hello," ",world!}
107 CREATE TYPE foo AS (bar INTEGER, baz TEXT);
108 CREATE OR REPLACE FUNCTION plperl_array_of_rows(foo[]) RETURNS TEXT AS $$
109 my $array_arg = shift;
112 for my $row_ref (@$array_arg) {
113 die "not a hash reference" unless (ref $row_ref eq "HASH");
114 $result .= $row_ref->{bar}." items of ".$row_ref->{baz}.";";
116 return $result .' '. $array_arg;
118 select plperl_array_of_rows(ARRAY[ ROW(2, 'coffee'), ROW(0, 'sugar')]::foo[]);
120 ----------------------------------------------------------------
121 2 items of coffee;0 items of sugar; {"(2,coffee)","(0,sugar)"}
124 -- composite type containing arrays
125 CREATE TYPE rowfoo AS (bar INTEGER, baz INTEGER[]);
126 CREATE OR REPLACE FUNCTION plperl_sum_row_elements(rowfoo) RETURNS TEXT AS $$
130 if (ref $row_ref ne 'HASH') {
134 $result = $row_ref->{bar};
135 die "not an array reference".ref ($row_ref->{baz})
136 unless (is_array_ref($row_ref->{baz}));
137 # process a single-dimensional array
138 foreach my $elem (@{$row_ref->{baz}}) {
139 $result += $elem unless ref $elem;
144 select plperl_sum_row_elements(ROW(1, ARRAY[2,3,4,5,6,7,8,9,10])::rowfoo);
145 plperl_sum_row_elements
146 -------------------------
150 -- composite type containing array of another composite type, which, in order,
151 -- contains an array of integers.
152 CREATE TYPE rowbar AS (foo rowfoo[]);
153 CREATE OR REPLACE FUNCTION plperl_sum_array_of_rows(rowbar) RETURNS TEXT AS $$
154 my $rowfoo_ref = shift;
157 if (ref $rowfoo_ref eq 'HASH') {
158 my $row_array_ref = $rowfoo_ref->{foo};
159 if (is_array_ref($row_array_ref)) {
160 foreach my $row_ref (@{$row_array_ref}) {
161 if (ref $row_ref eq 'HASH') {
162 $result += $row_ref->{bar};
163 die "not an array reference".ref ($row_ref->{baz})
164 unless (is_array_ref($row_ref->{baz}));
165 foreach my $elem (@{$row_ref->{baz}}) {
166 $result += $elem unless ref $elem;
170 die "element baz is not a reference to a rowfoo";
174 die "not a reference to an array of rowfoo elements"
177 die "not a reference to type rowbar";
181 select plperl_sum_array_of_rows(ROW(ARRAY[ROW(1, ARRAY[2,3,4,5,6,7,8,9,10])::rowfoo,
182 ROW(11, ARRAY[12,13,14,15,16,17,18,19,20])::rowfoo])::rowbar);
183 plperl_sum_array_of_rows
184 --------------------------
188 -- check arrays as out parameters
189 CREATE OR REPLACE FUNCTION plperl_arrays_out(OUT INTEGER[]) AS $$
190 return [[1,2,3],[4,5,6]];
192 select plperl_arrays_out();
198 -- check that we can return the array we passed in
199 CREATE OR REPLACE FUNCTION plperl_arrays_inout(INTEGER[]) returns INTEGER[] AS $$
202 select plperl_arrays_inout('{{1}, {2}, {3}}');
204 ---------------------
208 -- check that we can return an array literal
209 CREATE OR REPLACE FUNCTION plperl_arrays_inout_l(INTEGER[]) returns INTEGER[] AS $$
210 return shift.''; # stringify it
212 select plperl_arrays_inout_l('{{1}, {2}, {3}}');
213 plperl_arrays_inout_l
214 -----------------------
218 -- make sure setof works
219 create or replace function perl_setof_array(integer[]) returns setof integer[] language plperl as $$
226 select perl_setof_array('{{1}, {2}, {3}}');