parse_node.c 11.9 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * parse_node.c
4
 *	  various routines that make nodes for querytrees
5
 *
B
Bruce Momjian 已提交
6
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  $Header: /cvsroot/pgsql/src/backend/parser/parse_node.c,v 1.75 2002/12/12 15:49:39 tgl Exp $
12 13 14
 *
 *-------------------------------------------------------------------------
 */
15 16
#include "postgres.h"

17
#include "access/heapam.h"
18 19 20
#include "catalog/pg_operator.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
21
#include "parser/parsetree.h"
B
Bruce Momjian 已提交
22
#include "parser/parse_coerce.h"
23 24
#include "parser/parse_expr.h"
#include "parser/parse_node.h"
B
Bruce Momjian 已提交
25
#include "parser/parse_oper.h"
26
#include "parser/parse_relation.h"
27
#include "utils/builtins.h"
28
#include "utils/int8.h"
B
Bruce Momjian 已提交
29
#include "utils/lsyscache.h"
B
Bruce Momjian 已提交
30
#include "utils/syscache.h"
31
#include "utils/varbit.h"
32

33

34 35 36
/* make_parsestate()
 * Allocate and initialize a new ParseState.
 * The CALLER is responsible for freeing the ParseState* returned.
B
Bruce Momjian 已提交
37
 */
38
ParseState *
39
make_parsestate(ParseState *parentParseState)
40
{
41 42
	ParseState *pstate;

43
	pstate = palloc0(sizeof(ParseState));
44

45
	pstate->parentParseState = parentParseState;
46
	pstate->p_last_resno = 1;
47

48
	return pstate;
49 50
}

51 52 53 54

/* make_operand()
 * Ensure argument type match by forcing conversion of constants.
 */
55
Node *
56
make_operand(Node *tree, Oid orig_typeId, Oid target_typeId)
57
{
58
	Node	   *result;
59 60 61

	if (tree != NULL)
	{
62
		/* must coerce? */
63
		if (target_typeId != orig_typeId)
64 65
			result = coerce_type(tree, orig_typeId, target_typeId,
								 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
66 67
		else
			result = tree;
68 69 70
	}
	else
	{
71
		/* otherwise, this is a NULL value */
72
		result = (Node *) makeNullConst(target_typeId);
73
	}
74 75

	return result;
76
}	/* make_operand() */
77 78


79 80 81 82 83 84
/* make_op()
 * Operator construction.
 *
 * Transform operator expression ensuring type compatibility.
 * This is where some type conversion happens.
 */
85
Expr *
86
make_op(List *opname, Node *ltree, Node *rtree)
87
{
88 89
	Oid			ltypeId,
				rtypeId;
90
	Operator	tup;
91
	Form_pg_operator opform;
92 93
	Node	   *left,
			   *right;
94
	OpExpr	   *result;
95

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

99
	/* right operator? */
100 101
	if (rtree == NULL)
	{
102
		tup = right_oper(opname, ltypeId, false);
103
		opform = (Form_pg_operator) GETSTRUCT(tup);
104
		left = make_operand(ltree, ltypeId, opform->oprleft);
105 106
		right = NULL;
	}
107 108

	/* left operator? */
109 110
	else if (ltree == NULL)
	{
111
		tup = left_oper(opname, rtypeId, false);
112
		opform = (Form_pg_operator) GETSTRUCT(tup);
113
		right = make_operand(rtree, rtypeId, opform->oprright);
114 115
		left = NULL;
	}
116 117

	/* otherwise, binary operator */
118 119
	else
	{
120
		tup = oper(opname, ltypeId, rtypeId, false);
121
		opform = (Form_pg_operator) GETSTRUCT(tup);
122 123
		left = make_operand(ltree, ltypeId, opform->oprleft);
		right = make_operand(rtree, rtypeId, opform->oprright);
124 125
	}

126 127 128 129 130
	result = makeNode(OpExpr);
	result->opno = oprid(tup);
	result->opfuncid = InvalidOid;
	result->opresulttype = opform->oprresult;
	result->opretset = get_func_retset(opform->oprcode);
131 132

	if (!left)
133
		result->args = makeList1(right);
134
	else if (!right)
135
		result->args = makeList1(left);
136
	else
137
		result->args = makeList2(left, right);
138

139 140
	ReleaseSysCache(tup);

141
	return (Expr *) result;
142
}	/* make_op() */
143

144

145 146
/*
 * make_var
147
 *		Build a Var node for an attribute identified by RTE and attrno
148
 */
149
Var *
150
make_var(ParseState *pstate, RangeTblEntry *rte, int attrno)
151
{
152
	int			vnum,
153
				sublevels_up;
154 155
	Oid			vartypeid;
	int32		type_mod;
156 157

	vnum = RTERangeTablePosn(pstate, rte, &sublevels_up);
158
	get_rte_attribute_type(rte, attrno, &vartypeid, &type_mod);
159
	return makeVar(vnum, attrno, vartypeid, type_mod, sublevels_up);
160 161 162
}

/*
163 164 165
 * transformArraySubscripts()
 *		Transform array subscripting.  This is used for both
 *		array fetch and array assignment.
166
 *
167 168 169
 * 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.
170
 *
171 172
 * 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
173
 * that array.	We produce an expression that represents the new array value
174 175 176 177
 * 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
178 179
 *				(may be NULL if we are handling an INSERT)
 * arrayType	OID of array's datatype
180
 * arrayTypMod	typmod to be applied to array elements
181 182 183
 * 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.
184
 */
185
ArrayRef *
186 187
transformArraySubscripts(ParseState *pstate,
						 Node *arrayBase,
188
						 Oid arrayType,
189
						 int32 arrayTypMod,
190 191 192
						 List *indirection,
						 bool forceSlice,
						 Node *assignFrom)
193
{
194 195
	Oid			elementType,
				resultType;
196 197
	HeapTuple	type_tuple_array,
				type_tuple_element;
198
	Form_pg_type type_struct_array,
199
				type_struct_element;
200
	bool		isSlice = forceSlice;
201 202
	List	   *upperIndexpr = NIL;
	List	   *lowerIndexpr = NIL;
203 204
	List	   *idx;
	ArrayRef   *aref;
205

206
	/* Get the type tuple for the array */
207
	type_tuple_array = SearchSysCache(TYPEOID,
208
									  ObjectIdGetDatum(arrayType),
209 210
									  0, 0, 0);
	if (!HeapTupleIsValid(type_tuple_array))
211
		elog(ERROR, "transformArraySubscripts: Cache lookup failed for array type %u",
212
			 arrayType);
213
	type_struct_array = (Form_pg_type) GETSTRUCT(type_tuple_array);
214

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

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

229 230 231
	/*
	 * A list containing only single subscripts refers to a single array
	 * element.  If any of the items are double subscripts (lower:upper),
232 233 234 235 236 237
	 * 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.
238
	 */
239
	if (!isSlice)
240
	{
241
		foreach(idx, indirection)
242 243
		{
			A_Indices  *ai = (A_Indices *) lfirst(idx);
244

245 246 247 248 249 250
			if (ai->lidx != NULL)
			{
				isSlice = true;
				break;
			}
		}
251
	}
252

253 254 255 256
	/*
	 * 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.
257 258
	 */
	if (isSlice || assignFrom != NULL)
259
		resultType = arrayType;
260
	else
261
		resultType = elementType;
262 263

	/*
264
	 * Transform the subscript expressions.
265
	 */
266
	foreach(idx, indirection)
267 268 269
	{
		A_Indices  *ai = (A_Indices *) lfirst(idx);
		Node	   *subexpr;
270

271 272 273 274
		if (isSlice)
		{
			if (ai->lidx)
			{
B
Bruce Momjian 已提交
275
				subexpr = transformExpr(pstate, ai->lidx, NULL);
276
				/* If it's not int4 already, try to coerce */
277 278 279 280
				subexpr = coerce_to_target_type(subexpr, exprType(subexpr),
												INT4OID, -1,
												COERCION_ASSIGNMENT,
												COERCE_IMPLICIT_CAST);
281 282 283 284 285 286 287 288 289 290
				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,
291
											 true);		/* pass by value */
292 293 294
			}
			lowerIndexpr = lappend(lowerIndexpr, subexpr);
		}
B
Bruce Momjian 已提交
295
		subexpr = transformExpr(pstate, ai->uidx, NULL);
296
		/* If it's not int4 already, try to coerce */
297 298 299 300
		subexpr = coerce_to_target_type(subexpr, exprType(subexpr),
										INT4OID, -1,
										COERCION_ASSIGNMENT,
										COERCE_IMPLICIT_CAST);
301 302 303 304
		if (subexpr == NULL)
			elog(ERROR, "array index expressions must be integers");
		upperIndexpr = lappend(upperIndexpr, subexpr);
	}
305

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

314 315
		if (typesource != InvalidOid)
		{
316 317 318 319 320 321 322 323 324 325
			assignFrom = coerce_to_target_type(assignFrom, typesource,
											   typeneeded, arrayTypMod,
											   COERCION_ASSIGNMENT,
											   COERCE_IMPLICIT_CAST);
			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));
326 327
		}
	}
328

329 330 331
	/*
	 * Ready to build the ArrayRef node.
	 */
332
	aref = makeNode(ArrayRef);
333
	aref->refrestype = resultType;		/* XXX should save element type
334
										 * OID too */
335 336 337
	aref->refattrlength = type_struct_array->typlen;
	aref->refelemlength = type_struct_element->typlen;
	aref->refelembyval = type_struct_element->typbyval;
338
	aref->refelemalign = type_struct_element->typalign;
339 340
	aref->refupperindexpr = upperIndexpr;
	aref->reflowerindexpr = lowerIndexpr;
341 342
	aref->refexpr = (Expr *) arrayBase;
	aref->refassgnexpr = (Expr *) assignFrom;
343

344 345 346
	ReleaseSysCache(type_tuple_array);
	ReleaseSysCache(type_tuple_element);

347
	return aref;
348 349 350
}

/*
351
 * make_const
352
 *
353
 *	Convert a Value node (as returned by the grammar) to a Const node
354 355 356 357 358 359 360 361 362
 *	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.
 *
363 364 365 366
 *	For integers and floats we produce int4, int8, or numeric depending
 *	on the value of the number.  XXX This should include int2 as well,
 *	but additional cleanup is needed before we can do that; else cases
 *	like "WHERE int4var = 42" will fail to be indexable.
367
 */
368
Const *
369
make_const(Value *value)
370
{
371
	Datum		val;
372
	int64		val64;
373 374 375
	Oid			typeid;
	int			typelen;
	bool		typebyval;
376
	Const	   *con;
377

378 379
	switch (nodeTag(value))
	{
380 381
		case T_Integer:
			val = Int32GetDatum(intVal(value));
382 383 384 385

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

388
		case T_Float:
389 390
			/* could be an oversize integer as well as a float ... */
			if (scanint8(strVal(value), true, &val64))
391
			{
392
				val = Int64GetDatum(val64);
393

394 395
				typeid = INT8OID;
				typelen = sizeof(int64);
B
Bruce Momjian 已提交
396
				typebyval = false;		/* XXX might change someday */
397
			}
398 399
			else
			{
400 401 402 403
				val = DirectFunctionCall3(numeric_in,
										  CStringGetDatum(strVal(value)),
										  ObjectIdGetDatum(InvalidOid),
										  Int32GetDatum(-1));
404 405 406 407 408

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

411
		case T_String:
412 413
			val = DirectFunctionCall1(unknownin,
									  CStringGetDatum(strVal(value)));
414

415
			typeid = UNKNOWNOID;	/* will be coerced later */
416 417
			typelen = -1;		/* variable len */
			typebyval = false;
418
			break;
419

420
		case T_BitString:
421
			val = DirectFunctionCall3(bit_in,
422 423 424
									  CStringGetDatum(strVal(value)),
									  ObjectIdGetDatum(InvalidOid),
									  Int32GetDatum(-1));
425
			typeid = BITOID;
426 427 428 429
			typelen = -1;
			typebyval = false;
			break;

430
		default:
B
Bruce Momjian 已提交
431
			elog(WARNING, "make_const: unknown type %d", nodeTag(value));
432
			/* FALLTHROUGH */
433

434
		case T_Null:
435
			/* return a null const */
436 437 438 439 440
			con = makeConst(UNKNOWNOID,
							-1,
							(Datum) NULL,
							true,
							false);
441
			return con;
442 443
	}

444 445
	con = makeConst(typeid,
					typelen,
446 447
					val,
					false,
448
					typebyval);
449

450
	return con;
451
}