Skip site navigation (1) Skip section navigation (2)

Re: How hard would it be to support LIKE in return declaration of generic record function calls ?

From: Hannu Krosing <hannu(at)2ndQuadrant(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: How hard would it be to support LIKE in return declaration of generic record function calls ?
Date: 2012-05-03 10:19:25
Message-ID: 1336040366.3106.474.camel@hvost (view raw or flat)
Thread:
Lists: pgsql-hackers
On Wed, 2012-05-02 at 14:32 -0500, Merlin Moncure wrote:
> On Wed, May 2, 2012 at 12:06 PM, Peter Eisentraut <peter_e(at)gmx(dot)net> wrote:
> > On ons, 2012-05-02 at 13:40 +0200, Hannu Krosing wrote:
> >> How hard would it be to add support for LIKE syntax, similar to table
> >> def in field list declaration for generic record functions
> >>
> >> What I'dd like to be able to do is to have a generic json_to_record
> >> function
> >>
> >> CREATE OR REPLACE RECORD json_to_record(json) RETURNS RECORD AS $$
> >> ...
> >> $$ LANGUAGE ... ;
> >>
> >> and then be able to call it like this
> >>
> >> insert into test2
> >> select * from json_to_record(jrec json) as (like test2);
> >
> > That would be very useful, and shouldn't be too hard to implement.  (I
> > had to look about three times to understand what this was supposed to
> > achieve, but I think the syntax is the right one after all.)
> 
> Although I like the functionality, is this better than the trick used
> by hstore/populate_record?  That approach doesn't require syntax
> changes and allows you to execute the function without 'FROM'.

You mean the one using a null::returntype for as first argument for
defining a return type of a function:

Convert an hstore to a predefined record type:

CREATE TABLE test (col1 integer, col2 text, col3 text);

SELECT * FROM populate_record(null::test,
                              '"col1"=>"456", "col2"=>"zzz"');
 col1 | col2 | col3 
------+------+------
  456 | zzz  | 
(1 row)

This works the same indeed, just seems to be a hack, though a cool
one :)

hannu=# insert into test
hannu-# SELECT * FROM populate_record(null::test,
                              '"id"=>"456", "data"=>"zzz"');
INSERT 0 1

putting the same functionality in LIKE at syntax level kind of feels
more orthogonal to table definition:)


select * from to_record(null::mytable, datasource);

vs

select * from to_record(datasource) as (like mytable);

OTOH, we do not support LIKE in type definition either.

If we were to overhaul template-based structure definition, the perhaps
the following syntax woul be better:

create type mytape as mytable; -- exact copy
create type mytape as (like mytable, comment text);  -- copy + extra
field

and same for giving type to generic record in function calls.

If it does not mess up the syntax, it would also be good to add simple
casts in record-->table case

select * from to_record(datasource)::mytable;



-- 
-------
Hannu Krosing
PostgreSQL Unlimited Scalability and Performance Consultant
2ndQuadrant Nordic
PG Admin Book: http://www.2ndQuadrant.com/books/


In response to

Responses

pgsql-hackers by date

Next:From: Pavel StehuleDate: 2012-05-03 12:13:09
Subject: Re: How hard would it be to support LIKE in return declaration of generic record function calls ?
Previous:From: Magnus HaganderDate: 2012-05-03 09:56:57
Subject: Re: Latch for the WAL writer - further reducing idle wake-ups.

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group