1/*-------------------------------------------------------------------------
4 * various routines that make nodes for querytrees
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/parser/parse_node.c
13 *-------------------------------------------------------------------------
34 * Allocate and initialize a new ParseState.
36 * Caller should eventually release the ParseState via free_parsestate().
47 /* Fill in fields that don't start at null/false/zero */
54 /* all hooks are copied from parent */
60 /* query environment stays in context for the whole parse analysis */
69 * Release a ParseState and any subsidiary resources.
75 * Check that we did not produce too many resnos; at the very least we
76 * cannot allow more than 2^16, since that would exceed the range of a
77 * AttrNumber. It seems safest to use MaxTupleAttributeNumber.
81 (
errcode(ERRCODE_TOO_MANY_COLUMNS),
82 errmsg(
"target lists can have at most %d entries",
94 * Report a parse-analysis-time cursor position, if possible.
96 * This is expected to be used within an ereport() call. The return value
97 * is a dummy (always 0, in fact).
99 * The locations stored in raw parsetrees are byte offsets into the source
100 * string. We have to convert them to 1-based character indexes for reporting
101 * to clients. (We do things this way to avoid unnecessary overhead in the
102 * normal non-error case: computing character indexes would be much more
103 * expensive than storing token offsets.)
110 /* No-op if location was not provided */
113 /* Can't do anything if source text is not available */
116 /* Convert offset to character number */
118 /* And pass it to the ereport mechanism */
124 * setup_parser_errposition_callback
125 * Arrange for non-parser errors to report an error position
127 * Sometimes the parser calls functions that aren't part of the parser
128 * subsystem and can't reasonably be passed a ParseState; yet we would
129 * like any errors thrown in those functions to be tagged with a parse
130 * error location. Use this function to set up an error context stack
131 * entry that will accomplish that. Usage pattern:
133 * declare a local variable "ParseCallbackState pcbstate"
135 * setup_parser_errposition_callback(&pcbstate, pstate, location);
136 * call function that might throw error;
137 * cancel_parser_errposition_callback(&pcbstate);
143 /* Setup error traceback support for ereport() */
144 pcbstate->
pstate = pstate;
153 * Cancel a previously-set-up errposition callback.
158 /* Pop the error context stack */
163 * Error context callback for inserting parser error location.
165 * Note that this will be called for *any* error occurring while the
166 * callback is installed. We avoid inserting an irrelevant error location
167 * if the error is a query cancel --- are there any other important cases?
180 * transformContainerType()
181 * Identify the actual container type for a subscripting operation.
183 * containerType/containerTypmod are modified if necessary to identify
184 * the actual container type and typmod. This mainly involves smashing
185 * any domain to its base type, but there are some special considerations.
186 * Note that caller still needs to check if the result type is a container.
192 * If the input is a domain, smash to base type, and extract the actual
193 * typmod to be applied to the base type. Subscripting a domain is an
194 * operation that necessarily works on the base container type, not the
195 * domain itself. (Note that we provide no method whereby the creator of a
196 * domain over a container type could hide its ability to be subscripted.)
201 * We treat int2vector and oidvector as though they were domains over
202 * int2[] and oid[]. This is needed because array slicing could create an
203 * array that doesn't satisfy the dimensionality constraints of the
204 * xxxvector type; so we want the result of a slice operation to be
205 * considered to be of the more general type.
207 if (*containerType == INT2VECTOROID)
208 *containerType = INT2ARRAYOID;
209 else if (*containerType == OIDVECTOROID)
210 *containerType = OIDARRAYOID;
214 * transformContainerSubscripts()
215 * Transform container (array, etc) subscripting. This is used for both
216 * container fetch and container assignment.
218 * In a container fetch, we are given a source container value and we produce
219 * an expression that represents the result of extracting a single container
220 * element or a container slice.
222 * Container assignments are treated basically the same as container fetches
223 * here. The caller will modify the result node to insert the source value
224 * that is to be assigned to the element or slice that a fetch would have
225 * retrieved. The execution result will be a new container value with
226 * the source value inserted into the right part of the container.
228 * For both cases, if the source is of a domain-over-container type, the
229 * result is the same as if it had been of the container type; essentially,
230 * we must fold a domain to its base type before applying subscripting.
231 * (Note that int2vector and oidvector are treated as domains here.)
234 * containerBase Already-transformed expression for the container as a whole
235 * containerType OID of container's datatype (should match type of
236 * containerBase, or be the base type of containerBase's
238 * containerTypMod typmod for the container
239 * indirection Untransformed list of subscripts (must not be NIL)
240 * isAssignment True if this will become a container assignment.
246 int32 containerTypMod,
253 bool isSlice =
false;
257 * Determine the actual container type, smashing any domain. In the
258 * assignment case the caller already did this, since it also needs to
259 * know the actual container type.
265 * Verify that the container type is subscriptable, and get its support
266 * functions and typelem.
271 (
errcode(ERRCODE_DATATYPE_MISMATCH),
272 errmsg(
"cannot subscript type %s because it does not support subscripting",
277 * Detect whether any of the indirection items are slice specifiers.
279 * A list containing only simple subscripts refers to a single container
280 * element. If any of the items are slice specifiers (lower:upper), then
281 * the subscript expression means a container slice operation.
283 foreach(
idx, indirection)
295 * Ready to build the SubscriptingRef node.
299 sbsref->refcontainertype = containerType;
300 sbsref->refelemtype = elementType;
301 /* refrestype is to be set by container-specific logic */
302 sbsref->reftypmod = containerTypMod;
303 /* refcollid will be set by parse_collate.c */
304 /* refupperindexpr, reflowerindexpr are to be set by container logic */
306 sbsref->
refassgnexpr = NULL;
/* caller will fill if it's an assignment */
309 * Call the container-type-specific logic to transform the subscripts and
310 * determine the subscripting result type.
312 sbsroutines->
transform(sbsref, indirection, pstate,
313 isSlice, isAssignment);
316 * Verify we got a valid type (this defends, for example, against someone
317 * using array_subscript_handler as typsubscript without setting typelem).
321 (
errcode(ERRCODE_DATATYPE_MISMATCH),
322 errmsg(
"cannot subscript type %s because it does not support subscripting",
331 * Convert an A_Const node (as returned by the grammar) to a Const node
332 * of the "natural" type for the constant. Note that this routine is
333 * only used when there is no explicit cast for the constant, so we
334 * have to guess what type is wanted.
336 * For string literals we produce a constant of type UNKNOWN ---- whose
337 * representation is the same as cstring, but it indicates to later type
338 * resolution that we're not sure yet what type it should be considered.
339 * Explicit "NULL" constants are also typed as UNKNOWN.
341 * For integers and floats we produce int4, int8, or numeric depending
342 * on the value of the number. XXX We should produce int2 as well,
343 * but additional cleanup is needed before we can do that; there are
344 * too many examples that fail if we try.
358 /* return a null const */
376 typelen =
sizeof(
int32);
382 /* could be an oversize integer as well as a float ... */
391 * It might actually fit in int32. Probably only INT_MIN
392 * can occur, but we'll code the test generally just to be
397 if (val64 == (
int64) val32)
402 typelen =
sizeof(
int32);
410 typelen =
sizeof(
int64);
416 /* arrange to report location if numeric_in() fails */
425 typelen = -1;
/* variable len */
442 * We assume here that UNKNOWN's internal representation is the
447 typeid = UNKNOWNOID;
/* will be coerced later */
448 typelen = -2;
/* cstring-style varwidth type */
453 /* arrange to report location if bit_in() fails */
467 return NULL;
/* keep compiler quiet */
471 -1,
/* typmod -1 is OK for all cases */
472 InvalidOid,
/* all cases are uncollatable types */
Datum idx(PG_FUNCTION_ARGS)
Datum numeric_in(PG_FUNCTION_ARGS)
#define OidIsValid(objectId)
ErrorContextCallback * error_context_stack
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
int errposition(int cursorpos)
#define ereport(elevel,...)
#define DirectFunctionCall3(func, arg1, arg2, arg3)
#define MaxTupleAttributeNumber
const struct SubscriptRoutines * getSubscriptingRoutines(Oid typid, Oid *typelemp)
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
int pg_mbstrlen_with_len(const char *mbstr, int limit)
void pfree(void *pointer)
void * palloc0(Size size)
int exprLocation(const Node *expr)
int64 pg_strtoint64_safe(const char *s, Node *escontext)
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
void free_parsestate(ParseState *pstate)
int parser_errposition(ParseState *pstate, int location)
SubscriptingRef * transformContainerSubscripts(ParseState *pstate, Node *containerBase, Oid containerType, int32 containerTypMod, List *indirection, bool isAssignment)
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Const * make_const(ParseState *pstate, A_Const *aconst)
void transformContainerType(Oid *containerType, int32 *containerTypmod)
ParseState * make_parsestate(ParseState *parentParseState)
static void pcb_error_callback(void *arg)
#define lfirst_node(type, lc)
static Datum Int64GetDatum(int64 X)
static Datum BoolGetDatum(bool X)
static Datum ObjectIdGetDatum(Oid X)
static Datum CStringGetDatum(const char *X)
static Datum Int32GetDatum(int32 X)
struct ErrorContextCallback * previous
void(* callback)(void *arg)
ErrorContextCallback errcallback
ParseState * parentParseState
ParseParamRefHook p_paramref_hook
PreParseColumnRefHook p_pre_columnref_hook
QueryEnvironment * p_queryEnv
const char * p_sourcetext
Relation p_target_relation
CoerceParamHook p_coerce_param_hook
PostParseColumnRefHook p_post_columnref_hook
SubscriptTransform transform
void table_close(Relation relation, LOCKMODE lockmode)
Datum bit_in(PG_FUNCTION_ARGS)