PostgreSQL Source Code  git master
parse_node.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/table.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/subscripting.h"
#include "parser/parse_coerce.h"
#include "parser/parse_expr.h"
#include "parser/parse_relation.h"
#include "parser/parsetree.h"
#include "utils/builtins.h"
#include "utils/int8.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/varbit.h"
Include dependency graph for parse_node.c:

Go to the source code of this file.

Functions

static void pcb_error_callback (void *arg)
 
ParseStatemake_parsestate (ParseState *parentParseState)
 
void free_parsestate (ParseState *pstate)
 
int parser_errposition (ParseState *pstate, int location)
 
void setup_parser_errposition_callback (ParseCallbackState *pcbstate, ParseState *pstate, int location)
 
void cancel_parser_errposition_callback (ParseCallbackState *pcbstate)
 
void transformContainerType (Oid *containerType, int32 *containerTypmod)
 
SubscriptingReftransformContainerSubscripts (ParseState *pstate, Node *containerBase, Oid containerType, int32 containerTypMod, List *indirection, bool isAssignment)
 
Constmake_const (ParseState *pstate, A_Const *aconst)
 

Function Documentation

◆ cancel_parser_errposition_callback()

void cancel_parser_errposition_callback ( ParseCallbackState pcbstate)

◆ free_parsestate()

void free_parsestate ( ParseState pstate)

Definition at line 77 of file parse_node.c.

78 {
79  /*
80  * Check that we did not produce too many resnos; at the very least we
81  * cannot allow more than 2^16, since that would exceed the range of a
82  * AttrNumber. It seems safest to use MaxTupleAttributeNumber.
83  */
84  if (pstate->p_next_resno - 1 > MaxTupleAttributeNumber)
85  ereport(ERROR,
86  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
87  errmsg("target lists can have at most %d entries",
89 
90  if (pstate->p_target_relation != NULL)
92 
93  pfree(pstate);
94 }
int errcode(int sqlerrcode)
Definition: elog.c:698
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
#define MaxTupleAttributeNumber
Definition: htup_details.h:33
#define NoLock
Definition: lockdefs.h:34
void pfree(void *pointer)
Definition: mcxt.c:1169
int p_next_resno
Definition: parse_node.h:197
Relation p_target_relation
Definition: parse_node.h:192
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167

References ereport, errcode(), errmsg(), ERROR, MaxTupleAttributeNumber, NoLock, ParseState::p_next_resno, ParseState::p_target_relation, pfree(), and table_close().

Referenced by AlterPolicy(), CreatePolicy(), CreateTriggerFiringOn(), inline_function(), interpret_AS_clause(), parse_analyze(), parse_analyze_varparams(), parse_sub_analyze(), pg_analyze_and_rewrite_params(), standard_ProcessUtility(), transformIndexStmt(), transformInsertStmt(), transformRuleStmt(), and transformStatsStmt().

◆ make_const()

Const* make_const ( ParseState pstate,
A_Const aconst 
)

Definition at line 352 of file parse_node.c.

353 {
354  Const *con;
355  Datum val;
356  int64 val64;
357  Oid typeid;
358  int typelen;
359  bool typebyval;
360  ParseCallbackState pcbstate;
361 
362  if (aconst->isnull)
363  {
364  /* return a null const */
365  con = makeConst(UNKNOWNOID,
366  -1,
367  InvalidOid,
368  -2,
369  (Datum) 0,
370  true,
371  false);
372  con->location = aconst->location;
373  return con;
374  }
375 
376  switch (nodeTag(&aconst->val))
377  {
378  case T_Integer:
379  val = Int32GetDatum(aconst->val.ival.val);
380 
381  typeid = INT4OID;
382  typelen = sizeof(int32);
383  typebyval = true;
384  break;
385 
386  case T_Float:
387  /* could be an oversize integer as well as a float ... */
388  if (scanint8(aconst->val.fval.val, true, &val64))
389  {
390  /*
391  * It might actually fit in int32. Probably only INT_MIN can
392  * occur, but we'll code the test generally just to be sure.
393  */
394  int32 val32 = (int32) val64;
395 
396  if (val64 == (int64) val32)
397  {
398  val = Int32GetDatum(val32);
399 
400  typeid = INT4OID;
401  typelen = sizeof(int32);
402  typebyval = true;
403  }
404  else
405  {
406  val = Int64GetDatum(val64);
407 
408  typeid = INT8OID;
409  typelen = sizeof(int64);
410  typebyval = FLOAT8PASSBYVAL; /* int8 and float8 alike */
411  }
412  }
413  else
414  {
415  /* arrange to report location if numeric_in() fails */
416  setup_parser_errposition_callback(&pcbstate, pstate, aconst->location);
418  CStringGetDatum(aconst->val.fval.val),
420  Int32GetDatum(-1));
422 
423  typeid = NUMERICOID;
424  typelen = -1; /* variable len */
425  typebyval = false;
426  }
427  break;
428 
429  case T_String:
430 
431  /*
432  * We assume here that UNKNOWN's internal representation is the
433  * same as CSTRING
434  */
435  val = CStringGetDatum(aconst->val.sval.val);
436 
437  typeid = UNKNOWNOID; /* will be coerced later */
438  typelen = -2; /* cstring-style varwidth type */
439  typebyval = false;
440  break;
441 
442  case T_BitString:
443  /* arrange to report location if bit_in() fails */
444  setup_parser_errposition_callback(&pcbstate, pstate, aconst->location);
446  CStringGetDatum(aconst->val.bsval.val),
448  Int32GetDatum(-1));
450  typeid = BITOID;
451  typelen = -1;
452  typebyval = false;
453  break;
454 
455  default:
456  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(&aconst->val));
457  return NULL; /* keep compiler quiet */
458  }
459 
460  con = makeConst(typeid,
461  -1, /* typmod -1 is OK for all cases */
462  InvalidOid, /* all cases are uncollatable types */
463  typelen,
464  val,
465  false,
466  typebyval);
467  con->location = aconst->location;
468 
469  return con;
470 }
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:615
signed int int32
Definition: c.h:429
#define FLOAT8PASSBYVAL
Definition: c.h:570
#define elog(elevel,...)
Definition: elog.h:218
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1697
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:635
long val
Definition: informix.c:664
bool scanint8(const char *str, bool errorOK, int64 *result)
Definition: int8.c:55
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:299
#define nodeTag(nodeptr)
Definition: nodes.h:543
@ T_BitString
Definition: nodes.h:296
@ T_Float
Definition: nodes.h:294
@ T_String
Definition: nodes.h:295
@ T_Integer
Definition: nodes.h:293
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:161
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:145
#define CStringGetDatum(X)
Definition: postgres.h:622
uintptr_t Datum
Definition: postgres.h:411
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define Int32GetDatum(X)
Definition: postgres.h:523
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
bool isnull
Definition: parsenodes.h:309
int location
Definition: parsenodes.h:310
union A_Const::ValUnion val
char * val
Definition: value.h:60
int location
Definition: primnodes.h:226
char * val
Definition: value.h:48
int val
Definition: value.h:31
char * val
Definition: value.h:54
BitString bsval
Definition: parsenodes.h:307
Datum bit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:147

References bit_in(), A_Const::ValUnion::bsval, cancel_parser_errposition_callback(), CStringGetDatum, DirectFunctionCall3, elog, ERROR, FLOAT8PASSBYVAL, A_Const::ValUnion::fval, Int32GetDatum, Int64GetDatum(), InvalidOid, A_Const::isnull, A_Const::ValUnion::ival, A_Const::location, Const::location, makeConst(), nodeTag, numeric_in(), ObjectIdGetDatum, scanint8(), setup_parser_errposition_callback(), A_Const::ValUnion::sval, T_BitString, T_Float, T_Integer, T_String, A_Const::val, Integer::val, Float::val, String::val, BitString::val, and val.

Referenced by transformExprRecurse().

◆ make_parsestate()

ParseState* make_parsestate ( ParseState parentParseState)

Definition at line 44 of file parse_node.c.

45 {
46  ParseState *pstate;
47 
48  pstate = palloc0(sizeof(ParseState));
49 
50  pstate->parentParseState = parentParseState;
51 
52  /* Fill in fields that don't start at null/false/zero */
53  pstate->p_next_resno = 1;
54  pstate->p_resolve_unknowns = true;
55 
56  if (parentParseState)
57  {
58  pstate->p_sourcetext = parentParseState->p_sourcetext;
59  /* all hooks are copied from parent */
60  pstate->p_pre_columnref_hook = parentParseState->p_pre_columnref_hook;
61  pstate->p_post_columnref_hook = parentParseState->p_post_columnref_hook;
62  pstate->p_paramref_hook = parentParseState->p_paramref_hook;
63  pstate->p_coerce_param_hook = parentParseState->p_coerce_param_hook;
64  pstate->p_ref_hook_state = parentParseState->p_ref_hook_state;
65  /* query environment stays in context for the whole parse analysis */
66  pstate->p_queryEnv = parentParseState->p_queryEnv;
67  }
68 
69  return pstate;
70 }
void * palloc0(Size size)
Definition: mcxt.c:1093
ParseState * parentParseState
Definition: parse_node.h:180
void * p_ref_hook_state
Definition: parse_node.h:224
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:222
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:220
QueryEnvironment * p_queryEnv
Definition: parse_node.h:205
const char * p_sourcetext
Definition: parse_node.h:181
bool p_resolve_unknowns
Definition: parse_node.h:202
CoerceParamHook p_coerce_param_hook
Definition: parse_node.h:223
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:221

References ParseState::p_coerce_param_hook, ParseState::p_next_resno, ParseState::p_paramref_hook, ParseState::p_post_columnref_hook, ParseState::p_pre_columnref_hook, ParseState::p_queryEnv, ParseState::p_ref_hook_state, ParseState::p_resolve_unknowns, ParseState::p_sourcetext, palloc0(), and ParseState::parentParseState.

Referenced by AddRelationNewConstraints(), AlterDomainDefault(), AlterPolicy(), ATExecAttachPartition(), ATPrepAlterColumnType(), convert_ANY_sublink_to_join(), copy_table(), CreatePolicy(), CreateTriggerFiringOn(), DefineDomain(), DefineRelation(), domainAddConstraint(), ExplainExecuteQuery(), inline_function(), interpret_AS_clause(), parse_analyze(), parse_analyze_varparams(), parse_sub_analyze(), pg_analyze_and_rewrite_params(), rewriteTargetView(), standard_ProcessUtility(), test_rls_hooks_permissive(), test_rls_hooks_restrictive(), transformAlterTableStmt(), transformCreateStmt(), transformIndexStmt(), transformInsertStmt(), transformPartitionSpec(), transformRuleStmt(), transformStatsStmt(), and UpdateRangeTableOfViewParse().

◆ parser_errposition()

int parser_errposition ( ParseState pstate,
int  location 
)

Definition at line 111 of file parse_node.c.

112 {
113  int pos;
114 
115  /* No-op if location was not provided */
116  if (location < 0)
117  return 0;
118  /* Can't do anything if source text is not available */
119  if (pstate == NULL || pstate->p_sourcetext == NULL)
120  return 0;
121  /* Convert offset to character number */
122  pos = pg_mbstrlen_with_len(pstate->p_sourcetext, location) + 1;
123  /* And pass it to the ereport mechanism */
124  return errposition(pos);
125 }
int errposition(int cursorpos)
Definition: elog.c:1285
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:1000

References errposition(), ParseState::p_sourcetext, and pg_mbstrlen_with_len().

Referenced by addRangeTableEntryForCTE(), addRangeTableEntryForFunction(), AlterDatabase(), analyzeCTE(), analyzeCTETargetList(), array_subscript_transform(), assign_collations_walker(), assign_hypothetical_collations(), check_agg_arguments(), check_agg_arguments_walker(), check_agglevels_and_constraints(), check_lateral_ref_ok(), check_nested_generated_walker(), check_new_partition_bound(), check_parameter_resolution_walker(), check_srf_call_placement(), check_ungrouped_columns_walker(), checkExprIsVarFree(), checkInsertTargets(), checkTargetlistEntrySQL92(), checkWellFormedRecursion(), checkWellFormedRecursionWalker(), cluster(), coerce_to_boolean(), coerce_to_common_type(), coerce_to_specific_type_typmod(), colNameToVar(), compatible_oper(), compute_common_attribute(), compute_function_attributes(), ComputePartitionAttrs(), createdb(), CreateTriggerFiringOn(), DefineCollation(), DefineType(), DropDatabase(), errorConflictingDefElem(), errorMissingColumn(), errorMissingRTE(), EvaluateParams(), ExecReindex(), ExecVacuum(), ExpandAllTables(), ExpandColumnRefStar(), ExplainQuery(), finalize_grouping_exprs_walker(), findTargetlistEntrySQL92(), fixed_paramref_hook(), GetColumnDefCollation(), hstore_subscript_transform(), init_params(), jsonb_subscript_transform(), LookupOperName(), LookupTypeNameExtended(), LookupTypeNameOid(), make_distinct_op(), make_op(), make_row_comparison_op(), make_row_distinct_op(), make_scalar_array_op(), merge_collation_state(), op_error(), parseCheckAggregates(), ParseFuncOrColumn(), parser_coercion_errposition(), parseTypeString(), pcb_error_callback(), plpgsql_post_column_ref(), ProcessCopyOptions(), resolve_column_ref(), resolve_unique_index_expr(), scanNameSpaceForRefname(), scanNameSpaceForRelid(), scanNSItemForColumn(), scanRTEForColumn(), select_common_collation(), select_common_type(), TopologicalSort(), transformAExprNullIf(), transformAggregateCall(), transformArrayExpr(), transformAssignedExpr(), transformAssignmentIndirection(), transformAssignmentSubscripts(), transformCaseExpr(), transformCoalesceExpr(), transformCollateClause(), transformColumnDefinition(), transformColumnRef(), transformColumnType(), transformConstraintAttrs(), transformContainerSubscripts(), transformDistinctClause(), transformDistinctOnClause(), transformExprRecurse(), transformFrameOffset(), transformFromClauseItem(), transformGroupingFunc(), transformGroupingSet(), transformIndexConstraint(), transformIndirection(), transformInsertRow(), transformLockingClause(), transformMultiAssignRef(), transformOnConflictArbiter(), transformParamRef(), transformPartitionBound(), transformPartitionBoundValue(), transformPLAssignStmt(), transformRangeFunction(), transformRangeTableFunc(), transformRangeTableSample(), transformReturningList(), transformSelectStmt(), transformSetOperationStmt(), transformSetOperationTree(), transformSubLink(), transformTableConstraint(), transformUpdateTargetList(), transformValuesClause(), transformWindowDefinitions(), transformWindowFuncCall(), transformWithClause(), transformXmlExpr(), transformXmlSerialize(), typenameType(), typenameTypeMod(), unknown_attribute(), validateInfiniteBounds(), variable_coerce_param_hook(), and variable_paramref_hook().

◆ pcb_error_callback()

static void pcb_error_callback ( void *  arg)
static

Definition at line 175 of file parse_node.c.

176 {
177  ParseCallbackState *pcbstate = (ParseCallbackState *) arg;
178 
179  if (geterrcode() != ERRCODE_QUERY_CANCELED)
180  (void) parser_errposition(pcbstate->pstate, pcbstate->location);
181 }
int geterrcode(void)
Definition: elog.c:1400
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
void * arg
ParseState * pstate
Definition: parse_node.h:314

References arg, geterrcode(), ParseCallbackState::location, parser_errposition(), and ParseCallbackState::pstate.

Referenced by setup_parser_errposition_callback().

◆ setup_parser_errposition_callback()

void setup_parser_errposition_callback ( ParseCallbackState pcbstate,
ParseState pstate,
int  location 
)

◆ transformContainerSubscripts()

SubscriptingRef* transformContainerSubscripts ( ParseState pstate,
Node containerBase,
Oid  containerType,
int32  containerTypMod,
List indirection,
bool  isAssignment 
)

Definition at line 248 of file parse_node.c.

254 {
255  SubscriptingRef *sbsref;
256  const SubscriptRoutines *sbsroutines;
257  Oid elementType;
258  bool isSlice = false;
259  ListCell *idx;
260 
261  /*
262  * Determine the actual container type, smashing any domain. In the
263  * assignment case the caller already did this, since it also needs to
264  * know the actual container type.
265  */
266  if (!isAssignment)
267  transformContainerType(&containerType, &containerTypMod);
268 
269  /*
270  * Verify that the container type is subscriptable, and get its support
271  * functions and typelem.
272  */
273  sbsroutines = getSubscriptingRoutines(containerType, &elementType);
274  if (!sbsroutines)
275  ereport(ERROR,
276  (errcode(ERRCODE_DATATYPE_MISMATCH),
277  errmsg("cannot subscript type %s because it does not support subscripting",
278  format_type_be(containerType)),
279  parser_errposition(pstate, exprLocation(containerBase))));
280 
281  /*
282  * Detect whether any of the indirection items are slice specifiers.
283  *
284  * A list containing only simple subscripts refers to a single container
285  * element. If any of the items are slice specifiers (lower:upper), then
286  * the subscript expression means a container slice operation.
287  */
288  foreach(idx, indirection)
289  {
291 
292  if (ai->is_slice)
293  {
294  isSlice = true;
295  break;
296  }
297  }
298 
299  /*
300  * Ready to build the SubscriptingRef node.
301  */
302  sbsref = makeNode(SubscriptingRef);
303 
304  sbsref->refcontainertype = containerType;
305  sbsref->refelemtype = elementType;
306  /* refrestype is to be set by container-specific logic */
307  sbsref->reftypmod = containerTypMod;
308  /* refcollid will be set by parse_collate.c */
309  /* refupperindexpr, reflowerindexpr are to be set by container logic */
310  sbsref->refexpr = (Expr *) containerBase;
311  sbsref->refassgnexpr = NULL; /* caller will fill if it's an assignment */
312 
313  /*
314  * Call the container-type-specific logic to transform the subscripts and
315  * determine the subscripting result type.
316  */
317  sbsroutines->transform(sbsref, indirection, pstate,
318  isSlice, isAssignment);
319 
320  /*
321  * Verify we got a valid type (this defends, for example, against someone
322  * using array_subscript_handler as typsubscript without setting typelem).
323  */
324  if (!OidIsValid(sbsref->refrestype))
325  ereport(ERROR,
326  (errcode(ERRCODE_DATATYPE_MISMATCH),
327  errmsg("cannot subscript type %s because it does not support subscripting",
328  format_type_be(containerType))));
329 
330  return sbsref;
331 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
#define OidIsValid(objectId)
Definition: c.h:710
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
const struct SubscriptRoutines * getSubscriptingRoutines(Oid typid, Oid *typelemp)
Definition: lsyscache.c:3077
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1250
#define makeNode(_type_)
Definition: nodes.h:586
void transformContainerType(Oid *containerType, int32 *containerTypmod)
Definition: parse_node.c:194
#define lfirst_node(type, lc)
Definition: pg_list.h:172
bool is_slice
Definition: parsenodes.h:405
SubscriptTransform transform
Definition: subscripting.h:160
Expr * refassgnexpr
Definition: primnodes.h:451
Expr * refexpr
Definition: primnodes.h:449
Oid refcontainertype
Definition: primnodes.h:439

References ereport, errcode(), errmsg(), ERROR, exprLocation(), format_type_be(), getSubscriptingRoutines(), idx(), A_Indices::is_slice, lfirst_node, makeNode, OidIsValid, parser_errposition(), SubscriptingRef::refassgnexpr, SubscriptingRef::refcontainertype, SubscriptingRef::refelemtype, SubscriptingRef::refexpr, SubscriptingRef::refrestype, SubscriptingRef::reftypmod, SubscriptRoutines::transform, and transformContainerType().

Referenced by transformAssignmentSubscripts(), and transformIndirection().

◆ transformContainerType()

void transformContainerType ( Oid containerType,
int32 containerTypmod 
)

Definition at line 194 of file parse_node.c.

195 {
196  /*
197  * If the input is a domain, smash to base type, and extract the actual
198  * typmod to be applied to the base type. Subscripting a domain is an
199  * operation that necessarily works on the base container type, not the
200  * domain itself. (Note that we provide no method whereby the creator of a
201  * domain over a container type could hide its ability to be subscripted.)
202  */
203  *containerType = getBaseTypeAndTypmod(*containerType, containerTypmod);
204 
205  /*
206  * We treat int2vector and oidvector as though they were domains over
207  * int2[] and oid[]. This is needed because array slicing could create an
208  * array that doesn't satisfy the dimensionality constraints of the
209  * xxxvector type; so we want the result of a slice operation to be
210  * considered to be of the more general type.
211  */
212  if (*containerType == INT2VECTOROID)
213  *containerType = INT2ARRAYOID;
214  else if (*containerType == OIDVECTOROID)
215  *containerType = OIDARRAYOID;
216 }
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2485

References getBaseTypeAndTypmod().

Referenced by transformAssignmentSubscripts(), and transformContainerSubscripts().