PostgreSQL Source Code  git master
parse_node.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * parse_node.c
4  * various routines that make nodes for querytrees
5  *
6  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/parser/parse_node.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/htup_details.h"
18 #include "access/table.h"
19 #include "catalog/pg_type.h"
20 #include "mb/pg_wchar.h"
21 #include "nodes/makefuncs.h"
22 #include "nodes/nodeFuncs.h"
23 #include "nodes/subscripting.h"
24 #include "parser/parse_coerce.h"
25 #include "parser/parse_expr.h"
26 #include "parser/parse_relation.h"
27 #include "parser/parsetree.h"
28 #include "utils/builtins.h"
29 #include "utils/lsyscache.h"
30 #include "utils/syscache.h"
31 #include "utils/varbit.h"
32 
33 static void pcb_error_callback(void *arg);
34 
35 
36 /*
37  * make_parsestate
38  * Allocate and initialize a new ParseState.
39  *
40  * Caller should eventually release the ParseState via free_parsestate().
41  */
42 ParseState *
43 make_parsestate(ParseState *parentParseState)
44 {
45  ParseState *pstate;
46 
47  pstate = palloc0(sizeof(ParseState));
48 
49  pstate->parentParseState = parentParseState;
50 
51  /* Fill in fields that don't start at null/false/zero */
52  pstate->p_next_resno = 1;
53  pstate->p_resolve_unknowns = true;
54 
55  if (parentParseState)
56  {
57  pstate->p_sourcetext = parentParseState->p_sourcetext;
58  /* all hooks are copied from parent */
59  pstate->p_pre_columnref_hook = parentParseState->p_pre_columnref_hook;
60  pstate->p_post_columnref_hook = parentParseState->p_post_columnref_hook;
61  pstate->p_paramref_hook = parentParseState->p_paramref_hook;
62  pstate->p_coerce_param_hook = parentParseState->p_coerce_param_hook;
63  pstate->p_ref_hook_state = parentParseState->p_ref_hook_state;
64  /* query environment stays in context for the whole parse analysis */
65  pstate->p_queryEnv = parentParseState->p_queryEnv;
66  }
67 
68  return pstate;
69 }
70 
71 /*
72  * free_parsestate
73  * Release a ParseState and any subsidiary resources.
74  */
75 void
77 {
78  /*
79  * Check that we did not produce too many resnos; at the very least we
80  * cannot allow more than 2^16, since that would exceed the range of a
81  * AttrNumber. It seems safest to use MaxTupleAttributeNumber.
82  */
83  if (pstate->p_next_resno - 1 > MaxTupleAttributeNumber)
84  ereport(ERROR,
85  (errcode(ERRCODE_TOO_MANY_COLUMNS),
86  errmsg("target lists can have at most %d entries",
88 
89  if (pstate->p_target_relation != NULL)
91 
92  pfree(pstate);
93 }
94 
95 
96 /*
97  * parser_errposition
98  * Report a parse-analysis-time cursor position, if possible.
99  *
100  * This is expected to be used within an ereport() call. The return value
101  * is a dummy (always 0, in fact).
102  *
103  * The locations stored in raw parsetrees are byte offsets into the source
104  * string. We have to convert them to 1-based character indexes for reporting
105  * to clients. (We do things this way to avoid unnecessary overhead in the
106  * normal non-error case: computing character indexes would be much more
107  * expensive than storing token offsets.)
108  */
109 int
110 parser_errposition(ParseState *pstate, int location)
111 {
112  int pos;
113 
114  /* No-op if location was not provided */
115  if (location < 0)
116  return 0;
117  /* Can't do anything if source text is not available */
118  if (pstate == NULL || pstate->p_sourcetext == NULL)
119  return 0;
120  /* Convert offset to character number */
121  pos = pg_mbstrlen_with_len(pstate->p_sourcetext, location) + 1;
122  /* And pass it to the ereport mechanism */
123  return errposition(pos);
124 }
125 
126 
127 /*
128  * setup_parser_errposition_callback
129  * Arrange for non-parser errors to report an error position
130  *
131  * Sometimes the parser calls functions that aren't part of the parser
132  * subsystem and can't reasonably be passed a ParseState; yet we would
133  * like any errors thrown in those functions to be tagged with a parse
134  * error location. Use this function to set up an error context stack
135  * entry that will accomplish that. Usage pattern:
136  *
137  * declare a local variable "ParseCallbackState pcbstate"
138  * ...
139  * setup_parser_errposition_callback(&pcbstate, pstate, location);
140  * call function that might throw error;
141  * cancel_parser_errposition_callback(&pcbstate);
142  */
143 void
145  ParseState *pstate, int location)
146 {
147  /* Setup error traceback support for ereport() */
148  pcbstate->pstate = pstate;
149  pcbstate->location = location;
151  pcbstate->errcallback.arg = (void *) pcbstate;
153  error_context_stack = &pcbstate->errcallback;
154 }
155 
156 /*
157  * Cancel a previously-set-up errposition callback.
158  */
159 void
161 {
162  /* Pop the error context stack */
164 }
165 
166 /*
167  * Error context callback for inserting parser error location.
168  *
169  * Note that this will be called for *any* error occurring while the
170  * callback is installed. We avoid inserting an irrelevant error location
171  * if the error is a query cancel --- are there any other important cases?
172  */
173 static void
175 {
176  ParseCallbackState *pcbstate = (ParseCallbackState *) arg;
177 
178  if (geterrcode() != ERRCODE_QUERY_CANCELED)
179  (void) parser_errposition(pcbstate->pstate, pcbstate->location);
180 }
181 
182 
183 /*
184  * transformContainerType()
185  * Identify the actual container type for a subscripting operation.
186  *
187  * containerType/containerTypmod are modified if necessary to identify
188  * the actual container type and typmod. This mainly involves smashing
189  * any domain to its base type, but there are some special considerations.
190  * Note that caller still needs to check if the result type is a container.
191  */
192 void
193 transformContainerType(Oid *containerType, int32 *containerTypmod)
194 {
195  /*
196  * If the input is a domain, smash to base type, and extract the actual
197  * typmod to be applied to the base type. Subscripting a domain is an
198  * operation that necessarily works on the base container type, not the
199  * domain itself. (Note that we provide no method whereby the creator of a
200  * domain over a container type could hide its ability to be subscripted.)
201  */
202  *containerType = getBaseTypeAndTypmod(*containerType, containerTypmod);
203 
204  /*
205  * We treat int2vector and oidvector as though they were domains over
206  * int2[] and oid[]. This is needed because array slicing could create an
207  * array that doesn't satisfy the dimensionality constraints of the
208  * xxxvector type; so we want the result of a slice operation to be
209  * considered to be of the more general type.
210  */
211  if (*containerType == INT2VECTOROID)
212  *containerType = INT2ARRAYOID;
213  else if (*containerType == OIDVECTOROID)
214  *containerType = OIDARRAYOID;
215 }
216 
217 /*
218  * transformContainerSubscripts()
219  * Transform container (array, etc) subscripting. This is used for both
220  * container fetch and container assignment.
221  *
222  * In a container fetch, we are given a source container value and we produce
223  * an expression that represents the result of extracting a single container
224  * element or a container slice.
225  *
226  * Container assignments are treated basically the same as container fetches
227  * here. The caller will modify the result node to insert the source value
228  * that is to be assigned to the element or slice that a fetch would have
229  * retrieved. The execution result will be a new container value with
230  * the source value inserted into the right part of the container.
231  *
232  * For both cases, if the source is of a domain-over-container type, the
233  * result is the same as if it had been of the container type; essentially,
234  * we must fold a domain to its base type before applying subscripting.
235  * (Note that int2vector and oidvector are treated as domains here.)
236  *
237  * pstate Parse state
238  * containerBase Already-transformed expression for the container as a whole
239  * containerType OID of container's datatype (should match type of
240  * containerBase, or be the base type of containerBase's
241  * domain type)
242  * containerTypMod typmod for the container
243  * indirection Untransformed list of subscripts (must not be NIL)
244  * isAssignment True if this will become a container assignment.
245  */
248  Node *containerBase,
249  Oid containerType,
250  int32 containerTypMod,
251  List *indirection,
252  bool isAssignment)
253 {
254  SubscriptingRef *sbsref;
255  const SubscriptRoutines *sbsroutines;
256  Oid elementType;
257  bool isSlice = false;
258  ListCell *idx;
259 
260  /*
261  * Determine the actual container type, smashing any domain. In the
262  * assignment case the caller already did this, since it also needs to
263  * know the actual container type.
264  */
265  if (!isAssignment)
266  transformContainerType(&containerType, &containerTypMod);
267 
268  /*
269  * Verify that the container type is subscriptable, and get its support
270  * functions and typelem.
271  */
272  sbsroutines = getSubscriptingRoutines(containerType, &elementType);
273  if (!sbsroutines)
274  ereport(ERROR,
275  (errcode(ERRCODE_DATATYPE_MISMATCH),
276  errmsg("cannot subscript type %s because it does not support subscripting",
277  format_type_be(containerType)),
278  parser_errposition(pstate, exprLocation(containerBase))));
279 
280  /*
281  * Detect whether any of the indirection items are slice specifiers.
282  *
283  * A list containing only simple subscripts refers to a single container
284  * element. If any of the items are slice specifiers (lower:upper), then
285  * the subscript expression means a container slice operation.
286  */
287  foreach(idx, indirection)
288  {
290 
291  if (ai->is_slice)
292  {
293  isSlice = true;
294  break;
295  }
296  }
297 
298  /*
299  * Ready to build the SubscriptingRef node.
300  */
301  sbsref = makeNode(SubscriptingRef);
302 
303  sbsref->refcontainertype = containerType;
304  sbsref->refelemtype = elementType;
305  /* refrestype is to be set by container-specific logic */
306  sbsref->reftypmod = containerTypMod;
307  /* refcollid will be set by parse_collate.c */
308  /* refupperindexpr, reflowerindexpr are to be set by container logic */
309  sbsref->refexpr = (Expr *) containerBase;
310  sbsref->refassgnexpr = NULL; /* caller will fill if it's an assignment */
311 
312  /*
313  * Call the container-type-specific logic to transform the subscripts and
314  * determine the subscripting result type.
315  */
316  sbsroutines->transform(sbsref, indirection, pstate,
317  isSlice, isAssignment);
318 
319  /*
320  * Verify we got a valid type (this defends, for example, against someone
321  * using array_subscript_handler as typsubscript without setting typelem).
322  */
323  if (!OidIsValid(sbsref->refrestype))
324  ereport(ERROR,
325  (errcode(ERRCODE_DATATYPE_MISMATCH),
326  errmsg("cannot subscript type %s because it does not support subscripting",
327  format_type_be(containerType))));
328 
329  return sbsref;
330 }
331 
332 /*
333  * make_const
334  *
335  * Convert an A_Const node (as returned by the grammar) to a Const node
336  * of the "natural" type for the constant. Note that this routine is
337  * only used when there is no explicit cast for the constant, so we
338  * have to guess what type is wanted.
339  *
340  * For string literals we produce a constant of type UNKNOWN ---- whose
341  * representation is the same as cstring, but it indicates to later type
342  * resolution that we're not sure yet what type it should be considered.
343  * Explicit "NULL" constants are also typed as UNKNOWN.
344  *
345  * For integers and floats we produce int4, int8, or numeric depending
346  * on the value of the number. XXX We should produce int2 as well,
347  * but additional cleanup is needed before we can do that; there are
348  * too many examples that fail if we try.
349  */
350 Const *
351 make_const(ParseState *pstate, A_Const *aconst)
352 {
353  Const *con;
354  Datum val;
355  Oid typeid;
356  int typelen;
357  bool typebyval;
358  ParseCallbackState pcbstate;
359 
360  if (aconst->isnull)
361  {
362  /* return a null const */
363  con = makeConst(UNKNOWNOID,
364  -1,
365  InvalidOid,
366  -2,
367  (Datum) 0,
368  true,
369  false);
370  con->location = aconst->location;
371  return con;
372  }
373 
374  switch (nodeTag(&aconst->val))
375  {
376  case T_Integer:
377  val = Int32GetDatum(intVal(&aconst->val));
378 
379  typeid = INT4OID;
380  typelen = sizeof(int32);
381  typebyval = true;
382  break;
383 
384  case T_Float:
385  {
386  /* could be an oversize integer as well as a float ... */
387 
388  int base = 10;
389  char *startptr;
390  int sign;
391  char *testvalue;
392  int64 val64;
393  char *endptr;
394 
395  startptr = aconst->val.fval.fval;
396  if (startptr[0] == '-')
397  {
398  sign = -1;
399  startptr++;
400  }
401  else
402  sign = +1;
403  if (startptr[0] == '0')
404  {
405  if (startptr[1] == 'b' || startptr[1] == 'B')
406  {
407  base = 2;
408  startptr += 2;
409  }
410  else if (startptr[1] == 'o' || startptr[1] == 'O')
411  {
412  base = 8;
413  startptr += 2;
414  }
415  else if (startptr[1] == 'x' || startptr[1] == 'X')
416  {
417  base = 16;
418  startptr += 2;
419  }
420  }
421 
422  if (sign == +1)
423  testvalue = startptr;
424  else
425  testvalue = psprintf("-%s", startptr);
426  errno = 0;
427  val64 = strtoi64(testvalue, &endptr, base);
428  if (errno == 0 && *endptr == '\0')
429  {
430  /*
431  * It might actually fit in int32. Probably only INT_MIN
432  * can occur, but we'll code the test generally just to be
433  * sure.
434  */
435  int32 val32 = (int32) val64;
436 
437  if (val64 == (int64) val32)
438  {
439  val = Int32GetDatum(val32);
440 
441  typeid = INT4OID;
442  typelen = sizeof(int32);
443  typebyval = true;
444  }
445  else
446  {
447  val = Int64GetDatum(val64);
448 
449  typeid = INT8OID;
450  typelen = sizeof(int64);
451  typebyval = FLOAT8PASSBYVAL; /* int8 and float8 alike */
452  }
453  }
454  else
455  {
456  /* arrange to report location if numeric_in() fails */
457  setup_parser_errposition_callback(&pcbstate, pstate, aconst->location);
459  CStringGetDatum(aconst->val.fval.fval),
461  Int32GetDatum(-1));
463 
464  typeid = NUMERICOID;
465  typelen = -1; /* variable len */
466  typebyval = false;
467  }
468  break;
469  }
470 
471  case T_Boolean:
472  val = BoolGetDatum(boolVal(&aconst->val));
473 
474  typeid = BOOLOID;
475  typelen = 1;
476  typebyval = true;
477  break;
478 
479  case T_String:
480 
481  /*
482  * We assume here that UNKNOWN's internal representation is the
483  * same as CSTRING
484  */
485  val = CStringGetDatum(strVal(&aconst->val));
486 
487  typeid = UNKNOWNOID; /* will be coerced later */
488  typelen = -2; /* cstring-style varwidth type */
489  typebyval = false;
490  break;
491 
492  case T_BitString:
493  /* arrange to report location if bit_in() fails */
494  setup_parser_errposition_callback(&pcbstate, pstate, aconst->location);
496  CStringGetDatum(aconst->val.bsval.bsval),
498  Int32GetDatum(-1));
500  typeid = BITOID;
501  typelen = -1;
502  typebyval = false;
503  break;
504 
505  default:
506  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(&aconst->val));
507  return NULL; /* keep compiler quiet */
508  }
509 
510  con = makeConst(typeid,
511  -1, /* typmod -1 is OK for all cases */
512  InvalidOid, /* all cases are uncollatable types */
513  typelen,
514  val,
515  false,
516  typebyval);
517  con->location = aconst->location;
518 
519  return con;
520 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:627
signed int int32
Definition: c.h:478
#define strtoi64(str, endptr, base)
Definition: c.h:1284
#define FLOAT8PASSBYVAL
Definition: c.h:619
#define OidIsValid(objectId)
Definition: c.h:759
int geterrcode(void)
Definition: elog.c:1560
ErrorContextCallback * error_context_stack
Definition: elog.c:95
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
int errposition(int cursorpos)
Definition: elog.c:1445
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1794
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:646
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
#define MaxTupleAttributeNumber
Definition: htup_details.h:34
long val
Definition: informix.c:664
char sign
Definition: informix.c:668
#define NoLock
Definition: lockdefs.h:34
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2496
const struct SubscriptRoutines * getSubscriptingRoutines(Oid typid, Oid *typelemp)
Definition: lsyscache.c:3088
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:299
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:1058
void pfree(void *pointer)
Definition: mcxt.c:1436
void * palloc0(Size size)
Definition: mcxt.c:1241
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1243
#define nodeTag(nodeptr)
Definition: nodes.h:122
#define makeNode(_type_)
Definition: nodes.h:165
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:160
void free_parsestate(ParseState *pstate)
Definition: parse_node.c:76
SubscriptingRef * transformContainerSubscripts(ParseState *pstate, Node *containerBase, Oid containerType, int32 containerTypMod, List *indirection, bool isAssignment)
Definition: parse_node.c:247
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:144
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:43
void transformContainerType(Oid *containerType, int32 *containerTypmod)
Definition: parse_node.c:193
static void pcb_error_callback(void *arg)
Definition: parse_node.c:174
Const * make_const(ParseState *pstate, A_Const *aconst)
Definition: parse_node.c:351
void * arg
#define lfirst_node(type, lc)
Definition: pg_list.h:176
uintptr_t Datum
Definition: postgres.h:64
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
bool isnull
Definition: parsenodes.h:354
int location
Definition: parsenodes.h:355
union ValUnion val
Definition: parsenodes.h:353
bool is_slice
Definition: parsenodes.h:450
char * bsval
Definition: value.h:76
int location
Definition: primnodes.h:289
struct ErrorContextCallback * previous
Definition: elog.h:295
void(* callback)(void *arg)
Definition: elog.h:296
char * fval
Definition: value.h:52
Definition: pg_list.h:54
Definition: nodes.h:118
ErrorContextCallback errcallback
Definition: parse_node.h:327
ParseState * pstate
Definition: parse_node.h:325
ParseState * parentParseState
Definition: parse_node.h:184
void * p_ref_hook_state
Definition: parse_node.h:230
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:228
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:226
QueryEnvironment * p_queryEnv
Definition: parse_node.h:211
const char * p_sourcetext
Definition: parse_node.h:185
bool p_resolve_unknowns
Definition: parse_node.h:208
int p_next_resno
Definition: parse_node.h:203
Relation p_target_relation
Definition: parse_node.h:198
CoerceParamHook p_coerce_param_hook
Definition: parse_node.h:229
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:227
SubscriptTransform transform
Definition: subscripting.h:160
Expr * refassgnexpr
Definition: primnodes.h:595
Expr * refexpr
Definition: primnodes.h:593
Oid refcontainertype
Definition: primnodes.h:575
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
BitString bsval
Definition: parsenodes.h:345
Float fval
Definition: parsenodes.h:342
#define boolVal(v)
Definition: value.h:81
#define intVal(v)
Definition: value.h:79
#define strVal(v)
Definition: value.h:82
Datum bit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:147