PostgreSQL Source Code git master
Functions
parse_target.h File Reference
#include "parser/parse_node.h"
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 ( ParseStatepstate,
Listcols,
List **  attrnos 
)

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
1040 col = makeNode(ResTarget);
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 */
1054 Bitmapset *wholecols = NULL;
1055 Bitmapset *partialcols = NULL;
1056 ListCell *tl;
1057
1058 foreach(tl, cols)
1059 {
1060 ResTarget *col = (ResTarget *) lfirst(tl);
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);
1066 if (attrno == InvalidAttrNumber)
1067 ereport(ERROR,
1068 (errcode(ERRCODE_UNDEFINED_COLUMN),
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 */
1081 if (bms_is_member(attrno, wholecols) ||
1082 bms_is_member(attrno, partialcols))
1083 ereport(ERROR,
1084 (errcode(ERRCODE_DUPLICATE_COLUMN),
1085 errmsg("column \"%s\" specified more than once",
1086 name),
1087 parser_errposition(pstate, col->location)));
1088 wholecols = bms_add_member(wholecols, attrno);
1089 }
1090 else
1091 {
1092 /* partial column; must not have any whole assignment */
1093 if (bms_is_member(attrno, wholecols))
1094 ereport(ERROR,
1095 (errcode(ERRCODE_DUPLICATE_COLUMN),
1096 errmsg("column \"%s\" specified more than once",
1097 name),
1098 parser_errposition(pstate, col->location)));
1099 partialcols = bms_add_member(partialcols, attrno);
1100 }
1101
1102 *attrnos = lappend_int(*attrnos, attrno);
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:815
#define NameStr(name)
Definition: c.h:751
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
i
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:1759
#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
Definition: pg_attribute.h:202
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
#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
Node * val
Definition: parsenodes.h:547
ParseLoc location
Definition: parsenodes.h:548
List * indirection
Definition: parsenodes.h:546
char * name
Definition: parsenodes.h:545
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160
Definition: pg_list.h:46
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 ( ParseStatepstate,
Varvar,
int  levelsup 
)

Definition at line 1521 of file parse_target.c.

1522{
1523 TupleDesc tupleDesc;
1524 int netlevelsup;
1525 RangeTblEntry *rte;
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;
1540 rte = GetRTEByRangeTablePosn(pstate, var->varno, netlevelsup);
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
1562 TupleDescInitEntry(tupleDesc, i,
1563 label,
1564 exprType(varnode),
1565 exprTypmod(varnode),
1566 0);
1567 TupleDescInitEntryCollation(tupleDesc, i,
1568 exprCollation(varnode));
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 */
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 {
1653 CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
1654 TargetEntry *ste;
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
unsigned int Index
Definition: c.h:619
#define elog(elevel,...)
Definition: elog.h:226
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:551
Assert(PointerIsAligned(start, uint64))
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
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)
Definition: parse_target.c:1521
#define GetCTETargetList(cte)
Definition: parsenodes.h:1736
@ RTE_JOIN
Definition: parsenodes.h:1045
@ RTE_CTE
Definition: parsenodes.h:1049
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1050
@ RTE_VALUES
Definition: parsenodes.h:1048
@ RTE_SUBQUERY
Definition: parsenodes.h:1044
@ RTE_RESULT
Definition: parsenodes.h:1051
@ RTE_FUNCTION
Definition: parsenodes.h:1046
@ RTE_TABLEFUNC
Definition: parsenodes.h:1047
@ RTE_GROUP
Definition: parsenodes.h:1054
@ RTE_RELATION
Definition: parsenodes.h:1043
int16 attnum
Definition: pg_attribute.h:74
static char * label
Definition: pg_basebackup.c:135
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
Node * ctequery
Definition: parsenodes.h:1712
Definition: pg_list.h:54
Definition: nodes.h:135
ParseState * parentParseState
Definition: parse_node.h:194
List * p_rtable
Definition: parse_node.h:196
Definition: parsenodes.h:118
List * rtable
Definition: parsenodes.h:175
List * targetList
Definition: parsenodes.h:198
Index ctelevelsup
Definition: parsenodes.h:1229
Query * subquery
Definition: parsenodes.h:1135
RTEKind rtekind
Definition: parsenodes.h:1078
Expr * expr
Definition: primnodes.h:2239
Definition: primnodes.h:262
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
Definition: regcomp.c:282
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(), 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 ( Nodenode )

Definition at line 1712 of file parse_target.c.

1713{
1714 char *name = NULL;
1715
1716 (void) FigureColnameInternal(node, &name);
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)
Definition: parse_target.c:1751

References FigureColnameInternal(), and name.

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

FigureIndexColname()

char * FigureIndexColname ( Nodenode )

Definition at line 1731 of file parse_target.c.

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

References FigureColnameInternal(), and name.

Referenced by transformIndexStmt().

markTargetListOrigins()

void markTargetListOrigins ( ParseStatepstate,
Listtargetlist 
)

Definition at line 317 of file parse_target.c.

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

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

Referenced by transformReturningClause(), and transformSelectStmt().

resolveTargetListUnknowns()

void resolveTargetListUnknowns ( ParseStatepstate,
Listtargetlist 
)

Definition at line 287 of file parse_target.c.

288{
289 ListCell *l;
290
291 foreach(l, targetlist)
292 {
293 TargetEntry *tle = (TargetEntry *) lfirst(l);
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)
Definition: parse_coerce.c:158
unsigned int Oid
Definition: postgres_ext.h:32
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:768
@ COERCION_IMPLICIT
Definition: primnodes.h:746
Definition: primnodes.h:189

References COERCE_IMPLICIT_CAST, coerce_type(), COERCION_IMPLICIT, TargetEntry::expr, exprType(), and lfirst.

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

transformAssignedExpr()

Expr * transformAssignedExpr ( ParseStatepstate,
Exprexpr,
ParseExprKind  exprKind,
const char *  colname,
int  attrno,
Listindirection,
int  location 
)

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 */
467 ParseExprKind sv_expr_kind;
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 */
474 Assert(exprKind != EXPR_KIND_NONE);
475 sv_expr_kind = pstate->p_expr_kind;
476 pstate->p_expr_kind = exprKind;
477
478 Assert(rd != NULL);
479 if (attrno <= 0)
481 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
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))
509 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
510 errmsg("cannot set an array element to DEFAULT"),
511 parser_errposition(pstate, location)));
512 else
514 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
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,
541 attrcollation);
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,
564 attrcollation,
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)
588 (errcode(ERRCODE_DATATYPE_MISMATCH),
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."),
595 parser_errposition(pstate, exprLocation(orig_expr))));
596 }
597
598 pstate->p_expr_kind = sv_expr_kind;
599
600 return expr;
601}
int32_t int32
Definition: c.h:534
int errhint(const char *fmt,...)
Definition: elog.c:1321
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:388
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:79
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:685
#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
Definition: rel.h:56
Oid typeId
Definition: primnodes.h:2097

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 ( ParseStatepstate,
Nodebasenode,
const char *  targetName,
bool  targetIsSubscripting,
Oid  targetTypeId,
int32  targetTypMod,
Oid  targetCollation,
Listindirection,
ListCellindirection_cell,
Noderhs,
CoercionContext  ccontext,
int  location 
)

Definition at line 685 of file parse_target.c.

697{
698 Node *result;
699 List *subscripts = NIL;
700 ListCell *i;
701
702 if (indirection_cell && !basenode)
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))
729 subscripts = lappend(subscripts, n);
730 else if (IsA(n, A_Star))
731 {
733 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
734 errmsg("row expansion via \"*\" is not supported here"),
735 parser_errposition(pstate, location)));
736 }
737 else
738 {
739 FieldStore *fstore;
740 Oid baseTypeId;
741 int32 baseTypeMod;
742 Oid typrelid;
744 Oid fieldTypeId;
745 int32 fieldTypMod;
746 Oid fieldCollation;
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,
756 targetName,
757 targetTypeId,
758 targetTypMod,
759 targetCollation,
760 subscripts,
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 */
774 baseTypeMod = targetTypMod;
775 baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
776
777 typrelid = typeidTypeRelid(baseTypeId);
778 if (!typrelid)
780 (errcode(ERRCODE_DATATYPE_MISMATCH),
781 errmsg("cannot assign to field \"%s\" of column \"%s\" because its type %s is not a composite type",
782 strVal(n), targetName,
783 format_type_be(targetTypeId)),
784 parser_errposition(pstate, location)));
785
786 attnum = get_attnum(typrelid, strVal(n));
789 (errcode(ERRCODE_UNDEFINED_COLUMN),
790 errmsg("cannot assign to field \"%s\" of column \"%s\" because there is no such column in data type %s",
791 strVal(n), targetName,
792 format_type_be(targetTypeId)),
793 parser_errposition(pstate, location)));
794 if (attnum < 0)
796 (errcode(ERRCODE_UNDEFINED_COLUMN),
797 errmsg("cannot assign to system column \"%s\"",
798 strVal(n)),
799 parser_errposition(pstate, location)));
800
802 &fieldTypeId, &fieldTypMod, &fieldCollation);
803
804 /* recurse to create appropriate RHS for field assign */
806 NULL,
807 strVal(n),
808 false,
809 fieldTypeId,
810 fieldTypMod,
811 fieldCollation,
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 */
835 if (baseTypeId != targetTypeId)
836 return coerce_to_domain((Node *) fstore,
837 baseTypeId, baseTypeMod,
838 targetTypeId,
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,
854 targetName,
855 targetTypeId,
856 targetTypMod,
857 targetCollation,
858 subscripts,
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),
870 targetTypeId, targetTypMod,
871 ccontext,
873 -1);
874 if (result == NULL)
875 {
876 if (targetIsSubscripting)
878 (errcode(ERRCODE_DATATYPE_MISMATCH),
879 errmsg("subscripted assignment to \"%s\" requires type %s"
880 " but expression is of type %s",
881 targetName,
882 format_type_be(targetTypeId),
884 errhint("You will need to rewrite or cast the expression."),
885 parser_errposition(pstate, location)));
886 else
888 (errcode(ERRCODE_DATATYPE_MISMATCH),
889 errmsg("subfield \"%s\" is of type %s"
890 " but expression is of type %s",
891 targetName,
892 format_type_be(targetTypeId),
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:951
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2705
void get_atttypetypmodcoll(Oid relid, AttrNumber attnum, Oid *typid, int32 *typmod, Oid *collid)
Definition: lsyscache.c:1036
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:905
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
Oid typeId
Definition: primnodes.h:1388
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(), 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 transformPLAssignStmtTarget().

transformExpressionList()

List * transformExpressionList ( ParseStatepstate,
Listexprlist,
ParseExprKind  exprKind,
bool  allowDefault 
)

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,
242 ExpandColumnRefStar(pstate, cref,
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 */
267 if (allowDefault && IsA(e, SetToDefault))
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:118
static List * ExpandColumnRefStar(ParseState *pstate, ColumnRef *cref, bool make_target_entry)
Definition: parse_target.c:1122
static List * ExpandIndirectionStar(ParseState *pstate, A_Indirection *ind, bool make_target_entry, ParseExprKind exprKind)
Definition: parse_target.c:1347
struct A_Star A_Star
#define llast(l)
Definition: pg_list.h:198
e
e
Definition: preproc-init.c:82
List * fields
Definition: parsenodes.h:311

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

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

transformTargetEntry()

TargetEntry * transformTargetEntry ( ParseStatepstate,
Nodenode,
Nodeexpr,
ParseExprKind  exprKind,
char *  colname,
bool  resjunk 
)

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 */
89 if (exprKind == EXPR_KIND_UPDATE_SOURCE && IsA(node, SetToDefault))
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)
Definition: parse_target.c:1712
int p_next_resno
Definition: parse_node.h:215

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

Referenced by findTargetlistEntrySQL99(), and transformTargetList().

transformTargetList()

List * transformTargetList ( ParseStatepstate,
Listtargetlist,
ParseExprKind  exprKind 
)

Definition at line 120 of file parse_target.c.

122{
123 List *p_target = NIL;
124 bool expand_star;
125 ListCell *o_target;
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 */
131 expand_star = (exprKind != EXPR_KIND_UPDATE_SOURCE);
132
133 foreach(o_target, targetlist)
134 {
135 ResTarget *res = (ResTarget *) lfirst(o_target);
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 */
151 p_target = list_concat(p_target,
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 */
165 p_target = list_concat(p_target,
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 */
179 p_target = lappend(p_target,
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 {
196 Assert(exprKind == EXPR_KIND_UPDATE_SOURCE);
197 p_target = list_concat(p_target, pstate->p_multiassign_exprs);
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)
Definition: parse_target.c:74
List * p_multiassign_exprs
Definition: parse_node.h:216

References Assert(), ExpandColumnRefStar(), ExpandIndirectionStar(), EXPR_KIND_UPDATE_SOURCE, ColumnRef::fields, if(), 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 ( ParseStatepstate,
TargetEntrytle,
char *  colname,
int  attrno,
Listindirection,
int  location 
)

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)
Definition: parse_target.c:454
AttrNumber resno
Definition: primnodes.h:2241

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

Referenced by transformUpdateTargetList().

AltStyle によって変換されたページ (->オリジナル) /