1 /*-------------------------------------------------------------------------
4 * various routines that make nodes for querytrees
6 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/parser/parse_node.c
13 *-------------------------------------------------------------------------
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/lsyscache.h"
29 #include "utils/syscache.h"
30 #include "utils/varbit.h"
33 static void pcb_error_callback(void *arg);
38 * Allocate and initialize a new ParseState.
40 * Caller should eventually release the ParseState via free_parsestate().
43 make_parsestate(ParseState *parentParseState)
47 pstate = palloc0(sizeof(ParseState));
49 pstate->parentParseState = parentParseState;
51 /* Fill in fields that don't start at null/false/zero */
52 pstate->p_next_resno = 1;
56 pstate->p_sourcetext = parentParseState->p_sourcetext;
57 /* all hooks are copied from parent */
58 pstate->p_pre_columnref_hook = parentParseState->p_pre_columnref_hook;
59 pstate->p_post_columnref_hook = parentParseState->p_post_columnref_hook;
60 pstate->p_paramref_hook = parentParseState->p_paramref_hook;
61 pstate->p_coerce_param_hook = parentParseState->p_coerce_param_hook;
62 pstate->p_ref_hook_state = parentParseState->p_ref_hook_state;
70 * Release a ParseState and any subsidiary resources.
73 free_parsestate(ParseState *pstate)
76 * Check that we did not produce too many resnos; at the very least we
77 * cannot allow more than 2^16, since that would exceed the range of a
78 * AttrNumber. It seems safest to use MaxTupleAttributeNumber.
80 if (pstate->p_next_resno - 1 > MaxTupleAttributeNumber)
82 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
83 errmsg("target lists can have at most %d entries",
84 MaxTupleAttributeNumber)));
86 if (pstate->p_target_relation != NULL)
87 heap_close(pstate->p_target_relation, NoLock);
95 * Report a parse-analysis-time cursor position, if possible.
97 * This is expected to be used within an ereport() call. The return value
98 * is a dummy (always 0, in fact).
100 * The locations stored in raw parsetrees are byte offsets into the source
101 * string. We have to convert them to 1-based character indexes for reporting
102 * to clients. (We do things this way to avoid unnecessary overhead in the
103 * normal non-error case: computing character indexes would be much more
104 * expensive than storing token offsets.)
107 parser_errposition(ParseState *pstate, int location)
111 /* No-op if location was not provided */
114 /* Can't do anything if source text is not available */
115 if (pstate == NULL || pstate->p_sourcetext == NULL)
117 /* Convert offset to character number */
118 pos = pg_mbstrlen_with_len(pstate->p_sourcetext, location) + 1;
119 /* And pass it to the ereport mechanism */
120 return errposition(pos);
125 * setup_parser_errposition_callback
126 * Arrange for non-parser errors to report an error position
128 * Sometimes the parser calls functions that aren't part of the parser
129 * subsystem and can't reasonably be passed a ParseState; yet we would
130 * like any errors thrown in those functions to be tagged with a parse
131 * error location. Use this function to set up an error context stack
132 * entry that will accomplish that. Usage pattern:
134 * declare a local variable "ParseCallbackState pcbstate"
136 * setup_parser_errposition_callback(&pcbstate, pstate, location);
137 * call function that might throw error;
138 * cancel_parser_errposition_callback(&pcbstate);
141 setup_parser_errposition_callback(ParseCallbackState *pcbstate,
142 ParseState *pstate, int location)
144 /* Setup error traceback support for ereport() */
145 pcbstate->pstate = pstate;
146 pcbstate->location = location;
147 pcbstate->errcontext.callback = pcb_error_callback;
148 pcbstate->errcontext.arg = (void *) pcbstate;
149 pcbstate->errcontext.previous = error_context_stack;
150 error_context_stack = &pcbstate->errcontext;
154 * Cancel a previously-set-up errposition callback.
157 cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
159 /* Pop the error context stack */
160 error_context_stack = pcbstate->errcontext.previous;
164 * Error context callback for inserting parser error location.
166 * Note that this will be called for *any* error occurring while the
167 * callback is installed. We avoid inserting an irrelevant error location
168 * if the error is a query cancel --- are there any other important cases?
171 pcb_error_callback(void *arg)
173 ParseCallbackState *pcbstate = (ParseCallbackState *) arg;
175 if (geterrcode() != ERRCODE_QUERY_CANCELED)
176 (void) parser_errposition(pcbstate->pstate, pcbstate->location);
182 * Build a Var node for an attribute identified by RTE and attrno
185 make_var(ParseState *pstate, RangeTblEntry *rte, int attrno, int location)
194 vnum = RTERangeTablePosn(pstate, rte, &sublevels_up);
195 get_rte_attribute_type(rte, attrno, &vartypeid, &type_mod, &varcollid);
196 result = makeVar(vnum, attrno, vartypeid, type_mod, varcollid, sublevels_up);
197 result->location = location;
202 * transformArrayType()
203 * Identify the types involved in a subscripting operation
205 * On entry, arrayType/arrayTypmod identify the type of the input value
206 * to be subscripted (which could be a domain type). These are modified
207 * if necessary to identify the actual array type and typmod, and the
208 * array's element type is returned. An error is thrown if the input isn't
212 transformArrayType(Oid *arrayType, int32 *arrayTypmod)
214 Oid origArrayType = *arrayType;
216 HeapTuple type_tuple_array;
217 Form_pg_type type_struct_array;
220 * If the input is a domain, smash to base type, and extract the actual
221 * typmod to be applied to the base type. Subscripting a domain is an
222 * operation that necessarily works on the base array type, not the domain
223 * itself. (Note that we provide no method whereby the creator of a
224 * domain over an array type could hide its ability to be subscripted.)
226 *arrayType = getBaseTypeAndTypmod(*arrayType, arrayTypmod);
228 /* Get the type tuple for the array */
229 type_tuple_array = SearchSysCache1(TYPEOID, ObjectIdGetDatum(*arrayType));
230 if (!HeapTupleIsValid(type_tuple_array))
231 elog(ERROR, "cache lookup failed for type %u", *arrayType);
232 type_struct_array = (Form_pg_type) GETSTRUCT(type_tuple_array);
234 /* needn't check typisdefined since this will fail anyway */
236 elementType = type_struct_array->typelem;
237 if (elementType == InvalidOid)
239 (errcode(ERRCODE_DATATYPE_MISMATCH),
240 errmsg("cannot subscript type %s because it is not an array",
241 format_type_be(origArrayType))));
243 ReleaseSysCache(type_tuple_array);
249 * transformArraySubscripts()
250 * Transform array subscripting. This is used for both
251 * array fetch and array assignment.
253 * In an array fetch, we are given a source array value and we produce an
254 * expression that represents the result of extracting a single array element
257 * In an array assignment, we are given a destination array value plus a
258 * source value that is to be assigned to a single element or a slice of
259 * that array. We produce an expression that represents the new array value
260 * with the source data inserted into the right part of the array.
262 * For both cases, if the source array is of a domain-over-array type,
263 * the result is of the base array type or its element type; essentially,
264 * we must fold a domain to its base type before applying subscripting.
267 * arrayBase Already-transformed expression for the array as a whole
268 * arrayType OID of array's datatype (should match type of arrayBase,
269 * or be the base type of arrayBase's domain type)
270 * elementType OID of array's element type (fetch with transformArrayType,
271 * or pass InvalidOid to do it here)
272 * arrayTypMod typmod for the array (which is also typmod for the elements)
273 * indirection Untransformed list of subscripts (must not be NIL)
274 * assignFrom NULL for array fetch, else transformed expression for source.
277 transformArraySubscripts(ParseState *pstate,
285 bool isSlice = false;
286 List *upperIndexpr = NIL;
287 List *lowerIndexpr = NIL;
292 * Caller may or may not have bothered to determine elementType. Note
293 * that if the caller did do so, arrayType/arrayTypMod must be as modified
294 * by transformArrayType, ie, smash domain to base type.
296 if (!OidIsValid(elementType))
297 elementType = transformArrayType(&arrayType, &arrayTypMod);
300 * A list containing only single subscripts refers to a single array
301 * element. If any of the items are double subscripts (lower:upper), then
302 * the subscript expression means an array slice operation. In this case,
303 * we supply a default lower bound of 1 for any items that contain only a
304 * single subscript. We have to prescan the indirection list to see if
305 * there are any double subscripts.
307 foreach(idx, indirection)
309 A_Indices *ai = (A_Indices *) lfirst(idx);
311 if (ai->lidx != NULL)
319 * Transform the subscript expressions.
321 foreach(idx, indirection)
323 A_Indices *ai = (A_Indices *) lfirst(idx);
326 Assert(IsA(ai, A_Indices));
331 subexpr = transformExpr(pstate, ai->lidx);
332 /* If it's not int4 already, try to coerce */
333 subexpr = coerce_to_target_type(pstate,
334 subexpr, exprType(subexpr),
337 COERCE_IMPLICIT_CAST,
341 (errcode(ERRCODE_DATATYPE_MISMATCH),
342 errmsg("array subscript must have type integer"),
343 parser_errposition(pstate, exprLocation(ai->lidx))));
347 /* Make a constant 1 */
348 subexpr = (Node *) makeConst(INT4OID,
354 true); /* pass by value */
356 lowerIndexpr = lappend(lowerIndexpr, subexpr);
358 subexpr = transformExpr(pstate, ai->uidx);
359 /* If it's not int4 already, try to coerce */
360 subexpr = coerce_to_target_type(pstate,
361 subexpr, exprType(subexpr),
364 COERCE_IMPLICIT_CAST,
368 (errcode(ERRCODE_DATATYPE_MISMATCH),
369 errmsg("array subscript must have type integer"),
370 parser_errposition(pstate, exprLocation(ai->uidx))));
371 upperIndexpr = lappend(upperIndexpr, subexpr);
375 * If doing an array store, coerce the source value to the right type.
376 * (This should agree with the coercion done by transformAssignedExpr.)
378 if (assignFrom != NULL)
380 Oid typesource = exprType(assignFrom);
381 Oid typeneeded = isSlice ? arrayType : elementType;
384 newFrom = coerce_to_target_type(pstate,
385 assignFrom, typesource,
386 typeneeded, arrayTypMod,
388 COERCE_IMPLICIT_CAST,
392 (errcode(ERRCODE_DATATYPE_MISMATCH),
393 errmsg("array assignment requires type %s"
394 " but expression is of type %s",
395 format_type_be(typeneeded),
396 format_type_be(typesource)),
397 errhint("You will need to rewrite or cast the expression."),
398 parser_errposition(pstate, exprLocation(assignFrom))));
399 assignFrom = newFrom;
403 * Ready to build the ArrayRef node.
405 aref = makeNode(ArrayRef);
406 aref->refarraytype = arrayType;
407 aref->refelemtype = elementType;
408 aref->reftypmod = arrayTypMod;
409 /* refcollid will be set by parse_collate.c */
410 aref->refupperindexpr = upperIndexpr;
411 aref->reflowerindexpr = lowerIndexpr;
412 aref->refexpr = (Expr *) arrayBase;
413 aref->refassgnexpr = (Expr *) assignFrom;
421 * Convert a Value node (as returned by the grammar) to a Const node
422 * of the "natural" type for the constant. Note that this routine is
423 * only used when there is no explicit cast for the constant, so we
424 * have to guess what type is wanted.
426 * For string literals we produce a constant of type UNKNOWN ---- whose
427 * representation is the same as cstring, but it indicates to later type
428 * resolution that we're not sure yet what type it should be considered.
429 * Explicit "NULL" constants are also typed as UNKNOWN.
431 * For integers and floats we produce int4, int8, or numeric depending
432 * on the value of the number. XXX We should produce int2 as well,
433 * but additional cleanup is needed before we can do that; there are
434 * too many examples that fail if we try.
437 make_const(ParseState *pstate, Value *value, int location)
445 ParseCallbackState pcbstate;
447 switch (nodeTag(value))
450 val = Int32GetDatum(intVal(value));
453 typelen = sizeof(int32);
458 /* could be an oversize integer as well as a float ... */
459 if (scanint8(strVal(value), true, &val64))
462 * It might actually fit in int32. Probably only INT_MIN can
463 * occur, but we'll code the test generally just to be sure.
465 int32 val32 = (int32) val64;
467 if (val64 == (int64) val32)
469 val = Int32GetDatum(val32);
472 typelen = sizeof(int32);
477 val = Int64GetDatum(val64);
480 typelen = sizeof(int64);
481 typebyval = FLOAT8PASSBYVAL; /* int8 and float8 alike */
486 /* arrange to report location if numeric_in() fails */
487 setup_parser_errposition_callback(&pcbstate, pstate, location);
488 val = DirectFunctionCall3(numeric_in,
489 CStringGetDatum(strVal(value)),
490 ObjectIdGetDatum(InvalidOid),
492 cancel_parser_errposition_callback(&pcbstate);
495 typelen = -1; /* variable len */
503 * We assume here that UNKNOWN's internal representation is the
506 val = CStringGetDatum(strVal(value));
508 typeid = UNKNOWNOID; /* will be coerced later */
509 typelen = -2; /* cstring-style varwidth type */
514 /* arrange to report location if bit_in() fails */
515 setup_parser_errposition_callback(&pcbstate, pstate, location);
516 val = DirectFunctionCall3(bit_in,
517 CStringGetDatum(strVal(value)),
518 ObjectIdGetDatum(InvalidOid),
520 cancel_parser_errposition_callback(&pcbstate);
527 /* return a null const */
528 con = makeConst(UNKNOWNOID,
535 con->location = location;
539 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(value));
540 return NULL; /* keep compiler quiet */
543 con = makeConst(typeid,
544 -1, /* typmod -1 is OK for all cases */
545 InvalidOid, /* all cases are uncollatable types */
550 con->location = location;