plpgsql: another new reserved word

Lists: pgsql-hackers
From: "Marko Kreen" <markokr(at)gmail(dot)com>
To: "Postgres Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: plpgsql: another new reserved word
Date: 2007-11-09 23:47:06
Message-ID: e51f66da0711091547i723afe1dx7589a780b67b2154@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

I stumbled on another gotcha in 8.3's plpgsql:

create or replace function foobar() returns text as $$
declare
foobar text;
begin return 'ok'; end;
$$ language plpgsql;

Results in error:

ERROR: syntax error at or near "foobar"
LINE 3: foobar text;
^

If this is intentional, then could the error message be made better?

--
marko


From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: "Marko Kreen" <markokr(at)gmail(dot)com>
Cc: "Postgres Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpgsql: another new reserved word
Date: 2007-11-09 23:59:08
Message-ID: 162867790711091559p464f6594q2aa113055890be27@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 10/11/2007, Marko Kreen <markokr(at)gmail(dot)com> wrote:
> I stumbled on another gotcha in 8.3's plpgsql:
>
> create or replace function foobar() returns text as $$
> declare
> foobar text;
> begin return 'ok'; end;
> $$ language plpgsql;
>
>
> Results in error:
>
> ERROR: syntax error at or near "foobar"
> LINE 3: foobar text;
> ^
>

It's label for function's parameters.

Pavel

> If this is intentional, then could the error message be made better?
>
> --
> marko
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faq
>


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
Cc: "Marko Kreen" <markokr(at)gmail(dot)com>, Jan Wieck <JanWieck(at)Yahoo(dot)com>, "Postgres Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpgsql: another new reserved word
Date: 2007-11-10 00:53:32
Message-ID: 6665.1194656012@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:
> On 10/11/2007, Marko Kreen <markokr(at)gmail(dot)com> wrote:
>> I stumbled on another gotcha in 8.3's plpgsql:

> It's label for function's parameters.

But now that you mention it, that behavior is a little bit ugly.
I believe it's a pretty common practice to use a variable named
the same as the function to hold the eventual function result.
We've just broken that coding practice.

It's especially annoying because there isn't any obvious need
for it: in

create or replace function foobar() returns text as $$
declare
foobar text;
begin
foobar := 'ok';
return foobar;
end;
$$ language plpgsql;

there is no use of "foobar" in a place where a block label would
be syntactically legal, so it seems like we should be able to
keep the two types of name separate.

I think the reason for this may be that there are weird cases where
things *are* ambiguous. Consider

<< foo >>
declare bar int;
begin
...
declare foo record;
begin
foo.bar := 42;

Are we assigning to the outer block's variable bar, or to a field of the
inner block's variable foo?

The current plpgsql code seems to be designed to force a qualifier to be
interpreted as a block label if at all possible, even if there are
more-closely-nested alternative interpretations; so in the above example
it would assign to the outer variable bar. This seems a tad bogus
to me. Can anyone comment on how Oracle handles cases like this?

regards, tom lane


From: "Marko Kreen" <markokr(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>, "Jan Wieck" <JanWieck(at)yahoo(dot)com>, "Postgres Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpgsql: another new reserved word
Date: 2007-11-26 10:25:53
Message-ID: e51f66da0711260225w30fca8f4xfb2fcaa09e56cbb4@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 11/10/07, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> The current plpgsql code seems to be designed to force a qualifier to be
> interpreted as a block label if at all possible, even if there are
> more-closely-nested alternative interpretations; so in the above example
> it would assign to the outer variable bar. This seems a tad bogus
> to me. Can anyone comment on how Oracle handles cases like this?

Some googling brought following link:

http://download-uk.oracle.com/docs/cd/B14117_01/appdev.101/b10807/d_names.htm

I have not parsed it completely, but rule seems simple - inner
scope overrides outer one and no magic on unqualified idents,
if ident is unqualified, it wont be matched to schema, block
or some other qualifier. (well, at least no such magic behaviour
is mentioned.)

--
marko


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Marko Kreen" <markokr(at)gmail(dot)com>
Cc: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>, "Jan Wieck" <JanWieck(at)yahoo(dot)com>, "Postgres Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpgsql: another new reserved word
Date: 2007-11-26 15:26:24
Message-ID: 14075.1196090784@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Marko Kreen" <markokr(at)gmail(dot)com> writes:
> On 11/10/07, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Can anyone comment on how Oracle handles cases like this?

> Some googling brought following link:

> http://download-uk.oracle.com/docs/cd/B14117_01/appdev.101/b10807/d_names.htm

Hmm, interesting document. I think the bit that is relevant for us is
the statement

: An outer capture occurs when a name in an inner scope, which once
: resolved to an entity in an inner scope, is resolved to an entity in an
: outer scope. SQL and PL/SQL are designed to prevent outer captures. You
: do not need to take any action to avoid this condition.

AFAICT this means that if there is any ambiguity, the most closely
nested possible interpretation will always win. Therefore the current
behavior of plpgsql is indeed wrong, because it searches up the
namespace stack for block labels too soon. I'll go see about changing
that.

The subsection "Qualifying References to Object Attributes and Methods"
describes some truly bletcherous behavior --- you can't reference a
complex-column component unless you start from a table alias? How
non-orthogonal. I prefer our current solution of making you
parenthesize the column reference before you access its component ...

regards, tom lane