summaryrefslogtreecommitdiff
path: root/src/backend/parser/parse_node.c
blob: 8625af7b96e71b8f35e43b6b43a752e7a46ea272 (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
/*-------------------------------------------------------------------------
 *
 * parse_node.c
 *	  various routines that make nodes for query plans
 *
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/parser/parse_node.c,v 1.55 2001/08/09 18:28:18 petere Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <ctype.h>
#include <errno.h>
#include <float.h>

#include "postgres.h"

#include "access/heapam.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_type.h"
#include "fmgr.h"
#include "nodes/makefuncs.h"
#include "parser/parse_coerce.h"
#include "parser/parse_expr.h"
#include "parser/parse_node.h"
#include "parser/parse_oper.h"
#include "parser/parse_relation.h"
#include "parser/parse_target.h"
#include "parser/parse_type.h"
#include "utils/builtins.h"
#include "utils/varbit.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"

static bool fitsInFloat(Value *value);


/* make_parsestate()
 * Allocate and initialize a new ParseState.
 * The CALLER is responsible for freeing the ParseState* returned.
 */
ParseState *
make_parsestate(ParseState *parentParseState)
{
	ParseState *pstate;

	pstate = palloc(sizeof(ParseState));
	MemSet(pstate, 0, sizeof(ParseState));

	pstate->parentParseState = parentParseState;
	pstate->p_last_resno = 1;

	return pstate;
}


/* make_operand()
 * Ensure argument type match by forcing conversion of constants.
 */
Node *
make_operand(char *opname,
			 Node *tree,
			 Oid orig_typeId,
			 Oid target_typeId)
{
	Node	   *result;

	if (tree != NULL)
	{
		/* must coerce? */
		if (target_typeId != orig_typeId)
			result = coerce_type(NULL, tree, orig_typeId, target_typeId, -1);
		else
			result = tree;
	}
	else
	{
		/* otherwise, this is a NULL value */
		result = (Node *) makeNullConst(target_typeId);
	}

	return result;
}	/* make_operand() */


/* make_op()
 * Operator construction.
 *
 * Transform operator expression ensuring type compatibility.
 * This is where some type conversion happens.
 */
Expr *
make_op(char *opname, Node *ltree, Node *rtree)
{
	Oid			ltypeId,
				rtypeId;
	Operator	tup;
	Form_pg_operator opform;
	Oper	   *newop;
	Node	   *left,
			   *right;
	Expr	   *result;

	ltypeId = (ltree == NULL) ? UNKNOWNOID : exprType(ltree);
	rtypeId = (rtree == NULL) ? UNKNOWNOID : exprType(rtree);

	/* right operator? */
	if (rtree == NULL)
	{
		tup = right_oper(opname, ltypeId);
		opform = (Form_pg_operator) GETSTRUCT(tup);
		left = make_operand(opname, ltree, ltypeId, opform->oprleft);
		right = NULL;
	}

	/* left operator? */
	else if (ltree == NULL)
	{
		tup = left_oper(opname, rtypeId);
		opform = (Form_pg_operator) GETSTRUCT(tup);
		right = make_operand(opname, rtree, rtypeId, opform->oprright);
		left = NULL;
	}

	/* otherwise, binary operator */
	else
	{
		tup = oper(opname, ltypeId, rtypeId, false);
		opform = (Form_pg_operator) GETSTRUCT(tup);
		left = make_operand(opname, ltree, ltypeId, opform->oprleft);
		right = make_operand(opname, rtree, rtypeId, opform->oprright);
	}

	newop = makeOper(oprid(tup),/* opno */
					 InvalidOid,/* opid */
					 opform->oprresult);		/* operator result type */

	result = makeNode(Expr);
	result->typeOid = opform->oprresult;
	result->opType = OP_EXPR;
	result->oper = (Node *) newop;

	if (!left)
		result->args = makeList1(right);
	else if (!right)
		result->args = makeList1(left);
	else
		result->args = makeList2(left, right);

	ReleaseSysCache(tup);

	return result;
}	/* make_op() */


/*
 * make_var
 *		Build a Var node for an attribute identified by RTE and attrno
 */
Var *
make_var(ParseState *pstate, RangeTblEntry *rte, int attrno)
{
	int			vnum,
				sublevels_up;
	Oid			vartypeid = 0;
	int32		type_mod = 0;

	vnum = RTERangeTablePosn(pstate, rte, &sublevels_up);

	if (rte->relid != InvalidOid)
	{
		/* Plain relation RTE --- get the attribute's type info */
		HeapTuple	tp;
		Form_pg_attribute att_tup;

		tp = SearchSysCache(ATTNUM,
							ObjectIdGetDatum(rte->relid),
							Int16GetDatum(attrno),
							0, 0);
		/* this shouldn't happen... */
		if (!HeapTupleIsValid(tp))
			elog(ERROR, "Relation %s does not have attribute %d",
				 rte->relname, attrno);
		att_tup = (Form_pg_attribute) GETSTRUCT(tp);
		vartypeid = att_tup->atttypid;
		type_mod = att_tup->atttypmod;
		ReleaseSysCache(tp);
	}
	else
	{
		/* Subselect RTE --- get type info from subselect's tlist */
		List	   *tlistitem;

		foreach(tlistitem, rte->subquery->targetList)
		{
			TargetEntry *te = (TargetEntry *) lfirst(tlistitem);

			if (te->resdom->resjunk || te->resdom->resno != attrno)
				continue;
			vartypeid = te->resdom->restype;
			type_mod = te->resdom->restypmod;
			break;
		}
		/* falling off end of list shouldn't happen... */
		if (tlistitem == NIL)
			elog(ERROR, "Subquery %s does not have attribute %d",
				 rte->eref->relname, attrno);
	}

	return makeVar(vnum, attrno, vartypeid, type_mod, sublevels_up);
}

/*
 * transformArraySubscripts()
 *		Transform array subscripting.  This is used for both
 *		array fetch and array assignment.
 *
 * In an array fetch, we are given a source array value and we produce an
 * expression that represents the result of extracting a single array element
 * or an array slice.
 *
 * In an array assignment, we are given a destination array value plus a
 * source value that is to be assigned to a single element or a slice of
 * that array.	We produce an expression that represents the new array value
 * with the source data inserted into the right part of the array.
 *
 * pstate		Parse state
 * arrayBase	Already-transformed expression for the array as a whole
 *				(may be NULL if we are handling an INSERT)
 * arrayType	OID of array's datatype
 * indirection	Untransformed list of subscripts (must not be NIL)
 * forceSlice	If true, treat subscript as array slice in all cases
 * assignFrom	NULL for array fetch, else transformed expression for source.
 */
ArrayRef   *
transformArraySubscripts(ParseState *pstate,
						 Node *arrayBase,
						 Oid arrayType,
						 List *indirection,
						 bool forceSlice,
						 Node *assignFrom)
{
	Oid			elementType,
				resultType;
	HeapTuple	type_tuple_array,
				type_tuple_element;
	Form_pg_type type_struct_array,
				type_struct_element;
	bool		isSlice = forceSlice;
	List	   *upperIndexpr = NIL;
	List	   *lowerIndexpr = NIL;
	List	   *idx;
	ArrayRef   *aref;

	/* Get the type tuple for the array */
	type_tuple_array = SearchSysCache(TYPEOID,
									  ObjectIdGetDatum(arrayType),
									  0, 0, 0);
	if (!HeapTupleIsValid(type_tuple_array))
		elog(ERROR, "transformArraySubscripts: Cache lookup failed for array type %u",
			 arrayType);
	type_struct_array = (Form_pg_type) GETSTRUCT(type_tuple_array);

	elementType = type_struct_array->typelem;
	if (elementType == InvalidOid)
		elog(ERROR, "transformArraySubscripts: type %s is not an array",
			 NameStr(type_struct_array->typname));

	/* Get the type tuple for the array element type */
	type_tuple_element = SearchSysCache(TYPEOID,
										ObjectIdGetDatum(elementType),
										0, 0, 0);
	if (!HeapTupleIsValid(type_tuple_element))
		elog(ERROR, "transformArraySubscripts: Cache lookup failed for array element type %u",
			 elementType);
	type_struct_element = (Form_pg_type) GETSTRUCT(type_tuple_element);

	/*
	 * A list containing only single subscripts refers to a single array
	 * element.  If any of the items are double subscripts (lower:upper),
	 * then the subscript expression means an array slice operation. In
	 * this case, we supply a default lower bound of 1 for any items that
	 * contain only a single subscript. The forceSlice parameter forces us
	 * to treat the operation as a slice, even if no lower bounds are
	 * mentioned.  Otherwise, we have to prescan the indirection list to
	 * see if there are any double subscripts.
	 */
	if (!isSlice)
	{
		foreach(idx, indirection)
		{
			A_Indices  *ai = (A_Indices *) lfirst(idx);

			if (ai->lidx != NULL)
			{
				isSlice = true;
				break;
			}
		}
	}

	/*
	 * The type represented by the subscript expression is the element
	 * type if we are fetching a single element, but it is the same as the
	 * array type if we are fetching a slice or storing.
	 */
	if (isSlice || assignFrom != NULL)
		resultType = arrayType;
	else
		resultType = elementType;

	/*
	 * Transform the subscript expressions.
	 */
	foreach(idx, indirection)
	{
		A_Indices  *ai = (A_Indices *) lfirst(idx);
		Node	   *subexpr;

		if (isSlice)
		{
			if (ai->lidx)
			{
				subexpr = transformExpr(pstate, ai->lidx, EXPR_COLUMN_FIRST);
				/* If it's not int4 already, try to coerce */
				subexpr = CoerceTargetExpr(pstate, subexpr, exprType(subexpr),
										   INT4OID, -1);
				if (subexpr == NULL)
					elog(ERROR, "array index expressions must be integers");
			}
			else
			{
				/* Make a constant 1 */
				subexpr = (Node *) makeConst(INT4OID,
											 sizeof(int32),
											 Int32GetDatum(1),
											 false,
											 true,		/* pass by value */
											 false,
											 false);
			}
			lowerIndexpr = lappend(lowerIndexpr, subexpr);
		}
		subexpr = transformExpr(pstate, ai->uidx, EXPR_COLUMN_FIRST);
		/* If it's not int4 already, try to coerce */
		subexpr = CoerceTargetExpr(pstate, subexpr, exprType(subexpr),
								   INT4OID, -1);
		if (subexpr == NULL)
			elog(ERROR, "array index expressions must be integers");
		upperIndexpr = lappend(upperIndexpr, subexpr);
	}

	/*
	 * If doing an array store, coerce the source value to the right type.
	 */
	if (assignFrom != NULL)
	{
		Oid			typesource = exprType(assignFrom);
		Oid			typeneeded = isSlice ? arrayType : elementType;

		if (typesource != InvalidOid)
		{
			if (typesource != typeneeded)
			{
				/* XXX fixme: need to get the array's atttypmod? */
				assignFrom = CoerceTargetExpr(pstate, assignFrom,
											  typesource, typeneeded,
											  -1);
				if (assignFrom == NULL)
					elog(ERROR, "Array assignment requires type '%s'"
						 " but expression is of type '%s'"
					"\n\tYou will need to rewrite or cast the expression",
						 format_type_be(typeneeded),
						 format_type_be(typesource));
			}
		}
	}

	/*
	 * Ready to build the ArrayRef node.
	 */
	aref = makeNode(ArrayRef);
	aref->refattrlength = type_struct_array->typlen;
	aref->refelemlength = type_struct_element->typlen;
	aref->refelemtype = resultType;		/* XXX should save element type
										 * too */
	aref->refelembyval = type_struct_element->typbyval;
	aref->refupperindexpr = upperIndexpr;
	aref->reflowerindexpr = lowerIndexpr;
	aref->refexpr = arrayBase;
	aref->refassgnexpr = assignFrom;

	ReleaseSysCache(type_tuple_array);
	ReleaseSysCache(type_tuple_element);

	return aref;
}

/*
 * make_const
 *
 *	Convert a Value node (as returned by the grammar) to a Const node
 *	of the "natural" type for the constant.  Note that this routine is
 *	only used when there is no explicit cast for the constant, so we
 *	have to guess what type is wanted.
 *
 *	For string literals we produce a constant of type UNKNOWN ---- whose
 *	representation is the same as text, but it indicates to later type
 *	resolution that we're not sure that it should be considered text.
 *	Explicit "NULL" constants are also typed as UNKNOWN.
 *
 *	For integers and floats we produce int4, float8, or numeric depending
 *	on the value of the number.  XXX In some cases it would be nice to take
 *	context into account when determining the type to convert to, but in
 *	other cases we can't delay the type choice.  One possibility is to invent
 *	a dummy type "UNKNOWNNUMERIC" that's treated similarly to UNKNOWN;
 *	that would allow us to do the right thing in examples like a simple
 *	INSERT INTO table (numericcolumn) VALUES (1.234), since we wouldn't
 *	have to resolve the unknown type until we knew the destination column
 *	type.  On the other hand UNKNOWN has considerable problems of its own.
 *	We would not like "SELECT 1.2 + 3.4" to claim it can't choose a type.
 */
Const *
make_const(Value *value)
{
	Datum		val;
	Oid			typeid;
	int			typelen;
	bool		typebyval;
	Const	   *con;

	switch (nodeTag(value))
	{
		case T_Integer:
			val = Int32GetDatum(intVal(value));

			typeid = INT4OID;
			typelen = sizeof(int32);
			typebyval = true;
			break;

		case T_Float:
			if (fitsInFloat(value))
			{
				val = Float8GetDatum(floatVal(value));

				typeid = FLOAT8OID;
				typelen = sizeof(float8);
				typebyval = false;		/* XXX might change someday */
			}
			else
			{
				val = DirectFunctionCall3(numeric_in,
										  CStringGetDatum(strVal(value)),
										  ObjectIdGetDatum(InvalidOid),
										  Int32GetDatum(-1));

				typeid = NUMERICOID;
				typelen = -1;	/* variable len */
				typebyval = false;
			}
			break;

		case T_String:
			val = DirectFunctionCall1(textin, CStringGetDatum(strVal(value)));

			typeid = UNKNOWNOID;/* will be coerced later */
			typelen = -1;		/* variable len */
			typebyval = false;
			break;

		case T_BitString:
			val = DirectFunctionCall3(bit_in,
									  CStringGetDatum(strVal(value)),
									  ObjectIdGetDatum(InvalidOid),
									  Int32GetDatum(-1));
			typeid = BITOID;
			typelen = -1;
			typebyval = false;
			break;

		default:
			elog(NOTICE, "make_const: unknown type %d", nodeTag(value));
			/* FALLTHROUGH */

		case T_Null:
			/* return a null const */
			con = makeConst(UNKNOWNOID,
							-1,
							(Datum) NULL,
							true,
							false,
							false,
							false);
			return con;
	}

	con = makeConst(typeid,
					typelen,
					val,
					false,
					typebyval,
					false,		/* not a set */
					false);		/* not coerced */

	return con;
}

/*
 * Decide whether a T_Float value fits in float8, or must be treated as
 * type "numeric".	We check the number of digits and check for overflow/
 * underflow.  (With standard compilation options, Postgres' NUMERIC type
 * can handle decimal exponents up to 1000, considerably more than most
 * implementations of float8, so this is a sensible test.)
 */
static bool
fitsInFloat(Value *value)
{
	const char *ptr;
	int			ndigits;
	char	   *endptr;

	/*
	 * Count digits, ignoring leading zeroes (but not trailing zeroes).
	 * DBL_DIG is the maximum safe number of digits for "double".
	 */
	ptr = strVal(value);
	while (*ptr == '+' || *ptr == '-' || *ptr == '0' || *ptr == '.')
		ptr++;
	ndigits = 0;
	for (; *ptr; ptr++)
	{
		if (isdigit((unsigned char) *ptr))
			ndigits++;
		else if (*ptr == 'e' || *ptr == 'E')
			break;				/* don't count digits in exponent */
	}
	if (ndigits > DBL_DIG)
		return false;

	/*
	 * Use strtod() to check for overflow/underflow.
	 */
	errno = 0;
	(void) strtod(strVal(value), &endptr);
	if (*endptr != '\0' || errno != 0)
		return false;

	return true;
}