1 /*-------------------------------------------------------------------------
4 * handle clauses in parser
6 * Portions Copyright (c) 1996-2001, 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.78 2001/03/22 03:59:41 momjian Exp $
13 *-------------------------------------------------------------------------
18 #include "access/heapam.h"
19 #include "nodes/makefuncs.h"
20 #include "optimizer/tlist.h"
21 #include "optimizer/var.h"
22 #include "parser/analyze.h"
23 #include "parser/parse.h"
24 #include "parser/parsetree.h"
25 #include "parser/parse_clause.h"
26 #include "parser/parse_coerce.h"
27 #include "parser/parse_expr.h"
28 #include "parser/parse_oper.h"
29 #include "parser/parse_relation.h"
30 #include "parser/parse_target.h"
31 #include "parser/parse_type.h"
32 #include "utils/guc.h"
35 #define ORDER_CLAUSE 0
36 #define GROUP_CLAUSE 1
37 #define DISTINCT_ON_CLAUSE 2
39 static char *clauseText[] = {"ORDER BY", "GROUP BY", "DISTINCT ON"};
41 static void extractUniqueColumns(List *common_colnames,
42 List *src_colnames, List *src_colvars,
43 List **res_colnames, List **res_colvars);
44 static Node *transformJoinUsingClause(ParseState *pstate,
45 List *leftVars, List *rightVars);
46 static Node *transformJoinOnClause(ParseState *pstate, JoinExpr *j,
48 static RangeTblRef *transformTableEntry(ParseState *pstate, RangeVar *r);
49 static RangeTblRef *transformRangeSubselect(ParseState *pstate,
51 static Node *transformFromClauseItem(ParseState *pstate, Node *n,
52 List **containedRels);
53 static TargetEntry *findTargetlistEntry(ParseState *pstate, Node *node,
54 List *tlist, int clause);
55 static List *addTargetToSortList(TargetEntry *tle, List *sortlist,
56 List *targetlist, char *opname);
57 static bool exprIsInSortList(Node *expr, List *sortList, List *targetList);
61 * transformFromClause -
62 * Process the FROM clause and add items to the query's range table,
63 * joinlist, and namespace.
65 * Note: we assume that pstate's p_rtable, p_joinlist, and p_namespace lists
66 * were initialized to NIL when the pstate was created. We will add onto
67 * any entries already present --- this is needed for rule processing, as
68 * well as for UPDATE and DELETE.
70 * The range table may grow still further when we transform the expressions
71 * in the query's quals and target list. (This is possible because in
72 * POSTQUEL, we allowed references to relations not specified in the
73 * from-clause. PostgreSQL keeps this extension to standard SQL.)
76 transformFromClause(ParseState *pstate, List *frmList)
81 * The grammar will have produced a list of RangeVars,
82 * RangeSubselects, and/or JoinExprs. Transform each one (possibly
83 * adding entries to the rtable), check for duplicate refnames, and
84 * then add it to the joinlist and namespace.
91 n = transformFromClauseItem(pstate, n, &containedRels);
92 checkNameSpaceConflicts(pstate, (Node *) pstate->p_namespace, n);
93 pstate->p_joinlist = lappend(pstate->p_joinlist, n);
94 pstate->p_namespace = lappend(pstate->p_namespace, n);
100 * Add the target relation of INSERT/UPDATE/DELETE to the range table,
101 * and make the special links to it in the ParseState.
103 * We also open the target relation and acquire a write lock on it.
104 * This must be done before processing the FROM list, in case the target
105 * is also mentioned as a source relation --- we want to be sure to grab
106 * the write lock before any read lock.
108 * If alsoSource is true, add the target to the query's joinlist and
109 * namespace. For INSERT, we don't want the target to be joined to;
110 * it's a destination of tuples, not a source. For UPDATE/DELETE,
111 * we do need to scan or join the target. (NOTE: we do not bother
112 * to check for namespace conflict; we assume that the namespace was
113 * initially empty in these cases.)
115 * Returns the rangetable index of the target relation.
118 setTargetTable(ParseState *pstate, char *relname,
119 bool inh, bool alsoSource)
124 /* Close old target; this could only happen for multi-action rules */
125 if (pstate->p_target_relation != NULL)
126 heap_close(pstate->p_target_relation, NoLock);
129 * Open target rel and grab suitable lock (which we will hold till end
132 * analyze.c will eventually do the corresponding heap_close(), but *not*
135 pstate->p_target_relation = heap_openr(relname, RowExclusiveLock);
140 rte = addRangeTableEntry(pstate, relname, NULL, inh, false);
141 pstate->p_target_rangetblentry = rte;
143 /* assume new rte is at end */
144 rtindex = length(pstate->p_rtable);
145 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
148 * Override addRangeTableEntry's default checkForRead, and instead
149 * mark target table as requiring write access.
151 * If we find an explicit reference to the rel later during parse
152 * analysis, scanRTEForColumn will change checkForRead to 'true'
153 * again. That can't happen for INSERT but it is possible for UPDATE
156 rte->checkForRead = false;
157 rte->checkForWrite = true;
160 * If UPDATE/DELETE, add table to joinlist and namespace.
163 addRTEtoQuery(pstate, rte, true, true);
169 * Simplify InhOption (yes/no/default) into boolean yes/no.
171 * The reason we do things this way is that we don't want to examine the
172 * SQL_inheritance option flag until parse_analyze is run. Otherwise,
173 * we'd do the wrong thing with query strings that intermix SET commands
177 interpretInhOption(InhOption inhOpt)
186 return SQL_inheritance;
188 elog(ERROR, "Bogus InhOption value");
189 return false; /* keep compiler quiet */
193 * Extract all not-in-common columns from column lists of a source table
196 extractUniqueColumns(List *common_colnames,
197 List *src_colnames, List *src_colvars,
198 List **res_colnames, List **res_colvars)
200 List *new_colnames = NIL;
201 List *new_colvars = NIL;
203 *lvars = src_colvars;
205 foreach(lnames, src_colnames)
207 char *colname = strVal(lfirst(lnames));
211 foreach(cnames, common_colnames)
213 char *ccolname = strVal(lfirst(cnames));
215 if (strcmp(colname, ccolname) == 0)
224 new_colnames = lappend(new_colnames, lfirst(lnames));
225 new_colvars = lappend(new_colvars, lfirst(lvars));
228 lvars = lnext(lvars);
231 *res_colnames = new_colnames;
232 *res_colvars = new_colvars;
235 /* transformJoinUsingClause()
236 * Build a complete ON clause from a partially-transformed USING list.
237 * We are given lists of nodes representing left and right match columns.
238 * Result is a transformed qualification expression.
241 transformJoinUsingClause(ParseState *pstate, List *leftVars, List *rightVars)
248 * We cheat a little bit here by building an untransformed operator
249 * tree whose leaves are the already-transformed Vars. This is OK
250 * because transformExpr() won't complain about already-transformed
253 foreach(lvars, leftVars)
255 Node *lvar = (Node *) lfirst(lvars);
256 Node *rvar = (Node *) lfirst(rvars);
259 e = makeNode(A_Expr);
262 e->lexpr = copyObject(lvar);
263 e->rexpr = copyObject(rvar);
269 A_Expr *a = makeNode(A_Expr);
274 a->rexpr = (Node *) e;
278 rvars = lnext(rvars);
282 * Since the references are already Vars, and are certainly from the
283 * input relations, we don't have to go through the same pushups that
284 * transformJoinOnClause() does. Just invoke transformExpr() to fix
285 * up the operators, and we're done.
287 result = transformExpr(pstate, result, EXPR_COLUMN_FIRST);
289 if (exprType(result) != BOOLOID)
293 * This could only happen if someone defines a funny version of
296 elog(ERROR, "JOIN/USING clause must return type bool, not type %s",
297 typeidTypeName(exprType(result)));
301 } /* transformJoinUsingClause() */
303 /* transformJoinOnClause()
304 * Transform the qual conditions for JOIN/ON.
305 * Result is a transformed qualification expression.
308 transformJoinOnClause(ParseState *pstate, JoinExpr *j,
312 List *save_namespace;
317 * This is a tad tricky, for two reasons. First, the namespace that
318 * the join expression should see is just the two subtrees of the JOIN
319 * plus any outer references from upper pstate levels. So,
320 * temporarily set this pstate's namespace accordingly. (We need not
321 * check for refname conflicts, because transformFromClauseItem()
322 * already did.) NOTE: this code is OK only because the ON clause
323 * can't legally alter the namespace by causing implicit relation refs
326 save_namespace = pstate->p_namespace;
327 pstate->p_namespace = makeList2(j->larg, j->rarg);
329 /* This part is just like transformWhereClause() */
330 result = transformExpr(pstate, j->quals, EXPR_COLUMN_FIRST);
331 if (exprType(result) != BOOLOID)
333 elog(ERROR, "JOIN/ON clause must return type bool, not type %s",
334 typeidTypeName(exprType(result)));
337 pstate->p_namespace = save_namespace;
340 * Second, we need to check that the ON condition doesn't refer to any
341 * rels outside the input subtrees of the JOIN. It could do that
342 * despite our hack on the namespace if it uses fully-qualified names.
343 * So, grovel through the transformed clause and make sure there are
344 * no bogus references. (Outer references are OK, and are ignored
347 clause_varnos = pull_varnos(result);
348 foreach(l, clause_varnos)
350 int varno = lfirsti(l);
352 if (!intMember(varno, containedRels))
354 elog(ERROR, "JOIN/ON clause refers to \"%s\", which is not part of JOIN",
355 rt_fetch(varno, pstate->p_rtable)->eref->relname);
358 freeList(clause_varnos);
364 * transformTableEntry --- transform a RangeVar (simple relation reference)
367 transformTableEntry(ParseState *pstate, RangeVar *r)
369 char *relname = r->relname;
374 * mark this entry to indicate it comes from the FROM clause. In SQL,
375 * the target list can only refer to range variables specified in the
376 * from clause but we follow the more powerful POSTQUEL semantics and
377 * automatically generate the range variable if not specified. However
378 * there are times we need to know whether the entries are legitimate.
380 rte = addRangeTableEntry(pstate, relname, r->name,
381 interpretInhOption(r->inhOpt), true);
384 * We create a RangeTblRef, but we do not add it to the joinlist or
385 * namespace; our caller must do that if appropriate.
387 rtr = makeNode(RangeTblRef);
388 /* assume new rte is at end */
389 rtr->rtindex = length(pstate->p_rtable);
390 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
397 * transformRangeSubselect --- transform a sub-SELECT appearing in FROM
400 transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
402 List *save_namespace;
409 * We require user to supply an alias for a subselect, per SQL92. To
410 * relax this, we'd have to be prepared to gin up a unique alias for
411 * an unlabeled subselect.
414 elog(ERROR, "sub-select in FROM must have an alias");
417 * Analyze and transform the subquery. This is a bit tricky because
418 * we don't want the subquery to be able to see any FROM items already
419 * created in the current query (per SQL92, the scope of a FROM item
420 * does not include other FROM items). But it does need to be able to
421 * see any further-up parent states, so we can't just pass a null
422 * parent pstate link. So, temporarily make the current query level
423 * have an empty namespace.
425 save_namespace = pstate->p_namespace;
426 pstate->p_namespace = NIL;
427 parsetrees = parse_analyze(r->subquery, pstate);
428 pstate->p_namespace = save_namespace;
431 * Check that we got something reasonable. Some of these conditions
432 * are probably impossible given restrictions of the grammar, but
435 if (length(parsetrees) != 1)
436 elog(ERROR, "Unexpected parse analysis result for subselect in FROM");
437 query = (Query *) lfirst(parsetrees);
438 if (query == NULL || !IsA(query, Query))
439 elog(ERROR, "Unexpected parse analysis result for subselect in FROM");
441 if (query->commandType != CMD_SELECT)
442 elog(ERROR, "Expected SELECT query from subselect in FROM");
443 if (query->resultRelation != 0 || query->into != NULL || query->isPortal)
444 elog(ERROR, "Subselect in FROM may not have SELECT INTO");
447 * OK, build an RTE for the subquery.
449 rte = addRangeTableEntryForSubquery(pstate, query, r->name, true);
452 * We create a RangeTblRef, but we do not add it to the joinlist or
453 * namespace; our caller must do that if appropriate.
455 rtr = makeNode(RangeTblRef);
456 /* assume new rte is at end */
457 rtr->rtindex = length(pstate->p_rtable);
458 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
465 * transformFromClauseItem -
466 * Transform a FROM-clause item, adding any required entries to the
467 * range table list being built in the ParseState, and return the
468 * transformed item ready to include in the joinlist and namespace.
469 * This routine can recurse to handle SQL92 JOIN expressions.
471 * Aside from the primary return value (the transformed joinlist item)
472 * this routine also returns an integer list of the rangetable indexes
473 * of all the base relations represented in the joinlist item. This
474 * list is needed for checking JOIN/ON conditions in higher levels.
477 transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels)
479 if (IsA(n, RangeVar))
481 /* Plain relation reference */
484 rtr = transformTableEntry(pstate, (RangeVar *) n);
485 *containedRels = makeListi1(rtr->rtindex);
488 else if (IsA(n, RangeSubselect))
490 /* sub-SELECT is like a plain relation */
493 rtr = transformRangeSubselect(pstate, (RangeSubselect *) n);
494 *containedRels = makeListi1(rtr->rtindex);
497 else if (IsA(n, JoinExpr))
499 /* A newfangled join expression */
500 JoinExpr *j = (JoinExpr *) n;
501 List *l_containedRels,
511 * Recursively process the left and right subtrees
513 j->larg = transformFromClauseItem(pstate, j->larg, &l_containedRels);
514 j->rarg = transformFromClauseItem(pstate, j->rarg, &r_containedRels);
517 * Generate combined list of relation indexes
519 *containedRels = nconc(l_containedRels, r_containedRels);
522 * Check for conflicting refnames in left and right subtrees.
523 * Must do this because higher levels will assume I hand back a
524 * self- consistent namespace subtree.
526 checkNameSpaceConflicts(pstate, j->larg, j->rarg);
529 * Extract column name and var lists from both subtrees
531 if (IsA(j->larg, JoinExpr))
533 /* Make a copy of the subtree's lists so we can modify! */
534 l_colnames = copyObject(((JoinExpr *) j->larg)->colnames);
535 l_colvars = copyObject(((JoinExpr *) j->larg)->colvars);
541 Assert(IsA(j->larg, RangeTblRef));
542 rte = rt_fetch(((RangeTblRef *) j->larg)->rtindex,
544 expandRTE(pstate, rte, &l_colnames, &l_colvars);
545 /* expandRTE returns new lists, so no need for copyObject */
547 if (IsA(j->rarg, JoinExpr))
549 /* Make a copy of the subtree's lists so we can modify! */
550 r_colnames = copyObject(((JoinExpr *) j->rarg)->colnames);
551 r_colvars = copyObject(((JoinExpr *) j->rarg)->colvars);
557 Assert(IsA(j->rarg, RangeTblRef));
558 rte = rt_fetch(((RangeTblRef *) j->rarg)->rtindex,
560 expandRTE(pstate, rte, &r_colnames, &r_colvars);
561 /* expandRTE returns new lists, so no need for copyObject */
565 * Natural join does not explicitly specify columns; must generate
566 * columns to join. Need to run through the list of columns from
567 * each table or join result and match up the column names. Use
568 * the first table, and check every column in the second table for
569 * a match. (We'll check that the matches were unique later on.)
570 * The result of this step is a list of column names just like an
571 * explicitly-written USING list.
579 Assert(j->using == NIL); /* shouldn't have USING() too */
581 foreach(lx, l_colnames)
583 char *l_colname = strVal(lfirst(lx));
584 Value *m_name = NULL;
586 foreach(rx, r_colnames)
588 char *r_colname = strVal(lfirst(rx));
590 if (strcmp(l_colname, r_colname) == 0)
592 m_name = makeString(l_colname);
597 /* matched a right column? then keep as join column... */
599 rlist = lappend(rlist, m_name);
606 * Now transform the join qualifications, if any.
615 * JOIN/USING (or NATURAL JOIN, as transformed above).
616 * Transform the list into an explicit ON-condition, and
617 * generate a list of result columns.
619 List *ucols = j->using;
620 List *l_usingvars = NIL;
621 List *r_usingvars = NIL;
624 Assert(j->quals == NULL); /* shouldn't have ON() too */
628 char *u_colname = strVal(lfirst(ucol));
638 foreach(col, l_colnames)
640 char *l_colname = strVal(lfirst(col));
642 if (strcmp(l_colname, u_colname) == 0)
645 elog(ERROR, "Common column name \"%s\" appears more than once in left table", u_colname);
651 elog(ERROR, "JOIN/USING column \"%s\" not found in left table",
655 foreach(col, r_colnames)
657 char *r_colname = strVal(lfirst(col));
659 if (strcmp(r_colname, u_colname) == 0)
662 elog(ERROR, "Common column name \"%s\" appears more than once in right table", u_colname);
668 elog(ERROR, "JOIN/USING column \"%s\" not found in right table",
671 l_colvar = nth(l_index, l_colvars);
672 l_usingvars = lappend(l_usingvars, l_colvar);
673 r_colvar = nth(r_index, r_colvars);
674 r_usingvars = lappend(r_usingvars, r_colvar);
676 res_colnames = lappend(res_colnames,
677 nth(l_index, l_colnames));
689 /* Need COALESCE(l_colvar, r_colvar) */
690 CaseExpr *c = makeNode(CaseExpr);
691 CaseWhen *w = makeNode(CaseWhen);
692 A_Expr *a = makeNode(A_Expr);
696 w->expr = (Node *) a;
697 w->result = l_colvar;
698 c->args = makeList1(w);
699 c->defresult = r_colvar;
700 colvar = transformExpr(pstate, (Node *) c,
705 res_colvars = lappend(res_colvars, colvar);
708 j->quals = transformJoinUsingClause(pstate,
714 /* User-written ON-condition; transform it */
715 j->quals = transformJoinOnClause(pstate, j, *containedRels);
719 /* CROSS JOIN: no quals */
722 /* Add remaining columns from each side to the output columns */
723 extractUniqueColumns(res_colnames,
724 l_colnames, l_colvars,
725 &l_colnames, &l_colvars);
726 extractUniqueColumns(res_colnames,
727 r_colnames, r_colvars,
728 &r_colnames, &r_colvars);
729 res_colnames = nconc(res_colnames, l_colnames);
730 res_colvars = nconc(res_colvars, l_colvars);
731 res_colnames = nconc(res_colnames, r_colnames);
732 res_colvars = nconc(res_colvars, r_colvars);
735 * Process alias (AS clause), if any.
741 * If a column alias list is specified, substitute the alias
742 * names into my output-column list
744 if (j->alias->attrs != NIL)
746 if (length(j->alias->attrs) != length(res_colnames))
747 elog(ERROR, "Column alias list for \"%s\" has wrong number of entries (need %d)",
748 j->alias->relname, length(res_colnames));
749 res_colnames = j->alias->attrs;
753 j->colnames = res_colnames;
754 j->colvars = res_colvars;
759 elog(ERROR, "transformFromClauseItem: unexpected node (internal error)"
760 "\n\t%s", nodeToString(n));
761 return NULL; /* can't get here, just keep compiler
767 * transformWhereClause -
768 * transforms the qualification and make sure it is of type Boolean
771 transformWhereClause(ParseState *pstate, Node *clause)
778 qual = transformExpr(pstate, clause, EXPR_COLUMN_FIRST);
780 if (exprType(qual) != BOOLOID)
782 elog(ERROR, "WHERE clause must return type bool, not type %s",
783 typeidTypeName(exprType(qual)));
790 * findTargetlistEntry -
791 * Returns the targetlist entry matching the given (untransformed) node.
792 * If no matching entry exists, one is created and appended to the target
793 * list as a "resjunk" node.
795 * node the ORDER BY, GROUP BY, or DISTINCT ON expression to be matched
796 * tlist the existing target list (NB: this will never be NIL, which is a
797 * good thing since we'd be unable to append to it if it were...)
798 * clause identifies clause type being processed.
801 findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
803 TargetEntry *target_result = NULL;
808 * Handle two special cases as mandated by the SQL92 spec:
810 * 1. Bare ColumnName (no qualifier or subscripts)
811 * For a bare identifier, we search for a matching column name
812 * in the existing target list. Multiple matches are an error
813 * unless they refer to identical values; for example,
814 * we allow SELECT a, a FROM table ORDER BY a
815 * but not SELECT a AS b, b FROM table ORDER BY b
816 * If no match is found, we fall through and treat the identifier
818 * For GROUP BY, it is incorrect to match the grouping item against
819 * targetlist entries: according to SQL92, an identifier in GROUP BY
820 * is a reference to a column name exposed by FROM, not to a target
821 * list column. However, many implementations (including pre-7.0
822 * PostgreSQL) accept this anyway. So for GROUP BY, we look first
823 * to see if the identifier matches any FROM column name, and only
824 * try for a targetlist name if it doesn't. This ensures that we
825 * adhere to the spec in the case where the name could be both.
826 * DISTINCT ON isn't in the standard, so we can do what we like there;
827 * we choose to make it work like ORDER BY, on the rather flimsy
828 * grounds that ordinary DISTINCT works on targetlist entries.
831 * This means to use the n'th item in the existing target list.
832 * Note that it would make no sense to order/group/distinct by an
833 * actual constant, so this does not create a conflict with our
834 * extension to order/group by an expression.
835 * GROUP BY column-number is not allowed by SQL92, but since
836 * the standard has no other behavior defined for this syntax,
837 * we may as well accept this common extension.
839 * Note that pre-existing resjunk targets must not be used in either case,
840 * since the user didn't write them in his SELECT list.
842 * If neither special case applies, fall through to treat the item as
846 if (IsA(node, Ident) &&((Ident *) node)->indirection == NIL)
848 char *name = ((Ident *) node)->name;
850 if (clause == GROUP_CLAUSE)
854 * In GROUP BY, we must prefer a match against a FROM-clause
855 * column to one against the targetlist. Look to see if there
856 * is a matching column. If so, fall through to let
857 * transformExpr() do the rest. NOTE: if name could refer
858 * ambiguously to more than one column name exposed by FROM,
859 * colnameToVar will elog(ERROR). That's just what we want
862 if (colnameToVar(pstate, name) != NULL)
870 TargetEntry *tle = (TargetEntry *) lfirst(tl);
871 Resdom *resnode = tle->resdom;
873 if (!resnode->resjunk &&
874 strcmp(resnode->resname, name) == 0)
876 if (target_result != NULL)
878 if (!equal(target_result->expr, tle->expr))
879 elog(ERROR, "%s '%s' is ambiguous",
880 clauseText[clause], name);
884 /* Stay in loop to check for ambiguity */
887 if (target_result != NULL)
888 return target_result; /* return the first match */
891 if (IsA(node, A_Const))
893 Value *val = &((A_Const *) node)->val;
894 int targetlist_pos = 0;
897 if (!IsA(val, Integer))
898 elog(ERROR, "Non-integer constant in %s", clauseText[clause]);
899 target_pos = intVal(val);
902 TargetEntry *tle = (TargetEntry *) lfirst(tl);
903 Resdom *resnode = tle->resdom;
905 if (!resnode->resjunk)
907 if (++targetlist_pos == target_pos)
908 return tle; /* return the unique match */
911 elog(ERROR, "%s position %d is not in target list",
912 clauseText[clause], target_pos);
916 * Otherwise, we have an expression (this is a Postgres extension not
917 * found in SQL92). Convert the untransformed node to a transformed
918 * expression, and search for a match in the tlist. NOTE: it doesn't
919 * really matter whether there is more than one match. Also, we are
920 * willing to match a resjunk target here, though the above cases must
921 * ignore resjunk targets.
923 expr = transformExpr(pstate, node, EXPR_COLUMN_FIRST);
927 TargetEntry *tle = (TargetEntry *) lfirst(tl);
929 if (equal(expr, tle->expr))
934 * If no matches, construct a new target entry which is appended to
935 * the end of the target list. This target is given resjunk = TRUE so
936 * that it will not be projected into the final tuple.
938 target_result = transformTargetEntry(pstate, node, expr, NULL, true);
939 lappend(tlist, target_result);
941 return target_result;
946 * transformGroupClause -
947 * transform a Group By clause
951 transformGroupClause(ParseState *pstate, List *grouplist, List *targetlist)
956 foreach(gl, grouplist)
960 tle = findTargetlistEntry(pstate, lfirst(gl),
961 targetlist, GROUP_CLAUSE);
963 /* avoid making duplicate grouplist entries */
964 if (!exprIsInSortList(tle->expr, glist, targetlist))
966 GroupClause *grpcl = makeNode(GroupClause);
968 grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
970 grpcl->sortop = any_ordering_op(tle->resdom->restype);
972 glist = lappend(glist, grpcl);
980 * transformSortClause -
981 * transform an ORDER BY clause
984 transformSortClause(ParseState *pstate,
988 List *sortlist = NIL;
991 foreach(olitem, orderlist)
993 SortGroupBy *sortby = lfirst(olitem);
996 tle = findTargetlistEntry(pstate, sortby->node,
997 targetlist, ORDER_CLAUSE);
999 sortlist = addTargetToSortList(tle, sortlist, targetlist,
1007 * transformDistinctClause -
1008 * transform a DISTINCT or DISTINCT ON clause
1010 * Since we may need to add items to the query's sortClause list, that list
1011 * is passed by reference. We might also need to add items to the query's
1012 * targetlist, but we assume that cannot be empty initially, so we can
1013 * lappend to it even though the pointer is passed by value.
1016 transformDistinctClause(ParseState *pstate, List *distinctlist,
1017 List *targetlist, List **sortClause)
1023 /* No work if there was no DISTINCT clause */
1024 if (distinctlist == NIL)
1027 if (lfirst(distinctlist) == NIL)
1029 /* We had SELECT DISTINCT */
1032 * All non-resjunk elements from target list that are not already
1033 * in the sort list should be added to it. (We don't really care
1034 * what order the DISTINCT fields are checked in, so we can leave
1035 * the user's ORDER BY spec alone, and just add additional sort
1036 * keys to it to ensure that all targetlist items get sorted.)
1038 *sortClause = addAllTargetsToSortList(*sortClause, targetlist);
1041 * Now, DISTINCT list consists of all non-resjunk sortlist items.
1042 * Actually, all the sortlist items had better be non-resjunk!
1043 * Otherwise, user wrote SELECT DISTINCT with an ORDER BY item
1044 * that does not appear anywhere in the SELECT targetlist, and we
1045 * can't implement that with only one sorting pass...
1047 foreach(slitem, *sortClause)
1049 SortClause *scl = (SortClause *) lfirst(slitem);
1050 TargetEntry *tle = get_sortgroupclause_tle(scl, targetlist);
1052 if (tle->resdom->resjunk)
1053 elog(ERROR, "For SELECT DISTINCT, ORDER BY expressions must appear in target list");
1055 result = lappend(result, copyObject(scl));
1060 /* We had SELECT DISTINCT ON (expr, ...) */
1063 * If the user writes both DISTINCT ON and ORDER BY, then the two
1064 * expression lists must match (until one or the other runs out).
1065 * Otherwise the ORDER BY requires a different sort order than the
1066 * DISTINCT does, and we can't implement that with only one sort
1067 * pass (and if we do two passes, the results will be rather
1068 * unpredictable). However, it's OK to have more DISTINCT ON
1069 * expressions than ORDER BY expressions; we can just add the
1070 * extra DISTINCT values to the sort list, much as we did above
1071 * for ordinary DISTINCT fields.
1073 * Actually, it'd be OK for the common prefixes of the two lists to
1074 * match in any order, but implementing that check seems like more
1075 * trouble than it's worth.
1077 List *nextsortlist = *sortClause;
1079 foreach(dlitem, distinctlist)
1083 tle = findTargetlistEntry(pstate, lfirst(dlitem),
1084 targetlist, DISTINCT_ON_CLAUSE);
1086 if (nextsortlist != NIL)
1088 SortClause *scl = (SortClause *) lfirst(nextsortlist);
1090 if (tle->resdom->ressortgroupref != scl->tleSortGroupRef)
1091 elog(ERROR, "SELECT DISTINCT ON expressions must match initial ORDER BY expressions");
1092 result = lappend(result, copyObject(scl));
1093 nextsortlist = lnext(nextsortlist);
1097 *sortClause = addTargetToSortList(tle, *sortClause,
1101 * Probably, the tle should always have been added at the
1102 * end of the sort list ... but search to be safe.
1104 foreach(slitem, *sortClause)
1106 SortClause *scl = (SortClause *) lfirst(slitem);
1108 if (tle->resdom->ressortgroupref == scl->tleSortGroupRef)
1110 result = lappend(result, copyObject(scl));
1115 elog(ERROR, "transformDistinctClause: failed to add DISTINCT ON clause to target list");
1124 * addAllTargetsToSortList
1125 * Make sure all non-resjunk targets in the targetlist are in the
1126 * ORDER BY list, adding the not-yet-sorted ones to the end of the list.
1127 * This is typically used to help implement SELECT DISTINCT.
1129 * Returns the updated ORDER BY list.
1132 addAllTargetsToSortList(List *sortlist, List *targetlist)
1136 foreach(i, targetlist)
1138 TargetEntry *tle = (TargetEntry *) lfirst(i);
1140 if (!tle->resdom->resjunk)
1141 sortlist = addTargetToSortList(tle, sortlist, targetlist, NULL);
1147 * addTargetToSortList
1148 * If the given targetlist entry isn't already in the ORDER BY list,
1149 * add it to the end of the list, using the sortop with given name
1150 * or any available sort operator if opname == NULL.
1152 * Returns the updated ORDER BY list.
1155 addTargetToSortList(TargetEntry *tle, List *sortlist, List *targetlist,
1158 /* avoid making duplicate sortlist entries */
1159 if (!exprIsInSortList(tle->expr, sortlist, targetlist))
1161 SortClause *sortcl = makeNode(SortClause);
1163 sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
1166 sortcl->sortop = compatible_oper_opid(opname,
1167 tle->resdom->restype,
1168 tle->resdom->restype,
1171 sortcl->sortop = any_ordering_op(tle->resdom->restype);
1173 sortlist = lappend(sortlist, sortcl);
1179 * assignSortGroupRef
1180 * Assign the targetentry an unused ressortgroupref, if it doesn't
1181 * already have one. Return the assigned or pre-existing refnumber.
1183 * 'tlist' is the targetlist containing (or to contain) the given targetentry.
1186 assignSortGroupRef(TargetEntry *tle, List *tlist)
1191 if (tle->resdom->ressortgroupref) /* already has one? */
1192 return tle->resdom->ressortgroupref;
1194 /* easiest way to pick an unused refnumber: max used + 1 */
1198 Index ref = ((TargetEntry *) lfirst(l))->resdom->ressortgroupref;
1203 tle->resdom->ressortgroupref = maxRef + 1;
1204 return tle->resdom->ressortgroupref;
1209 * Is the given expression already in the sortlist?
1210 * Note we will say 'yes' if it is equal() to any sortlist item,
1211 * even though that might be a different targetlist member.
1213 * Works for both SortClause and GroupClause lists.
1216 exprIsInSortList(Node *expr, List *sortList, List *targetList)
1220 foreach(i, sortList)
1222 SortClause *scl = (SortClause *) lfirst(i);
1224 if (equal(expr, get_sortgroupclause_expr(scl, targetList)))