Re: BUG #17637: case-when branches taken even if they dont match, raising errors - Mailing list pgsql-bugs

From Richard Guo
Subject Re: BUG #17637: case-when branches taken even if they dont match, raising errors
Date
Msg-id CAMbWs48yWBw6JRGck7qB3Eyvkz3xzRYqmHNvB=DZeNrpgQF3RQ@mail.gmail.com
Whole thread Raw
In response to BUG #17637: case-when branches taken even if they dont match, raising errors  (PG Bug reporting form <noreply@postgresql.org>)
Responses Re: BUG #17637: case-when branches taken even if they dont match, raising errors
List pgsql-bugs

On Thu, Oct 13, 2022 at 6:08 PM PG Bug reporting form <noreply@postgresql.org> wrote:
drop table if exists test;
drop table if exists tmap;
create table test(
 id int8,
 vf float8,
 vb bool
);
create table tmap(
 id int8,
 mapped_to int8
);
insert into tmap values(1, 1);
insert into tmap values(2, 2);
insert into test
with tmp as (select 1::int8 id, '123.4'::text v)
select t.id,
case m.mapped_to when 1 then v::float8 else null end,
case m.mapped_to when 2 then v::bool else null end
from tmp t
join tmap m on m.id = t.id;
 
I think this has something to do with the CTE used here. In
preprocess_expression, we do not know the value of m.mapped_to, so we
cannot tell the test condition is constant FALSE. Thus we need go on
processing the result. But thanks to the CTE, we know t.v is const
'123.4'::text, and we want to convert it to boolean, which triggers the
error.

I'm not sure about this being a bug.

Thanks
Richard

pgsql-bugs by date:

Previous
From: PG Bug reporting form
Date:
Subject: BUG #17637: case-when branches taken even if they dont match, raising errors
Next
From: Tom Lane
Date:
Subject: Re: BUG #17637: case-when branches taken even if they dont match, raising errors