Skip to content

Commit 6c61af6

Browse files
committed
Remove arithmetic operators on the 1-byte-char datatype, as per proposals
made several times in the past. Add coercion functions between "char" and integer so that a workaround is possible if needed. Initdb forced.
1 parent 1ab4155 commit 6c61af6

File tree

7 files changed

+56
-53
lines changed

7 files changed

+56
-53
lines changed

doc/src/sgml/release.sgml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/release.sgml,v 1.299 2004/10/04 14:42:46 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/release.sgml,v 1.300 2004/10/04 22:49:47 tgl Exp $
33
-->
44

55
<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
263263
</para>
264264
</listitem>
265265

266+
<listitem>
267+
<para>
268+
The arithmetic operators associated with the <quote>char</> data type
269+
have been removed.
270+
</para>
271+
</listitem>
272+
266273
<listitem>
267274
<para>
268275
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
12411248
</para>
12421249
</listitem>
12431250

1251+
<listitem>
1252+
<para>
1253+
The arithmetic operators associated with the <quote>char</> data type
1254+
have been removed.
1255+
</para>
1256+
<para>
1257+
Formerly, the parser would select these operators in many situations
1258+
where an <quote>unable to select an operator</> error would be more
1259+
appropriate, such as <literal>null * null</>. If you actually want
1260+
to do arithmetic on a <quote>char</> column, you can cast it to integer.
1261+
</para>
1262+
</listitem>
1263+
12441264
<listitem>
12451265
<para>
12461266
Syntax checking of array input values considerably tightened up (Joe)

src/backend/utils/adt/char.c

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,25 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/utils/adt/char.c,v 1.40 2004/08/29 04:12:51 momjian Exp $
12+
* $PostgreSQL: pgsql/src/backend/utils/adt/char.c,v 1.41 2004/10/04 22:49:51 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
1616
#include "postgres.h"
1717

18+
#include <limits.h>
19+
1820
#include "libpq/pqformat.h"
1921
#include "utils/builtins.h"
2022

23+
#ifndef SCHAR_MAX
24+
#define SCHAR_MAX (0x7F)
25+
#endif
26+
#ifndef SCHAR_MIN
27+
#define SCHAR_MIN (-SCHAR_MAX-1)
28+
#endif
29+
30+
2131
/*****************************************************************************
2232
* USER I/O ROUTINES *
2333
*****************************************************************************/
@@ -88,7 +98,7 @@ charsend(PG_FUNCTION_ARGS)
8898

8999
/*
90100
* NOTE: comparisons are done as though char is unsigned (uint8).
91-
* Arithmetic is done as though char is signed (int8).
101+
* Conversions to and from integer are done as though char is signed (int8).
92102
*
93103
* You wanted consistency?
94104
*/
@@ -147,45 +157,26 @@ charge(PG_FUNCTION_ARGS)
147157
PG_RETURN_BOOL((uint8) arg1 >= (uint8) arg2);
148158
}
149159

150-
Datum
151-
charpl(PG_FUNCTION_ARGS)
152-
{
153-
char arg1 = PG_GETARG_CHAR(0);
154-
char arg2 = PG_GETARG_CHAR(1);
155-
156-
PG_RETURN_CHAR((int8) arg1 + (int8) arg2);
157-
}
158160

159161
Datum
160-
charmi(PG_FUNCTION_ARGS)
162+
chartoi4(PG_FUNCTION_ARGS)
161163
{
162164
char arg1 = PG_GETARG_CHAR(0);
163-
char arg2 = PG_GETARG_CHAR(1);
164165

165-
PG_RETURN_CHAR((int8) arg1 - (int8) arg2);
166+
PG_RETURN_INT32((int32) ((int8) arg1));
166167
}
167168

168169
Datum
169-
charmul(PG_FUNCTION_ARGS)
170+
i4tochar(PG_FUNCTION_ARGS)
170171
{
171-
char arg1 = PG_GETARG_CHAR(0);
172-
char arg2 = PG_GETARG_CHAR(1);
173-
174-
PG_RETURN_CHAR((int8) arg1 * (int8) arg2);
175-
}
176-
177-
Datum
178-
chardiv(PG_FUNCTION_ARGS)
179-
{
180-
char arg1 = PG_GETARG_CHAR(0);
181-
char arg2 = PG_GETARG_CHAR(1);
172+
int32 arg1 = PG_GETARG_INT32(0);
182173

183-
if (arg2 == 0)
174+
if (arg1 < SCHAR_MIN || arg1 > SCHAR_MAX)
184175
ereport(ERROR,
185-
(errcode(ERRCODE_DIVISION_BY_ZERO),
186-
errmsg("division by zero")));
176+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
177+
errmsg("\"char\" out of range")));
187178

188-
PG_RETURN_CHAR((int8) arg1 / (int8) arg2);
179+
PG_RETURN_CHAR((int8) arg1);
189180
}
190181

191182

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.249 2004/08/29 04:13:04 momjian Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.250 2004/10/04 22:49:54 tgl Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200408031
56+
#define CATALOG_VERSION_NO 200410041
5757

5858
#endif

src/include/catalog/pg_cast.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* Copyright (c) 2002-2004, PostgreSQL Global Development Group
1212
*
13-
* $PostgreSQL: pgsql/src/include/catalog/pg_cast.h,v 1.15 2004/08/29 05:06:55 momjian Exp $
13+
* $PostgreSQL: pgsql/src/include/catalog/pg_cast.h,v 1.16 2004/10/04 22:49:54 tgl Exp $
1414
*
1515
* NOTES
1616
* the genbki.sh script reads this file and generates .bki
@@ -182,6 +182,9 @@ DATA(insert ( 1043 18 944 a ));
182182
DATA(insert ( 25 19 407 i ));
183183
DATA(insert ( 1042 19 409 i ));
184184
DATA(insert ( 1043 19 1400 i ));
185+
/* Cross-category casts between int4 and "char" */
186+
DATA(insert ( 18 23 77 e ));
187+
DATA(insert ( 23 18 78 e ));
185188

186189
/*
187190
* Datetime category

src/include/catalog/pg_operator.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $PostgreSQL: pgsql/src/include/catalog/pg_operator.h,v 1.128 2004/08/29 05:06:55 momjian Exp $
11+
* $PostgreSQL: pgsql/src/include/catalog/pg_operator.h,v 1.129 2004/10/04 22:49:54 tgl Exp $
1212
*
1313
* NOTES
1414
* 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
299299
DATA(insert OID = 633 ( ">" PGNSP PGUID b f 18 18 16 631 632 0 0 0 0 chargt scalargtsel scalargtjoinsel ));
300300
DATA(insert OID = 634 ( ">=" PGNSP PGUID b f 18 18 16 632 631 0 0 0 0 charge scalargtsel scalargtjoinsel ));
301301

302-
DATA(insert OID = 635 ( "+" PGNSP PGUID b f 18 18 18 0 0 0 0 0 0 charpl - - ));
303-
DATA(insert OID = 636 ( "-" PGNSP PGUID b f 18 18 18 0 0 0 0 0 0 charmi - - ));
304-
DATA(insert OID = 637 ( "*" PGNSP PGUID b f 18 18 18 0 0 0 0 0 0 charmul - - ));
305-
DATA(insert OID = 638 ( "/" PGNSP PGUID b f 18 18 18 0 0 0 0 0 0 chardiv - - ));
306-
307302
DATA(insert OID = 639 ( "~" PGNSP PGUID b f 19 25 16 0 640 0 0 0 0 nameregexeq regexeqsel regexeqjoinsel ));
308303
#define OID_NAME_REGEXEQ_OP 639
309304
DATA(insert OID = 640 ( "!~" PGNSP PGUID b f 19 25 16 0 639 0 0 0 0 nameregexne regexnesel regexnejoinsel ));

src/include/catalog/pg_proc.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.346 2004/10/04 22:13:14 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.347 2004/10/04 22:49:55 tgl Exp $
1111
*
1212
* NOTES
1313
* 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
181181
DESCR("greater-than");
182182
DATA(insert OID = 74 ( charge PGNSP PGUID 12 f f t f i 2 16 "18 18" _null_ charge - _null_ ));
183183
DESCR("greater-than-or-equal");
184-
DATA(insert OID = 1248 ( charpl PGNSP PGUID 12 f f t f i 2 18 "18 18" _null_ charpl - _null_ ));
185-
DESCR("add");
186-
DATA(insert OID = 1250 ( charmi PGNSP PGUID 12 f f t f i 2 18 "18 18" _null_ charmi - _null_ ));
187-
DESCR("subtract");
188-
DATA(insert OID = 77 ( charmul PGNSP PGUID 12 f f t f i 2 18 "18 18" _null_ charmul - _null_ ));
189-
DESCR("multiply");
190-
DATA(insert OID = 78 ( chardiv PGNSP PGUID 12 f f t f i 2 18 "18 18" _null_ chardiv - _null_ ));
191-
DESCR("divide");
184+
DATA(insert OID = 77 ( int4 PGNSP PGUID 12 f f t f i 1 23 "18" _null_ chartoi4 - _null_ ));
185+
DESCR("convert char to int4");
186+
DATA(insert OID = 78 ( char PGNSP PGUID 12 f f t f i 1 18 "23" _null_ i4tochar - _null_ ));
187+
DESCR("convert int4 to char");
192188

193189
DATA(insert OID = 79 ( nameregexeq PGNSP PGUID 12 f f t f i 2 16 "19 25" _null_ nameregexeq - _null_ ));
194190
DESCR("matches regex., case-sensitive");

src/include/utils/builtins.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.250 2004/08/30 02:54:40 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.251 2004/10/04 22:49:59 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -88,10 +88,8 @@ extern Datum charlt(PG_FUNCTION_ARGS);
8888
extern Datum charle(PG_FUNCTION_ARGS);
8989
extern Datum chargt(PG_FUNCTION_ARGS);
9090
extern Datum charge(PG_FUNCTION_ARGS);
91-
extern Datum charpl(PG_FUNCTION_ARGS);
92-
extern Datum charmi(PG_FUNCTION_ARGS);
93-
extern Datum charmul(PG_FUNCTION_ARGS);
94-
extern Datum chardiv(PG_FUNCTION_ARGS);
91+
extern Datum chartoi4(PG_FUNCTION_ARGS);
92+
extern Datum i4tochar(PG_FUNCTION_ARGS);
9593
extern Datum text_char(PG_FUNCTION_ARGS);
9694
extern Datum char_text(PG_FUNCTION_ARGS);
9795

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy