PostgreSQL Source Code git master
Loading...
Searching...
No Matches
parse_target.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * parse_target.c
4 * handle target lists
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/parser/parse_target.c
12 *
13 *-------------------------------------------------------------------------
14 */
15#include "postgres.h"
16
17#include "catalog/namespace.h"
18#include "catalog/pg_type.h"
19#include "funcapi.h"
20#include "miscadmin.h"
21#include "nodes/makefuncs.h"
22#include "nodes/nodeFuncs.h"
23#include "parser/parse_coerce.h"
24#include "parser/parse_expr.h"
26#include "parser/parse_target.h"
27#include "parser/parse_type.h"
28#include "parser/parsetree.h"
29#include "utils/builtins.h"
30#include "utils/lsyscache.h"
31#include "utils/rel.h"
32
34 Var *var, int levelsup);
37 const char *targetName,
42 List *indirection,
44 Node *rhs,
46 int location);
49static List *ExpandAllTables(ParseState *pstate, int location);
53 int sublevels_up, int location,
55static List *ExpandRowReference(ParseState *pstate, Node *expr,
57static int FigureColnameInternal(Node *node, char **name);
58
59
60/*
61 * transformTargetEntry()
62 * Transform any ordinary "expression-type" node into a targetlist entry.
63 * This is exported so that parse_clause.c can generate targetlist entries
64 * for ORDER/GROUP BY items that are not already in the targetlist.
65 *
66 * node the (untransformed) parse tree for the value expression.
67 * expr the transformed expression, or NULL if caller didn't do it yet.
68 * exprKind expression kind (EXPR_KIND_SELECT_TARGET, etc)
69 * colname the column name to be assigned, or NULL if none yet set.
70 * resjunk true if the target should be marked resjunk, ie, it is not
71 * wanted in the final projected tuple.
72 */
75 Node *node,
76 Node *expr,
78 char *colname,
79 bool resjunk)
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}
109
110
111/*
112 * transformTargetList()
113 * Turns a list of ResTarget's into a list of TargetEntry's.
114 *
115 * This code acts mostly the same for SELECT, UPDATE, or RETURNING lists;
116 * the main thing is to transform the given expressions (the "val" fields).
117 * The exprKind parameter distinguishes these cases when necessary.
118 */
119List *
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}
203
204
205/*
206 * transformExpressionList()
207 *
208 * This is the identical transformation to transformTargetList, except that
209 * the input list elements are bare expressions without ResTarget decoration,
210 * and the output elements are likewise just expressions without TargetEntry
211 * decoration. Also, we don't expect any multiassign constructs within the
212 * list, so there's nothing to do for that. We use this for ROW() and
213 * VALUES() constructs.
214 *
215 * exprKind is not enough to tell us whether to allow SetToDefault, so
216 * an additional flag is needed for that.
217 */
218List *
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}
277
278
279/*
280 * resolveTargetListUnknowns()
281 * Convert any unknown-type targetlist entries to type TEXT.
282 *
283 * We do this after we've exhausted all other ways of identifying the output
284 * column types of a query.
285 */
286void
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}
306
307
308/*
309 * markTargetListOrigins()
310 * Mark targetlist columns that are simple Vars with the source
311 * table's OID and column number.
312 *
313 * Currently, this is done only for SELECT targetlists and RETURNING lists,
314 * since we only need the info if we are going to send it to the frontend.
315 */
316void
318{
319 ListCell *l;
320
321 foreach(l, targetlist)
322 {
324
325 markTargetListOrigin(pstate, tle, (Var *) tle->expr, 0);
326 }
327}
328
329/*
330 * markTargetListOrigin()
331 * If 'var' is a Var of a plain relation, mark 'tle' with its origin
332 *
333 * levelsup is an extra offset to interpret the Var's varlevelsup correctly.
334 *
335 * Note that we do not drill down into views, but report the view as the
336 * column owner. There's also no need to drill down into joins: if we see
337 * a join alias Var, it must be a merged JOIN USING column (or possibly a
338 * whole-row Var); that is not a direct reference to any plain table column,
339 * so we don't report it.
340 */
341static void
343 Var *var, int levelsup)
344{
345 int netlevelsup;
348
349 if (var == NULL || !IsA(var, Var))
350 return;
351 netlevelsup = var->varlevelsup + levelsup;
353 attnum = var->varattno;
354
355 switch (rte->rtekind)
356 {
357 case RTE_RELATION:
358 /* It's a table or view, report it */
359 tle->resorigtbl = rte->relid;
360 tle->resorigcol = attnum;
361 break;
362 case RTE_GRAPH_TABLE:
363 tle->resorigtbl = rte->relid;
364 tle->resorigcol = InvalidAttrNumber;
365 break;
366 case RTE_SUBQUERY:
367 /* Subselect-in-FROM: copy up from the subselect */
369 {
370 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
371 attnum);
372
373 if (ste == NULL || ste->resjunk)
374 elog(ERROR, "subquery %s does not have attribute %d",
375 rte->eref->aliasname, attnum);
376 tle->resorigtbl = ste->resorigtbl;
377 tle->resorigcol = ste->resorigcol;
378 }
379 break;
380 case RTE_JOIN:
381 case RTE_FUNCTION:
382 case RTE_VALUES:
383 case RTE_TABLEFUNC:
385 case RTE_RESULT:
386 /* not a simple relation, leave it unmarked */
387 break;
388 case RTE_CTE:
389
390 /*
391 * CTE reference: copy up from the subquery, if possible. If the
392 * RTE is a recursive self-reference then we can't do anything
393 * because we haven't finished analyzing it yet. However, it's no
394 * big loss because we must be down inside the recursive term of a
395 * recursive CTE, and so any markings on the current targetlist
396 * are not going to affect the results anyway.
397 */
398 if (attnum != InvalidAttrNumber && !rte->self_reference)
399 {
402 List *tl = GetCTETargetList(cte);
403 int extra_cols = 0;
404
405 /*
406 * RTE for CTE will already have the search and cycle columns
407 * added, but the subquery won't, so skip looking those up.
408 */
409 if (cte->search_clause)
410 extra_cols += 1;
411 if (cte->cycle_clause)
412 extra_cols += 2;
413 if (extra_cols &&
414 attnum > list_length(tl) &&
416 break;
417
419 if (ste == NULL || ste->resjunk)
420 elog(ERROR, "CTE %s does not have attribute %d",
421 rte->eref->aliasname, attnum);
422 tle->resorigtbl = ste->resorigtbl;
423 tle->resorigcol = ste->resorigcol;
424 }
425 break;
426 case RTE_GROUP:
427 /* We couldn't get here: the RTE_GROUP RTE has not been added */
428 break;
429 }
430}
431
432
433/*
434 * transformAssignedExpr()
435 * This is used in INSERT and UPDATE statements only. It prepares an
436 * expression for assignment to a column of the target table.
437 * This includes coercing the given value to the target column's type
438 * (if necessary), and dealing with any subfield names or subscripts
439 * attached to the target column itself. The input expression has
440 * already been through transformExpr().
441 *
442 * pstate parse state
443 * expr expression to be modified
444 * exprKind indicates which type of statement we're dealing with
445 * (EXPR_KIND_INSERT_TARGET or EXPR_KIND_UPDATE_TARGET)
446 * colname target column name (ie, name of attribute to be assigned to)
447 * attrno target attribute number
448 * indirection subscripts/field names for target column, if any
449 * location error cursor position for the target column, or -1
450 *
451 * Returns the modified expression.
452 *
453 * Note: location points at the target column name (SET target or INSERT
454 * column name list entry), and must therefore be -1 in an INSERT that
455 * omits the column name list. So we should usually prefer to use
456 * exprLocation(expr) for errors that can happen in a default INSERT.
457 */
458Expr *
460 Expr *expr,
462 const char *colname,
463 int attrno,
464 List *indirection,
465 int location)
466{
467 Relation rd = pstate->p_target_relation;
468 Oid type_id; /* type of value provided */
469 Oid attrtype; /* type of target column */
470 int32 attrtypmod;
471 Oid attrcollation; /* collation of target column */
473
474 /*
475 * Save and restore identity of expression type we're parsing. We must
476 * set p_expr_kind here because we can parse subscripts without going
477 * through transformExpr().
478 */
481 sv_expr_kind = pstate->p_expr_kind;
482 pstate->p_expr_kind = exprKind;
483
484 Assert(rd != NULL);
485 if (attrno <= 0)
488 errmsg("cannot assign to system column \"%s\"",
489 colname),
490 parser_errposition(pstate, location)));
491 attrtype = attnumTypeId(rd, attrno);
492 attrtypmod = TupleDescAttr(rd->rd_att, attrno - 1)->atttypmod;
493 attrcollation = TupleDescAttr(rd->rd_att, attrno - 1)->attcollation;
494
495 /*
496 * If the expression is a DEFAULT placeholder, insert the attribute's
497 * type/typmod/collation into it so that exprType etc will report the
498 * right things. (We expect that the eventually substituted default
499 * expression will in fact have this type and typmod. The collation
500 * likely doesn't matter, but let's set it correctly anyway.) Also,
501 * reject trying to update a subfield or array element with DEFAULT, since
502 * there can't be any default for portions of a column.
503 */
504 if (expr && IsA(expr, SetToDefault))
505 {
506 SetToDefault *def = (SetToDefault *) expr;
507
508 def->typeId = attrtype;
509 def->typeMod = attrtypmod;
510 def->collation = attrcollation;
511 if (indirection)
512 {
513 if (IsA(linitial(indirection), A_Indices))
516 errmsg("cannot set an array element to DEFAULT"),
517 parser_errposition(pstate, location)));
518 else
521 errmsg("cannot set a subfield to DEFAULT"),
522 parser_errposition(pstate, location)));
523 }
524 }
525
526 /* Now we can use exprType() safely. */
527 type_id = exprType((Node *) expr);
528
529 /*
530 * If there is indirection on the target column, prepare an array or
531 * subfield assignment expression. This will generate a new column value
532 * that the source value has been inserted into, which can then be placed
533 * in the new tuple constructed by INSERT or UPDATE.
534 */
535 if (indirection)
536 {
537 Node *colVar;
538
540 {
541 /*
542 * The command is INSERT INTO table (col.something) ... so there
543 * is not really a source value to work with. Insert a NULL
544 * constant as the source value.
545 */
546 colVar = (Node *) makeNullConst(attrtype, attrtypmod,
548 }
549 else
550 {
551 /*
552 * Build a Var for the column to be updated.
553 */
554 Var *var;
555
556 var = makeVar(pstate->p_target_nsitem->p_rtindex, attrno,
557 attrtype, attrtypmod, attrcollation, 0);
558 var->location = location;
559
560 colVar = (Node *) var;
561 }
562
563 expr = (Expr *)
565 colVar,
566 colname,
567 false,
568 attrtype,
569 attrtypmod,
571 indirection,
572 list_head(indirection),
573 (Node *) expr,
575 location);
576 }
577 else
578 {
579 /*
580 * For normal non-qualified target column, do type checking and
581 * coercion.
582 */
583 Node *orig_expr = (Node *) expr;
584
585 expr = (Expr *)
587 orig_expr, type_id,
588 attrtype, attrtypmod,
591 -1);
592 if (expr == NULL)
595 errmsg("column \"%s\" is of type %s"
596 " but expression is of type %s",
597 colname,
598 format_type_be(attrtype),
599 format_type_be(type_id)),
600 errhint("You will need to rewrite or cast the expression."),
602 }
603
604 pstate->p_expr_kind = sv_expr_kind;
605
606 return expr;
607}
608
609
610/*
611 * updateTargetListEntry()
612 * This is used in UPDATE statements (and ON CONFLICT DO UPDATE)
613 * only. It prepares an UPDATE TargetEntry for assignment to a
614 * column of the target table. This includes coercing the given
615 * value to the target column's type (if necessary), and dealing with
616 * any subfield names or subscripts attached to the target column
617 * itself.
618 *
619 * pstate parse state
620 * tle target list entry to be modified
621 * colname target column name (ie, name of attribute to be assigned to)
622 * attrno target attribute number
623 * indirection subscripts/field names for target column, if any
624 * location error cursor position (should point at column name), or -1
625 */
626void
629 char *colname,
630 int attrno,
631 List *indirection,
632 int location)
633{
634 /* Fix up expression as needed */
635 tle->expr = transformAssignedExpr(pstate,
636 tle->expr,
638 colname,
639 attrno,
640 indirection,
641 location);
642
643 /*
644 * Set the resno to identify the target column --- the rewriter and
645 * planner depend on this. We also set the resname to identify the target
646 * column, but this is only for debugging purposes; it should not be
647 * relied on. (In particular, it might be out of date in a stored rule.)
648 */
649 tle->resno = (AttrNumber) attrno;
650 tle->resname = colname;
651}
652
653
654/*
655 * Process indirection (field selection or subscripting) of the target
656 * column in INSERT/UPDATE/assignment. This routine recurses for multiple
657 * levels of indirection --- but note that several adjacent A_Indices nodes
658 * in the indirection list are treated as a single multidimensional subscript
659 * operation.
660 *
661 * In the initial call, basenode is a Var for the target column in UPDATE,
662 * or a null Const of the target's type in INSERT, or a Param for the target
663 * variable in PL/pgSQL assignment. In recursive calls, basenode is NULL,
664 * indicating that a substitute node should be consed up if needed.
665 *
666 * targetName is the name of the field or subfield we're assigning to, and
667 * targetIsSubscripting is true if we're subscripting it. These are just for
668 * error reporting.
669 *
670 * targetTypeId, targetTypMod, targetCollation indicate the datatype and
671 * collation of the object to be assigned to (initially the target column,
672 * later some subobject).
673 *
674 * indirection is the list of indirection nodes, and indirection_cell is the
675 * start of the sublist remaining to process. When it's NULL, we're done
676 * recursing and can just coerce and return the RHS.
677 *
678 * rhs is the already-transformed value to be assigned; note it has not been
679 * coerced to any particular type.
680 *
681 * ccontext is the coercion level to use while coercing the rhs. For
682 * normal statements it'll be COERCION_ASSIGNMENT, but PL/pgSQL uses
683 * a special value.
684 *
685 * location is the cursor error position for any errors. (Note: this points
686 * to the head of the target clause, eg "foo" in "foo.bar[baz]". Later we
687 * might want to decorate indirection cells with their own location info,
688 * in which case the location argument could probably be dropped.)
689 */
690Node *
692 Node *basenode,
693 const char *targetName,
698 List *indirection,
700 Node *rhs,
702 int location)
703{
704 Node *result;
706 ListCell *i;
707
709 {
710 /*
711 * Set up a substitution. We abuse CaseTestExpr for this. It's safe
712 * to do so because the only nodes that will be above the CaseTestExpr
713 * in the finished expression will be FieldStore and SubscriptingRef
714 * nodes. (There could be other stuff in the tree, but it will be
715 * within other child fields of those node types.)
716 */
718
719 ctest->typeId = targetTypeId;
720 ctest->typeMod = targetTypMod;
721 ctest->collation = targetCollation;
722 basenode = (Node *) ctest;
723 }
724
725 /*
726 * We have to split any field-selection operations apart from
727 * subscripting. Adjacent A_Indices nodes have to be treated as a single
728 * multidimensional subscript operation.
729 */
730 for_each_cell(i, indirection, indirection_cell)
731 {
732 Node *n = lfirst(i);
733
734 if (IsA(n, A_Indices))
736 else if (IsA(n, A_Star))
737 {
740 errmsg("row expansion via \"*\" is not supported here"),
741 parser_errposition(pstate, location)));
742 }
743 else
744 {
745 FieldStore *fstore;
748 Oid typrelid;
753
754 Assert(IsA(n, String));
755
756 /* process subscripts before this field selection */
757 if (subscripts)
758 {
759 /* recurse, and then return because we're done */
760 return transformAssignmentSubscripts(pstate,
761 basenode,
767 indirection,
768 i,
769 rhs,
770 ccontext,
771 location);
772 }
773
774 /* No subscripts, so can process field selection here */
775
776 /*
777 * Look up the composite type, accounting for possibility that
778 * what we are given is a domain over composite.
779 */
782
783 typrelid = typeidTypeRelid(baseTypeId);
784 if (!typrelid)
787 errmsg("cannot assign to field \"%s\" of column \"%s\" because its type %s is not a composite type",
788 strVal(n), targetName,
790 parser_errposition(pstate, location)));
791
792 attnum = get_attnum(typrelid, strVal(n));
796 errmsg("cannot assign to field \"%s\" of column \"%s\" because there is no such column in data type %s",
797 strVal(n), targetName,
799 parser_errposition(pstate, location)));
800 if (attnum < 0)
803 errmsg("cannot assign to system column \"%s\"",
804 strVal(n)),
805 parser_errposition(pstate, location)));
806
809
810 /* recurse to create appropriate RHS for field assign */
812 NULL,
813 strVal(n),
814 false,
818 indirection,
819 lnext(indirection, i),
820 rhs,
821 ccontext,
822 location);
823
824 /* and build a FieldStore node */
825 fstore = makeNode(FieldStore);
826 fstore->arg = (Expr *) basenode;
827 fstore->newvals = list_make1(rhs);
828 fstore->fieldnums = list_make1_int(attnum);
829 fstore->resulttype = baseTypeId;
830
831 /*
832 * If target is a domain, apply constraints. Notice that this
833 * isn't totally right: the expression tree we build would check
834 * the domain's constraints on a composite value with only this
835 * one field populated or updated, possibly leading to an unwanted
836 * failure. The rewriter will merge together any subfield
837 * assignments to the same table column, resulting in the domain's
838 * constraints being checked only once after we've assigned to all
839 * the fields that the INSERT or UPDATE means to.
840 */
842 return coerce_to_domain((Node *) fstore,
847 location,
848 false);
849
850 return (Node *) fstore;
851 }
852 }
853
854 /* process trailing subscripts, if any */
855 if (subscripts)
856 {
857 /* recurse, and then return because we're done */
858 return transformAssignmentSubscripts(pstate,
859 basenode,
865 indirection,
866 NULL,
867 rhs,
868 ccontext,
869 location);
870 }
871
872 /* base case: just coerce RHS to match target type ID */
873
874 result = coerce_to_target_type(pstate,
875 rhs, exprType(rhs),
877 ccontext,
879 -1);
880 if (result == NULL)
881 {
885 errmsg("subscripted assignment to \"%s\" requires type %s"
886 " but expression is of type %s",
890 errhint("You will need to rewrite or cast the expression."),
891 parser_errposition(pstate, location)));
892 else
895 errmsg("subfield \"%s\" is of type %s"
896 " but expression is of type %s",
900 errhint("You will need to rewrite or cast the expression."),
901 parser_errposition(pstate, location)));
902 }
903
904 return result;
905}
906
907/*
908 * helper for transformAssignmentIndirection: process container assignment
909 */
910static Node *
912 Node *basenode,
913 const char *targetName,
918 List *indirection,
920 Node *rhs,
922 int location)
923{
924 Node *result;
925 SubscriptingRef *sbsref;
931
933
934 /* Identify the actual container type involved */
938
939 /* Process subscripts and identify required type for RHS */
940 sbsref = transformContainerSubscripts(pstate,
941 basenode,
945 true);
946
947 typeNeeded = sbsref->refrestype;
948 typmodNeeded = sbsref->reftypmod;
949
950 /*
951 * Container normally has same collation as its elements, but there's an
952 * exception: we might be subscripting a domain over a container type. In
953 * that case use collation of the base type. (This is shaky for arbitrary
954 * subscripting semantics, but it doesn't matter all that much since we
955 * only use this to label the collation of a possible CaseTestExpr.)
956 */
959 else
961
962 /* recurse to create appropriate RHS for container assign */
964 NULL,
966 true,
970 indirection,
972 rhs,
973 ccontext,
974 location);
975
976 /*
977 * Insert the already-properly-coerced RHS into the SubscriptingRef. Then
978 * set refrestype and reftypmod back to the container type's values.
979 */
980 sbsref->refassgnexpr = (Expr *) rhs;
981 sbsref->refrestype = containerType;
982 sbsref->reftypmod = containerTypMod;
983
984 result = (Node *) sbsref;
985
986 /*
987 * If target was a domain over container, need to coerce up to the domain.
988 * As in transformAssignmentIndirection, this coercion is premature if the
989 * query assigns to multiple elements of the container; but we'll fix that
990 * during query rewrite.
991 */
993 {
994 Oid resulttype = exprType(result);
995
996 result = coerce_to_target_type(pstate,
997 result, resulttype,
999 ccontext,
1001 -1);
1002 /* can fail if we had int2vector/oidvector, but not for true domains */
1003 if (result == NULL)
1004 ereport(ERROR,
1006 errmsg("cannot cast type %s to %s",
1007 format_type_be(resulttype),
1009 parser_errposition(pstate, location)));
1010 }
1011
1012 return result;
1013}
1014
1015
1016/*
1017 * checkInsertTargets -
1018 * generate a list of INSERT column targets if not supplied, or
1019 * test supplied column names to make sure they are in target table.
1020 * Also return an integer list of the columns' attribute numbers.
1021 */
1022List *
1024{
1025 *attrnos = NIL;
1026
1027 if (cols == NIL)
1028 {
1029 /*
1030 * Generate default column list for INSERT.
1031 */
1033
1034 int i;
1035
1036 for (i = 0; i < numcol; i++)
1037 {
1038 ResTarget *col;
1039 Form_pg_attribute attr;
1040
1041 attr = TupleDescAttr(pstate->p_target_relation->rd_att, i);
1042
1043 if (attr->attisdropped)
1044 continue;
1045
1047 col->name = pstrdup(NameStr(attr->attname));
1048 col->indirection = NIL;
1049 col->val = NULL;
1050 col->location = -1;
1051 cols = lappend(cols, col);
1052 *attrnos = lappend_int(*attrnos, i + 1);
1053 }
1054 }
1055 else
1056 {
1057 /*
1058 * Do initial validation of user-supplied INSERT column list.
1059 */
1062 ListCell *tl;
1063
1064 foreach(tl, cols)
1065 {
1067 char *name = col->name;
1068 int attrno;
1069
1070 /* Lookup column name, ereport on failure */
1071 attrno = attnameAttNum(pstate->p_target_relation, name, false);
1073 ereport(ERROR,
1075 errmsg("column \"%s\" of relation \"%s\" does not exist",
1076 name,
1078 parser_errposition(pstate, col->location)));
1079
1080 /*
1081 * Check for duplicates, but only of whole columns --- we allow
1082 * INSERT INTO foo (col.subcol1, col.subcol2)
1083 */
1084 if (col->indirection == NIL)
1085 {
1086 /* whole column; must not have any other assignment */
1089 ereport(ERROR,
1091 errmsg("column \"%s\" specified more than once",
1092 name),
1093 parser_errposition(pstate, col->location)));
1095 }
1096 else
1097 {
1098 /* partial column; must not have any whole assignment */
1100 ereport(ERROR,
1102 errmsg("column \"%s\" specified more than once",
1103 name),
1104 parser_errposition(pstate, col->location)));
1106 }
1107
1109 }
1110 }
1111
1112 return cols;
1113}
1114
1115/*
1116 * ExpandColumnRefStar()
1117 * Transforms foo.* into a list of expressions or targetlist entries.
1118 *
1119 * This handles the case where '*' appears as the last or only item in a
1120 * ColumnRef. The code is shared between the case of foo.* at the top level
1121 * in a SELECT target list (where we want TargetEntry nodes in the result)
1122 * and foo.* in a ROW() or VALUES() construct (where we want just bare
1123 * expressions).
1124 *
1125 * The referenced columns are marked as requiring SELECT access.
1126 */
1127static List *
1129 bool make_target_entry)
1130{
1131 List *fields = cref->fields;
1132 int numnames = list_length(fields);
1133
1134 if (numnames == 1)
1135 {
1136 /*
1137 * Target item is a bare '*', expand all tables
1138 *
1139 * (e.g., SELECT * FROM emp, dept)
1140 *
1141 * Since the grammar only accepts bare '*' at top level of SELECT, we
1142 * need not handle the make_target_entry==false case here.
1143 */
1145 return ExpandAllTables(pstate, cref->location);
1146 }
1147 else
1148 {
1149 /*
1150 * Target item is relation.*, expand that table
1151 *
1152 * (e.g., SELECT emp.*, dname FROM emp, dept)
1153 *
1154 * Note: this code is a lot like transformColumnRef; it's tempting to
1155 * call that instead and then replace the resulting whole-row Var with
1156 * a list of Vars. However, that would leave us with the relation's
1157 * selectedCols bitmap showing the whole row as needing select
1158 * permission, as well as the individual columns. That would be
1159 * incorrect (since columns added later shouldn't need select
1160 * permissions). We could try to remove the whole-row permission bit
1161 * after the fact, but duplicating code is less messy.
1162 */
1163 char *nspname = NULL;
1164 char *relname = NULL;
1166 int levels_up;
1167 enum
1168 {
1173
1174 /*
1175 * Give the PreParseColumnRefHook, if any, first shot. If it returns
1176 * non-null then we should use that expression.
1177 */
1178 if (pstate->p_pre_columnref_hook != NULL)
1179 {
1180 Node *node;
1181
1182 node = pstate->p_pre_columnref_hook(pstate, cref);
1183 if (node != NULL)
1184 return ExpandRowReference(pstate, node, make_target_entry);
1185 }
1186
1187 switch (numnames)
1188 {
1189 case 2:
1190 relname = strVal(linitial(fields));
1191 nsitem = refnameNamespaceItem(pstate, nspname, relname,
1192 cref->location,
1193 &levels_up);
1194 break;
1195 case 3:
1196 nspname = strVal(linitial(fields));
1197 relname = strVal(lsecond(fields));
1198 nsitem = refnameNamespaceItem(pstate, nspname, relname,
1199 cref->location,
1200 &levels_up);
1201 break;
1202 case 4:
1203 {
1204 char *catname = strVal(linitial(fields));
1205
1206 /*
1207 * We check the catalog name and then ignore it.
1208 */
1209 if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
1210 {
1212 break;
1213 }
1214 nspname = strVal(lsecond(fields));
1215 relname = strVal(lthird(fields));
1216 nsitem = refnameNamespaceItem(pstate, nspname, relname,
1217 cref->location,
1218 &levels_up);
1219 break;
1220 }
1221 default:
1223 break;
1224 }
1225
1226 /*
1227 * Now give the PostParseColumnRefHook, if any, a chance. We cheat a
1228 * bit by passing the RangeTblEntry, not a Var, as the planned
1229 * translation. (A single Var wouldn't be strictly correct anyway.
1230 * This convention allows hooks that really care to know what is
1231 * happening. It might be better to pass the nsitem, but we'd have to
1232 * promote that struct to a full-fledged Node type so that callees
1233 * could identify its type.)
1234 */
1235 if (pstate->p_post_columnref_hook != NULL)
1236 {
1237 Node *node;
1238
1239 node = pstate->p_post_columnref_hook(pstate, cref,
1240 (Node *) (nsitem ? nsitem->p_rte : NULL));
1241 if (node != NULL)
1242 {
1243 if (nsitem != NULL)
1244 ereport(ERROR,
1246 errmsg("column reference \"%s\" is ambiguous",
1247 NameListToString(cref->fields)),
1248 parser_errposition(pstate, cref->location)));
1249 return ExpandRowReference(pstate, node, make_target_entry);
1250 }
1251 }
1252
1253 /*
1254 * Throw error if no translation found.
1255 */
1256 if (nsitem == NULL)
1257 {
1258 switch (crserr)
1259 {
1260 case CRSERR_NO_RTE:
1261 errorMissingRTE(pstate, makeRangeVar(nspname, relname,
1262 cref->location));
1263 break;
1264 case CRSERR_WRONG_DB:
1265 ereport(ERROR,
1267 errmsg("cross-database references are not implemented: %s",
1268 NameListToString(cref->fields)),
1269 parser_errposition(pstate, cref->location)));
1270 break;
1271 case CRSERR_TOO_MANY:
1272 ereport(ERROR,
1274 errmsg("improper qualified name (too many dotted names): %s",
1275 NameListToString(cref->fields)),
1276 parser_errposition(pstate, cref->location)));
1277 break;
1278 }
1279 }
1280
1281 /*
1282 * OK, expand the nsitem into fields.
1283 */
1284 return ExpandSingleTable(pstate, nsitem, levels_up, cref->location,
1286 }
1287}
1288
1289/*
1290 * ExpandAllTables()
1291 * Transforms '*' (in the target list) into a list of targetlist entries.
1292 *
1293 * tlist entries are generated for each relation visible for unqualified
1294 * column name access. We do not consider qualified-name-only entries because
1295 * that would include input tables of aliasless JOINs, NEW/OLD pseudo-entries,
1296 * etc.
1297 *
1298 * The referenced relations/columns are marked as requiring SELECT access.
1299 */
1300static List *
1301ExpandAllTables(ParseState *pstate, int location)
1302{
1303 List *target = NIL;
1304 bool found_table = false;
1305 ListCell *l;
1306
1307 foreach(l, pstate->p_namespace)
1308 {
1310
1311 /* Ignore table-only items */
1312 if (!nsitem->p_cols_visible)
1313 continue;
1314 /* Should not have any lateral-only items when parsing targetlist */
1315 Assert(!nsitem->p_lateral_only);
1316 /* Remember we found a p_cols_visible item */
1317 found_table = true;
1318
1319 target = list_concat(target,
1320 expandNSItemAttrs(pstate,
1321 nsitem,
1322 0,
1323 true,
1324 location));
1325 }
1326
1327 /*
1328 * Check for "SELECT *;". We do it this way, rather than checking for
1329 * target == NIL, because we want to allow SELECT * FROM a zero_column
1330 * table.
1331 */
1332 if (!found_table)
1333 ereport(ERROR,
1335 errmsg("SELECT * with no tables specified is not valid"),
1336 parser_errposition(pstate, location)));
1337
1338 return target;
1339}
1340
1341/*
1342 * ExpandIndirectionStar()
1343 * Transforms foo.* into a list of expressions or targetlist entries.
1344 *
1345 * This handles the case where '*' appears as the last item in A_Indirection.
1346 * The code is shared between the case of foo.* at the top level in a SELECT
1347 * target list (where we want TargetEntry nodes in the result) and foo.* in
1348 * a ROW() or VALUES() construct (where we want just bare expressions).
1349 * For robustness, we use a separate "make_target_entry" flag to control
1350 * this rather than relying on exprKind.
1351 */
1352static List *
1355{
1356 Node *expr;
1357
1358 /* Strip off the '*' to create a reference to the rowtype object */
1359 ind = copyObject(ind);
1360 ind->indirection = list_truncate(ind->indirection,
1361 list_length(ind->indirection) - 1);
1362
1363 /* And transform that */
1364 expr = transformExpr(pstate, (Node *) ind, exprKind);
1365
1366 /* Expand the rowtype expression into individual fields */
1367 return ExpandRowReference(pstate, expr, make_target_entry);
1368}
1369
1370/*
1371 * ExpandSingleTable()
1372 * Transforms foo.* into a list of expressions or targetlist entries.
1373 *
1374 * This handles the case where foo has been determined to be a simple
1375 * reference to an RTE, so we can just generate Vars for the expressions.
1376 *
1377 * The referenced columns are marked as requiring SELECT access.
1378 */
1379static List *
1381 int sublevels_up, int location, bool make_target_entry)
1382{
1384 {
1385 /* expandNSItemAttrs handles permissions marking */
1386 return expandNSItemAttrs(pstate, nsitem, sublevels_up, true, location);
1387 }
1388 else
1389 {
1390 RangeTblEntry *rte = nsitem->p_rte;
1391 RTEPermissionInfo *perminfo = nsitem->p_perminfo;
1392 List *vars;
1393 ListCell *l;
1394
1395 vars = expandNSItemVars(pstate, nsitem, sublevels_up, location, NULL);
1396
1397 /*
1398 * Require read access to the table. This is normally redundant with
1399 * the markVarForSelectPriv calls below, but not if the table has zero
1400 * columns. We need not do anything if the nsitem is for a join: its
1401 * component tables will have been marked ACL_SELECT when they were
1402 * added to the rangetable. (This step changes things only for the
1403 * target relation of UPDATE/DELETE, which cannot be under a join.)
1404 */
1405 if (rte->rtekind == RTE_RELATION)
1406 {
1407 Assert(perminfo != NULL);
1408 perminfo->requiredPerms |= ACL_SELECT;
1409 }
1410
1411 /* Require read access to each column */
1412 foreach(l, vars)
1413 {
1414 Var *var = (Var *) lfirst(l);
1415
1416 markVarForSelectPriv(pstate, var);
1417 }
1418
1419 return vars;
1420 }
1421}
1422
1423/*
1424 * ExpandRowReference()
1425 * Transforms foo.* into a list of expressions or targetlist entries.
1426 *
1427 * This handles the case where foo is an arbitrary expression of composite
1428 * type.
1429 */
1430static List *
1432 bool make_target_entry)
1433{
1434 List *result = NIL;
1436 int numAttrs;
1437 int i;
1438
1439 /*
1440 * If the rowtype expression is a whole-row Var, we can expand the fields
1441 * as simple Vars. Note: if the RTE is a relation, this case leaves us
1442 * with its RTEPermissionInfo's selectedCols bitmap showing the whole row
1443 * as needing select permission, as well as the individual columns.
1444 * However, we can only get here for weird notations like (table.*).*, so
1445 * it's not worth trying to clean up --- arguably, the permissions marking
1446 * is correct anyway for such cases.
1447 */
1448 if (IsA(expr, Var) &&
1449 ((Var *) expr)->varattno == InvalidAttrNumber)
1450 {
1451 Var *var = (Var *) expr;
1453
1454 nsitem = GetNSItemByRangeTablePosn(pstate, var->varno, var->varlevelsup);
1455 return ExpandSingleTable(pstate, nsitem, var->varlevelsup, var->location, make_target_entry);
1456 }
1457
1458 /*
1459 * Otherwise we have to do it the hard way. Our current implementation is
1460 * to generate multiple copies of the expression and do FieldSelects.
1461 * (This can be pretty inefficient if the expression involves nontrivial
1462 * computation :-(.)
1463 *
1464 * Verify it's a composite type, and get the tupdesc.
1465 * get_expr_result_tupdesc() handles this conveniently.
1466 *
1467 * If it's a Var of type RECORD, we have to work even harder: we have to
1468 * find what the Var refers to, and pass that to get_expr_result_tupdesc.
1469 * That task is handled by expandRecordVariable().
1470 */
1471 if (IsA(expr, Var) &&
1472 ((Var *) expr)->vartype == RECORDOID)
1473 tupleDesc = expandRecordVariable(pstate, (Var *) expr, 0);
1474 else
1475 tupleDesc = get_expr_result_tupdesc(expr, false);
1477
1478 /* Generate a list of references to the individual fields */
1479 numAttrs = tupleDesc->natts;
1480 for (i = 0; i < numAttrs; i++)
1481 {
1484
1485 if (att->attisdropped)
1486 continue;
1487
1489 fselect->arg = (Expr *) copyObject(expr);
1490 fselect->fieldnum = i + 1;
1491 fselect->resulttype = att->atttypid;
1492 fselect->resulttypmod = att->atttypmod;
1493 /* save attribute's collation for parse_collate.c */
1494 fselect->resultcollid = att->attcollation;
1495
1497 {
1498 /* add TargetEntry decoration */
1499 TargetEntry *te;
1500
1501 te = makeTargetEntry((Expr *) fselect,
1502 (AttrNumber) pstate->p_next_resno++,
1503 pstrdup(NameStr(att->attname)),
1504 false);
1505 result = lappend(result, te);
1506 }
1507 else
1508 result = lappend(result, fselect);
1509 }
1510
1511 return result;
1512}
1513
1514/*
1515 * expandRecordVariable
1516 * Get the tuple descriptor for a Var of type RECORD, if possible.
1517 *
1518 * Since no actual table or view column is allowed to have type RECORD, such
1519 * a Var must refer to a JOIN or FUNCTION RTE or to a subquery output. We
1520 * drill down to find the ultimate defining expression and attempt to infer
1521 * the tupdesc from it. We ereport if we can't determine the tupdesc.
1522 *
1523 * levelsup is an extra offset to interpret the Var's varlevelsup correctly
1524 * when recursing. Outside callers should pass zero.
1525 */
1527expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
1528{
1530 int netlevelsup;
1533 Node *expr;
1534
1535 /* Check my caller didn't mess up */
1536 Assert(IsA(var, Var));
1537 Assert(var->vartype == RECORDOID);
1538
1539 /*
1540 * Note: it's tempting to use GetNSItemByRangeTablePosn here so that we
1541 * can use expandNSItemVars instead of expandRTE; but that does not work
1542 * for some of the recursion cases below, where we have consed up a
1543 * ParseState that lacks p_namespace data.
1544 */
1545 netlevelsup = var->varlevelsup + levelsup;
1547 attnum = var->varattno;
1548
1550 {
1551 /* Whole-row reference to an RTE, so expand the known fields */
1552 List *names,
1553 *vars;
1554 ListCell *lname,
1555 *lvar;
1556 int i;
1557
1558 expandRTE(rte, var->varno, 0, var->varreturningtype,
1559 var->location, false, &names, &vars);
1560
1562 i = 1;
1563 forboth(lname, names, lvar, vars)
1564 {
1565 char *label = strVal(lfirst(lname));
1566 Node *varnode = (Node *) lfirst(lvar);
1567
1569 label,
1572 0);
1575 i++;
1576 }
1577 Assert(lname == NULL && lvar == NULL); /* lists same length? */
1578
1580
1581 return tupleDesc;
1582 }
1583
1584 expr = (Node *) var; /* default if we can't drill down */
1585
1586 switch (rte->rtekind)
1587 {
1588 case RTE_RELATION:
1589 case RTE_VALUES:
1591 case RTE_GRAPH_TABLE:
1592 case RTE_RESULT:
1593
1594 /*
1595 * This case should not occur: a column of a table, values list,
1596 * or ENR shouldn't have type RECORD. Fall through and fail (most
1597 * likely) at the bottom.
1598 */
1599 break;
1600 case RTE_SUBQUERY:
1601 {
1602 /* Subselect-in-FROM: examine sub-select's output expr */
1603 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
1604 attnum);
1605
1606 if (ste == NULL || ste->resjunk)
1607 elog(ERROR, "subquery %s does not have attribute %d",
1608 rte->eref->aliasname, attnum);
1609 expr = (Node *) ste->expr;
1610 if (IsA(expr, Var))
1611 {
1612 /*
1613 * Recurse into the sub-select to see what its Var refers
1614 * to. We have to build an additional level of ParseState
1615 * to keep in step with varlevelsup in the subselect;
1616 * furthermore, the subquery RTE might be from an outer
1617 * query level, in which case the ParseState for the
1618 * subselect must have that outer level as parent.
1619 */
1620 ParseState mypstate = {0};
1621
1622 /* this loop must work, since GetRTEByRangeTablePosn did */
1623 for (Index level = 0; level < netlevelsup; level++)
1624 pstate = pstate->parentParseState;
1625 mypstate.parentParseState = pstate;
1626 mypstate.p_rtable = rte->subquery->rtable;
1627 /* don't bother filling the rest of the fake pstate */
1628
1629 return expandRecordVariable(&mypstate, (Var *) expr, 0);
1630 }
1631 /* else fall through to inspect the expression */
1632 }
1633 break;
1634 case RTE_JOIN:
1635 /* Join RTE --- recursively inspect the alias variable */
1636 Assert(attnum > 0 && attnum <= list_length(rte->joinaliasvars));
1637 expr = (Node *) list_nth(rte->joinaliasvars, attnum - 1);
1638 Assert(expr != NULL);
1639 /* We intentionally don't strip implicit coercions here */
1640 if (IsA(expr, Var))
1641 return expandRecordVariable(pstate, (Var *) expr, netlevelsup);
1642 /* else fall through to inspect the expression */
1643 break;
1644 case RTE_FUNCTION:
1645
1646 /*
1647 * We couldn't get here unless a function is declared with one of
1648 * its result columns as RECORD, which is not allowed.
1649 */
1650 break;
1651 case RTE_TABLEFUNC:
1652
1653 /*
1654 * Table function cannot have columns with RECORD type.
1655 */
1656 break;
1657 case RTE_CTE:
1658 /* CTE reference: examine subquery's output expr */
1659 if (!rte->self_reference)
1660 {
1663
1665 if (ste == NULL || ste->resjunk)
1666 elog(ERROR, "CTE %s does not have attribute %d",
1667 rte->eref->aliasname, attnum);
1668 expr = (Node *) ste->expr;
1669 if (IsA(expr, Var))
1670 {
1671 /*
1672 * Recurse into the CTE to see what its Var refers to. We
1673 * have to build an additional level of ParseState to keep
1674 * in step with varlevelsup in the CTE; furthermore it
1675 * could be an outer CTE (compare SUBQUERY case above).
1676 */
1677 ParseState mypstate = {0};
1678
1679 /* this loop must work, since GetCTEForRTE did */
1680 for (Index level = 0;
1681 level < rte->ctelevelsup + netlevelsup;
1682 level++)
1683 pstate = pstate->parentParseState;
1684 mypstate.parentParseState = pstate;
1685 mypstate.p_rtable = ((Query *) cte->ctequery)->rtable;
1686 /* don't bother filling the rest of the fake pstate */
1687
1688 return expandRecordVariable(&mypstate, (Var *) expr, 0);
1689 }
1690 /* else fall through to inspect the expression */
1691 }
1692 break;
1693 case RTE_GROUP:
1694
1695 /*
1696 * We couldn't get here: the RTE_GROUP RTE has not been added.
1697 */
1698 break;
1699 }
1700
1701 /*
1702 * We now have an expression we can't expand any more, so see if
1703 * get_expr_result_tupdesc() can do anything with it.
1704 */
1705 return get_expr_result_tupdesc(expr, false);
1706}
1707
1708
1709/*
1710 * FigureColname -
1711 * if the name of the resulting column is not specified in the target
1712 * list, we have to guess a suitable name. The SQL spec provides some
1713 * guidance, but not much...
1714 *
1715 * Note that the argument is the *untransformed* parse tree for the target
1716 * item. This is a shade easier to work with than the transformed tree.
1717 */
1718char *
1720{
1721 char *name = NULL;
1722
1724 if (name != NULL)
1725 return name;
1726 /* default result if we can't guess anything */
1727 return "?column?";
1728}
1729
1730/*
1731 * FigureIndexColname -
1732 * choose the name for an expression column in an index
1733 *
1734 * This is actually just like FigureColname, except we return NULL if
1735 * we can't pick a good name.
1736 */
1737char *
1739{
1740 char *name = NULL;
1741
1743 return name;
1744}
1745
1746/*
1747 * FigureColnameInternal -
1748 * internal workhorse for FigureColname
1749 *
1750 * Return value indicates strength of confidence in result:
1751 * 0 - no information
1752 * 1 - second-best name choice
1753 * 2 - good name choice
1754 * The return value is actually only used internally.
1755 * If the result isn't zero, *name is set to the chosen name.
1756 */
1757static int
1759{
1760 int strength = 0;
1761
1762 if (node == NULL)
1763 return strength;
1764
1765 switch (nodeTag(node))
1766 {
1767 case T_ColumnRef:
1768 {
1769 char *fname = NULL;
1770 ListCell *l;
1771
1772 /* find last field name, if any, ignoring "*" */
1773 foreach(l, ((ColumnRef *) node)->fields)
1774 {
1775 Node *i = lfirst(l);
1776
1777 if (IsA(i, String))
1778 fname = strVal(i);
1779 }
1780 if (fname)
1781 {
1782 *name = fname;
1783 return 2;
1784 }
1785 }
1786 break;
1787 case T_A_Indirection:
1788 {
1789 A_Indirection *ind = (A_Indirection *) node;
1790 char *fname = NULL;
1791 ListCell *l;
1792
1793 /* find last field name, if any, ignoring "*" and subscripts */
1794 foreach(l, ind->indirection)
1795 {
1796 Node *i = lfirst(l);
1797
1798 if (IsA(i, String))
1799 fname = strVal(i);
1800 }
1801 if (fname)
1802 {
1803 *name = fname;
1804 return 2;
1805 }
1806 return FigureColnameInternal(ind->arg, name);
1807 }
1808 break;
1809 case T_FuncCall:
1810 *name = strVal(llast(((FuncCall *) node)->funcname));
1811 return 2;
1812 case T_A_Expr:
1813 if (((A_Expr *) node)->kind == AEXPR_NULLIF)
1814 {
1815 /* make nullif() act like a regular function */
1816 *name = "nullif";
1817 return 2;
1818 }
1819 break;
1820 case T_TypeCast:
1821 strength = FigureColnameInternal(((TypeCast *) node)->arg,
1822 name);
1823 if (strength <= 1)
1824 {
1825 if (((TypeCast *) node)->typeName != NULL)
1826 {
1827 *name = strVal(llast(((TypeCast *) node)->typeName->names));
1828 return 1;
1829 }
1830 }
1831 break;
1832 case T_CollateClause:
1833 return FigureColnameInternal(((CollateClause *) node)->arg, name);
1834 case T_GroupingFunc:
1835 /* make GROUPING() act like a regular function */
1836 *name = "grouping";
1837 return 2;
1838 case T_MergeSupportFunc:
1839 /* make MERGE_ACTION() act like a regular function */
1840 *name = "merge_action";
1841 return 2;
1842 case T_SubLink:
1843 switch (((SubLink *) node)->subLinkType)
1844 {
1845 case EXISTS_SUBLINK:
1846 *name = "exists";
1847 return 2;
1848 case ARRAY_SUBLINK:
1849 *name = "array";
1850 return 2;
1851 case EXPR_SUBLINK:
1852 {
1853 /* Get column name of the subquery's single target */
1854 SubLink *sublink = (SubLink *) node;
1855 Query *query = (Query *) sublink->subselect;
1856
1857 /*
1858 * The subquery has probably already been transformed,
1859 * but let's be careful and check that. (The reason
1860 * we can see a transformed subquery here is that
1861 * transformSubLink is lazy and modifies the SubLink
1862 * node in-place.)
1863 */
1864 if (IsA(query, Query))
1865 {
1866 TargetEntry *te = (TargetEntry *) linitial(query->targetList);
1867
1868 if (te->resname)
1869 {
1870 *name = te->resname;
1871 return 2;
1872 }
1873 }
1874 }
1875 break;
1876 /* As with other operator-like nodes, these have no names */
1877 case MULTIEXPR_SUBLINK:
1878 case ALL_SUBLINK:
1879 case ANY_SUBLINK:
1880 case ROWCOMPARE_SUBLINK:
1881 case CTE_SUBLINK:
1882 break;
1883 }
1884 break;
1885 case T_CaseExpr:
1886 strength = FigureColnameInternal((Node *) ((CaseExpr *) node)->defresult,
1887 name);
1888 if (strength <= 1)
1889 {
1890 *name = "case";
1891 return 1;
1892 }
1893 break;
1894 case T_A_ArrayExpr:
1895 /* make ARRAY[] act like a function */
1896 *name = "array";
1897 return 2;
1898 case T_RowExpr:
1899 /* make ROW() act like a function */
1900 *name = "row";
1901 return 2;
1902 case T_CoalesceExpr:
1903 /* make coalesce() act like a regular function */
1904 *name = "coalesce";
1905 return 2;
1906 case T_MinMaxExpr:
1907 /* make greatest/least act like a regular function */
1908 switch (((MinMaxExpr *) node)->op)
1909 {
1910 case IS_GREATEST:
1911 *name = "greatest";
1912 return 2;
1913 case IS_LEAST:
1914 *name = "least";
1915 return 2;
1916 }
1917 break;
1918 case T_SQLValueFunction:
1919 /* make these act like a function or variable */
1920 switch (((SQLValueFunction *) node)->op)
1921 {
1922 case SVFOP_CURRENT_DATE:
1923 *name = "current_date";
1924 return 2;
1925 case SVFOP_CURRENT_TIME:
1927 *name = "current_time";
1928 return 2;
1931 *name = "current_timestamp";
1932 return 2;
1933 case SVFOP_LOCALTIME:
1934 case SVFOP_LOCALTIME_N:
1935 *name = "localtime";
1936 return 2;
1939 *name = "localtimestamp";
1940 return 2;
1941 case SVFOP_CURRENT_ROLE:
1942 *name = "current_role";
1943 return 2;
1944 case SVFOP_CURRENT_USER:
1945 *name = "current_user";
1946 return 2;
1947 case SVFOP_USER:
1948 *name = "user";
1949 return 2;
1950 case SVFOP_SESSION_USER:
1951 *name = "session_user";
1952 return 2;
1954 *name = "current_catalog";
1955 return 2;
1957 *name = "current_schema";
1958 return 2;
1959 }
1960 break;
1961 case T_XmlExpr:
1962 /* make SQL/XML functions act like a regular function */
1963 switch (((XmlExpr *) node)->op)
1964 {
1965 case IS_XMLCONCAT:
1966 *name = "xmlconcat";
1967 return 2;
1968 case IS_XMLELEMENT:
1969 *name = "xmlelement";
1970 return 2;
1971 case IS_XMLFOREST:
1972 *name = "xmlforest";
1973 return 2;
1974 case IS_XMLPARSE:
1975 *name = "xmlparse";
1976 return 2;
1977 case IS_XMLPI:
1978 *name = "xmlpi";
1979 return 2;
1980 case IS_XMLROOT:
1981 *name = "xmlroot";
1982 return 2;
1983 case IS_XMLSERIALIZE:
1984 *name = "xmlserialize";
1985 return 2;
1986 case IS_DOCUMENT:
1987 /* nothing */
1988 break;
1989 }
1990 break;
1991 case T_XmlSerialize:
1992 /* make XMLSERIALIZE act like a regular function */
1993 *name = "xmlserialize";
1994 return 2;
1995 case T_JsonParseExpr:
1996 /* make JSON act like a regular function */
1997 *name = "json";
1998 return 2;
1999 case T_JsonScalarExpr:
2000 /* make JSON_SCALAR act like a regular function */
2001 *name = "json_scalar";
2002 return 2;
2004 /* make JSON_SERIALIZE act like a regular function */
2005 *name = "json_serialize";
2006 return 2;
2008 /* make JSON_OBJECT act like a regular function */
2009 *name = "json_object";
2010 return 2;
2013 /* make JSON_ARRAY act like a regular function */
2014 *name = "json_array";
2015 return 2;
2016 case T_JsonObjectAgg:
2017 /* make JSON_OBJECTAGG act like a regular function */
2018 *name = "json_objectagg";
2019 return 2;
2020 case T_JsonArrayAgg:
2021 /* make JSON_ARRAYAGG act like a regular function */
2022 *name = "json_arrayagg";
2023 return 2;
2024 case T_JsonFuncExpr:
2025 /* make SQL/JSON functions act like a regular function */
2026 switch (((JsonFuncExpr *) node)->op)
2027 {
2028 case JSON_EXISTS_OP:
2029 *name = "json_exists";
2030 return 2;
2031 case JSON_QUERY_OP:
2032 *name = "json_query";
2033 return 2;
2034 case JSON_VALUE_OP:
2035 *name = "json_value";
2036 return 2;
2037 /* JSON_TABLE_OP can't happen here. */
2038 default:
2039 elog(ERROR, "unrecognized JsonExpr op: %d",
2040 (int) ((JsonFuncExpr *) node)->op);
2041 }
2042 break;
2043 default:
2044 break;
2045 }
2046
2047 return strength;
2048}
int16 AttrNumber
Definition attnum.h:21
#define InvalidAttrNumber
Definition attnum.h:23
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
#define NameStr(name)
Definition c.h:837
#define Assert(condition)
Definition c.h:945
int32_t int32
Definition c.h:614
unsigned int Index
Definition c.h:700
Datum arg
Definition elog.c:1322
int errcode(int sqlerrcode)
Definition elog.c:874
int errhint(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
char * format_type_be(Oid type_oid)
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition funcapi.c:553
Oid MyDatabaseId
Definition globals.c:94
#define funcname
int i
Definition isn.c:77
List * lappend(List *list, void *datum)
Definition list.c:339
List * list_concat(List *list1, const List *list2)
Definition list.c:561
List * lappend_int(List *list, int datum)
Definition list.c:357
List * list_truncate(List *list, int new_size)
Definition list.c:631
AttrNumber get_attnum(Oid relid, const char *attname)
Definition lsyscache.c:977
char * get_database_name(Oid dbid)
Definition lsyscache.c:1312
Oid get_typcollation(Oid typid)
Definition lsyscache.c:3278
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition lsyscache.c:2760
void get_atttypetypmodcoll(Oid relid, AttrNumber attnum, Oid *typid, int32 *typmod, Oid *collid)
Definition lsyscache.c:1062
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
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition makefuncs.c:473
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition makefuncs.c:289
char * pstrdup(const char *in)
Definition mcxt.c:1781
char * NameListToString(const List *names)
Definition namespace.c:3666
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition nodeFuncs.c:304
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:826
int exprLocation(const Node *expr)
Definition nodeFuncs.c:1392
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define copyObject(obj)
Definition nodes.h:232
#define nodeTag(nodeptr)
Definition nodes.h:139
#define makeNode(_type_)
Definition nodes.h:161
static char * errmsg
Node * coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, CoercionContext ccontext, CoercionForm cformat, int location, bool hideInputCoercion)
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition parse_expr.c:121
int parser_errposition(ParseState *pstate, int location)
Definition parse_node.c:106
SubscriptingRef * transformContainerSubscripts(ParseState *pstate, Node *containerBase, Oid containerType, int32 containerTypMod, List *indirection, bool isAssignment)
Definition parse_node.c:243
void transformContainerType(Oid *containerType, int32 *containerTypmod)
Definition parse_node.c:189
ParseExprKind
Definition parse_node.h:39
@ EXPR_KIND_INSERT_TARGET
Definition parse_node.h:55
@ EXPR_KIND_UPDATE_TARGET
Definition parse_node.h:57
@ EXPR_KIND_UPDATE_SOURCE
Definition parse_node.h:56
CommonTableExpr * GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
void markVarForSelectPriv(ParseState *pstate, Var *var)
void errorMissingRTE(ParseState *pstate, RangeVar *relation)
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
List * expandNSItemVars(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, VarReturningType returning_type, int location, bool include_dropped, List **colnames, List **colvars)
ParseNamespaceItem * GetNSItemByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
Oid attnumTypeId(Relation rd, int attid)
List * expandNSItemAttrs(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, bool require_col_privs, int location)
ParseNamespaceItem * refnameNamespaceItem(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
TargetEntry * transformTargetEntry(ParseState *pstate, Node *node, Node *expr, ParseExprKind exprKind, char *colname, bool resjunk)
static List * ExpandColumnRefStar(ParseState *pstate, ColumnRef *cref, bool make_target_entry)
Expr * transformAssignedExpr(ParseState *pstate, Expr *expr, ParseExprKind exprKind, const char *colname, int attrno, List *indirection, int location)
static int FigureColnameInternal(Node *node, char **name)
List * transformExpressionList(ParseState *pstate, List *exprlist, ParseExprKind exprKind, bool allowDefault)
static void markTargetListOrigin(ParseState *pstate, TargetEntry *tle, Var *var, int levelsup)
char * FigureColname(Node *node)
static List * ExpandSingleTable(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location, bool make_target_entry)
char * FigureIndexColname(Node *node)
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)
static List * ExpandRowReference(ParseState *pstate, Node *expr, bool make_target_entry)
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)
void updateTargetListEntry(ParseState *pstate, TargetEntry *tle, char *colname, int attrno, List *indirection, int location)
List * transformTargetList(ParseState *pstate, List *targetlist, ParseExprKind exprKind)
void resolveTargetListUnknowns(ParseState *pstate, List *targetlist)
void markTargetListOrigins(ParseState *pstate, List *targetlist)
static List * ExpandIndirectionStar(ParseState *pstate, A_Indirection *ind, bool make_target_entry, ParseExprKind exprKind)
static List * ExpandAllTables(ParseState *pstate, int location)
List * checkInsertTargets(ParseState *pstate, List *cols, List **attrnos)
TupleDesc expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
Oid typeidTypeRelid(Oid type_id)
Definition parse_type.c:668
#define GetCTETargetList(cte)
@ AEXPR_NULLIF
Definition parsenodes.h:335
@ RTE_JOIN
@ RTE_CTE
@ RTE_NAMEDTUPLESTORE
@ RTE_VALUES
@ RTE_SUBQUERY
@ RTE_RESULT
@ RTE_FUNCTION
@ RTE_TABLEFUNC
@ RTE_GROUP
@ RTE_GRAPH_TABLE
@ RTE_RELATION
#define ACL_SELECT
Definition parsenodes.h:77
int16 attnum
FormData_pg_attribute * Form_pg_attribute
static char * label
NameData relname
Definition pg_class.h:40
#define lfirst(lc)
Definition pg_list.h:172
#define llast(l)
Definition pg_list.h:198
static int list_length(const List *l)
Definition pg_list.h:152
#define NIL
Definition pg_list.h:68
#define forboth(cell1, list1, cell2, list2)
Definition pg_list.h:518
#define lthird(l)
Definition pg_list.h:188
#define list_make1(x1)
Definition pg_list.h:212
#define for_each_cell(cell, lst, initcell)
Definition pg_list.h:438
static void * list_nth(const List *list, int n)
Definition pg_list.h:299
#define linitial(l)
Definition pg_list.h:178
#define lsecond(l)
Definition pg_list.h:183
static ListCell * list_head(const List *l)
Definition pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:343
#define list_make1_int(x1)
Definition pg_list.h:227
unsigned int Oid
e
static int fb(int x)
@ ARRAY_SUBLINK
Definition primnodes.h:1036
@ ANY_SUBLINK
Definition primnodes.h:1032
@ MULTIEXPR_SUBLINK
Definition primnodes.h:1035
@ CTE_SUBLINK
Definition primnodes.h:1037
@ EXPR_SUBLINK
Definition primnodes.h:1034
@ ROWCOMPARE_SUBLINK
Definition primnodes.h:1033
@ ALL_SUBLINK
Definition primnodes.h:1031
@ EXISTS_SUBLINK
Definition primnodes.h:1030
@ IS_LEAST
Definition primnodes.h:1529
@ IS_GREATEST
Definition primnodes.h:1528
@ SVFOP_CURRENT_CATALOG
Definition primnodes.h:1575
@ SVFOP_LOCALTIME_N
Definition primnodes.h:1568
@ SVFOP_CURRENT_TIMESTAMP
Definition primnodes.h:1565
@ SVFOP_LOCALTIME
Definition primnodes.h:1567
@ SVFOP_CURRENT_TIMESTAMP_N
Definition primnodes.h:1566
@ SVFOP_CURRENT_ROLE
Definition primnodes.h:1571
@ SVFOP_USER
Definition primnodes.h:1573
@ SVFOP_CURRENT_SCHEMA
Definition primnodes.h:1576
@ SVFOP_LOCALTIMESTAMP_N
Definition primnodes.h:1570
@ SVFOP_CURRENT_DATE
Definition primnodes.h:1562
@ SVFOP_CURRENT_TIME_N
Definition primnodes.h:1564
@ SVFOP_CURRENT_TIME
Definition primnodes.h:1563
@ SVFOP_LOCALTIMESTAMP
Definition primnodes.h:1569
@ SVFOP_CURRENT_USER
Definition primnodes.h:1572
@ SVFOP_SESSION_USER
Definition primnodes.h:1574
@ IS_DOCUMENT
Definition primnodes.h:1613
@ IS_XMLFOREST
Definition primnodes.h:1608
@ IS_XMLCONCAT
Definition primnodes.h:1606
@ IS_XMLPI
Definition primnodes.h:1610
@ IS_XMLPARSE
Definition primnodes.h:1609
@ IS_XMLSERIALIZE
Definition primnodes.h:1612
@ IS_XMLROOT
Definition primnodes.h:1611
@ IS_XMLELEMENT
Definition primnodes.h:1607
@ JSON_QUERY_OP
Definition primnodes.h:1830
@ JSON_EXISTS_OP
Definition primnodes.h:1829
@ JSON_VALUE_OP
Definition primnodes.h:1831
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:769
CoercionContext
Definition primnodes.h:746
@ COERCION_ASSIGNMENT
Definition primnodes.h:748
@ COERCION_IMPLICIT
Definition primnodes.h:747
#define RelationGetNumberOfAttributes(relation)
Definition rel.h:520
#define RelationGetRelationName(relation)
Definition rel.h:548
List * newvals
Definition primnodes.h:1194
Expr * arg
Definition primnodes.h:1193
Definition pg_list.h:54
Definition nodes.h:135
ParseState * parentParseState
Definition parse_node.h:209
ParseNamespaceItem * p_target_nsitem
Definition parse_node.h:225
ParseExprKind p_expr_kind
Definition parse_node.h:228
List * p_multiassign_exprs
Definition parse_node.h:230
PreParseColumnRefHook p_pre_columnref_hook
Definition parse_node.h:254
List * p_namespace
Definition parse_node.h:218
int p_next_resno
Definition parse_node.h:229
Relation p_target_relation
Definition parse_node.h:224
PostParseColumnRefHook p_post_columnref_hook
Definition parse_node.h:255
List * p_rtable
Definition parse_node.h:211
List * targetList
Definition parsenodes.h:198
TupleDesc rd_att
Definition rel.h:112
Node * val
Definition parsenodes.h:547
char * name
Definition parsenodes.h:545
Definition value.h:64
Expr * refassgnexpr
Definition primnodes.h:736
ParseLoc location
Definition primnodes.h:311
AttrNumber varattno
Definition primnodes.h:275
int varno
Definition primnodes.h:270
VarReturningType varreturningtype
Definition primnodes.h:298
Index varlevelsup
Definition primnodes.h:295
TupleDesc CreateTemplateTupleDesc(int natts)
Definition tupdesc.c:165
void TupleDescFinalize(TupleDesc tupdesc)
Definition tupdesc.c:508
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition tupdesc.c:1081
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition tupdesc.c:897
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178
#define strVal(v)
Definition value.h:82
const char * name