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 1017 of file parse_target.c.

1018{
1019 *attrnos = NIL;
1020
1021 if (cols == NIL)
1022 {
1023 /*
1024 * Generate default column list for INSERT.
1025 */
1027
1028 int i;
1029
1030 for (i = 0; i < numcol; i++)
1031 {
1032 ResTarget *col;
1033 Form_pg_attribute attr;
1034
1035 attr = TupleDescAttr(pstate->p_target_relation->rd_att, i);
1036
1037 if (attr->attisdropped)
1038 continue;
1039
1041 col->name = pstrdup(NameStr(attr->attname));
1042 col->indirection = NIL;
1043 col->val = NULL;
1044 col->location = -1;
1045 cols = lappend(cols, col);
1046 *attrnos = lappend_int(*attrnos, i + 1);
1047 }
1048 }
1049 else
1050 {
1051 /*
1052 * Do initial validation of user-supplied INSERT column list.
1053 */
1056 ListCell *tl;
1057
1058 foreach(tl, cols)
1059 {
1061 char *name = col->name;
1062 int attrno;
1063
1064 /* Lookup column name, ereport on failure */
1065 attrno = attnameAttNum(pstate->p_target_relation, name, false);
1067 ereport(ERROR,
1069 errmsg("column \"%s\" of relation \"%s\" does not exist",
1070 name,
1072 parser_errposition(pstate, col->location)));
1073
1074 /*
1075 * Check for duplicates, but only of whole columns --- we allow
1076 * INSERT INTO foo (col.subcol1, col.subcol2)
1077 */
1078 if (col->indirection == NIL)
1079 {
1080 /* whole column; must not have any other assignment */
1083 ereport(ERROR,
1085 errmsg("column \"%s\" specified more than once",
1086 name),
1087 parser_errposition(pstate, col->location)));
1089 }
1090 else
1091 {
1092 /* partial column; must not have any whole assignment */
1094 ereport(ERROR,
1096 errmsg("column \"%s\" specified more than once",
1097 name),
1098 parser_errposition(pstate, col->location)));
1100 }
1101
1103 }
1104 }
1105
1106 return cols;
1107}
#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:814
#define NameStr(name)
Definition c.h:765
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#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
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:209
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:160
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 1521 of file parse_target.c.

1522{
1524 int netlevelsup;
1527 Node *expr;
1528
1529 /* Check my caller didn't mess up */
1530 Assert(IsA(var, Var));
1531 Assert(var->vartype == RECORDOID);
1532
1533 /*
1534 * Note: it's tempting to use GetNSItemByRangeTablePosn here so that we
1535 * can use expandNSItemVars instead of expandRTE; but that does not work
1536 * for some of the recursion cases below, where we have consed up a
1537 * ParseState that lacks p_namespace data.
1538 */
1539 netlevelsup = var->varlevelsup + levelsup;
1541 attnum = var->varattno;
1542
1544 {
1545 /* Whole-row reference to an RTE, so expand the known fields */
1546 List *names,
1547 *vars;
1548 ListCell *lname,
1549 *lvar;
1550 int i;
1551
1552 expandRTE(rte, var->varno, 0, var->varreturningtype,
1553 var->location, false, &names, &vars);
1554
1556 i = 1;
1557 forboth(lname, names, lvar, vars)
1558 {
1559 char *label = strVal(lfirst(lname));
1560 Node *varnode = (Node *) lfirst(lvar);
1561
1563 label,
1566 0);
1569 i++;
1570 }
1571 Assert(lname == NULL && lvar == NULL); /* lists same length? */
1572
1573 return tupleDesc;
1574 }
1575
1576 expr = (Node *) var; /* default if we can't drill down */
1577
1578 switch (rte->rtekind)
1579 {
1580 case RTE_RELATION:
1581 case RTE_VALUES:
1583 case RTE_RESULT:
1584
1585 /*
1586 * This case should not occur: a column of a table, values list,
1587 * or ENR shouldn't have type RECORD. Fall through and fail (most
1588 * likely) at the bottom.
1589 */
1590 break;
1591 case RTE_SUBQUERY:
1592 {
1593 /* Subselect-in-FROM: examine sub-select's output expr */
1594 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
1595 attnum);
1596
1597 if (ste == NULL || ste->resjunk)
1598 elog(ERROR, "subquery %s does not have attribute %d",
1599 rte->eref->aliasname, attnum);
1600 expr = (Node *) ste->expr;
1601 if (IsA(expr, Var))
1602 {
1603 /*
1604 * Recurse into the sub-select to see what its Var refers
1605 * to. We have to build an additional level of ParseState
1606 * to keep in step with varlevelsup in the subselect;
1607 * furthermore, the subquery RTE might be from an outer
1608 * query level, in which case the ParseState for the
1609 * subselect must have that outer level as parent.
1610 */
1611 ParseState mypstate = {0};
1612 Index levelsup;
1613
1614 /* this loop must work, since GetRTEByRangeTablePosn did */
1615 for (levelsup = 0; levelsup < netlevelsup; levelsup++)
1616 pstate = pstate->parentParseState;
1617 mypstate.parentParseState = pstate;
1618 mypstate.p_rtable = rte->subquery->rtable;
1619 /* don't bother filling the rest of the fake pstate */
1620
1621 return expandRecordVariable(&mypstate, (Var *) expr, 0);
1622 }
1623 /* else fall through to inspect the expression */
1624 }
1625 break;
1626 case RTE_JOIN:
1627 /* Join RTE --- recursively inspect the alias variable */
1628 Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
1629 expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
1630 Assert(expr != NULL);
1631 /* We intentionally don't strip implicit coercions here */
1632 if (IsA(expr, Var))
1633 return expandRecordVariable(pstate, (Var *) expr, netlevelsup);
1634 /* else fall through to inspect the expression */
1635 break;
1636 case RTE_FUNCTION:
1637
1638 /*
1639 * We couldn't get here unless a function is declared with one of
1640 * its result columns as RECORD, which is not allowed.
1641 */
1642 break;
1643 case RTE_TABLEFUNC:
1644
1645 /*
1646 * Table function cannot have columns with RECORD type.
1647 */
1648 break;
1649 case RTE_CTE:
1650 /* CTE reference: examine subquery's output expr */
1651 if (!rte->self_reference)
1652 {
1655
1657 if (ste == NULL || ste->resjunk)
1658 elog(ERROR, "CTE %s does not have attribute %d",
1659 rte->eref->aliasname, attnum);
1660 expr = (Node *) ste->expr;
1661 if (IsA(expr, Var))
1662 {
1663 /*
1664 * Recurse into the CTE to see what its Var refers to. We
1665 * have to build an additional level of ParseState to keep
1666 * in step with varlevelsup in the CTE; furthermore it
1667 * could be an outer CTE (compare SUBQUERY case above).
1668 */
1669 ParseState mypstate = {0};
1670 Index levelsup;
1671
1672 /* this loop must work, since GetCTEForRTE did */
1673 for (levelsup = 0;
1674 levelsup < rte->ctelevelsup + netlevelsup;
1675 levelsup++)
1676 pstate = pstate->parentParseState;
1677 mypstate.parentParseState = pstate;
1678 mypstate.p_rtable = ((Query *) cte->ctequery)->rtable;
1679 /* don't bother filling the rest of the fake pstate */
1680
1681 return expandRecordVariable(&mypstate, (Var *) expr, 0);
1682 }
1683 /* else fall through to inspect the expression */
1684 }
1685 break;
1686 case RTE_GROUP:
1687
1688 /*
1689 * We couldn't get here: the RTE_GROUP RTE has not been added.
1690 */
1691 break;
1692 }
1693
1694 /*
1695 * We now have an expression we can't expand any more, so see if
1696 * get_expr_result_tupdesc() can do anything with it.
1697 */
1698 return get_expr_result_tupdesc(expr, false);
1699}
int16 AttrNumber
Definition attnum.h:21
#define Assert(condition)
Definition c.h:873
unsigned int Index
Definition c.h:628
#define elog(elevel,...)
Definition elog.h:226
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition funcapi.c:551
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition nodeFuncs.c:301
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:821
#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_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:194
List * p_rtable
Definition parse_node.h:196
ParseLoc location
Definition primnodes.h:310
AttrNumber varattno
Definition primnodes.h:274
int varno
Definition primnodes.h:269
VarReturningType varreturningtype
Definition primnodes.h:297
Index varlevelsup
Definition primnodes.h:294
TupleDesc CreateTemplateTupleDesc(int natts)
Definition tupdesc.c:182
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition tupdesc.c:1026
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition tupdesc.c:842
#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_GROUP, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, strVal, 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 1712 of file parse_target.c.

1713{
1714 char *name = NULL;
1715
1717 if (name != NULL)
1718 return name;
1719 /* default result if we can't guess anything */
1720 return "?column?";
1721}
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 1731 of file parse_target.c.

1732{
1733 char *name = NULL;
1734
1736 return name;
1737}

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:768
@ COERCION_IMPLICIT
Definition primnodes.h:746

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 454 of file parse_target.c.

461{
462 Relation rd = pstate->p_target_relation;
463 Oid type_id; /* type of value provided */
464 Oid attrtype; /* type of target column */
465 int32 attrtypmod;
466 Oid attrcollation; /* collation of target column */
468
469 /*
470 * Save and restore identity of expression type we're parsing. We must
471 * set p_expr_kind here because we can parse subscripts without going
472 * through transformExpr().
473 */
475 sv_expr_kind = pstate->p_expr_kind;
476 pstate->p_expr_kind = exprKind;
477
478 Assert(rd != NULL);
479 if (attrno <= 0)
482 errmsg("cannot assign to system column \"%s\"",
483 colname),
484 parser_errposition(pstate, location)));
485 attrtype = attnumTypeId(rd, attrno);
486 attrtypmod = TupleDescAttr(rd->rd_att, attrno - 1)->atttypmod;
487 attrcollation = TupleDescAttr(rd->rd_att, attrno - 1)->attcollation;
488
489 /*
490 * If the expression is a DEFAULT placeholder, insert the attribute's
491 * type/typmod/collation into it so that exprType etc will report the
492 * right things. (We expect that the eventually substituted default
493 * expression will in fact have this type and typmod. The collation
494 * likely doesn't matter, but let's set it correctly anyway.) Also,
495 * reject trying to update a subfield or array element with DEFAULT, since
496 * there can't be any default for portions of a column.
497 */
498 if (expr && IsA(expr, SetToDefault))
499 {
500 SetToDefault *def = (SetToDefault *) expr;
501
502 def->typeId = attrtype;
503 def->typeMod = attrtypmod;
504 def->collation = attrcollation;
505 if (indirection)
506 {
507 if (IsA(linitial(indirection), A_Indices))
510 errmsg("cannot set an array element to DEFAULT"),
511 parser_errposition(pstate, location)));
512 else
515 errmsg("cannot set a subfield to DEFAULT"),
516 parser_errposition(pstate, location)));
517 }
518 }
519
520 /* Now we can use exprType() safely. */
521 type_id = exprType((Node *) expr);
522
523 /*
524 * If there is indirection on the target column, prepare an array or
525 * subfield assignment expression. This will generate a new column value
526 * that the source value has been inserted into, which can then be placed
527 * in the new tuple constructed by INSERT or UPDATE.
528 */
529 if (indirection)
530 {
531 Node *colVar;
532
533 if (pstate->p_is_insert)
534 {
535 /*
536 * The command is INSERT INTO table (col.something) ... so there
537 * is not really a source value to work with. Insert a NULL
538 * constant as the source value.
539 */
540 colVar = (Node *) makeNullConst(attrtype, attrtypmod,
542 }
543 else
544 {
545 /*
546 * Build a Var for the column to be updated.
547 */
548 Var *var;
549
550 var = makeVar(pstate->p_target_nsitem->p_rtindex, attrno,
551 attrtype, attrtypmod, attrcollation, 0);
552 var->location = location;
553
554 colVar = (Node *) var;
555 }
556
557 expr = (Expr *)
559 colVar,
560 colname,
561 false,
562 attrtype,
563 attrtypmod,
565 indirection,
566 list_head(indirection),
567 (Node *) expr,
569 location);
570 }
571 else
572 {
573 /*
574 * For normal non-qualified target column, do type checking and
575 * coercion.
576 */
577 Node *orig_expr = (Node *) expr;
578
579 expr = (Expr *)
581 orig_expr, type_id,
582 attrtype, attrtypmod,
585 -1);
586 if (expr == NULL)
589 errmsg("column \"%s\" is of type %s"
590 " but expression is of type %s",
591 colname,
592 format_type_be(attrtype),
593 format_type_be(type_id)),
594 errhint("You will need to rewrite or cast the expression."),
596 }
597
598 pstate->p_expr_kind = sv_expr_kind;
599
600 return expr;
601}
int32_t int32
Definition c.h:542
int errhint(const char *fmt,...)
Definition elog.c:1330
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:1384
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_NONE
Definition parse_node.h:40
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:747
ParseNamespaceItem * p_target_nsitem
Definition parse_node.h:210
ParseExprKind p_expr_kind
Definition parse_node.h:214
bool p_is_insert
Definition parse_node.h:212

References Assert, attnumTypeId(), COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, ereport, errcode(), errhint(), errmsg(), ERROR, EXPR_KIND_NONE, exprLocation(), exprType(), fb(), format_type_be(), IsA, linitial, list_head(), Var::location, makeNullConst(), makeVar(), ParseState::p_expr_kind, ParseState::p_is_insert, 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 685 of file parse_target.c.

697{
698 Node *result;
700 ListCell *i;
701
703 {
704 /*
705 * Set up a substitution. We abuse CaseTestExpr for this. It's safe
706 * to do so because the only nodes that will be above the CaseTestExpr
707 * in the finished expression will be FieldStore and SubscriptingRef
708 * nodes. (There could be other stuff in the tree, but it will be
709 * within other child fields of those node types.)
710 */
712
713 ctest->typeId = targetTypeId;
714 ctest->typeMod = targetTypMod;
715 ctest->collation = targetCollation;
716 basenode = (Node *) ctest;
717 }
718
719 /*
720 * We have to split any field-selection operations apart from
721 * subscripting. Adjacent A_Indices nodes have to be treated as a single
722 * multidimensional subscript operation.
723 */
724 for_each_cell(i, indirection, indirection_cell)
725 {
726 Node *n = lfirst(i);
727
728 if (IsA(n, A_Indices))
730 else if (IsA(n, A_Star))
731 {
734 errmsg("row expansion via \"*\" is not supported here"),
735 parser_errposition(pstate, location)));
736 }
737 else
738 {
739 FieldStore *fstore;
742 Oid typrelid;
747
748 Assert(IsA(n, String));
749
750 /* process subscripts before this field selection */
751 if (subscripts)
752 {
753 /* recurse, and then return because we're done */
754 return transformAssignmentSubscripts(pstate,
755 basenode,
761 indirection,
762 i,
763 rhs,
764 ccontext,
765 location);
766 }
767
768 /* No subscripts, so can process field selection here */
769
770 /*
771 * Look up the composite type, accounting for possibility that
772 * what we are given is a domain over composite.
773 */
776
777 typrelid = typeidTypeRelid(baseTypeId);
778 if (!typrelid)
781 errmsg("cannot assign to field \"%s\" of column \"%s\" because its type %s is not a composite type",
782 strVal(n), targetName,
784 parser_errposition(pstate, location)));
785
786 attnum = get_attnum(typrelid, strVal(n));
790 errmsg("cannot assign to field \"%s\" of column \"%s\" because there is no such column in data type %s",
791 strVal(n), targetName,
793 parser_errposition(pstate, location)));
794 if (attnum < 0)
797 errmsg("cannot assign to system column \"%s\"",
798 strVal(n)),
799 parser_errposition(pstate, location)));
800
803
804 /* recurse to create appropriate RHS for field assign */
806 NULL,
807 strVal(n),
808 false,
812 indirection,
813 lnext(indirection, i),
814 rhs,
815 ccontext,
816 location);
817
818 /* and build a FieldStore node */
819 fstore = makeNode(FieldStore);
820 fstore->arg = (Expr *) basenode;
821 fstore->newvals = list_make1(rhs);
822 fstore->fieldnums = list_make1_int(attnum);
823 fstore->resulttype = baseTypeId;
824
825 /*
826 * If target is a domain, apply constraints. Notice that this
827 * isn't totally right: the expression tree we build would check
828 * the domain's constraints on a composite value with only this
829 * one field populated or updated, possibly leading to an unwanted
830 * failure. The rewriter will merge together any subfield
831 * assignments to the same table column, resulting in the domain's
832 * constraints being checked only once after we've assigned to all
833 * the fields that the INSERT or UPDATE means to.
834 */
836 return coerce_to_domain((Node *) fstore,
841 location,
842 false);
843
844 return (Node *) fstore;
845 }
846 }
847
848 /* process trailing subscripts, if any */
849 if (subscripts)
850 {
851 /* recurse, and then return because we're done */
852 return transformAssignmentSubscripts(pstate,
853 basenode,
859 indirection,
860 NULL,
861 rhs,
862 ccontext,
863 location);
864 }
865
866 /* base case: just coerce RHS to match target type ID */
867
868 result = coerce_to_target_type(pstate,
869 rhs, exprType(rhs),
871 ccontext,
873 -1);
874 if (result == NULL)
875 {
879 errmsg("subscripted assignment to \"%s\" requires type %s"
880 " but expression is of type %s",
884 errhint("You will need to rewrite or cast the expression."),
885 parser_errposition(pstate, location)));
886 else
889 errmsg("subfield \"%s\" is of type %s"
890 " but expression is of type %s",
894 errhint("You will need to rewrite or cast the expression."),
895 parser_errposition(pstate, location)));
896 }
897
898 return result;
899}
AttrNumber get_attnum(Oid relid, const char *attname)
Definition lsyscache.c:934
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition lsyscache.c:2688
void get_atttypetypmodcoll(Oid relid, AttrNumber attnum, Oid *typid, int32 *typmod, Oid *collid)
Definition lsyscache.c:1019
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:1193
Expr * arg
Definition primnodes.h:1192
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:120
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:215

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:216
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 transformReturningClause(), transformSelectStmt(), and transformUpdateTargetList().

◆ updateTargetListEntry()

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

Definition at line 621 of file parse_target.c.

627{
628 /* Fix up expression as needed */
629 tle->expr = transformAssignedExpr(pstate,
630 tle->expr,
632 colname,
633 attrno,
634 indirection,
635 location);
636
637 /*
638 * Set the resno to identify the target column --- the rewriter and
639 * planner depend on this. We also set the resname to identify the target
640 * column, but this is only for debugging purposes; it should not be
641 * relied on. (In particular, it might be out of date in a stored rule.)
642 */
643 tle->resno = (AttrNumber) attrno;
644 tle->resname = colname;
645}
@ EXPR_KIND_UPDATE_TARGET
Definition parse_node.h:57
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().