Thread: expr ? trueval : falseval

expr ? trueval : falseval

From
Brett McCormick
Date:
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?

--brett

Re: [HACKERS] expr ? trueval : falseval

From
"Thomas G. Lockhart"
Date:
Brett McCormick 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?

I suspect that this is covered in the SQL92 CASE expression, which we
don't yet support.

              - Tom

Re: [HACKERS] expr ? trueval : falseval

From
Bruce Momjian
Date:
>
>
> 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?

Good question.  Answer is no, I think, unless you can do some fancy
things with functions.

--
Bruce Momjian                          |  830 Blythe Avenue
maillist@candle.pha.pa.us              |  Drexel Hill, Pennsylvania 19026
  +  If your life is a hard drive,     |  (610) 353-9879(w)
  +  Christ can be your backup.        |  (610) 853-3000(h)

Re: expr ? trueval : falseval

From
Hannu Krosing
Date:
Brett McCormick <brett@work.chicken.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(choice(quarter(buy_time),'1',amount,0)) as "Q1",
  sum(choice(quarter(buy_time),'2',amount,0)) as "Q2",
  sum(choice(quarter(buy_time),'3',amount,0)) as "Q3",
  sum(choice(quarter(buy_time),'4',amount,0)) as "Q4",
  sum(amount) as "Year total"
from
  buyings
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

Re: [HACKERS] Re: expr ? trueval : falseval

From
Stephane Lajeunesse
Date:
Hannu Krosing wrote:
> This function (i can't remember its name, but lets assume it is
> called CHOICE) behaves as follows:

The function name is DECODE and after using it a few times, I find that
it is a timesaver.

BTW, is anybody in charge of the oracle-compat portion.  If not, I might
be interested?

Also, is anybody addressing the ALTER USER .. IN GROUP ... and CREATE
USER .. IN GROUP ... (basically anything pertaining to groups)? I've
started hacking away at it to make it work.

Thanks,

--
Stephane Lajeunesse.
Oracle and Sybase DBA

Re: [HACKERS] Re: expr ? trueval : falseval

From
Bruce Momjian
Date:
>
> Hannu Krosing wrote:
> > This function (i can't remember its name, but lets assume it is
> > called CHOICE) behaves as follows:
>
> The function name is DECODE and after using it a few times, I find that
> it is a timesaver.
>
> BTW, is anybody in charge of the oracle-compat portion.  If not, I might
> be interested?

Edmund Mergl <E.Mergl@bawue.de>.  He is still involved, by hasn't made
any additions to those functions since maybe September.

>
> Also, is anybody addressing the ALTER USER .. IN GROUP ... and CREATE
> USER .. IN GROUP ... (basically anything pertaining to groups)? I've
> started hacking away at it to make it work.

Good.  We need help there.  See the FAQ on groups, if you haven't
already.



--
Bruce Momjian                          |  830 Blythe Avenue
maillist@candle.pha.pa.us              |  Drexel Hill, Pennsylvania 19026
  +  If your life is a hard drive,     |  (610) 353-9879(w)
  +  Christ can be your backup.        |  (610) 853-3000(h)