I was surprised to learn that 2 is a valid boolean (thanks Berge):

# select 2::boolean;
 bool
──────
 t

... while '2' is not:

# select '2'::boolean;
ERROR:  22P02: invalid input syntax for type boolean: "2"
LINE 1: select '2'::boolean;
               ^
LOCATION:  boolin, bool.c:151


The first cast is the int4_bool function, but it isn't covered by the
regression tests at all. The attached patch adds tests.

Christoph
>From 5752d75122db323b4066dd604d0c7a19077641a6 Mon Sep 17 00:00:00 2001
From: Christoph Berg <m...@debian.org>
Date: Thu, 21 Dec 2023 11:43:28 +0100
Subject: [PATCH] Add tests for int4_bool

This cast was previously not covered at all by the regression tests.
---
 src/test/regress/expected/boolean.out | 19 +++++++++++++++++++
 src/test/regress/sql/boolean.sql      |  6 ++++++
 2 files changed, 25 insertions(+)

diff --git a/src/test/regress/expected/boolean.out b/src/test/regress/expected/boolean.out
index ee9c244bf8..ff9440df7e 100644
--- a/src/test/regress/expected/boolean.out
+++ b/src/test/regress/expected/boolean.out
@@ -566,6 +566,25 @@ SELECT isnul OR istrue OR isfalse FROM booltbl4;
  t
 (1 row)
 
+-- Casts
+SELECT 0::boolean;
+ bool 
+------
+ f
+(1 row)
+
+SELECT 1::boolean;
+ bool 
+------
+ t
+(1 row)
+
+SELECT 2::boolean;
+ bool 
+------
+ t
+(1 row)
+
 --
 -- Clean up
 -- Many tables are retained by the regression test, but these do not seem
diff --git a/src/test/regress/sql/boolean.sql b/src/test/regress/sql/boolean.sql
index bc9937d692..cde6cd3576 100644
--- a/src/test/regress/sql/boolean.sql
+++ b/src/test/regress/sql/boolean.sql
@@ -251,6 +251,12 @@ SELECT istrue OR isfalse OR isnul FROM booltbl4;
 SELECT isnul OR istrue OR isfalse FROM booltbl4;
 
 
+-- Casts
+SELECT 0::boolean;
+SELECT 1::boolean;
+SELECT 2::boolean;
+
+
 --
 -- Clean up
 -- Many tables are retained by the regression test, but these do not seem
-- 
2.43.0

Reply via email to