| 1 |
CREATE OR REPLACE FUNCTION plperl_sum_array(INTEGER[]) RETURNS text AS $$ |
| 2 |
my $array_arg = shift; |
| 3 |
my $result = 0; |
| 4 |
my @arrays; |
| 5 |
|
| 6 |
push @arrays, @$array_arg; |
| 7 |
|
| 8 |
while (@arrays > 0) { |
| 9 |
my $el = shift @arrays; |
| 10 |
if (is_array_ref($el)) { |
| 11 |
push @arrays, @$el; |
| 12 |
} else { |
| 13 |
$result += $el; |
| 14 |
} |
| 15 |
} |
| 16 |
return $result.' '.$array_arg; |
| 17 |
$$ LANGUAGE plperl; |
| 18 |
select plperl_sum_array('{1,2,NULL}'); |
| 19 |
plperl_sum_array |
| 20 |
------------------ |
| 21 |
3 {1,2,NULL} |
| 22 |
(1 row) |
| 23 |
|
| 24 |
select plperl_sum_array('{}'); |
| 25 |
plperl_sum_array |
| 26 |
------------------ |
| 27 |
0 {} |
| 28 |
(1 row) |
| 29 |
|
| 30 |
select plperl_sum_array('{{1,2,3}, {4,5,6}}'); |
| 31 |
plperl_sum_array |
| 32 |
---------------------- |
| 33 |
21 {{1,2,3},{4,5,6}} |
| 34 |
(1 row) |
| 35 |
|
| 36 |
select plperl_sum_array('{{{1,2,3}, {4,5,6}}, {{7,8,9}, {10,11,12}}}'); |
| 37 |
plperl_sum_array |
| 38 |
--------------------------------------------- |
| 39 |
78 {{{1,2,3},{4,5,6}},{{7,8,9},{10,11,12}}} |
| 40 |
(1 row) |
| 41 |
|
| 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]], |
| 44 |
[[13,14],[15,16]]]], |
| 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]]]]]]); |
| 48 |
plperl_sum_array |
| 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}}}}}} |
| 51 |
(1 row) |
| 52 |
|
| 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}}, |
| 55 |
{{13,14},{15,16}}}}, |
| 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}}}}}}}' |
| 63 |
); |
| 64 |
ERROR: number of array dimensions exceeds the maximum allowed (6) |
| 65 |
LINE 1: select plperl_sum_array('{{{{{{{1,2},{3,4}},{{5,6},{7,8}}},{... |
| 66 |
^ |
| 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... |
| 70 |
^ |
| 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; |
| 74 |
my $result = ""; |
| 75 |
my @arrays; |
| 76 |
|
| 77 |
push @arrays, @$array_arg; |
| 78 |
while (@arrays > 0) { |
| 79 |
my $el = shift @arrays; |
| 80 |
if (is_array_ref($el)) { |
| 81 |
push @arrays, @$el; |
| 82 |
} else { |
| 83 |
$result .= $el; |
| 84 |
} |
| 85 |
} |
| 86 |
return $result.' '.$array_arg; |
| 87 |
$$ LANGUAGE plperl; |
| 88 |
select plperl_concat('{"NULL","NULL","NULL''"}'); |
| 89 |
plperl_concat |
| 90 |
------------------------------------- |
| 91 |
NULLNULLNULL' {"NULL","NULL",NULL'} |
| 92 |
(1 row) |
| 93 |
|
| 94 |
select plperl_concat('{{NULL,NULL,NULL}}'); |
| 95 |
plperl_concat |
| 96 |
--------------------- |
| 97 |
{{NULL,NULL,NULL}} |
| 98 |
(1 row) |
| 99 |
|
| 100 |
select plperl_concat('{"hello"," ","world!"}'); |
| 101 |
plperl_concat |
| 102 |
--------------------------------- |
| 103 |
hello world! {hello," ",world!} |
| 104 |
(1 row) |
| 105 |
|
| 106 |
-- array of rows -- |
| 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; |
| 110 |
my $result = ""; |
| 111 |
|
| 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}.";"; |
| 115 |
} |
| 116 |
return $result .' '. $array_arg; |
| 117 |
$$ LANGUAGE plperl; |
| 118 |
select plperl_array_of_rows(ARRAY[ ROW(2, 'coffee'), ROW(0, 'sugar')]::foo[]); |
| 119 |
plperl_array_of_rows |
| 120 |
---------------------------------------------------------------- |
| 121 |
2 items of coffee;0 items of sugar; {"(2,coffee)","(0,sugar)"} |
| 122 |
(1 row) |
| 123 |
|
| 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 $$ |
| 127 |
my $row_ref = shift; |
| 128 |
my $result; |
| 129 |
|
| 130 |
if (ref $row_ref ne 'HASH') { |
| 131 |
$result = 0; |
| 132 |
} |
| 133 |
else { |
| 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; |
| 140 |
} |
| 141 |
} |
| 142 |
return $result; |
| 143 |
$$ LANGUAGE plperl; |
| 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 |
------------------------- |
| 147 |
55 |
| 148 |
(1 row) |
| 149 |
|
| 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; |
| 155 |
my $result = 0; |
| 156 |
|
| 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; |
| 167 |
} |
| 168 |
} |
| 169 |
else { |
| 170 |
die "element baz is not a reference to a rowfoo"; |
| 171 |
} |
| 172 |
} |
| 173 |
} else { |
| 174 |
die "not a reference to an array of rowfoo elements" |
| 175 |
} |
| 176 |
} else { |
| 177 |
die "not a reference to type rowbar"; |
| 178 |
} |
| 179 |
return $result; |
| 180 |
$$ LANGUAGE plperl; |
| 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 |
-------------------------- |
| 185 |
210 |
| 186 |
(1 row) |
| 187 |
|
| 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]]; |
| 191 |
$$ LANGUAGE plperl; |
| 192 |
select plperl_arrays_out(); |
| 193 |
plperl_arrays_out |
| 194 |
------------------- |
| 195 |
{{1,2,3},{4,5,6}} |
| 196 |
(1 row) |
| 197 |
|
| 198 |
-- check that we can return the array we passed in |
| 199 |
CREATE OR REPLACE FUNCTION plperl_arrays_inout(INTEGER[]) returns INTEGER[] AS $$ |
| 200 |
return shift; |
| 201 |
$$ LANGUAGE plperl; |
| 202 |
select plperl_arrays_inout('{{1}, {2}, {3}}'); |
| 203 |
plperl_arrays_inout |
| 204 |
--------------------- |
| 205 |
{{1},{2},{3}} |
| 206 |
(1 row) |
| 207 |
|
| 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 |
| 211 |
$$ LANGUAGE plperl; |
| 212 |
select plperl_arrays_inout_l('{{1}, {2}, {3}}'); |
| 213 |
plperl_arrays_inout_l |
| 214 |
----------------------- |
| 215 |
{{1},{2},{3}} |
| 216 |
(1 row) |
| 217 |
|
| 218 |
-- check output of multi-dimensional arrays |
| 219 |
CREATE FUNCTION plperl_md_array_out() RETURNS text[] AS $$ |
| 220 |
return [['a'], ['b'], ['c']]; |
| 221 |
$$ LANGUAGE plperl; |
| 222 |
select plperl_md_array_out(); |
| 223 |
plperl_md_array_out |
| 224 |
--------------------- |
| 225 |
{{a},{b},{c}} |
| 226 |
(1 row) |
| 227 |
|
| 228 |
CREATE OR REPLACE FUNCTION plperl_md_array_out() RETURNS text[] AS $$ |
| 229 |
return [[], []]; |
| 230 |
$$ LANGUAGE plperl; |
| 231 |
select plperl_md_array_out(); |
| 232 |
plperl_md_array_out |
| 233 |
--------------------- |
| 234 |
{} |
| 235 |
(1 row) |
| 236 |
|
| 237 |
CREATE OR REPLACE FUNCTION plperl_md_array_out() RETURNS text[] AS $$ |
| 238 |
return [[], [1]]; |
| 239 |
$$ LANGUAGE plperl; |
| 240 |
select plperl_md_array_out(); -- fail |
| 241 |
ERROR: multidimensional arrays must have array expressions with matching dimensions |
| 242 |
CONTEXT: PL/Perl function "plperl_md_array_out" |
| 243 |
CREATE OR REPLACE FUNCTION plperl_md_array_out() RETURNS text[] AS $$ |
| 244 |
return [[], 1]; |
| 245 |
$$ LANGUAGE plperl; |
| 246 |
select plperl_md_array_out(); -- fail |
| 247 |
ERROR: multidimensional arrays must have array expressions with matching dimensions |
| 248 |
CONTEXT: PL/Perl function "plperl_md_array_out" |
| 249 |
CREATE OR REPLACE FUNCTION plperl_md_array_out() RETURNS text[] AS $$ |
| 250 |
return [1, []]; |
| 251 |
$$ LANGUAGE plperl; |
| 252 |
select plperl_md_array_out(); -- fail |
| 253 |
ERROR: multidimensional arrays must have array expressions with matching dimensions |
| 254 |
CONTEXT: PL/Perl function "plperl_md_array_out" |
| 255 |
CREATE OR REPLACE FUNCTION plperl_md_array_out() RETURNS text[] AS $$ |
| 256 |
return [[1], [[]]]; |
| 257 |
$$ LANGUAGE plperl; |
| 258 |
select plperl_md_array_out(); -- fail |
| 259 |
ERROR: multidimensional arrays must have array expressions with matching dimensions |
| 260 |
CONTEXT: PL/Perl function "plperl_md_array_out" |
| 261 |
-- make sure setof works |
| 262 |
create or replace function perl_setof_array(integer[]) returns setof integer[] language plperl as $$ |
| 263 |
my $arr = shift; |
| 264 |
for my $r (@$arr) { |
| 265 |
return_next $r; |
| 266 |
} |
| 267 |
return undef; |
| 268 |
$$; |
| 269 |
select perl_setof_array('{{1}, {2}, {3}}'); |
| 270 |
perl_setof_array |
| 271 |
------------------ |
| 272 |
{1} |
| 273 |
{2} |
| 274 |
{3} |
| 275 |
(3 rows) |
| 276 |
|
| 277 |
-- Test a forged PostgreSQL::InServer::ARRAY object lacking the 'array' key |
| 278 |
CREATE OR REPLACE FUNCTION perl_forged_array() RETURNS integer[] AS $$ |
| 279 |
return bless {}, "PostgreSQL::InServer::ARRAY"; |
| 280 |
$$ LANGUAGE plperl; |
| 281 |
SELECT perl_forged_array(); |
| 282 |
ERROR: could not get array reference from PostgreSQL::InServer::ARRAY object |
| 283 |
CONTEXT: PL/Perl function "perl_forged_array" |