Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / src / backend / parser / parse_node.c
blob0a57b80d1761ffb421ee0ef14a6635c37cee4598
1 /*-------------------------------------------------------------------------
3 * parse_node.c
4 * various routines that make nodes for querytrees
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * $PostgreSQL$
13 *-------------------------------------------------------------------------
15 #include "postgres.h"
17 #include "access/heapam.h"
18 #include "catalog/pg_type.h"
19 #include "mb/pg_wchar.h"
20 #include "nodes/makefuncs.h"
21 #include "nodes/nodeFuncs.h"
22 #include "parser/parsetree.h"
23 #include "parser/parse_coerce.h"
24 #include "parser/parse_expr.h"
25 #include "parser/parse_relation.h"
26 #include "utils/builtins.h"
27 #include "utils/int8.h"
28 #include "utils/syscache.h"
29 #include "utils/varbit.h"
32 static void pcb_error_callback(void *arg);
36 * make_parsestate
37 * Allocate and initialize a new ParseState.
39 * Caller should eventually release the ParseState via free_parsestate().
41 ParseState *
42 make_parsestate(ParseState *parentParseState)
44 ParseState *pstate;
46 pstate = palloc0(sizeof(ParseState));
48 pstate->parentParseState = parentParseState;
50 /* Fill in fields that don't start at null/false/zero */
51 pstate->p_next_resno = 1;
53 if (parentParseState)
55 pstate->p_sourcetext = parentParseState->p_sourcetext;
56 pstate->p_variableparams = parentParseState->p_variableparams;
59 return pstate;
63 * free_parsestate
64 * Release a ParseState and any subsidiary resources.
66 void
67 free_parsestate(ParseState *pstate)
70 * Check that we did not produce too many resnos; at the very least we
71 * cannot allow more than 2^16, since that would exceed the range of a
72 * AttrNumber. It seems safest to use MaxTupleAttributeNumber.
74 if (pstate->p_next_resno - 1 > MaxTupleAttributeNumber)
75 ereport(ERROR,
76 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
77 errmsg("target lists can have at most %d entries",
78 MaxTupleAttributeNumber)));
80 if (pstate->p_target_relation != NULL)
81 heap_close(pstate->p_target_relation, NoLock);
83 pfree(pstate);
88 * parser_errposition
89 * Report a parse-analysis-time cursor position, if possible.
91 * This is expected to be used within an ereport() call. The return value
92 * is a dummy (always 0, in fact).
94 * The locations stored in raw parsetrees are byte offsets into the source
95 * string. We have to convert them to 1-based character indexes for reporting
96 * to clients. (We do things this way to avoid unnecessary overhead in the
97 * normal non-error case: computing character indexes would be much more
98 * expensive than storing token offsets.)
101 parser_errposition(ParseState *pstate, int location)
103 int pos;
105 /* No-op if location was not provided */
106 if (location < 0)
107 return 0;
108 /* Can't do anything if source text is not available */
109 if (pstate == NULL || pstate->p_sourcetext == NULL)
110 return 0;
111 /* Convert offset to character number */
112 pos = pg_mbstrlen_with_len(pstate->p_sourcetext, location) + 1;
113 /* And pass it to the ereport mechanism */
114 return errposition(pos);
119 * setup_parser_errposition_callback
120 * Arrange for non-parser errors to report an error position
122 * Sometimes the parser calls functions that aren't part of the parser
123 * subsystem and can't reasonably be passed a ParseState; yet we would
124 * like any errors thrown in those functions to be tagged with a parse
125 * error location. Use this function to set up an error context stack
126 * entry that will accomplish that. Usage pattern:
128 * declare a local variable "ParseCallbackState pcbstate"
129 * ...
130 * setup_parser_errposition_callback(&pcbstate, pstate, location);
131 * call function that might throw error;
132 * cancel_parser_errposition_callback(&pcbstate);
134 void
135 setup_parser_errposition_callback(ParseCallbackState *pcbstate,
136 ParseState *pstate, int location)
138 /* Setup error traceback support for ereport() */
139 pcbstate->pstate = pstate;
140 pcbstate->location = location;
141 pcbstate->errcontext.callback = pcb_error_callback;
142 pcbstate->errcontext.arg = (void *) pcbstate;
143 pcbstate->errcontext.previous = error_context_stack;
144 error_context_stack = &pcbstate->errcontext;
148 * Cancel a previously-set-up errposition callback.
150 void
151 cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
153 /* Pop the error context stack */
154 error_context_stack = pcbstate->errcontext.previous;
158 * Error context callback for inserting parser error location.
160 * Note that this will be called for *any* error occurring while the
161 * callback is installed. We avoid inserting an irrelevant error location
162 * if the error is a query cancel --- are there any other important cases?
164 static void
165 pcb_error_callback(void *arg)
167 ParseCallbackState *pcbstate = (ParseCallbackState *) arg;
169 if (geterrcode() != ERRCODE_QUERY_CANCELED)
170 (void) parser_errposition(pcbstate->pstate, pcbstate->location);
175 * make_var
176 * Build a Var node for an attribute identified by RTE and attrno
178 Var *
179 make_var(ParseState *pstate, RangeTblEntry *rte, int attrno, int location)
181 Var *result;
182 int vnum,
183 sublevels_up;
184 Oid vartypeid;
185 int32 type_mod;
187 vnum = RTERangeTablePosn(pstate, rte, &sublevels_up);
188 get_rte_attribute_type(rte, attrno, &vartypeid, &type_mod);
189 result = makeVar(vnum, attrno, vartypeid, type_mod, sublevels_up);
190 result->location = location;
191 return result;
195 * transformArrayType()
196 * Get the element type of an array type in preparation for subscripting
199 transformArrayType(Oid arrayType)
201 Oid elementType;
202 HeapTuple type_tuple_array;
203 Form_pg_type type_struct_array;
205 /* Get the type tuple for the array */
206 type_tuple_array = SearchSysCache(TYPEOID,
207 ObjectIdGetDatum(arrayType),
208 0, 0, 0);
209 if (!HeapTupleIsValid(type_tuple_array))
210 elog(ERROR, "cache lookup failed for type %u", arrayType);
211 type_struct_array = (Form_pg_type) GETSTRUCT(type_tuple_array);
213 /* needn't check typisdefined since this will fail anyway */
215 elementType = type_struct_array->typelem;
216 if (elementType == InvalidOid)
217 ereport(ERROR,
218 (errcode(ERRCODE_DATATYPE_MISMATCH),
219 errmsg("cannot subscript type %s because it is not an array",
220 format_type_be(arrayType))));
222 ReleaseSysCache(type_tuple_array);
224 return elementType;
228 * transformArraySubscripts()
229 * Transform array subscripting. This is used for both
230 * array fetch and array assignment.
232 * In an array fetch, we are given a source array value and we produce an
233 * expression that represents the result of extracting a single array element
234 * or an array slice.
236 * In an array assignment, we are given a destination array value plus a
237 * source value that is to be assigned to a single element or a slice of
238 * that array. We produce an expression that represents the new array value
239 * with the source data inserted into the right part of the array.
241 * pstate Parse state
242 * arrayBase Already-transformed expression for the array as a whole
243 * arrayType OID of array's datatype (should match type of arrayBase)
244 * elementType OID of array's element type (fetch with transformArrayType,
245 * or pass InvalidOid to do it here)
246 * elementTypMod typmod to be applied to array elements (if storing) or of
247 * the source array (if fetching)
248 * indirection Untransformed list of subscripts (must not be NIL)
249 * assignFrom NULL for array fetch, else transformed expression for source.
251 ArrayRef *
252 transformArraySubscripts(ParseState *pstate,
253 Node *arrayBase,
254 Oid arrayType,
255 Oid elementType,
256 int32 elementTypMod,
257 List *indirection,
258 Node *assignFrom)
260 bool isSlice = false;
261 List *upperIndexpr = NIL;
262 List *lowerIndexpr = NIL;
263 ListCell *idx;
264 ArrayRef *aref;
266 /* Caller may or may not have bothered to determine elementType */
267 if (!OidIsValid(elementType))
268 elementType = transformArrayType(arrayType);
271 * A list containing only single subscripts refers to a single array
272 * element. If any of the items are double subscripts (lower:upper), then
273 * the subscript expression means an array slice operation. In this case,
274 * we supply a default lower bound of 1 for any items that contain only a
275 * single subscript. We have to prescan the indirection list to see if
276 * there are any double subscripts.
278 foreach(idx, indirection)
280 A_Indices *ai = (A_Indices *) lfirst(idx);
282 if (ai->lidx != NULL)
284 isSlice = true;
285 break;
290 * Transform the subscript expressions.
292 foreach(idx, indirection)
294 A_Indices *ai = (A_Indices *) lfirst(idx);
295 Node *subexpr;
297 Assert(IsA(ai, A_Indices));
298 if (isSlice)
300 if (ai->lidx)
302 subexpr = transformExpr(pstate, ai->lidx);
303 /* If it's not int4 already, try to coerce */
304 subexpr = coerce_to_target_type(pstate,
305 subexpr, exprType(subexpr),
306 INT4OID, -1,
307 COERCION_ASSIGNMENT,
308 COERCE_IMPLICIT_CAST,
309 -1);
310 if (subexpr == NULL)
311 ereport(ERROR,
312 (errcode(ERRCODE_DATATYPE_MISMATCH),
313 errmsg("array subscript must have type integer"),
314 parser_errposition(pstate, exprLocation(ai->lidx))));
316 else
318 /* Make a constant 1 */
319 subexpr = (Node *) makeConst(INT4OID,
321 sizeof(int32),
322 Int32GetDatum(1),
323 false,
324 true); /* pass by value */
326 lowerIndexpr = lappend(lowerIndexpr, subexpr);
328 subexpr = transformExpr(pstate, ai->uidx);
329 /* If it's not int4 already, try to coerce */
330 subexpr = coerce_to_target_type(pstate,
331 subexpr, exprType(subexpr),
332 INT4OID, -1,
333 COERCION_ASSIGNMENT,
334 COERCE_IMPLICIT_CAST,
335 -1);
336 if (subexpr == NULL)
337 ereport(ERROR,
338 (errcode(ERRCODE_DATATYPE_MISMATCH),
339 errmsg("array subscript must have type integer"),
340 parser_errposition(pstate, exprLocation(ai->uidx))));
341 upperIndexpr = lappend(upperIndexpr, subexpr);
345 * If doing an array store, coerce the source value to the right type.
346 * (This should agree with the coercion done by transformAssignedExpr.)
348 if (assignFrom != NULL)
350 Oid typesource = exprType(assignFrom);
351 Oid typeneeded = isSlice ? arrayType : elementType;
352 Node *newFrom;
354 newFrom = coerce_to_target_type(pstate,
355 assignFrom, typesource,
356 typeneeded, elementTypMod,
357 COERCION_ASSIGNMENT,
358 COERCE_IMPLICIT_CAST,
359 -1);
360 if (newFrom == NULL)
361 ereport(ERROR,
362 (errcode(ERRCODE_DATATYPE_MISMATCH),
363 errmsg("array assignment requires type %s"
364 " but expression is of type %s",
365 format_type_be(typeneeded),
366 format_type_be(typesource)),
367 errhint("You will need to rewrite or cast the expression."),
368 parser_errposition(pstate, exprLocation(assignFrom))));
369 assignFrom = newFrom;
373 * Ready to build the ArrayRef node.
375 aref = makeNode(ArrayRef);
376 aref->refarraytype = arrayType;
377 aref->refelemtype = elementType;
378 aref->reftypmod = elementTypMod;
379 aref->refupperindexpr = upperIndexpr;
380 aref->reflowerindexpr = lowerIndexpr;
381 aref->refexpr = (Expr *) arrayBase;
382 aref->refassgnexpr = (Expr *) assignFrom;
384 return aref;
388 * make_const
390 * Convert a Value node (as returned by the grammar) to a Const node
391 * of the "natural" type for the constant. Note that this routine is
392 * only used when there is no explicit cast for the constant, so we
393 * have to guess what type is wanted.
395 * For string literals we produce a constant of type UNKNOWN ---- whose
396 * representation is the same as cstring, but it indicates to later type
397 * resolution that we're not sure yet what type it should be considered.
398 * Explicit "NULL" constants are also typed as UNKNOWN.
400 * For integers and floats we produce int4, int8, or numeric depending
401 * on the value of the number. XXX We should produce int2 as well,
402 * but additional cleanup is needed before we can do that; there are
403 * too many examples that fail if we try.
405 Const *
406 make_const(ParseState *pstate, Value *value, int location)
408 Const *con;
409 Datum val;
410 int64 val64;
411 Oid typeid;
412 int typelen;
413 bool typebyval;
414 ParseCallbackState pcbstate;
416 switch (nodeTag(value))
418 case T_Integer:
419 val = Int32GetDatum(intVal(value));
421 typeid = INT4OID;
422 typelen = sizeof(int32);
423 typebyval = true;
424 break;
426 case T_Float:
427 /* could be an oversize integer as well as a float ... */
428 if (scanint8(strVal(value), true, &val64))
431 * It might actually fit in int32. Probably only INT_MIN can
432 * occur, but we'll code the test generally just to be sure.
434 int32 val32 = (int32) val64;
436 if (val64 == (int64) val32)
438 val = Int32GetDatum(val32);
440 typeid = INT4OID;
441 typelen = sizeof(int32);
442 typebyval = true;
444 else
446 val = Int64GetDatum(val64);
448 typeid = INT8OID;
449 typelen = sizeof(int64);
450 typebyval = FLOAT8PASSBYVAL; /* int8 and float8 alike */
453 else
455 /* arrange to report location if numeric_in() fails */
456 setup_parser_errposition_callback(&pcbstate, pstate, location);
457 val = DirectFunctionCall3(numeric_in,
458 CStringGetDatum(strVal(value)),
459 ObjectIdGetDatum(InvalidOid),
460 Int32GetDatum(-1));
461 cancel_parser_errposition_callback(&pcbstate);
463 typeid = NUMERICOID;
464 typelen = -1; /* variable len */
465 typebyval = false;
467 break;
469 case T_String:
472 * We assume here that UNKNOWN's internal representation is the
473 * same as CSTRING
475 val = CStringGetDatum(strVal(value));
477 typeid = UNKNOWNOID; /* will be coerced later */
478 typelen = -2; /* cstring-style varwidth type */
479 typebyval = false;
480 break;
482 case T_BitString:
483 /* arrange to report location if bit_in() fails */
484 setup_parser_errposition_callback(&pcbstate, pstate, location);
485 val = DirectFunctionCall3(bit_in,
486 CStringGetDatum(strVal(value)),
487 ObjectIdGetDatum(InvalidOid),
488 Int32GetDatum(-1));
489 cancel_parser_errposition_callback(&pcbstate);
490 typeid = BITOID;
491 typelen = -1;
492 typebyval = false;
493 break;
495 case T_Null:
496 /* return a null const */
497 con = makeConst(UNKNOWNOID,
500 (Datum) 0,
501 true,
502 false);
503 con->location = location;
504 return con;
506 default:
507 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(value));
508 return NULL; /* keep compiler quiet */
511 con = makeConst(typeid,
512 -1, /* typmod -1 is OK for all cases */
513 typelen,
514 val,
515 false,
516 typebyval);
517 con->location = location;
519 return con;