PostgreSQL Source Code git master
Loading...
Searching...
No Matches
parse_target.h File Reference
Include dependency graph for parse_target.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ListtransformTargetList (ParseState *pstate, List *targetlist, ParseExprKind exprKind)
 
ListtransformExpressionList (ParseState *pstate, List *exprlist, ParseExprKind exprKind, bool allowDefault)
 
void resolveTargetListUnknowns (ParseState *pstate, List *targetlist)
 
void markTargetListOrigins (ParseState *pstate, List *targetlist)
 
TargetEntrytransformTargetEntry (ParseState *pstate, Node *node, Node *expr, ParseExprKind exprKind, char *colname, bool resjunk)
 
ExprtransformAssignedExpr (ParseState *pstate, Expr *expr, ParseExprKind exprKind, const char *colname, int attrno, List *indirection, int location)
 
void updateTargetListEntry (ParseState *pstate, TargetEntry *tle, char *colname, int attrno, List *indirection, int location)
 
NodetransformAssignmentIndirection (ParseState *pstate, Node *basenode, const char *targetName, bool targetIsSubscripting, Oid targetTypeId, int32 targetTypMod, Oid targetCollation, List *indirection, ListCell *indirection_cell, Node *rhs, CoercionContext ccontext, int location)
 
ListcheckInsertTargets (ParseState *pstate, List *cols, List **attrnos)
 
TupleDesc expandRecordVariable (ParseState *pstate, Var *var, int levelsup)
 
charFigureColname (Node *node)
 
charFigureIndexColname (Node *node)
 

Function Documentation

◆ checkInsertTargets()

List * checkInsertTargets ( ParseState pstate,
List cols,
List **  attrnos 
)
extern

Definition at line 1023 of file parse_target.c.

1024{
1025 *attrnos = NIL;
1026
1027 if (cols == NIL)
1028 {
1029 /*
1030 * Generate default column list for INSERT.
1031 */
1033
1034 int i;
1035
1036 for (i = 0; i < numcol; i++)
1037 {
1038 ResTarget *col;
1039 Form_pg_attribute attr;
1040
1041 attr = TupleDescAttr(pstate->p_target_relation->rd_att, i);
1042
1043 if (attr->attisdropped)
1044 continue;
1045
1047 col->name = pstrdup(NameStr(attr->attname));
1048 col->indirection = NIL;
1049 col->val = NULL;
1050 col->location = -1;
1051 cols = lappend(cols, col);
1052 *attrnos = lappend_int(*attrnos, i + 1);
1053 }
1054 }
1055 else
1056 {
1057 /*
1058 * Do initial validation of user-supplied INSERT column list.
1059 */
1062 ListCell *tl;
1063
1064 foreach(tl, cols)
1065 {
1067 char *name = col->name;
1068 int attrno;
1069
1070 /* Lookup column name, ereport on failure */
1071 attrno = attnameAttNum(pstate->p_target_relation, name, false);
1073 ereport(ERROR,
1075 errmsg("column \"%s\" of relation \"%s\" does not exist",
1076 name,
1078 parser_errposition(pstate, col->location)));
1079
1080 /*
1081 * Check for duplicates, but only of whole columns --- we allow
1082 * INSERT INTO foo (col.subcol1, col.subcol2)
1083 */
1084 if (col->indirection == NIL)
1085 {
1086 /* whole column; must not have any other assignment */
1089 ereport(ERROR,
1091 errmsg("column \"%s\" specified more than once",
1092 name),
1093 parser_errposition(pstate, col->location)));
1095 }
1096 else
1097 {
1098 /* partial column; must not have any whole assignment */
1100 ereport(ERROR,
1102 errmsg("column \"%s\" specified more than once",
1103 name),
1104 parser_errposition(pstate, col->location)));
1106 }
1107
1109 }
1110 }
1111
1112 return cols;
1113}
#define InvalidAttrNumber
Definition attnum.h:23
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
#define NameStr(name)
Definition c.h:837
int errcode(int sqlerrcode)
Definition elog.c:874
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
int i
Definition isn.c:77
List * lappend(List *list, void *datum)
Definition list.c:339
List * lappend_int(List *list, int datum)
Definition list.c:357
char * pstrdup(const char *in)
Definition mcxt.c:1781
#define makeNode(_type_)
Definition nodes.h:161
static char * errmsg
int parser_errposition(ParseState *pstate, int location)
Definition parse_node.c:106
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
FormData_pg_attribute * Form_pg_attribute
#define lfirst(lc)
Definition pg_list.h:172
#define NIL
Definition pg_list.h:68
static int fb(int x)
#define RelationGetNumberOfAttributes(relation)
Definition rel.h:520
#define RelationGetRelationName(relation)
Definition rel.h:548
Relation p_target_relation
Definition parse_node.h:224
TupleDesc rd_att
Definition rel.h:112
char * name
Definition parsenodes.h:545
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178
const char * name

References attnameAttNum(), bms_add_member(), bms_is_member(), ereport, errcode(), errmsg, ERROR, fb(), i, InvalidAttrNumber, lappend(), lappend_int(), lfirst, makeNode, name, ResTarget::name, NameStr, NIL, ParseState::p_target_relation, parser_errposition(), pstrdup(), RelationData::rd_att, RelationGetNumberOfAttributes, RelationGetRelationName, and TupleDescAttr().

Referenced by transformInsertStmt(), and transformMergeStmt().

◆ expandRecordVariable()

TupleDesc expandRecordVariable ( ParseState pstate,
Var var,
int  levelsup 
)
extern

Definition at line 1527 of file parse_target.c.

1528{
1530 int netlevelsup;
1533 Node *expr;
1534
1535 /* Check my caller didn't mess up */
1536 Assert(IsA(var, Var));
1537 Assert(var->vartype == RECORDOID);
1538
1539 /*
1540 * Note: it's tempting to use GetNSItemByRangeTablePosn here so that we
1541 * can use expandNSItemVars instead of expandRTE; but that does not work
1542 * for some of the recursion cases below, where we have consed up a
1543 * ParseState that lacks p_namespace data.
1544 */
1545 netlevelsup = var->varlevelsup + levelsup;
1547 attnum = var->varattno;
1548
1550 {
1551 /* Whole-row reference to an RTE, so expand the known fields */
1552 List *names,
1553 *vars;
1554 ListCell *lname,
1555 *lvar;
1556 int i;
1557
1558 expandRTE(rte, var->varno, 0, var->varreturningtype,
1559 var->location, false, &names, &vars);
1560
1562 i = 1;
1563 forboth(lname, names, lvar, vars)
1564 {
1565 char *label = strVal(lfirst(lname));
1566 Node *varnode = (Node *) lfirst(lvar);
1567
1569 label,
1572 0);
1575 i++;
1576 }
1577 Assert(lname == NULL && lvar == NULL); /* lists same length? */
1578
1580
1581 return tupleDesc;
1582 }
1583
1584 expr = (Node *) var; /* default if we can't drill down */
1585
1586 switch (rte->rtekind)
1587 {
1588 case RTE_RELATION:
1589 case RTE_VALUES:
1591 case RTE_GRAPH_TABLE:
1592 case RTE_RESULT:
1593
1594 /*
1595 * This case should not occur: a column of a table, values list,
1596 * or ENR shouldn't have type RECORD. Fall through and fail (most
1597 * likely) at the bottom.
1598 */
1599 break;
1600 case RTE_SUBQUERY:
1601 {
1602 /* Subselect-in-FROM: examine sub-select's output expr */
1603 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
1604 attnum);
1605
1606 if (ste == NULL || ste->resjunk)
1607 elog(ERROR, "subquery %s does not have attribute %d",
1608 rte->eref->aliasname, attnum);
1609 expr = (Node *) ste->expr;
1610 if (IsA(expr, Var))
1611 {
1612 /*
1613 * Recurse into the sub-select to see what its Var refers
1614 * to. We have to build an additional level of ParseState
1615 * to keep in step with varlevelsup in the subselect;
1616 * furthermore, the subquery RTE might be from an outer
1617 * query level, in which case the ParseState for the
1618 * subselect must have that outer level as parent.
1619 */
1620 ParseState mypstate = {0};
1621
1622 /* this loop must work, since GetRTEByRangeTablePosn did */
1623 for (Index level = 0; level < netlevelsup; level++)
1624 pstate = pstate->parentParseState;
1625 mypstate.parentParseState = pstate;
1626 mypstate.p_rtable = rte->subquery->rtable;
1627 /* don't bother filling the rest of the fake pstate */
1628
1629 return expandRecordVariable(&mypstate, (Var *) expr, 0);
1630 }
1631 /* else fall through to inspect the expression */
1632 }
1633 break;
1634 case RTE_JOIN:
1635 /* Join RTE --- recursively inspect the alias variable */
1636 Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
1637 expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
1638 Assert(expr != NULL);
1639 /* We intentionally don't strip implicit coercions here */
1640 if (IsA(expr, Var))
1641 return expandRecordVariable(pstate, (Var *) expr, netlevelsup);
1642 /* else fall through to inspect the expression */
1643 break;
1644 case RTE_FUNCTION:
1645
1646 /*
1647 * We couldn't get here unless a function is declared with one of
1648 * its result columns as RECORD, which is not allowed.
1649 */
1650 break;
1651 case RTE_TABLEFUNC:
1652
1653 /*
1654 * Table function cannot have columns with RECORD type.
1655 */
1656 break;
1657 case RTE_CTE:
1658 /* CTE reference: examine subquery's output expr */
1659 if (!rte->self_reference)
1660 {
1663
1665 if (ste == NULL || ste->resjunk)
1666 elog(ERROR, "CTE %s does not have attribute %d",
1667 rte->eref->aliasname, attnum);
1668 expr = (Node *) ste->expr;
1669 if (IsA(expr, Var))
1670 {
1671 /*
1672 * Recurse into the CTE to see what its Var refers to. We
1673 * have to build an additional level of ParseState to keep
1674 * in step with varlevelsup in the CTE; furthermore it
1675 * could be an outer CTE (compare SUBQUERY case above).
1676 */
1677 ParseState mypstate = {0};
1678
1679 /* this loop must work, since GetCTEForRTE did */
1680 for (Index level = 0;
1681 level < rte->ctelevelsup + netlevelsup;
1682 level++)
1683 pstate = pstate->parentParseState;
1684 mypstate.parentParseState = pstate;
1685 mypstate.p_rtable = ((Query *) cte->ctequery)->rtable;
1686 /* don't bother filling the rest of the fake pstate */
1687
1688 return expandRecordVariable(&mypstate, (Var *) expr, 0);
1689 }
1690 /* else fall through to inspect the expression */
1691 }
1692 break;
1693 case RTE_GROUP:
1694
1695 /*
1696 * We couldn't get here: the RTE_GROUP RTE has not been added.
1697 */
1698 break;
1699 }
1700
1701 /*
1702 * We now have an expression we can't expand any more, so see if
1703 * get_expr_result_tupdesc() can do anything with it.
1704 */
1705 return get_expr_result_tupdesc(expr, false);
1706}
int16 AttrNumber
Definition attnum.h:21
#define Assert(condition)
Definition c.h:945
unsigned int Index
Definition c.h:700
#define elog(elevel,...)
Definition elog.h:226
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition funcapi.c:553
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition nodeFuncs.c:304
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:826
#define IsA(nodeptr, _type_)
Definition nodes.h:164
CommonTableExpr * GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, VarReturningType returning_type, int location, bool include_dropped, List **colnames, List **colvars)
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
TupleDesc expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
#define GetCTETargetList(cte)
@ RTE_JOIN
@ RTE_CTE
@ RTE_NAMEDTUPLESTORE
@ RTE_VALUES
@ RTE_SUBQUERY
@ RTE_RESULT
@ RTE_FUNCTION
@ RTE_TABLEFUNC
@ RTE_GROUP
@ RTE_GRAPH_TABLE
@ RTE_RELATION
int16 attnum
static char * label
static int list_length(const List *l)
Definition pg_list.h:152
#define forboth(cell1, list1, cell2, list2)
Definition pg_list.h:518
static void * list_nth(const List *list, int n)
Definition pg_list.h:299
Definition pg_list.h:54
Definition nodes.h:135
ParseState * parentParseState
Definition parse_node.h:209
List * p_rtable
Definition parse_node.h:211
ParseLoc location
Definition primnodes.h:311
AttrNumber varattno
Definition primnodes.h:275
int varno
Definition primnodes.h:270
VarReturningType varreturningtype
Definition primnodes.h:298
Index varlevelsup
Definition primnodes.h:295
TupleDesc CreateTemplateTupleDesc(int natts)
Definition tupdesc.c:165
void TupleDescFinalize(TupleDesc tupdesc)
Definition tupdesc.c:508
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition tupdesc.c:1081
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition tupdesc.c:897
#define strVal(v)
Definition value.h:82

References Assert, attnum, CreateTemplateTupleDesc(), CommonTableExpr::ctequery, elog, ERROR, expandRecordVariable(), expandRTE(), exprCollation(), exprType(), exprTypmod(), fb(), forboth, get_expr_result_tupdesc(), get_tle_by_resno(), GetCTEForRTE(), GetCTETargetList, GetRTEByRangeTablePosn(), i, InvalidAttrNumber, IsA, label, lfirst, list_length(), list_nth(), Var::location, ParseState::p_rtable, ParseState::parentParseState, RTE_CTE, RTE_FUNCTION, RTE_GRAPH_TABLE, RTE_GROUP, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, strVal, TupleDescFinalize(), TupleDescInitEntry(), TupleDescInitEntryCollation(), Var::varattno, Var::varlevelsup, Var::varno, and Var::varreturningtype.

Referenced by expandRecordVariable(), ExpandRowReference(), and ParseComplexProjection().

◆ FigureColname()

char * FigureColname ( Node node)
extern

Definition at line 1719 of file parse_target.c.

1720{
1721 char *name = NULL;
1722
1724 if (name != NULL)
1725 return name;
1726 /* default result if we can't guess anything */
1727 return "?column?";
1728}
static int FigureColnameInternal(Node *node, char **name)

References fb(), FigureColnameInternal(), and name.

Referenced by transformRangeFunction(), transformTargetEntry(), and transformXmlExpr().

◆ FigureIndexColname()

char * FigureIndexColname ( Node node)
extern

Definition at line 1738 of file parse_target.c.

1739{
1740 char *name = NULL;
1741
1743 return name;
1744}

References fb(), FigureColnameInternal(), and name.

Referenced by transformIndexStmt().

◆ markTargetListOrigins()

void markTargetListOrigins ( ParseState pstate,
List targetlist 
)
extern

Definition at line 317 of file parse_target.c.

318{
319 ListCell *l;
320
321 foreach(l, targetlist)
322 {
324
325 markTargetListOrigin(pstate, tle, (Var *) tle->expr, 0);
326 }
327}
static void markTargetListOrigin(ParseState *pstate, TargetEntry *tle, Var *var, int levelsup)

References fb(), lfirst, and markTargetListOrigin().

Referenced by transformReturningClause(), and transformSelectStmt().

◆ resolveTargetListUnknowns()

void resolveTargetListUnknowns ( ParseState pstate,
List targetlist 
)
extern

Definition at line 287 of file parse_target.c.

288{
289 ListCell *l;
290
291 foreach(l, targetlist)
292 {
294 Oid restype = exprType((Node *) tle->expr);
295
296 if (restype == UNKNOWNOID)
297 {
298 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
299 restype, TEXTOID, -1,
302 -1);
303 }
304 }
305}
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
unsigned int Oid
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:769
@ COERCION_IMPLICIT
Definition primnodes.h:747

References COERCE_IMPLICIT_CAST, coerce_type(), COERCION_IMPLICIT, exprType(), fb(), and lfirst.

Referenced by transformReturningClause(), transformReturnStmt(), and transformSelectStmt().

◆ transformAssignedExpr()

Expr * transformAssignedExpr ( ParseState pstate,
Expr expr,
ParseExprKind  exprKind,
const char colname,
int  attrno,
List indirection,
int  location 
)
extern

Definition at line 459 of file parse_target.c.

466{
467 Relation rd = pstate->p_target_relation;
468 Oid type_id; /* type of value provided */
469 Oid attrtype; /* type of target column */
470 int32 attrtypmod;
471 Oid attrcollation; /* collation of target column */
473
474 /*
475 * Save and restore identity of expression type we're parsing. We must
476 * set p_expr_kind here because we can parse subscripts without going
477 * through transformExpr().
478 */
481 sv_expr_kind = pstate->p_expr_kind;
482 pstate->p_expr_kind = exprKind;
483
484 Assert(rd != NULL);
485 if (attrno <= 0)
488 errmsg("cannot assign to system column \"%s\"",
489 colname),
490 parser_errposition(pstate, location)));
491 attrtype = attnumTypeId(rd, attrno);
492 attrtypmod = TupleDescAttr(rd->rd_att, attrno - 1)->atttypmod;
493 attrcollation = TupleDescAttr(rd->rd_att, attrno - 1)->attcollation;
494
495 /*
496 * If the expression is a DEFAULT placeholder, insert the attribute's
497 * type/typmod/collation into it so that exprType etc will report the
498 * right things. (We expect that the eventually substituted default
499 * expression will in fact have this type and typmod. The collation
500 * likely doesn't matter, but let's set it correctly anyway.) Also,
501 * reject trying to update a subfield or array element with DEFAULT, since
502 * there can't be any default for portions of a column.
503 */
504 if (expr && IsA(expr, SetToDefault))
505 {
506 SetToDefault *def = (SetToDefault *) expr;
507
508 def->typeId = attrtype;
509 def->typeMod = attrtypmod;
510 def->collation = attrcollation;
511 if (indirection)
512 {
513 if (IsA(linitial(indirection), A_Indices))
516 errmsg("cannot set an array element to DEFAULT"),
517 parser_errposition(pstate, location)));
518 else
521 errmsg("cannot set a subfield to DEFAULT"),
522 parser_errposition(pstate, location)));
523 }
524 }
525
526 /* Now we can use exprType() safely. */
527 type_id = exprType((Node *) expr);
528
529 /*
530 * If there is indirection on the target column, prepare an array or
531 * subfield assignment expression. This will generate a new column value
532 * that the source value has been inserted into, which can then be placed
533 * in the new tuple constructed by INSERT or UPDATE.
534 */
535 if (indirection)
536 {
537 Node *colVar;
538
540 {
541 /*
542 * The command is INSERT INTO table (col.something) ... so there
543 * is not really a source value to work with. Insert a NULL
544 * constant as the source value.
545 */
546 colVar = (Node *) makeNullConst(attrtype, attrtypmod,
548 }
549 else
550 {
551 /*
552 * Build a Var for the column to be updated.
553 */
554 Var *var;
555
556 var = makeVar(pstate->p_target_nsitem->p_rtindex, attrno,
557 attrtype, attrtypmod, attrcollation, 0);
558 var->location = location;
559
560 colVar = (Node *) var;
561 }
562
563 expr = (Expr *)
565 colVar,
566 colname,
567 false,
568 attrtype,
569 attrtypmod,
571 indirection,
572 list_head(indirection),
573 (Node *) expr,
575 location);
576 }
577 else
578 {
579 /*
580 * For normal non-qualified target column, do type checking and
581 * coercion.
582 */
583 Node *orig_expr = (Node *) expr;
584
585 expr = (Expr *)
587 orig_expr, type_id,
588 attrtype, attrtypmod,
591 -1);
592 if (expr == NULL)
595 errmsg("column \"%s\" is of type %s"
596 " but expression is of type %s",
597 colname,
598 format_type_be(attrtype),
599 format_type_be(type_id)),
600 errhint("You will need to rewrite or cast the expression."),
602 }
603
604 pstate->p_expr_kind = sv_expr_kind;
605
606 return expr;
607}
int32_t int32
Definition c.h:614
int errhint(const char *fmt,...) pg_attribute_printf(1
char * format_type_be(Oid type_oid)
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition makefuncs.c:66
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition makefuncs.c:388
int exprLocation(const Node *expr)
Definition nodeFuncs.c:1392
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
ParseExprKind
Definition parse_node.h:39
@ EXPR_KIND_INSERT_TARGET
Definition parse_node.h:55
@ EXPR_KIND_UPDATE_TARGET
Definition parse_node.h:57
Oid attnumTypeId(Relation rd, int attid)
Node * transformAssignmentIndirection(ParseState *pstate, Node *basenode, const char *targetName, bool targetIsSubscripting, Oid targetTypeId, int32 targetTypMod, Oid targetCollation, List *indirection, ListCell *indirection_cell, Node *rhs, CoercionContext ccontext, int location)
#define linitial(l)
Definition pg_list.h:178
static ListCell * list_head(const List *l)
Definition pg_list.h:128
@ COERCION_ASSIGNMENT
Definition primnodes.h:748
ParseNamespaceItem * p_target_nsitem
Definition parse_node.h:225
ParseExprKind p_expr_kind
Definition parse_node.h:228

References Assert, attnumTypeId(), COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, ereport, errcode(), errhint(), errmsg, ERROR, EXPR_KIND_INSERT_TARGET, EXPR_KIND_UPDATE_TARGET, exprLocation(), exprType(), fb(), format_type_be(), IsA, linitial, list_head(), Var::location, makeNullConst(), makeVar(), ParseState::p_expr_kind, ParseNamespaceItem::p_rtindex, ParseState::p_target_nsitem, ParseState::p_target_relation, parser_errposition(), transformAssignmentIndirection(), TupleDescAttr(), and SetToDefault::typeId.

Referenced by transformInsertRow(), and updateTargetListEntry().

◆ transformAssignmentIndirection()

Node * transformAssignmentIndirection ( ParseState pstate,
Node basenode,
const char targetName,
bool  targetIsSubscripting,
Oid  targetTypeId,
int32  targetTypMod,
Oid  targetCollation,
List indirection,
ListCell indirection_cell,
Node rhs,
CoercionContext  ccontext,
int  location 
)
extern

Definition at line 691 of file parse_target.c.

703{
704 Node *result;
706 ListCell *i;
707
709 {
710 /*
711 * Set up a substitution. We abuse CaseTestExpr for this. It's safe
712 * to do so because the only nodes that will be above the CaseTestExpr
713 * in the finished expression will be FieldStore and SubscriptingRef
714 * nodes. (There could be other stuff in the tree, but it will be
715 * within other child fields of those node types.)
716 */
718
719 ctest->typeId = targetTypeId;
720 ctest->typeMod = targetTypMod;
721 ctest->collation = targetCollation;
722 basenode = (Node *) ctest;
723 }
724
725 /*
726 * We have to split any field-selection operations apart from
727 * subscripting. Adjacent A_Indices nodes have to be treated as a single
728 * multidimensional subscript operation.
729 */
730 for_each_cell(i, indirection, indirection_cell)
731 {
732 Node *n = lfirst(i);
733
734 if (IsA(n, A_Indices))
736 else if (IsA(n, A_Star))
737 {
740 errmsg("row expansion via \"*\" is not supported here"),
741 parser_errposition(pstate, location)));
742 }
743 else
744 {
745 FieldStore *fstore;
748 Oid typrelid;
753
754 Assert(IsA(n, String));
755
756 /* process subscripts before this field selection */
757 if (subscripts)
758 {
759 /* recurse, and then return because we're done */
760 return transformAssignmentSubscripts(pstate,
761 basenode,
767 indirection,
768 i,
769 rhs,
770 ccontext,
771 location);
772 }
773
774 /* No subscripts, so can process field selection here */
775
776 /*
777 * Look up the composite type, accounting for possibility that
778 * what we are given is a domain over composite.
779 */
782
783 typrelid = typeidTypeRelid(baseTypeId);
784 if (!typrelid)
787 errmsg("cannot assign to field \"%s\" of column \"%s\" because its type %s is not a composite type",
788 strVal(n), targetName,
790 parser_errposition(pstate, location)));
791
792 attnum = get_attnum(typrelid, strVal(n));
796 errmsg("cannot assign to field \"%s\" of column \"%s\" because there is no such column in data type %s",
797 strVal(n), targetName,
799 parser_errposition(pstate, location)));
800 if (attnum < 0)
803 errmsg("cannot assign to system column \"%s\"",
804 strVal(n)),
805 parser_errposition(pstate, location)));
806
809
810 /* recurse to create appropriate RHS for field assign */
812 NULL,
813 strVal(n),
814 false,
818 indirection,
819 lnext(indirection, i),
820 rhs,
821 ccontext,
822 location);
823
824 /* and build a FieldStore node */
825 fstore = makeNode(FieldStore);
826 fstore->arg = (Expr *) basenode;
827 fstore->newvals = list_make1(rhs);
828 fstore->fieldnums = list_make1_int(attnum);
829 fstore->resulttype = baseTypeId;
830
831 /*
832 * If target is a domain, apply constraints. Notice that this
833 * isn't totally right: the expression tree we build would check
834 * the domain's constraints on a composite value with only this
835 * one field populated or updated, possibly leading to an unwanted
836 * failure. The rewriter will merge together any subfield
837 * assignments to the same table column, resulting in the domain's
838 * constraints being checked only once after we've assigned to all
839 * the fields that the INSERT or UPDATE means to.
840 */
842 return coerce_to_domain((Node *) fstore,
847 location,
848 false);
849
850 return (Node *) fstore;
851 }
852 }
853
854 /* process trailing subscripts, if any */
855 if (subscripts)
856 {
857 /* recurse, and then return because we're done */
858 return transformAssignmentSubscripts(pstate,
859 basenode,
865 indirection,
866 NULL,
867 rhs,
868 ccontext,
869 location);
870 }
871
872 /* base case: just coerce RHS to match target type ID */
873
874 result = coerce_to_target_type(pstate,
875 rhs, exprType(rhs),
877 ccontext,
879 -1);
880 if (result == NULL)
881 {
885 errmsg("subscripted assignment to \"%s\" requires type %s"
886 " but expression is of type %s",
890 errhint("You will need to rewrite or cast the expression."),
891 parser_errposition(pstate, location)));
892 else
895 errmsg("subfield \"%s\" is of type %s"
896 " but expression is of type %s",
900 errhint("You will need to rewrite or cast the expression."),
901 parser_errposition(pstate, location)));
902 }
903
904 return result;
905}
AttrNumber get_attnum(Oid relid, const char *attname)
Definition lsyscache.c:977
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition lsyscache.c:2760
void get_atttypetypmodcoll(Oid relid, AttrNumber attnum, Oid *typid, int32 *typmod, Oid *collid)
Definition lsyscache.c:1062
Node * coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, CoercionContext ccontext, CoercionForm cformat, int location, bool hideInputCoercion)
static Node * transformAssignmentSubscripts(ParseState *pstate, Node *basenode, const char *targetName, Oid targetTypeId, int32 targetTypMod, Oid targetCollation, List *subscripts, List *indirection, ListCell *next_indirection, Node *rhs, CoercionContext ccontext, int location)
Oid typeidTypeRelid(Oid type_id)
Definition parse_type.c:668
#define list_make1(x1)
Definition pg_list.h:212
#define for_each_cell(cell, lst, initcell)
Definition pg_list.h:438
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:343
#define list_make1_int(x1)
Definition pg_list.h:227
List * newvals
Definition primnodes.h:1194
Expr * arg
Definition primnodes.h:1193
Definition value.h:64

References FieldStore::arg, Assert, attnum, COERCE_IMPLICIT_CAST, coerce_to_domain(), coerce_to_target_type(), COERCION_IMPLICIT, ereport, errcode(), errhint(), errmsg, ERROR, exprType(), fb(), for_each_cell, format_type_be(), get_attnum(), get_atttypetypmodcoll(), getBaseTypeAndTypmod(), i, InvalidAttrNumber, IsA, lappend(), lfirst, list_make1, list_make1_int, lnext(), makeNode, FieldStore::newvals, NIL, parser_errposition(), strVal, transformAssignmentIndirection(), transformAssignmentSubscripts(), and typeidTypeRelid().

Referenced by transformAssignedExpr(), transformAssignmentIndirection(), transformAssignmentSubscripts(), and transformPLAssignStmtTarget().

◆ transformExpressionList()

List * transformExpressionList ( ParseState pstate,
List exprlist,
ParseExprKind  exprKind,
bool  allowDefault 
)
extern

Definition at line 219 of file parse_target.c.

221{
222 List *result = NIL;
223 ListCell *lc;
224
225 foreach(lc, exprlist)
226 {
227 Node *e = (Node *) lfirst(lc);
228
229 /*
230 * Check for "something.*". Depending on the complexity of the
231 * "something", the star could appear as the last field in ColumnRef,
232 * or as the last indirection item in A_Indirection.
233 */
234 if (IsA(e, ColumnRef))
235 {
236 ColumnRef *cref = (ColumnRef *) e;
237
238 if (IsA(llast(cref->fields), A_Star))
239 {
240 /* It is something.*, expand into multiple items */
241 result = list_concat(result,
243 false));
244 continue;
245 }
246 }
247 else if (IsA(e, A_Indirection))
248 {
250
251 if (IsA(llast(ind->indirection), A_Star))
252 {
253 /* It is something.*, expand into multiple items */
254 result = list_concat(result,
256 false, exprKind));
257 continue;
258 }
259 }
260
261 /*
262 * Not "something.*", so transform as a single expression. If it's a
263 * SetToDefault node and we should allow that, pass it through
264 * unmodified. (transformExpr will throw the appropriate error if
265 * we're disallowing it.)
266 */
268 /* do nothing */ ;
269 else
270 e = transformExpr(pstate, e, exprKind);
271
272 result = lappend(result, e);
273 }
274
275 return result;
276}
List * list_concat(List *list1, const List *list2)
Definition list.c:561
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition parse_expr.c:121
static List * ExpandColumnRefStar(ParseState *pstate, ColumnRef *cref, bool make_target_entry)
static List * ExpandIndirectionStar(ParseState *pstate, A_Indirection *ind, bool make_target_entry, ParseExprKind exprKind)
#define llast(l)
Definition pg_list.h:198
e

References ExpandColumnRefStar(), ExpandIndirectionStar(), fb(), IsA, lappend(), lfirst, list_concat(), llast, NIL, and transformExpr().

Referenced by transformInsertStmt(), transformMergeStmt(), transformRowExpr(), and transformValuesClause().

◆ transformTargetEntry()

TargetEntry * transformTargetEntry ( ParseState pstate,
Node node,
Node expr,
ParseExprKind  exprKind,
char colname,
bool  resjunk 
)
extern

Definition at line 74 of file parse_target.c.

80{
81 /* Transform the node if caller didn't do it already */
82 if (expr == NULL)
83 {
84 /*
85 * If it's a SetToDefault node and we should allow that, pass it
86 * through unmodified. (transformExpr will throw the appropriate
87 * error if we're disallowing it.)
88 */
90 expr = node;
91 else
92 expr = transformExpr(pstate, node, exprKind);
93 }
94
95 if (colname == NULL && !resjunk)
96 {
97 /*
98 * Generate a suitable column name for a column without any explicit
99 * 'AS ColumnName' clause.
100 */
101 colname = FigureColname(node);
102 }
103
104 return makeTargetEntry((Expr *) expr,
105 (AttrNumber) pstate->p_next_resno++,
106 colname,
107 resjunk);
108}
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition makefuncs.c:289
@ EXPR_KIND_UPDATE_SOURCE
Definition parse_node.h:56
char * FigureColname(Node *node)
int p_next_resno
Definition parse_node.h:229

References EXPR_KIND_UPDATE_SOURCE, fb(), FigureColname(), IsA, makeTargetEntry(), ParseState::p_next_resno, and transformExpr().

Referenced by findTargetlistEntrySQL99(), and transformTargetList().

◆ transformTargetList()

List * transformTargetList ( ParseState pstate,
List targetlist,
ParseExprKind  exprKind 
)
extern

Definition at line 120 of file parse_target.c.

122{
123 List *p_target = NIL;
124 bool expand_star;
126
127 /* Shouldn't have any leftover multiassign items at start */
128 Assert(pstate->p_multiassign_exprs == NIL);
129
130 /* Expand "something.*" in SELECT and RETURNING, but not UPDATE */
132
133 foreach(o_target, targetlist)
134 {
136
137 /*
138 * Check for "something.*". Depending on the complexity of the
139 * "something", the star could appear as the last field in ColumnRef,
140 * or as the last indirection item in A_Indirection.
141 */
142 if (expand_star)
143 {
144 if (IsA(res->val, ColumnRef))
145 {
146 ColumnRef *cref = (ColumnRef *) res->val;
147
148 if (IsA(llast(cref->fields), A_Star))
149 {
150 /* It is something.*, expand into multiple items */
152 ExpandColumnRefStar(pstate,
153 cref,
154 true));
155 continue;
156 }
157 }
158 else if (IsA(res->val, A_Indirection))
159 {
161
162 if (IsA(llast(ind->indirection), A_Star))
163 {
164 /* It is something.*, expand into multiple items */
167 ind,
168 true,
169 exprKind));
170 continue;
171 }
172 }
173 }
174
175 /*
176 * Not "something.*", or we want to treat that as a plain whole-row
177 * variable, so transform as a single expression
178 */
181 res->val,
182 NULL,
183 exprKind,
184 res->name,
185 false));
186 }
187
188 /*
189 * If any multiassign resjunk items were created, attach them to the end
190 * of the targetlist. This should only happen in an UPDATE tlist. We
191 * don't need to worry about numbering of these items; transformUpdateStmt
192 * will set their resnos.
193 */
194 if (pstate->p_multiassign_exprs)
195 {
198 pstate->p_multiassign_exprs = NIL;
199 }
200
201 return p_target;
202}
TargetEntry * transformTargetEntry(ParseState *pstate, Node *node, Node *expr, ParseExprKind exprKind, char *colname, bool resjunk)
List * p_multiassign_exprs
Definition parse_node.h:230
Node * val
Definition parsenodes.h:547

References Assert, ExpandColumnRefStar(), ExpandIndirectionStar(), EXPR_KIND_UPDATE_SOURCE, fb(), IsA, lappend(), lfirst, list_concat(), llast, ResTarget::name, NIL, ParseState::p_multiassign_exprs, transformTargetEntry(), and ResTarget::val.

Referenced by insert_property_records(), transformReturningClause(), transformSelectStmt(), and transformUpdateTargetList().

◆ updateTargetListEntry()

void updateTargetListEntry ( ParseState pstate,
TargetEntry tle,
char colname,
int  attrno,
List indirection,
int  location 
)
extern

Definition at line 627 of file parse_target.c.

633{
634 /* Fix up expression as needed */
635 tle->expr = transformAssignedExpr(pstate,
636 tle->expr,
638 colname,
639 attrno,
640 indirection,
641 location);
642
643 /*
644 * Set the resno to identify the target column --- the rewriter and
645 * planner depend on this. We also set the resname to identify the target
646 * column, but this is only for debugging purposes; it should not be
647 * relied on. (In particular, it might be out of date in a stored rule.)
648 */
649 tle->resno = (AttrNumber) attrno;
650 tle->resname = colname;
651}
Expr * transformAssignedExpr(ParseState *pstate, Expr *expr, ParseExprKind exprKind, const char *colname, int attrno, List *indirection, int location)

References EXPR_KIND_UPDATE_TARGET, fb(), and transformAssignedExpr().

Referenced by transformUpdateTargetList().