Is there any reason why the backend doesn't cast an unquoted integer to
a boolean value?
Hidden cross-category typecasts are evil. I'd accept this as an explicit cast ('e' in pg_cast) but not automatic.
Also, what about the other direction? Providing a cast in only one direction is pretty inconsistent.
test=> SELECT 1::BOOL, 0::BOOL, TRUE::INT4, FALSE::INT4; bool | bool | int4 | int4 ------+------+------+------ t | f | 1 | 0 (1 row)
Okey doke, both directions are now provided and the cast has to be explicit. -sc
Index: src/backend/utils/adt/int.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/utils/adt/int.c,v
retrieving revision 1.63
diff -u -r1.63 int.c
--- src/backend/utils/adt/int.c 4 Oct 2004 14:42:46 -0000 1.63
+++ src/backend/utils/adt/int.c 11 Oct 2004 18:28:41 -0000
@@ -361,6 +361,15 @@
return result;
}
+Datum
+int4_bool(PG_FUNCTION_ARGS)
+{
+ if (PG_GETARG_INT32(0) == 0)
+ PG_RETURN_BOOL(false);
+ else
+ PG_RETURN_BOOL(true);
+}
+
/*
* ============================
Index: src/backend/utils/adt/bool.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/utils/adt/bool.c,v
retrieving revision 1.35
diff -u -r1.35 bool.c
--- src/backend/utils/adt/bool.c 29 Aug 2004 05:06:49 -0000 1.35
+++ src/backend/utils/adt/bool.c 11 Oct 2004 18:28:41 -0000
@@ -127,6 +127,18 @@
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
+/*
+ * bool_int4 - Casts a boolean to an INT4
+ */
+Datum
+bool_int4(PG_FUNCTION_ARGS)
+{
+ if (PG_GETARG_INT32(0) == 0)
+ PG_RETURN_BOOL(false);
+ else
+ PG_RETURN_BOOL(true);
+}
+
/*****************************************************************************
* PUBLIC ROUTINES
*
Index: src/include/utils/builtins.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/utils/builtins.h,v
retrieving revision 1.251
diff -u -r1.251 builtins.h
--- src/include/utils/builtins.h 4 Oct 2004 22:49:59 -0000 1.251
+++ src/include/utils/builtins.h 11 Oct 2004 18:28:41 -0000
@@ -76,6 +76,7 @@
extern Datum isnotfalse(PG_FUNCTION_ARGS);
extern Datum booland_statefunc(PG_FUNCTION_ARGS);
extern Datum boolor_statefunc(PG_FUNCTION_ARGS);
+extern Datum bool_int4(PG_FUNCTION_ARGS);
/* char.c */
extern Datum charin(PG_FUNCTION_ARGS);
@@ -111,6 +112,7 @@
extern Datum i4toi2(PG_FUNCTION_ARGS);
extern Datum int2_text(PG_FUNCTION_ARGS);
extern Datum text_int2(PG_FUNCTION_ARGS);
+extern Datum int4_bool(PG_FUNCTION_ARGS);
extern Datum int4_text(PG_FUNCTION_ARGS);
extern Datum text_int4(PG_FUNCTION_ARGS);
extern Datum int4eq(PG_FUNCTION_ARGS);
Index: src/include/catalog/pg_cast.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/catalog/pg_cast.h,v
retrieving revision 1.16
diff -u -r1.16 pg_cast.h
--- src/include/catalog/pg_cast.h 4 Oct 2004 22:49:54 -0000 1.16
+++ src/include/catalog/pg_cast.h 11 Oct 2004 18:28:41 -0000
@@ -80,6 +80,7 @@
DATA(insert ( 21 700 236 i ));
DATA(insert ( 21 701 235 i ));
DATA(insert ( 21 1700 1782 i ));
+DATA(insert ( 23 16 2557 e ));
DATA(insert ( 23 20 481 i ));
DATA(insert ( 23 21 314 a ));
DATA(insert ( 23 700 318 i ));
@@ -381,4 +382,9 @@
DATA(insert ( 1562 1562 1687 i ));
DATA(insert ( 1700 1700 1703 i ));
+/*
+ * Misc. coercion functions (ex: bool)
+ */
+DATA(insert ( 16 23 2558 e ));
+
#endif /* PG_CAST_H */
Index: src/include/catalog/pg_proc.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/catalog/pg_proc.h,v
retrieving revision 1.348
diff -u -r1.348 pg_proc.h
--- src/include/catalog/pg_proc.h 7 Oct 2004 18:38:50 -0000 1.348
+++ src/include/catalog/pg_proc.h 11 Oct 2004 18:28:42 -0000
@@ -3604,6 +3604,11 @@
DATA(insert OID = 2556 ( pg_tablespace_databases PGNSP PGUID 12 f f t t s 1 26
"26" _null_ pg_tablespace_databases - _null_));
DESCR("returns database oids in a tablespace");
+DATA(insert OID = 2557 ( bool PGNSP PGUID 12 f f t f i 1
16 "23" _null_ int4_bool - _null_ ));
+DESCR("convert int4 to boolean");
+DATA(insert OID = 2558 ( int4 PGNSP PGUID 12 f f t f i 1
23 "16" _null_ bool_int4 - _null_ ));
+DESCR("convert boolean to int4");
+
/*
* Symbolic values for provolatile column: these indicate whether the result
-- Sean Chittenden
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to [EMAIL PROTECTED] so that your
message can get through to the mailing list cleanly
