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 * $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.123 2003/09/25 06:58:01 petere 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 * Returns the rangetable index of the target relation.
122 setTargetTable(ParseState *pstate, RangeVar *relation,
123 bool inh, bool alsoSource)
128 /* Close old target; this could only happen for multi-action rules */
129 if (pstate->p_target_relation != NULL)
130 heap_close(pstate->p_target_relation, NoLock);
133 * Open target rel and grab suitable lock (which we will hold till end
136 * analyze.c will eventually do the corresponding heap_close(), but *not*
139 pstate->p_target_relation = heap_openrv(relation, RowExclusiveLock);
144 rte = addRangeTableEntry(pstate, relation, NULL, inh, false);
145 pstate->p_target_rangetblentry = rte;
147 /* assume new rte is at end */
148 rtindex = length(pstate->p_rtable);
149 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
152 * Override addRangeTableEntry's default checkForRead, and instead
153 * mark target table as requiring write access.
155 * If we find an explicit reference to the rel later during parse
156 * analysis, scanRTEForColumn will change checkForRead to 'true'
157 * again. That can't happen for INSERT but it is possible for UPDATE
160 rte->checkForRead = false;
161 rte->checkForWrite = true;
164 * If UPDATE/DELETE, add table to joinlist and namespace.
167 addRTEtoQuery(pstate, rte, true, true);
173 * Simplify InhOption (yes/no/default) into boolean yes/no.
175 * The reason we do things this way is that we don't want to examine the
176 * SQL_inheritance option flag until parse_analyze is run. Otherwise,
177 * we'd do the wrong thing with query strings that intermix SET commands
181 interpretInhOption(InhOption inhOpt)
190 return SQL_inheritance;
192 elog(ERROR, "bogus InhOption value");
193 return false; /* keep compiler quiet */
197 * Extract all not-in-common columns from column lists of a source table
200 extractRemainingColumns(List *common_colnames,
201 List *src_colnames, List *src_colvars,
202 List **res_colnames, List **res_colvars)
204 List *new_colnames = NIL;
205 List *new_colvars = NIL;
207 *lvars = src_colvars;
209 foreach(lnames, src_colnames)
211 char *colname = strVal(lfirst(lnames));
215 foreach(cnames, common_colnames)
217 char *ccolname = strVal(lfirst(cnames));
219 if (strcmp(colname, ccolname) == 0)
228 new_colnames = lappend(new_colnames, lfirst(lnames));
229 new_colvars = lappend(new_colvars, lfirst(lvars));
232 lvars = lnext(lvars);
235 *res_colnames = new_colnames;
236 *res_colvars = new_colvars;
239 /* transformJoinUsingClause()
240 * Build a complete ON clause from a partially-transformed USING list.
241 * We are given lists of nodes representing left and right match columns.
242 * Result is a transformed qualification expression.
245 transformJoinUsingClause(ParseState *pstate, List *leftVars, List *rightVars)
252 * We cheat a little bit here by building an untransformed operator
253 * tree whose leaves are the already-transformed Vars. This is OK
254 * because transformExpr() won't complain about already-transformed
257 foreach(lvars, leftVars)
259 Node *lvar = (Node *) lfirst(lvars);
260 Node *rvar = (Node *) lfirst(rvars);
263 e = makeSimpleA_Expr(AEXPR_OP, "=", copyObject(lvar), copyObject(rvar));
271 a = makeA_Expr(AEXPR_AND, NIL, result, (Node *) e);
275 rvars = lnext(rvars);
279 * Since the references are already Vars, and are certainly from the
280 * input relations, we don't have to go through the same pushups that
281 * transformJoinOnClause() does. Just invoke transformExpr() to fix
282 * up the operators, and we're done.
284 result = transformExpr(pstate, result);
286 result = coerce_to_boolean(pstate, result, "JOIN/USING");
289 } /* transformJoinUsingClause() */
291 /* transformJoinOnClause()
292 * Transform the qual conditions for JOIN/ON.
293 * Result is a transformed qualification expression.
296 transformJoinOnClause(ParseState *pstate, JoinExpr *j,
300 List *save_namespace;
301 Relids clause_varnos;
305 * This is a tad tricky, for two reasons. First, the namespace that
306 * the join expression should see is just the two subtrees of the JOIN
307 * plus any outer references from upper pstate levels. So,
308 * temporarily set this pstate's namespace accordingly. (We need not
309 * check for refname conflicts, because transformFromClauseItem()
310 * already did.) NOTE: this code is OK only because the ON clause
311 * can't legally alter the namespace by causing implicit relation refs
314 save_namespace = pstate->p_namespace;
315 pstate->p_namespace = makeList2(j->larg, j->rarg);
317 result = transformWhereClause(pstate, j->quals, "JOIN/ON");
319 pstate->p_namespace = save_namespace;
322 * Second, we need to check that the ON condition doesn't refer to any
323 * rels outside the input subtrees of the JOIN. It could do that
324 * despite our hack on the namespace if it uses fully-qualified names.
325 * So, grovel through the transformed clause and make sure there are
326 * no bogus references. (Outer references are OK, and are ignored
329 clause_varnos = pull_varnos(result);
330 while ((varno = bms_first_member(clause_varnos)) >= 0)
332 if (!intMember(varno, containedRels))
335 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
336 errmsg("JOIN/ON clause refers to \"%s\", which is not part of JOIN",
337 rt_fetch(varno, pstate->p_rtable)->eref->aliasname)));
340 bms_free(clause_varnos);
346 * transformTableEntry --- transform a RangeVar (simple relation reference)
349 transformTableEntry(ParseState *pstate, RangeVar *r)
355 * mark this entry to indicate it comes from the FROM clause. In SQL,
356 * the target list can only refer to range variables specified in the
357 * from clause but we follow the more powerful POSTQUEL semantics and
358 * automatically generate the range variable if not specified. However
359 * there are times we need to know whether the entries are legitimate.
361 rte = addRangeTableEntry(pstate, r, r->alias,
362 interpretInhOption(r->inhOpt), true);
365 * We create a RangeTblRef, but we do not add it to the joinlist or
366 * namespace; our caller must do that if appropriate.
368 rtr = makeNode(RangeTblRef);
369 /* assume new rte is at end */
370 rtr->rtindex = length(pstate->p_rtable);
371 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
378 * transformRangeSubselect --- transform a sub-SELECT appearing in FROM
381 transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
389 * We require user to supply an alias for a subselect, per SQL92. To
390 * relax this, we'd have to be prepared to gin up a unique alias for
391 * an unlabeled subselect.
393 if (r->alias == NULL)
395 (errcode(ERRCODE_SYNTAX_ERROR),
396 errmsg("subquery in FROM must have an alias")));
399 * Analyze and transform the subquery.
401 parsetrees = parse_sub_analyze(r->subquery, pstate);
404 * Check that we got something reasonable. Most of these conditions
405 * are probably impossible given restrictions of the grammar, but
408 if (length(parsetrees) != 1)
409 elog(ERROR, "unexpected parse analysis result for subquery in FROM");
410 query = (Query *) lfirst(parsetrees);
411 if (query == NULL || !IsA(query, Query))
412 elog(ERROR, "unexpected parse analysis result for subquery in FROM");
414 if (query->commandType != CMD_SELECT)
415 elog(ERROR, "expected SELECT query from subquery in FROM");
416 if (query->resultRelation != 0 || query->into != NULL)
418 (errcode(ERRCODE_SYNTAX_ERROR),
419 errmsg("subquery in FROM may not have SELECT INTO")));
422 * The subquery cannot make use of any variables from FROM items
423 * created earlier in the current query. Per SQL92, the scope of a
424 * FROM item does not include other FROM items. Formerly we hacked
425 * the namespace so that the other variables weren't even visible, but
426 * it seems more useful to leave them visible and give a specific
429 * XXX this will need further work to support SQL99's LATERAL() feature,
430 * wherein such references would indeed be legal.
432 * We can skip groveling through the subquery if there's not anything
433 * visible in the current query. Also note that outer references are
436 if (pstate->p_namespace)
438 if (contain_vars_of_level((Node *) query, 1))
440 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
441 errmsg("subquery in FROM may not refer to other relations of same query level")));
445 * OK, build an RTE for the subquery.
447 rte = addRangeTableEntryForSubquery(pstate, query, r->alias, true);
450 * We create a RangeTblRef, but we do not add it to the joinlist or
451 * namespace; our caller must do that if appropriate.
453 rtr = makeNode(RangeTblRef);
454 /* assume new rte is at end */
455 rtr->rtindex = length(pstate->p_rtable);
456 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
463 * transformRangeFunction --- transform a function call appearing in FROM
466 transformRangeFunction(ParseState *pstate, RangeFunction *r)
473 /* Get function name for possible use as alias */
474 Assert(IsA(r->funccallnode, FuncCall));
475 funcname = strVal(llast(((FuncCall *) r->funccallnode)->funcname));
478 * Transform the raw FuncCall node.
480 funcexpr = transformExpr(pstate, r->funccallnode);
483 * The function parameters cannot make use of any variables from other
484 * FROM items. (Compare to transformRangeSubselect(); the coding is
485 * different though because we didn't parse as a sub-select with its
486 * own level of namespace.)
488 * XXX this will need further work to support SQL99's LATERAL() feature,
489 * wherein such references would indeed be legal.
491 if (pstate->p_namespace)
493 if (contain_vars_of_level(funcexpr, 0))
495 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
496 errmsg("function expression in FROM may not refer to other relations of same query level")));
500 * Disallow aggregate functions in the expression. (No reason to
501 * postpone this check until parseCheckAggregates.)
503 if (pstate->p_hasAggs)
505 if (checkExprHasAggs(funcexpr))
507 (errcode(ERRCODE_GROUPING_ERROR),
508 errmsg("cannot use aggregate function in function expression in FROM")));
512 * If a coldeflist is supplied, ensure it defines a legal set of names
513 * (no duplicates) and datatypes (no pseudo-types, for instance).
519 tupdesc = BuildDescForRelation(r->coldeflist);
520 CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE);
524 * OK, build an RTE for the function.
526 rte = addRangeTableEntryForFunction(pstate, funcname, funcexpr,
530 * We create a RangeTblRef, but we do not add it to the joinlist or
531 * namespace; our caller must do that if appropriate.
533 rtr = makeNode(RangeTblRef);
534 /* assume new rte is at end */
535 rtr->rtindex = length(pstate->p_rtable);
536 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
543 * transformFromClauseItem -
544 * Transform a FROM-clause item, adding any required entries to the
545 * range table list being built in the ParseState, and return the
546 * transformed item ready to include in the joinlist and namespace.
547 * This routine can recurse to handle SQL92 JOIN expressions.
549 * Aside from the primary return value (the transformed joinlist item)
550 * this routine also returns an integer list of the rangetable indexes
551 * of all the base and join relations represented in the joinlist item.
552 * This list is needed for checking JOIN/ON conditions in higher levels.
555 transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels)
557 if (IsA(n, RangeVar))
559 /* Plain relation reference */
562 rtr = transformTableEntry(pstate, (RangeVar *) n);
563 *containedRels = makeListi1(rtr->rtindex);
566 else if (IsA(n, RangeSubselect))
568 /* sub-SELECT is like a plain relation */
571 rtr = transformRangeSubselect(pstate, (RangeSubselect *) n);
572 *containedRels = makeListi1(rtr->rtindex);
575 else if (IsA(n, RangeFunction))
577 /* function is like a plain relation */
580 rtr = transformRangeFunction(pstate, (RangeFunction *) n);
581 *containedRels = makeListi1(rtr->rtindex);
584 else if (IsA(n, JoinExpr))
586 /* A newfangled join expression */
587 JoinExpr *j = (JoinExpr *) n;
588 List *my_containedRels,
602 * Recursively process the left and right subtrees
604 j->larg = transformFromClauseItem(pstate, j->larg, &l_containedRels);
605 j->rarg = transformFromClauseItem(pstate, j->rarg, &r_containedRels);
608 * Generate combined list of relation indexes for possible use by
609 * transformJoinOnClause below.
611 my_containedRels = nconc(l_containedRels, r_containedRels);
614 * Check for conflicting refnames in left and right subtrees. Must
615 * do this because higher levels will assume I hand back a self-
616 * consistent namespace subtree.
618 checkNameSpaceConflicts(pstate, j->larg, j->rarg);
621 * Extract column name and var lists from both subtrees
623 * Note: expandRTE returns new lists, safe for me to modify
625 if (IsA(j->larg, RangeTblRef))
626 leftrti = ((RangeTblRef *) j->larg)->rtindex;
627 else if (IsA(j->larg, JoinExpr))
628 leftrti = ((JoinExpr *) j->larg)->rtindex;
631 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(j->larg));
632 leftrti = 0; /* keep compiler quiet */
634 rte = rt_fetch(leftrti, pstate->p_rtable);
635 expandRTE(pstate, rte, &l_colnames, &l_colvars);
637 if (IsA(j->rarg, RangeTblRef))
638 rightrti = ((RangeTblRef *) j->rarg)->rtindex;
639 else if (IsA(j->rarg, JoinExpr))
640 rightrti = ((JoinExpr *) j->rarg)->rtindex;
643 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(j->rarg));
644 rightrti = 0; /* keep compiler quiet */
646 rte = rt_fetch(rightrti, pstate->p_rtable);
647 expandRTE(pstate, rte, &r_colnames, &r_colvars);
650 * Natural join does not explicitly specify columns; must generate
651 * columns to join. Need to run through the list of columns from
652 * each table or join result and match up the column names. Use
653 * the first table, and check every column in the second table for
654 * a match. (We'll check that the matches were unique later on.)
655 * The result of this step is a list of column names just like an
656 * explicitly-written USING list.
664 Assert(j->using == NIL); /* shouldn't have USING() too */
666 foreach(lx, l_colnames)
668 char *l_colname = strVal(lfirst(lx));
669 Value *m_name = NULL;
671 foreach(rx, r_colnames)
673 char *r_colname = strVal(lfirst(rx));
675 if (strcmp(l_colname, r_colname) == 0)
677 m_name = makeString(l_colname);
682 /* matched a right column? then keep as join column... */
684 rlist = lappend(rlist, m_name);
691 * Now transform the join qualifications, if any.
699 * JOIN/USING (or NATURAL JOIN, as transformed above).
700 * Transform the list into an explicit ON-condition, and
701 * generate a list of merged result columns.
703 List *ucols = j->using;
704 List *l_usingvars = NIL;
705 List *r_usingvars = NIL;
708 Assert(j->quals == NULL); /* shouldn't have ON() too */
712 char *u_colname = strVal(lfirst(ucol));
720 /* Check for USING(foo,foo) */
721 foreach(col, res_colnames)
723 char *res_colname = strVal(lfirst(col));
725 if (strcmp(res_colname, u_colname) == 0)
727 (errcode(ERRCODE_DUPLICATE_COLUMN),
728 errmsg("column name \"%s\" appears more than once in USING clause",
732 /* Find it in left input */
734 foreach(col, l_colnames)
736 char *l_colname = strVal(lfirst(col));
738 if (strcmp(l_colname, u_colname) == 0)
742 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
743 errmsg("common column name \"%s\" appears more than once in left table",
751 (errcode(ERRCODE_UNDEFINED_COLUMN),
752 errmsg("column \"%s\" specified in USING clause not found in left table",
755 /* Find it in right input */
757 foreach(col, r_colnames)
759 char *r_colname = strVal(lfirst(col));
761 if (strcmp(r_colname, u_colname) == 0)
765 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
766 errmsg("common column name \"%s\" appears more than once in right table",
774 (errcode(ERRCODE_UNDEFINED_COLUMN),
775 errmsg("column \"%s\" specified in USING clause not found in right table",
778 l_colvar = nth(l_index, l_colvars);
779 l_usingvars = lappend(l_usingvars, l_colvar);
780 r_colvar = nth(r_index, r_colvars);
781 r_usingvars = lappend(r_usingvars, r_colvar);
783 res_colnames = lappend(res_colnames, lfirst(ucol));
784 res_colvars = lappend(res_colvars,
785 buildMergedJoinVar(pstate,
791 j->quals = transformJoinUsingClause(pstate,
797 /* User-written ON-condition; transform it */
798 j->quals = transformJoinOnClause(pstate, j, my_containedRels);
802 /* CROSS JOIN: no quals */
805 /* Add remaining columns from each side to the output columns */
806 extractRemainingColumns(res_colnames,
807 l_colnames, l_colvars,
808 &l_colnames, &l_colvars);
809 extractRemainingColumns(res_colnames,
810 r_colnames, r_colvars,
811 &r_colnames, &r_colvars);
812 res_colnames = nconc(res_colnames, l_colnames);
813 res_colvars = nconc(res_colvars, l_colvars);
814 res_colnames = nconc(res_colnames, r_colnames);
815 res_colvars = nconc(res_colvars, r_colvars);
818 * Check alias (AS clause), if any.
822 if (j->alias->colnames != NIL)
824 if (length(j->alias->colnames) > length(res_colnames))
826 (errcode(ERRCODE_SYNTAX_ERROR),
827 errmsg("column alias list for \"%s\" has too many entries",
828 j->alias->aliasname)));
833 * Now build an RTE for the result of the join
835 rte = addRangeTableEntryForJoin(pstate,
842 /* assume new rte is at end */
843 j->rtindex = length(pstate->p_rtable);
844 Assert(rte == rt_fetch(j->rtindex, pstate->p_rtable));
847 * Include join RTE in returned containedRels list
849 *containedRels = lconsi(j->rtindex, my_containedRels);
854 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(n));
855 return NULL; /* can't get here, keep compiler quiet */
859 * buildMergedJoinVar -
860 * generate a suitable replacement expression for a merged join column
863 buildMergedJoinVar(ParseState *pstate, JoinType jointype,
864 Var *l_colvar, Var *r_colvar)
873 * Choose output type if input types are dissimilar.
875 outcoltype = l_colvar->vartype;
876 outcoltypmod = l_colvar->vartypmod;
877 if (outcoltype != r_colvar->vartype)
879 outcoltype = select_common_type(makeListo2(l_colvar->vartype,
882 outcoltypmod = -1; /* ie, unknown */
884 else if (outcoltypmod != r_colvar->vartypmod)
886 /* same type, but not same typmod */
887 outcoltypmod = -1; /* ie, unknown */
891 * Insert coercion functions if needed. Note that a difference in
892 * typmod can only happen if input has typmod but outcoltypmod is -1.
893 * In that case we insert a RelabelType to clearly mark that result's
894 * typmod is not same as input.
896 if (l_colvar->vartype != outcoltype)
897 l_node = coerce_type(pstate, (Node *) l_colvar, l_colvar->vartype,
899 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
900 else if (l_colvar->vartypmod != outcoltypmod)
901 l_node = (Node *) makeRelabelType((Expr *) l_colvar,
902 outcoltype, outcoltypmod,
903 COERCE_IMPLICIT_CAST);
905 l_node = (Node *) l_colvar;
907 if (r_colvar->vartype != outcoltype)
908 r_node = coerce_type(pstate, (Node *) r_colvar, r_colvar->vartype,
910 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
911 else if (r_colvar->vartypmod != outcoltypmod)
912 r_node = (Node *) makeRelabelType((Expr *) r_colvar,
913 outcoltype, outcoltypmod,
914 COERCE_IMPLICIT_CAST);
916 r_node = (Node *) r_colvar;
919 * Choose what to emit
926 * We can use either var; prefer non-coerced one if available.
928 if (IsA(l_node, Var))
930 else if (IsA(r_node, Var))
936 /* Always use left var */
940 /* Always use right var */
946 * Here we must build a COALESCE expression to ensure that
947 * the join output is non-null if either input is.
949 CoalesceExpr *c = makeNode(CoalesceExpr);
951 c->coalescetype = outcoltype;
952 c->args = makeList2(l_node, r_node);
953 res_node = (Node *) c;
957 elog(ERROR, "unrecognized join type: %d", (int) jointype);
958 res_node = NULL; /* keep compiler quiet */
967 * transformWhereClause -
968 * Transform the qualification and make sure it is of type boolean.
969 * Used for WHERE and allied clauses.
971 * constructName does not affect the semantics, but is used in error messages
974 transformWhereClause(ParseState *pstate, Node *clause,
975 const char *constructName)
982 qual = transformExpr(pstate, clause);
984 qual = coerce_to_boolean(pstate, qual, constructName);
991 * transformLimitClause -
992 * Transform the expression and make sure it is of type integer.
993 * Used for LIMIT and allied clauses.
995 * constructName does not affect the semantics, but is used in error messages
998 transformLimitClause(ParseState *pstate, Node *clause,
999 const char *constructName)
1006 qual = transformExpr(pstate, clause);
1008 qual = coerce_to_integer(pstate, qual, constructName);
1011 * LIMIT can't refer to any vars or aggregates of the current query;
1012 * we don't allow subselects either (though that case would at least
1015 if (contain_vars_of_level(qual, 0))
1018 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1019 /* translator: %s is name of a SQL construct, eg LIMIT */
1020 errmsg("argument of %s must not contain variables",
1023 if (checkExprHasAggs(qual))
1026 (errcode(ERRCODE_GROUPING_ERROR),
1027 /* translator: %s is name of a SQL construct, eg LIMIT */
1028 errmsg("argument of %s must not contain aggregates",
1031 if (contain_subplans(qual))
1034 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1035 /* translator: %s is name of a SQL construct, eg LIMIT */
1036 errmsg("argument of %s must not contain subqueries",
1045 * findTargetlistEntry -
1046 * Returns the targetlist entry matching the given (untransformed) node.
1047 * If no matching entry exists, one is created and appended to the target
1048 * list as a "resjunk" node.
1050 * node the ORDER BY, GROUP BY, or DISTINCT ON expression to be matched
1051 * tlist the existing target list (NB: this will never be NIL, which is a
1052 * good thing since we'd be unable to append to it if it were...)
1053 * clause identifies clause type being processed.
1055 static TargetEntry *
1056 findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
1058 TargetEntry *target_result = NULL;
1063 * Handle two special cases as mandated by the SQL92 spec:
1065 * 1. Bare ColumnName (no qualifier or subscripts)
1066 * For a bare identifier, we search for a matching column name
1067 * in the existing target list. Multiple matches are an error
1068 * unless they refer to identical values; for example,
1069 * we allow SELECT a, a FROM table ORDER BY a
1070 * but not SELECT a AS b, b FROM table ORDER BY b
1071 * If no match is found, we fall through and treat the identifier
1073 * For GROUP BY, it is incorrect to match the grouping item against
1074 * targetlist entries: according to SQL92, an identifier in GROUP BY
1075 * is a reference to a column name exposed by FROM, not to a target
1076 * list column. However, many implementations (including pre-7.0
1077 * PostgreSQL) accept this anyway. So for GROUP BY, we look first
1078 * to see if the identifier matches any FROM column name, and only
1079 * try for a targetlist name if it doesn't. This ensures that we
1080 * adhere to the spec in the case where the name could be both.
1081 * DISTINCT ON isn't in the standard, so we can do what we like there;
1082 * we choose to make it work like ORDER BY, on the rather flimsy
1083 * grounds that ordinary DISTINCT works on targetlist entries.
1085 * 2. IntegerConstant
1086 * This means to use the n'th item in the existing target list.
1087 * Note that it would make no sense to order/group/distinct by an
1088 * actual constant, so this does not create a conflict with our
1089 * extension to order/group by an expression.
1090 * GROUP BY column-number is not allowed by SQL92, but since
1091 * the standard has no other behavior defined for this syntax,
1092 * we may as well accept this common extension.
1094 * Note that pre-existing resjunk targets must not be used in either case,
1095 * since the user didn't write them in his SELECT list.
1097 * If neither special case applies, fall through to treat the item as
1101 if (IsA(node, ColumnRef) &&
1102 length(((ColumnRef *) node)->fields) == 1 &&
1103 ((ColumnRef *) node)->indirection == NIL)
1105 char *name = strVal(lfirst(((ColumnRef *) node)->fields));
1107 if (clause == GROUP_CLAUSE)
1110 * In GROUP BY, we must prefer a match against a FROM-clause
1111 * column to one against the targetlist. Look to see if there
1112 * is a matching column. If so, fall through to let
1113 * transformExpr() do the rest. NOTE: if name could refer
1114 * ambiguously to more than one column name exposed by FROM,
1115 * colnameToVar will ereport(ERROR). That's just what we want
1118 if (colnameToVar(pstate, name) != NULL)
1126 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1127 Resdom *resnode = tle->resdom;
1129 if (!resnode->resjunk &&
1130 strcmp(resnode->resname, name) == 0)
1132 if (target_result != NULL)
1134 if (!equal(target_result->expr, tle->expr))
1136 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1137 /* translator: first %s is name of a SQL construct, eg ORDER BY */
1138 errmsg("%s \"%s\" is ambiguous",
1139 clauseText[clause], name)));
1142 target_result = tle;
1143 /* Stay in loop to check for ambiguity */
1146 if (target_result != NULL)
1147 return target_result; /* return the first match */
1150 if (IsA(node, A_Const))
1152 Value *val = &((A_Const *) node)->val;
1153 int targetlist_pos = 0;
1156 if (!IsA(val, Integer))
1158 (errcode(ERRCODE_SYNTAX_ERROR),
1159 /* translator: %s is name of a SQL construct, eg ORDER BY */
1160 errmsg("non-integer constant in %s",
1161 clauseText[clause])));
1162 target_pos = intVal(val);
1165 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1166 Resdom *resnode = tle->resdom;
1168 if (!resnode->resjunk)
1170 if (++targetlist_pos == target_pos)
1171 return tle; /* return the unique match */
1175 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1176 /* translator: %s is name of a SQL construct, eg ORDER BY */
1177 errmsg("%s position %d is not in select list",
1178 clauseText[clause], target_pos)));
1182 * Otherwise, we have an expression (this is a Postgres extension not
1183 * found in SQL92). Convert the untransformed node to a transformed
1184 * expression, and search for a match in the tlist. NOTE: it doesn't
1185 * really matter whether there is more than one match. Also, we are
1186 * willing to match a resjunk target here, though the above cases must
1187 * ignore resjunk targets.
1189 expr = transformExpr(pstate, node);
1193 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1195 if (equal(expr, tle->expr))
1200 * If no matches, construct a new target entry which is appended to
1201 * the end of the target list. This target is given resjunk = TRUE so
1202 * that it will not be projected into the final tuple.
1204 target_result = transformTargetEntry(pstate, node, expr, NULL, true);
1205 lappend(tlist, target_result);
1207 return target_result;
1212 * transformGroupClause -
1213 * transform a GROUP BY clause
1216 transformGroupClause(ParseState *pstate, List *grouplist,
1217 List *targetlist, List *sortClause)
1222 foreach(gl, grouplist)
1229 tle = findTargetlistEntry(pstate, lfirst(gl),
1230 targetlist, GROUP_CLAUSE);
1232 /* avoid making duplicate grouplist entries */
1233 if (targetIsInSortList(tle, glist))
1236 /* if tlist item is an UNKNOWN literal, change it to TEXT */
1237 restype = tle->resdom->restype;
1239 if (restype == UNKNOWNOID)
1241 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
1244 COERCE_IMPLICIT_CAST);
1245 restype = tle->resdom->restype = TEXTOID;
1246 tle->resdom->restypmod = -1;
1250 * If the GROUP BY clause matches the ORDER BY clause, we want to
1251 * adopt the ordering operators from the latter rather than using
1252 * the default ops. This allows "GROUP BY foo ORDER BY foo DESC"
1253 * to be done with only one sort step. Note we are assuming that
1254 * any user-supplied ordering operator will bring equal values
1255 * together, which is all that GROUP BY needs.
1258 ((SortClause *) lfirst(sortClause))->tleSortGroupRef ==
1259 tle->resdom->ressortgroupref)
1261 ordering_op = ((SortClause *) lfirst(sortClause))->sortop;
1262 sortClause = lnext(sortClause);
1266 ordering_op = ordering_oper_opid(restype);
1267 sortClause = NIL; /* disregard ORDER BY once match fails */
1270 grpcl = makeNode(GroupClause);
1271 grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
1272 grpcl->sortop = ordering_op;
1273 glist = lappend(glist, grpcl);
1280 * transformSortClause -
1281 * transform an ORDER BY clause
1284 transformSortClause(ParseState *pstate,
1287 bool resolveUnknown)
1289 List *sortlist = NIL;
1292 foreach(olitem, orderlist)
1294 SortBy *sortby = lfirst(olitem);
1297 tle = findTargetlistEntry(pstate, sortby->node,
1298 targetlist, ORDER_CLAUSE);
1300 sortlist = addTargetToSortList(pstate, tle,
1301 sortlist, targetlist,
1302 sortby->sortby_kind,
1311 * transformDistinctClause -
1312 * transform a DISTINCT or DISTINCT ON clause
1314 * Since we may need to add items to the query's sortClause list, that list
1315 * is passed by reference. We might also need to add items to the query's
1316 * targetlist, but we assume that cannot be empty initially, so we can
1317 * lappend to it even though the pointer is passed by value.
1320 transformDistinctClause(ParseState *pstate, List *distinctlist,
1321 List *targetlist, List **sortClause)
1327 /* No work if there was no DISTINCT clause */
1328 if (distinctlist == NIL)
1331 if (lfirst(distinctlist) == NIL)
1333 /* We had SELECT DISTINCT */
1336 * All non-resjunk elements from target list that are not already
1337 * in the sort list should be added to it. (We don't really care
1338 * what order the DISTINCT fields are checked in, so we can leave
1339 * the user's ORDER BY spec alone, and just add additional sort
1340 * keys to it to ensure that all targetlist items get sorted.)
1342 *sortClause = addAllTargetsToSortList(pstate,
1348 * Now, DISTINCT list consists of all non-resjunk sortlist items.
1349 * Actually, all the sortlist items had better be non-resjunk!
1350 * Otherwise, user wrote SELECT DISTINCT with an ORDER BY item
1351 * that does not appear anywhere in the SELECT targetlist, and we
1352 * can't implement that with only one sorting pass...
1354 foreach(slitem, *sortClause)
1356 SortClause *scl = (SortClause *) lfirst(slitem);
1357 TargetEntry *tle = get_sortgroupclause_tle(scl, targetlist);
1359 if (tle->resdom->resjunk)
1361 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1362 errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list")));
1364 result = lappend(result, copyObject(scl));
1369 /* We had SELECT DISTINCT ON (expr, ...) */
1372 * If the user writes both DISTINCT ON and ORDER BY, then the two
1373 * expression lists must match (until one or the other runs out).
1374 * Otherwise the ORDER BY requires a different sort order than the
1375 * DISTINCT does, and we can't implement that with only one sort
1376 * pass (and if we do two passes, the results will be rather
1377 * unpredictable). However, it's OK to have more DISTINCT ON
1378 * expressions than ORDER BY expressions; we can just add the
1379 * extra DISTINCT values to the sort list, much as we did above
1380 * for ordinary DISTINCT fields.
1382 * Actually, it'd be OK for the common prefixes of the two lists to
1383 * match in any order, but implementing that check seems like more
1384 * trouble than it's worth.
1386 List *nextsortlist = *sortClause;
1388 foreach(dlitem, distinctlist)
1392 tle = findTargetlistEntry(pstate, lfirst(dlitem),
1393 targetlist, DISTINCT_ON_CLAUSE);
1395 if (nextsortlist != NIL)
1397 SortClause *scl = (SortClause *) lfirst(nextsortlist);
1399 if (tle->resdom->ressortgroupref != scl->tleSortGroupRef)
1401 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1402 errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions")));
1403 result = lappend(result, copyObject(scl));
1404 nextsortlist = lnext(nextsortlist);
1408 *sortClause = addTargetToSortList(pstate, tle,
1409 *sortClause, targetlist,
1410 SORTBY_ASC, NIL, true);
1413 * Probably, the tle should always have been added at the
1414 * end of the sort list ... but search to be safe.
1416 foreach(slitem, *sortClause)
1418 SortClause *scl = (SortClause *) lfirst(slitem);
1420 if (tle->resdom->ressortgroupref == scl->tleSortGroupRef)
1422 result = lappend(result, copyObject(scl));
1426 if (slitem == NIL) /* should not happen */
1427 elog(ERROR, "failed to add DISTINCT ON clause to target list");
1436 * addAllTargetsToSortList
1437 * Make sure all non-resjunk targets in the targetlist are in the
1438 * ORDER BY list, adding the not-yet-sorted ones to the end of the list.
1439 * This is typically used to help implement SELECT DISTINCT.
1441 * See addTargetToSortList for info about pstate and resolveUnknown inputs.
1443 * Returns the updated ORDER BY list.
1446 addAllTargetsToSortList(ParseState *pstate, List *sortlist,
1447 List *targetlist, bool resolveUnknown)
1451 foreach(i, targetlist)
1453 TargetEntry *tle = (TargetEntry *) lfirst(i);
1455 if (!tle->resdom->resjunk)
1456 sortlist = addTargetToSortList(pstate, tle,
1457 sortlist, targetlist,
1465 * addTargetToSortList
1466 * If the given targetlist entry isn't already in the ORDER BY list,
1467 * add it to the end of the list, using the sortop with given name
1468 * or the default sort operator if opname == NIL.
1470 * If resolveUnknown is TRUE, convert TLEs of type UNKNOWN to TEXT. If not,
1471 * do nothing (which implies the search for a sort operator will fail).
1472 * pstate should be provided if resolveUnknown is TRUE, but can be NULL
1475 * Returns the updated ORDER BY list.
1478 addTargetToSortList(ParseState *pstate, TargetEntry *tle,
1479 List *sortlist, List *targetlist,
1480 int sortby_kind, List *sortby_opname,
1481 bool resolveUnknown)
1483 /* avoid making duplicate sortlist entries */
1484 if (!targetIsInSortList(tle, sortlist))
1486 SortClause *sortcl = makeNode(SortClause);
1487 Oid restype = tle->resdom->restype;
1489 /* if tlist item is an UNKNOWN literal, change it to TEXT */
1490 if (restype == UNKNOWNOID && resolveUnknown)
1492 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
1495 COERCE_IMPLICIT_CAST);
1496 restype = tle->resdom->restype = TEXTOID;
1497 tle->resdom->restypmod = -1;
1500 sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
1502 switch (sortby_kind)
1505 sortcl->sortop = ordering_oper_opid(restype);
1508 sortcl->sortop = reverse_ordering_oper_opid(restype);
1511 Assert(sortby_opname != NIL);
1512 sortcl->sortop = compatible_oper_opid(sortby_opname,
1518 elog(ERROR, "unrecognized sortby_kind: %d", sortby_kind);
1522 sortlist = lappend(sortlist, sortcl);
1528 * assignSortGroupRef
1529 * Assign the targetentry an unused ressortgroupref, if it doesn't
1530 * already have one. Return the assigned or pre-existing refnumber.
1532 * 'tlist' is the targetlist containing (or to contain) the given targetentry.
1535 assignSortGroupRef(TargetEntry *tle, List *tlist)
1540 if (tle->resdom->ressortgroupref) /* already has one? */
1541 return tle->resdom->ressortgroupref;
1543 /* easiest way to pick an unused refnumber: max used + 1 */
1547 Index ref = ((TargetEntry *) lfirst(l))->resdom->ressortgroupref;
1552 tle->resdom->ressortgroupref = maxRef + 1;
1553 return tle->resdom->ressortgroupref;
1557 * targetIsInSortList
1558 * Is the given target item already in the sortlist?
1560 * Works for both SortClause and GroupClause lists. Note that the main
1561 * reason we need this routine (and not just a quick test for nonzeroness
1562 * of ressortgroupref) is that a TLE might be in only one of the lists.
1565 targetIsInSortList(TargetEntry *tle, List *sortList)
1567 Index ref = tle->resdom->ressortgroupref;
1570 /* no need to scan list if tle has no marker */
1574 foreach(i, sortList)
1576 SortClause *scl = (SortClause *) lfirst(i);
1578 if (scl->tleSortGroupRef == ref)