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;
19
select plperl_sum_array('{1,2,NULL}');
20
select plperl_sum_array('{}');
21
select plperl_sum_array('{{1,2,3}, {4,5,6}}');
22
select plperl_sum_array('{{{1,2,3}, {4,5,6}}, {{7,8,9}, {10,11,12}}}');
24
-- check whether we can handle arrays of maximum dimension (6)
25
select plperl_sum_array(ARRAY[[[[[[1,2],[3,4]],[[5,6],[7,8]]],[[[9,10],[11,12]],
27
[[[[17,18],[19,20]],[[21,22],[23,24]]],[[[25,26],[27,28]],[[29,30],[31,32]]]]],
28
[[[[[1,2],[3,4]],[[5,6],[7,8]]],[[[9,10],[11,12]],[[13,14],[15,16]]]],
29
[[[[17,18],[19,20]],[[21,22],[23,24]]],[[[25,26],[27,28]],[[29,30],[31,32]]]]]]);
31
-- what would we do with the arrays exceeding maximum dimension (7)
32
select plperl_sum_array('{{{{{{{1,2},{3,4}},{{5,6},{7,8}}},{{{9,10},{11,12}},
34
{{{{17,18},{19,20}},{{21,22},{23,24}}},{{{25,26},{27,28}},{{29,30},{31,32}}}}},
35
{{{{{1,2},{3,4}},{{5,6},{7,8}}},{{{9,10},{11,12}},{{13,14},{15,16}}}},
36
{{{{17,18},{19,20}},{{21,22},{23,24}}},{{{25,26},{27,28}},{{29,30},{31,32}}}}}},
37
{{{{{{1,2},{3,4}},{{5,6},{7,8}}},{{{9,10},{11,12}},{{13,14},{15,16}}}},
38
{{{{17,18},{19,20}},{{21,22},{23,24}}},{{{25,26},{27,28}},{{29,30},{31,32}}}}},
39
{{{{{1,2},{3,4}},{{5,6},{7,8}}},{{{9,10},{11,12}},{{13,14},{15,16}}}},
40
{{{{17,18},{19,20}},{{21,22},{23,24}}},{{{25,26},{27,28}},{{29,30},{31,32}}}}}}}'
43
select plperl_sum_array('{{{1,2,3}, {4,5,6,7}}, {{7,8,9}, {10, 11, 12}}}');
45
CREATE OR REPLACE FUNCTION plperl_concat(TEXT[]) RETURNS TEXT AS $$
46
my $array_arg = shift;
50
push @arrays, @$array_arg;
52
my $el = shift @arrays;
53
if (is_array_ref($el)) {
59
return $result.' '.$array_arg;
62
select plperl_concat('{"NULL","NULL","NULL''"}');
63
select plperl_concat('{{NULL,NULL,NULL}}');
64
select plperl_concat('{"hello"," ","world!"}');
67
CREATE TYPE foo AS (bar INTEGER, baz TEXT);
68
CREATE OR REPLACE FUNCTION plperl_array_of_rows(foo[]) RETURNS TEXT AS $$
69
my $array_arg = shift;
72
for my $row_ref (@$array_arg) {
73
die "not a hash reference" unless (ref $row_ref eq "HASH");
74
$result .= $row_ref->{bar}." items of ".$row_ref->{baz}.";";
76
return $result .' '. $array_arg;
79
select plperl_array_of_rows(ARRAY[ ROW(2, 'coffee'), ROW(0, 'sugar')]::foo[]);
81
-- composite type containing arrays
82
CREATE TYPE rowfoo AS (bar INTEGER, baz INTEGER[]);
84
CREATE OR REPLACE FUNCTION plperl_sum_row_elements(rowfoo) RETURNS TEXT AS $$
88
if (ref $row_ref ne 'HASH') {
92
$result = $row_ref->{bar};
93
die "not an array reference".ref ($row_ref->{baz})
94
unless (is_array_ref($row_ref->{baz}));
95
# process a single-dimensional array
96
foreach my $elem (@{$row_ref->{baz}}) {
97
$result += $elem unless ref $elem;
103
select plperl_sum_row_elements(ROW(1, ARRAY[2,3,4,5,6,7,8,9,10])::rowfoo);
105
-- composite type containing array of another composite type, which, in order,
106
-- contains an array of integers.
107
CREATE TYPE rowbar AS (foo rowfoo[]);
109
CREATE OR REPLACE FUNCTION plperl_sum_array_of_rows(rowbar) RETURNS TEXT AS $$
110
my $rowfoo_ref = shift;
113
if (ref $rowfoo_ref eq 'HASH') {
114
my $row_array_ref = $rowfoo_ref->{foo};
115
if (is_array_ref($row_array_ref)) {
116
foreach my $row_ref (@{$row_array_ref}) {
117
if (ref $row_ref eq 'HASH') {
118
$result += $row_ref->{bar};
119
die "not an array reference".ref ($row_ref->{baz})
120
unless (is_array_ref($row_ref->{baz}));
121
foreach my $elem (@{$row_ref->{baz}}) {
122
$result += $elem unless ref $elem;
126
die "element baz is not a reference to a rowfoo";
130
die "not a reference to an array of rowfoo elements"
133
die "not a reference to type rowbar";
138
select plperl_sum_array_of_rows(ROW(ARRAY[ROW(1, ARRAY[2,3,4,5,6,7,8,9,10])::rowfoo,
139
ROW(11, ARRAY[12,13,14,15,16,17,18,19,20])::rowfoo])::rowbar);
141
-- check arrays as out parameters
142
CREATE OR REPLACE FUNCTION plperl_arrays_out(OUT INTEGER[]) AS $$
143
return [[1,2,3],[4,5,6]];
146
select plperl_arrays_out();
148
-- check that we can return the array we passed in
149
CREATE OR REPLACE FUNCTION plperl_arrays_inout(INTEGER[]) returns INTEGER[] AS $$
153
select plperl_arrays_inout('{{1}, {2}, {3}}');
155
-- make sure setof works
156
create or replace function perl_setof_array(integer[]) returns setof integer[] language plperl as $$
164
select perl_setof_array('{{1}, {2}, {3}}');