1 /*-------------------------------------------------------------------------
4 * handle clauses in parser
6 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/parser/parse_clause.c,v 1.133 2004/06/16 01:26:44 tgl Exp $
13 *-------------------------------------------------------------------------
18 #include "access/heapam.h"
19 #include "catalog/heap.h"
20 #include "nodes/makefuncs.h"
21 #include "optimizer/clauses.h"
22 #include "optimizer/tlist.h"
23 #include "optimizer/var.h"
24 #include "parser/analyze.h"
25 #include "parser/parsetree.h"
26 #include "parser/parse_clause.h"
27 #include "parser/parse_coerce.h"
28 #include "parser/parse_expr.h"
29 #include "parser/parse_oper.h"
30 #include "parser/parse_relation.h"
31 #include "parser/parse_target.h"
32 #include "parser/parse_type.h"
33 #include "rewrite/rewriteManip.h"
34 #include "utils/builtins.h"
35 #include "utils/guc.h"
38 #define ORDER_CLAUSE 0
39 #define GROUP_CLAUSE 1
40 #define DISTINCT_ON_CLAUSE 2
42 static char *clauseText[] = {"ORDER BY", "GROUP BY", "DISTINCT ON"};
44 static void extractRemainingColumns(List *common_colnames,
45 List *src_colnames, List *src_colvars,
46 List **res_colnames, List **res_colvars);
47 static Node *transformJoinUsingClause(ParseState *pstate,
48 List *leftVars, List *rightVars);
49 static Node *transformJoinOnClause(ParseState *pstate, JoinExpr *j,
51 static RangeTblRef *transformTableEntry(ParseState *pstate, RangeVar *r);
52 static RangeTblRef *transformRangeSubselect(ParseState *pstate,
54 static RangeTblRef *transformRangeFunction(ParseState *pstate,
56 static Node *transformFromClauseItem(ParseState *pstate, Node *n,
57 List **containedRels);
58 static Node *buildMergedJoinVar(ParseState *pstate, JoinType jointype,
59 Var *l_colvar, Var *r_colvar);
60 static TargetEntry *findTargetlistEntry(ParseState *pstate, Node *node,
61 List **tlist, int clause);
65 * transformFromClause -
66 * Process the FROM clause and add items to the query's range table,
67 * joinlist, and namespace.
69 * Note: we assume that pstate's p_rtable, p_joinlist, and p_namespace lists
70 * were initialized to NIL when the pstate was created. We will add onto
71 * any entries already present --- this is needed for rule processing, as
72 * well as for UPDATE and DELETE.
74 * The range table may grow still further when we transform the expressions
75 * in the query's quals and target list. (This is possible because in
76 * POSTQUEL, we allowed references to relations not specified in the
77 * from-clause. PostgreSQL keeps this extension to standard SQL.)
80 transformFromClause(ParseState *pstate, List *frmList)
85 * The grammar will have produced a list of RangeVars,
86 * RangeSubselects, RangeFunctions, and/or JoinExprs. Transform each
87 * one (possibly adding entries to the rtable), check for duplicate
88 * refnames, and then add it to the joinlist and namespace.
95 n = transformFromClauseItem(pstate, n, &containedRels);
96 checkNameSpaceConflicts(pstate, (Node *) pstate->p_namespace, n);
97 pstate->p_joinlist = lappend(pstate->p_joinlist, n);
98 pstate->p_namespace = lappend(pstate->p_namespace, n);
104 * Add the target relation of INSERT/UPDATE/DELETE to the range table,
105 * and make the special links to it in the ParseState.
107 * We also open the target relation and acquire a write lock on it.
108 * This must be done before processing the FROM list, in case the target
109 * is also mentioned as a source relation --- we want to be sure to grab
110 * the write lock before any read lock.
112 * If alsoSource is true, add the target to the query's joinlist and
113 * namespace. For INSERT, we don't want the target to be joined to;
114 * it's a destination of tuples, not a source. For UPDATE/DELETE,
115 * we do need to scan or join the target. (NOTE: we do not bother
116 * to check for namespace conflict; we assume that the namespace was
117 * initially empty in these cases.)
119 * Finally, we mark the relation as requiring the permissions specified
122 * Returns the rangetable index of the target relation.
125 setTargetTable(ParseState *pstate, RangeVar *relation,
126 bool inh, bool alsoSource, AclMode requiredPerms)
131 /* Close old target; this could only happen for multi-action rules */
132 if (pstate->p_target_relation != NULL)
133 heap_close(pstate->p_target_relation, NoLock);
136 * Open target rel and grab suitable lock (which we will hold till end
139 * analyze.c will eventually do the corresponding heap_close(), but *not*
142 pstate->p_target_relation = heap_openrv(relation, RowExclusiveLock);
147 rte = addRangeTableEntry(pstate, relation, NULL, inh, false);
148 pstate->p_target_rangetblentry = rte;
150 /* assume new rte is at end */
151 rtindex = list_length(pstate->p_rtable);
152 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
155 * Override addRangeTableEntry's default ACL_SELECT permissions check,
156 * and instead mark target table as requiring exactly the specified
159 * If we find an explicit reference to the rel later during parse
160 * analysis, scanRTEForColumn will add the ACL_SELECT bit back again.
161 * That can't happen for INSERT but it is possible for UPDATE and DELETE.
163 rte->requiredPerms = requiredPerms;
166 * If UPDATE/DELETE, add table to joinlist and namespace.
169 addRTEtoQuery(pstate, rte, true, true);
175 * Simplify InhOption (yes/no/default) into boolean yes/no.
177 * The reason we do things this way is that we don't want to examine the
178 * SQL_inheritance option flag until parse_analyze is run. Otherwise,
179 * we'd do the wrong thing with query strings that intermix SET commands
183 interpretInhOption(InhOption inhOpt)
192 return SQL_inheritance;
194 elog(ERROR, "bogus InhOption value: %d", inhOpt);
195 return false; /* keep compiler quiet */
199 * Given an enum that indicates whether WITH / WITHOUT OIDS was
200 * specified by the user, return true iff the specified table/result
201 * set should be created with OIDs. This needs to be done after
202 * parsing the query string because the return value can depend upon
203 * the default_with_oids GUC var.
206 interpretOidsOption(ContainsOids opt)
213 case MUST_NOT_HAVE_OIDS:
217 return default_with_oids;
220 elog(ERROR, "bogus ContainsOids value: %d", opt);
221 return false; /* keep compiler quiet */
225 * Extract all not-in-common columns from column lists of a source table
228 extractRemainingColumns(List *common_colnames,
229 List *src_colnames, List *src_colvars,
230 List **res_colnames, List **res_colvars)
232 List *new_colnames = NIL;
233 List *new_colvars = NIL;
234 ListCell *lnames, *lvars;
236 Assert(list_length(src_colnames) == list_length(src_colvars));
238 forboth(lnames, src_colnames, lvars, src_colvars)
240 char *colname = strVal(lfirst(lnames));
244 foreach(cnames, common_colnames)
246 char *ccolname = strVal(lfirst(cnames));
248 if (strcmp(colname, ccolname) == 0)
257 new_colnames = lappend(new_colnames, lfirst(lnames));
258 new_colvars = lappend(new_colvars, lfirst(lvars));
262 *res_colnames = new_colnames;
263 *res_colvars = new_colvars;
266 /* transformJoinUsingClause()
267 * Build a complete ON clause from a partially-transformed USING list.
268 * We are given lists of nodes representing left and right match columns.
269 * Result is a transformed qualification expression.
272 transformJoinUsingClause(ParseState *pstate, List *leftVars, List *rightVars)
275 ListCell *lvars, *rvars;
278 * We cheat a little bit here by building an untransformed operator
279 * tree whose leaves are the already-transformed Vars. This is OK
280 * because transformExpr() won't complain about already-transformed
283 forboth(lvars, leftVars, rvars, rightVars)
285 Node *lvar = (Node *) lfirst(lvars);
286 Node *rvar = (Node *) lfirst(rvars);
289 e = makeSimpleA_Expr(AEXPR_OP, "=", copyObject(lvar), copyObject(rvar));
297 a = makeA_Expr(AEXPR_AND, NIL, result, (Node *) e);
303 * Since the references are already Vars, and are certainly from the
304 * input relations, we don't have to go through the same pushups that
305 * transformJoinOnClause() does. Just invoke transformExpr() to fix
306 * up the operators, and we're done.
308 result = transformExpr(pstate, result);
310 result = coerce_to_boolean(pstate, result, "JOIN/USING");
315 /* transformJoinOnClause()
316 * Transform the qual conditions for JOIN/ON.
317 * Result is a transformed qualification expression.
320 transformJoinOnClause(ParseState *pstate, JoinExpr *j,
324 List *save_namespace;
325 Relids clause_varnos;
329 * This is a tad tricky, for two reasons. First, the namespace that
330 * the join expression should see is just the two subtrees of the JOIN
331 * plus any outer references from upper pstate levels. So,
332 * temporarily set this pstate's namespace accordingly. (We need not
333 * check for refname conflicts, because transformFromClauseItem()
334 * already did.) NOTE: this code is OK only because the ON clause
335 * can't legally alter the namespace by causing implicit relation refs
338 save_namespace = pstate->p_namespace;
339 pstate->p_namespace = list_make2(j->larg, j->rarg);
341 result = transformWhereClause(pstate, j->quals, "JOIN/ON");
343 pstate->p_namespace = save_namespace;
346 * Second, we need to check that the ON condition doesn't refer to any
347 * rels outside the input subtrees of the JOIN. It could do that
348 * despite our hack on the namespace if it uses fully-qualified names.
349 * So, grovel through the transformed clause and make sure there are
350 * no bogus references. (Outer references are OK, and are ignored
353 clause_varnos = pull_varnos(result);
354 while ((varno = bms_first_member(clause_varnos)) >= 0)
356 if (!list_member_int(containedRels, varno))
359 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
360 errmsg("JOIN/ON clause refers to \"%s\", which is not part of JOIN",
361 rt_fetch(varno, pstate->p_rtable)->eref->aliasname)));
364 bms_free(clause_varnos);
370 * transformTableEntry --- transform a RangeVar (simple relation reference)
373 transformTableEntry(ParseState *pstate, RangeVar *r)
379 * mark this entry to indicate it comes from the FROM clause. In SQL,
380 * the target list can only refer to range variables specified in the
381 * from clause but we follow the more powerful POSTQUEL semantics and
382 * automatically generate the range variable if not specified. However
383 * there are times we need to know whether the entries are legitimate.
385 rte = addRangeTableEntry(pstate, r, r->alias,
386 interpretInhOption(r->inhOpt), true);
389 * We create a RangeTblRef, but we do not add it to the joinlist or
390 * namespace; our caller must do that if appropriate.
392 rtr = makeNode(RangeTblRef);
393 /* assume new rte is at end */
394 rtr->rtindex = list_length(pstate->p_rtable);
395 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
402 * transformRangeSubselect --- transform a sub-SELECT appearing in FROM
405 transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
413 * We require user to supply an alias for a subselect, per SQL92. To
414 * relax this, we'd have to be prepared to gin up a unique alias for
415 * an unlabeled subselect.
417 if (r->alias == NULL)
419 (errcode(ERRCODE_SYNTAX_ERROR),
420 errmsg("subquery in FROM must have an alias")));
423 * Analyze and transform the subquery.
425 parsetrees = parse_sub_analyze(r->subquery, pstate);
428 * Check that we got something reasonable. Most of these conditions
429 * are probably impossible given restrictions of the grammar, but
432 if (list_length(parsetrees) != 1)
433 elog(ERROR, "unexpected parse analysis result for subquery in FROM");
434 query = (Query *) linitial(parsetrees);
435 if (query == NULL || !IsA(query, Query))
436 elog(ERROR, "unexpected parse analysis result for subquery in FROM");
438 if (query->commandType != CMD_SELECT)
439 elog(ERROR, "expected SELECT query from subquery in FROM");
440 if (query->resultRelation != 0 || query->into != NULL)
442 (errcode(ERRCODE_SYNTAX_ERROR),
443 errmsg("subquery in FROM may not have SELECT INTO")));
446 * The subquery cannot make use of any variables from FROM items
447 * created earlier in the current query. Per SQL92, the scope of a
448 * FROM item does not include other FROM items. Formerly we hacked
449 * the namespace so that the other variables weren't even visible, but
450 * it seems more useful to leave them visible and give a specific
453 * XXX this will need further work to support SQL99's LATERAL() feature,
454 * wherein such references would indeed be legal.
456 * We can skip groveling through the subquery if there's not anything
457 * visible in the current query. Also note that outer references are
460 if (pstate->p_namespace)
462 if (contain_vars_of_level((Node *) query, 1))
464 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
465 errmsg("subquery in FROM may not refer to other relations of same query level")));
469 * OK, build an RTE for the subquery.
471 rte = addRangeTableEntryForSubquery(pstate, query, r->alias, true);
474 * We create a RangeTblRef, but we do not add it to the joinlist or
475 * namespace; our caller must do that if appropriate.
477 rtr = makeNode(RangeTblRef);
478 /* assume new rte is at end */
479 rtr->rtindex = list_length(pstate->p_rtable);
480 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
487 * transformRangeFunction --- transform a function call appearing in FROM
490 transformRangeFunction(ParseState *pstate, RangeFunction *r)
497 /* Get function name for possible use as alias */
498 Assert(IsA(r->funccallnode, FuncCall));
499 funcname = strVal(llast(((FuncCall *) r->funccallnode)->funcname));
502 * Transform the raw FuncCall node.
504 funcexpr = transformExpr(pstate, r->funccallnode);
507 * The function parameters cannot make use of any variables from other
508 * FROM items. (Compare to transformRangeSubselect(); the coding is
509 * different though because we didn't parse as a sub-select with its
510 * own level of namespace.)
512 * XXX this will need further work to support SQL99's LATERAL() feature,
513 * wherein such references would indeed be legal.
515 if (pstate->p_namespace)
517 if (contain_vars_of_level(funcexpr, 0))
519 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
520 errmsg("function expression in FROM may not refer to other relations of same query level")));
524 * Disallow aggregate functions in the expression. (No reason to
525 * postpone this check until parseCheckAggregates.)
527 if (pstate->p_hasAggs)
529 if (checkExprHasAggs(funcexpr))
531 (errcode(ERRCODE_GROUPING_ERROR),
532 errmsg("cannot use aggregate function in function expression in FROM")));
536 * If a coldeflist is supplied, ensure it defines a legal set of names
537 * (no duplicates) and datatypes (no pseudo-types, for instance).
543 tupdesc = BuildDescForRelation(r->coldeflist);
544 CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE);
548 * OK, build an RTE for the function.
550 rte = addRangeTableEntryForFunction(pstate, funcname, funcexpr,
554 * We create a RangeTblRef, but we do not add it to the joinlist or
555 * namespace; our caller must do that if appropriate.
557 rtr = makeNode(RangeTblRef);
558 /* assume new rte is at end */
559 rtr->rtindex = list_length(pstate->p_rtable);
560 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
567 * transformFromClauseItem -
568 * Transform a FROM-clause item, adding any required entries to the
569 * range table list being built in the ParseState, and return the
570 * transformed item ready to include in the joinlist and namespace.
571 * This routine can recurse to handle SQL92 JOIN expressions.
573 * Aside from the primary return value (the transformed joinlist item)
574 * this routine also returns an integer list of the rangetable indexes
575 * of all the base and join relations represented in the joinlist item.
576 * This list is needed for checking JOIN/ON conditions in higher levels.
579 transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels)
581 if (IsA(n, RangeVar))
583 /* Plain relation reference */
586 rtr = transformTableEntry(pstate, (RangeVar *) n);
587 *containedRels = list_make1_int(rtr->rtindex);
590 else if (IsA(n, RangeSubselect))
592 /* sub-SELECT is like a plain relation */
595 rtr = transformRangeSubselect(pstate, (RangeSubselect *) n);
596 *containedRels = list_make1_int(rtr->rtindex);
599 else if (IsA(n, RangeFunction))
601 /* function is like a plain relation */
604 rtr = transformRangeFunction(pstate, (RangeFunction *) n);
605 *containedRels = list_make1_int(rtr->rtindex);
608 else if (IsA(n, JoinExpr))
610 /* A newfangled join expression */
611 JoinExpr *j = (JoinExpr *) n;
612 List *my_containedRels,
626 * Recursively process the left and right subtrees
628 j->larg = transformFromClauseItem(pstate, j->larg, &l_containedRels);
629 j->rarg = transformFromClauseItem(pstate, j->rarg, &r_containedRels);
632 * Generate combined list of relation indexes for possible use by
633 * transformJoinOnClause below.
635 my_containedRels = list_concat(l_containedRels, r_containedRels);
638 * Check for conflicting refnames in left and right subtrees. Must
639 * do this because higher levels will assume I hand back a self-
640 * consistent namespace subtree.
642 checkNameSpaceConflicts(pstate, j->larg, j->rarg);
645 * Extract column name and var lists from both subtrees
647 * Note: expandRTE returns new lists, safe for me to modify
649 if (IsA(j->larg, RangeTblRef))
650 leftrti = ((RangeTblRef *) j->larg)->rtindex;
651 else if (IsA(j->larg, JoinExpr))
652 leftrti = ((JoinExpr *) j->larg)->rtindex;
655 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(j->larg));
656 leftrti = 0; /* keep compiler quiet */
658 rte = rt_fetch(leftrti, pstate->p_rtable);
659 expandRTE(pstate, rte, &l_colnames, &l_colvars);
661 if (IsA(j->rarg, RangeTblRef))
662 rightrti = ((RangeTblRef *) j->rarg)->rtindex;
663 else if (IsA(j->rarg, JoinExpr))
664 rightrti = ((JoinExpr *) j->rarg)->rtindex;
667 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(j->rarg));
668 rightrti = 0; /* keep compiler quiet */
670 rte = rt_fetch(rightrti, pstate->p_rtable);
671 expandRTE(pstate, rte, &r_colnames, &r_colvars);
674 * Natural join does not explicitly specify columns; must generate
675 * columns to join. Need to run through the list of columns from
676 * each table or join result and match up the column names. Use
677 * the first table, and check every column in the second table for
678 * a match. (We'll check that the matches were unique later on.)
679 * The result of this step is a list of column names just like an
680 * explicitly-written USING list.
688 Assert(j->using == NIL); /* shouldn't have USING() too */
690 foreach(lx, l_colnames)
692 char *l_colname = strVal(lfirst(lx));
693 Value *m_name = NULL;
695 foreach(rx, r_colnames)
697 char *r_colname = strVal(lfirst(rx));
699 if (strcmp(l_colname, r_colname) == 0)
701 m_name = makeString(l_colname);
706 /* matched a right column? then keep as join column... */
708 rlist = lappend(rlist, m_name);
715 * Now transform the join qualifications, if any.
723 * JOIN/USING (or NATURAL JOIN, as transformed above).
724 * Transform the list into an explicit ON-condition, and
725 * generate a list of merged result columns.
727 List *ucols = j->using;
728 List *l_usingvars = NIL;
729 List *r_usingvars = NIL;
732 Assert(j->quals == NULL); /* shouldn't have ON() too */
736 char *u_colname = strVal(lfirst(ucol));
744 /* Check for USING(foo,foo) */
745 foreach(col, res_colnames)
747 char *res_colname = strVal(lfirst(col));
749 if (strcmp(res_colname, u_colname) == 0)
751 (errcode(ERRCODE_DUPLICATE_COLUMN),
752 errmsg("column name \"%s\" appears more than once in USING clause",
756 /* Find it in left input */
758 foreach(col, l_colnames)
760 char *l_colname = strVal(lfirst(col));
762 if (strcmp(l_colname, u_colname) == 0)
766 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
767 errmsg("common column name \"%s\" appears more than once in left table",
775 (errcode(ERRCODE_UNDEFINED_COLUMN),
776 errmsg("column \"%s\" specified in USING clause does not exist in left table",
779 /* Find it in right input */
781 foreach(col, r_colnames)
783 char *r_colname = strVal(lfirst(col));
785 if (strcmp(r_colname, u_colname) == 0)
789 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
790 errmsg("common column name \"%s\" appears more than once in right table",
798 (errcode(ERRCODE_UNDEFINED_COLUMN),
799 errmsg("column \"%s\" specified in USING clause does not exist in right table",
802 l_colvar = list_nth(l_colvars, l_index);
803 l_usingvars = lappend(l_usingvars, l_colvar);
804 r_colvar = list_nth(r_colvars, r_index);
805 r_usingvars = lappend(r_usingvars, r_colvar);
807 res_colnames = lappend(res_colnames, lfirst(ucol));
808 res_colvars = lappend(res_colvars,
809 buildMergedJoinVar(pstate,
815 j->quals = transformJoinUsingClause(pstate,
821 /* User-written ON-condition; transform it */
822 j->quals = transformJoinOnClause(pstate, j, my_containedRels);
826 /* CROSS JOIN: no quals */
829 /* Add remaining columns from each side to the output columns */
830 extractRemainingColumns(res_colnames,
831 l_colnames, l_colvars,
832 &l_colnames, &l_colvars);
833 extractRemainingColumns(res_colnames,
834 r_colnames, r_colvars,
835 &r_colnames, &r_colvars);
836 res_colnames = list_concat(res_colnames, l_colnames);
837 res_colvars = list_concat(res_colvars, l_colvars);
838 res_colnames = list_concat(res_colnames, r_colnames);
839 res_colvars = list_concat(res_colvars, r_colvars);
842 * Check alias (AS clause), if any.
846 if (j->alias->colnames != NIL)
848 if (list_length(j->alias->colnames) > list_length(res_colnames))
850 (errcode(ERRCODE_SYNTAX_ERROR),
851 errmsg("column alias list for \"%s\" has too many entries",
852 j->alias->aliasname)));
857 * Now build an RTE for the result of the join
859 rte = addRangeTableEntryForJoin(pstate,
866 /* assume new rte is at end */
867 j->rtindex = list_length(pstate->p_rtable);
868 Assert(rte == rt_fetch(j->rtindex, pstate->p_rtable));
871 * Include join RTE in returned containedRels list
873 *containedRels = lcons_int(j->rtindex, my_containedRels);
878 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(n));
879 return NULL; /* can't get here, keep compiler quiet */
883 * buildMergedJoinVar -
884 * generate a suitable replacement expression for a merged join column
887 buildMergedJoinVar(ParseState *pstate, JoinType jointype,
888 Var *l_colvar, Var *r_colvar)
897 * Choose output type if input types are dissimilar.
899 outcoltype = l_colvar->vartype;
900 outcoltypmod = l_colvar->vartypmod;
901 if (outcoltype != r_colvar->vartype)
903 outcoltype = select_common_type(list_make2_oid(l_colvar->vartype,
906 outcoltypmod = -1; /* ie, unknown */
908 else if (outcoltypmod != r_colvar->vartypmod)
910 /* same type, but not same typmod */
911 outcoltypmod = -1; /* ie, unknown */
915 * Insert coercion functions if needed. Note that a difference in
916 * typmod can only happen if input has typmod but outcoltypmod is -1.
917 * In that case we insert a RelabelType to clearly mark that result's
918 * typmod is not same as input. We never need coerce_type_typmod.
920 if (l_colvar->vartype != outcoltype)
921 l_node = coerce_type(pstate, (Node *) l_colvar, l_colvar->vartype,
922 outcoltype, outcoltypmod,
923 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
924 else if (l_colvar->vartypmod != outcoltypmod)
925 l_node = (Node *) makeRelabelType((Expr *) l_colvar,
926 outcoltype, outcoltypmod,
927 COERCE_IMPLICIT_CAST);
929 l_node = (Node *) l_colvar;
931 if (r_colvar->vartype != outcoltype)
932 r_node = coerce_type(pstate, (Node *) r_colvar, r_colvar->vartype,
933 outcoltype, outcoltypmod,
934 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
935 else if (r_colvar->vartypmod != outcoltypmod)
936 r_node = (Node *) makeRelabelType((Expr *) r_colvar,
937 outcoltype, outcoltypmod,
938 COERCE_IMPLICIT_CAST);
940 r_node = (Node *) r_colvar;
943 * Choose what to emit
950 * We can use either var; prefer non-coerced one if available.
952 if (IsA(l_node, Var))
954 else if (IsA(r_node, Var))
960 /* Always use left var */
964 /* Always use right var */
970 * Here we must build a COALESCE expression to ensure that
971 * the join output is non-null if either input is.
973 CoalesceExpr *c = makeNode(CoalesceExpr);
975 c->coalescetype = outcoltype;
976 c->args = list_make2(l_node, r_node);
977 res_node = (Node *) c;
981 elog(ERROR, "unrecognized join type: %d", (int) jointype);
982 res_node = NULL; /* keep compiler quiet */
991 * transformWhereClause -
992 * Transform the qualification and make sure it is of type boolean.
993 * Used for WHERE and allied clauses.
995 * constructName does not affect the semantics, but is used in error messages
998 transformWhereClause(ParseState *pstate, Node *clause,
999 const char *constructName)
1006 qual = transformExpr(pstate, clause);
1008 qual = coerce_to_boolean(pstate, qual, constructName);
1015 * transformLimitClause -
1016 * Transform the expression and make sure it is of type integer.
1017 * Used for LIMIT and allied clauses.
1019 * constructName does not affect the semantics, but is used in error messages
1022 transformLimitClause(ParseState *pstate, Node *clause,
1023 const char *constructName)
1030 qual = transformExpr(pstate, clause);
1032 qual = coerce_to_integer(pstate, qual, constructName);
1035 * LIMIT can't refer to any vars or aggregates of the current query;
1036 * we don't allow subselects either (though that case would at least
1039 if (contain_vars_of_level(qual, 0))
1042 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1043 /* translator: %s is name of a SQL construct, eg LIMIT */
1044 errmsg("argument of %s must not contain variables",
1047 if (checkExprHasAggs(qual))
1050 (errcode(ERRCODE_GROUPING_ERROR),
1051 /* translator: %s is name of a SQL construct, eg LIMIT */
1052 errmsg("argument of %s must not contain aggregates",
1055 if (contain_subplans(qual))
1058 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1059 /* translator: %s is name of a SQL construct, eg LIMIT */
1060 errmsg("argument of %s must not contain subqueries",
1069 * findTargetlistEntry -
1070 * Returns the targetlist entry matching the given (untransformed) node.
1071 * If no matching entry exists, one is created and appended to the target
1072 * list as a "resjunk" node.
1074 * node the ORDER BY, GROUP BY, or DISTINCT ON expression to be matched
1075 * tlist the target list (passed by reference so we can append to it)
1076 * clause identifies clause type being processed
1078 static TargetEntry *
1079 findTargetlistEntry(ParseState *pstate, Node *node, List **tlist, int clause)
1081 TargetEntry *target_result = NULL;
1086 * Handle two special cases as mandated by the SQL92 spec:
1088 * 1. Bare ColumnName (no qualifier or subscripts)
1089 * For a bare identifier, we search for a matching column name
1090 * in the existing target list. Multiple matches are an error
1091 * unless they refer to identical values; for example,
1092 * we allow SELECT a, a FROM table ORDER BY a
1093 * but not SELECT a AS b, b FROM table ORDER BY b
1094 * If no match is found, we fall through and treat the identifier
1096 * For GROUP BY, it is incorrect to match the grouping item against
1097 * targetlist entries: according to SQL92, an identifier in GROUP BY
1098 * is a reference to a column name exposed by FROM, not to a target
1099 * list column. However, many implementations (including pre-7.0
1100 * PostgreSQL) accept this anyway. So for GROUP BY, we look first
1101 * to see if the identifier matches any FROM column name, and only
1102 * try for a targetlist name if it doesn't. This ensures that we
1103 * adhere to the spec in the case where the name could be both.
1104 * DISTINCT ON isn't in the standard, so we can do what we like there;
1105 * we choose to make it work like ORDER BY, on the rather flimsy
1106 * grounds that ordinary DISTINCT works on targetlist entries.
1108 * 2. IntegerConstant
1109 * This means to use the n'th item in the existing target list.
1110 * Note that it would make no sense to order/group/distinct by an
1111 * actual constant, so this does not create a conflict with our
1112 * extension to order/group by an expression.
1113 * GROUP BY column-number is not allowed by SQL92, but since
1114 * the standard has no other behavior defined for this syntax,
1115 * we may as well accept this common extension.
1117 * Note that pre-existing resjunk targets must not be used in either case,
1118 * since the user didn't write them in his SELECT list.
1120 * If neither special case applies, fall through to treat the item as
1124 if (IsA(node, ColumnRef) &&
1125 list_length(((ColumnRef *) node)->fields) == 1)
1127 char *name = strVal(linitial(((ColumnRef *) node)->fields));
1129 if (clause == GROUP_CLAUSE)
1132 * In GROUP BY, we must prefer a match against a FROM-clause
1133 * column to one against the targetlist. Look to see if there
1134 * is a matching column. If so, fall through to let
1135 * transformExpr() do the rest. NOTE: if name could refer
1136 * ambiguously to more than one column name exposed by FROM,
1137 * colNameToVar will ereport(ERROR). That's just what we want
1140 * Small tweak for 7.4.3: ignore matches in upper query levels.
1141 * This effectively changes the search order for bare names to
1142 * (1) local FROM variables, (2) local targetlist aliases,
1143 * (3) outer FROM variables, whereas before it was (1) (3) (2).
1144 * SQL92 and SQL99 do not allow GROUPing BY an outer reference,
1145 * so this breaks no cases that are legal per spec, and it
1146 * seems a more self-consistent behavior.
1148 if (colNameToVar(pstate, name, true) != NULL)
1156 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1157 Resdom *resnode = tle->resdom;
1159 if (!resnode->resjunk &&
1160 strcmp(resnode->resname, name) == 0)
1162 if (target_result != NULL)
1164 if (!equal(target_result->expr, tle->expr))
1166 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1167 /* translator: first %s is name of a SQL construct, eg ORDER BY */
1168 errmsg("%s \"%s\" is ambiguous",
1169 clauseText[clause], name)));
1172 target_result = tle;
1173 /* Stay in loop to check for ambiguity */
1176 if (target_result != NULL)
1177 return target_result; /* return the first match */
1180 if (IsA(node, A_Const))
1182 Value *val = &((A_Const *) node)->val;
1183 int targetlist_pos = 0;
1186 if (!IsA(val, Integer))
1188 (errcode(ERRCODE_SYNTAX_ERROR),
1189 /* translator: %s is name of a SQL construct, eg ORDER BY */
1190 errmsg("non-integer constant in %s",
1191 clauseText[clause])));
1192 target_pos = intVal(val);
1195 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1196 Resdom *resnode = tle->resdom;
1198 if (!resnode->resjunk)
1200 if (++targetlist_pos == target_pos)
1201 return tle; /* return the unique match */
1205 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1206 /* translator: %s is name of a SQL construct, eg ORDER BY */
1207 errmsg("%s position %d is not in select list",
1208 clauseText[clause], target_pos)));
1212 * Otherwise, we have an expression (this is a Postgres extension not
1213 * found in SQL92). Convert the untransformed node to a transformed
1214 * expression, and search for a match in the tlist. NOTE: it doesn't
1215 * really matter whether there is more than one match. Also, we are
1216 * willing to match a resjunk target here, though the above cases must
1217 * ignore resjunk targets.
1219 expr = transformExpr(pstate, node);
1223 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1225 if (equal(expr, tle->expr))
1230 * If no matches, construct a new target entry which is appended to
1231 * the end of the target list. This target is given resjunk = TRUE so
1232 * that it will not be projected into the final tuple.
1234 target_result = transformTargetEntry(pstate, node, expr, NULL, true);
1236 *tlist = lappend(*tlist, target_result);
1238 return target_result;
1243 * transformGroupClause -
1244 * transform a GROUP BY clause
1246 * GROUP BY items will be added to the targetlist (as resjunk columns)
1247 * if not already present, so the targetlist must be passed by reference.
1250 transformGroupClause(ParseState *pstate, List *grouplist,
1251 List **targetlist, List *sortClause)
1257 sortItem = list_head(sortClause);
1259 foreach(gl, grouplist)
1266 tle = findTargetlistEntry(pstate, lfirst(gl),
1267 targetlist, GROUP_CLAUSE);
1269 /* avoid making duplicate grouplist entries */
1270 if (targetIsInSortList(tle, glist))
1273 /* if tlist item is an UNKNOWN literal, change it to TEXT */
1274 restype = tle->resdom->restype;
1276 if (restype == UNKNOWNOID)
1278 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
1279 restype, TEXTOID, -1,
1281 COERCE_IMPLICIT_CAST);
1282 restype = tle->resdom->restype = TEXTOID;
1283 tle->resdom->restypmod = -1;
1287 * If the GROUP BY clause matches the ORDER BY clause, we want to
1288 * adopt the ordering operators from the latter rather than using
1289 * the default ops. This allows "GROUP BY foo ORDER BY foo DESC"
1290 * to be done with only one sort step. Note we are assuming that
1291 * any user-supplied ordering operator will bring equal values
1292 * together, which is all that GROUP BY needs.
1295 ((SortClause *) lfirst(sortItem))->tleSortGroupRef ==
1296 tle->resdom->ressortgroupref)
1298 ordering_op = ((SortClause *) lfirst(sortItem))->sortop;
1299 sortItem = lnext(sortItem);
1303 ordering_op = ordering_oper_opid(restype);
1304 sortItem = NULL; /* disregard ORDER BY once match fails */
1307 grpcl = makeNode(GroupClause);
1308 grpcl->tleSortGroupRef = assignSortGroupRef(tle, *targetlist);
1309 grpcl->sortop = ordering_op;
1310 glist = lappend(glist, grpcl);
1317 * transformSortClause -
1318 * transform an ORDER BY clause
1320 * ORDER BY items will be added to the targetlist (as resjunk columns)
1321 * if not already present, so the targetlist must be passed by reference.
1324 transformSortClause(ParseState *pstate,
1327 bool resolveUnknown)
1329 List *sortlist = NIL;
1332 foreach(olitem, orderlist)
1334 SortBy *sortby = lfirst(olitem);
1337 tle = findTargetlistEntry(pstate, sortby->node,
1338 targetlist, ORDER_CLAUSE);
1340 sortlist = addTargetToSortList(pstate, tle,
1341 sortlist, *targetlist,
1342 sortby->sortby_kind,
1351 * transformDistinctClause -
1352 * transform a DISTINCT or DISTINCT ON clause
1354 * Since we may need to add items to the query's sortClause list, that list
1355 * is passed by reference. Likewise for the targetlist.
1358 transformDistinctClause(ParseState *pstate, List *distinctlist,
1359 List **targetlist, List **sortClause)
1365 /* No work if there was no DISTINCT clause */
1366 if (distinctlist == NIL)
1369 if (linitial(distinctlist) == NULL)
1371 /* We had SELECT DISTINCT */
1374 * All non-resjunk elements from target list that are not already
1375 * in the sort list should be added to it. (We don't really care
1376 * what order the DISTINCT fields are checked in, so we can leave
1377 * the user's ORDER BY spec alone, and just add additional sort
1378 * keys to it to ensure that all targetlist items get sorted.)
1380 *sortClause = addAllTargetsToSortList(pstate,
1386 * Now, DISTINCT list consists of all non-resjunk sortlist items.
1387 * Actually, all the sortlist items had better be non-resjunk!
1388 * Otherwise, user wrote SELECT DISTINCT with an ORDER BY item
1389 * that does not appear anywhere in the SELECT targetlist, and we
1390 * can't implement that with only one sorting pass...
1392 foreach(slitem, *sortClause)
1394 SortClause *scl = (SortClause *) lfirst(slitem);
1395 TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
1397 if (tle->resdom->resjunk)
1399 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1400 errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list")));
1402 result = lappend(result, copyObject(scl));
1407 /* We had SELECT DISTINCT ON (expr, ...) */
1410 * If the user writes both DISTINCT ON and ORDER BY, then the two
1411 * expression lists must match (until one or the other runs out).
1412 * Otherwise the ORDER BY requires a different sort order than the
1413 * DISTINCT does, and we can't implement that with only one sort
1414 * pass (and if we do two passes, the results will be rather
1415 * unpredictable). However, it's OK to have more DISTINCT ON
1416 * expressions than ORDER BY expressions; we can just add the
1417 * extra DISTINCT values to the sort list, much as we did above
1418 * for ordinary DISTINCT fields.
1420 * Actually, it'd be OK for the common prefixes of the two lists to
1421 * match in any order, but implementing that check seems like more
1422 * trouble than it's worth.
1424 ListCell *nextsortlist = list_head(*sortClause);
1426 foreach(dlitem, distinctlist)
1430 tle = findTargetlistEntry(pstate, lfirst(dlitem),
1431 targetlist, DISTINCT_ON_CLAUSE);
1433 if (nextsortlist != NULL)
1435 SortClause *scl = (SortClause *) lfirst(nextsortlist);
1437 if (tle->resdom->ressortgroupref != scl->tleSortGroupRef)
1439 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1440 errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions")));
1441 result = lappend(result, copyObject(scl));
1442 nextsortlist = lnext(nextsortlist);
1446 *sortClause = addTargetToSortList(pstate, tle,
1447 *sortClause, *targetlist,
1448 SORTBY_ASC, NIL, true);
1451 * Probably, the tle should always have been added at the
1452 * end of the sort list ... but search to be safe.
1454 foreach(slitem, *sortClause)
1456 SortClause *scl = (SortClause *) lfirst(slitem);
1458 if (tle->resdom->ressortgroupref == scl->tleSortGroupRef)
1460 result = lappend(result, copyObject(scl));
1464 if (slitem == NULL) /* should not happen */
1465 elog(ERROR, "failed to add DISTINCT ON clause to target list");
1474 * addAllTargetsToSortList
1475 * Make sure all non-resjunk targets in the targetlist are in the
1476 * ORDER BY list, adding the not-yet-sorted ones to the end of the list.
1477 * This is typically used to help implement SELECT DISTINCT.
1479 * See addTargetToSortList for info about pstate and resolveUnknown inputs.
1481 * Returns the updated ORDER BY list.
1484 addAllTargetsToSortList(ParseState *pstate, List *sortlist,
1485 List *targetlist, bool resolveUnknown)
1489 foreach(l, targetlist)
1491 TargetEntry *tle = (TargetEntry *) lfirst(l);
1493 if (!tle->resdom->resjunk)
1494 sortlist = addTargetToSortList(pstate, tle,
1495 sortlist, targetlist,
1503 * addTargetToSortList
1504 * If the given targetlist entry isn't already in the ORDER BY list,
1505 * add it to the end of the list, using the sortop with given name
1506 * or the default sort operator if opname == NIL.
1508 * If resolveUnknown is TRUE, convert TLEs of type UNKNOWN to TEXT. If not,
1509 * do nothing (which implies the search for a sort operator will fail).
1510 * pstate should be provided if resolveUnknown is TRUE, but can be NULL
1513 * Returns the updated ORDER BY list.
1516 addTargetToSortList(ParseState *pstate, TargetEntry *tle,
1517 List *sortlist, List *targetlist,
1518 int sortby_kind, List *sortby_opname,
1519 bool resolveUnknown)
1521 /* avoid making duplicate sortlist entries */
1522 if (!targetIsInSortList(tle, sortlist))
1524 SortClause *sortcl = makeNode(SortClause);
1525 Oid restype = tle->resdom->restype;
1527 /* if tlist item is an UNKNOWN literal, change it to TEXT */
1528 if (restype == UNKNOWNOID && resolveUnknown)
1530 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
1531 restype, TEXTOID, -1,
1533 COERCE_IMPLICIT_CAST);
1534 restype = tle->resdom->restype = TEXTOID;
1535 tle->resdom->restypmod = -1;
1538 sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
1540 switch (sortby_kind)
1543 sortcl->sortop = ordering_oper_opid(restype);
1546 sortcl->sortop = reverse_ordering_oper_opid(restype);
1549 Assert(sortby_opname != NIL);
1550 sortcl->sortop = compatible_oper_opid(sortby_opname,
1556 elog(ERROR, "unrecognized sortby_kind: %d", sortby_kind);
1560 sortlist = lappend(sortlist, sortcl);
1566 * assignSortGroupRef
1567 * Assign the targetentry an unused ressortgroupref, if it doesn't
1568 * already have one. Return the assigned or pre-existing refnumber.
1570 * 'tlist' is the targetlist containing (or to contain) the given targetentry.
1573 assignSortGroupRef(TargetEntry *tle, List *tlist)
1578 if (tle->resdom->ressortgroupref) /* already has one? */
1579 return tle->resdom->ressortgroupref;
1581 /* easiest way to pick an unused refnumber: max used + 1 */
1585 Index ref = ((TargetEntry *) lfirst(l))->resdom->ressortgroupref;
1590 tle->resdom->ressortgroupref = maxRef + 1;
1591 return tle->resdom->ressortgroupref;
1595 * targetIsInSortList
1596 * Is the given target item already in the sortlist?
1598 * Works for both SortClause and GroupClause lists. Note that the main
1599 * reason we need this routine (and not just a quick test for nonzeroness
1600 * of ressortgroupref) is that a TLE might be in only one of the lists.
1603 targetIsInSortList(TargetEntry *tle, List *sortList)
1605 Index ref = tle->resdom->ressortgroupref;
1608 /* no need to scan list if tle has no marker */
1612 foreach(l, sortList)
1614 SortClause *scl = (SortClause *) lfirst(l);
1616 if (scl->tleSortGroupRef == ref)