aboutsummaryrefslogtreecommitdiff
path: root/src/backend/parser/parse_coerce.c
blob: acdf0439c47cf8238d237a83bb07d7a026dd7d9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*-------------------------------------------------------------------------
 *
 * parse_coerce.c
 *		handle type coersions/conversions for parser
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.4 1998/08/14 16:06:52 thomas Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <string.h>
#include "postgres.h"
#include "utils/builtins.h"
#include "fmgr.h"
#include "nodes/makefuncs.h"

#include "parser/parse_expr.h"

#include "catalog/pg_type.h"
#include "parser/parse_type.h"
#include "parser/parse_target.h"
#include "parser/parse_coerce.h"
#include "utils/syscache.h"

Oid DemoteType(Oid inType);
Oid PromoteTypeToNext(Oid inType);


/* coerce_type()
 * Convert a function argument to a different type.
 */
Node *
coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId)
{
	Node   *result = NULL;
	Oid		infunc;
	Datum	val;

#ifdef PARSEDEBUG
printf("coerce_type: argument types are %d -> %d\n",
 inputTypeId, targetTypeId);
#endif

	if (targetTypeId == InvalidOid)
	{
#ifdef PARSEDEBUG
printf("coerce_type: apparent NULL target argument; suppress type conversion\n");
#endif
		result = node;
	}
	else if (inputTypeId != targetTypeId)
	{
		/* one of the known-good transparent conversions? then drop through... */
		if (IS_BINARY_COMPATIBLE(inputTypeId, targetTypeId))
		{
#ifdef PARSEDEBUG
printf("coerce_type: argument type %s is known to be convertible to type %s\n",
 typeidTypeName(inputTypeId), typeidTypeName(targetTypeId));
#endif
			result = node;
		}

		/* if not unknown input type, try for explicit conversion using functions... */
		else if (inputTypeId != UNKNOWNOID)
		{
			/* We already know there is a function which will do this, so let's use it */
			FuncCall *n = makeNode(FuncCall);
			n->funcname = typeidTypeName(targetTypeId);
			n->args = lcons(node, NIL);

#ifdef PARSEDEBUG
printf("coerce_type: construct function %s(%s)\n",
 typeidTypeName(targetTypeId), typeidTypeName(inputTypeId));
#endif

			result = transformExpr(pstate, (Node *) n, EXPR_COLUMN_FIRST);
		}
		else
		{
#ifdef PARSEDEBUG
printf("coerce_type: node is UNKNOWN type\n");
#endif
            if (nodeTag(node) == T_Const)
            {
				Const *con = (Const *) node;

				val = (Datum) textout((struct varlena *)
									   con->constvalue);
				infunc = typeidInfunc(targetTypeId);
				con = makeNode(Const);
				con->consttype = targetTypeId;
				con->constlen = typeLen(typeidType(targetTypeId));

				/* use "-1" for varchar() type */
				con->constvalue = (Datum) fmgr(infunc,
											   val,
											   typeidTypElem(targetTypeId),
											   -1);
				con->constisnull = false;
				con->constbyval = true;
				con->constisset = false;
				result = (Node *) con;
			}
			else
			{
#ifdef PARSEDEBUG
printf("coerce_type: should never get here!\n");
#endif
				result = node;
			}
		}
	}
	else
	{
#ifdef PARSEDEBUG
printf("coerce_type: argument type IDs %d match\n", inputTypeId);
#endif

		result = node;
	}

	return result;
} /* coerce_type() */


/* can_coerce_type()
 * Can input_typeids be coerced to func_typeids?
 *
 * There are a few types which are known apriori to be convertible.
 * We will check for those cases first, and then look for possible
 *  conversion functions.
 *
 * Notes:
 * This uses the same mechanism as the CAST() SQL construct in gram.y.
 * We should also check the function return type on candidate conversion
 *  routines just to be safe but we do not do that yet...
 * We need to have a zero-filled OID array here, otherwise the cache lookup fails.
 * - thomas 1998-03-31
 */
bool
can_coerce_type(int nargs, Oid *input_typeids, Oid *func_typeids)
{
	HeapTuple	ftup;
	int			i;
	Type		tp;
	Oid			oid_array[8];

	/* run through argument list... */
	for (i = 0; i < nargs; i++)
	{
#ifdef PARSEDEBUG
printf("can_coerce_type: argument #%d types are %d -> %d\n",
 i, input_typeids[i], func_typeids[i]);
#endif
		if (input_typeids[i] != func_typeids[i])
		{
			/* one of the known-good transparent conversions? then drop through... */
			if (IS_BINARY_COMPATIBLE(input_typeids[i], func_typeids[i]))
			{
#ifdef PARSEDEBUG
printf("can_coerce_type: argument #%d type %s is known to be convertible to type %s\n",
 i, typeidTypeName(input_typeids[i]), typeidTypeName(func_typeids[i]));
#endif
			}

			/* don't know what to do for the output type? then quit... */
			else if (func_typeids[i] == InvalidOid)
			{
#ifdef PARSEDEBUG
printf("can_coerce_type: output OID func_typeids[%d] is zero\n", i);
#endif
				return false;
			}

			/* don't know what to do for the input type? then quit... */
			else if (input_typeids[i] == InvalidOid)
			{
#ifdef PARSEDEBUG
printf("can_coerce_type: input OID input_typeids[%d] is zero\n", i);
#endif
				return false;
			}

			/* if not unknown input type, try for explicit conversion using functions... */
			else if (input_typeids[i] != UNKNOWNOID)
			{
				MemSet(&oid_array[0], 0, 8 * sizeof(Oid));
				oid_array[0] = input_typeids[i];

				/* look for a single-argument function named with the target type name */
				ftup = SearchSysCacheTuple(PRONAME,
										   PointerGetDatum(typeidTypeName(func_typeids[i])),
										   Int32GetDatum(1),
										   PointerGetDatum(oid_array),
										   0);

				/* should also check the function return type just to be safe... */
				if (HeapTupleIsValid(ftup))
				{
#ifdef PARSEDEBUG
printf("can_coerce_type: found function %s(%s) to convert argument #%d\n",
 typeidTypeName(func_typeids[i]), typeidTypeName(input_typeids[i]), i);
#endif
				}
				else
				{
#ifdef PARSEDEBUG
printf("can_coerce_type: did not find function %s(%s) to convert argument #%d\n",
 typeidTypeName(func_typeids[i]), typeidTypeName(input_typeids[i]), i);
#endif
					return false;
				}
			}
			else
			{
#ifdef PARSEDEBUG
printf("can_coerce_type: argument #%d type is %d (UNKNOWN)\n",
 i, input_typeids[i]);
#endif
			}

			tp = typeidType(input_typeids[i]);
			if (typeTypeFlag(tp) == 'c')
			{
#ifdef PARSEDEBUG
printf("can_coerce_type: typeTypeFlag for %s is 'c'\n",
 typeidTypeName(input_typeids[i]));
#endif
				return false;
			}

#ifdef PARSEDEBUG
printf("can_coerce_type: conversion from %s to %s is possible\n",
 typeidTypeName(input_typeids[i]), typeidTypeName(func_typeids[i]));
#endif
		}
		else
		{
#ifdef PARSEDEBUG
printf("can_coerce_type: argument #%d type IDs %d match\n",
 i, input_typeids[i]);
#endif
		}
	}

	return true;
} /* can_coerce_type() */


/* TypeCategory()
 * Assign a category to the specified OID.
 */
CATEGORY
TypeCategory(Oid inType)
{
	CATEGORY result;

	switch (inType)
	{
		case (BOOLOID):
			result = BOOLEAN_TYPE;
			break;

		case (CHAROID):
		case (BPCHAROID):
		case (VARCHAROID):
		case (TEXTOID):
			result = STRING_TYPE;
			break;

		case (OIDOID):
		case (INT2OID):
		case (INT4OID):
		case (INT8OID):
		case (FLOAT4OID):
		case (FLOAT8OID):
		case (CASHOID):
			result = NUMERIC_TYPE;
			break;

		case (ABSTIMEOID):
		case (TIMESTAMPOID):
		case (DATETIMEOID):
			result = DATETIME_TYPE;
			break;

		case (RELTIMEOID):
		case (TIMESPANOID):
			result = TIMESPAN_TYPE;
			break;

		case (POINTOID):
		case (LSEGOID):
		case (LINEOID):
		case (BOXOID):
		case (PATHOID):
		case (CIRCLEOID):
		case (POLYGONOID):
			result = GEOMETRIC_TYPE;
			break;

		default:
			result = USER_TYPE;
			break;
	}
	return (result);
} /* TypeCategory() */


/* IsPreferredType()
 * Assign a category to the specified OID.
 */
bool
IsPreferredType(CATEGORY category, Oid type)
{
	return (type == PreferredType(category, type));
} /* IsPreferredType() */


/* PreferredType()
 * Return the preferred type OID for the specified category.
 */
Oid
PreferredType(CATEGORY category, Oid type)
{
	Oid		result;

	switch (category)
	{
		case (BOOLEAN_TYPE):
			result = BOOLOID;
			break;

		case (STRING_TYPE):
			result = TEXTOID;
			break;

		case (NUMERIC_TYPE):
			if (type == OIDOID)
				result = OIDOID;
			else
				result = FLOAT8OID;
			break;

		case (DATETIME_TYPE):
			result = DATETIMEOID;
			break;

		case (TIMESPAN_TYPE):
			result = TIMESPANOID;
			break;

		case (GEOMETRIC_TYPE):
		case (USER_TYPE):
			result = type;
			break;

		default:
			result = UNKNOWNOID;
			break;
	}
#ifdef PARSEDEBUG
printf("PreferredType- (%d) preferred type is %s\n", category, typeidTypeName(result));
#endif
	return (result);
} /* PreferredType() */


#if FALSE
Oid
PromoteTypeToNext(Oid inType)
{
	Oid result;

	switch (inType)
	{
		case (CHAROID):
		case (BPCHAROID):
			result = VARCHAROID;
			break;

		case (VARCHAROID):
			result = TEXTOID;
			break;

		case (INT2OID):
		case (CASHOID):
			result = INT4OID;
			break;

		case (INT4OID):
		case (INT8OID):
		case (FLOAT4OID):
			result = FLOAT8OID;
			break;

		case (DATEOID):
		case (ABSTIMEOID):
		case (TIMESTAMPOID):
			result = DATETIMEOID;
			break;

		case (TIMEOID):
		case (RELTIMEOID):
			result = TIMESPANOID;
			break;

		case (BOOLOID):
		case (TEXTOID):
		case (FLOAT8OID):
		case (DATETIMEOID):
		case (TIMESPANOID):
		default:
			result = inType;
			break;
	}
	return (result);
} /* PromoteTypeToNext() */


Oid
DemoteType(Oid inType)
{
	Oid result;

	switch (inType)
	{
		case (FLOAT4OID):
		case (FLOAT8OID):
			result = INT4OID;
			break;

		default:
			result = inType;
			break;
	}
	return (result);
} /* DemoteType() */


Oid
PromoteLesserType(Oid inType1, Oid inType2, Oid *newType1, Oid *newType2)
{
	Oid result;

	if (inType1 == inType2)
	{
		result = PromoteTypeToNext(inType1);
		inType1 = result;
		*arg2 = result;
		return (result);
	}

	kind1 = ClassifyType(inType1);
	kind2 = ClassifyType(*arg2);
	if (kind1 != kind2)
	{
		*newType1 = inType1;
		*newType2 = inType2;
		result = InvalidOid;
	}

	isBuiltIn1 = IS_BUILTIN_TYPE(inType1);
	isBuiltIn2 = IS_BUILTIN_TYPE(*arg2);

	if (isBuiltIn1 && isBuiltIn2)
	{
		switch (*arg1)
		{
			case (CHAROID):
			switch (*arg2)
			{
		case (BPCHAROID):
		case (VARCHAROID):
		case (TEXTOID):

		case (INT2OID):
		case (INT4OID):
		case (FLOAT4OID):
		case (FLOAT8OID):
		case (CASHOID):

		case (POINTOID):
		case (LSEGOID):
		case (LINEOID):
		case (BOXOID):
		case (PATHOID):
		case (CIRCLEOID):
		case (POLYGONOID):

		case (InvalidOid):
		case (UNKNOWNOID):
		case (BOOLOID):
		default:
			*arg1 = InvalidOid;
			*arg2 = InvalidOid;
			result = InvalidOid;
	}
	}
	else if (isBuiltIn1 && !isBuiltIn2)
	{
		if ((promotedType = PromoteBuiltInType(*arg1)) != *arg1)
		{
			*arg1 = promotedType;
			return (promotedType);
		}
		else if (CanCoerceType(*arg1, *arg2))
		{
			*arg1 = *arg2;
			return (*arg2);
		}
	}
	else if (!isBuiltIn1 && isBuiltIn2)
	{
		if ((promotedType = PromoteBuiltInType(*arg2)) != *arg2)
		{
			*arg2 = promotedType;
			return (promotedType);
		}
		else if (CanCoerceType(*arg2, *arg1))
		{
			*arg2 = *arg1;
			return (*arg1);
		}
	}


	if (*arg2 == InvalidOid)
		return InvalidOid;

	switch (*arg1)
	{
		case (CHAROID):
			switch (*arg2)
			{
		case (BPCHAROID):
		case (VARCHAROID):
		case (TEXTOID):

		case (INT2OID):
		case (INT4OID):
		case (FLOAT4OID):
		case (FLOAT8OID):
		case (CASHOID):

		case (POINTOID):
		case (LSEGOID):
		case (LINEOID):
		case (BOXOID):
		case (PATHOID):
		case (CIRCLEOID):
		case (POLYGONOID):

		case (InvalidOid):
		case (UNKNOWNOID):
		case (BOOLOID):
		default:
			*arg1 = InvalidOid;
			*arg2 = InvalidOid;
			result = InvalidOid;
	}
}
#endif