Re: Casting Integer to Boolean in assignment

From: Geoff Winkless <pgsqladmin(at)geoff(dot)dj>
To: Alexandre GRAIL <postgresql(dot)general(at)augure(dot)net>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Casting Integer to Boolean in assignment
Date: 2019-01-24 11:45:54
Message-ID: CAEzk6ff-420-Ny_H0nWTM1nxxxcYQ4edJdG4Myz1hjxyCHPGAQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 24 Jan 2019 at 11:04, Alexandre GRAIL
<postgresql(dot)general(at)augure(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Kellerer 2019-01-24 12:17:52 Re: Casting Integer to Boolean in assignment
Previous Message Alexandre GRAIL 2019-01-24 11:04:14 Casting Integer to Boolean in assignment