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 1gmglg-0000fn-Fw for pgsql-general@arkaria.postgresql.org; Thu, 24 Jan 2019 15:18:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gmglf-0003kj-5f for pgsql-general@arkaria.postgresql.org; Thu, 24 Jan 2019 15:18:11 +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 1gmgld-0003kc-Eg for pgsql-general@lists.postgresql.org; Thu, 24 Jan 2019 15:18:10 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gmglV-0006bw-IO for pgsql-general@lists.postgresql.org; Thu, 24 Jan 2019 15:18:08 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id A736D21BF4; Thu, 24 Jan 2019 10:17:59 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 24 Jan 2019 10:17:59 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=O b4BBb94bH6S3UmJjZLXE2WCCR9z6Vk7l8tyz128+b0=; b=w2NcfzQ78kZ6Nbl1t b1tDJoko1vdYBZe6bZn4q6aMaERwgFkmJl2Qil6ZMDoPWpQgm1gWoK9qWpPEbtmV AT1J5mVftNl9eyE9OjGVJQLtyjtzeuTtf+VkZL0YKzd50FTE8HCCH0dIwJLlBj9S IR7wSsfyaBD0y4NxgdbvKgAOWQCqnLk2WSPgcsjQhzRhHS7A4bdx0Ljs+A61pV6U MHlaAY5QPhdRLv5rrfshZLsXg6jBRaPCCSVHr97c7pvaY/WyrOwQXs7NBF+1TiPe bqfW9uGLplb9xEB1J+3YMydR+T5ESxdwp7Wdc2Lg9VMe7EV1aCebPZDrWsx7+Hsu S1PqQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=Ob4BBb94bH6S3UmJjZLXE2WCCR9z6Vk7l8tyz128+ b0=; b=ujgEUzzyJdGw2nCxoOSx/jv2O8PHz492sj1Zdb2OnfIEGv8CaqeWCA4ST oSyhY4FPXSsARBiiAP1aE141kQEf9KUniBxNur95YEwZNO5FDzcuUh7l91b/bGUG W1tT+XI7Ny+S3qOV+FM25AEi6C4rcmVQGi8lkLt6q1U6UIhh9EYkcSQUS/koqpi/ uVCxQ2odIVXvpU3HBgP616gKCUehFZWBm6V8nPOs0gv5bnMB+yFMy6kiqlx9SvVS 1jKfOButlHcCGiDxnYC6eljCAzmOmxYfL77QzrwKvHL/dqqhZNPnCfERr2vKt8eE q6hiGjJhxtoFJiz6jaipKfb8i+71Q== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledriedvgdejfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfhuthenuceurghilhhouhhtmecufedt tdenucenucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpe etughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgv rhdrtghomheqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucfkphepje durddvuddvrddufeegrdefnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.2] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id 9B345E4668; Thu, 24 Jan 2019 10:17:58 -0500 (EST) Subject: Re: Casting Integer to Boolean in assignment To: Alexandre GRAIL , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: <67e16eaa-0975-63ca-5c4c-3e362455b2b9@aklaver.com> Date: Thu, 24 Jan 2019 07:17:57 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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