From: Alexandre GRAIL <postgresql.general@augure.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: Casting Integer to Boolean in assignment
Date: Thu, 24 Jan 2019 18:26:06 +0100
Message-ID: <c5be754a-359f-311b-0eb7-cfc2ec50e1cb@augure-ng.fr> (raw)
In-Reply-To: <CAEzk6ff-420-Ny_H0nWTM1nxxxcYQ4edJdG4Myz1hjxyCHPGAQ@mail.gmail.com>
References: <d8fe822a-ac75-5763-00b3-273f11394fc5@augure-ng.fr>
<CAEzk6ff-420-Ny_H0nWTM1nxxxcYQ4edJdG4Myz1hjxyCHPGAQ@mail.gmail.com>
On 24/01/2019 12:45, Geoff Winkless wrote:
> 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')
> );
Thanks Geoff for this solution, I was thinking it cannot be changed ! I
end up doing this :
UPDATE pg_cast SET castcontext = 'a' 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%')
);
Only to have 0/1 => bool working in assignment. It saved me from
injecting ALTER TYPE before and after any INSERT/UPDATE.
In my case I don't control the query which is auto generated. (And the
framework assumes 1 and 0 are safe boolean values without cast or quote.
Changing that is not possible.)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: postgresql.general@augure.net
Subject: Re: Casting Integer to Boolean in assignment
In-Reply-To: <c5be754a-359f-311b-0eb7-cfc2ec50e1cb@augure-ng.fr>
References: <d8fe822a-ac75-5763-00b3-273f11394fc5@augure-ng.fr> <CAEzk6ff-420-Ny_H0nWTM1nxxxcYQ4edJdG4Myz1hjxyCHPGAQ@mail.gmail.com> <c5be754a-359f-311b-0eb7-cfc2ec50e1cb@augure-ng.fr>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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