Skip to content

Commit 89ab5c4

Browse files
committed
Remove grotty special-case code in coerce_to_target_type() that
implemented casts to varchar and bpchar using a cast-to-text function. This is a holdover from before we had pg_cast; it now makes more sense to just list these casts in pg_cast. While at it, add pg_cast entries for the other direction (casts from varchar/bpchar) where feasible.
1 parent 64fe1fd commit 89ab5c4

File tree

5 files changed

+101
-53
lines changed

5 files changed

+101
-53
lines changed

src/backend/parser/parse_coerce.c

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/parser/parse_coerce.c,v 2.113 2003/12/17 19:49:39 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/parser/parse_coerce.c,v 2.114 2004/03/15 01:13:40 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -63,50 +63,6 @@ coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype,
6363
if (can_coerce_type(1, &exprtype, &targettype, ccontext))
6464
expr = coerce_type(pstate, expr, exprtype, targettype,
6565
ccontext, cformat);
66-
else if (ccontext >= COERCION_ASSIGNMENT)
67-
{
68-
/*
69-
* String hacks to get transparent conversions for char and
70-
* varchar: if a coercion to text is available, use it for forced
71-
* coercions to char(n) or varchar(n) or domains thereof.
72-
*
73-
* This is pretty grotty, but seems easier to maintain than providing
74-
* entries in pg_cast that parallel all the ones for text.
75-
*/
76-
Oid targetbasetype = getBaseType(targettype);
77-
78-
if (targetbasetype == BPCHAROID || targetbasetype == VARCHAROID)
79-
{
80-
Oid text_id = TEXTOID;
81-
82-
if (can_coerce_type(1, &exprtype, &text_id, ccontext))
83-
{
84-
expr = coerce_type(pstate, expr, exprtype, text_id,
85-
ccontext, cformat);
86-
if (targetbasetype != targettype)
87-
{
88-
/* need to coerce to domain over char or varchar */
89-
expr = coerce_to_domain(expr, targetbasetype, targettype,
90-
cformat);
91-
}
92-
else
93-
{
94-
/*
95-
* need a RelabelType if no typmod coercion will be
96-
* performed
97-
*/
98-
if (targettypmod < 0)
99-
expr = (Node *) makeRelabelType((Expr *) expr,
100-
targettype, -1,
101-
cformat);
102-
}
103-
}
104-
else
105-
expr = NULL;
106-
}
107-
else
108-
expr = NULL;
109-
}
11066
else
11167
expr = NULL;
11268

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-2003, 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.219 2004/02/14 20:16:17 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.220 2004/03/15 01:13:41 tgl Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200402141
56+
#define CATALOG_VERSION_NO 200403141
5757

5858
#endif

src/include/catalog/pg_cast.h

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Copyright (c) 2002-2003, PostgreSQL Global Development Group
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_cast.h,v 1.10 2003/11/29 22:40:58 pgsql Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_cast.h,v 1.11 2004/03/15 01:13:41 tgl Exp $
1111
*
1212
* NOTES
1313
* the genbki.sh script reads this file and generates .bki
@@ -168,11 +168,14 @@ DATA(insert ( 1042 1043 401 i ));
168168
DATA(insert ( 1043 25 0 i ));
169169
DATA(insert ( 1043 1042 0 i ));
170170
DATA(insert ( 18 25 946 i ));
171-
DATA(insert ( 18 1042 860 i ));
171+
DATA(insert ( 18 1042 860 a ));
172+
DATA(insert ( 18 1043 946 a ));
172173
DATA(insert ( 19 25 406 i ));
173-
DATA(insert ( 19 1042 408 i ));
174-
DATA(insert ( 19 1043 1401 i ));
174+
DATA(insert ( 19 1042 408 a ));
175+
DATA(insert ( 19 1043 1401 a ));
175176
DATA(insert ( 25 18 944 a ));
177+
DATA(insert ( 1042 18 944 a ));
178+
DATA(insert ( 1043 18 944 a ));
176179
DATA(insert ( 25 19 407 i ));
177180
DATA(insert ( 1042 19 409 i ));
178181
DATA(insert ( 1043 19 1400 i ));
@@ -281,4 +284,81 @@ DATA(insert ( 25 1266 938 e ));
281284
DATA(insert ( 1700 25 1688 i ));
282285
DATA(insert ( 25 1700 1686 e ));
283286

287+
/*
288+
* Cross-category casts to and from VARCHAR
289+
*
290+
* We support all the same casts as for TEXT, but none are implicit.
291+
*/
292+
DATA(insert ( 20 1043 1289 a ));
293+
DATA(insert ( 1043 20 1290 e ));
294+
DATA(insert ( 21 1043 113 a ));
295+
DATA(insert ( 1043 21 818 e ));
296+
DATA(insert ( 23 1043 112 a ));
297+
DATA(insert ( 1043 23 819 e ));
298+
DATA(insert ( 26 1043 114 a ));
299+
DATA(insert ( 1043 26 817 e ));
300+
DATA(insert ( 1043 650 1714 e ));
301+
DATA(insert ( 700 1043 841 a ));
302+
DATA(insert ( 1043 700 839 e ));
303+
DATA(insert ( 701 1043 840 a ));
304+
DATA(insert ( 1043 701 838 e ));
305+
DATA(insert ( 829 1043 752 e ));
306+
DATA(insert ( 1043 829 767 e ));
307+
DATA(insert ( 650 1043 730 e ));
308+
DATA(insert ( 869 1043 730 e ));
309+
DATA(insert ( 1043 869 1713 e ));
310+
DATA(insert ( 1082 1043 749 a ));
311+
DATA(insert ( 1043 1082 748 e ));
312+
DATA(insert ( 1083 1043 948 a ));
313+
DATA(insert ( 1043 1083 837 e ));
314+
DATA(insert ( 1114 1043 2034 a ));
315+
DATA(insert ( 1043 1114 2022 e ));
316+
DATA(insert ( 1184 1043 1192 a ));
317+
DATA(insert ( 1043 1184 1191 e ));
318+
DATA(insert ( 1186 1043 1193 a ));
319+
DATA(insert ( 1043 1186 1263 e ));
320+
DATA(insert ( 1266 1043 939 a ));
321+
DATA(insert ( 1043 1266 938 e ));
322+
DATA(insert ( 1700 1043 1688 a ));
323+
DATA(insert ( 1043 1700 1686 e ));
324+
325+
/*
326+
* Cross-category casts to and from BPCHAR
327+
*
328+
* A function supporting cast to TEXT/VARCHAR can be used for cast to BPCHAR,
329+
* but the other direction is okay only if the function treats trailing
330+
* blanks as insignificant. So this is a subset of the VARCHAR list.
331+
* (Arguably the holdouts should be fixed, but I'm not doing that now...)
332+
*/
333+
DATA(insert ( 20 1042 1289 a ));
334+
DATA(insert ( 1042 20 1290 e ));
335+
DATA(insert ( 21 1042 113 a ));
336+
DATA(insert ( 1042 21 818 e ));
337+
DATA(insert ( 23 1042 112 a ));
338+
DATA(insert ( 1042 23 819 e ));
339+
DATA(insert ( 26 1042 114 a ));
340+
DATA(insert ( 1042 26 817 e ));
341+
DATA(insert ( 700 1042 841 a ));
342+
DATA(insert ( 1042 700 839 e ));
343+
DATA(insert ( 701 1042 840 a ));
344+
DATA(insert ( 1042 701 838 e ));
345+
DATA(insert ( 829 1042 752 e ));
346+
DATA(insert ( 1042 829 767 e ));
347+
DATA(insert ( 650 1042 730 e ));
348+
DATA(insert ( 869 1042 730 e ));
349+
DATA(insert ( 1082 1042 749 a ));
350+
DATA(insert ( 1042 1082 748 e ));
351+
DATA(insert ( 1083 1042 948 a ));
352+
DATA(insert ( 1042 1083 837 e ));
353+
DATA(insert ( 1114 1042 2034 a ));
354+
DATA(insert ( 1042 1114 2022 e ));
355+
DATA(insert ( 1184 1042 1192 a ));
356+
DATA(insert ( 1042 1184 1191 e ));
357+
DATA(insert ( 1186 1042 1193 a ));
358+
DATA(insert ( 1042 1186 1263 e ));
359+
DATA(insert ( 1266 1042 939 a ));
360+
DATA(insert ( 1042 1266 938 e ));
361+
DATA(insert ( 1700 1042 1688 a ));
362+
DATA(insert ( 1042 1700 1686 e ));
363+
284364
#endif /* PG_CAST_H */

src/test/regress/expected/opr_sanity.out

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,17 @@ WHERE castsource = casttarget OR castsource = 0 OR casttarget = 0
239239
-- Look for cast functions that don't have the right signature. The
240240
-- argument and result types in pg_proc must be the same as, or binary
241241
-- compatible with, what it says in pg_cast.
242+
-- As a special case, we allow casts from CHAR(n) that use functions
243+
-- declared to take TEXT. This does not pass the binary-coercibility test
244+
-- because CHAR(n)-to-TEXT normally invokes rtrim(). However, the results
245+
-- are the same, so long as the function is one that ignores trailing blanks.
242246
SELECT c.*
243247
FROM pg_cast c, pg_proc p
244248
WHERE c.castfunc = p.oid AND
245249
(p.pronargs <> 1
246-
OR NOT binary_coercible(c.castsource, p.proargtypes[0])
250+
OR NOT (binary_coercible(c.castsource, p.proargtypes[0])
251+
OR (c.castsource = 'character'::regtype AND
252+
p.proargtypes[0] = 'text'::regtype))
247253
OR NOT binary_coercible(p.prorettype, c.casttarget));
248254
castsource | casttarget | castfunc | castcontext
249255
------------+------------+----------+-------------

src/test/regress/sql/opr_sanity.sql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,18 @@ WHERE castsource = casttarget OR castsource = 0 OR casttarget = 0
196196
-- Look for cast functions that don't have the right signature. The
197197
-- argument and result types in pg_proc must be the same as, or binary
198198
-- compatible with, what it says in pg_cast.
199+
-- As a special case, we allow casts from CHAR(n) that use functions
200+
-- declared to take TEXT. This does not pass the binary-coercibility test
201+
-- because CHAR(n)-to-TEXT normally invokes rtrim(). However, the results
202+
-- are the same, so long as the function is one that ignores trailing blanks.
199203

200204
SELECT c.*
201205
FROM pg_cast c, pg_proc p
202206
WHERE c.castfunc = p.oid AND
203207
(p.pronargs <> 1
204-
OR NOT binary_coercible(c.castsource, p.proargtypes[0])
208+
OR NOT (binary_coercible(c.castsource, p.proargtypes[0])
209+
OR (c.castsource = 'character'::regtype AND
210+
p.proargtypes[0] = 'text'::regtype))
205211
OR NOT binary_coercible(p.prorettype, c.casttarget));
206212

207213
-- Look for binary compatible casts that do not have the reverse

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