Re: Arrays of Records in PL/Perl

Lists: pgsql-hackers
From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Arrays of Records in PL/Perl
Date: 2011-07-12 18:45:19
Message-ID: 25EBA9D7-6B0F-4550-8D6F-354CB3185972@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hackers,

Given this script:

BEGIN;

CREATE TYPE foo AS ( this int, that int );

CREATE OR REPLACE FUNCTION dump(foo[]) returns text language plperlu AS $$
use Data::Dumper; Dumper shift;
$$;

CREATE OR REPLACE FUNCTION dump(foo) returns text language plperlu AS $$
use Data::Dumper; Dumper shift;
$$;

select dump(row(3, 5)::foo);
select dump(ARRAY[row(3, 5)::foo]);

ROLLBACK;

The output is:

dump
--------------------------
$VAR1 = { +
'that' => '5',+
'this' => '3' +
}; +

(1 row)

Time: 0.936 ms
dump
----------------------
$VAR1 = '{"(3,5)"}';+

(1 row)

That is, if a record is passed to a PL/Perl function, it's correctly converted into a hash. If, however, an array of records are passed, the record are stringified, rather than turned into hashes. This seems inconsistent. Bug?

Thanks,

David


From: Alex Hunsaker <badalex(at)gmail(dot)com>
To: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Arrays of Records in PL/Perl
Date: 2011-07-12 19:19:43
Message-ID: CAFaPBrSAvxpk=kU8pPcznJdTr-+959MWG+o95gHaM6vLytD-Qw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Jul 12, 2011 at 12:45, David E. Wheeler <david(at)kineticode(dot)com> wrote:
> Hackers,

> That is, if a record is passed to a PL/Perl function, it's correctly converted into a hash. If, however, an array of records are passed, the record are stringified, rather than turned into hashes. This seems inconsistent. Bug?

All Arrays in 9.0 and lower are strings, regardless of if they are
comprised of composite types. Its not so much a bug as a limitation.
Alexey Klyukin fixed this for 9.1 :-)

[ In 9.1 we could not make them straight perl arrayrefs as we needed
to keep the string representation for backwards compatibility. What we
did in 9.1 is overload the arrayref and string operations on blessed
object so you can treat it as either. ]


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Alex Hunsaker <badalex(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Arrays of Records in PL/Perl
Date: 2011-07-12 22:06:13
Message-ID: BA84CD41-25A6-46ED-95DD-240BF9F9E35C@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Jul 12, 2011, at 12:19 PM, Alex Hunsaker wrote:

> All Arrays in 9.0 and lower are strings, regardless of if they are
> comprised of composite types. Its not so much a bug as a limitation.
> Alexey Klyukin fixed this for 9.1 :-)

Oh?

dump
--------------------------
$VAR1 = { +
'that' => '5',+
'this' => '3' +
}; +

(1 row)

Time: 2.016 ms
dump
----------------------------------------------------
$VAR1 = bless( { +
'array' => [ +
{ +
'that' => '5', +
'this' => '3' +
} +
], +
'typeoid' => 16457 +
}, 'PostgreSQL::InServer::ARRAY' );+

(1 row)

Woo! Thanks!

> [ In 9.1 we could not make them straight perl arrayrefs as we needed
> to keep the string representation for backwards compatibility. What we
> did in 9.1 is overload the arrayref and string operations on blessed
> object so you can treat it as either. ]

Yep, awesome.

Best,

David