Re: expression evaluation with expected datatypes

Lists: pgsql-hackers
From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: expression evaluation with expected datatypes
Date: 2012-07-08 07:13:09
Message-ID: CAFj8pRB9Nh3RpW-zoq3Z7gj5txrFmhJj68a3H5KhQ6Y9T=YkFQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hello

When I worked on parametrised DO statement, I had to solve following issue:

Syntax is:

DO (param list) $$ ... $$ LANGUAGE ... USING expr_list

What is correct way for evaluation of expr_list with specified target types?

I used two techniques:

1) evaluation expressions -
http://archives.postgresql.org/pgsql-hackers/2012-07/msg00340.php

this code is elegant and works well - with one significant issue -
doesn't support subqueries

2) SPI with parse_tree execution - SPI expect so SQL will be entered
in plain text form. But sometimes we have as input parsed tree - as
result of some parser. We can serialize tree to string, but then we
get different queryString and we will have problem with possible error
identification in queryString, I patched SPI and append a two
functions for evaluation parsed tree. Probably it should be better
done without SPI, but I missing some like DestSPI for general usage.

http://archives.postgresql.org/pgsql-hackers/2012-07/msg00361.php

What is correct way for solution of this task? I am thinking so there
some interface is missing

Regards

Pavel


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: expression evaluation with expected datatypes
Date: 2012-07-08 16:00:23
Message-ID: 3924.1341763223@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> writes:
> When I worked on parametrised DO statement, I had to solve following issue:

> Syntax is:

> DO (param list) $$ ... $$ LANGUAGE ... USING expr_list

> What is correct way for evaluation of expr_list with specified target types?

I'd argue that that's a pointlessly unwieldy syntax that's creating an
unnecessary problem. Just use a SELECT list, that is

DO ... USING value AS name, value2 AS name2, ...

The value expressions can define their own types just fine.

regards, tom lane


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: expression evaluation with expected datatypes
Date: 2012-07-08 16:25:07
Message-ID: CAFj8pRCxr-_-xD7BR0Cn+LM-63P1fV_xJE4DuWLZkW4O2fk2Tw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/7/8 Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>:
> Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> writes:
>> When I worked on parametrised DO statement, I had to solve following issue:
>
>> Syntax is:
>
>> DO (param list) $$ ... $$ LANGUAGE ... USING expr_list
>
>> What is correct way for evaluation of expr_list with specified target types?
>
> I'd argue that that's a pointlessly unwieldy syntax that's creating an
> unnecessary problem. Just use a SELECT list, that is
>
> DO ... USING value AS name, value2 AS name2, ...
>
> The value expressions can define their own types just fine.

it is solution, but it is consistent with nothing what we support

* it is different than USING clause in other statements or in dynamic
SQL in plpgsql
* it is different than named parameter syntax

and I don't think so plpgsql style in main core is ok - when parser
returns some tree, but we use substring of queryString instead.

Regards

Pavel

>
> regards, tom lane


From: Dimitri Fontaine <dimitri(at)2ndQuadrant(dot)fr>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: expression evaluation with expected datatypes
Date: 2012-07-10 11:47:49
Message-ID: m2liirkfbu.fsf@2ndQuadrant.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> writes:
> When I worked on parametrised DO statement, I had to solve following issue:

DO currently is a utility command, not a query. Do you mean to change
that?

Also, did you think about a lambda construct, which is basically
allowing functions to be defined inline in a query?

We could imagine several syntax to show up the idea, common keywords
here include LAMBDA, FLET or LABELS, but I think that expanding WITH
would be preferable for us.

WITH FUNCTION foo(param list) returns rettype language foo AS (
definition here
)
<query using foo() here>;

Other WITH extensions we can think about include support for DCL as
asked by David Fetter in the past already, and support for variables too
(a kind of per-query SET LOCAL).

I don't see how adding parameters and return values to utility commands
is going to be easier than adding a "lambda" facility.

Regards,
--
Dimitri Fontaine
http://2ndQuadrant.fr PostgreSQL : Expertise, Formation et Support


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: expression evaluation with expected datatypes
Date: 2012-07-10 12:25:01
Message-ID: CAFj8pRDTHNvmfcnViQHyPSFPNwdxK1jZo+bS_vH0zfwiHeQ=bw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/7/10 Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>:
> Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> writes:
>> When I worked on parametrised DO statement, I had to solve following issue:
>
> DO currently is a utility command, not a query. Do you mean to change
> that?
>
> Also, did you think about a lambda construct, which is basically
> allowing functions to be defined inline in a query?
>
> We could imagine several syntax to show up the idea, common keywords
> here include LAMBDA, FLET or LABELS, but I think that expanding WITH
> would be preferable for us.
>
> WITH FUNCTION foo(param list) returns rettype language foo AS (
> definition here
> )
> <query using foo() here>;
>
> Other WITH extensions we can think about include support for DCL as
> asked by David Fetter in the past already, and support for variables too
> (a kind of per-query SET LOCAL).
>
> I don't see how adding parameters and return values to utility commands
> is going to be easier than adding a "lambda" facility.

I don't think so we need true LAMBDA - we don't need support for
recursion and we don't need to modify system tables.

I don't see any advantage and usage of this complex syntax

Regards

Pavel

>
> Regards,
> --
> Dimitri Fontaine
> http://2ndQuadrant.fr PostgreSQL : Expertise, Formation et Support


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Dimitri Fontaine <dimitri(at)2ndQuadrant(dot)fr>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: expression evaluation with expected datatypes
Date: 2012-07-10 14:31:49
Message-ID: 20735.1341930709@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Dimitri Fontaine <dimitri(at)2ndQuadrant(dot)fr> writes:
> We could imagine several syntax to show up the idea, common keywords
> here include LAMBDA, FLET or LABELS, but I think that expanding WITH
> would be preferable for us.

> WITH FUNCTION foo(param list) returns rettype language foo AS (
> definition here
> )
> <query using foo() here>;

I like this idea. This gets rid of both the "how to pass parameters"
and the "how to return results" issues that exist with DO, as well as
assorted implementation problems that you hinted at by asking whether
DO would still be a utility command.

In the syntax-bikeshedding department, we'd still need the function body
to be a string literal, and I think we'd want the ability to add
options such as IMMUTABLE/VOLATILE. So I'd be inclined to move all
these options inside the parentheses that the WITH syntax dictates.
Perhaps

WITH FUNCTION foo(paramlist) AS (
returns int
as $$ ... $$
language plpgsql
... other CREATE FUNCTION options as needed ...
)
query here ...

regards, tom lane


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: expression evaluation with expected datatypes
Date: 2012-07-10 14:35:32
Message-ID: CAFj8pRAAfTTR_NpER5toir_Jjj=A_dsJCuby8Y0FEA1ybnOAEw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/7/10 Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>:
> Dimitri Fontaine <dimitri(at)2ndQuadrant(dot)fr> writes:
>> We could imagine several syntax to show up the idea, common keywords
>> here include LAMBDA, FLET or LABELS, but I think that expanding WITH
>> would be preferable for us.
>
>> WITH FUNCTION foo(param list) returns rettype language foo AS (
>> definition here
>> )
>> <query using foo() here>;
>
> I like this idea. This gets rid of both the "how to pass parameters"
> and the "how to return results" issues that exist with DO, as well as
> assorted implementation problems that you hinted at by asking whether
> DO would still be a utility command.

what is use case for this statement?

Regards

Pavel

>
> In the syntax-bikeshedding department, we'd still need the function body
> to be a string literal, and I think we'd want the ability to add
> options such as IMMUTABLE/VOLATILE. So I'd be inclined to move all
> these options inside the parentheses that the WITH syntax dictates.
> Perhaps
>
> WITH FUNCTION foo(paramlist) AS (
> returns int
> as $$ ... $$
> language plpgsql
> ... other CREATE FUNCTION options as needed ...
> )
> query here ...
>
> regards, tom lane


From: Dimitri Fontaine <dimitri(at)2ndQuadrant(dot)fr>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: expression evaluation with expected datatypes
Date: 2012-07-10 14:44:02
Message-ID: m2sjczekwd.fsf@2ndQuadrant.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> writes:
>>> WITH FUNCTION foo(param list) returns rettype language foo AS (
>>> definition here
>>> )
>>> <query using foo() here>;
>>
>> I like this idea. This gets rid of both the "how to pass parameters"
>> and the "how to return results" issues that exist with DO, as well as
>> assorted implementation problems that you hinted at by asking whether
>> DO would still be a utility command.
>
> what is use case for this statement?

It's the DO block idea turned into a query rather than a utility
command: you can now run a function that does not exists in the catalogs
*and* feed it parameters (either from the client, as literals in the
main query, or from the query itself) *and* you get a query result our
of it.

I'm not sure I can understand the difference between that and the use
case for which you want to implement DO blocks with parameters.

Regards,
--
Dimitri Fontaine
http://2ndQuadrant.fr PostgreSQL : Expertise, Formation et Support


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: expression evaluation with expected datatypes
Date: 2012-07-10 14:50:15
Message-ID: CAFj8pRCaNw7zZudga1TFW5gitdBbg-55J+fLhhYpGY9jFD09cQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/7/10 Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>:
> Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> writes:
>>>> WITH FUNCTION foo(param list) returns rettype language foo AS (
>>>> definition here
>>>> )
>>>> <query using foo() here>;
>>>
>>> I like this idea. This gets rid of both the "how to pass parameters"
>>> and the "how to return results" issues that exist with DO, as well as
>>> assorted implementation problems that you hinted at by asking whether
>>> DO would still be a utility command.
>>
>> what is use case for this statement?
>
> It's the DO block idea turned into a query rather than a utility
> command: you can now run a function that does not exists in the catalogs
> *and* feed it parameters (either from the client, as literals in the
> main query, or from the query itself) *and* you get a query result our
> of it.
>
> I'm not sure I can understand the difference between that and the use
> case for which you want to implement DO blocks with parameters.

this is similar to temporary functions - you need some temporary name
- it is insert to pg_proc, and you have to solve possible conflicts.

>
> Regards,
> --
> Dimitri Fontaine
> http://2ndQuadrant.fr PostgreSQL : Expertise, Formation et Support


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: expression evaluation with expected datatypes
Date: 2012-07-10 14:56:50
Message-ID: 21329.1341932210@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> writes:
> 2012/7/10 Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>:
>> I'm not sure I can understand the difference between that and the use
>> case for which you want to implement DO blocks with parameters.

> this is similar to temporary functions - you need some temporary name
> - it is insert to pg_proc, and you have to solve possible conflicts.

What's to solve? Presumably the WITH function name would take
precedence over anything in the catalogs, the same as WITH query names
take precedence over actual tables.

regards, tom lane


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: expression evaluation with expected datatypes
Date: 2012-07-10 15:05:34
Message-ID: 1341932624-sup-3455@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


Excerpts from Tom Lane's message of mar jul 10 10:56:50 -0400 2012:
> Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> writes:
> > 2012/7/10 Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>:
> >> I'm not sure I can understand the difference between that and the use
> >> case for which you want to implement DO blocks with parameters.
>
> > this is similar to temporary functions - you need some temporary name
> > - it is insert to pg_proc, and you have to solve possible conflicts.
>
> What's to solve? Presumably the WITH function name would take
> precedence over anything in the catalogs, the same as WITH query names
> take precedence over actual tables.

Hm, would the newly defined function mask all regular functions with
that name? If not, a seemingly innocuous change in a query could mean
calling not the function defined in the WITH FUNCTION clause but another
one with the same name but different parameter count/types.

--
Álvaro Herrera <alvherre(at)commandprompt(dot)com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: expression evaluation with expected datatypes
Date: 2012-07-10 15:49:49
Message-ID: 22439.1341935389@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Alvaro Herrera <alvherre(at)commandprompt(dot)com> writes:
> Excerpts from Tom Lane's message of mar jul 10 10:56:50 -0400 2012:
>> What's to solve? Presumably the WITH function name would take
>> precedence over anything in the catalogs, the same as WITH query names
>> take precedence over actual tables.

> Hm, would the newly defined function mask all regular functions with
> that name?

Only the ones with the same parameter types ...

> If not, a seemingly innocuous change in a query could mean
> calling not the function defined in the WITH FUNCTION clause but another
> one with the same name but different parameter count/types.

I would see this working as if the WITH function appeared in a schema
earlier in the search path than any regular functions. So the risk is
not greater, nor indeed different, than from any other overloaded
function name.

regards, tom lane