pl/python custom datatype parsers

From: Jan Urbański <wulczer(at)wulczer(dot)org>
To: Postgres - Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: pl/python custom datatype parsers
Date: 2010-12-23 14:15:08
Message-ID: 4D13596C.3020204@wulczer.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Here's a patch implementing custom parsers for data types mentioned in
http://archives.postgresql.org/pgsql-hackers/2010-12/msg01991.php. It's
an incremental patch on top of the plpython-refactor patch sent eariler.

Git branch for this patch:
https://github.com/wulczer/postgres/tree/custom-parsers.

The idea has been discussed in
http://archives.postgresql.org/pgsql-hackers/2010-12/msg01307.php.

With that patch, when built with --with-python, the hstore module
includes code that adds a GUC called plpython.hstore.

This GUC should be set to the full name of the hstore datatype, for
instance plpython.hstore = 'public.hstore'.

If it is set, the datatype's OID is looked up and hstore sets up a
rendezvous variable called PLPYTHON_<OID>_PARSERS that points to two
functions that can convert a hstore Datum to a PyObject and back.

PL/Python ot the other hand when it sees an argument with an unknown
type tries to look up a rendezvous variable using the type's OID and if
it finds it, it uses the parser functions pointed at by that variable.

Long story short, it works so:

LOAD 'hstore';
SET plpython.hstore = 'public.hstore'
CREATE FUNCTION pick_one(h hstore, key text) RETURNS hstore AS $$ return
{key: h[key]} $$ LANGUAGE plpythonu;
SELECT pick_one('a=>3,b=>4', 'b')
-- gives bask a hstore 'b=>4'

There's some ugliness with how hstore's Makefile handles building it,
and I'm not sure what's needed to make it work with the Windows build
system. Also, documentation is missing. It's already usable, but if we
decide to commit that, I'll probably need some help with Windows and docs.

I first tried to make hstore generate a separate .so with that
functionality if --with-python was specified, but couldn't convince the
Makefile to do that. So if you configure the tree with --with-python,
hstore will link to libpython, maybe that's OK?

Cheers,
Jan

PS: of course, once committed we can add custom parsers for isbn,
citext, uuids, cubes, and other weird things.

J

Attachment Content-Type Size
plpython-custom-parsers.diff text/x-patch 14.7 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Marti Raudsepp 2010-12-23 14:17:51 Re: Why is sorting on two columns so slower than sorting on one column?
Previous Message Kenneth Marshall 2010-12-23 14:03:00 Re: Why is sorting on two columns so slower than sorting on one column?