1 /*-------------------------------------------------------------------------
4 * handle clauses in parser
6 * Portions Copyright (c) 1996-2006, 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.160 2006/12/24 00:29:19 tgl Exp $
13 *-------------------------------------------------------------------------
18 #include "access/heapam.h"
19 #include "catalog/heap.h"
20 #include "catalog/pg_type.h"
21 #include "commands/defrem.h"
22 #include "nodes/makefuncs.h"
23 #include "optimizer/clauses.h"
24 #include "optimizer/tlist.h"
25 #include "optimizer/var.h"
26 #include "parser/analyze.h"
27 #include "parser/parsetree.h"
28 #include "parser/parse_clause.h"
29 #include "parser/parse_coerce.h"
30 #include "parser/parse_expr.h"
31 #include "parser/parse_oper.h"
32 #include "parser/parse_relation.h"
33 #include "parser/parse_target.h"
34 #include "rewrite/rewriteManip.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,
53 Relids containedRels);
54 static RangeTblEntry *transformTableEntry(ParseState *pstate, RangeVar *r);
55 static RangeTblEntry *transformRangeSubselect(ParseState *pstate,
57 static RangeTblEntry *transformRangeFunction(ParseState *pstate,
59 static Node *transformFromClauseItem(ParseState *pstate, Node *n,
60 RangeTblEntry **top_rte, int *top_rti,
62 Relids *containedRels);
63 static Node *buildMergedJoinVar(ParseState *pstate, JoinType jointype,
64 Var *l_colvar, Var *r_colvar);
65 static TargetEntry *findTargetlistEntry(ParseState *pstate, Node *node,
66 List **tlist, int clause);
70 * transformFromClause -
71 * Process the FROM clause and add items to the query's range table,
72 * joinlist, and namespaces.
74 * Note: we assume that pstate's p_rtable, p_joinlist, p_relnamespace, and
75 * p_varnamespace lists were initialized to NIL when the pstate was created.
76 * We will add onto any entries already present --- this is needed for rule
77 * processing, as well as for UPDATE and DELETE.
79 * The range table may grow still further when we transform the expressions
80 * in the query's quals and target list. (This is possible because in
81 * POSTQUEL, we allowed references to relations not specified in the
82 * from-clause. PostgreSQL keeps this extension to standard SQL.)
85 transformFromClause(ParseState *pstate, List *frmList)
90 * The grammar will have produced a list of RangeVars, RangeSubselects,
91 * RangeFunctions, and/or JoinExprs. Transform each one (possibly adding
92 * entries to the rtable), check for duplicate refnames, and then add it
93 * to the joinlist and namespaces.
101 Relids containedRels;
103 n = transformFromClauseItem(pstate, n,
108 checkNameSpaceConflicts(pstate, pstate->p_relnamespace, relnamespace);
109 pstate->p_joinlist = lappend(pstate->p_joinlist, n);
110 pstate->p_relnamespace = list_concat(pstate->p_relnamespace,
112 pstate->p_varnamespace = lappend(pstate->p_varnamespace, rte);
113 bms_free(containedRels);
119 * Add the target relation of INSERT/UPDATE/DELETE to the range table,
120 * and make the special links to it in the ParseState.
122 * We also open the target relation and acquire a write lock on it.
123 * This must be done before processing the FROM list, in case the target
124 * is also mentioned as a source relation --- we want to be sure to grab
125 * the write lock before any read lock.
127 * If alsoSource is true, add the target to the query's joinlist and
128 * namespace. For INSERT, we don't want the target to be joined to;
129 * it's a destination of tuples, not a source. For UPDATE/DELETE,
130 * we do need to scan or join the target. (NOTE: we do not bother
131 * to check for namespace conflict; we assume that the namespace was
132 * initially empty in these cases.)
134 * Finally, we mark the relation as requiring the permissions specified
137 * Returns the rangetable index of the target relation.
140 setTargetTable(ParseState *pstate, RangeVar *relation,
141 bool inh, bool alsoSource, AclMode requiredPerms)
146 /* Close old target; this could only happen for multi-action rules */
147 if (pstate->p_target_relation != NULL)
148 heap_close(pstate->p_target_relation, NoLock);
151 * Open target rel and grab suitable lock (which we will hold till end of
154 * analyze.c will eventually do the corresponding heap_close(), but *not*
157 pstate->p_target_relation = heap_openrv(relation, RowExclusiveLock);
162 rte = addRangeTableEntryForRelation(pstate, pstate->p_target_relation,
163 relation->alias, inh, false);
164 pstate->p_target_rangetblentry = rte;
166 /* assume new rte is at end */
167 rtindex = list_length(pstate->p_rtable);
168 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
171 * Override addRangeTableEntry's default ACL_SELECT permissions check, and
172 * instead mark target table as requiring exactly the specified
175 * If we find an explicit reference to the rel later during parse
176 * analysis, we will add the ACL_SELECT bit back again; see
177 * scanRTEForColumn (for simple field references), ExpandColumnRefStar
178 * (for foo.*) and ExpandAllTables (for *).
180 rte->requiredPerms = requiredPerms;
183 * If UPDATE/DELETE, add table to joinlist and namespaces.
186 addRTEtoQuery(pstate, rte, true, true, true);
192 * Simplify InhOption (yes/no/default) into boolean yes/no.
194 * The reason we do things this way is that we don't want to examine the
195 * SQL_inheritance option flag until parse_analyze is run. Otherwise,
196 * we'd do the wrong thing with query strings that intermix SET commands
200 interpretInhOption(InhOption inhOpt)
209 return SQL_inheritance;
211 elog(ERROR, "bogus InhOption value: %d", inhOpt);
212 return false; /* keep compiler quiet */
216 * Given a relation-options list (of DefElems), return true iff the specified
217 * table/result set should be created with OIDs. This needs to be done after
218 * parsing the query string because the return value can depend upon the
219 * default_with_oids GUC var.
222 interpretOidsOption(List *defList)
226 /* Scan list to see if OIDS was included */
227 foreach(cell, defList)
229 DefElem *def = (DefElem *) lfirst(cell);
231 if (pg_strcasecmp(def->defname, "oids") == 0)
232 return defGetBoolean(def);
235 /* OIDS option was not specified, so use default. */
236 return default_with_oids;
240 * Extract all not-in-common columns from column lists of a source table
243 extractRemainingColumns(List *common_colnames,
244 List *src_colnames, List *src_colvars,
245 List **res_colnames, List **res_colvars)
247 List *new_colnames = NIL;
248 List *new_colvars = NIL;
252 Assert(list_length(src_colnames) == list_length(src_colvars));
254 forboth(lnames, src_colnames, lvars, src_colvars)
256 char *colname = strVal(lfirst(lnames));
260 foreach(cnames, common_colnames)
262 char *ccolname = strVal(lfirst(cnames));
264 if (strcmp(colname, ccolname) == 0)
273 new_colnames = lappend(new_colnames, lfirst(lnames));
274 new_colvars = lappend(new_colvars, lfirst(lvars));
278 *res_colnames = new_colnames;
279 *res_colvars = new_colvars;
282 /* transformJoinUsingClause()
283 * Build a complete ON clause from a partially-transformed USING list.
284 * We are given lists of nodes representing left and right match columns.
285 * Result is a transformed qualification expression.
288 transformJoinUsingClause(ParseState *pstate, List *leftVars, List *rightVars)
295 * We cheat a little bit here by building an untransformed operator tree
296 * whose leaves are the already-transformed Vars. This is OK because
297 * transformExpr() won't complain about already-transformed subnodes.
299 forboth(lvars, leftVars, rvars, rightVars)
301 Node *lvar = (Node *) lfirst(lvars);
302 Node *rvar = (Node *) lfirst(rvars);
305 e = makeSimpleA_Expr(AEXPR_OP, "=",
306 copyObject(lvar), copyObject(rvar),
315 a = makeA_Expr(AEXPR_AND, NIL, result, (Node *) e, -1);
321 * Since the references are already Vars, and are certainly from the input
322 * relations, we don't have to go through the same pushups that
323 * transformJoinOnClause() does. Just invoke transformExpr() to fix up
324 * the operators, and we're done.
326 result = transformExpr(pstate, result);
328 result = coerce_to_boolean(pstate, result, "JOIN/USING");
333 /* transformJoinOnClause()
334 * Transform the qual conditions for JOIN/ON.
335 * Result is a transformed qualification expression.
338 transformJoinOnClause(ParseState *pstate, JoinExpr *j,
339 RangeTblEntry *l_rte,
340 RangeTblEntry *r_rte,
342 Relids containedRels)
345 List *save_relnamespace;
346 List *save_varnamespace;
347 Relids clause_varnos;
351 * This is a tad tricky, for two reasons. First, the namespace that the
352 * join expression should see is just the two subtrees of the JOIN plus
353 * any outer references from upper pstate levels. So, temporarily set
354 * this pstate's namespace accordingly. (We need not check for refname
355 * conflicts, because transformFromClauseItem() already did.) NOTE: this
356 * code is OK only because the ON clause can't legally alter the namespace
357 * by causing implicit relation refs to be added.
359 save_relnamespace = pstate->p_relnamespace;
360 save_varnamespace = pstate->p_varnamespace;
362 pstate->p_relnamespace = relnamespace;
363 pstate->p_varnamespace = list_make2(l_rte, r_rte);
365 result = transformWhereClause(pstate, j->quals, "JOIN/ON");
367 pstate->p_relnamespace = save_relnamespace;
368 pstate->p_varnamespace = save_varnamespace;
371 * Second, we need to check that the ON condition doesn't refer to any
372 * rels outside the input subtrees of the JOIN. It could do that despite
373 * our hack on the namespace if it uses fully-qualified names. So, grovel
374 * through the transformed clause and make sure there are no bogus
375 * references. (Outer references are OK, and are ignored here.)
377 clause_varnos = pull_varnos(result);
378 clause_varnos = bms_del_members(clause_varnos, containedRels);
379 if ((varno = bms_first_member(clause_varnos)) >= 0)
382 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
383 errmsg("JOIN/ON clause refers to \"%s\", which is not part of JOIN",
384 rt_fetch(varno, pstate->p_rtable)->eref->aliasname)));
386 bms_free(clause_varnos);
392 * transformTableEntry --- transform a RangeVar (simple relation reference)
394 static RangeTblEntry *
395 transformTableEntry(ParseState *pstate, RangeVar *r)
400 * mark this entry to indicate it comes from the FROM clause. In SQL, the
401 * target list can only refer to range variables specified in the from
402 * clause but we follow the more powerful POSTQUEL semantics and
403 * automatically generate the range variable if not specified. However
404 * there are times we need to know whether the entries are legitimate.
406 rte = addRangeTableEntry(pstate, r, r->alias,
407 interpretInhOption(r->inhOpt), true);
414 * transformRangeSubselect --- transform a sub-SELECT appearing in FROM
416 static RangeTblEntry *
417 transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
424 * We require user to supply an alias for a subselect, per SQL92. To relax
425 * this, we'd have to be prepared to gin up a unique alias for an
426 * unlabeled subselect.
428 if (r->alias == NULL)
430 (errcode(ERRCODE_SYNTAX_ERROR),
431 errmsg("subquery in FROM must have an alias")));
434 * Analyze and transform the subquery.
436 parsetrees = parse_sub_analyze(r->subquery, pstate);
439 * Check that we got something reasonable. Most of these conditions are
440 * probably impossible given restrictions of the grammar, but check 'em
443 if (list_length(parsetrees) != 1)
444 elog(ERROR, "unexpected parse analysis result for subquery in FROM");
445 query = (Query *) linitial(parsetrees);
446 if (query == NULL || !IsA(query, Query))
447 elog(ERROR, "unexpected parse analysis result for subquery in FROM");
449 if (query->commandType != CMD_SELECT)
450 elog(ERROR, "expected SELECT query from subquery in FROM");
451 if (query->into != NULL)
453 (errcode(ERRCODE_SYNTAX_ERROR),
454 errmsg("subquery in FROM may not have SELECT INTO")));
457 * The subquery cannot make use of any variables from FROM items created
458 * earlier in the current query. Per SQL92, the scope of a FROM item does
459 * not include other FROM items. Formerly we hacked the namespace so that
460 * the other variables weren't even visible, but it seems more useful to
461 * leave them visible and give a specific error message.
463 * XXX this will need further work to support SQL99's LATERAL() feature,
464 * wherein such references would indeed be legal.
466 * We can skip groveling through the subquery if there's not anything
467 * visible in the current query. Also note that outer references are OK.
469 if (pstate->p_relnamespace || pstate->p_varnamespace)
471 if (contain_vars_of_level((Node *) query, 1))
473 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
474 errmsg("subquery in FROM may not refer to other relations of same query level")));
478 * OK, build an RTE for the subquery.
480 rte = addRangeTableEntryForSubquery(pstate, query, r->alias, true);
487 * transformRangeFunction --- transform a function call appearing in FROM
489 static RangeTblEntry *
490 transformRangeFunction(ParseState *pstate, RangeFunction *r)
497 * Get function name for possible use as alias. We use the same
498 * transformation rules as for a SELECT output expression. For a FuncCall
499 * node, the result will be the function name, but it is possible for the
500 * grammar to hand back other node types.
502 funcname = FigureColname(r->funccallnode);
505 * Transform the raw expression.
507 funcexpr = transformExpr(pstate, r->funccallnode);
510 * The function parameters cannot make use of any variables from other
511 * FROM items. (Compare to transformRangeSubselect(); the coding is
512 * different though because we didn't parse as a sub-select with its own
513 * level of namespace.)
515 * XXX this will need further work to support SQL99's LATERAL() feature,
516 * wherein such references would indeed be legal.
518 if (pstate->p_relnamespace || pstate->p_varnamespace)
520 if (contain_vars_of_level(funcexpr, 0))
522 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
523 errmsg("function expression in FROM may not refer to other relations of same query level")));
527 * Disallow aggregate functions in the expression. (No reason to postpone
528 * this check until parseCheckAggregates.)
530 if (pstate->p_hasAggs)
532 if (checkExprHasAggs(funcexpr))
534 (errcode(ERRCODE_GROUPING_ERROR),
535 errmsg("cannot use aggregate function in function expression in FROM")));
539 * OK, build an RTE for the function.
541 rte = addRangeTableEntryForFunction(pstate, funcname, funcexpr,
545 * If a coldeflist was supplied, ensure it defines a legal set of names
546 * (no duplicates) and datatypes (no pseudo-types, for instance).
547 * addRangeTableEntryForFunction looked up the type names but didn't check
548 * them further than that.
554 tupdesc = BuildDescFromLists(rte->eref->colnames,
556 rte->funccoltypmods);
557 CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE);
565 * transformFromClauseItem -
566 * Transform a FROM-clause item, adding any required entries to the
567 * range table list being built in the ParseState, and return the
568 * transformed item ready to include in the joinlist and namespaces.
569 * This routine can recurse to handle SQL92 JOIN expressions.
571 * The function return value is the node to add to the jointree (a
572 * RangeTblRef or JoinExpr). Additional output parameters are:
574 * *top_rte: receives the RTE corresponding to the jointree item.
575 * (We could extract this from the function return node, but it saves cycles
576 * to pass it back separately.)
578 * *top_rti: receives the rangetable index of top_rte. (Ditto.)
580 * *relnamespace: receives a List of the RTEs exposed as relation names
583 * *containedRels: receives a bitmap set of the rangetable indexes
584 * of all the base and join relations represented in this jointree item.
585 * This is needed for checking JOIN/ON conditions in higher levels.
587 * We do not need to pass back an explicit varnamespace value, because
588 * in all cases the varnamespace contribution is exactly top_rte.
591 transformFromClauseItem(ParseState *pstate, Node *n,
592 RangeTblEntry **top_rte, int *top_rti,
594 Relids *containedRels)
596 if (IsA(n, RangeVar))
598 /* Plain relation reference */
603 rte = transformTableEntry(pstate, (RangeVar *) n);
604 /* assume new rte is at end */
605 rtindex = list_length(pstate->p_rtable);
606 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
609 *relnamespace = list_make1(rte);
610 *containedRels = bms_make_singleton(rtindex);
611 rtr = makeNode(RangeTblRef);
612 rtr->rtindex = rtindex;
615 else if (IsA(n, RangeSubselect))
617 /* sub-SELECT is like a plain relation */
622 rte = transformRangeSubselect(pstate, (RangeSubselect *) n);
623 /* assume new rte is at end */
624 rtindex = list_length(pstate->p_rtable);
625 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
628 *relnamespace = list_make1(rte);
629 *containedRels = bms_make_singleton(rtindex);
630 rtr = makeNode(RangeTblRef);
631 rtr->rtindex = rtindex;
634 else if (IsA(n, RangeFunction))
636 /* function is like a plain relation */
641 rte = transformRangeFunction(pstate, (RangeFunction *) n);
642 /* assume new rte is at end */
643 rtindex = list_length(pstate->p_rtable);
644 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
647 *relnamespace = list_make1(rte);
648 *containedRels = bms_make_singleton(rtindex);
649 rtr = makeNode(RangeTblRef);
650 rtr->rtindex = rtindex;
653 else if (IsA(n, JoinExpr))
655 /* A newfangled join expression */
656 JoinExpr *j = (JoinExpr *) n;
657 RangeTblEntry *l_rte;
658 RangeTblEntry *r_rte;
661 Relids l_containedRels,
664 List *l_relnamespace,
676 * Recursively process the left and right subtrees
678 j->larg = transformFromClauseItem(pstate, j->larg,
683 j->rarg = transformFromClauseItem(pstate, j->rarg,
690 * Check for conflicting refnames in left and right subtrees. Must do
691 * this because higher levels will assume I hand back a self-
692 * consistent namespace subtree.
694 checkNameSpaceConflicts(pstate, l_relnamespace, r_relnamespace);
697 * Generate combined relation membership info for possible use by
698 * transformJoinOnClause below.
700 my_relnamespace = list_concat(l_relnamespace, r_relnamespace);
701 my_containedRels = bms_join(l_containedRels, r_containedRels);
703 pfree(r_relnamespace); /* free unneeded list header */
706 * Extract column name and var lists from both subtrees
708 * Note: expandRTE returns new lists, safe for me to modify
710 expandRTE(l_rte, l_rtindex, 0, false,
711 &l_colnames, &l_colvars);
712 expandRTE(r_rte, r_rtindex, 0, false,
713 &r_colnames, &r_colvars);
716 * Natural join does not explicitly specify columns; must generate
717 * columns to join. Need to run through the list of columns from each
718 * table or join result and match up the column names. Use the first
719 * table, and check every column in the second table for a match.
720 * (We'll check that the matches were unique later on.) The result of
721 * this step is a list of column names just like an explicitly-written
730 Assert(j->using == NIL); /* shouldn't have USING() too */
732 foreach(lx, l_colnames)
734 char *l_colname = strVal(lfirst(lx));
735 Value *m_name = NULL;
737 foreach(rx, r_colnames)
739 char *r_colname = strVal(lfirst(rx));
741 if (strcmp(l_colname, r_colname) == 0)
743 m_name = makeString(l_colname);
748 /* matched a right column? then keep as join column... */
750 rlist = lappend(rlist, m_name);
757 * Now transform the join qualifications, if any.
765 * JOIN/USING (or NATURAL JOIN, as transformed above). Transform
766 * the list into an explicit ON-condition, and generate a list of
767 * merged result columns.
769 List *ucols = j->using;
770 List *l_usingvars = NIL;
771 List *r_usingvars = NIL;
774 Assert(j->quals == NULL); /* shouldn't have ON() too */
778 char *u_colname = strVal(lfirst(ucol));
786 /* Check for USING(foo,foo) */
787 foreach(col, res_colnames)
789 char *res_colname = strVal(lfirst(col));
791 if (strcmp(res_colname, u_colname) == 0)
793 (errcode(ERRCODE_DUPLICATE_COLUMN),
794 errmsg("column name \"%s\" appears more than once in USING clause",
798 /* Find it in left input */
800 foreach(col, l_colnames)
802 char *l_colname = strVal(lfirst(col));
804 if (strcmp(l_colname, u_colname) == 0)
808 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
809 errmsg("common column name \"%s\" appears more than once in left table",
817 (errcode(ERRCODE_UNDEFINED_COLUMN),
818 errmsg("column \"%s\" specified in USING clause does not exist in left table",
821 /* Find it in right input */
823 foreach(col, r_colnames)
825 char *r_colname = strVal(lfirst(col));
827 if (strcmp(r_colname, u_colname) == 0)
831 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
832 errmsg("common column name \"%s\" appears more than once in right table",
840 (errcode(ERRCODE_UNDEFINED_COLUMN),
841 errmsg("column \"%s\" specified in USING clause does not exist in right table",
844 l_colvar = list_nth(l_colvars, l_index);
845 l_usingvars = lappend(l_usingvars, l_colvar);
846 r_colvar = list_nth(r_colvars, r_index);
847 r_usingvars = lappend(r_usingvars, r_colvar);
849 res_colnames = lappend(res_colnames, lfirst(ucol));
850 res_colvars = lappend(res_colvars,
851 buildMergedJoinVar(pstate,
857 j->quals = transformJoinUsingClause(pstate,
863 /* User-written ON-condition; transform it */
864 j->quals = transformJoinOnClause(pstate, j,
871 /* CROSS JOIN: no quals */
874 /* Add remaining columns from each side to the output columns */
875 extractRemainingColumns(res_colnames,
876 l_colnames, l_colvars,
877 &l_colnames, &l_colvars);
878 extractRemainingColumns(res_colnames,
879 r_colnames, r_colvars,
880 &r_colnames, &r_colvars);
881 res_colnames = list_concat(res_colnames, l_colnames);
882 res_colvars = list_concat(res_colvars, l_colvars);
883 res_colnames = list_concat(res_colnames, r_colnames);
884 res_colvars = list_concat(res_colvars, r_colvars);
887 * Check alias (AS clause), if any.
891 if (j->alias->colnames != NIL)
893 if (list_length(j->alias->colnames) > list_length(res_colnames))
895 (errcode(ERRCODE_SYNTAX_ERROR),
896 errmsg("column alias list for \"%s\" has too many entries",
897 j->alias->aliasname)));
902 * Now build an RTE for the result of the join
904 rte = addRangeTableEntryForJoin(pstate,
911 /* assume new rte is at end */
912 j->rtindex = list_length(pstate->p_rtable);
913 Assert(rte == rt_fetch(j->rtindex, pstate->p_rtable));
916 *top_rti = j->rtindex;
919 * Prepare returned namespace list. If the JOIN has an alias then it
920 * hides the contained RTEs as far as the relnamespace goes;
921 * otherwise, put the contained RTEs and *not* the JOIN into
926 *relnamespace = list_make1(rte);
927 list_free(my_relnamespace);
930 *relnamespace = my_relnamespace;
933 * Include join RTE in returned containedRels set
935 *containedRels = bms_add_member(my_containedRels, j->rtindex);
940 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(n));
941 return NULL; /* can't get here, keep compiler quiet */
945 * buildMergedJoinVar -
946 * generate a suitable replacement expression for a merged join column
949 buildMergedJoinVar(ParseState *pstate, JoinType jointype,
950 Var *l_colvar, Var *r_colvar)
959 * Choose output type if input types are dissimilar.
961 outcoltype = l_colvar->vartype;
962 outcoltypmod = l_colvar->vartypmod;
963 if (outcoltype != r_colvar->vartype)
965 outcoltype = select_common_type(list_make2_oid(l_colvar->vartype,
968 outcoltypmod = -1; /* ie, unknown */
970 else if (outcoltypmod != r_colvar->vartypmod)
972 /* same type, but not same typmod */
973 outcoltypmod = -1; /* ie, unknown */
977 * Insert coercion functions if needed. Note that a difference in typmod
978 * can only happen if input has typmod but outcoltypmod is -1. In that
979 * case we insert a RelabelType to clearly mark that result's typmod is
980 * not same as input. We never need coerce_type_typmod.
982 if (l_colvar->vartype != outcoltype)
983 l_node = coerce_type(pstate, (Node *) l_colvar, l_colvar->vartype,
984 outcoltype, outcoltypmod,
985 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
986 else if (l_colvar->vartypmod != outcoltypmod)
987 l_node = (Node *) makeRelabelType((Expr *) l_colvar,
988 outcoltype, outcoltypmod,
989 COERCE_IMPLICIT_CAST);
991 l_node = (Node *) l_colvar;
993 if (r_colvar->vartype != outcoltype)
994 r_node = coerce_type(pstate, (Node *) r_colvar, r_colvar->vartype,
995 outcoltype, outcoltypmod,
996 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
997 else if (r_colvar->vartypmod != outcoltypmod)
998 r_node = (Node *) makeRelabelType((Expr *) r_colvar,
999 outcoltype, outcoltypmod,
1000 COERCE_IMPLICIT_CAST);
1002 r_node = (Node *) r_colvar;
1005 * Choose what to emit
1012 * We can use either var; prefer non-coerced one if available.
1014 if (IsA(l_node, Var))
1016 else if (IsA(r_node, Var))
1022 /* Always use left var */
1026 /* Always use right var */
1032 * Here we must build a COALESCE expression to ensure that the
1033 * join output is non-null if either input is.
1035 CoalesceExpr *c = makeNode(CoalesceExpr);
1037 c->coalescetype = outcoltype;
1038 c->args = list_make2(l_node, r_node);
1039 res_node = (Node *) c;
1043 elog(ERROR, "unrecognized join type: %d", (int) jointype);
1044 res_node = NULL; /* keep compiler quiet */
1053 * transformWhereClause -
1054 * Transform the qualification and make sure it is of type boolean.
1055 * Used for WHERE and allied clauses.
1057 * constructName does not affect the semantics, but is used in error messages
1060 transformWhereClause(ParseState *pstate, Node *clause,
1061 const char *constructName)
1068 qual = transformExpr(pstate, clause);
1070 qual = coerce_to_boolean(pstate, qual, constructName);
1077 * transformLimitClause -
1078 * Transform the expression and make sure it is of type bigint.
1079 * Used for LIMIT and allied clauses.
1081 * Note: as of Postgres 8.2, LIMIT expressions are expected to yield int8,
1082 * rather than int4 as before.
1084 * constructName does not affect the semantics, but is used in error messages
1087 transformLimitClause(ParseState *pstate, Node *clause,
1088 const char *constructName)
1095 qual = transformExpr(pstate, clause);
1097 qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
1100 * LIMIT can't refer to any vars or aggregates of the current query; we
1101 * don't allow subselects either (though that case would at least be
1104 if (contain_vars_of_level(qual, 0))
1107 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1108 /* translator: %s is name of a SQL construct, eg LIMIT */
1109 errmsg("argument of %s must not contain variables",
1112 if (checkExprHasAggs(qual))
1115 (errcode(ERRCODE_GROUPING_ERROR),
1116 /* translator: %s is name of a SQL construct, eg LIMIT */
1117 errmsg("argument of %s must not contain aggregates",
1120 if (contain_subplans(qual))
1123 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1124 /* translator: %s is name of a SQL construct, eg LIMIT */
1125 errmsg("argument of %s must not contain subqueries",
1134 * findTargetlistEntry -
1135 * Returns the targetlist entry matching the given (untransformed) node.
1136 * If no matching entry exists, one is created and appended to the target
1137 * list as a "resjunk" node.
1139 * node the ORDER BY, GROUP BY, or DISTINCT ON expression to be matched
1140 * tlist the target list (passed by reference so we can append to it)
1141 * clause identifies clause type being processed
1143 static TargetEntry *
1144 findTargetlistEntry(ParseState *pstate, Node *node, List **tlist, int clause)
1146 TargetEntry *target_result = NULL;
1151 * Handle two special cases as mandated by the SQL92 spec:
1153 * 1. Bare ColumnName (no qualifier or subscripts)
1154 * For a bare identifier, we search for a matching column name
1155 * in the existing target list. Multiple matches are an error
1156 * unless they refer to identical values; for example,
1157 * we allow SELECT a, a FROM table ORDER BY a
1158 * but not SELECT a AS b, b FROM table ORDER BY b
1159 * If no match is found, we fall through and treat the identifier
1161 * For GROUP BY, it is incorrect to match the grouping item against
1162 * targetlist entries: according to SQL92, an identifier in GROUP BY
1163 * is a reference to a column name exposed by FROM, not to a target
1164 * list column. However, many implementations (including pre-7.0
1165 * PostgreSQL) accept this anyway. So for GROUP BY, we look first
1166 * to see if the identifier matches any FROM column name, and only
1167 * try for a targetlist name if it doesn't. This ensures that we
1168 * adhere to the spec in the case where the name could be both.
1169 * DISTINCT ON isn't in the standard, so we can do what we like there;
1170 * we choose to make it work like ORDER BY, on the rather flimsy
1171 * grounds that ordinary DISTINCT works on targetlist entries.
1173 * 2. IntegerConstant
1174 * This means to use the n'th item in the existing target list.
1175 * Note that it would make no sense to order/group/distinct by an
1176 * actual constant, so this does not create a conflict with our
1177 * extension to order/group by an expression.
1178 * GROUP BY column-number is not allowed by SQL92, but since
1179 * the standard has no other behavior defined for this syntax,
1180 * we may as well accept this common extension.
1182 * Note that pre-existing resjunk targets must not be used in either case,
1183 * since the user didn't write them in his SELECT list.
1185 * If neither special case applies, fall through to treat the item as
1189 if (IsA(node, ColumnRef) &&
1190 list_length(((ColumnRef *) node)->fields) == 1)
1192 char *name = strVal(linitial(((ColumnRef *) node)->fields));
1193 int location = ((ColumnRef *) node)->location;
1195 if (clause == GROUP_CLAUSE)
1198 * In GROUP BY, we must prefer a match against a FROM-clause
1199 * column to one against the targetlist. Look to see if there is
1200 * a matching column. If so, fall through to let transformExpr()
1201 * do the rest. NOTE: if name could refer ambiguously to more
1202 * than one column name exposed by FROM, colNameToVar will
1203 * ereport(ERROR). That's just what we want here.
1205 * Small tweak for 7.4.3: ignore matches in upper query levels.
1206 * This effectively changes the search order for bare names to (1)
1207 * local FROM variables, (2) local targetlist aliases, (3) outer
1208 * FROM variables, whereas before it was (1) (3) (2). SQL92 and
1209 * SQL99 do not allow GROUPing BY an outer reference, so this
1210 * breaks no cases that are legal per spec, and it seems a more
1211 * self-consistent behavior.
1213 if (colNameToVar(pstate, name, true, location) != NULL)
1221 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1223 if (!tle->resjunk &&
1224 strcmp(tle->resname, name) == 0)
1226 if (target_result != NULL)
1228 if (!equal(target_result->expr, tle->expr))
1230 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1233 translator: first %s is name of a SQL construct, eg ORDER BY */
1234 errmsg("%s \"%s\" is ambiguous",
1235 clauseText[clause], name),
1236 parser_errposition(pstate, location)));
1239 target_result = tle;
1240 /* Stay in loop to check for ambiguity */
1243 if (target_result != NULL)
1244 return target_result; /* return the first match */
1247 if (IsA(node, A_Const))
1249 Value *val = &((A_Const *) node)->val;
1250 int targetlist_pos = 0;
1253 if (!IsA(val, Integer))
1255 (errcode(ERRCODE_SYNTAX_ERROR),
1256 /* translator: %s is name of a SQL construct, eg ORDER BY */
1257 errmsg("non-integer constant in %s",
1258 clauseText[clause])));
1259 target_pos = intVal(val);
1262 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1266 if (++targetlist_pos == target_pos)
1267 return tle; /* return the unique match */
1271 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1272 /* translator: %s is name of a SQL construct, eg ORDER BY */
1273 errmsg("%s position %d is not in select list",
1274 clauseText[clause], target_pos)));
1278 * Otherwise, we have an expression (this is a Postgres extension not
1279 * found in SQL92). Convert the untransformed node to a transformed
1280 * expression, and search for a match in the tlist. NOTE: it doesn't
1281 * really matter whether there is more than one match. Also, we are
1282 * willing to match a resjunk target here, though the above cases must
1283 * ignore resjunk targets.
1285 expr = transformExpr(pstate, node);
1289 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1291 if (equal(expr, tle->expr))
1296 * If no matches, construct a new target entry which is appended to the
1297 * end of the target list. This target is given resjunk = TRUE so that it
1298 * will not be projected into the final tuple.
1300 target_result = transformTargetEntry(pstate, node, expr, NULL, true);
1302 *tlist = lappend(*tlist, target_result);
1304 return target_result;
1307 static GroupClause *
1308 make_group_clause(TargetEntry *tle, List *targetlist, Oid sortop)
1310 GroupClause *result;
1312 result = makeNode(GroupClause);
1313 result->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
1314 result->sortop = sortop;
1319 * transformGroupClause -
1320 * transform a GROUP BY clause
1322 * GROUP BY items will be added to the targetlist (as resjunk columns)
1323 * if not already present, so the targetlist must be passed by reference.
1325 * The order of the elements of the grouping clause does not affect
1326 * the semantics of the query. However, the optimizer is not currently
1327 * smart enough to reorder the grouping clause, so we try to do some
1328 * primitive reordering here.
1331 transformGroupClause(ParseState *pstate, List *grouplist,
1332 List **targetlist, List *sortClause)
1335 List *tle_list = NIL;
1338 /* Preprocess the grouping clause, lookup TLEs */
1339 foreach(l, grouplist)
1344 tle = findTargetlistEntry(pstate, lfirst(l),
1345 targetlist, GROUP_CLAUSE);
1347 /* if tlist item is an UNKNOWN literal, change it to TEXT */
1348 restype = exprType((Node *) tle->expr);
1350 if (restype == UNKNOWNOID)
1351 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
1352 restype, TEXTOID, -1,
1354 COERCE_IMPLICIT_CAST);
1356 tle_list = lappend(tle_list, tle);
1360 * Now iterate through the ORDER BY clause. If we find a grouping element
1361 * that matches the ORDER BY element, append the grouping element to the
1362 * result set immediately. Otherwise, stop iterating. The effect of this
1363 * is to look for a prefix of the ORDER BY list in the grouping clauses,
1364 * and to move that prefix to the front of the GROUP BY.
1366 foreach(l, sortClause)
1368 SortClause *sc = (SortClause *) lfirst(l);
1369 ListCell *prev = NULL;
1373 foreach(tl, tle_list)
1375 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1377 if (sc->tleSortGroupRef == tle->ressortgroupref)
1381 tle_list = list_delete_cell(tle_list, tl, prev);
1383 /* Use the sort clause's sorting operator */
1384 gc = make_group_clause(tle, *targetlist, sc->sortop);
1385 result = lappend(result, gc);
1393 /* As soon as we've failed to match an ORDER BY element, stop */
1399 * Now add any remaining elements of the GROUP BY list in the order we
1402 * XXX: are there any additional criteria to consider when ordering
1405 foreach(l, tle_list)
1407 TargetEntry *tle = (TargetEntry *) lfirst(l);
1411 /* avoid making duplicate grouplist entries */
1412 if (targetIsInSortList(tle, result))
1415 sort_op = ordering_oper_opid(exprType((Node *) tle->expr));
1416 gc = make_group_clause(tle, *targetlist, sort_op);
1417 result = lappend(result, gc);
1420 list_free(tle_list);
1425 * transformSortClause -
1426 * transform an ORDER BY clause
1428 * ORDER BY items will be added to the targetlist (as resjunk columns)
1429 * if not already present, so the targetlist must be passed by reference.
1432 transformSortClause(ParseState *pstate,
1435 bool resolveUnknown)
1437 List *sortlist = NIL;
1440 foreach(olitem, orderlist)
1442 SortBy *sortby = lfirst(olitem);
1445 tle = findTargetlistEntry(pstate, sortby->node,
1446 targetlist, ORDER_CLAUSE);
1448 sortlist = addTargetToSortList(pstate, tle,
1449 sortlist, *targetlist,
1450 sortby->sortby_kind,
1459 * transformDistinctClause -
1460 * transform a DISTINCT or DISTINCT ON clause
1462 * Since we may need to add items to the query's sortClause list, that list
1463 * is passed by reference. Likewise for the targetlist.
1466 transformDistinctClause(ParseState *pstate, List *distinctlist,
1467 List **targetlist, List **sortClause)
1473 /* No work if there was no DISTINCT clause */
1474 if (distinctlist == NIL)
1477 if (linitial(distinctlist) == NULL)
1479 /* We had SELECT DISTINCT */
1482 * All non-resjunk elements from target list that are not already in
1483 * the sort list should be added to it. (We don't really care what
1484 * order the DISTINCT fields are checked in, so we can leave the
1485 * user's ORDER BY spec alone, and just add additional sort keys to it
1486 * to ensure that all targetlist items get sorted.)
1488 *sortClause = addAllTargetsToSortList(pstate,
1494 * Now, DISTINCT list consists of all non-resjunk sortlist items.
1495 * Actually, all the sortlist items had better be non-resjunk!
1496 * Otherwise, user wrote SELECT DISTINCT with an ORDER BY item that
1497 * does not appear anywhere in the SELECT targetlist, and we can't
1498 * implement that with only one sorting pass...
1500 foreach(slitem, *sortClause)
1502 SortClause *scl = (SortClause *) lfirst(slitem);
1503 TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
1507 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1508 errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list")));
1510 result = lappend(result, copyObject(scl));
1515 /* We had SELECT DISTINCT ON (expr, ...) */
1518 * If the user writes both DISTINCT ON and ORDER BY, then the two
1519 * expression lists must match (until one or the other runs out).
1520 * Otherwise the ORDER BY requires a different sort order than the
1521 * DISTINCT does, and we can't implement that with only one sort pass
1522 * (and if we do two passes, the results will be rather
1523 * unpredictable). However, it's OK to have more DISTINCT ON
1524 * expressions than ORDER BY expressions; we can just add the extra
1525 * DISTINCT values to the sort list, much as we did above for ordinary
1528 * Actually, it'd be OK for the common prefixes of the two lists to
1529 * match in any order, but implementing that check seems like more
1530 * trouble than it's worth.
1532 ListCell *nextsortlist = list_head(*sortClause);
1534 foreach(dlitem, distinctlist)
1538 tle = findTargetlistEntry(pstate, lfirst(dlitem),
1539 targetlist, DISTINCT_ON_CLAUSE);
1541 if (nextsortlist != NULL)
1543 SortClause *scl = (SortClause *) lfirst(nextsortlist);
1545 if (tle->ressortgroupref != scl->tleSortGroupRef)
1547 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1548 errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions")));
1549 result = lappend(result, copyObject(scl));
1550 nextsortlist = lnext(nextsortlist);
1554 *sortClause = addTargetToSortList(pstate, tle,
1555 *sortClause, *targetlist,
1556 SORTBY_ASC, NIL, true);
1559 * Probably, the tle should always have been added at the end
1560 * of the sort list ... but search to be safe.
1562 foreach(slitem, *sortClause)
1564 SortClause *scl = (SortClause *) lfirst(slitem);
1566 if (tle->ressortgroupref == scl->tleSortGroupRef)
1568 result = lappend(result, copyObject(scl));
1572 if (slitem == NULL) /* should not happen */
1573 elog(ERROR, "failed to add DISTINCT ON clause to target list");
1582 * addAllTargetsToSortList
1583 * Make sure all non-resjunk targets in the targetlist are in the
1584 * ORDER BY list, adding the not-yet-sorted ones to the end of the list.
1585 * This is typically used to help implement SELECT DISTINCT.
1587 * See addTargetToSortList for info about pstate and resolveUnknown inputs.
1589 * Returns the updated ORDER BY list.
1592 addAllTargetsToSortList(ParseState *pstate, List *sortlist,
1593 List *targetlist, bool resolveUnknown)
1597 foreach(l, targetlist)
1599 TargetEntry *tle = (TargetEntry *) lfirst(l);
1602 sortlist = addTargetToSortList(pstate, tle,
1603 sortlist, targetlist,
1611 * addTargetToSortList
1612 * If the given targetlist entry isn't already in the ORDER BY list,
1613 * add it to the end of the list, using the sortop with given name
1614 * or the default sort operator if opname == NIL.
1616 * If resolveUnknown is TRUE, convert TLEs of type UNKNOWN to TEXT. If not,
1617 * do nothing (which implies the search for a sort operator will fail).
1618 * pstate should be provided if resolveUnknown is TRUE, but can be NULL
1621 * Returns the updated ORDER BY list.
1624 addTargetToSortList(ParseState *pstate, TargetEntry *tle,
1625 List *sortlist, List *targetlist,
1626 int sortby_kind, List *sortby_opname,
1627 bool resolveUnknown)
1629 /* avoid making duplicate sortlist entries */
1630 if (!targetIsInSortList(tle, sortlist))
1632 SortClause *sortcl = makeNode(SortClause);
1633 Oid restype = exprType((Node *) tle->expr);
1635 /* if tlist item is an UNKNOWN literal, change it to TEXT */
1636 if (restype == UNKNOWNOID && resolveUnknown)
1638 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
1639 restype, TEXTOID, -1,
1641 COERCE_IMPLICIT_CAST);
1645 sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
1647 switch (sortby_kind)
1650 sortcl->sortop = ordering_oper_opid(restype);
1653 sortcl->sortop = reverse_ordering_oper_opid(restype);
1656 Assert(sortby_opname != NIL);
1657 sortcl->sortop = compatible_oper_opid(sortby_opname,
1663 elog(ERROR, "unrecognized sortby_kind: %d", sortby_kind);
1667 sortlist = lappend(sortlist, sortcl);
1673 * assignSortGroupRef
1674 * Assign the targetentry an unused ressortgroupref, if it doesn't
1675 * already have one. Return the assigned or pre-existing refnumber.
1677 * 'tlist' is the targetlist containing (or to contain) the given targetentry.
1680 assignSortGroupRef(TargetEntry *tle, List *tlist)
1685 if (tle->ressortgroupref) /* already has one? */
1686 return tle->ressortgroupref;
1688 /* easiest way to pick an unused refnumber: max used + 1 */
1692 Index ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
1697 tle->ressortgroupref = maxRef + 1;
1698 return tle->ressortgroupref;
1702 * targetIsInSortList
1703 * Is the given target item already in the sortlist?
1705 * Works for both SortClause and GroupClause lists. Note that the main
1706 * reason we need this routine (and not just a quick test for nonzeroness
1707 * of ressortgroupref) is that a TLE might be in only one of the lists.
1710 targetIsInSortList(TargetEntry *tle, List *sortList)
1712 Index ref = tle->ressortgroupref;
1715 /* no need to scan list if tle has no marker */
1719 foreach(l, sortList)
1721 SortClause *scl = (SortClause *) lfirst(l);
1723 if (scl->tleSortGroupRef == ref)