]> granicus.if.org Git - postgresql/commitdiff
Remove arithmetic operators on the 1-byte-char datatype, as per proposals
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 4 Oct 2004 22:49:59 +0000 (22:49 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 4 Oct 2004 22:49:59 +0000 (22:49 +0000)
made several times in the past.  Add coercion functions between "char"
and integer so that a workaround is possible if needed.

Initdb forced.

doc/src/sgml/release.sgml
src/backend/utils/adt/char.c
src/include/catalog/catversion.h
src/include/catalog/pg_cast.h
src/include/catalog/pg_operator.h
src/include/catalog/pg_proc.h
src/include/utils/builtins.h

index 20ea24d3f0fc510eaceca2d2997ad1b42afcfe69..66ae275c3388c68855418e030ff533b69a68d2b3 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/release.sgml,v 1.299 2004/10/04 14:42:46 tgl Exp $
+$PostgreSQL: pgsql/doc/src/sgml/release.sgml,v 1.300 2004/10/04 22:49:47 tgl Exp $
 -->
 
 <appendix id="release">
@@ -263,6 +263,13 @@ $PostgreSQL: pgsql/doc/src/sgml/release.sgml,v 1.299 2004/10/04 14:42:46 tgl Exp
       </para>
      </listitem>
 
+     <listitem>
+      <para>
+       The arithmetic operators associated with the <quote>char</> data type
+       have been removed.
+      </para>
+     </listitem>
+
      <listitem>
       <para>
        The server now warns of empty strings passed to
@@ -1241,6 +1248,19 @@ $PostgreSQL: pgsql/doc/src/sgml/release.sgml,v 1.299 2004/10/04 14:42:46 tgl Exp
       </para>
      </listitem>
 
+     <listitem>
+      <para>
+       The arithmetic operators associated with the <quote>char</> data type
+       have been removed.
+      </para>
+      <para>
+       Formerly, the parser would select these operators in many situations
+       where an <quote>unable to select an operator</> error would be more
+       appropriate, such as <literal>null * null</>.  If you actually want
+       to do arithmetic on a <quote>char</> column, you can cast it to integer.
+      </para>
+     </listitem>
+
     <listitem>
       <para>
        Syntax checking of array input values considerably tightened up (Joe)
index c3ed8134d7a14ed1c5533a9e13dbfed0a9f3e767..1a5a70d430b4d030822879ed20cbc6f079eb6bfc 100644 (file)
@@ -9,15 +9,25 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/adt/char.c,v 1.40 2004/08/29 04:12:51 momjian Exp $
+ *       $PostgreSQL: pgsql/src/backend/utils/adt/char.c,v 1.41 2004/10/04 22:49:51 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 #include "postgres.h"
 
+#include <limits.h>
+
 #include "libpq/pqformat.h"
 #include "utils/builtins.h"
 
+#ifndef SCHAR_MAX
+#define SCHAR_MAX (0x7F)
+#endif
+#ifndef SCHAR_MIN
+#define SCHAR_MIN (-SCHAR_MAX-1)
+#endif
+
+
 /*****************************************************************************
  *      USER I/O ROUTINES                                                                                                               *
  *****************************************************************************/
@@ -88,7 +98,7 @@ charsend(PG_FUNCTION_ARGS)
 
 /*
  * NOTE: comparisons are done as though char is unsigned (uint8).
- * Arithmetic is done as though char is signed (int8).
+ * Conversions to and from integer are done as though char is signed (int8).
  *
  * You wanted consistency?
  */
@@ -147,45 +157,26 @@ charge(PG_FUNCTION_ARGS)
        PG_RETURN_BOOL((uint8) arg1 >= (uint8) arg2);
 }
 
-Datum
-charpl(PG_FUNCTION_ARGS)
-{
-       char            arg1 = PG_GETARG_CHAR(0);
-       char            arg2 = PG_GETARG_CHAR(1);
-
-       PG_RETURN_CHAR((int8) arg1 + (int8) arg2);
-}
 
 Datum
-charmi(PG_FUNCTION_ARGS)
+chartoi4(PG_FUNCTION_ARGS)
 {
        char            arg1 = PG_GETARG_CHAR(0);
-       char            arg2 = PG_GETARG_CHAR(1);
 
-       PG_RETURN_CHAR((int8) arg1 - (int8) arg2);
+       PG_RETURN_INT32((int32) ((int8) arg1));
 }
 
 Datum
-charmul(PG_FUNCTION_ARGS)
+i4tochar(PG_FUNCTION_ARGS)
 {
-       char            arg1 = PG_GETARG_CHAR(0);
-       char            arg2 = PG_GETARG_CHAR(1);
-
-       PG_RETURN_CHAR((int8) arg1 * (int8) arg2);
-}
-
-Datum
-chardiv(PG_FUNCTION_ARGS)
-{
-       char            arg1 = PG_GETARG_CHAR(0);
-       char            arg2 = PG_GETARG_CHAR(1);
+       int32           arg1 = PG_GETARG_INT32(0);
 
-       if (arg2 == 0)
+       if (arg1 < SCHAR_MIN || arg1 > SCHAR_MAX)
                ereport(ERROR,
-                               (errcode(ERRCODE_DIVISION_BY_ZERO),
-                                errmsg("division by zero")));
+                               (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+                                errmsg("\"char\" out of range")));
 
-       PG_RETURN_CHAR((int8) arg1 / (int8) arg2);
+       PG_RETURN_CHAR((int8) arg1);
 }
 
 
index 281c82e2d0b1ef96dda9b67d3e336d216e83624a..84765001870466394697113a8f942e49b05dc940 100644 (file)
@@ -37,7 +37,7 @@
  * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.249 2004/08/29 04:13:04 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.250 2004/10/04 22:49:54 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -53,6 +53,6 @@
  */
 
 /*                                                     yyyymmddN */
-#define CATALOG_VERSION_NO     200408031
+#define CATALOG_VERSION_NO     200410041
 
 #endif
index c99e60dca0477fb087dac5537a81d744d4ce27d6..df341ec15a17f2bf80658ca4ace5795e5e2c1b6f 100644 (file)
@@ -10,7 +10,7 @@
  *
  * Copyright (c) 2002-2004, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/include/catalog/pg_cast.h,v 1.15 2004/08/29 05:06:55 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/pg_cast.h,v 1.16 2004/10/04 22:49:54 tgl Exp $
  *
  * NOTES
  *       the genbki.sh script reads this file and generates .bki
@@ -182,6 +182,9 @@ DATA(insert ( 1043   18  944 a ));
 DATA(insert (  25       19  407 i ));
 DATA(insert ( 1042      19  409 i ));
 DATA(insert ( 1043      19 1400 i ));
+/* Cross-category casts between int4 and "char" */
+DATA(insert (  18       23   77 e ));
+DATA(insert (  23       18   78 e ));
 
 /*
  * Datetime category
index 7108ae51dd164ae512f3f5ae187e6dd102b9af40..f3f36e54b2f9bc34bce966bef5c326eb3a1ee128 100644 (file)
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/catalog/pg_operator.h,v 1.128 2004/08/29 05:06:55 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/pg_operator.h,v 1.129 2004/10/04 22:49:54 tgl Exp $
  *
  * NOTES
  *       the genbki.sh script reads this file and generates .bki
@@ -299,11 +299,6 @@ DATA(insert OID = 632 (  "<="         PGNSP PGUID b f      18      18      16 634 633      0 0 0 0 charle
 DATA(insert OID = 633 (  ">"      PGNSP PGUID b f      18      18      16 631 632      0 0 0 0 chargt scalargtsel scalargtjoinsel ));
 DATA(insert OID = 634 (  ">="     PGNSP PGUID b f      18      18      16 632 631      0 0 0 0 charge scalargtsel scalargtjoinsel ));
 
-DATA(insert OID = 635 (  "+"      PGNSP PGUID b f      18      18      18 0 0  0 0 0 0 charpl - - ));
-DATA(insert OID = 636 (  "-"      PGNSP PGUID b f      18      18      18 0 0  0 0 0 0 charmi - - ));
-DATA(insert OID = 637 (  "*"      PGNSP PGUID b f      18      18      18 0 0  0 0 0 0 charmul - - ));
-DATA(insert OID = 638 (  "/"      PGNSP PGUID b f      18      18      18 0 0  0 0 0 0 chardiv - - ));
-
 DATA(insert OID = 639 (  "~"      PGNSP PGUID b f      19      25      16 0 640        0 0   0   0 nameregexeq regexeqsel regexeqjoinsel ));
 #define OID_NAME_REGEXEQ_OP            639
 DATA(insert OID = 640 (  "!~"     PGNSP PGUID b f      19      25      16 0 639        0 0   0   0 nameregexne regexnesel regexnejoinsel ));
index 0721beec0cd426eeefe05099a247f671f5bc2a0b..8a525035fc0b35ee1e7d4c6df78808de1ff41e3d 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.346 2004/10/04 22:13:14 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.347 2004/10/04 22:49:55 tgl Exp $
  *
  * NOTES
  *       The script catalog/genbki.sh reads this file and generates .bki
@@ -181,14 +181,10 @@ DATA(insert OID =  73 (  chargt                      PGNSP PGUID 12 f f t f i 2 16 "18 18" _null
 DESCR("greater-than");
 DATA(insert OID =  74 (  charge                           PGNSP PGUID 12 f f t f i 2 16 "18 18" _null_ charge - _null_ ));
 DESCR("greater-than-or-equal");
-DATA(insert OID = 1248 (  charpl                  PGNSP PGUID 12 f f t f i 2 18 "18 18" _null_ charpl - _null_ ));
-DESCR("add");
-DATA(insert OID = 1250 (  charmi                  PGNSP PGUID 12 f f t f i 2 18 "18 18" _null_ charmi - _null_ ));
-DESCR("subtract");
-DATA(insert OID =  77 (  charmul                  PGNSP PGUID 12 f f t f i 2 18 "18 18" _null_ charmul - _null_ ));
-DESCR("multiply");
-DATA(insert OID =  78 (  chardiv                  PGNSP PGUID 12 f f t f i 2 18 "18 18" _null_ chardiv - _null_ ));
-DESCR("divide");
+DATA(insert OID =  77 (  int4                     PGNSP PGUID 12 f f t f i 1  23  "18" _null_  chartoi4 - _null_ ));
+DESCR("convert char to int4");
+DATA(insert OID =  78 (  char                     PGNSP PGUID 12 f f t f i 1  18  "23" _null_  i4tochar - _null_ ));
+DESCR("convert int4 to char");
 
 DATA(insert OID =  79 (  nameregexeq      PGNSP PGUID 12 f f t f i 2 16 "19 25" _null_ nameregexeq - _null_ ));
 DESCR("matches regex., case-sensitive");
index b1d7b9a8542eeeceefef3b6ae323288a5b36e12d..4b88596fdbac41811113af764531701ce50b6fae 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.250 2004/08/30 02:54:40 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.251 2004/10/04 22:49:59 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -88,10 +88,8 @@ extern Datum charlt(PG_FUNCTION_ARGS);
 extern Datum charle(PG_FUNCTION_ARGS);
 extern Datum chargt(PG_FUNCTION_ARGS);
 extern Datum charge(PG_FUNCTION_ARGS);
-extern Datum charpl(PG_FUNCTION_ARGS);
-extern Datum charmi(PG_FUNCTION_ARGS);
-extern Datum charmul(PG_FUNCTION_ARGS);
-extern Datum chardiv(PG_FUNCTION_ARGS);
+extern Datum chartoi4(PG_FUNCTION_ARGS);
+extern Datum i4tochar(PG_FUNCTION_ARGS);
 extern Datum text_char(PG_FUNCTION_ARGS);
 extern Datum char_text(PG_FUNCTION_ARGS);