Re: expr ? trueval : falseval

From: Hannu Krosing brett(at)work(dot)chicken(dot)org pgsql-hackers(at)hub(dot)org Re: expr ? trueval : falseval 1998-03-21 19:58:56 35141C00.358B066E@sid.trust.ee (view raw, whole thread or download thread mbox) 1998-03-21 19:58:56 from Hannu Krosing  1998-03-23 00:25:11 from Stephane Lajeunesse   1998-03-23 02:01:26 from Bruce Momjian pgsql-hackers
```Brett McCormick <brett(at)work(dot)chicken(dot)org> wrote:

> expr ? trueval : falseval
>
> if expr evaluated is true, the entire expression is trueval evaluated,
> otherwise it is falseval evaluated.
>
> Is it possible to have this sort of grammar logic in the parser?

For achieving this kind of functionality, you can also define a
function like Oracle does. Actually the Oracle way is more general.

This function (i can't remember its name, but lets assume it is
called CHOICE) behaves as follows:

it has as its first argument a testvalue followed by pairs of arguments,
match and value, and possibly a default value as last argument.

the function is evaluated so that a value whose match equals
testvalue is returned.

so for your case you would call:

CHOICE(expr,'T',trueval,'F',falseval)

but it is much more general, for example for getting a nice table of
amounts of something bought quarterly, you do the following.

select
name as "Name",
sum(amount) as "Year total"
from
group by name;

and get the following

Name   | Q1  | Q2  | Q3  | Q4  | Total
-------+-----+-----+-----+-----+--------
cats   |   0 |   0 |   3 |   7 |   10
dogs   |   1 |   2 |   3 |   4 |   10
ducks  |   1 |   1 |   1 |   1 |    4

What holds us back from defining a function like this now is
difficulty of defining functions that can accept arguments and
can return values of an opaque type (or actually type defined
at invocation time)

Or actually there is (or at least was a little while back) a function
COUNT that can take any type of argument and return an integer, so
_this_ should be possible to define.

But I have not found any reference how to tell a function that it has
variable number of arguments and that it can itself tell what it returns
based on what arguments are given. I think that this is quite hard given
the current implementation.

Hannu

```

pgsql-hackers by date

 Next: From: The Hermit Hacker Date: 1998-03-21 23:55:39 Subject: Re: [HACKERS] patch for memory overrun on Linux(i386) Previous: From: Maurice Gittens Date: 1998-03-21 18:44:12 Subject: patch for memory overrun on Linux(i386)