IFNULL -> COALESCE

Lists: pgsql-hackers
From: Lee Kindness <lkindness(at)csl(dot)co(dot)uk>
To: pgsql-hackers(at)postgresql(dot)org
Cc: Lee Kindness <lkindness(at)csl(dot)co(dot)uk>
Subject: IFNULL -> COALESCE
Date: 2002-02-08 16:37:03
Message-ID: 15459.65199.280887.269425@kelvin.csl.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Guys, I've recently being going back over code from Ingres and porting
it over to PostgreSQL. Heavy use was made of the IFNULL function, this
function simply returns the 2nd argument if the first is
NULL. Consider the following query:

SELECT COALESCE(MAX(id), 0) + 1 from test;

can be replaced by the following PostgreSQL query:

SELECT COALESCE(MAX(id), 0) + 1 from test;

I've manually done this, but wouldn't this be a useful auto-tranlation
to make in the parser? Aid to porting and all...

Yeah, I know i should be using a SERIAL column, that's later work...

Regards, Lee.


From: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>
To: Lee Kindness <lkindness(at)csl(dot)co(dot)uk>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: IFNULL -> COALESCE
Date: 2002-02-08 17:12:10
Message-ID: 20020209011111.J14715-100000@houston.familyhealth.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

> Guys, I've recently being going back over code from Ingres and porting
> it over to PostgreSQL. Heavy use was made of the IFNULL function, this
> function simply returns the 2nd argument if the first is
> NULL. Consider the following query:
>
> SELECT COALESCE(MAX(id), 0) + 1 from test;
>
> can be replaced by the following PostgreSQL query:
>
> SELECT COALESCE(MAX(id), 0) + 1 from test;

Ummm...did you make a mistake here? Those statements are identical...

Chris


From: Lee Kindness <lkindness(at)csl(dot)co(dot)uk>
To: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>
Cc: Lee Kindness <lkindness(at)csl(dot)co(dot)uk>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: IFNULL -> COALESCE
Date: 2002-02-08 17:17:03
Message-ID: 15460.2063.311288.503373@kelvin.csl.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Christopher Kings-Lynne writes:
> lkindness(at)csl(dot)co(dot)uk writes:
> > SELECT COALESCE(MAX(id), 0) + 1 from test;
> > can be replaced by the following PostgreSQL query:
> > SELECT COALESCE(MAX(id), 0) + 1 from test;
> Ummm...did you make a mistake here? Those statements are
> identical...

Okay, lets try that again...

SELECT IFNULL(MAX(id), 0) + 1 from test;

can be replaced by the following PostgreSQL query:

SELECT COALESCE(MAX(id), 0) + 1 from test;

Thanks, Lee.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Lee Kindness <lkindness(at)csl(dot)co(dot)uk>
Cc: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: IFNULL -> COALESCE
Date: 2002-02-08 18:23:03
Message-ID: 24776.1013192583@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Lee Kindness <lkindness(at)csl(dot)co(dot)uk> writes:
> Okay, lets try that again...
> SELECT IFNULL(MAX(id), 0) + 1 from test;
> can be replaced by the following PostgreSQL query:
> SELECT COALESCE(MAX(id), 0) + 1 from test;

For any specific datatype that you might need this for, you could
provide a user-defined IFNULL function to avoid having to translate
your code. Might get a bit tedious if you are doing it for a lot
of different datatypes, however.

Not sure if it's worth adding a keyword and a grammar production
to get Postgres to do this for you. If it were part of a full-court
press to improve our Oracle compatibility, I wouldn't object, but
I'm not sure I see the point of doing just the one nonstandard
feature.

regards, tom lane


From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Lee Kindness <lkindness(at)csl(dot)co(dot)uk>
Cc: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>, <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: IFNULL -> COALESCE
Date: 2002-02-08 18:29:00
Message-ID: 20020208102617.Y44162-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, 8 Feb 2002, Lee Kindness wrote:

> Christopher Kings-Lynne writes:
> > lkindness(at)csl(dot)co(dot)uk writes:
> > > SELECT COALESCE(MAX(id), 0) + 1 from test;
> > > can be replaced by the following PostgreSQL query:
> > > SELECT COALESCE(MAX(id), 0) + 1 from test;
> > Ummm...did you make a mistake here? Those statements are
> > identical...
>
> Okay, lets try that again...
>
> SELECT IFNULL(MAX(id), 0) + 1 from test;
>
> can be replaced by the following PostgreSQL query:
>
> SELECT COALESCE(MAX(id), 0) + 1 from test;

Might be nice to have it done automatically, but as a workaround
why not just define ifnull(int, int) - or whatever types are
necessary.

create function ifnull(int, int) returns int as
'select coalesce($1, $2);' language 'sql';
should work for 7.1 and above unless I'm missing something.


From: Lee Kindness <lkindness(at)csl(dot)co(dot)uk>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Lee Kindness <lkindness(at)csl(dot)co(dot)uk>, Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: IFNULL -> COALESCE
Date: 2002-02-11 09:44:27
Message-ID: 15463.37499.44819.752088@kelvin.csl.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Oh, i'd agree - it's not really worth the hassle adding the code to
automatically do this. Useful to have it mentioned in the archives so
someone else coming up against the same issue can pick up on it
quicker...

Got me thinking about an option for ecpg to report about any
non-standard/user-defined functions used in the source (which of
course it assumes are such and just lets them through). Also that
'sqlca is included by default' message added for 7.2 is annoying!

And Bruce, yeah there's a lock ;)

Regards, Lee Kindness.

Tom Lane writes:
> Lee Kindness <lkindness(at)csl(dot)co(dot)uk> writes:
> > Okay, lets try that again...
> > SELECT IFNULL(MAX(id), 0) + 1 from test;
> > can be replaced by the following PostgreSQL query:
> > SELECT COALESCE(MAX(id), 0) + 1 from test;
>
> For any specific datatype that you might need this for, you could
> provide a user-defined IFNULL function to avoid having to translate
> your code. Might get a bit tedious if you are doing it for a lot
> of different datatypes, however.
>
> Not sure if it's worth adding a keyword and a grammar production
> to get Postgres to do this for you. If it were part of a full-court
> press to improve our Oracle compatibility, I wouldn't object, but
> I'm not sure I see the point of doing just the one nonstandard
> feature.
>
> regards, tom lane


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Lee Kindness <lkindness(at)csl(dot)co(dot)uk>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: IFNULL -> COALESCE
Date: 2002-02-11 16:39:21
Message-ID: Pine.LNX.4.30.0202111138550.686-100000@peter.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Lee Kindness writes:

> Got me thinking about an option for ecpg to report about any
> non-standard/user-defined functions used in the source (which of
> course it assumes are such and just lets them through). Also that
> 'sqlca is included by default' message added for 7.2 is annoying!

No kidding. Can we remove that for 7.2.1?

--
Peter Eisentraut peter_e(at)gmx(dot)net


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Lee Kindness <lkindness(at)csl(dot)co(dot)uk>, Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: IFNULL -> COALESCE
Date: 2002-02-11 16:46:09
Message-ID: 7392.1013445969@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Peter Eisentraut <peter_e(at)gmx(dot)net> writes:
>> 'sqlca is included by default' message added for 7.2 is annoying!

> No kidding. Can we remove that for 7.2.1?

I didn't understand why it was put in in the first place. There's
no need for it.

regards, tom lane


From: "Ross J(dot) Reedstrom" <reedstrm(at)rice(dot)edu>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: IFNULL -> COALESCE
Date: 2002-02-11 21:06:20
Message-ID: 20020211210620.GC31039@rice.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Feb 11, 2002 at 09:44:27AM +0000, Lee Kindness wrote:
> Oh, i'd agree - it's not really worth the hassle adding the code to
> automatically do this. Useful to have it mentioned in the archives so
> someone else coming up against the same issue can pick up on it
> quicker...
>
> Got me thinking about an option for ecpg to report about any
> non-standard/user-defined functions used in the source (which of
> course it assumes are such and just lets them through). Also that
> 'sqlca is included by default' message added for 7.2 is annoying!

That's actually something needed for FIPS (US federal gov't standard)
although it's optional (not mentionec at all?) for ANSI or ISO: a
'flagger' that reports all non-standard extensions.

Ross


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Lee Kindness <lkindness(at)csl(dot)co(dot)uk>, Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, PostgreSQL Development <pgsql-hackers(at)postgresql(dot)org>, Michael Meskes <meskes(at)postgresql(dot)org>
Subject: sqlca warning (was Re: IFNULL -> COALESCE)
Date: 2002-02-11 21:56:00
Message-ID: Pine.LNX.4.30.0202111654120.1903-100000@peter.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Tom Lane writes:

> Peter Eisentraut <peter_e(at)gmx(dot)net> writes:
> >> 'sqlca is included by default' message added for 7.2 is annoying!
>
> > No kidding. Can we remove that for 7.2.1?
>
> I didn't understand why it was put in in the first place. There's
> no need for it.

As it stands, sqlca will actually be included twice, so the warning has
some merit. But it might be better to actually prevent the second
inclusion.

--
Peter Eisentraut peter_e(at)gmx(dot)net


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Lee Kindness <lkindness(at)csl(dot)co(dot)uk>, Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, PostgreSQL Development <pgsql-hackers(at)postgresql(dot)org>, Michael Meskes <meskes(at)postgresql(dot)org>
Subject: Re: sqlca warning (was Re: IFNULL -> COALESCE)
Date: 2002-02-11 21:58:56
Message-ID: 15786.1013464736@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Peter Eisentraut <peter_e(at)gmx(dot)net> writes:
> As it stands, sqlca will actually be included twice, so the warning has
> some merit. But it might be better to actually prevent the second
> inclusion.

Why? sqlca.h has an #ifndef guard, so there's no harm done. I'd vote
for just suppressing the check and notice completely.

regards, tom lane


From: Lee Kindness <lkindness(at)csl(dot)co(dot)uk>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Lee Kindness <lkindness(at)csl(dot)co(dot)uk>, Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, PostgreSQL Development <pgsql-hackers(at)postgresql(dot)org>, Michael Meskes <meskes(at)postgresql(dot)org>
Subject: sqlca warning (was Re: IFNULL -> COALESCE)
Date: 2002-02-12 09:34:03
Message-ID: 15464.57739.160955.706247@kelvin.csl.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Peter Eisentraut writes:
> Tom Lane writes:
> > I didn't understand why it was put in in the first place. There's
> > no need for it.
> As it stands, sqlca will actually be included twice, so the warning has
> some merit. But it might be better to actually prevent the second
> inclusion.

As I understand it with 7.1 you HAD to have an 'EXEC SQL INCLUDE
sqlca' line for things to work (assuming you actually access the sqlca
structure). With 7.2 this file is now automatically included (whether
you need it or not) and when you explicitly tell the precompiler what
you're using you get a warning! Imagine the response if a C compiler
was compiling the following program:

#include <stdio.h>

int main(int argc, char **argv)
{
printf("Hello world!\n");
}

and gave you a warning for including stdio.h! For reference gcc must
be doing something similar to ecpg - because you don't NEED to include
stdio.h (which is bad).

It's nothing major... just annoying!

Best Regards, Lee.