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

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

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

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

1734{
1735 char *name = NULL;
1736
1738 return name;
1739}

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 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 */
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 */
477 sv_expr_kind = pstate->p_expr_kind;
478 pstate->p_expr_kind = exprKind;
479
480 Assert(rd != NULL);
481 if (attrno <= 0)
484 errmsg("cannot assign to system column \"%s\"",
485 colname),
486 parser_errposition(pstate, location)));
487 attrtype = attnumTypeId(rd, attrno);
488 attrtypmod = TupleDescAttr(rd->rd_att, attrno - 1)->atttypmod;
489 attrcollation = TupleDescAttr(rd->rd_att, attrno - 1)->attcollation;
490
491 /*
492 * If the expression is a DEFAULT placeholder, insert the attribute's
493 * type/typmod/collation into it so that exprType etc will report the
494 * right things. (We expect that the eventually substituted default
495 * expression will in fact have this type and typmod. The collation
496 * likely doesn't matter, but let's set it correctly anyway.) Also,
497 * reject trying to update a subfield or array element with DEFAULT, since
498 * there can't be any default for portions of a column.
499 */
500 if (expr && IsA(expr, SetToDefault))
501 {
502 SetToDefault *def = (SetToDefault *) expr;
503
504 def->typeId = attrtype;
505 def->typeMod = attrtypmod;
506 def->collation = attrcollation;
507 if (indirection)
508 {
509 if (IsA(linitial(indirection), A_Indices))
512 errmsg("cannot set an array element to DEFAULT"),
513 parser_errposition(pstate, location)));
514 else
517 errmsg("cannot set a subfield to DEFAULT"),
518 parser_errposition(pstate, location)));
519 }
520 }
521
522 /* Now we can use exprType() safely. */
523 type_id = exprType((Node *) expr);
524
525 /*
526 * If there is indirection on the target column, prepare an array or
527 * subfield assignment expression. This will generate a new column value
528 * that the source value has been inserted into, which can then be placed
529 * in the new tuple constructed by INSERT or UPDATE.
530 */
531 if (indirection)
532 {
533 Node *colVar;
534
536 {
537 /*
538 * The command is INSERT INTO table (col.something) ... so there
539 * is not really a source value to work with. Insert a NULL
540 * constant as the source value.
541 */
542 colVar = (Node *) makeNullConst(attrtype, attrtypmod,
544 }
545 else
546 {
547 /*
548 * Build a Var for the column to be updated.
549 */
550 Var *var;
551
552 var = makeVar(pstate->p_target_nsitem->p_rtindex, attrno,
553 attrtype, attrtypmod, attrcollation, 0);
554 var->location = location;
555
556 colVar = (Node *) var;
557 }
558
559 expr = (Expr *)
561 colVar,
562 colname,
563 false,
564 attrtype,
565 attrtypmod,
567 indirection,
568 list_head(indirection),
569 (Node *) expr,
571 location);
572 }
573 else
574 {
575 /*
576 * For normal non-qualified target column, do type checking and
577 * coercion.
578 */
579 Node *orig_expr = (Node *) expr;
580
581 expr = (Expr *)
583 orig_expr, type_id,
584 attrtype, attrtypmod,
587 -1);
588 if (expr == NULL)
591 errmsg("column \"%s\" is of type %s"
592 " but expression is of type %s",
593 colname,
594 format_type_be(attrtype),
595 format_type_be(type_id)),
596 errhint("You will need to rewrite or cast the expression."),
598 }
599
600 pstate->p_expr_kind = sv_expr_kind;
601
602 return expr;
603}
int32_t int32
Definition c.h:554
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: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_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:206
ParseExprKind p_expr_kind
Definition parse_node.h:209

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

699{
700 Node *result;
702 ListCell *i;
703
705 {
706 /*
707 * Set up a substitution. We abuse CaseTestExpr for this. It's safe
708 * to do so because the only nodes that will be above the CaseTestExpr
709 * in the finished expression will be FieldStore and SubscriptingRef
710 * nodes. (There could be other stuff in the tree, but it will be
711 * within other child fields of those node types.)
712 */
714
715 ctest->typeId = targetTypeId;
716 ctest->typeMod = targetTypMod;
717 ctest->collation = targetCollation;
718 basenode = (Node *) ctest;
719 }
720
721 /*
722 * We have to split any field-selection operations apart from
723 * subscripting. Adjacent A_Indices nodes have to be treated as a single
724 * multidimensional subscript operation.
725 */
726 for_each_cell(i, indirection, indirection_cell)
727 {
728 Node *n = lfirst(i);
729
730 if (IsA(n, A_Indices))
732 else if (IsA(n, A_Star))
733 {
736 errmsg("row expansion via \"*\" is not supported here"),
737 parser_errposition(pstate, location)));
738 }
739 else
740 {
741 FieldStore *fstore;
744 Oid typrelid;
749
750 Assert(IsA(n, String));
751
752 /* process subscripts before this field selection */
753 if (subscripts)
754 {
755 /* recurse, and then return because we're done */
756 return transformAssignmentSubscripts(pstate,
757 basenode,
763 indirection,
764 i,
765 rhs,
766 ccontext,
767 location);
768 }
769
770 /* No subscripts, so can process field selection here */
771
772 /*
773 * Look up the composite type, accounting for possibility that
774 * what we are given is a domain over composite.
775 */
778
779 typrelid = typeidTypeRelid(baseTypeId);
780 if (!typrelid)
783 errmsg("cannot assign to field \"%s\" of column \"%s\" because its type %s is not a composite type",
784 strVal(n), targetName,
786 parser_errposition(pstate, location)));
787
788 attnum = get_attnum(typrelid, strVal(n));
792 errmsg("cannot assign to field \"%s\" of column \"%s\" because there is no such column in data type %s",
793 strVal(n), targetName,
795 parser_errposition(pstate, location)));
796 if (attnum < 0)
799 errmsg("cannot assign to system column \"%s\"",
800 strVal(n)),
801 parser_errposition(pstate, location)));
802
805
806 /* recurse to create appropriate RHS for field assign */
808 NULL,
809 strVal(n),
810 false,
814 indirection,
815 lnext(indirection, i),
816 rhs,
817 ccontext,
818 location);
819
820 /* and build a FieldStore node */
821 fstore = makeNode(FieldStore);
822 fstore->arg = (Expr *) basenode;
823 fstore->newvals = list_make1(rhs);
824 fstore->fieldnums = list_make1_int(attnum);
825 fstore->resulttype = baseTypeId;
826
827 /*
828 * If target is a domain, apply constraints. Notice that this
829 * isn't totally right: the expression tree we build would check
830 * the domain's constraints on a composite value with only this
831 * one field populated or updated, possibly leading to an unwanted
832 * failure. The rewriter will merge together any subfield
833 * assignments to the same table column, resulting in the domain's
834 * constraints being checked only once after we've assigned to all
835 * the fields that the INSERT or UPDATE means to.
836 */
838 return coerce_to_domain((Node *) fstore,
843 location,
844 false);
845
846 return (Node *) fstore;
847 }
848 }
849
850 /* process trailing subscripts, if any */
851 if (subscripts)
852 {
853 /* recurse, and then return because we're done */
854 return transformAssignmentSubscripts(pstate,
855 basenode,
861 indirection,
862 NULL,
863 rhs,
864 ccontext,
865 location);
866 }
867
868 /* base case: just coerce RHS to match target type ID */
869
870 result = coerce_to_target_type(pstate,
871 rhs, exprType(rhs),
873 ccontext,
875 -1);
876 if (result == NULL)
877 {
881 errmsg("subscripted assignment to \"%s\" requires type %s"
882 " but expression is of type %s",
886 errhint("You will need to rewrite or cast the expression."),
887 parser_errposition(pstate, location)));
888 else
891 errmsg("subfield \"%s\" is of type %s"
892 " but expression is of type %s",
896 errhint("You will need to rewrite or cast the expression."),
897 parser_errposition(pstate, location)));
898 }
899
900 return result;
901}
AttrNumber get_attnum(Oid relid, const char *attname)
Definition lsyscache.c:934
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition lsyscache.c:2690
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: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: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:210

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:211
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 623 of file parse_target.c.

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