PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
parse_node.c File Reference
#include "postgres.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/parsetree.h"
#include "parser/parse_coerce.h"
#include "parser/parse_expr.h"
#include "parser/parse_relation.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)
 
Varmake_var (ParseState *pstate, RangeTblEntry *rte, int attrno, int location)
 
Oid transformArrayType (Oid *arrayType, int32 *arrayTypmod)
 
ArrayReftransformArraySubscripts (ParseState *pstate, Node *arrayBase, Oid arrayType, Oid elementType, int32 arrayTypMod, List *indirection, Node *assignFrom)
 
Constmake_const (ParseState *pstate, Value *value, int location)
 

Function Documentation

void cancel_parser_errposition_callback ( ParseCallbackState pcbstate)
void free_parsestate ( ParseState pstate)

Definition at line 75 of file parse_node.c.

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

Referenced by AlterPolicy(), CreatePolicy(), CreateTrigger(), inline_function(), parse_analyze(), parse_analyze_varparams(), parse_sub_analyze(), pg_analyze_and_rewrite_params(), RemoveRoleFromObjectPolicy(), standard_ProcessUtility(), transformIndexStmt(), transformInsertStmt(), and transformRuleStmt().

76 {
77  /*
78  * Check that we did not produce too many resnos; at the very least we
79  * cannot allow more than 2^16, since that would exceed the range of a
80  * AttrNumber. It seems safest to use MaxTupleAttributeNumber.
81  */
82  if (pstate->p_next_resno - 1 > MaxTupleAttributeNumber)
83  ereport(ERROR,
84  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
85  errmsg("target lists can have at most %d entries",
87 
88  if (pstate->p_target_relation != NULL)
90 
91  pfree(pstate);
92 }
#define MaxTupleAttributeNumber
Definition: htup_details.h:33
int errcode(int sqlerrcode)
Definition: elog.c:575
#define heap_close(r, l)
Definition: heapam.h:97
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
int p_next_resno
Definition: parse_node.h:183
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
Relation p_target_relation
Definition: parse_node.h:178
Const* make_const ( ParseState pstate,
Value value,
int  location 
)

Definition at line 468 of file parse_node.c.

References bit_in(), BITOID, cancel_parser_errposition_callback(), CStringGetDatum, DirectFunctionCall3, elog, ERROR, Int32GetDatum, INT4OID, Int64GetDatum(), INT8OID, intVal, InvalidOid, Const::location, makeConst(), nodeTag, NULL, numeric_in(), NUMERICOID, ObjectIdGetDatum, scanint8(), setup_parser_errposition_callback(), strVal, T_BitString, T_Float, T_Integer, T_Null, T_String, UNKNOWNOID, and val.

Referenced by transformExprRecurse(), and transformPartitionBound().

469 {
470  Const *con;
471  Datum val;
472  int64 val64;
473  Oid typeid;
474  int typelen;
475  bool typebyval;
476  ParseCallbackState pcbstate;
477 
478  switch (nodeTag(value))
479  {
480  case T_Integer:
481  val = Int32GetDatum(intVal(value));
482 
483  typeid = INT4OID;
484  typelen = sizeof(int32);
485  typebyval = true;
486  break;
487 
488  case T_Float:
489  /* could be an oversize integer as well as a float ... */
490  if (scanint8(strVal(value), true, &val64))
491  {
492  /*
493  * It might actually fit in int32. Probably only INT_MIN can
494  * occur, but we'll code the test generally just to be sure.
495  */
496  int32 val32 = (int32) val64;
497 
498  if (val64 == (int64) val32)
499  {
500  val = Int32GetDatum(val32);
501 
502  typeid = INT4OID;
503  typelen = sizeof(int32);
504  typebyval = true;
505  }
506  else
507  {
508  val = Int64GetDatum(val64);
509 
510  typeid = INT8OID;
511  typelen = sizeof(int64);
512  typebyval = FLOAT8PASSBYVAL; /* int8 and float8 alike */
513  }
514  }
515  else
516  {
517  /* arrange to report location if numeric_in() fails */
518  setup_parser_errposition_callback(&pcbstate, pstate, location);
520  CStringGetDatum(strVal(value)),
522  Int32GetDatum(-1));
524 
525  typeid = NUMERICOID;
526  typelen = -1; /* variable len */
527  typebyval = false;
528  }
529  break;
530 
531  case T_String:
532 
533  /*
534  * We assume here that UNKNOWN's internal representation is the
535  * same as CSTRING
536  */
537  val = CStringGetDatum(strVal(value));
538 
539  typeid = UNKNOWNOID; /* will be coerced later */
540  typelen = -2; /* cstring-style varwidth type */
541  typebyval = false;
542  break;
543 
544  case T_BitString:
545  /* arrange to report location if bit_in() fails */
546  setup_parser_errposition_callback(&pcbstate, pstate, location);
548  CStringGetDatum(strVal(value)),
550  Int32GetDatum(-1));
552  typeid = BITOID;
553  typelen = -1;
554  typebyval = false;
555  break;
556 
557  case T_Null:
558  /* return a null const */
559  con = makeConst(UNKNOWNOID,
560  -1,
561  InvalidOid,
562  -2,
563  (Datum) 0,
564  true,
565  false);
566  con->location = location;
567  return con;
568 
569  default:
570  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(value));
571  return NULL; /* keep compiler quiet */
572  }
573 
574  con = makeConst(typeid,
575  -1, /* typmod -1 is OK for all cases */
576  InvalidOid, /* all cases are uncollatable types */
577  typelen,
578  val,
579  false,
580  typebyval);
581  con->location = location;
582 
583  return con;
584 }
#define NUMERICOID
Definition: pg_type.h:542
#define INT4OID
Definition: pg_type.h:316
#define strVal(v)
Definition: value.h:54
unsigned int Oid
Definition: postgres_ext.h:31
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:296
signed int int32
Definition: c.h:253
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition: parse_node.c:159
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
Datum bit_in(PG_FUNCTION_ARGS)
Definition: varbit.c:113
#define CStringGetDatum(X)
Definition: postgres.h:586
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:559
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:2102
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition: parse_node.c:143
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:559
int location
Definition: primnodes.h:181
uintptr_t Datum
Definition: postgres.h:374
#define InvalidOid
Definition: postgres_ext.h:36
#define INT8OID
Definition: pg_type.h:304
#define BITOID
Definition: pg_type.h:530
#define NULL
Definition: c.h:226
#define UNKNOWNOID
Definition: pg_type.h:423
#define nodeTag(nodeptr)
Definition: nodes.h:514
Definition: nodes.h:291
Definition: nodes.h:288
#define Int32GetDatum(X)
Definition: postgres.h:487
#define intVal(v)
Definition: value.h:52
#define elog
Definition: elog.h:219
long val
Definition: informix.c:689
bool scanint8(const char *str, bool errorOK, int64 *result)
Definition: int8.c:55
ParseState* make_parsestate ( ParseState parentParseState)

Definition at line 44 of file parse_node.c.

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_ref_hook_state, ParseState::p_resolve_unknowns, ParseState::p_sourcetext, palloc0(), and ParseState::parentParseState.

Referenced by AddRelationNewConstraints(), AlterDomainDefault(), AlterPolicy(), ATPrepAlterColumnType(), check_new_partition_bound(), convert_ANY_sublink_to_join(), CreatePolicy(), CreateTrigger(), DefineDomain(), DefineRelation(), domainAddConstraint(), EvaluateParams(), inline_function(), parse_analyze(), parse_analyze_varparams(), parse_sub_analyze(), pg_analyze_and_rewrite_params(), PrepareQuery(), RemoveRoleFromObjectPolicy(), standard_ProcessUtility(), test_rls_hooks_permissive(), test_rls_hooks_restrictive(), transformAlterTableStmt(), transformCreateStmt(), transformIndexStmt(), transformInsertStmt(), transformPartitionSpec(), transformRuleStmt(), and UpdateRangeTableOfViewParse().

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  }
66 
67  return pstate;
68 }
CoerceParamHook p_coerce_param_hook
Definition: parse_node.h:205
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:203
bool p_resolve_unknowns
Definition: parse_node.h:188
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:204
int p_next_resno
Definition: parse_node.h:183
const char * p_sourcetext
Definition: parse_node.h:167
struct ParseState * parentParseState
Definition: parse_node.h:166
void * palloc0(Size size)
Definition: mcxt.c:878
void * p_ref_hook_state
Definition: parse_node.h:206
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:202
Var* make_var ( ParseState pstate,
RangeTblEntry rte,
int  attrno,
int  location 
)

Definition at line 187 of file parse_node.c.

References get_rte_attribute_type(), Var::location, makeVar(), and RTERangeTablePosn().

Referenced by scanRTEForColumn(), and transformAssignedExpr().

188 {
189  Var *result;
190  int vnum,
191  sublevels_up;
192  Oid vartypeid;
193  int32 type_mod;
194  Oid varcollid;
195 
196  vnum = RTERangeTablePosn(pstate, rte, &sublevels_up);
197  get_rte_attribute_type(rte, attrno, &vartypeid, &type_mod, &varcollid);
198  result = makeVar(vnum, attrno, vartypeid, type_mod, varcollid, sublevels_up);
199  result->location = location;
200  return result;
201 }
void get_rte_attribute_type(RangeTblEntry *rte, AttrNumber attnum, Oid *vartype, int32 *vartypmod, Oid *varcollid)
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:141
signed int int32
Definition: c.h:253
int location
Definition: primnodes.h:156
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:67
int RTERangeTablePosn(ParseState *pstate, RangeTblEntry *rte, int *sublevels_up)
int parser_errposition ( ParseState pstate,
int  location 
)

Definition at line 109 of file parse_node.c.

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

Referenced by addRangeTableEntryForCTE(), addRangeTableEntryForFunction(), AlterDatabase(), analyzeCTE(), analyzeCTETargetList(), assign_collations_walker(), assign_hypothetical_collations(), check_agg_arguments(), check_agg_arguments_walker(), check_agglevels_and_constraints(), check_lateral_ref_ok(), check_new_partition_bound(), check_parameter_resolution_walker(), check_srf_call_placement(), check_ungrouped_columns_walker(), checkExprIsVarFree(), checkInsertTargets(), checkTargetlistEntrySQL92(), checkWellFormedRecursion(), checkWellFormedRecursionWalker(), coerce_to_boolean(), coerce_to_common_type(), coerce_to_specific_type(), colNameToVar(), compatible_oper(), compute_attributes_sql_style(), compute_attributes_with_style(), compute_common_attribute(), createdb(), CreateExtension(), CreateRole(), CreateTrigger(), DefineCollation(), DefineType(), emit_precedence_warnings(), errorMissingColumn(), errorMissingRTE(), ExecAlterDefaultPrivilegesStmt(), ExecAlterExtensionStmt(), ExpandAllTables(), ExpandColumnRefStar(), ExplainQuery(), finalize_grouping_exprs_walker(), findTargetlistEntrySQL92(), fixed_paramref_hook(), GetColumnDefCollation(), init_params(), LookupOperName(), LookupTypeName(), 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(), scanRTEForColumn(), select_common_collation(), select_common_type(), TopologicalSort(), transformAExprNullIf(), transformAggregateCall(), transformArrayExpr(), transformArraySubscripts(), transformAssignedExpr(), transformAssignmentIndirection(), transformAssignmentSubscripts(), transformCollateClause(), transformColumnDefinition(), transformColumnRef(), transformColumnType(), transformConstraintAttrs(), transformDistinctClause(), transformDistinctOnClause(), transformExprRecurse(), transformFromClauseItem(), transformGroupingFunc(), transformGroupingSet(), transformIndexConstraint(), transformIndirection(), transformInsertRow(), transformLockingClause(), transformMultiAssignRef(), transformOnConflictArbiter(), transformParamRef(), transformPartitionBound(), transformPartitionSpec(), transformRangeFunction(), transformRangeTableSample(), transformReturningList(), transformSelectStmt(), transformSetOperationStmt(), transformSetOperationTree(), transformSubLink(), transformTableConstraint(), transformUpdateTargetList(), transformValuesClause(), transformWindowDefinitions(), transformWindowFuncCall(), transformWithClause(), transformXmlExpr(), transformXmlSerialize(), typenameType(), typenameTypeMod(), unknown_attribute(), variable_coerce_param_hook(), and variable_paramref_hook().

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

Definition at line 173 of file parse_node.c.

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

Referenced by setup_parser_errposition_callback().

174 {
175  ParseCallbackState *pcbstate = (ParseCallbackState *) arg;
176 
177  if (geterrcode() != ERRCODE_QUERY_CANCELED)
178  (void) parser_errposition(pcbstate->pstate, pcbstate->location);
179 }
int geterrcode(void)
Definition: elog.c:1240
ParseState * pstate
Definition: parse_node.h:248
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
void * arg
void setup_parser_errposition_callback ( ParseCallbackState pcbstate,
ParseState pstate,
int  location 
)

Definition at line 143 of file parse_node.c.

References ErrorContextCallback::arg, ErrorContextCallback::callback, ParseCallbackState::errcallback, error_context_stack, ParseCallbackState::location, pcb_error_callback(), ErrorContextCallback::previous, and ParseCallbackState::pstate.

Referenced by addTargetToGroupList(), addTargetToSortList(), coerce_type(), LookupCollation(), LookupTypeName(), make_const(), make_oper_cache_key(), ParseFuncOrColumn(), parserOpenTable(), transformCreateStmt(), transformSetOperationTree(), transformTableLikeClause(), and typenameTypeMod().

145 {
146  /* Setup error traceback support for ereport() */
147  pcbstate->pstate = pstate;
148  pcbstate->location = location;
150  pcbstate->errcallback.arg = (void *) pcbstate;
152  error_context_stack = &pcbstate->errcallback;
153 }
ErrorContextCallback errcallback
Definition: parse_node.h:250
struct ErrorContextCallback * previous
Definition: elog.h:238
ErrorContextCallback * error_context_stack
Definition: elog.c:88
ParseState * pstate
Definition: parse_node.h:248
void(* callback)(void *arg)
Definition: elog.h:239
static void pcb_error_callback(void *arg)
Definition: parse_node.c:173
ArrayRef* transformArraySubscripts ( ParseState pstate,
Node arrayBase,
Oid  arrayType,
Oid  elementType,
int32  arrayTypMod,
List indirection,
Node assignFrom 
)

Definition at line 292 of file parse_node.c.

References Assert, castNode, COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, ereport, errcode(), errhint(), errmsg(), ERROR, exprLocation(), exprType(), format_type_be(), idx(), Int32GetDatum, INT4OID, InvalidOid, A_Indices::is_slice, lappend(), lfirst, A_Indices::lidx, makeConst(), makeNode, NIL, NULL, OidIsValid, ParseState::p_expr_kind, parser_errposition(), ArrayRef::refarraytype, ArrayRef::refassgnexpr, ArrayRef::refelemtype, ArrayRef::refexpr, ArrayRef::reflowerindexpr, ArrayRef::reftypmod, ArrayRef::refupperindexpr, transformArrayType(), transformExpr(), and A_Indices::uidx.

Referenced by transformAssignmentSubscripts(), and transformIndirection().

299 {
300  bool isSlice = false;
301  List *upperIndexpr = NIL;
302  List *lowerIndexpr = NIL;
303  ListCell *idx;
304  ArrayRef *aref;
305 
306  /*
307  * Caller may or may not have bothered to determine elementType. Note
308  * that if the caller did do so, arrayType/arrayTypMod must be as modified
309  * by transformArrayType, ie, smash domain to base type.
310  */
311  if (!OidIsValid(elementType))
312  elementType = transformArrayType(&arrayType, &arrayTypMod);
313 
314  /*
315  * A list containing only simple subscripts refers to a single array
316  * element. If any of the items are slice specifiers (lower:upper), then
317  * the subscript expression means an array slice operation. In this case,
318  * we convert any non-slice items to slices by treating the single
319  * subscript as the upper bound and supplying an assumed lower bound of 1.
320  * We have to prescan the list to see if there are any slice items.
321  */
322  foreach(idx, indirection)
323  {
324  A_Indices *ai = (A_Indices *) lfirst(idx);
325 
326  if (ai->is_slice)
327  {
328  isSlice = true;
329  break;
330  }
331  }
332 
333  /*
334  * Transform the subscript expressions.
335  */
336  foreach(idx, indirection)
337  {
338  A_Indices *ai = castNode(A_Indices, lfirst(idx));
339  Node *subexpr;
340 
341  if (isSlice)
342  {
343  if (ai->lidx)
344  {
345  subexpr = transformExpr(pstate, ai->lidx, pstate->p_expr_kind);
346  /* If it's not int4 already, try to coerce */
347  subexpr = coerce_to_target_type(pstate,
348  subexpr, exprType(subexpr),
349  INT4OID, -1,
352  -1);
353  if (subexpr == NULL)
354  ereport(ERROR,
355  (errcode(ERRCODE_DATATYPE_MISMATCH),
356  errmsg("array subscript must have type integer"),
357  parser_errposition(pstate, exprLocation(ai->lidx))));
358  }
359  else if (!ai->is_slice)
360  {
361  /* Make a constant 1 */
362  subexpr = (Node *) makeConst(INT4OID,
363  -1,
364  InvalidOid,
365  sizeof(int32),
366  Int32GetDatum(1),
367  false,
368  true); /* pass by value */
369  }
370  else
371  {
372  /* Slice with omitted lower bound, put NULL into the list */
373  subexpr = NULL;
374  }
375  lowerIndexpr = lappend(lowerIndexpr, subexpr);
376  }
377  else
378  Assert(ai->lidx == NULL && !ai->is_slice);
379 
380  if (ai->uidx)
381  {
382  subexpr = transformExpr(pstate, ai->uidx, pstate->p_expr_kind);
383  /* If it's not int4 already, try to coerce */
384  subexpr = coerce_to_target_type(pstate,
385  subexpr, exprType(subexpr),
386  INT4OID, -1,
389  -1);
390  if (subexpr == NULL)
391  ereport(ERROR,
392  (errcode(ERRCODE_DATATYPE_MISMATCH),
393  errmsg("array subscript must have type integer"),
394  parser_errposition(pstate, exprLocation(ai->uidx))));
395  }
396  else
397  {
398  /* Slice with omitted upper bound, put NULL into the list */
399  Assert(isSlice && ai->is_slice);
400  subexpr = NULL;
401  }
402  upperIndexpr = lappend(upperIndexpr, subexpr);
403  }
404 
405  /*
406  * If doing an array store, coerce the source value to the right type.
407  * (This should agree with the coercion done by transformAssignedExpr.)
408  */
409  if (assignFrom != NULL)
410  {
411  Oid typesource = exprType(assignFrom);
412  Oid typeneeded = isSlice ? arrayType : elementType;
413  Node *newFrom;
414 
415  newFrom = coerce_to_target_type(pstate,
416  assignFrom, typesource,
417  typeneeded, arrayTypMod,
420  -1);
421  if (newFrom == NULL)
422  ereport(ERROR,
423  (errcode(ERRCODE_DATATYPE_MISMATCH),
424  errmsg("array assignment requires type %s"
425  " but expression is of type %s",
426  format_type_be(typeneeded),
427  format_type_be(typesource)),
428  errhint("You will need to rewrite or cast the expression."),
429  parser_errposition(pstate, exprLocation(assignFrom))));
430  assignFrom = newFrom;
431  }
432 
433  /*
434  * Ready to build the ArrayRef node.
435  */
436  aref = makeNode(ArrayRef);
437  aref->refarraytype = arrayType;
438  aref->refelemtype = elementType;
439  aref->reftypmod = arrayTypMod;
440  /* refcollid will be set by parse_collate.c */
441  aref->refupperindexpr = upperIndexpr;
442  aref->reflowerindexpr = lowerIndexpr;
443  aref->refexpr = (Expr *) arrayBase;
444  aref->refassgnexpr = (Expr *) assignFrom;
445 
446  return aref;
447 }
#define NIL
Definition: pg_list.h:69
Expr * refassgnexpr
Definition: primnodes.h:387
int errhint(const char *fmt,...)
Definition: elog.c:987
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1204
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:147
#define INT4OID
Definition: pg_type.h:316
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
unsigned int Oid
Definition: postgres_ext.h:31
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:296
#define OidIsValid(objectId)
Definition: c.h:534
signed int int32
Definition: c.h:253
bool is_slice
Definition: parsenodes.h:373
#define ERROR
Definition: elog.h:43
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:77
List * refupperindexpr
Definition: primnodes.h:381
List * reflowerindexpr
Definition: primnodes.h:383
int32 reftypmod
Definition: primnodes.h:379
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
Oid refelemtype
Definition: primnodes.h:378
ParseExprKind p_expr_kind
Definition: parse_node.h:182
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:109
Node * lidx
Definition: parsenodes.h:374
Oid refarraytype
Definition: primnodes.h:377
#define Int32GetDatum(X)
Definition: postgres.h:487
Oid transformArrayType(Oid *arrayType, int32 *arrayTypmod)
Definition: parse_node.c:214
int errmsg(const char *fmt,...)
Definition: elog.c:797
Node * uidx
Definition: parsenodes.h:375
Definition: pg_list.h:45
Expr * refexpr
Definition: primnodes.h:385
Oid transformArrayType ( Oid arrayType,
int32 arrayTypmod 
)

Definition at line 214 of file parse_node.c.

References elog, ereport, errcode(), errmsg(), ERROR, format_type_be(), getBaseTypeAndTypmod(), GETSTRUCT, HeapTupleIsValid, INT2ARRAYOID, INT2VECTOROID, InvalidOid, ObjectIdGetDatum, OIDARRAYOID, OIDVECTOROID, ReleaseSysCache(), SearchSysCache1, and TYPEOID.

Referenced by transformArraySubscripts(), and transformAssignmentSubscripts().

215 {
216  Oid origArrayType = *arrayType;
217  Oid elementType;
218  HeapTuple type_tuple_array;
219  Form_pg_type type_struct_array;
220 
221  /*
222  * If the input is a domain, smash to base type, and extract the actual
223  * typmod to be applied to the base type. Subscripting a domain is an
224  * operation that necessarily works on the base array type, not the domain
225  * itself. (Note that we provide no method whereby the creator of a
226  * domain over an array type could hide its ability to be subscripted.)
227  */
228  *arrayType = getBaseTypeAndTypmod(*arrayType, arrayTypmod);
229 
230  /*
231  * We treat int2vector and oidvector as though they were domains over
232  * int2[] and oid[]. This is needed because array slicing could create an
233  * array that doesn't satisfy the dimensionality constraints of the
234  * xxxvector type; so we want the result of a slice operation to be
235  * considered to be of the more general type.
236  */
237  if (*arrayType == INT2VECTOROID)
238  *arrayType = INT2ARRAYOID;
239  else if (*arrayType == OIDVECTOROID)
240  *arrayType = OIDARRAYOID;
241 
242  /* Get the type tuple for the array */
243  type_tuple_array = SearchSysCache1(TYPEOID, ObjectIdGetDatum(*arrayType));
244  if (!HeapTupleIsValid(type_tuple_array))
245  elog(ERROR, "cache lookup failed for type %u", *arrayType);
246  type_struct_array = (Form_pg_type) GETSTRUCT(type_tuple_array);
247 
248  /* needn't check typisdefined since this will fail anyway */
249 
250  elementType = type_struct_array->typelem;
251  if (elementType == InvalidOid)
252  ereport(ERROR,
253  (errcode(ERRCODE_DATATYPE_MISMATCH),
254  errmsg("cannot subscript type %s because it is not an array",
255  format_type_be(origArrayType))));
256 
257  ReleaseSysCache(type_tuple_array);
258 
259  return elementType;
260 }
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2256
#define INT2VECTOROID
Definition: pg_type.h:312
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
#define OIDVECTOROID
Definition: pg_type.h:344
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
#define INT2ARRAYOID
Definition: pg_type.h:453
#define ereport(elevel, rest)
Definition: elog.h:122
#define OIDARRAYOID
Definition: pg_type.h:461
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219