pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
postgres - CASE evaluates subexpression that is not needed to determine the result
3+ messages / 3 participants
[nested] [flat]

* postgres - CASE evaluates subexpression that is not needed to determine the result
@ 2018-11-05 13:07 Kamenoqd Zelen <kamenoqd@gmail.com>
  2018-11-05 13:50 ` Re: postgres - CASE evaluates subexpression that is not needed to determine the result Pavel Stehule <pavel.stehule@gmail.com>
  2018-11-05 14:45 ` Re: postgres - CASE evaluates subexpression that is not needed to determine the result Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 3+ messages in thread

From: Kamenoqd Zelen @ 2018-11-05 13:07 UTC (permalink / raw)
  To: pgsql-bugs

Hello,
I have the following 3 examples of case expressions in postgres, which I
would expect to evaluate in the same way. However the first and the third
give ERROR: invalid input syntax for integer: "2017.7". The second one
returns true. Why is the difference?

Postgres documentation
<https://www.postgresql.org/docs/7.4/static/functions-conditional.html;
 states:

"A CASE expression does not evaluate any subexpressions that are not needed
to determine the result."

select case when 0 = 0 then 1 < 2

        when 0 = 2 then 2000 = ('2017.7')::bigint

    end;


select case when 0 = 0 then 1 < 2

        when 0 = 2 then 2000 = ('2017.7'||'')::bigint

    end;


select case when (array[1,2])[1] =1 then 1 < 2

        when (array[1,2])[1] = 2 then 2000 = ('2017.7'||'')::bigint

    end;

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: postgres - CASE evaluates subexpression that is not needed to determine the result
  2018-11-05 13:07 postgres - CASE evaluates subexpression that is not needed to determine the result Kamenoqd Zelen <kamenoqd@gmail.com>
@ 2018-11-05 13:50 ` Pavel Stehule <pavel.stehule@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Pavel Stehule @ 2018-11-05 13:50 UTC (permalink / raw)
  To: kamenoqd@gmail.com; +Cc: pgsql-bugs

Hi

po 5. 11. 2018 v 14:37 odesílatel Kamenoqd Zelen <kamenoqd@gmail.com>
napsal:

> Hello,
> I have the following 3 examples of case expressions in postgres, which I
> would expect to evaluate in the same way. However the first and the third
> give ERROR: invalid input syntax for integer: "2017.7". The second one
> returns true. Why is the difference?
>

I am not sure, but it looks so there is a problem in simplification in
planning time. Any expression used in SQL query is checked and if it is
possible - then it is evaluated in planning time. This evaluation is
limited to some deterministic operations over constant. On my comp doesn't
work only first when the constant should be casted to bigint. When you try
to assign empty string, then this simplification is not possible - and
because the expression is not evaluated in running time, then you got
expected result.

It is not a bug. This is side effect of expression simplification at parser
time.

Regards

Pavel


> Postgres documentation
> <https://www.postgresql.org/docs/7.4/static/functions-conditional.html;
>  states:
>
> "A CASE expression does not evaluate any subexpressions that are not
> needed to determine the result."
>
> select case when 0 = 0 then 1 < 2
>
>         when 0 = 2 then 2000 = ('2017.7')::bigint
>
>     end;
>
>
> select case when 0 = 0 then 1 < 2
>
>         when 0 = 2 then 2000 = ('2017.7'||'')::bigint
>
>     end;
>
>
> select case when (array[1,2])[1] =1 then 1 < 2
>
>         when (array[1,2])[1] = 2 then 2000 = ('2017.7'||'')::bigint
>
>     end;
>

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: postgres - CASE evaluates subexpression that is not needed to determine the result
  2018-11-05 13:07 postgres - CASE evaluates subexpression that is not needed to determine the result Kamenoqd Zelen <kamenoqd@gmail.com>
@ 2018-11-05 14:45 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 3+ messages in thread

From: Tom Lane @ 2018-11-05 14:45 UTC (permalink / raw)
  To: Kamenoqd Zelen <kamenoqd@gmail.com>; +Cc: pgsql-bugs

Kamenoqd Zelen <kamenoqd@gmail.com> writes:
> I have the following 3 examples of case expressions in postgres, which I
> would expect to evaluate in the same way. However the first and the third
> give ERROR: invalid input syntax for integer: "2017.7". The second one
> returns true. Why is the difference?

Constant subexpressions get folded regardless of location.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2018-11-05 14:45 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-11-05 13:07 postgres - CASE evaluates subexpression that is not needed to determine the result Kamenoqd Zelen <kamenoqd@gmail.com>
2018-11-05 13:50 ` Pavel Stehule <pavel.stehule@gmail.com>
2018-11-05 14:45 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox