pl/pgsql oddity

Started by Joolzover 21 years ago25 messagesgeneral
Jump to latest
#1Joolz
joolz@arbodienst-limburg.nl

Hello everyone,

When writing some serverside code I ran into an oddity that I
managed to boil down to this:

-------------------------------------------------------
create or replace function fubar() returns varchar as '
declare
l integer;
begin
l = 38;
if l < 38 then
return ''< 38'';
elseif l >= 38 then
return ''>= 38'';
else
return ''this is not possible'';
end if;
end;'
language 'plpgsql';
-------------------------------------------------------

But I can't understand or solve it. The function always returns
'this is not possible'. Can someone tell me what I'm overlooking?
Thanks!

#2Matteo Beccati
php@beccati.com
In reply to: Joolz (#1)
Re: pl/pgsql oddity

Hi,

l = 38;

This should be:

l := 38;

otherwise l would remain uninitialized (NULL).

Ciao ciao
--
Matteo Beccati
http://phpadsnew.com
http://phppgads.com

#3Ian Lawrence Barwick
barwick@gmail.com
In reply to: Joolz (#1)
Re: pl/pgsql oddity

On Thu, 16 Dec 2004 10:06:19 +0100 (CET), Joolz
<joolz@arbodienst-limburg.nl> wrote:

Hello everyone,

When writing some serverside code I ran into an oddity that I
managed to boil down to this:

-------------------------------------------------------
create or replace function fubar() returns varchar as '
declare
l integer;
begin
l = 38;
if l < 38 then
return ''< 38'';
elseif l >= 38 then

Try "elsif" here.
(No, I don't know what the problem with "elseif" is).

Ian Barwick

#4Richard Huxton
dev@archonet.com
In reply to: Joolz (#1)
Re: pl/pgsql oddity

Joolz wrote:

Hello everyone,

When writing some serverside code I ran into an oddity that I
managed to boil down to this:

elseif l >= 38 then

You want "elsif" - plpgsql isn't a hugely sophisticated language and its
parser is having trouble there. I'm guessing the parser is somehow
putting the "elseif" branch under the initial "then" so it never gets
executed. If you rewrite the function like so:

create or replace function fubar() returns varchar as '
declare
l integer;
begin
l = 34;
if l < 38 then
raise notice ''< 38: %'',l;
elseif l >= 38
then raise notice ''>= 38: %'',l;
else
raise notice ''this is not possible: %'',l;
end if;

return 0;
end;'
language 'plpgsql';

Now, try different values for "l" and you'll see what is happening.
Congratulations - I think you've found a bug. You can report it formally
via the bugs mailing list or http://www.postgresql.org/bugform.html

--
Richard Huxton
Archonet Ltd

#5Joolz
joolz@arbodienst-limburg.nl
In reply to: Ian Lawrence Barwick (#3)
Re: pl/pgsql oddity

Ian Barwick zei:

On Thu, 16 Dec 2004 10:06:19 +0100 (CET), Joolz
<joolz@arbodienst-limburg.nl> wrote:

Hello everyone,

When writing some serverside code I ran into an oddity that I
managed to boil down to this:

-------------------------------------------------------
create or replace function fubar() returns varchar as '
declare
l integer;
begin
l = 38;
if l < 38 then
return ''< 38'';
elseif l >= 38 then

Try "elsif" here.
(No, I don't know what the problem with "elseif" is).

Thanks Ian, but I don't understand... I _am_ already using elseif...

#6Tomasz Myrta
jasiek@klaster.net
In reply to: Joolz (#1)
Re: pl/pgsql oddity

When writing some serverside code I ran into an oddity that I
managed to boil down to this:

-------------------------------------------------------
create or replace function fubar() returns varchar as '
declare
l integer;
begin
l = 38;
if l < 38 then
return ''< 38'';
elseif l >= 38 then
return ''>= 38'';
else
return ''this is not possible'';
end if;
end;'
language 'plpgsql';
-------------------------------------------------------

But I can't understand or solve it. The function always returns
'this is not possible'. Can someone tell me what I'm overlooking?
Thanks!

Documentation says you should use "elsif" rather than "elseif". In your
case everything between "return <38" and "else" is discarded as
unreachable code. The same sense will have function below:

create or replace function fubar() returns varchar as '
declare
l integer;
begin
l = 38;
if l < 38 then
return ''< 38'';
bleblbebleblebe sfdsdf;
nothing special;
else
return ''this is not possible'';
end if;
end;'
language 'plpgsql';
select fubar();

Regards,
Tomasz Myrta

#7Joolz
joolz@arbodienst-limburg.nl
In reply to: Tomasz Myrta (#6)
Re: pl/pgsql oddity

Tomasz Myrta zei:

When writing some serverside code I ran into an oddity that I
managed to boil down to this:

-------------------------------------------------------
create or replace function fubar() returns varchar as '
declare
l integer;
begin
l = 38;
if l < 38 then
return ''< 38'';
elseif l >= 38 then
return ''>= 38'';
else
return ''this is not possible'';
end if;
end;'
language 'plpgsql';
-------------------------------------------------------

But I can't understand or solve it. The function always returns
'this is not possible'. Can someone tell me what I'm overlooking?
Thanks!

Documentation says you should use "elsif" rather than "elseif". In
your
case everything between "return <38" and "else" is discarded as
unreachable code.

That's it, thanks!

#8Joolz
joolz@arbodienst-limburg.nl
In reply to: Richard Huxton (#4)
Re: pl/pgsql oddity

Richard Huxton zei:

Hi Richard,

See the other posting,

elseif l >= 38

Apparently this is parsed as

elseif l >= 38
^ ^
| |
code|
|
comment from here on

It should be "elsif", not "elseif" :-\

Thanks everyone!

#9Neil Conway
neilc@samurai.com
In reply to: Richard Huxton (#4)
Re: pl/pgsql oddity

Richard Huxton wrote:

You want "elsif" - plpgsql isn't a hugely sophisticated language and its
parser is having trouble there. I'm guessing the parser is somehow
putting the "elseif" branch under the initial "then" so it never gets
executed.

Indeed; the parser thinks an unrecognized keyword indicates the
beginning of a SQL statement: since the PL/PgSQL parser and the SQL
parser are completely separate, we need to do some guessing about what
constitutes a legal SQL statement. See the more detailed diagnose of the
problem here:

http://archives.postgresql.org/pgsql-bugs/2004-11/msg00297.php

There's a patch in that thread that provides better PL/PgSQL error
checking (which results in flagging this kind of code as invalid at
compile time). Some form of that patch will be in 8.1, as well as other
nice stuff like warning for unreachable code.

Tom also suggested just adding 'elseif' as an alternative for 'elsif'.
That sounds like it would be worth doing.

Congratulations - I think you've found a bug. You can report it formally
via the bugs mailing list

No need, this is a known issue.

-Neil

#10Richard Huxton
dev@archonet.com
In reply to: Matteo Beccati (#2)
Re: pl/pgsql oddity

Matteo Beccati wrote:

Hi,

l = 38;

This should be:

l := 38;

otherwise l would remain uninitialized (NULL).

Actually, either work. You are right that the docs suggest the second
form though.

--
Richard Huxton
Archonet Ltd

#11Tom Lane
tgl@sss.pgh.pa.us
In reply to: Neil Conway (#9)
Re: pl/pgsql oddity

Neil Conway <neilc@samurai.com> writes:

Tom also suggested just adding 'elseif' as an alternative for 'elsif'.
That sounds like it would be worth doing.

I think we should go ahead and do that for 8.0. I'm getting tired of
reading reports that stem from this mistake (I think this is the third
one in the past month ...). I can't see any real downside to accepting
both spellings, can you?

regards, tom lane

#12Geoffrey
esoteric@3times25.net
In reply to: Tom Lane (#11)
Re: pl/pgsql oddity

Tom Lane wrote:

Neil Conway <neilc@samurai.com> writes:

Tom also suggested just adding 'elseif' as an alternative for 'elsif'.
That sounds like it would be worth doing.

I think we should go ahead and do that for 8.0. I'm getting tired of
reading reports that stem from this mistake (I think this is the third
one in the past month ...). I can't see any real downside to accepting
both spellings, can you?

Well, you end up with the opposite problem. Someone has a issue and is
focusing on the spelling of elseif (elsif), because the google searches
they've done happen to come up with a different spelling then what
they're using. So, they're looking for a solution in the wrong place.

I don't know of any other language that permits multiple spellings for
the same construct. I'd be concerned with starting such a precedent.

Now I'm sure someone will post back with some valid examples from other
languages. :(

--
Until later, Geoffrey

#13Michael Fuhr
mike@fuhr.org
In reply to: Geoffrey (#12)
Re: pl/pgsql oddity

On Thu, Dec 16, 2004 at 12:27:53PM -0500, Geoffrey wrote:

I don't know of any other language that permits multiple spellings for
the same construct. I'd be concerned with starting such a precedent.

I'd be in favor of making it a bloody law that every bloody language
use the same bloody spelling. I'm forever forgetting whether a
particular language uses ELSE IF, ELSEIF, ELSIF, or ELIF. Grumble,
grumble, grumble....

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

#14Tom Lane
tgl@sss.pgh.pa.us
In reply to: Geoffrey (#12)
Re: pl/pgsql oddity

Geoffrey <esoteric@3times25.net> writes:

I don't know of any other language that permits multiple spellings for
the same construct. I'd be concerned with starting such a precedent.

Well, we have plenty of precedent already at the SQL language level:
ANALYZE vs ANALYSE, NOTNULL vs IS NOT NULL, and so on.

regards, tom lane

#15Geoffrey
esoteric@3times25.net
In reply to: Tom Lane (#14)
Re: pl/pgsql oddity

Tom Lane wrote:

Geoffrey <esoteric@3times25.net> writes:

I don't know of any other language that permits multiple spellings for
the same construct. I'd be concerned with starting such a precedent.

Well, we have plenty of precedent already at the SQL language level:
ANALYZE vs ANALYSE, NOTNULL vs IS NOT NULL, and so on.

Like I said, someone would come up with some real world examples. :) I
personally wouldn't lose too much sleep over it.

--
Until later, Geoffrey

#16Guy Rouillier
guyr@masergy.com
In reply to: Geoffrey (#15)
Re: pl/pgsql oddity

Michael Fuhr wrote:

On Thu, Dec 16, 2004 at 12:27:53PM -0500, Geoffrey wrote:

I don't know of any other language that permits multiple spellings
for the same construct. I'd be concerned with starting such a
precedent.

I'd be in favor of making it a bloody law that every bloody language
use the same bloody spelling. I'm forever forgetting whether a
particular language uses ELSE IF, ELSEIF, ELSIF, or ELIF. Grumble,
grumble, grumble....

As a relative newbie to PostgreSQL (but an old-timer to programming
languages and other DMBSs) I would certainly vote for allowing elseif.
This is my first encounter of "else" without the terminating "e", and
that would not be a natural omission for me.

--
Guy Rouillier

#17Frank D. Engel, Jr.
fde101@fjrhome.net
In reply to: Guy Rouillier (#16)
Re: pl/pgsql oddity

"elsif" is the spelling used by Ada. I'm getting rather used to it,
myself.

I'm really starting to like Ada. So "elsif" is fine with me.

As far as alternate spellings being accepted within a language, look at
the Transcript language used by Runtime Revolution (www.runrev.com),
which is a so-called "Xtalk" language (based on HyperTalk, which was
used by HyperCard; and yes, HyperTalk was like this too...)

-- and # are synonyms (comment delimiters)

abbreviated can be abbreviated as abbr or abbrev ;-)

audioClip can be abbreviated as ac

accelKey and acceleratorKey

acceleratorModifiers and accelMods

arm and armed

autoHilite and autoHighlight

recentNames and backList

group, background, bg, and bkgnd

The list goes on and On and ON...

On Dec 16, 2004, at 2:12 PM, Guy Rouillier wrote:

Michael Fuhr wrote:

On Thu, Dec 16, 2004 at 12:27:53PM -0500, Geoffrey wrote:

I don't know of any other language that permits multiple spellings
for the same construct. I'd be concerned with starting such a
precedent.

I'd be in favor of making it a bloody law that every bloody language
use the same bloody spelling. I'm forever forgetting whether a
particular language uses ELSE IF, ELSEIF, ELSIF, or ELIF. Grumble,
grumble, grumble....

As a relative newbie to PostgreSQL (but an old-timer to programming
languages and other DMBSs) I would certainly vote for allowing elseif.
This is my first encounter of "else" without the terminating "e", and
that would not be a natural omission for me.

--
Guy Rouillier

---------------------------(end of
broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if
your
joining column's datatypes do not match

-----------------------------------------------------------
Frank D. Engel, Jr. <fde101@fjrhome.net>

$ ln -s /usr/share/kjvbible /usr/manual
$ true | cat /usr/manual | grep "John 3:16"
John 3:16 For God so loved the world, that he gave his only begotten
Son, that whosoever believeth in him should not perish, but have
everlasting life.
$

___________________________________________________________
$0 Web Hosting with up to 120MB web space, 1000 MB Transfer
10 Personalized POP and Web E-mail Accounts, and much more.
Signup at www.doteasy.com

#18Clodoaldo Pinto Neto
clodoaldo_pinto@yahoo.com.br
In reply to: Tom Lane (#11)
Re: pl/pgsql oddity
 --- Tom Lane <tgl@sss.pgh.pa.us> escreveu: 

Neil Conway <neilc@samurai.com> writes:

Tom also suggested just adding 'elseif' as an alternative for 'elsif'.
That sounds like it would be worth doing.

I think we should go ahead and do that for 8.0. I'm getting tired of
reading reports that stem from this mistake (I think this is the third
one in the past month ...). I can't see any real downside to accepting
both spellings, can you?

regards, tom lane

I made the exact same mistake and it took me a good chunk of energy to figure
it out. I didn't report it to the list. When one look at the manual it is very
easy to read elseif in instead of elsif.

Regards,
Clodoaldo Pinto

_______________________________________________________
Yahoo! Mail - Agora com 250MB de espa�o gratuito. Abra
uma conta agora! http://br.info.mail.yahoo.com/

#19Martijn van Oosterhout
kleptog@svana.org
In reply to: Clodoaldo Pinto Neto (#18)
Re: pl/pgsql oddity

On Thu, Dec 16, 2004 at 05:14:42PM -0300, Clodoaldo Pinto wrote:

--- Tom Lane <tgl@sss.pgh.pa.us> escreveu: 

Neil Conway <neilc@samurai.com> writes:

Tom also suggested just adding 'elseif' as an alternative for 'elsif'.
That sounds like it would be worth doing.

I think we should go ahead and do that for 8.0. I'm getting tired of
reading reports that stem from this mistake (I think this is the third
one in the past month ...). I can't see any real downside to accepting
both spellings, can you?

regards, tom lane

I made the exact same mistake and it took me a good chunk of energy to figure
it out. I didn't report it to the list. When one look at the manual it is very
easy to read elseif in instead of elsif.

I'd vote for one of two options:

1. Accept both elseif and elsif as equivalent or
2. Add some parsing magic to give a meaningful error message so people
see it immediatly.

Maybe just match in "elseif" at beginning of a line and error out with
"maybe you meant elsif".
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/

Show quoted text

Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
tool for doing 5% of the work and then sitting around waiting for someone
else to do the other 95% so you can sue them.

#20Richard D Levine
Richard_D_Levine@raytheon.com
In reply to: Martijn van Oosterhout (#19)
Re: pl/pgsql oddity

I prefer the FORTRAN66 construct

IF <condition> <label1> <label2> <label3>

where it jumps to label1 if <condition> is negative, label2 if zero, and
label3 if positive. No else ifs about it.

I hope you realize I'm as kidding as I am obviously too old.

Michael Fuhr
<mike@fuhr.org> To: Geoffrey <esoteric@3times25.net>
Sent by: cc: pgsql-general@postgresql.org
pgsql-general-owner@pos Subject: Re: [GENERAL] pl/pgsql oddity
tgresql.org

12/16/2004 01:16 PM

On Thu, Dec 16, 2004 at 12:27:53PM -0500, Geoffrey wrote:

I don't know of any other language that permits multiple spellings for
the same construct. I'd be concerned with starting such a precedent.

I'd be in favor of making it a bloody law that every bloody language
use the same bloody spelling. I'm forever forgetting whether a
particular language uses ELSE IF, ELSEIF, ELSIF, or ELIF. Grumble,
grumble, grumble....

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

#21Richard D Levine
Richard_D_Levine@raytheon.com
In reply to: Richard D Levine (#20)
#22Frank D. Engel, Jr.
fde101@fjrhome.net
In reply to: Richard D Levine (#21)
#23Bruno Wolff III
bruno@wolff.to
In reply to: Richard D Levine (#20)
#24Neil Conway
neilc@samurai.com
In reply to: Tom Lane (#11)
#25Richard D Levine
Richard_D_Levine@raytheon.com
In reply to: Neil Conway (#24)