WIP: Allow SQL-language functions to reference parameters by parameter name

Lists: pgsql-hackers
From: Matthew Draper <matthew(at)trebex(dot)net>
To: pgsql-hackers(at)postgresql(dot)org
Subject: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-25 19:33:24
Message-ID: 4D8CEE04.9000700@trebex.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


Attached is a WIP patch that allows SQL-language functions to reference
their parameters by name.

It uses p_post_columnref_hook, so potentially ambiguous references
prefer the column... that seems to make the most sense, both because it
avoids a backwards incompatibility, and it conforms with SQL's usual
notion of assuming you mean the "nearest" name.

It allows the parameter name to be qualified with the function name, for
when you really mean you want the parameter.

This being my first foray into the PostgreSQL source, I expect the code
is horribly wrong in a variety of ways. Other than that, the regression
tests I've been using are a slight modification of existing queries; I
imagine they should look measurably different.

And finally, conspicuously absent are the documentation changes that
will obviously need to accompany a real patch.

(This builds & passes `make check` on current HEAD, a4425e3)

Thanks!

Matthew

--
matthew(at)trebex(dot)net

Attachment Content-Type Size
sql-named-param-refs-v0.patch text/x-patch 10.4 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Matthew Draper <matthew(at)trebex(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-25 19:55:04
Message-ID: 15539.1301082904@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Matthew Draper <matthew(at)trebex(dot)net> writes:
> Attached is a WIP patch that allows SQL-language functions to reference
> their parameters by name.

> It uses p_post_columnref_hook, so potentially ambiguous references
> prefer the column... that seems to make the most sense, both because it
> avoids a backwards incompatibility, and it conforms with SQL's usual
> notion of assuming you mean the "nearest" name.

Personally I'd vote for *not* having any such dangerous semantics as
that. We should have learned better by now from plpgsql experience.
I think the best idea is to throw error for ambiguous references,
period. That means you do need ways to disambiguate in both directions.
For column references you can just qualify with the table name/alias.
If the parameter reference is intended, allow qualification with the
function name.

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: Matthew Draper <matthew(at)trebex(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-25 20:08:24
Message-ID: AANLkTi=ZTAfGSVN+4X+b8SW7Zbu7CRb3DXsucm3-OWWw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2011/3/25 Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>:
> Matthew Draper <matthew(at)trebex(dot)net> writes:
>> Attached is a WIP patch that allows SQL-language functions to reference
>> their parameters by name.
>
>> It uses p_post_columnref_hook, so potentially ambiguous references
>> prefer the column... that seems to make the most sense, both because it
>> avoids a backwards incompatibility, and it conforms with SQL's usual
>> notion of assuming you mean the "nearest" name.
>
> Personally I'd vote for *not* having any such dangerous semantics as
> that.  We should have learned better by now from plpgsql experience.
> I think the best idea is to throw error for ambiguous references,
> period.  That means you do need ways to disambiguate in both directions.
> For column references you can just qualify with the table name/alias.
> If the parameter reference is intended, allow qualification with the
> function name.

I agree with Tom.

There can be GUC for controlling use or don't use a parameter names. I
am for GUC, because there will be a bilion conflicts. But a talk about
priorities - sql identifier or parameter is useless.

Regards

Pavel Stehule

>
>                        regards, tom lane
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Matthew Draper <matthew(at)trebex(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-25 20:20:03
Message-ID: 15775.1301084403@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:
> 2011/3/25 Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>:
>> I think the best idea is to throw error for ambiguous references,
>> period.

> There can be GUC for controlling use or don't use a parameter names. I
> am for GUC, because there will be a bilion conflicts. But a talk about
> priorities - sql identifier or parameter is useless.

GUCs are not tremendously helpful for problems such as this. If we
actually wanted to preserve full backwards compatibility, we'd need to
think of a way to mark SQL functions per-function as to what to do.
But I don't think that's necessary. Up to now there's been relatively
little use for naming the parameters of SQL functions, so I think there
will be few conflicts in the field if we just change the behavior. The
mess and complication we have for the comparable behavior in plpgsql
seemed necessary because of the number of existing usages that would
certainly break --- but I doubt that SQL-language functions will have
anywhere near as big a problem.

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: Matthew Draper <matthew(at)trebex(dot)net>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-25 21:14:34
Message-ID: AANLkTikF1EEw+RCwHSoX-kjneBvTxOLaXF9GUw6UP-SZ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2011/3/25 Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>:
> Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> writes:
>> 2011/3/25 Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>:
>>> I think the best idea is to throw error for ambiguous references,
>>> period.
>
>> There can be GUC for controlling use or don't use a parameter names. I
>> am for GUC, because there will be a bilion conflicts. But a talk about
>> priorities - sql identifier or parameter is useless.
>
> GUCs are not tremendously helpful for problems such as this.  If we
> actually wanted to preserve full backwards compatibility, we'd need to
> think of a way to mark SQL functions per-function as to what to do.
> But I don't think that's necessary.  Up to now there's been relatively
> little use for naming the parameters of SQL functions, so I think there
> will be few conflicts in the field if we just change the behavior.  The
> mess and complication we have for the comparable behavior in plpgsql
> seemed necessary because of the number of existing usages that would
> certainly break --- but I doubt that SQL-language functions will have
> anywhere near as big a problem.

should be nice some converting tool for pg_dump or pg_upgrade. It can
dump SQL functions with only qualified identifiers.

Pavel

>
>                        regards, tom lane
>


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-25 22:59:09
Message-ID: FBB8511E-F274-4E0F-86A8-63EFFCC979D7@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mar 25, 2011, at 4:20 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> GUCs are not tremendously helpful for problems such as this. If we
> actually wanted to preserve full backwards compatibility, we'd need to
> think of a way to mark SQL functions per-function as to what to do.
> But I don't think that's necessary. Up to now there's been relatively
> little use for naming the parameters of SQL functions, so I think there
> will be few conflicts in the field if we just change the behavior.

Oh wow, I don't agree with that at all. People may name the parameters for documentation purposes, and then have things like WHERE foo = $1, foo happening also to be the name associated with $1. Boom!

In any case, I think this is 9.2 material. We need to get a beta out the door, and I emphatically think we should be focusing on resolving the issues with features already committed, not committing new ones.

...Robert


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-25 23:45:07
Message-ID: 26416.1301096707@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Mar 25, 2011, at 4:20 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> But I don't think that's necessary. Up to now there's been relatively
>> little use for naming the parameters of SQL functions, so I think there
>> will be few conflicts in the field if we just change the behavior.

> Oh wow, I don't agree with that at all. People may name the parameters for documentation purposes, and then have things like WHERE foo = $1, foo happening also to be the name associated with $1. Boom!

Well, maybe, but it's not like it's subtle or hard to fix.

> In any case, I think this is 9.2 material.

Oh, of course. It *is* just a WIP patch, anyway.

regards, tom lane


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-26 00:42:14
Message-ID: 53181FCB-916D-4750-8AA1-B568953554E1@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mar 25, 2011, at 7:45 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Well, maybe, but it's not like it's subtle or hard to fix.

Depends how much of it you have. I've become very skeptical of anything that breaks pg_dump-and-reload-ability. And doubly so now that such problems also mean breaking pg_upgrade after the old cluster has been shut down.

...Robert


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-26 00:58:47
Message-ID: 27844.1301101127@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Mar 25, 2011, at 7:45 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Well, maybe, but it's not like it's subtle or hard to fix.

> Depends how much of it you have. I've become very skeptical of
> anything that breaks pg_dump-and-reload-ability.

This wouldn't break pg_dump scripts, because they disable
check_function_bodies. You would get a failure on first *use*
of a function, which is something different.

Basically my concern here is that in the name of easing a short-term
conversion issue, we'll be condemning users to a future of subtle,
hard-to-find bugs due to ambiguous names. How many hundreds of
reports have we seen about the equivalent problem in plpgsql?

You could argue that the frequency of plpgsql issues was at least partly
due to having a poor choice of which way to resolve the ambiguity, but
I don't think it can be entirely blamed on that.

regards, tom lane


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-26 01:12:33
Message-ID: AANLkTinx1E40bRMFMWuF1rmU0mrsb5tLpMA3AXNEjT-w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Mar 25, 2011 at 8:58 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>> On Mar 25, 2011, at 7:45 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>>> Well, maybe, but it's not like it's subtle or hard to fix.
>
>> Depends how much of it you have. I've become very skeptical of
>> anything that breaks pg_dump-and-reload-ability.
>
> This wouldn't break pg_dump scripts, because they disable
> check_function_bodies.  You would get a failure on first *use*
> of a function, which is something different.
>
> Basically my concern here is that in the name of easing a short-term
> conversion issue, we'll be condemning users to a future of subtle,
> hard-to-find bugs due to ambiguous names.  How many hundreds of
> reports have we seen about the equivalent problem in plpgsql?
>
> You could argue that the frequency of plpgsql issues was at least partly
> due to having a poor choice of which way to resolve the ambiguity, but
> I don't think it can be entirely blamed on that.

As I've said before, I believe that the root cause of this problem is
that using the same syntax for variables and column names is a bad
idea in the first place. If we used $foo or ?foo or ${foo} or $.foo
or &&foo!!$#? to mean "the parameter called foo", then this would all
be a non-issue.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Joshua Berkus <josh(at)agliodbs(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org, Matthew Draper <matthew(at)trebex(dot)net>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-26 01:22:48
Message-ID: 154479001.271789.1301102568211.JavaMail.root@mail-1.01.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Tom,

> Personally I'd vote for *not* having any such dangerous semantics as
> that. We should have learned better by now from plpgsql experience.
> I think the best idea is to throw error for ambiguous references,
> period.

As a likely heavy user of this feature, I agree with Tom here. I really don't want the column being silently preferred in SQL functions, when PL/pgSQL functions are throwing an error. I'd end up spending hours debugging this.

Also, I don't understand why this would be a dump/reload issue if $1 and $2 continue to work.

--
Josh Berkus
PostgreSQL Experts Inc.
http://pgexperts.com
San Francisco


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Joshua Berkus <josh(at)agliodbs(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>, Matthew Draper <matthew(at)trebex(dot)net>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-26 02:45:13
Message-ID: 42265A7F-5F79-4669-87F8-45089C3E4492@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mar 25, 2011, at 9:22 PM, Joshua Berkus <josh(at)agliodbs(dot)com> wrote:
> Tom,
>
>> Personally I'd vote for *not* having any such dangerous semantics as
>> that. We should have learned better by now from plpgsql experience.
>> I think the best idea is to throw error for ambiguous references,
>> period.
>
> As a likely heavy user of this feature, I agree with Tom here. I really don't want the column being silently preferred in SQL functions, when PL/pgSQL functions are throwing an error. I'd end up spending hours debugging this.
>
> Also, I don't understand why this would be a dump/reload issue if $1 and $2 continue to work.
>

Because an identifier that previously referred unambiguously to a column might now be ambiguous, if there is a parameter with the same name.

...Robert


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Joshua Berkus <josh(at)agliodbs(dot)com>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>, Matthew Draper <matthew(at)trebex(dot)net>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-26 03:01:45
Message-ID: 28551.1301108505@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Mar 25, 2011, at 9:22 PM, Joshua Berkus <josh(at)agliodbs(dot)com> wrote:
>> Also, I don't understand why this would be a dump/reload issue if $1 and $2 continue to work.

> Because an identifier that previously referred unambiguously to a column might now be ambiguous, if there is a parameter with the same name.

Yes, a function that previously worked might now throw error, if we make
ambiguous names be errors. But this is *not* a failure that would occur
during dump/reload. You'd have to actually run the function.

regards, tom lane


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-26 03:05:50
Message-ID: 7FE95B65-3F93-4431-B5F4-E0D26981BB7F@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mar 25, 2011, at 9:12 PM, Robert Haas wrote:

>
> As I've said before, I believe that the root cause of this problem is
> that using the same syntax for variables and column names is a bad
> idea in the first place. If we used $foo or ?foo or ${foo} or $.foo
> or &&foo!!$#? to mean "the parameter called foo", then this would all
> be a non-issue.

Yes *please*. Man that would make maintenance of such functions easier.

Best,

David


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-26 03:23:42
Message-ID: 28741.1301109822@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> As I've said before, I believe that the root cause of this problem is
> that using the same syntax for variables and column names is a bad
> idea in the first place. If we used $foo or ?foo or ${foo} or $.foo
> or &&foo!!$#? to mean "the parameter called foo", then this would all
> be a non-issue.

If this were PL/perl, or PL/almost-anything-except-SQL, I could get
behind such a proposal. But it's not, it's SQL; and SQL doesn't do
things that way. SQL's idea of disambiguation is qualified names.

And even more to the point: to the extent you think that weird syntax
might be a suitable solution, you have to keep in mind that the SQL
committee could take over any such syntax at the drop of a hat.
See the recent unpleasantness concerning => ...

regards, tom lane


From: Darren Duncan <darren(at)darrenduncan(dot)net>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: resolving SQL ambiguity (was Re: WIP: Allow SQL-lang funcs to ref params by param name)
Date: 2011-03-26 03:50:21
Message-ID: 4D8D627D.9010001@darrenduncan.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas wrote:
> On Mar 25, 2011, at 9:22 PM, Joshua Berkus <josh(at)agliodbs(dot)com> wrote:
>> Tom,
>>
>>> Personally I'd vote for *not* having any such dangerous semantics as
>>> that. We should have learned better by now from plpgsql experience.
>>> I think the best idea is to throw error for ambiguous references,
>>> period.
>> As a likely heavy user of this feature, I agree with Tom here. I really don't want the column being silently preferred in SQL functions, when PL/pgSQL functions are throwing an error. I'd end up spending hours debugging this.
>>
>> Also, I don't understand why this would be a dump/reload issue if $1 and $2 continue to work.
>
> Because an identifier that previously referred unambiguously to a column might now be ambiguous, if there is a parameter with the same name.

I mention 2 possible solutions here, both which involve syntax alterations, each
between the ---------- lines. I personally like the second/lower option more.

------------

Might it be reasonable, perhaps as a 9.2 feature, to add top-level-namespaces so
that one could always explicitly qualify what they are referring to?

For example, you could have the 3 "sch", "lex", "attr" (I may have missed some
useful ones).

The "sch" TLN would unambiguously refer directly to a schema object, such as a
database table.

The "lex" TLN would unambiguously refer directly to a lexical, either a
parameter of the current routine or to a lexical variable.

The "attr" TLN would unambiguously refer to a table/etc column/attribute in the
manner typical for SQL.

Use them like:

sch.foo - the table/etc foo
lex.foo - the lexical variable foo
attr.foo - the column foo

Use of these TLN are optional where there is no ambiguity.

The TLN are not reserved words, but if one has an entity named the same, then
references to it must be TLN-qualified; eg:

lex.sch
lex.lex
lex.attr

Now these are just examples. You may find a different set works better.

--------------

There are also alternate solutions.

For example, it could be mandated that lexical-scope aliases for any
data/var-like schema object are required in routines, where the aliases are
distinct from all lexical vars/params/etc, and then all SQL/code in the routines
may only refer to the schema objects by the aliases.

Effectively this makes it so that routines can no longer see non-lexical vars
but for those from parameters, and this aliasing is defining a parameter whose
argument is supplied by the DBMS automatically rather than as an explicit
routine caller argument.

That way, inside a routine body there are only lexical names for things, and so
no namespace-qualification is ever needed by the regular SQL.

Similarly, if you always think of table column names as referring to an
attribute or element of a table variable, then just reference the column
qualified by the table name (or the lexical alias thereof). Same as you do in
any other programming language. Of course, sometimes you don't have to qualify
column name references as context could make it unambiguous. Or, a shorthand
like a simple leading "." could unambiguously say you're referring to a column
of the particular table in context.

With those in place, all unqualified references are straight to lexical
variables or parameters.

And so, this is also an effective way to resolve the ambiguity and I prefer the
latter design personally.

Here's an example in quasi-PL/PgSQL:

create function myrtn (myparam integer, mytbl ::= mydb.myschema.mytbl) as
declare
myvar integer := 5;
$body$
begin
select (.mycol + myvar * myparam) as mynewcol from mytbl;
end;
$body$

Note that I've already thought through this last example as these methods of
avoiding ambiguity are loosely-speaking how my language Muldis D avoids the
problem faced by many SQL procedures.

The ".mycol" syntax specifically was inspired originally for me by Perl 6 where
the lack of something just before the "." means that the implicit topic variable
is referred to, like if you said "$_.mycol".

A Perl 6 analogy being something like:

$mytbl.map:{ .mycol + $myvar * $myparam }

aka:

$mytbl.map:{ $_.mycol + $myvar * $myparam }

--------------

-- Darren Duncan


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-26 06:06:21
Message-ID: BDF350C1-A57D-436A-80F2-A876E47F971A@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mar 25, 2011, at 11:23 PM, Tom Lane wrote:

> If this were PL/perl, or PL/almost-anything-except-SQL, I could get
> behind such a proposal. But it's not, it's SQL; and SQL doesn't do
> things that way. SQL's idea of disambiguation is qualified names.
>
> And even more to the point: to the extent you think that weird syntax
> might be a suitable solution, you have to keep in mind that the SQL
> committee could take over any such syntax at the drop of a hat.
> See the recent unpleasantness concerning => ...

Perhaps we could use `=>varname`. ;-P

David


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Darren Duncan <darren(at)darrenduncan(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: resolving SQL ambiguity (was Re: WIP: Allow SQL-lang funcs to ref params by param name)
Date: 2011-03-26 07:24:09
Message-ID: AANLkTi=n-rkEwg3FUrAGAS5XK1qfcnCLwGkG0LeB3bFn@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2011/3/26 Darren Duncan <darren(at)darrenduncan(dot)net>:
> Robert Haas wrote:
>>
>> On Mar 25, 2011, at 9:22 PM, Joshua Berkus <josh(at)agliodbs(dot)com> wrote:
>>>
>>> Tom,
>>>
>>>> Personally I'd vote for *not* having any such dangerous semantics as
>>>> that. We should have learned better by now from plpgsql experience.
>>>> I think the best idea is to throw error for ambiguous references,
>>>> period.
>>>
>>> As a likely heavy user of this feature, I agree with Tom here.  I really
>>> don't want the column being silently preferred in SQL functions, when
>>> PL/pgSQL functions are throwing an error.  I'd end up spending hours
>>> debugging this.
>>>
>>> Also, I don't understand why this would be a dump/reload issue if $1 and
>>> $2 continue to work.
>>
>> Because an identifier that previously referred unambiguously to a column
>> might now be ambiguous, if there is a parameter with the same name.
>
> I mention 2 possible solutions here, both which involve syntax alterations,
> each between the ---------- lines.  I personally like the second/lower
> option more.
>
> ------------
>
> Might it be reasonable, perhaps as a 9.2 feature, to add
> top-level-namespaces so that one could always explicitly qualify what they
> are referring to?
>
> For example, you could have the 3 "sch", "lex", "attr" (I may have missed
> some useful ones).
>
> The "sch" TLN would unambiguously refer directly to a schema object, such as
> a database table.
>
> The "lex" TLN would unambiguously refer directly to a lexical, either a
> parameter of the current routine or to a lexical variable.
>
> The "attr" TLN would unambiguously refer to a table/etc column/attribute in
> the manner typical for SQL.
>
> Use them like:
>
>  sch.foo - the table/etc foo
>  lex.foo - the lexical variable foo
>  attr.foo - the column foo
>
> Use of these TLN are optional where there is no ambiguity.
>
> The TLN are not reserved words, but if one has an entity named the same,
> then references to it must be TLN-qualified; eg:
>
>  lex.sch
>  lex.lex
>  lex.attr
>
> Now these are just examples.  You may find a different set works better.

-1

this is not based on any pattern on SQL. It's not simple, and it
introduce a reserved keywords

Regards

Pavel

>
> --------------
>
> There are also alternate solutions.
>
> For example, it could be mandated that lexical-scope aliases for any
> data/var-like schema object are required in routines, where the aliases are
> distinct from all lexical vars/params/etc, and then all SQL/code in the
> routines may only refer to the schema objects by the aliases.
>
> Effectively this makes it so that routines can no longer see non-lexical
> vars but for those from parameters, and this aliasing is defining a
> parameter whose argument is supplied by the DBMS automatically rather than
> as an explicit routine caller argument.
>
> That way, inside a routine body there are only lexical names for things, and
> so no namespace-qualification is ever needed by the regular SQL.
>
> Similarly, if you always think of table column names as referring to an
> attribute or element of a table variable, then just reference the column
> qualified by the table name (or the lexical alias thereof).  Same as you do
> in any other programming language.  Of course, sometimes you don't have to
> qualify column name references as context could make it unambiguous.  Or, a
> shorthand like a simple leading "." could unambiguously say you're referring
> to a column of the particular table in context.
>
> With those in place, all unqualified references are straight to lexical
> variables or parameters.
>
> And so, this is also an effective way to resolve the ambiguity and I prefer
> the latter design personally.
>
> Here's an example in quasi-PL/PgSQL:
>
>  create function myrtn (myparam integer, mytbl ::= mydb.myschema.mytbl) as
>  declare
>      myvar integer := 5;
>  $body$
>  begin
>      select (.mycol + myvar * myparam) as mynewcol from mytbl;
>  end;
>  $body$
>
> Note that I've already thought through this last example as these methods of
> avoiding ambiguity are loosely-speaking how my language Muldis D avoids the
> problem faced by many SQL procedures.
>
> The ".mycol" syntax specifically was inspired originally for me by Perl 6
> where the lack of something just before the "." means that the implicit
> topic variable is referred to, like if you said "$_.mycol".
>
> A Perl 6 analogy being something like:
>
>  $mytbl.map:{ .mycol + $myvar * $myparam }
>
> aka:
>
>  $mytbl.map:{ $_.mycol + $myvar * $myparam }
>
> --------------
>
> -- Darren Duncan
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-26 13:41:46
Message-ID: 762095B9-DF7A-465F-8826-B79B085DFEFE@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mar 25, 2011, at 11:23 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> If this were PL/perl, or PL/almost-anything-except-SQL, I could get
> behind such a proposal. But it's not, it's SQL; and SQL doesn't do
> things that way. SQL's idea of disambiguation is qualified names.
>
> And even more to the point: to the extent you think that weird syntax
> might be a suitable solution, you have to keep in mind that the SQL
> committee could take over any such syntax at the drop of a hat.
> See the recent unpleasantness concerning => ...

You can't be guaranteed that they won't standardize something incompatible no matter what we do. We could choose to do it as you've proposed and they could then standardize some weird syntax - the => is a fairly relevant example of exactly that.

...Robert


From: Darren Duncan <darren(at)darrenduncan(dot)net>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: resolving SQL ambiguity (was Re: WIP: Allow SQL-lang funcs to ref params by param name)
Date: 2011-03-26 20:38:37
Message-ID: 4D8E4ECD.1010700@darrenduncan.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Pavel Stehule wrote:
> 2011/3/26 Darren Duncan <darren(at)darrenduncan(dot)net>:
>> I mention 2 possible solutions here, both which involve syntax alterations,
>> each between the ---------- lines. I personally like the second/lower
>> option more.
>
> -1
>
> this is not based on any pattern on SQL. It's not simple, and it
> introduce a reserved keywords

Okay, here's a much simpler proposal with the most important bit of the old one.

1. In all situations where there is ambiguity such that an identifier reference
(not declaration) may be referring to either a lexical variable/parameter of the
current routine, or to the name of the table column of the contextually current
table of the current SQL statement, the ambiguity is always resolved in favor of
the lexical var/param. If I am not mistaken, that is what PL/PgSQL already does
since 9.0.

2. If an identifier reference has a leading "." then that will force it to be
interpreted as a column instead (and the code will fail if there is no such
column), and so ".colname" is a shorthand for "tablename.colname"; but like with
the old "colname" it only works when just 1 of the source tables has "colname"
else it is still ambiguous like before.

Example:

select (.mycol + myvar * myparam) as mynewcol from mytbl;

This solution is a very terse and understandable change.

There are no reserved keywords. Legacy user code has no change where there were
no conflicts before. Legacy user code has no change in the case of conflict if
it was previously resolved to favor the lexical var/param.

Legacy user code only gains a leading "." in the few places where conflict was
resolved in favor of a column name before where a same-named lexical/param existed.

So what's not to like about this?

-- Darren Duncan


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Darren Duncan <darren(at)darrenduncan(dot)net>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: resolving SQL ambiguity (was Re: WIP: Allow SQL-lang funcs to ref params by param name)
Date: 2011-03-26 20:53:11
Message-ID: AANLkTine7xetebE72Hkz18hGXeXnDkUHQrL8H-akH5i0@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2011/3/26 Darren Duncan <darren(at)darrenduncan(dot)net>:
> Pavel Stehule wrote:
>>
>> 2011/3/26 Darren Duncan <darren(at)darrenduncan(dot)net>:
>>>
>>> I mention 2 possible solutions here, both which involve syntax
>>> alterations,
>>> each between the ---------- lines.  I personally like the second/lower
>>> option more.
>>
>> -1
>>
>> this is not based on any pattern on SQL. It's not simple, and it
>> introduce a reserved keywords
>
> Okay, here's a much simpler proposal with the most important bit of the old
> one.
>
> 1.  In all situations where there is ambiguity such that an identifier
> reference (not declaration) may be referring to either a lexical
> variable/parameter of the current routine, or to the name of the table
> column of the contextually current table of the current SQL statement, the
> ambiguity is always resolved in favor of the lexical var/param.  If I am not
> mistaken, that is what PL/PgSQL already does since 9.0.
>
> 2.  If an identifier reference has a leading "." then that will force it to
> be interpreted as a column instead (and the code will fail if there is no
> such column), and so ".colname" is a shorthand for "tablename.colname"; but
> like with the old "colname" it only works when just 1 of the source tables
> has "colname" else it is still ambiguous like before.
>
> Example:
>
>    select (.mycol + myvar * myparam) as mynewcol from mytbl;
>
> This solution is a very terse and understandable change.
>
> There are no reserved keywords.  Legacy user code has no change where there
> were no conflicts before.  Legacy user code has no change in the case of
> conflict if it was previously resolved to favor the lexical var/param.
>
> Legacy user code only gains a leading "." in the few places where conflict
> was resolved in favor of a column name before where a same-named
> lexical/param existed.
>
> So what's not to like about this?

sorry - I dislike this. The design is correct, but it is against to
SQL verbosity. A reader must to thinking about missing tablenames. I
dont't think so it is good solution, because it doesn't solve a
backing compatibility problem - somebody must to fix a function still,
and I think so it is much preferable to fix like:

select (mytbl.mycol + myvar * myparam) as mynewcol from mytbl;

your proposal saves a five chars, but it has a negative impacts on
readability - there should be more tables.

There are no reason to introduce a new concepts - SQL knows a aliases.

Regards

Pavel

>
> -- Darren Duncan
>


From: Darren Duncan <darren(at)darrenduncan(dot)net>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: resolving SQL ambiguity (was Re: WIP: Allow SQL-lang funcs to ref params by param name)
Date: 2011-03-26 21:04:14
Message-ID: 4D8E54CE.1060006@darrenduncan.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Pavel Stehule wrote:
> sorry - I dislike this. The design is correct, but it is against to
> SQL verbosity. A reader must to thinking about missing tablenames. I
> dont't think so it is good solution, because it doesn't solve a
> backing compatibility problem - somebody must to fix a function still,
> and I think so it is much preferable to fix like:
>
> select (mytbl.mycol + myvar * myparam) as mynewcol from mytbl;
>
> your proposal saves a five chars, but it has a negative impacts on
> readability - there should be more tables.
>
> There are no reason to introduce a new concepts - SQL knows a aliases.

Well, going forward, I know I would much rather have to say "mytbl.mycol" than
have to say "myfunc.myparam". And I certainly would want to expect that when
one says "... as foo" that this "foo" is treated as a declaration unambiguously
and is never substituted for some parameter or there be other grief as I seem to
recall having in 8.4. -- Darren Duncan


From: Dimitri Fontaine <dimitri(at)2ndQuadrant(dot)fr>
To: Joshua Berkus <josh(at)agliodbs(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org, Matthew Draper <matthew(at)trebex(dot)net>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-26 21:19:12
Message-ID: m2zkohd1gv.fsf@2ndQuadrant.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Joshua Berkus <josh(at)agliodbs(dot)com> writes:
>> Personally I'd vote for *not* having any such dangerous semantics as
>> that. We should have learned better by now from plpgsql experience.
>> I think the best idea is to throw error for ambiguous references,
>> period.
>
> As a likely heavy user of this feature, I agree with Tom here. I really
> don't want the column being silently preferred in SQL functions, when
> PL/pgSQL functions are throwing an error. I'd end up spending hours
> debugging this.

+1

I think the best choice is to only accept qualified parameter names in
SQL functions (function_name.parameter_name). If a referenced table
share the function's name, ERROR out and HINT to alias the table name.

If we allow more than that, we're opening the door to ambiguity, bug
reports, and more than that costly migrations. I don't see any benefit
in having to audit all SQL functions for ambiguity on a flag day, when
this could be avoided easily.

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: Joshua Berkus <josh(at)agliodbs(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org, Matthew Draper <matthew(at)trebex(dot)net>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-26 21:26:55
Message-ID: AANLkTimAMfpdEfiVz7u_S7P6FHwDmyvo-L_YO4oTEP87@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2011/3/26 Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>:
> Joshua Berkus <josh(at)agliodbs(dot)com> writes:
>>> Personally I'd vote for *not* having any such dangerous semantics as
>>> that. We should have learned better by now from plpgsql experience.
>>> I think the best idea is to throw error for ambiguous references,
>>> period.
>>
>> As a likely heavy user of this feature, I agree with Tom here.  I really
>> don't want the column being silently preferred in SQL functions, when
>> PL/pgSQL functions are throwing an error.  I'd end up spending hours
>> debugging this.
>
> +1
>
> I think the best choice is to only accept qualified parameter names in
> SQL functions (function_name.parameter_name).  If a referenced table
> share the function's name, ERROR out and HINT to alias the table name.

it's maybe too hard. I agree so we should to use a function_name alias
when collision is possible. Still there are more use cases, where SQL
function is used as macro, and there a alias isn't necessary

CREATE OR REPLACE FUNCTION greatest(VARIADIC "values" anyarray)
RETURNS anyelement AS $$
SELECT max(v) FROM unnest("values")
$$ LANGUAGE sql;

Regards

Pavel

>
> If we allow more than that, we're opening the door to ambiguity, bug
> reports, and more than that costly migrations.  I don't see any benefit
> in having to audit all SQL functions for ambiguity on a flag day, when
> this could be avoided easily.
>
> Regards,
> --
> Dimitri Fontaine
> http://2ndQuadrant.fr     PostgreSQL : Expertise, Formation et Support
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>
Cc: Joshua Berkus <josh(at)agliodbs(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org, Matthew Draper <matthew(at)trebex(dot)net>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-26 22:06:05
Message-ID: AANLkTingqk9KpZOzbMEfEy=a8oXyt-Nx4V2DEcgUjfRb@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sat, Mar 26, 2011 at 5:19 PM, Dimitri Fontaine
<dimitri(at)2ndquadrant(dot)fr> wrote:
> I think the best choice is to only accept qualified parameter names in
> SQL functions (function_name.parameter_name).  If a referenced table
> share the function's name, ERROR out and HINT to alias the table name.
>
> If we allow more than that, we're opening the door to ambiguity, bug
> reports, and more than that costly migrations.  I don't see any benefit
> in having to audit all SQL functions for ambiguity on a flag day, when
> this could be avoided easily.

That syntax is sufficiently unwieldly that few people will want to use
it in real life, but certainly the backward compatibility problem is
much less than with what Tom proposed.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Dimitri Fontaine <dimitri(at)2ndQuadrant(dot)fr>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>, Joshua Berkus <josh(at)agliodbs(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org, Matthew Draper <matthew(at)trebex(dot)net>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-27 10:11:09
Message-ID: m2r59sdgaq.fsf@2ndQuadrant.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:

> On Sat, Mar 26, 2011 at 5:19 PM, Dimitri Fontaine
> <dimitri(at)2ndquadrant(dot)fr> wrote:
>> I think the best choice is to only accept qualified parameter names in
>> SQL functions (function_name.parameter_name).  If a referenced table
>> share the function's name, ERROR out and HINT to alias the table name.
>>
>> If we allow more than that, we're opening the door to ambiguity, bug
>> reports, and more than that costly migrations.  I don't see any benefit
>> in having to audit all SQL functions for ambiguity on a flag day, when
>> this could be avoided easily.
>
> That syntax is sufficiently unwieldly that few people will want to use
> it in real life, but certainly the backward compatibility problem is
> much less than with what Tom proposed.

Well, we would still support positional arguments like $1 $2 etc, right?

In Pavel's example I wouldn't mind about using the "values" parameter
name but would stick to using $1.

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


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Dimitri Fontaine <dimitri(at)2ndQuadrant(dot)fr>
Cc: Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>, Joshua Berkus <josh(at)agliodbs(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>, Matthew Draper <matthew(at)trebex(dot)net>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-27 11:14:38
Message-ID: BA797B50-9BA9-4E4C-9A8E-2A8A4D7C4061@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mar 27, 2011, at 6:11 AM, Dimitri Fontaine <dimitri(at)2ndQuadrant(dot)fr> wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>> That syntax is sufficiently unwieldly that few people will want to use
>> it in real life, but certainly the backward compatibility problem is
>> much less than with what Tom proposed.
>
> Well, we would still support positional arguments like $1 $2 etc, right?

Yeah, that's not going away.

...Robert


From: David Fetter <david(at)fetter(dot)org>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-27 13:42:33
Message-ID: 20110327134233.GB31445@fetter.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Mar 25, 2011 at 09:12:33PM -0400, Robert Haas wrote:
> On Fri, Mar 25, 2011 at 8:58 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> > Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> >> On Mar 25, 2011, at 7:45 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> >>> Well, maybe, but it's not like it's subtle or hard to fix.
> >
> >> Depends how much of it you have. I've become very skeptical of
> >> anything that breaks pg_dump-and-reload-ability.
> >
> > This wouldn't break pg_dump scripts, because they disable
> > check_function_bodies.  You would get a failure on first *use*
> > of a function, which is something different.
> >
> > Basically my concern here is that in the name of easing a short-term
> > conversion issue, we'll be condemning users to a future of subtle,
> > hard-to-find bugs due to ambiguous names.  How many hundreds of
> > reports have we seen about the equivalent problem in plpgsql?
> >
> > You could argue that the frequency of plpgsql issues was at least partly
> > due to having a poor choice of which way to resolve the ambiguity, but
> > I don't think it can be entirely blamed on that.
>
> As I've said before, I believe that the root cause of this problem is
> that using the same syntax for variables and column names is a bad
> idea in the first place. If we used $foo or ?foo or ${foo} or $.foo
> or &&foo!!$#? to mean "the parameter called foo", then this would all
> be a non-issue.

How about psql's :foo syntax?

Cheers,
David.
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david(dot)fetter(at)gmail(dot)com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: David Fetter <david(at)fetter(dot)org>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-27 14:45:19
Message-ID: 4D8F4D7F.3020301@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 03/27/2011 09:42 AM, David Fetter wrote:
> On Fri, Mar 25, 2011 at 09:12:33PM -0400, Robert Haas wrote:
>> As I've said before, I believe that the root cause of this problem is
>> that using the same syntax for variables and column names is a bad
>> idea in the first place. If we used $foo or ?foo or ${foo} or $.foo
>> or&&foo!!$#? to mean "the parameter called foo", then this would all
>> be a non-issue.
> How about psql's :foo syntax?
>
>

Surely the time has long gone when we could have made such a choice. And
the choice was not made in a vacuum.

cheers

andrew


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: David Fetter <david(at)fetter(dot)org>, Robert Haas <robertmhaas(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-27 16:52:03
Message-ID: AANLkTinYbEbA7gtxzOykeeK_-4dpxGBbQ8kK1J2i3bJM@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2011/3/27 Andrew Dunstan <andrew(at)dunslane(dot)net>:
>
>
> On 03/27/2011 09:42 AM, David Fetter wrote:
>>
>> On Fri, Mar 25, 2011 at 09:12:33PM -0400, Robert Haas wrote:
>>>
>>> As I've said before, I believe that the root cause of this problem is
>>> that using the same syntax for variables and column names is a bad
>>> idea in the first place.  If we used $foo or ?foo or ${foo} or $.foo
>>> or&&foo!!$#? to mean "the parameter called foo", then this would all
>>> be a non-issue.
>>
>> How about psql's :foo syntax?
>>
>>
>
> Surely the time has long gone when we could have made such a choice. And the
> choice was not made in a vacuum.
>

Syntax for named parameters should be consistent with prepared
statement. Is there any comments in standard?

Regards

Pavel

SQL/PSM doesn't use any prefix - but it is little bit different
chapter then prepared statements.

> cheers
>
> andrew
>


From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, David Fetter <david(at)fetter(dot)org>, Robert Haas <robertmhaas(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-27 17:10:44
Message-ID: 20110327171044.GA30031@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sun, Mar 27, 2011 at 06:52:03PM +0200, Pavel Stehule wrote:
> Syntax for named parameters should be consistent with prepared
> statement. Is there any comments in standard?

Well, there's section 4.24 which says:

"In SQL-statements that are executed dynamically, the parameters are
called dynamic parameters (<dynamic parameter specification>s) and are
represented in SQL language by a <question mark> (?)."

which I think we can all agree is not a useful guide.

Have a nice day,

--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Patriotism is when love of your own people comes first; nationalism,
> when hate for people other than your own comes first.
> - Charles de Gaulle


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Martijn van Oosterhout <kleptog(at)svana(dot)org>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, David Fetter <david(at)fetter(dot)org>, Robert Haas <robertmhaas(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-27 17:30:43
Message-ID: AANLkTik8MShL2wNa1RkiLnbnWf219WC+9TZCK8JM4qhP@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2011/3/27 Martijn van Oosterhout <kleptog(at)svana(dot)org>:
> On Sun, Mar 27, 2011 at 06:52:03PM +0200, Pavel Stehule wrote:
>> Syntax for named parameters should be consistent with prepared
>> statement. Is there any comments in standard?
>
> Well, there's section 4.24 which says:
>
> "In SQL-statements that are executed dynamically, the parameters are
> called dynamic parameters (<dynamic parameter specification>s) and are
> represented in SQL language by a <question mark> (?)."
>

ok, so my preferences:

1. no prefix - using a qualified identifiers - it's consistent with
plpgsql and sql/psm
2 :name - it uses a Oracle and it can help to people who migrate from Oracle
3. $name - it is like enhancing of $number parameters - lot of
languages uses this notation

Regards

Pavel Stehule

> which I think we can all agree is not a useful guide.
>
> Have a nice day,
>
> --
> Martijn van Oosterhout   <kleptog(at)svana(dot)org>   http://svana.org/kleptog/
>> Patriotism is when love of your own people comes first; nationalism,
>> when hate for people other than your own comes first.
>>                                       - Charles de Gaulle
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
>
> iD8DBQFNj2+UIB7bNG8LQkwRAjGeAJ99K0Zrp1EM/ike6h+cL5ZEH1Lb2QCfY28L
> 7ETpiWfLtD90ISCVMvEgaSs=
> =YEx2
> -----END PGP SIGNATURE-----
>
>


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-03-28 20:18:16
Message-ID: 1301343496.17107.7.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On lör, 2011-03-26 at 09:41 -0400, Robert Haas wrote:
> You can't be guaranteed that they won't standardize something
> incompatible no matter what we do. We could choose to do it as you've
> proposed and they could then standardize some weird syntax - the => is
> a fairly relevant example of exactly that.

The matter of how to resolve SQL parameter names is already
standardized. See clause on <identifier chain>.


From: Jim Nasby <jim(at)nasby(dot)net>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-05 15:10:24
Message-ID: 2B10116B-0E83-44B2-B6FC-4CAA43421247@nasby.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mar 28, 2011, at 3:18 PM, Peter Eisentraut wrote:
> On lör, 2011-03-26 at 09:41 -0400, Robert Haas wrote:
>> You can't be guaranteed that they won't standardize something
>> incompatible no matter what we do. We could choose to do it as you've
>> proposed and they could then standardize some weird syntax - the => is
>> a fairly relevant example of exactly that.
>
> The matter of how to resolve SQL parameter names is already
> standardized. See clause on <identifier chain>.

Was there a final consensus on this?

FWIW, if we go with using function name, it'd be nice to be allowed to alias that. I don't have a strong opinion between that and using : or $ or whatever. I do feel strongly that we must continue to support existing SQL functions in a reasonable fashion. Having the function blow up on the first invocation is no better than breaking the dump. There should be either a backwards-compatibility mode, or better yet, a way to automatically convert functions to be compatible with the new syntax.
--
Jim C. Nasby, Database Architect jim(at)nasby(dot)net
512.569.9461 (cell) http://jim.nasby.net


From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-05 16:21:20
Message-ID: BANLkTingTngVenpQ1Hsh76BwMROr1gSk0A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Mar 25, 2011 at 10:05 PM, David E. Wheeler <david(at)kineticode(dot)com> wrote:
> On Mar 25, 2011, at 9:12 PM, Robert Haas wrote:
>
>>
>> As I've said before, I believe that the root cause of this problem is
>> that using the same syntax for variables and column names is a bad
>> idea in the first place.  If we used $foo or ?foo or ${foo} or $.foo
>> or &&foo!!$#? to mean "the parameter called foo", then this would all
>> be a non-issue.
>
> Yes *please*. Man that would make maintenance of such functions easier.

+1 on using $foo. Even with the standardization risk I think it's the
best choice. Prefer $"foo" to ${foo} though.

merlin


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-05 17:45:28
Message-ID: 1302025528.27487.1.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On tis, 2011-04-05 at 11:21 -0500, Merlin Moncure wrote:
> +1 on using $foo. Even with the standardization risk I think it's the
> best choice. Prefer $"foo" to ${foo} though.

What standardization risk? The standard has already existed for >10
years and is widely implemented.


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Merlin Moncure <mmoncure(at)gmail(dot)com>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-05 19:05:14
Message-ID: BANLkTimqTAxjegTwXb-AUPoqV_NOmc4Xuw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Apr 5, 2011 at 1:45 PM, Peter Eisentraut <peter_e(at)gmx(dot)net> wrote:
> On tis, 2011-04-05 at 11:21 -0500, Merlin Moncure wrote:
>> +1 on using $foo.  Even with the standardization risk I think it's the
>> best choice. Prefer $"foo" to ${foo} though.
>
> What standardization risk?  The standard has already existed for >10
> years and is widely implemented.

What is the standard, and who is it that has implemented it that way?

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Darren Duncan <darren(at)darrenduncan(dot)net>
To: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-05 19:32:21
Message-ID: 4D9B6E45.5090209@darrenduncan.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Merlin Moncure wrote:
> On Fri, Mar 25, 2011 at 10:05 PM, David E. Wheeler <david(at)kineticode(dot)com> wrote:
>> On Mar 25, 2011, at 9:12 PM, Robert Haas wrote:
>>
>>> As I've said before, I believe that the root cause of this problem is
>>> that using the same syntax for variables and column names is a bad
>>> idea in the first place. If we used $foo or ?foo or ${foo} or $.foo
>>> or &&foo!!$#? to mean "the parameter called foo", then this would all
>>> be a non-issue.
>> Yes *please*. Man that would make maintenance of such functions easier.
>
> +1 on using $foo. Even with the standardization risk I think it's the
> best choice. Prefer $"foo" to ${foo} though.

The "foo" syntax should be orthogonal to everything else and not have anything
specifically to do with parameters. Rather, "foo" anywhere is just a delimited
case-sensitive identifier and can be used anywhere that foo can where the latter
is a case-insensitive identifier.

As for the SQL standard for bind parameters, as I recall they use :foo and so
:"foo" would be the sensitive more general case of that.

-- Darren Duncan


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Merlin Moncure <mmoncure(at)gmail(dot)com>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-05 19:33:00
Message-ID: 1302031980.27487.37.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On tis, 2011-04-05 at 15:05 -0400, Robert Haas wrote:
> On Tue, Apr 5, 2011 at 1:45 PM, Peter Eisentraut <peter_e(at)gmx(dot)net> wrote:
> > On tis, 2011-04-05 at 11:21 -0500, Merlin Moncure wrote:
> >> +1 on using $foo. Even with the standardization risk I think it's the
> >> best choice. Prefer $"foo" to ${foo} though.
> >
> > What standardization risk? The standard has already existed for >10
> > years and is widely implemented.
>
> What is the standard, and who is it that has implemented it that way?

As mentioned earlier, see under clause on <identifier chain>. The
summary is that in

CREATE FUNCTION foo(a int)

you can refer to the parameter as either of

a
foo.a

with some scoping rules to resolve ambiguities with column references.
(These are essentially the same scoping rules that tell you what "a"
refers to when you have multiple tables with an "a" column in a query.)

As far as I can tell, the syntax is implemented, more or less, at least
in Oracle, DB2, MySQL, Firebird, and HSQL. I haven't checked what they
do with the scoping rules, of course.


From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-05 19:45:55
Message-ID: BANLkTimyU-TJeuCbJb-v0_bNtaCzrxi-hw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Apr 5, 2011 at 2:33 PM, Peter Eisentraut <peter_e(at)gmx(dot)net> wrote:
> On tis, 2011-04-05 at 15:05 -0400, Robert Haas wrote:
>> On Tue, Apr 5, 2011 at 1:45 PM, Peter Eisentraut <peter_e(at)gmx(dot)net> wrote:
>> > On tis, 2011-04-05 at 11:21 -0500, Merlin Moncure wrote:
>> >> +1 on using $foo.  Even with the standardization risk I think it's the
>> >> best choice. Prefer $"foo" to ${foo} though.
>> >
>> > What standardization risk?  The standard has already existed for >10
>> > years and is widely implemented.
>>
>> What is the standard, and who is it that has implemented it that way?
>
> As mentioned earlier, see under clause on <identifier chain>.  The
> summary is that in
>
>    CREATE FUNCTION foo(a int)
>
> you can refer to the parameter as either of
>
>    a
>    foo.a
>
> with some scoping rules to resolve ambiguities with column references.
> (These are essentially the same scoping rules that tell you what "a"
> refers to when you have multiple tables with an "a" column in a query.)
>
> As far as I can tell, the syntax is implemented, more or less, at least
> in Oracle, DB2, MySQL, Firebird, and HSQL.  I haven't checked what they
> do with the scoping rules, of course.

Talking about the standards compliance of functions is a bit silly:
our implementation of functions isn't even close to approximating what
looks to be the standard (according to this at least:
http://farrago.sourceforge.net/design/UserDefinedTypesAndRoutines.html)
and there is no point pretending that it is. In practice, database
functions and procedures are 100% vendor incompatible with each other,
and with the standard. I was just talking about $ getting reserved
for some special meaning in the future.

mysql supports psm, which we don't. oracle supports pl/sql, which is
similar to pl/pgsql, but means nothing in terms of postgresql sql
language argument disambiguation afaict. It's our language and we
should be able to extend it.

merlin


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Merlin Moncure <mmoncure(at)gmail(dot)com>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-05 19:46:10
Message-ID: BANLkTik4wgBSXeTySfyNS0OkmYAgAdtryw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2011/4/5 Peter Eisentraut <peter_e(at)gmx(dot)net>:
> On tis, 2011-04-05 at 15:05 -0400, Robert Haas wrote:
>> On Tue, Apr 5, 2011 at 1:45 PM, Peter Eisentraut <peter_e(at)gmx(dot)net> wrote:
>> > On tis, 2011-04-05 at 11:21 -0500, Merlin Moncure wrote:
>> >> +1 on using $foo.  Even with the standardization risk I think it's the
>> >> best choice. Prefer $"foo" to ${foo} though.
>> >
>> > What standardization risk?  The standard has already existed for >10
>> > years and is widely implemented.
>>
>> What is the standard, and who is it that has implemented it that way?
>
> As mentioned earlier, see under clause on <identifier chain>.  The
> summary is that in
>
>    CREATE FUNCTION foo(a int)
>
> you can refer to the parameter as either of
>
>    a
>    foo.a
>
> with some scoping rules to resolve ambiguities with column references.
> (These are essentially the same scoping rules that tell you what "a"
> refers to when you have multiple tables with an "a" column in a query.)

This is a good design. If we disallow a ambiguities, there isn't a
space for bugs. And if anybody needs to accent any parameter, then
there are still $n notation.

There is lot of notation and I don't think so it is necessary to add new one

MySQL, MSSQL uses @, DB2, ANSI SQL no prefix, Oracle and Firebird uses
":", but in different context.

simply - chaos.

There was request for some alias on function name. It could be.
PL/pgSQL knows a #option, so there can be some similar in SQL.

CREATE OR REPLACE FUNCTION longnamefunc(param integer)
RETURNS ... AS $$
#alias longnamefunc ln
SELECT ln.param;
$$

Regards

Pavel Stehule

>
> As far as I can tell, the syntax is implemented, more or less, at least
> in Oracle, DB2, MySQL, Firebird, and HSQL.  I haven't checked what they
> do with the scoping rules, of course.
>
>
>
>


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, Robert Haas <robertmhaas(at)gmail(dot)com>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-05 19:52:15
Message-ID: BANLkTimLirpf=8po4p+HSwcMAUWNuC__7g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2011/4/5 Merlin Moncure <mmoncure(at)gmail(dot)com>:
> On Tue, Apr 5, 2011 at 2:33 PM, Peter Eisentraut <peter_e(at)gmx(dot)net> wrote:
>> On tis, 2011-04-05 at 15:05 -0400, Robert Haas wrote:
>>> On Tue, Apr 5, 2011 at 1:45 PM, Peter Eisentraut <peter_e(at)gmx(dot)net> wrote:
>>> > On tis, 2011-04-05 at 11:21 -0500, Merlin Moncure wrote:
>>> >> +1 on using $foo.  Even with the standardization risk I think it's the
>>> >> best choice. Prefer $"foo" to ${foo} though.
>>> >
>>> > What standardization risk?  The standard has already existed for >10
>>> > years and is widely implemented.
>>>
>>> What is the standard, and who is it that has implemented it that way?
>>
>> As mentioned earlier, see under clause on <identifier chain>.  The
>> summary is that in
>>
>>    CREATE FUNCTION foo(a int)
>>
>> you can refer to the parameter as either of
>>
>>    a
>>    foo.a
>>
>> with some scoping rules to resolve ambiguities with column references.
>> (These are essentially the same scoping rules that tell you what "a"
>> refers to when you have multiple tables with an "a" column in a query.)
>>
>> As far as I can tell, the syntax is implemented, more or less, at least
>> in Oracle, DB2, MySQL, Firebird, and HSQL.  I haven't checked what they
>> do with the scoping rules, of course.
>
> Talking about the standards compliance of functions is a bit silly:
> our implementation of functions isn't even close to approximating what
> looks to be the standard (according to this at least:
> http://farrago.sourceforge.net/design/UserDefinedTypesAndRoutines.html)
> and there is no point pretending that it is.  In practice, database
> functions and procedures are 100% vendor incompatible with each other,
> and with the standard.  I was just talking about $ getting reserved
> for some special meaning in the future.
>
> mysql supports psm, which we don't.

A PSM support for PostgreSQL is almost done. I expect a production
quality for 9.2.

MySQL support own language based on PSM with lot of inspiration in T-SQL.

In MySQL - local variables are clasic, only session variables has a prefix @.

Regards

Pavel

 oracle supports pl/sql, which is
> similar to pl/pgsql, but means nothing in terms of postgresql sql
> language argument disambiguation afaict.  It's our language and we
> should be able to extend it.
>
> merlin
>


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-05 20:47:40
Message-ID: 1302036460.27487.38.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On tis, 2011-04-05 at 14:45 -0500, Merlin Moncure wrote:
> Talking about the standards compliance of functions is a bit silly:
> our implementation of functions isn't even close to approximating what
> looks to be the standard

That doesn't mean it couldn't be better in the future. We shouldn't
take it further away, in any case.

As long as we use LANGUAGE SQL, we are both technically and morally in
standards-space.


From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-05 21:30:58
Message-ID: BANLkTing3ny3ycSCg-nKGuJLs-y22rTUSA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Apr 5, 2011 at 3:47 PM, Peter Eisentraut <peter_e(at)gmx(dot)net> wrote:
> On tis, 2011-04-05 at 14:45 -0500, Merlin Moncure wrote:
>> Talking about the standards compliance of functions is a bit silly:
>> our implementation of functions isn't even close to approximating what
>> looks to be the standard
>
> That doesn't mean it couldn't be better in the future.  We shouldn't
> take it further away, in any case.
>
> As long as we use LANGUAGE SQL, we are both technically and morally in
> standards-space.

sql standard functions are psm routines aiui. Are you making the case
that 'language sql' in postgresql could or should in fact be psm at
some point in the future? I say that's not the case -- our 'language
sql' is not psm.

That said, if you well and truly stated that it was project objective
to allow psm constructions in 'language sql', and you could figure out
a way to do that without breaking current sql code, I would have to
say i'm coming around to your point of view. Either way, our $N
notation is already non-standard and highly in use - what's the big
deal about making it more useful?

merlin


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, Robert Haas <robertmhaas(at)gmail(dot)com>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-05 21:52:58
Message-ID: 4D9B8F3A.5070108@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 04/05/2011 03:45 PM, Merlin Moncure wrote:
> Talking about the standards compliance of functions is a bit silly:
> our implementation of functions isn't even close to approximating what
> looks to be the standard (according to this at least:
> http://farrago.sourceforge.net/design/UserDefinedTypesAndRoutines.html)
> and there is no point pretending that it is. In practice, database
> functions and procedures are 100% vendor incompatible with each other,
> and with the standard. I was just talking about $ getting reserved
> for some special meaning in the future.
>
> mysql supports psm, which we don't. oracle supports pl/sql, which is
> similar to pl/pgsql, but means nothing in terms of postgresql sql
> language argument disambiguation afaict. It's our language and we
> should be able to extend it.
>
>

That doesn't mean we should arbitrarily break compatibility with pl/sql,
nor that we should feel free to add on warts such as $varname that are
completely at odds with the style of the rest of the language. That
doesn't do anything except produce a mess.

cheers

andrew


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Merlin Moncure <mmoncure(at)gmail(dot)com>, Peter Eisentraut <peter_e(at)gmx(dot)net>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-05 22:36:19
Message-ID: BANLkTik4JohGR7KqnM-WnfN6AcSV4Efnrg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Apr 5, 2011 at 5:52 PM, Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
> That doesn't mean we should arbitrarily break compatibility with pl/sql, nor
> that we should feel free to add on warts such as $varname that are
> completely at odds with the style of the rest of the language. That doesn't
> do anything except produce a mess.

Well, what it does is avoid breaking compatibility with previous
versions of PostgreSQL. I think that actually does have some value.
Otherwise, we'd be folding to upper-case by default.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Merlin Moncure <mmoncure(at)gmail(dot)com>, Peter Eisentraut <peter_e(at)gmx(dot)net>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 01:58:50
Message-ID: 17818.1302227930@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Tue, Apr 5, 2011 at 5:52 PM, Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
>> That doesn't mean we should arbitrarily break compatibility with pl/sql, nor
>> that we should feel free to add on warts such as $varname that are
>> completely at odds with the style of the rest of the language. That doesn't
>> do anything except produce a mess.

> Well, what it does is avoid breaking compatibility with previous
> versions of PostgreSQL. I think that actually does have some value.
> Otherwise, we'd be folding to upper-case by default.

Well, if we're going to consider 100% backwards compatibility a "must",
then we should just stick with what the submitted patch does, ie,
unqualified names are matched first to query columns, and to parameters
only if there's no column match. This is also per spec if I interpreted
Peter's comments correctly. The whole thread started because I
suggested that throwing an error for ambiguous cases might be a better
design in the long run, but apparently long term ease of code
maintenance is far down our list of priorities ...

regards, tom lane


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, Merlin Moncure <mmoncure(at)gmail(dot)com>, Peter Eisentraut <peter_e(at)gmx(dot)net>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 03:06:14
Message-ID: 51431AA9-E413-49A2-B63A-5D776A09EE10@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Apr 7, 2011, at 6:58 PM, Tom Lane wrote:

> Well, if we're going to consider 100% backwards compatibility a "must",
> then we should just stick with what the submitted patch does, ie,
> unqualified names are matched first to query columns, and to parameters
> only if there's no column match. This is also per spec if I interpreted
> Peter's comments correctly. The whole thread started because I
> suggested that throwing an error for ambiguous cases might be a better
> design in the long run, but apparently long term ease of code
> maintenance is far down our list of priorities ...

I agree with you that it should throw an error, at least optionally. Could we not recycle the settings that control this for plpgsql functions?

Best,

David


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Merlin Moncure <mmoncure(at)gmail(dot)com>, Peter Eisentraut <peter_e(at)gmx(dot)net>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 03:30:50
Message-ID: BANLkTi=vdFDSL9vAxgexGa373WypEwjRQQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Apr 7, 2011 at 9:58 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>> On Tue, Apr 5, 2011 at 5:52 PM, Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
>>> That doesn't mean we should arbitrarily break compatibility with pl/sql, nor
>>> that we should feel free to add on warts such as $varname that are
>>> completely at odds with the style of the rest of the language. That doesn't
>>> do anything except produce a mess.
>
>> Well, what it does is avoid breaking compatibility with previous
>> versions of PostgreSQL.  I think that actually does have some value.
>> Otherwise, we'd be folding to upper-case by default.
>
> Well, if we're going to consider 100% backwards compatibility a "must",
> then we should just stick with what the submitted patch does, ie,
> unqualified names are matched first to query columns, and to parameters
> only if there's no column match.  This is also per spec if I interpreted
> Peter's comments correctly.  The whole thread started because I
> suggested that throwing an error for ambiguous cases might be a better
> design in the long run, but apparently long term ease of code
> maintenance is far down our list of priorities ...

Not really. But if you're going to shout down my proposal because
it's not in the spec, then it's a bit hard to see how you can argue
that we should implement your non-spec-compliant behavior instead,
especially at the cost of a painful backward compatibility break. I
actually am 100% in agreement with you that allowing ambiguous
references to resolve either way is a recipe for bugs, bugs, and more
bugs. But breaking people's code is not a better answer. We still
have people on 8.2 because the pain of upgrading to 8.3 is more than
they can bear, and how many releases have we spent trying to get
standard_conforming_strings worked out? I admit this probably
wouldn't be as bad, but we've managed to put out several releases in a
row now that are relatively painless to upgrade between, and I think
that's a trend we should try to keep going.

I am halfway tempted to say that we need to invent our own procedural
language that is designed not for compatibility with the SQL standard
or Oracle, but for non-crappiness.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Darren Duncan <darren(at)darrenduncan(dot)net>
To: Robert Haas <robertmhaas(at)gmail(dot)com>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 03:41:49
Message-ID: 4D9E83FD.9090008@darrenduncan.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas wrote:
> I am halfway tempted to say that we need to invent our own procedural
> language that is designed not for compatibility with the SQL standard
> or Oracle, but for non-crappiness.

I'm way ahead of you on that one. -- Darren Duncan


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Merlin Moncure <mmoncure(at)gmail(dot)com>, Peter Eisentraut <peter_e(at)gmx(dot)net>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 14:50:19
Message-ID: 4D9F20AB.4060300@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 04/07/2011 09:58 PM, Tom Lane wrote:
> Robert Haas<robertmhaas(at)gmail(dot)com> writes:
>> On Tue, Apr 5, 2011 at 5:52 PM, Andrew Dunstan<andrew(at)dunslane(dot)net> wrote:
>>> That doesn't mean we should arbitrarily break compatibility with pl/sql, nor
>>> that we should feel free to add on warts such as $varname that are
>>> completely at odds with the style of the rest of the language. That doesn't
>>> do anything except produce a mess.
>> Well, what it does is avoid breaking compatibility with previous
>> versions of PostgreSQL. I think that actually does have some value.
>> Otherwise, we'd be folding to upper-case by default.
> Well, if we're going to consider 100% backwards compatibility a "must",
> then we should just stick with what the submitted patch does, ie,
> unqualified names are matched first to query columns, and to parameters
> only if there's no column match. This is also per spec if I interpreted
> Peter's comments correctly. The whole thread started because I
> suggested that throwing an error for ambiguous cases might be a better
> design in the long run, but apparently long term ease of code
> maintenance is far down our list of priorities ...
>
>

I think the discussion went off into the weeds somewhat, and I'm guilty
of responding to suggestions that don't refer to the original subject.

For SQL language functions, I think you're right. The only caveat I have
is that if your function name is very long, having to use it as a
disambiguating qualifier can be a bit ugly.

cheers

andrew


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Robert Haas <robertmhaas(at)gmail(dot)com>, Merlin Moncure <mmoncure(at)gmail(dot)com>, Peter Eisentraut <peter_e(at)gmx(dot)net>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 14:53:27
Message-ID: BANLkTik7nQodDidaY-novQKxRUv=t3nUQA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hello

>>
>> Well, if we're going to consider 100% backwards compatibility a "must",
>> then we should just stick with what the submitted patch does, ie,
>> unqualified names are matched first to query columns, and to parameters
>> only if there's no column match.  This is also per spec if I interpreted
>> Peter's comments correctly.  The whole thread started because I
>> suggested that throwing an error for ambiguous cases might be a better
>> design in the long run, but apparently long term ease of code
>> maintenance is far down our list of priorities ...
>>
>>
>
> I think the discussion went off into the weeds somewhat, and I'm guilty of
> responding to suggestions that don't refer to the original subject.
>
> For SQL language functions, I think you're right. The only caveat I have is
> that if your function name is very long, having to use it as a
> disambiguating qualifier can be a bit ugly.

same mechanism works well in plpgsql and nobody requested a some
special shortcut.

Regards

Pavel

>
> cheers
>
> andrew
>


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Robert Haas <robertmhaas(at)gmail(dot)com>, Merlin Moncure <mmoncure(at)gmail(dot)com>, Peter Eisentraut <peter_e(at)gmx(dot)net>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 15:05:36
Message-ID: 4D9F2440.80200@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 04/08/2011 10:53 AM, Pavel Stehule wrote:
>> For SQL language functions, I think you're right. The only caveat I have is
>> that if your function name is very long, having to use it as a
>> disambiguating qualifier can be a bit ugly.
> same mechanism works well in plpgsql and nobody requested a some
> special shortcut.
>

I get annoyed by it there too, that's why I mentioned it :-)

cheers

andrew


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Merlin Moncure <mmoncure(at)gmail(dot)com>, Peter Eisentraut <peter_e(at)gmx(dot)net>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 15:05:58
Message-ID: BANLkTinRu4QUfSt6Vpc67WwB2mdUsHbd-A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Apr 8, 2011 at 10:53 AM, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
> same mechanism works well in plpgsql and nobody requested a some
> special shortcut.

I did. That mechanism sucks. But I think we're committed to doing
what the standard and/or Oracle do, so oh well.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-hackers(at)postgresql(dot)org
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Robert Haas <robertmhaas(at)gmail(dot)com>, Merlin Moncure <mmoncure(at)gmail(dot)com>, Peter Eisentraut <peter_e(at)gmx(dot)net>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 15:09:50
Message-ID: 201104081709.50950.andres@anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Friday, April 08, 2011 04:53:27 PM Pavel Stehule wrote:
> same mechanism works well in plpgsql and nobody requested a some
> special shortcut.
Well, for one it sucks there as well. For another it has been introduced for
quite some time and most people have introduced naming like p_param or v_param
for parameternames.

That has not been the case for sql functions. So I find it way much more
painfull there...

Andres


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Merlin Moncure <mmoncure(at)gmail(dot)com>, Peter Eisentraut <peter_e(at)gmx(dot)net>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 15:51:43
Message-ID: 7B05DFEE-E183-4248-BEB9-90F82A23E9A8@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Apr 8, 2011, at 8:05 AM, Robert Haas wrote:

>> same mechanism works well in plpgsql and nobody requested a some
>> special shortcut.
>
> I did. That mechanism sucks. But I think we're committed to doing
> what the standard and/or Oracle do, so oh well.

I think I've worked around that in PL/pgSQL using ALIAS…

David


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Merlin Moncure <mmoncure(at)gmail(dot)com>, Peter Eisentraut <peter_e(at)gmx(dot)net>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 15:57:57
Message-ID: BANLkTi=Wmv7=Gg-FB0LO-p9GcbCg2RYWAg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Apr 8, 2011 at 11:51 AM, David E. Wheeler <david(at)kineticode(dot)com> wrote:
> On Apr 8, 2011, at 8:05 AM, Robert Haas wrote:
>
>>> same mechanism works well in plpgsql and nobody requested a some
>>> special shortcut.
>>
>> I did.  That mechanism sucks.  But I think we're committed to doing
>> what the standard and/or Oracle do, so oh well.
>
> I think I've worked around that in PL/pgSQL using ALIAS…

I've worked around it, too, using various techniques. That doesn't
mean it doesn't suck.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, Peter Eisentraut <peter_e(at)gmx(dot)net>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 16:06:09
Message-ID: BANLkTinb62xx+sNT9hseksPyKQSHJsM-Xw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Apr 7, 2011 at 8:58 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>> On Tue, Apr 5, 2011 at 5:52 PM, Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
>>> That doesn't mean we should arbitrarily break compatibility with pl/sql, nor
>>> that we should feel free to add on warts such as $varname that are
>>> completely at odds with the style of the rest of the language. That doesn't
>>> do anything except produce a mess.
>
>> Well, what it does is avoid breaking compatibility with previous
>> versions of PostgreSQL.  I think that actually does have some value.
>> Otherwise, we'd be folding to upper-case by default.
>
> Well, if we're going to consider 100% backwards compatibility a "must",
> then we should just stick with what the submitted patch does, ie,
> unqualified names are matched first to query columns, and to parameters
> only if there's no column match.  This is also per spec if I interpreted
> Peter's comments correctly.  The whole thread started because I
> suggested that throwing an error for ambiguous cases might be a better
> design in the long run, but apparently long term ease of code
> maintenance is far down our list of priorities ...

+1, as long as you are 100.0% sure this is not going to break any
existing code. For example, what happens if the argument is named the
same as a table?

merlin


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, Peter Eisentraut <peter_e(at)gmx(dot)net>, "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Matthew Draper <matthew(at)trebex(dot)net>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 16:11:37
Message-ID: 13699.1302279097@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Merlin Moncure <mmoncure(at)gmail(dot)com> writes:
> On Thu, Apr 7, 2011 at 8:58 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Well, if we're going to consider 100% backwards compatibility a "must",
>> then we should just stick with what the submitted patch does, ie,
>> unqualified names are matched first to query columns, and to parameters
>> only if there's no column match. This is also per spec if I interpreted
>> Peter's comments correctly. The whole thread started because I
>> suggested that throwing an error for ambiguous cases might be a better
>> design in the long run, but apparently long term ease of code
>> maintenance is far down our list of priorities ...

> +1, as long as you are 100.0% sure this is not going to break any
> existing code. For example, what happens if the argument is named the
> same as a table?

I was a bit sloppy in my statement above --- what the code is actually
doing (or should be doing) is matching to parameters only after the core
parser fails to find any match. So "unqualified reference to whole-row"
would take precedence too.

regards, tom lane


From: Josh Berkus <josh(at)agliodbs(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 19:56:49
Message-ID: 4D9F6881.6060208@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


> But breaking people's code is not a better answer. We still
> have people on 8.2 because the pain of upgrading to 8.3 is more than
> they can bear, and how many releases have we spent trying to get
> standard_conforming_strings worked out? I admit this probably
> wouldn't be as bad, but we've managed to put out several releases in a
> row now that are relatively painless to upgrade between, and I think
> that's a trend we should try to keep going.

I guess I'm not understanding the backwards compatibility problem. I've
looked up the thread, and I still don't see a real-world issue. If we
(by default) throw an error on ambiguity, and have GUC to turn that off
(in which case, it resolves column-first), I really don't see what
problem anyone could have upgrading.

Can you explain it to me?

--
Josh Berkus
PostgreSQL Experts Inc.
http://pgexperts.com


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Josh Berkus <josh(at)agliodbs(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 20:10:05
Message-ID: BANLkTintgVG=91PQBZqQjp2GPtccBCQgxA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Apr 8, 2011 at 3:56 PM, Josh Berkus <josh(at)agliodbs(dot)com> wrote:
>> But breaking people's code is not a better answer.  We still
>> have people on 8.2 because the pain of upgrading to 8.3 is more than
>> they can bear, and how many releases have we spent trying to get
>> standard_conforming_strings worked out?  I admit this probably
>> wouldn't be as bad, but we've managed to put out several releases in a
>> row now that are relatively painless to upgrade between, and I think
>> that's a trend we should try to keep going.
>
> I guess I'm not understanding the backwards compatibility problem.  I've
> looked up the thread, and I still don't see a real-world issue.  If we
> (by default) throw an error on ambiguity, and have GUC to turn that off
> (in which case, it resolves column-first), I really don't see what
> problem anyone could have upgrading.
>
> Can you explain it to me?

Consider:

rhaas=# CREATE TABLE developer (id serial primary key, name text not null);
NOTICE: CREATE TABLE will create implicit sequence "developer_id_seq"
for serial column "developer.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"developer_pkey" for table "developer"
CREATE TABLE
rhaas=# insert into developer (name) values ('Tom'), ('Bruce');
INSERT 0 2
rhaas=# CREATE OR REPLACE FUNCTION developer_lookup(id integer)
RETURNS text AS $$SELECT name FROM developer WHERE id = $1$$ LANGUAGE
sql STABLE;
CREATE FUNCTION
rhaas=# SELECT developer_lookup(1);
developer_lookup
------------------
Tom
(1 row)

Now, when this person attempts to recreate this function on a
hypothetical version of PostgreSQL that thinks "id" is ambiguous, it
doesn't work.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Josh Berkus <josh(at)agliodbs(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 20:32:58
Message-ID: 4D9F70FA.1000102@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


> Now, when this person attempts to recreate this function on a
> hypothetical version of PostgreSQL that thinks "id" is ambiguous, it
> doesn't work.

Hence the GUC. Where's the issue?

--
Josh Berkus
PostgreSQL Experts Inc.
http://pgexperts.com


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Josh Berkus <josh(at)agliodbs(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 21:05:10
Message-ID: BANLkTiny00MRiArnbWzJ9+7x_k0ytzsk-Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Apr 8, 2011 at 4:32 PM, Josh Berkus <josh(at)agliodbs(dot)com> wrote:
>> Now, when this person attempts to recreate this function on a
>> hypothetical version of PostgreSQL that thinks "id" is ambiguous, it
>> doesn't work.
>
> Hence the GUC.   Where's the issue?

Behavior-changing GUCs for this kind of thing cause a lot of problems.
If you need one GUC setting for your application to work, and the
extension you have installed needs the other setting, you're screwed.
In the worst case, if a security-definer function is involved, you can
create a security hole, for example by convincing the system that id =
$1 is intended to mean $1 = $1, or some such. You can of course
attach the GUC settings to each individual function, but that doesn't
really work either unless you do it for every function in the system.
The fundamental problem here is that GUCs are dynamically scoped,
while this problem is lexically scoped.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-08 23:30:26
Message-ID: 21080.1302305426@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Fri, Apr 8, 2011 at 4:32 PM, Josh Berkus <josh(at)agliodbs(dot)com> wrote:
>> Hence the GUC. Where's the issue?

> Behavior-changing GUCs for this kind of thing cause a lot of problems.
> If you need one GUC setting for your application to work, and the
> extension you have installed needs the other setting, you're screwed.
> In the worst case, if a security-definer function is involved, you can
> create a security hole, for example by convincing the system that id =
> $1 is intended to mean $1 = $1, or some such. You can of course
> attach the GUC settings to each individual function, but that doesn't
> really work either unless you do it for every function in the system.
> The fundamental problem here is that GUCs are dynamically scoped,
> while this problem is lexically scoped.

Yeah. In the plpgsql case, we did make provisions to control the
behavior per-function. In principle we could do the same for SQL
functions, but it'd be rather a PITA I think. (In particular, the "easy
way out" of attaching SET clauses to the functions would be a bad idea
because it would defeat inlining.)

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: Robert Haas <robertmhaas(at)gmail(dot)com>, Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-09 03:19:45
Message-ID: BANLkTi=3D3KGhZGWBk3L5e1k17CwKxAmOg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2011/4/9 Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>> On Fri, Apr 8, 2011 at 4:32 PM, Josh Berkus <josh(at)agliodbs(dot)com> wrote:
>>> Hence the GUC.   Where's the issue?
>
>> Behavior-changing GUCs for this kind of thing cause a lot of problems.
>>  If you need one GUC setting for your application to work, and the
>> extension you have installed needs the other setting, you're screwed.
>> In the worst case, if a security-definer function is involved, you can
>> create a security hole, for example by convincing the system that id =
>> $1 is intended to mean $1 = $1, or some such.  You can of course
>> attach the GUC settings to each individual function, but that doesn't
>> really work either unless you do it for every function in the system.
>> The fundamental problem here is that GUCs are dynamically scoped,
>> while this problem is lexically scoped.
>
> Yeah.  In the plpgsql case, we did make provisions to control the
> behavior per-function.  In principle we could do the same for SQL
> functions, but it'd be rather a PITA I think.  (In particular, the "easy
> way out" of attaching SET clauses to the functions would be a bad idea
> because it would defeat inlining.)

what about a new language like SQLc? - like SQL compatibility.
pg_upgrade can move old code into this compatibility language when
detect some posible problems.

Pavel

>
>                        regards, tom lane
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>


From: Dimitri Fontaine <dimitri(at)2ndQuadrant(dot)fr>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-14 18:37:47
Message-ID: m2zknsy8zo.fsf@2ndQuadrant.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> rhaas=# CREATE OR REPLACE FUNCTION developer_lookup(id integer)
> RETURNS text AS $$SELECT name FROM developer WHERE id = $1$$ LANGUAGE
> sql STABLE;
>
> Now, when this person attempts to recreate this function on a
> hypothetical version of PostgreSQL that thinks "id" is ambiguous, it
> doesn't work.

Unless we make it so that no such version ever exists. Meaning that the
code works fine as is or using WHERE id = developer_lookup.id. AS id
can't ever be the parameter in this case, you're just fine.

Bearing in mind that $1 etc shortcuts still are available, I don't
really see this qualification of parameter names with function names so
big a problem that we should find a way to avoid it and risk breaking
compatibility.

Don't forget that any ambiguity here will mean *huge* migration costs.

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


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>
Cc: Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-14 18:54:39
Message-ID: BANLkTin1K_NXfa2ZK96i=5ZX5XEerOmkTg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Apr 14, 2011 at 11:37 AM, Dimitri Fontaine
<dimitri(at)2ndquadrant(dot)fr> wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>> rhaas=# CREATE OR REPLACE FUNCTION developer_lookup(id integer)
>> RETURNS text AS $$SELECT name FROM developer WHERE id = $1$$ LANGUAGE
>> sql STABLE;
>>
>> Now, when this person attempts to recreate this function on a
>> hypothetical version of PostgreSQL that thinks "id" is ambiguous, it
>> doesn't work.
>
> Unless we make it so that no such version ever exists.  Meaning that the
> code works fine as is or using WHERE id = developer_lookup.id.  AS id
> can't ever be the parameter in this case, you're just fine.
>
> Bearing in mind that $1 etc shortcuts still are available, I don't
> really see this qualification of parameter names with function names so
> big a problem that we should find a way to avoid it and risk breaking
> compatibility.
>
> Don't forget that any ambiguity here will mean *huge* migration costs.

If I'm reading your email correctly, we're in agreement.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>, Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-14 20:55:46
Message-ID: 20402.1302814546@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Thu, Apr 14, 2011 at 11:37 AM, Dimitri Fontaine
> <dimitri(at)2ndquadrant(dot)fr> wrote:
>> Unless we make it so that no such version ever exists. Meaning that the
>> code works fine as is or using WHERE id = developer_lookup.id. AS id
>> can't ever be the parameter in this case, you're just fine.
>>
>> Bearing in mind that $1 etc shortcuts still are available, I don't
>> really see this qualification of parameter names with function names so
>> big a problem that we should find a way to avoid it and risk breaking
>> compatibility.
>>
>> Don't forget that any ambiguity here will mean *huge* migration costs.

> If I'm reading your email correctly, we're in agreement.

Hmm, what I read Dimitri to be proposing is that we *require* parameter
names to be qualified with the function name. I don't recall hearing
that before. It would solve the problem perhaps, but I think the moans
and groans will be numerous.

regards, tom lane


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>, Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-14 21:10:11
Message-ID: BANLkTikfe7EidC1xMYkQLfLqnxNO6FsM0g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Apr 14, 2011 at 1:55 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>> On Thu, Apr 14, 2011 at 11:37 AM, Dimitri Fontaine
>> <dimitri(at)2ndquadrant(dot)fr> wrote:
>>> Unless we make it so that no such version ever exists.  Meaning that the
>>> code works fine as is or using WHERE id = developer_lookup.id.  AS id
>>> can't ever be the parameter in this case, you're just fine.
>>>
>>> Bearing in mind that $1 etc shortcuts still are available, I don't
>>> really see this qualification of parameter names with function names so
>>> big a problem that we should find a way to avoid it and risk breaking
>>> compatibility.
>>>
>>> Don't forget that any ambiguity here will mean *huge* migration costs.
>
>> If I'm reading your email correctly, we're in agreement.
>
> Hmm, what I read Dimitri to be proposing is that we *require* parameter
> names to be qualified with the function name.  I don't recall hearing
> that before.  It would solve the problem perhaps, but I think the moans
> and groans will be numerous.

So far the most promising proposal I've seen seems to be to let id
mean the parameter called id only when it can't refer to anything in
the query.

Tabula raza, I'd prefer your proposal to make any ambiguity an error,
but it's not worth the breakage. I'd be fine with having a way to
explicitly request that behavior though, a la Perl's "use strict".

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>, Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-14 21:16:24
Message-ID: 20795.1302815784@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Thu, Apr 14, 2011 at 1:55 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Hmm, what I read Dimitri to be proposing is that we *require* parameter
>> names to be qualified with the function name. I don't recall hearing
>> that before. It would solve the problem perhaps, but I think the moans
>> and groans will be numerous.

> So far the most promising proposal I've seen seems to be to let id
> mean the parameter called id only when it can't refer to anything in
> the query.

> Tabula raza, I'd prefer your proposal to make any ambiguity an error,
> but it's not worth the breakage.

Yeah, I've come round to that position too. I think allowing parameter
names to be checked only after query names is probably the best answer.

> I'd be fine with having a way to
> explicitly request that behavior though, a la Perl's "use strict".

This is possible but it's not clear it's worth the work.

regards, tom lane


From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: "Robert Haas" <robertmhaas(at)gmail(dot)com>, "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Dimitri Fontaine" <dimitri(at)2ndquadrant(dot)fr>, "Josh Berkus" <josh(at)agliodbs(dot)com>, <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-14 21:20:32
Message-ID: 4DA71ED0020000250003C849@gw.wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:

>> So far the most promising proposal I've seen seems to be to let
>> id mean the parameter called id only when it can't refer to
>> anything in the query.

> Yeah, I've come round to that position too. I think allowing
> parameter names to be checked only after query names is probably
> the best answer.

+1

That seems the most useful and least surprising approach to me.

-Kevin


From: Jim Nasby <jim(at)nasby(dot)net>
To: Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>
Cc: "Robert Haas" <robertmhaas(at)gmail(dot)com>, "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "Dimitri Fontaine" <dimitri(at)2ndquadrant(dot)fr>, "Josh Berkus" <josh(at)agliodbs(dot)com>, <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-15 02:07:19
Message-ID: DBAEDF2B-A3E9-45A3-BD0C-5E94F0982B05@nasby.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Apr 14, 2011, at 4:20 PM, Kevin Grittner wrote:
> Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>
>>> So far the most promising proposal I've seen seems to be to let
>>> id mean the parameter called id only when it can't refer to
>>> anything in the query.
>
>> Yeah, I've come round to that position too. I think allowing
>> parameter names to be checked only after query names is probably
>> the best answer.
>
> +1
>
> That seems the most useful and least surprising approach to me.

As part of this, can we also allow specifying an alias for the function name? That would make it far less onerous to disambiguate parameters. Unfortunately we obviously couldn't use AS as the keyword for this alias; maybe we could use ALIAS instead? IE:

CREATE FUNCTION function_with_really_really_descriptive_name (
some_parameter int
) RETURNS int LANGUAGE SQL ALIAS fwrrdn AS $$
SELECT fwrrdn.some_parameter
$$;
--
Jim C. Nasby, Database Architect jim(at)nasby(dot)net
512.569.9461 (cell) http://jim.nasby.net


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Jim Nasby <jim(at)nasby(dot)net>
Cc: Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>, Robert Haas <robertmhaas(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>, Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-15 02:56:10
Message-ID: BANLkTinvJW8AR0TiZuPxRQ73_YZ-=9sb3w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2011/4/15 Jim Nasby <jim(at)nasby(dot)net>:
> On Apr 14, 2011, at 4:20 PM, Kevin Grittner wrote:
>> Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>>> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>>
>>>> So far the most promising proposal I've seen seems to be to let
>>>> id mean the parameter called id only when it can't refer to
>>>> anything in the query.
>>
>>> Yeah, I've come round to that position too.  I think allowing
>>> parameter names to be checked only after query names is probably
>>> the best answer.
>>
>> +1
>>
>> That seems the most useful and least surprising approach to me.
>
> As part of this, can we also allow specifying an alias for the function name? That would make it far less onerous to disambiguate parameters. Unfortunately we obviously couldn't use AS as the keyword for this alias; maybe we could use ALIAS instead? IE:
>
> CREATE FUNCTION function_with_really_really_descriptive_name (
>  some_parameter int
> ) RETURNS int LANGUAGE SQL ALIAS fwrrdn AS $$
>        SELECT fwrrdn.some_parameter
> $$;
> --

I see this can be problem for other languages - mainly for PLpgSQL.
There should be aliases supported too. And this small feature can be
terible when somebody will try to port your code to other platforms.
Personally I am thinking, so it isn't necessary

-1

Regards

Pavel Stehule

> Jim C. Nasby, Database Architect                   jim(at)nasby(dot)net
> 512.569.9461 (cell)                         http://jim.nasby.net
>
>
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>


From: Dimitri Fontaine <dimitri(at)2ndQuadrant(dot)fr>
To: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
Cc: "Robert Haas" <robertmhaas(at)gmail(dot)com>, "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "Dimitri Fontaine" <dimitri(at)2ndquadrant(dot)fr>, "Josh Berkus" <josh(at)agliodbs(dot)com>, <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-15 08:32:52
Message-ID: m27hah6op5.fsf@hi-media.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


(oops this mail never reached out, it seems, resending)

"Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov> writes:
> Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Yeah, I've come round to that position too. I think allowing
>> parameter names to be checked only after query names is probably
>> the best answer.
>
> That seems the most useful and least surprising approach to me.

Allow me to disagree. I don't want to recheck all my SQL functions
whenever I ALTER TABLE … ADD|DROP|RENAME COLUMN …;

We don't even have a nice way to list functions dependencies towards
objects that are part of their body to help me out here, or do we?

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


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Jim Nasby <jim(at)nasby(dot)net>, Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>, Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-18 12:33:06
Message-ID: BANLkTin4RK+eL3bV8wBsUj3O=VOY2hB=0Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Apr 14, 2011 at 10:56 PM, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
> 2011/4/15 Jim Nasby <jim(at)nasby(dot)net>:
>> On Apr 14, 2011, at 4:20 PM, Kevin Grittner wrote:
>>> Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>>>> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>>>
>>>>> So far the most promising proposal I've seen seems to be to let
>>>>> id mean the parameter called id only when it can't refer to
>>>>> anything in the query.
>>>
>>>> Yeah, I've come round to that position too.  I think allowing
>>>> parameter names to be checked only after query names is probably
>>>> the best answer.
>>>
>>> +1
>>>
>>> That seems the most useful and least surprising approach to me.
>>
>> As part of this, can we also allow specifying an alias for the function name? That would make it far less onerous to disambiguate parameters. Unfortunately we obviously couldn't use AS as the keyword for this alias; maybe we could use ALIAS instead? IE:
>>
>> CREATE FUNCTION function_with_really_really_descriptive_name (
>>  some_parameter int
>> ) RETURNS int LANGUAGE SQL ALIAS fwrrdn AS $$
>>        SELECT fwrrdn.some_parameter
>> $$;
>> --
>
> I see this can be problem for other languages - mainly for PLpgSQL.
> There should be aliases supported too. And this small feature can be
> terible when somebody will try to port your code to other platforms.
> Personally I am thinking, so it isn't necessary
>
> -1

I don't much like Jim's syntax suggestion (the alias really ought to
be declared within the function body, I think, not added to the CREATE
FUNCTION statement) but I don't necessarily think it's a bad idea.
What would be even better, in my view, is having a short alias that is
defined by default, but all previous proposals in this vein have been
shot down by Tom and Andrew. As a practical matter, though, I think
what Jim is talking about speaks to a real need - people want to make
SQL function names long and descriptive, but they do NOT want to spell
out that long function name 16 times inside the function body.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Jim Nasby <jim(at)nasby(dot)net>, Kevin Grittner <kevin(dot)grittner(at)wicourts(dot)gov>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>, Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-18 16:33:13
Message-ID: 1303144262-sup-8577@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Excerpts from Robert Haas's message of lun abr 18 09:33:06 -0300 2011:

> I don't much like Jim's syntax suggestion (the alias really ought to
> be declared within the function body, I think, not added to the CREATE
> FUNCTION statement) but I don't necessarily think it's a bad idea.
> What would be even better, in my view, is having a short alias that is
> defined by default, but all previous proposals in this vein have been
> shot down by Tom and Andrew. As a practical matter, though, I think
> what Jim is talking about speaks to a real need - people want to make
> SQL function names long and descriptive, but they do NOT want to spell
> out that long function name 16 times inside the function body.

plpgsql has the #option thing in functions; why can't we have something
similar in SQL functions?

CREATE FUNCTION function_with_really_really_descriptive_name (
 some_parameter int
) RETURNS int LANGUAGE SQL AS $$
#option function_alias fwrrdn
       SELECT fwrrdn.some_parameter
$$;

Not necessarily that exact syntax.

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


From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Jim Nasby <jim(at)nasby(dot)net>, Kevin Grittner <kevin(dot)grittner(at)wicourts(dot)gov>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Dimitri Fontaine <dimitri(at)2ndquadrant(dot)fr>, Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Date: 2011-04-18 16:51:12
Message-ID: BANLkTimG8Aj-2LzCgyC0v-rB8zNChkYG+Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Apr 18, 2011 at 11:33 AM, Alvaro Herrera
<alvherre(at)commandprompt(dot)com> wrote:
> Excerpts from Robert Haas's message of lun abr 18 09:33:06 -0300 2011:
>
>> I don't much like Jim's syntax suggestion (the alias really ought to
>> be declared within the function body, I think, not added to the CREATE
>> FUNCTION statement) but I don't necessarily think it's a bad idea.
>> What would be even better, in my view, is having a short alias that is
>> defined by default, but all previous proposals in this vein have been
>> shot down by Tom and Andrew.  As a practical matter, though, I think
>> what Jim is talking about speaks to a real need - people want to make
>> SQL function names long and descriptive, but they do NOT want to spell
>> out that long function name 16 times inside the function body.
>
> plpgsql has the #option thing in functions; why can't we have something
> similar in SQL functions?
>
>  CREATE FUNCTION function_with_really_really_descriptive_name (
>   some_parameter int
>  ) RETURNS int LANGUAGE SQL AS $$
>     #option function_alias fwrrdn
>         SELECT fwrrdn.some_parameter
>  $$;
>
> Not necessarily that exact syntax.

If we are rejecting $foo on grounds of deviating from sql standard,
shouldn't this be rejected on the same grounds? There is no such
syntax in sql/psm.

merlin