pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: 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 07:17:57 -0800
Message-ID: <67e16eaa-0975-63ca-5c4c-3e362455b2b9@aklaver.com> (raw)
In-Reply-To: <d8fe822a-ac75-5763-00b3-273f11394fc5@augure-ng.fr>
References: <d8fe822a-ac75-5763-00b3-273f11394fc5@augure-ng.fr>

On 1/24/19 3:04 AM, Alexandre GRAIL wrote:
> Hello the list,
> 
> Maybe this question has been debated before (I didn't find anything 
> helpful) but :
> 
> Why the default is to throw an error when casting Integer to Boolean in 
> assignment, and accepting it everywhere else ?

The overall reason:

https://www.postgresql.org/docs/8.3/release-8-3.html

E.24.2.1. General

     Non-character data types are no longer automatically cast to TEXT 
(Peter, Tom)

     Previously, if a non-character value was supplied to an operator or 
function that requires text input, it was automatically cast to text, 
for most (though not all) built-in data types. This no longer happens: 
an explicit cast to text is now required for all non-character-string 
types. For example, these expressions formerly worked:

> 
> So you can type :
> 
> postgres=# select 1::boolean;
>   bool
> ------
>   t
> (1 row)
> 
> or
> 
> postgres=# select 0::boolean;
>   bool
> ------
>   f
> (1 row)
> 
> 
> But you *cannot* use 1 or 0 as valid input for boolean type when 
> inserting or updating :
> 
> test=# CREATE TABLE test1 (a boolean);
> CREATE TABLE
> test=# INSERT INTO test1 VALUES (1);
> ERROR:  column "a" is of type boolean but expression is of type integer
> LINE 1: INSERT INTO test1 VALUES (1);
>                                    ^
> HINT:  You will need to rewrite or cast the expression.
> 
> 
> This behavior cannot be changed, as this cast is hard coded with 
> "Implicit?=no".
> 
> And added to this weirdness is the fact that '1' or '0' (with quote) is OK.
> 
> So is there a reason to forbid 0 and 1 as valid boolean, without 
> explicit cast ?
> 
> Thanks!
> 
> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (21+ messages)  latest in thread

Message-ID: <67e16eaa-0975-63ca-5c4c-3e362455b2b9@aklaver.com>
Permalink:  ../67e16eaa-0975-63ca-5c4c-3e362455b2b9@aklaver.com/
Also on:    postgresql.org/message-id/67e16eaa-0975-63ca-5c4c-3e362455b2b9@aklaver.com

 · 

reply

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: adrian.klaver@aklaver.com, postgresql.general@augure.net
  Subject: Re: Casting Integer to Boolean in assignment
  In-Reply-To: <67e16eaa-0975-63ca-5c4c-3e362455b2b9@aklaver.com>
  References: <d8fe822a-ac75-5763-00b3-273f11394fc5@augure-ng.fr> <67e16eaa-0975-63ca-5c4c-3e362455b2b9@aklaver.com>

* 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