1 /*-------------------------------------------------------------------------
4 * handle clauses in parser
6 * Portions Copyright (c) 1996-2002, 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.107 2003/02/13 05:06:34 momjian 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 "utils/builtins.h"
34 #include "utils/guc.h"
37 #define ORDER_CLAUSE 0
38 #define GROUP_CLAUSE 1
39 #define DISTINCT_ON_CLAUSE 2
41 static char *clauseText[] = {"ORDER BY", "GROUP BY", "DISTINCT ON"};
43 static void extractRemainingColumns(List *common_colnames,
44 List *src_colnames, List *src_colvars,
45 List **res_colnames, List **res_colvars);
46 static Node *transformJoinUsingClause(ParseState *pstate,
47 List *leftVars, List *rightVars);
48 static Node *transformJoinOnClause(ParseState *pstate, JoinExpr *j,
50 static RangeTblRef *transformTableEntry(ParseState *pstate, RangeVar *r);
51 static RangeTblRef *transformRangeSubselect(ParseState *pstate,
53 static RangeTblRef *transformRangeFunction(ParseState *pstate,
55 static Node *transformFromClauseItem(ParseState *pstate, Node *n,
56 List **containedRels);
57 static Node *buildMergedJoinVar(JoinType jointype,
58 Var *l_colvar, Var *r_colvar);
59 static TargetEntry *findTargetlistEntry(ParseState *pstate, Node *node,
60 List *tlist, int clause);
61 static List *addTargetToSortList(TargetEntry *tle, List *sortlist,
62 List *targetlist, List *opname);
63 static bool targetIsInSortList(TargetEntry *tle, List *sortList);
67 * transformFromClause -
68 * Process the FROM clause and add items to the query's range table,
69 * joinlist, and namespace.
71 * Note: we assume that pstate's p_rtable, p_joinlist, and p_namespace lists
72 * were initialized to NIL when the pstate was created. We will add onto
73 * any entries already present --- this is needed for rule processing, as
74 * well as for UPDATE and DELETE.
76 * The range table may grow still further when we transform the expressions
77 * in the query's quals and target list. (This is possible because in
78 * POSTQUEL, we allowed references to relations not specified in the
79 * from-clause. PostgreSQL keeps this extension to standard SQL.)
82 transformFromClause(ParseState *pstate, List *frmList)
87 * The grammar will have produced a list of RangeVars,
88 * RangeSubselects, RangeFunctions, and/or JoinExprs. Transform each
89 * one (possibly adding entries to the rtable), check for duplicate
90 * refnames, and then add it to the joinlist and namespace.
97 n = transformFromClauseItem(pstate, n, &containedRels);
98 checkNameSpaceConflicts(pstate, (Node *) pstate->p_namespace, n);
99 pstate->p_joinlist = lappend(pstate->p_joinlist, n);
100 pstate->p_namespace = lappend(pstate->p_namespace, n);
106 * Add the target relation of INSERT/UPDATE/DELETE to the range table,
107 * and make the special links to it in the ParseState.
109 * We also open the target relation and acquire a write lock on it.
110 * This must be done before processing the FROM list, in case the target
111 * is also mentioned as a source relation --- we want to be sure to grab
112 * the write lock before any read lock.
114 * If alsoSource is true, add the target to the query's joinlist and
115 * namespace. For INSERT, we don't want the target to be joined to;
116 * it's a destination of tuples, not a source. For UPDATE/DELETE,
117 * we do need to scan or join the target. (NOTE: we do not bother
118 * to check for namespace conflict; we assume that the namespace was
119 * initially empty in these cases.)
121 * Returns the rangetable index of the target relation.
124 setTargetTable(ParseState *pstate, RangeVar *relation,
125 bool inh, bool alsoSource)
130 /* Close old target; this could only happen for multi-action rules */
131 if (pstate->p_target_relation != NULL)
132 heap_close(pstate->p_target_relation, NoLock);
135 * Open target rel and grab suitable lock (which we will hold till end
138 * analyze.c will eventually do the corresponding heap_close(), but *not*
141 pstate->p_target_relation = heap_openrv(relation, RowExclusiveLock);
146 rte = addRangeTableEntry(pstate, relation, NULL, inh, false);
147 pstate->p_target_rangetblentry = rte;
149 /* assume new rte is at end */
150 rtindex = length(pstate->p_rtable);
151 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
154 * Override addRangeTableEntry's default checkForRead, and instead
155 * mark target table as requiring write access.
157 * If we find an explicit reference to the rel later during parse
158 * analysis, scanRTEForColumn will change checkForRead to 'true'
159 * again. That can't happen for INSERT but it is possible for UPDATE
162 rte->checkForRead = false;
163 rte->checkForWrite = true;
166 * If UPDATE/DELETE, add table to joinlist and namespace.
169 addRTEtoQuery(pstate, rte, true, true);
175 * Simplify InhOption (yes/no/default) into boolean yes/no.
177 * The reason we do things this way is that we don't want to examine the
178 * SQL_inheritance option flag until parse_analyze is run. Otherwise,
179 * we'd do the wrong thing with query strings that intermix SET commands
183 interpretInhOption(InhOption inhOpt)
192 return SQL_inheritance;
194 elog(ERROR, "Bogus InhOption value");
195 return false; /* keep compiler quiet */
199 * Extract all not-in-common columns from column lists of a source table
202 extractRemainingColumns(List *common_colnames,
203 List *src_colnames, List *src_colvars,
204 List **res_colnames, List **res_colvars)
206 List *new_colnames = NIL;
207 List *new_colvars = NIL;
209 *lvars = src_colvars;
211 foreach(lnames, src_colnames)
213 char *colname = strVal(lfirst(lnames));
217 foreach(cnames, common_colnames)
219 char *ccolname = strVal(lfirst(cnames));
221 if (strcmp(colname, ccolname) == 0)
230 new_colnames = lappend(new_colnames, lfirst(lnames));
231 new_colvars = lappend(new_colvars, lfirst(lvars));
234 lvars = lnext(lvars);
237 *res_colnames = new_colnames;
238 *res_colvars = new_colvars;
241 /* transformJoinUsingClause()
242 * Build a complete ON clause from a partially-transformed USING list.
243 * We are given lists of nodes representing left and right match columns.
244 * Result is a transformed qualification expression.
247 transformJoinUsingClause(ParseState *pstate, List *leftVars, List *rightVars)
254 * We cheat a little bit here by building an untransformed operator
255 * tree whose leaves are the already-transformed Vars. This is OK
256 * because transformExpr() won't complain about already-transformed
259 foreach(lvars, leftVars)
261 Node *lvar = (Node *) lfirst(lvars);
262 Node *rvar = (Node *) lfirst(rvars);
265 e = makeSimpleA_Expr(AEXPR_OP, "=", copyObject(lvar), copyObject(rvar));
273 a = makeA_Expr(AEXPR_AND, NIL, result, (Node *) e);
277 rvars = lnext(rvars);
281 * Since the references are already Vars, and are certainly from the
282 * input relations, we don't have to go through the same pushups that
283 * transformJoinOnClause() does. Just invoke transformExpr() to fix
284 * up the operators, and we're done.
286 result = transformExpr(pstate, result);
288 result = coerce_to_boolean(result, "JOIN/USING");
291 } /* transformJoinUsingClause() */
293 /* transformJoinOnClause()
294 * Transform the qual conditions for JOIN/ON.
295 * Result is a transformed qualification expression.
298 transformJoinOnClause(ParseState *pstate, JoinExpr *j,
302 List *save_namespace;
303 Relids clause_varnos;
307 * This is a tad tricky, for two reasons. First, the namespace that
308 * the join expression should see is just the two subtrees of the JOIN
309 * plus any outer references from upper pstate levels. So,
310 * temporarily set this pstate's namespace accordingly. (We need not
311 * check for refname conflicts, because transformFromClauseItem()
312 * already did.) NOTE: this code is OK only because the ON clause
313 * can't legally alter the namespace by causing implicit relation refs
316 save_namespace = pstate->p_namespace;
317 pstate->p_namespace = makeList2(j->larg, j->rarg);
319 /* This part is just like transformWhereClause() */
320 result = transformExpr(pstate, j->quals);
322 result = coerce_to_boolean(result, "JOIN/ON");
324 pstate->p_namespace = save_namespace;
327 * Second, we need to check that the ON condition doesn't refer to any
328 * rels outside the input subtrees of the JOIN. It could do that
329 * despite our hack on the namespace if it uses fully-qualified names.
330 * So, grovel through the transformed clause and make sure there are
331 * no bogus references. (Outer references are OK, and are ignored
334 clause_varnos = pull_varnos(result);
335 while ((varno = bms_first_member(clause_varnos)) >= 0)
337 if (!intMember(varno, containedRels))
339 elog(ERROR, "JOIN/ON clause refers to \"%s\", which is not part of JOIN",
340 rt_fetch(varno, pstate->p_rtable)->eref->aliasname);
343 bms_free(clause_varnos);
349 * transformTableEntry --- transform a RangeVar (simple relation reference)
352 transformTableEntry(ParseState *pstate, RangeVar *r)
358 * mark this entry to indicate it comes from the FROM clause. In SQL,
359 * the target list can only refer to range variables specified in the
360 * from clause but we follow the more powerful POSTQUEL semantics and
361 * automatically generate the range variable if not specified. However
362 * there are times we need to know whether the entries are legitimate.
364 rte = addRangeTableEntry(pstate, r, r->alias,
365 interpretInhOption(r->inhOpt), true);
368 * We create a RangeTblRef, but we do not add it to the joinlist or
369 * namespace; our caller must do that if appropriate.
371 rtr = makeNode(RangeTblRef);
372 /* assume new rte is at end */
373 rtr->rtindex = length(pstate->p_rtable);
374 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
381 * transformRangeSubselect --- transform a sub-SELECT appearing in FROM
384 transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
386 List *save_namespace;
393 * We require user to supply an alias for a subselect, per SQL92. To
394 * relax this, we'd have to be prepared to gin up a unique alias for
395 * an unlabeled subselect.
397 if (r->alias == NULL)
398 elog(ERROR, "sub-select in FROM must have an alias");
401 * Analyze and transform the subquery. This is a bit tricky because
402 * we don't want the subquery to be able to see any FROM items already
403 * created in the current query (per SQL92, the scope of a FROM item
404 * does not include other FROM items). But it does need to be able to
405 * see any further-up parent states, so we can't just pass a null
406 * parent pstate link. So, temporarily make the current query level
407 * have an empty namespace.
409 save_namespace = pstate->p_namespace;
410 pstate->p_namespace = NIL;
412 parsetrees = parse_analyze(r->subquery, pstate);
414 pstate->p_namespace = save_namespace;
417 * Check that we got something reasonable. Some of these conditions
418 * are probably impossible given restrictions of the grammar, but
421 if (length(parsetrees) != 1)
422 elog(ERROR, "Unexpected parse analysis result for subselect in FROM");
423 query = (Query *) lfirst(parsetrees);
424 if (query == NULL || !IsA(query, Query))
425 elog(ERROR, "Unexpected parse analysis result for subselect in FROM");
427 if (query->commandType != CMD_SELECT)
428 elog(ERROR, "Expected SELECT query from subselect in FROM");
429 if (query->resultRelation != 0 || query->into != NULL || query->isPortal)
430 elog(ERROR, "Subselect in FROM may not have SELECT INTO");
433 * OK, build an RTE for the subquery.
435 rte = addRangeTableEntryForSubquery(pstate, query, r->alias, true);
438 * We create a RangeTblRef, but we do not add it to the joinlist or
439 * namespace; our caller must do that if appropriate.
441 rtr = makeNode(RangeTblRef);
442 /* assume new rte is at end */
443 rtr->rtindex = length(pstate->p_rtable);
444 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
451 * transformRangeFunction --- transform a function call appearing in FROM
454 transformRangeFunction(ParseState *pstate, RangeFunction *r)
458 List *save_namespace;
462 /* Get function name for possible use as alias */
463 Assert(IsA(r->funccallnode, FuncCall));
464 funcname = strVal(llast(((FuncCall *) r->funccallnode)->funcname));
467 * Transform the raw FuncCall node. This is a bit tricky because we
468 * don't want the function expression to be able to see any FROM items
469 * already created in the current query (compare to
470 * transformRangeSubselect). But it does need to be able to see any
471 * further-up parent states. So, temporarily make the current query
472 * level have an empty namespace. NOTE: this code is OK only because
473 * the expression can't legally alter the namespace by causing
474 * implicit relation refs to be added.
476 save_namespace = pstate->p_namespace;
477 pstate->p_namespace = NIL;
479 funcexpr = transformExpr(pstate, r->funccallnode);
481 pstate->p_namespace = save_namespace;
484 * We still need to check that the function parameters don't refer to
485 * any other rels. That could happen despite our hack on the
486 * namespace if fully-qualified names are used. So, check there are
487 * no local Var references in the transformed expression. (Outer
488 * references are OK, and are ignored here.)
490 if (!bms_is_empty(pull_varnos(funcexpr)))
491 elog(ERROR, "FROM function expression may not refer to other relations of same query level");
494 * Disallow aggregate functions in the expression. (No reason to
495 * postpone this check until parseCheckAggregates.)
497 if (pstate->p_hasAggs)
499 if (contain_agg_clause(funcexpr))
500 elog(ERROR, "cannot use aggregate function in FROM function expression");
504 * If a coldeflist is supplied, ensure it defines a legal set of names
505 * (no duplicates) and datatypes (no pseudo-types, for instance).
511 tupdesc = BuildDescForRelation(r->coldeflist);
512 CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE);
516 * OK, build an RTE for the function.
518 rte = addRangeTableEntryForFunction(pstate, funcname, funcexpr,
522 * We create a RangeTblRef, but we do not add it to the joinlist or
523 * namespace; our caller must do that if appropriate.
525 rtr = makeNode(RangeTblRef);
526 /* assume new rte is at end */
527 rtr->rtindex = length(pstate->p_rtable);
528 Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
535 * transformFromClauseItem -
536 * Transform a FROM-clause item, adding any required entries to the
537 * range table list being built in the ParseState, and return the
538 * transformed item ready to include in the joinlist and namespace.
539 * This routine can recurse to handle SQL92 JOIN expressions.
541 * Aside from the primary return value (the transformed joinlist item)
542 * this routine also returns an integer list of the rangetable indexes
543 * of all the base and join relations represented in the joinlist item.
544 * This list is needed for checking JOIN/ON conditions in higher levels.
547 transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels)
549 if (IsA(n, RangeVar))
551 /* Plain relation reference */
554 rtr = transformTableEntry(pstate, (RangeVar *) n);
555 *containedRels = makeListi1(rtr->rtindex);
558 else if (IsA(n, RangeSubselect))
560 /* sub-SELECT is like a plain relation */
563 rtr = transformRangeSubselect(pstate, (RangeSubselect *) n);
564 *containedRels = makeListi1(rtr->rtindex);
567 else if (IsA(n, RangeFunction))
569 /* function is like a plain relation */
572 rtr = transformRangeFunction(pstate, (RangeFunction *) n);
573 *containedRels = makeListi1(rtr->rtindex);
576 else if (IsA(n, JoinExpr))
578 /* A newfangled join expression */
579 JoinExpr *j = (JoinExpr *) n;
580 List *my_containedRels,
594 * Recursively process the left and right subtrees
596 j->larg = transformFromClauseItem(pstate, j->larg, &l_containedRels);
597 j->rarg = transformFromClauseItem(pstate, j->rarg, &r_containedRels);
600 * Generate combined list of relation indexes for possible use by
601 * transformJoinOnClause below.
603 my_containedRels = nconc(l_containedRels, r_containedRels);
606 * Check for conflicting refnames in left and right subtrees. Must
607 * do this because higher levels will assume I hand back a self-
608 * consistent namespace subtree.
610 checkNameSpaceConflicts(pstate, j->larg, j->rarg);
613 * Extract column name and var lists from both subtrees
615 * Note: expandRTE returns new lists, safe for me to modify
617 if (IsA(j->larg, RangeTblRef))
618 leftrti = ((RangeTblRef *) j->larg)->rtindex;
619 else if (IsA(j->larg, JoinExpr))
620 leftrti = ((JoinExpr *) j->larg)->rtindex;
623 elog(ERROR, "transformFromClauseItem: unexpected subtree type");
624 leftrti = 0; /* keep compiler quiet */
626 rte = rt_fetch(leftrti, pstate->p_rtable);
627 expandRTE(pstate, rte, &l_colnames, &l_colvars);
629 if (IsA(j->rarg, RangeTblRef))
630 rightrti = ((RangeTblRef *) j->rarg)->rtindex;
631 else if (IsA(j->rarg, JoinExpr))
632 rightrti = ((JoinExpr *) j->rarg)->rtindex;
635 elog(ERROR, "transformFromClauseItem: unexpected subtree type");
636 rightrti = 0; /* keep compiler quiet */
638 rte = rt_fetch(rightrti, pstate->p_rtable);
639 expandRTE(pstate, rte, &r_colnames, &r_colvars);
642 * Natural join does not explicitly specify columns; must generate
643 * columns to join. Need to run through the list of columns from
644 * each table or join result and match up the column names. Use
645 * the first table, and check every column in the second table for
646 * a match. (We'll check that the matches were unique later on.)
647 * The result of this step is a list of column names just like an
648 * explicitly-written USING list.
656 Assert(j->using == NIL); /* shouldn't have USING() too */
658 foreach(lx, l_colnames)
660 char *l_colname = strVal(lfirst(lx));
661 Value *m_name = NULL;
663 foreach(rx, r_colnames)
665 char *r_colname = strVal(lfirst(rx));
667 if (strcmp(l_colname, r_colname) == 0)
669 m_name = makeString(l_colname);
674 /* matched a right column? then keep as join column... */
676 rlist = lappend(rlist, m_name);
683 * Now transform the join qualifications, if any.
691 * JOIN/USING (or NATURAL JOIN, as transformed above).
692 * Transform the list into an explicit ON-condition, and
693 * generate a list of merged result columns.
695 List *ucols = j->using;
696 List *l_usingvars = NIL;
697 List *r_usingvars = NIL;
700 Assert(j->quals == NULL); /* shouldn't have ON() too */
704 char *u_colname = strVal(lfirst(ucol));
712 /* Check for USING(foo,foo) */
713 foreach(col, res_colnames)
715 char *res_colname = strVal(lfirst(col));
717 if (strcmp(res_colname, u_colname) == 0)
718 elog(ERROR, "USING column name \"%s\" appears more than once", u_colname);
721 /* Find it in left input */
723 foreach(col, l_colnames)
725 char *l_colname = strVal(lfirst(col));
727 if (strcmp(l_colname, u_colname) == 0)
730 elog(ERROR, "Common column name \"%s\" appears more than once in left table", u_colname);
736 elog(ERROR, "JOIN/USING column \"%s\" not found in left table",
739 /* Find it in right input */
741 foreach(col, r_colnames)
743 char *r_colname = strVal(lfirst(col));
745 if (strcmp(r_colname, u_colname) == 0)
748 elog(ERROR, "Common column name \"%s\" appears more than once in right table", u_colname);
754 elog(ERROR, "JOIN/USING column \"%s\" not found in right table",
757 l_colvar = nth(l_index, l_colvars);
758 l_usingvars = lappend(l_usingvars, l_colvar);
759 r_colvar = nth(r_index, r_colvars);
760 r_usingvars = lappend(r_usingvars, r_colvar);
762 res_colnames = lappend(res_colnames, lfirst(ucol));
763 res_colvars = lappend(res_colvars,
764 buildMergedJoinVar(j->jointype,
769 j->quals = transformJoinUsingClause(pstate,
775 /* User-written ON-condition; transform it */
776 j->quals = transformJoinOnClause(pstate, j, my_containedRels);
780 /* CROSS JOIN: no quals */
783 /* Add remaining columns from each side to the output columns */
784 extractRemainingColumns(res_colnames,
785 l_colnames, l_colvars,
786 &l_colnames, &l_colvars);
787 extractRemainingColumns(res_colnames,
788 r_colnames, r_colvars,
789 &r_colnames, &r_colvars);
790 res_colnames = nconc(res_colnames, l_colnames);
791 res_colvars = nconc(res_colvars, l_colvars);
792 res_colnames = nconc(res_colnames, r_colnames);
793 res_colvars = nconc(res_colvars, r_colvars);
796 * Check alias (AS clause), if any.
800 if (j->alias->colnames != NIL)
802 if (length(j->alias->colnames) > length(res_colnames))
803 elog(ERROR, "Column alias list for \"%s\" has too many entries",
804 j->alias->aliasname);
809 * Now build an RTE for the result of the join
811 rte = addRangeTableEntryForJoin(pstate,
818 /* assume new rte is at end */
819 j->rtindex = length(pstate->p_rtable);
820 Assert(rte == rt_fetch(j->rtindex, pstate->p_rtable));
823 * Include join RTE in returned containedRels list
825 *containedRels = lconsi(j->rtindex, my_containedRels);
830 elog(ERROR, "transformFromClauseItem: unexpected node (internal error)"
831 "\n\t%s", nodeToString(n));
832 return NULL; /* can't get here, just keep compiler
837 * buildMergedJoinVar -
838 * generate a suitable replacement expression for a merged join column
841 buildMergedJoinVar(JoinType jointype, Var *l_colvar, Var *r_colvar)
850 * Choose output type if input types are dissimilar.
852 outcoltype = l_colvar->vartype;
853 outcoltypmod = l_colvar->vartypmod;
854 if (outcoltype != r_colvar->vartype)
856 outcoltype = select_common_type(makeListo2(l_colvar->vartype,
859 outcoltypmod = -1; /* ie, unknown */
861 else if (outcoltypmod != r_colvar->vartypmod)
863 /* same type, but not same typmod */
864 outcoltypmod = -1; /* ie, unknown */
868 * Insert coercion functions if needed. Note that a difference in
869 * typmod can only happen if input has typmod but outcoltypmod is -1.
870 * In that case we insert a RelabelType to clearly mark that result's
871 * typmod is not same as input.
873 if (l_colvar->vartype != outcoltype)
874 l_node = coerce_type((Node *) l_colvar, l_colvar->vartype,
876 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
877 else if (l_colvar->vartypmod != outcoltypmod)
878 l_node = (Node *) makeRelabelType((Expr *) l_colvar,
879 outcoltype, outcoltypmod,
880 COERCE_IMPLICIT_CAST);
882 l_node = (Node *) l_colvar;
884 if (r_colvar->vartype != outcoltype)
885 r_node = coerce_type((Node *) r_colvar, r_colvar->vartype,
887 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
888 else if (r_colvar->vartypmod != outcoltypmod)
889 r_node = (Node *) makeRelabelType((Expr *) r_colvar,
890 outcoltype, outcoltypmod,
891 COERCE_IMPLICIT_CAST);
893 r_node = (Node *) r_colvar;
896 * Choose what to emit
903 * We can use either var; prefer non-coerced one if available.
905 if (IsA(l_node, Var))
907 else if (IsA(r_node, Var))
913 /* Always use left var */
917 /* Always use right var */
923 * Here we must build a COALESCE expression to ensure that
924 * the join output is non-null if either input is.
926 CaseExpr *c = makeNode(CaseExpr);
927 CaseWhen *w = makeNode(CaseWhen);
928 NullTest *n = makeNode(NullTest);
930 n->arg = (Expr *) l_node;
931 n->nulltesttype = IS_NOT_NULL;
932 w->expr = (Expr *) n;
933 w->result = (Expr *) l_node;
934 c->casetype = outcoltype;
935 c->args = makeList1(w);
936 c->defresult = (Expr *) r_node;
937 res_node = (Node *) c;
941 elog(ERROR, "buildMergedJoinVar: unexpected jointype %d",
943 res_node = NULL; /* keep compiler quiet */
952 * transformWhereClause -
953 * transforms the qualification and make sure it is of type Boolean
956 transformWhereClause(ParseState *pstate, Node *clause)
963 qual = transformExpr(pstate, clause);
965 qual = coerce_to_boolean(qual, "WHERE");
972 * findTargetlistEntry -
973 * Returns the targetlist entry matching the given (untransformed) node.
974 * If no matching entry exists, one is created and appended to the target
975 * list as a "resjunk" node.
977 * node the ORDER BY, GROUP BY, or DISTINCT ON expression to be matched
978 * tlist the existing target list (NB: this will never be NIL, which is a
979 * good thing since we'd be unable to append to it if it were...)
980 * clause identifies clause type being processed.
983 findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
985 TargetEntry *target_result = NULL;
990 * Handle two special cases as mandated by the SQL92 spec:
992 * 1. Bare ColumnName (no qualifier or subscripts)
993 * For a bare identifier, we search for a matching column name
994 * in the existing target list. Multiple matches are an error
995 * unless they refer to identical values; for example,
996 * we allow SELECT a, a FROM table ORDER BY a
997 * but not SELECT a AS b, b FROM table ORDER BY b
998 * If no match is found, we fall through and treat the identifier
1000 * For GROUP BY, it is incorrect to match the grouping item against
1001 * targetlist entries: according to SQL92, an identifier in GROUP BY
1002 * is a reference to a column name exposed by FROM, not to a target
1003 * list column. However, many implementations (including pre-7.0
1004 * PostgreSQL) accept this anyway. So for GROUP BY, we look first
1005 * to see if the identifier matches any FROM column name, and only
1006 * try for a targetlist name if it doesn't. This ensures that we
1007 * adhere to the spec in the case where the name could be both.
1008 * DISTINCT ON isn't in the standard, so we can do what we like there;
1009 * we choose to make it work like ORDER BY, on the rather flimsy
1010 * grounds that ordinary DISTINCT works on targetlist entries.
1012 * 2. IntegerConstant
1013 * This means to use the n'th item in the existing target list.
1014 * Note that it would make no sense to order/group/distinct by an
1015 * actual constant, so this does not create a conflict with our
1016 * extension to order/group by an expression.
1017 * GROUP BY column-number is not allowed by SQL92, but since
1018 * the standard has no other behavior defined for this syntax,
1019 * we may as well accept this common extension.
1021 * Note that pre-existing resjunk targets must not be used in either case,
1022 * since the user didn't write them in his SELECT list.
1024 * If neither special case applies, fall through to treat the item as
1028 if (IsA(node, ColumnRef) &&
1029 length(((ColumnRef *) node)->fields) == 1 &&
1030 ((ColumnRef *) node)->indirection == NIL)
1032 char *name = strVal(lfirst(((ColumnRef *) node)->fields));
1034 if (clause == GROUP_CLAUSE)
1037 * In GROUP BY, we must prefer a match against a FROM-clause
1038 * column to one against the targetlist. Look to see if there
1039 * is a matching column. If so, fall through to let
1040 * transformExpr() do the rest. NOTE: if name could refer
1041 * ambiguously to more than one column name exposed by FROM,
1042 * colnameToVar will elog(ERROR). That's just what we want
1045 if (colnameToVar(pstate, name) != NULL)
1053 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1054 Resdom *resnode = tle->resdom;
1056 if (!resnode->resjunk &&
1057 strcmp(resnode->resname, name) == 0)
1059 if (target_result != NULL)
1061 if (!equal(target_result->expr, tle->expr))
1062 elog(ERROR, "%s '%s' is ambiguous",
1063 clauseText[clause], name);
1066 target_result = tle;
1067 /* Stay in loop to check for ambiguity */
1070 if (target_result != NULL)
1071 return target_result; /* return the first match */
1074 if (IsA(node, A_Const))
1076 Value *val = &((A_Const *) node)->val;
1077 int targetlist_pos = 0;
1080 if (!IsA(val, Integer))
1081 elog(ERROR, "Non-integer constant in %s", clauseText[clause]);
1082 target_pos = intVal(val);
1085 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1086 Resdom *resnode = tle->resdom;
1088 if (!resnode->resjunk)
1090 if (++targetlist_pos == target_pos)
1091 return tle; /* return the unique match */
1094 elog(ERROR, "%s position %d is not in target list",
1095 clauseText[clause], target_pos);
1099 * Otherwise, we have an expression (this is a Postgres extension not
1100 * found in SQL92). Convert the untransformed node to a transformed
1101 * expression, and search for a match in the tlist. NOTE: it doesn't
1102 * really matter whether there is more than one match. Also, we are
1103 * willing to match a resjunk target here, though the above cases must
1104 * ignore resjunk targets.
1106 expr = transformExpr(pstate, node);
1110 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1112 if (equal(expr, tle->expr))
1117 * If no matches, construct a new target entry which is appended to
1118 * the end of the target list. This target is given resjunk = TRUE so
1119 * that it will not be projected into the final tuple.
1121 target_result = transformTargetEntry(pstate, node, expr, NULL, true, NULL);
1122 lappend(tlist, target_result);
1124 return target_result;
1129 * transformGroupClause -
1130 * transform a GROUP BY clause
1133 transformGroupClause(ParseState *pstate, List *grouplist, List *targetlist)
1138 foreach(gl, grouplist)
1142 tle = findTargetlistEntry(pstate, lfirst(gl),
1143 targetlist, GROUP_CLAUSE);
1145 /* avoid making duplicate grouplist entries */
1146 if (!targetIsInSortList(tle, glist))
1148 GroupClause *grpcl = makeNode(GroupClause);
1150 grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
1152 grpcl->sortop = ordering_oper_opid(tle->resdom->restype);
1154 glist = lappend(glist, grpcl);
1162 * transformSortClause -
1163 * transform an ORDER BY clause
1166 transformSortClause(ParseState *pstate,
1170 List *sortlist = NIL;
1173 foreach(olitem, orderlist)
1175 SortGroupBy *sortby = lfirst(olitem);
1178 tle = findTargetlistEntry(pstate, sortby->node,
1179 targetlist, ORDER_CLAUSE);
1181 sortlist = addTargetToSortList(tle, sortlist, targetlist,
1189 * transformDistinctClause -
1190 * transform a DISTINCT or DISTINCT ON clause
1192 * Since we may need to add items to the query's sortClause list, that list
1193 * is passed by reference. We might also need to add items to the query's
1194 * targetlist, but we assume that cannot be empty initially, so we can
1195 * lappend to it even though the pointer is passed by value.
1198 transformDistinctClause(ParseState *pstate, List *distinctlist,
1199 List *targetlist, List **sortClause)
1205 /* No work if there was no DISTINCT clause */
1206 if (distinctlist == NIL)
1209 if (lfirst(distinctlist) == NIL)
1211 /* We had SELECT DISTINCT */
1214 * All non-resjunk elements from target list that are not already
1215 * in the sort list should be added to it. (We don't really care
1216 * what order the DISTINCT fields are checked in, so we can leave
1217 * the user's ORDER BY spec alone, and just add additional sort
1218 * keys to it to ensure that all targetlist items get sorted.)
1220 *sortClause = addAllTargetsToSortList(*sortClause, targetlist);
1223 * Now, DISTINCT list consists of all non-resjunk sortlist items.
1224 * Actually, all the sortlist items had better be non-resjunk!
1225 * Otherwise, user wrote SELECT DISTINCT with an ORDER BY item
1226 * that does not appear anywhere in the SELECT targetlist, and we
1227 * can't implement that with only one sorting pass...
1229 foreach(slitem, *sortClause)
1231 SortClause *scl = (SortClause *) lfirst(slitem);
1232 TargetEntry *tle = get_sortgroupclause_tle(scl, targetlist);
1234 if (tle->resdom->resjunk)
1235 elog(ERROR, "For SELECT DISTINCT, ORDER BY expressions must appear in target list");
1237 result = lappend(result, copyObject(scl));
1242 /* We had SELECT DISTINCT ON (expr, ...) */
1245 * If the user writes both DISTINCT ON and ORDER BY, then the two
1246 * expression lists must match (until one or the other runs out).
1247 * Otherwise the ORDER BY requires a different sort order than the
1248 * DISTINCT does, and we can't implement that with only one sort
1249 * pass (and if we do two passes, the results will be rather
1250 * unpredictable). However, it's OK to have more DISTINCT ON
1251 * expressions than ORDER BY expressions; we can just add the
1252 * extra DISTINCT values to the sort list, much as we did above
1253 * for ordinary DISTINCT fields.
1255 * Actually, it'd be OK for the common prefixes of the two lists to
1256 * match in any order, but implementing that check seems like more
1257 * trouble than it's worth.
1259 List *nextsortlist = *sortClause;
1261 foreach(dlitem, distinctlist)
1265 tle = findTargetlistEntry(pstate, lfirst(dlitem),
1266 targetlist, DISTINCT_ON_CLAUSE);
1268 if (nextsortlist != NIL)
1270 SortClause *scl = (SortClause *) lfirst(nextsortlist);
1272 if (tle->resdom->ressortgroupref != scl->tleSortGroupRef)
1273 elog(ERROR, "SELECT DISTINCT ON expressions must match initial ORDER BY expressions");
1274 result = lappend(result, copyObject(scl));
1275 nextsortlist = lnext(nextsortlist);
1279 *sortClause = addTargetToSortList(tle, *sortClause,
1283 * Probably, the tle should always have been added at the
1284 * end of the sort list ... but search to be safe.
1286 foreach(slitem, *sortClause)
1288 SortClause *scl = (SortClause *) lfirst(slitem);
1290 if (tle->resdom->ressortgroupref == scl->tleSortGroupRef)
1292 result = lappend(result, copyObject(scl));
1297 elog(ERROR, "transformDistinctClause: failed to add DISTINCT ON clause to target list");
1306 * addAllTargetsToSortList
1307 * Make sure all non-resjunk targets in the targetlist are in the
1308 * ORDER BY list, adding the not-yet-sorted ones to the end of the list.
1309 * This is typically used to help implement SELECT DISTINCT.
1311 * Returns the updated ORDER BY list.
1314 addAllTargetsToSortList(List *sortlist, List *targetlist)
1318 foreach(i, targetlist)
1320 TargetEntry *tle = (TargetEntry *) lfirst(i);
1322 if (!tle->resdom->resjunk)
1323 sortlist = addTargetToSortList(tle, sortlist, targetlist, NIL);
1329 * addTargetToSortList
1330 * If the given targetlist entry isn't already in the ORDER BY list,
1331 * add it to the end of the list, using the sortop with given name
1332 * or the default sort operator if opname == NIL.
1334 * Returns the updated ORDER BY list.
1337 addTargetToSortList(TargetEntry *tle, List *sortlist, List *targetlist,
1340 /* avoid making duplicate sortlist entries */
1341 if (!targetIsInSortList(tle, sortlist))
1343 SortClause *sortcl = makeNode(SortClause);
1345 sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
1348 sortcl->sortop = compatible_oper_opid(opname,
1349 tle->resdom->restype,
1350 tle->resdom->restype,
1353 sortcl->sortop = ordering_oper_opid(tle->resdom->restype);
1355 sortlist = lappend(sortlist, sortcl);
1361 * assignSortGroupRef
1362 * Assign the targetentry an unused ressortgroupref, if it doesn't
1363 * already have one. Return the assigned or pre-existing refnumber.
1365 * 'tlist' is the targetlist containing (or to contain) the given targetentry.
1368 assignSortGroupRef(TargetEntry *tle, List *tlist)
1373 if (tle->resdom->ressortgroupref) /* already has one? */
1374 return tle->resdom->ressortgroupref;
1376 /* easiest way to pick an unused refnumber: max used + 1 */
1380 Index ref = ((TargetEntry *) lfirst(l))->resdom->ressortgroupref;
1385 tle->resdom->ressortgroupref = maxRef + 1;
1386 return tle->resdom->ressortgroupref;
1390 * targetIsInSortList
1391 * Is the given target item already in the sortlist?
1393 * Works for both SortClause and GroupClause lists. Note that the main
1394 * reason we need this routine (and not just a quick test for nonzeroness
1395 * of ressortgroupref) is that a TLE might be in only one of the lists.
1398 targetIsInSortList(TargetEntry *tle, List *sortList)
1400 Index ref = tle->resdom->ressortgroupref;
1403 /* no need to scan list if tle has no marker */
1407 foreach(i, sortList)
1409 SortClause *scl = (SortClause *) lfirst(i);
1411 if (scl->tleSortGroupRef == ref)