PostgreSQL Source Code git master
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)
 
char * FigureColname (Node *node)
 
char * FigureIndexColname (Node *node)
 

Function Documentation

◆ checkInsertTargets()

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

Definition at line 1018 of file parse_target.c.

1019{
1020 *attrnos = NIL;
1021
1022 if (cols == NIL)
1023 {
1024 /*
1025 * Generate default column list for INSERT.
1026 */
1028
1029 int i;
1030
1031 for (i = 0; i < numcol; i++)
1032 {
1033 ResTarget *col;
1034 Form_pg_attribute attr;
1035
1036 attr = TupleDescAttr(pstate->p_target_relation->rd_att, i);
1037
1038 if (attr->attisdropped)
1039 continue;
1040
1041 col = makeNode(ResTarget);
1042 col->name = pstrdup(NameStr(attr->attname));
1043 col->indirection = NIL;
1044 col->val = NULL;
1045 col->location = -1;
1046 cols = lappend(cols, col);
1047 *attrnos = lappend_int(*attrnos, i + 1);
1048 }
1049 }
1050 else
1051 {
1052 /*
1053 * Do initial validation of user-supplied INSERT column list.
1054 */
1055 Bitmapset *wholecols = NULL;
1056 Bitmapset *partialcols = NULL;
1057 ListCell *tl;
1058
1059 foreach(tl, cols)
1060 {
1061 ResTarget *col = (ResTarget *) lfirst(tl);
1062 char *name = col->name;
1063 int attrno;
1064
1065 /* Lookup column name, ereport on failure */
1066 attrno = attnameAttNum(pstate->p_target_relation, name, false);
1067 if (attrno == InvalidAttrNumber)
1068 ereport(ERROR,
1069 (errcode(ERRCODE_UNDEFINED_COLUMN),
1070 errmsg("column \"%s\" of relation \"%s\" does not exist",
1071 name,
1073 parser_errposition(pstate, col->location)));
1074
1075 /*
1076 * Check for duplicates, but only of whole columns --- we allow
1077 * INSERT INTO foo (col.subcol1, col.subcol2)
1078 */
1079 if (col->indirection == NIL)
1080 {
1081 /* whole column; must not have any other assignment */
1082 if (bms_is_member(attrno, wholecols) ||
1083 bms_is_member(attrno, partialcols))
1084 ereport(ERROR,
1085 (errcode(ERRCODE_DUPLICATE_COLUMN),
1086 errmsg("column \"%s\" specified more than once",
1087 name),
1088 parser_errposition(pstate, col->location)));
1089 wholecols = bms_add_member(wholecols, attrno);
1090 }
1091 else
1092 {
1093 /* partial column; must not have any whole assignment */
1094 if (bms_is_member(attrno, wholecols))
1095 ereport(ERROR,
1096 (errcode(ERRCODE_DUPLICATE_COLUMN),
1097 errmsg("column \"%s\" specified more than once",
1098 name),
1099 parser_errposition(pstate, col->location)));
1100 partialcols = bms_add_member(partialcols, attrno);
1101 }
1102
1103 *attrnos = lappend_int(*attrnos, attrno);
1104 }
1105 }
1106
1107 return cols;
1108}
#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:815
#define NameStr(name)
Definition: c.h:703
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
int i
Definition: isn.c:72
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:1696
#define makeNode(_type_)
Definition: nodes.h:155
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
Definition: pg_attribute.h:200
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:511
#define RelationGetRelationName(relation)
Definition: rel.h:539
Relation p_target_relation
Definition: parse_node.h:225
TupleDesc rd_att
Definition: rel.h:112
Node * val
Definition: parsenodes.h:530
ParseLoc location
Definition: parsenodes.h:531
List * indirection
Definition: parsenodes.h:529
char * name
Definition: parsenodes.h:528
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:153
const char * name

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

Referenced by transformInsertStmt(), and transformMergeStmt().

◆ expandRecordVariable()

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

Definition at line 1522 of file parse_target.c.

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

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

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

◆ FigureColname()

char * FigureColname ( Node node)

Definition at line 1713 of file parse_target.c.

1714{
1715 char *name = NULL;
1716
1717 (void) FigureColnameInternal(node, &name);
1718 if (name != NULL)
1719 return name;
1720 /* default result if we can't guess anything */
1721 return "?column?";
1722}
static int FigureColnameInternal(Node *node, char **name)

References FigureColnameInternal(), and name.

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

◆ FigureIndexColname()

char * FigureIndexColname ( Node node)

Definition at line 1732 of file parse_target.c.

1733{
1734 char *name = NULL;
1735
1736 (void) FigureColnameInternal(node, &name);
1737 return name;
1738}

References FigureColnameInternal(), and name.

Referenced by transformIndexStmt().

◆ markTargetListOrigins()

void markTargetListOrigins ( ParseState pstate,
List targetlist 
)

Definition at line 318 of file parse_target.c.

319{
320 ListCell *l;
321
322 foreach(l, targetlist)
323 {
324 TargetEntry *tle = (TargetEntry *) lfirst(l);
325
326 markTargetListOrigin(pstate, tle, (Var *) tle->expr, 0);
327 }
328}
static void markTargetListOrigin(ParseState *pstate, TargetEntry *tle, Var *var, int levelsup)
Definition: parse_target.c:343

References TargetEntry::expr, lfirst, and markTargetListOrigin().

Referenced by transformReturningClause(), and transformSelectStmt().

◆ resolveTargetListUnknowns()

void resolveTargetListUnknowns ( ParseState pstate,
List targetlist 
)

Definition at line 288 of file parse_target.c.

289{
290 ListCell *l;
291
292 foreach(l, targetlist)
293 {
294 TargetEntry *tle = (TargetEntry *) lfirst(l);
295 Oid restype = exprType((Node *) tle->expr);
296
297 if (restype == UNKNOWNOID)
298 {
299 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
300 restype, TEXTOID, -1,
303 -1);
304 }
305 }
306}
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:157
unsigned int Oid
Definition: postgres_ext.h:32
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:752
@ COERCION_IMPLICIT
Definition: primnodes.h:730

References COERCE_IMPLICIT_CAST, coerce_type(), COERCION_IMPLICIT, TargetEntry::expr, exprType(), 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 
)

Definition at line 455 of file parse_target.c.

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

References Assert, attnumTypeId(), COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, ereport, errcode(), errhint(), errmsg(), ERROR, EXPR_KIND_NONE, exprLocation(), exprType(), 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(), RelationData::rd_att, 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 
)

Definition at line 686 of file parse_target.c.

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

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

◆ transformExpressionList()

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

Definition at line 220 of file parse_target.c.

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

References ExpandColumnRefStar(), ExpandIndirectionStar(), ColumnRef::fields, IsA, lappend(), lfirst, list_concat(), llast, NIL, and transformExpr().

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

◆ transformTargetEntry()

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

Definition at line 75 of file parse_target.c.

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

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

Referenced by findTargetlistEntrySQL99(), and transformTargetList().

◆ transformTargetList()

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

Definition at line 121 of file parse_target.c.

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

References Assert, ExpandColumnRefStar(), ExpandIndirectionStar(), EXPR_KIND_UPDATE_SOURCE, ColumnRef::fields, if(), IsA, lappend(), lfirst, list_concat(), llast, NIL, ParseState::p_multiassign_exprs, res, and transformTargetEntry().

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

◆ updateTargetListEntry()

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

Definition at line 622 of file parse_target.c.

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

References TargetEntry::expr, EXPR_KIND_UPDATE_TARGET, TargetEntry::resno, and transformAssignedExpr().

Referenced by transformUpdateTargetList().