Re: Casting Integer to Boolean in assignment - Mailing list pgsql-general

From Geoff Winkless
Subject Re: Casting Integer to Boolean in assignment
Date
Msg-id CAEzk6ff-420-Ny_H0nWTM1nxxxcYQ4edJdG4Myz1hjxyCHPGAQ@mail.gmail.com
Whole thread Raw
In response to Casting Integer to Boolean in assignment  (Alexandre GRAIL <postgresql.general@augure.net>)
Responses Re: Casting Integer to Boolean in assignment  (Thomas Kellerer <spam_eater@gmx.net>)
Re: Casting Integer to Boolean in assignment  (Alexandre GRAIL <postgresql.general@augure.net>)
List pgsql-general
On Thu, 24 Jan 2019 at 11:04, Alexandre GRAIL
<postgresql.general@augure.net> wrote:
>
> And added to this weirdness is the fact that '1' or '0' (with quote) is OK.
>
The reason for that at least is that '1' and '0' are valid boolean values.

https://www.postgresql.org/docs/9.5/datatype-boolean.html

There's additional text describing why casts are chosen to be defined
as implicit or not here

https://www.postgresql.org/docs/9.5/typeconv-overview.html

My own opinion is that non-0 should implicitly cast as true and 0
should cast as false. I just run

UPDATE pg_cast SET castcontext = 'i' WHERE oid IN (
 SELECT c.oid
 FROM pg_cast c
 inner join pg_type src ON src.oid = c.castsource
 inner join pg_type tgt ON tgt.oid = c.casttarget
 WHERE (src.typname ILIKE '%int%' AND tgt.typname ILIKE 'bool%')
 OR (src.typname ILIKE 'bool%' AND tgt.typname ILIKE '%int%')
 OR (src.typname ILIKE 'bool%' AND tgt.typname ILIKE 'bit%')
 OR (src.typname ILIKE 'bit%' AND tgt.typname ILIKE 'bool%')
 OR (src.typname ILIKE 'bit%' AND tgt.typname ILIKE '%int')
);

when I install the system to solve this for my own uses.

Geoff


pgsql-general by date:

Previous
From: Alexandre GRAIL
Date:
Subject: Casting Integer to Boolean in assignment
Next
From: Thomas Kellerer
Date:
Subject: Re: Casting Integer to Boolean in assignment