Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gmfh9-0003sq-FW for pgsql-general@arkaria.postgresql.org; Thu, 24 Jan 2019 14:09:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gmfh8-0003Vv-0y for pgsql-general@arkaria.postgresql.org; Thu, 24 Jan 2019 14:09:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gmfh7-0003Vo-K6 for pgsql-general@lists.postgresql.org; Thu, 24 Jan 2019 14:09:25 +0000 Received: from mail-oi1-x236.google.com ([2607:f8b0:4864:20::236]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gmfh4-0004x4-Go for pgsql-general@lists.postgresql.org; Thu, 24 Jan 2019 14:09:24 +0000 Received: by mail-oi1-x236.google.com with SMTP id t204so4890914oie.7 for ; Thu, 24 Jan 2019 06:09:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=aX2NLJEU00RQceKNREggKr18va0iis+cewCty3LeUMs=; b=uXsdYnS/g+j9CBgkqPZUpJwf/pRJdG5l6I9KLkNuu92zXnYxuzZfEbrElWbevQsbR3 ProVeBDMIlqizzjOKKR3xcjqJdJ51jsybBIgOVm6ZyHlwhRrjsqHxXj1nFR7+yUilbPa zjiZoY3QxEJsAS9z01dpZQInN3ynIyXelCLQkjlEgjhviFxIrBwXnnCp09smwIPONBWN WKOmYnyDF/M6IFfTzTbZLuQ82YJzmcm38IACMa+eVTMFnYa5mCz8tRhuSyzkoWYFa45o XZq+wiilyT/FX7DvyMGq7bzLSpbgvh8gs/m2YGqwJQRpK5fiAUxanNql14FQYjtp+ixp CQmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=aX2NLJEU00RQceKNREggKr18va0iis+cewCty3LeUMs=; b=Jt/Hot98/ScKqyLLzHac/jYa8ORlMbrIQq9AU7GyW5csgyy9lfFvd/FcTCMS21cjny zsg5e2VYnF4bGh9cbppr4TB4g7EvnFkLIg3OdaYI77UXFqGHGnRNfpwpbUuWLgvUTkWd votQSGCjwT1gfw0t1pcQ8N75lwSE5Kc1MvVe29i6i5EL8gGOkgYjNbQbK0czQJCY6RKU GqN2WFC71N44jbUH9xdSSj6TGORZrabCGxjwGisAKTGa61S29K4MxWSKfrD62qdg6Nqa fTCscNKa4GJjx7ym/A28iMRbUnYkBsBuQg0HAMsjyo3NO5YkTENgcdbBHcNmgSkCK4H9 G8VQ== X-Gm-Message-State: AJcUukcqd8/uIDeWYmrq1svGdZvoJ6lcoGELh3EqmIIQ+sisbNcNNW/z XPgN98b/Y5LcREWSGAuomoU3xxHr X-Google-Smtp-Source: ALg8bN7cZALmVViyWwTYqJiN9hr4t+PfSeZNhN6xW+03BpefJyP+wCAeAVsCVClxvin7+Z/3SHV5Pg== X-Received: by 2002:aca:4341:: with SMTP id q62mr1257931oia.2.1548338960524; Thu, 24 Jan 2019 06:09:20 -0800 (PST) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id v68sm10542796oie.16.2019.01.24.06.09.19 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 24 Jan 2019 06:09:19 -0800 (PST) Subject: Re: Casting Integer to Boolean in assignment To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: <3c598971-fa25-81f8-13d8-6e4ba0d6995b@gmail.com> Date: Thu, 24 Jan 2019 08:09:18 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------711203B8700F973A2D6A3908" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------711203B8700F973A2D6A3908 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 1/24/19 5: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 ? > > 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 ? I'm confused at the question.  You are happily casting 1 and 0 to boolean in the SELECT statements, and then grumbling when *not* casting them in the INSERT statements.  Thus, why aren't you casting during the INSERT statements? test=# INSERT INTO test1 VALUES (1::boolean); -- Angular momentum makes the world go 'round. --------------711203B8700F973A2D6A3908 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

On 1/24/19 5: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 ?

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 ?

I'm confused at the question.  You are happily casting 1 and 0 to boolean in the SELECT statements, and then grumbling when not casting them in the INSERT statements.  Thus, why aren't you casting during the INSERT statements?

test=# INSERT INTO test1 VALUES (1::boolean);


--
Angular momentum makes the world go 'round.
--------------711203B8700F973A2D6A3908--