1 /*-------------------------------------------------------------------------
4 * handle clauses in parser
6 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/parser/parse_clause.c
13 *-------------------------------------------------------------------------
18 #include "access/heapam.h"
19 #include "catalog/heap.h"
20 #include "catalog/pg_type.h"
21 #include "commands/defrem.h"
22 #include "nodes/makefuncs.h"
23 #include "nodes/nodeFuncs.h"
24 #include "optimizer/tlist.h"
25 #include "parser/analyze.h"
26 #include "parser/parsetree.h"
27 #include "parser/parse_clause.h"
28 #include "parser/parse_coerce.h"
29 #include "parser/parse_collate.h"
30 #include "parser/parse_expr.h"
31 #include "parser/parse_oper.h"
32 #include "parser/parse_relation.h"
33 #include "parser/parse_target.h"
34 #include "rewrite/rewriteManip.h"
35 #include "utils/guc.h"
36 #include "utils/lsyscache.h"
37 #include "utils/rel.h"
40 /* Convenience macro for the most common makeNamespaceItem() case */
41 #define makeDefaultNSItem(rte) makeNamespaceItem(rte, true, true, false, true)
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 RangeTblEntry *leftRTE, RangeTblEntry *rightRTE,
48 List *leftVars, List *rightVars);
49 static Node *transformJoinOnClause(ParseState *pstate, JoinExpr *j,
51 static RangeTblEntry *transformTableEntry(ParseState *pstate, RangeVar *r);
52 static RangeTblEntry *transformCTEReference(ParseState *pstate, RangeVar *r,
53 CommonTableExpr *cte, Index levelsup);
54 static RangeTblEntry *transformRangeSubselect(ParseState *pstate,
56 static RangeTblEntry *transformRangeFunction(ParseState *pstate,
58 static Node *transformFromClauseItem(ParseState *pstate, Node *n,
59 RangeTblEntry **top_rte, int *top_rti,
61 static Node *buildMergedJoinVar(ParseState *pstate, JoinType jointype,
62 Var *l_colvar, Var *r_colvar);
63 static ParseNamespaceItem *makeNamespaceItem(RangeTblEntry *rte,
64 bool rel_visible, bool cols_visible,
65 bool lateral_only, bool lateral_ok);
66 static void setNamespaceColumnVisibility(List *namespace, bool cols_visible);
67 static void setNamespaceLateralState(List *namespace,
68 bool lateral_only, bool lateral_ok);
69 static void checkExprIsVarFree(ParseState *pstate, Node *n,
70 const char *constructName);
71 static TargetEntry *findTargetlistEntrySQL92(ParseState *pstate, Node *node,
72 List **tlist, ParseExprKind exprKind);
73 static TargetEntry *findTargetlistEntrySQL99(ParseState *pstate, Node *node,
74 List **tlist, ParseExprKind exprKind);
75 static int get_matching_location(int sortgroupref,
76 List *sortgrouprefs, List *exprs);
77 static List *addTargetToSortList(ParseState *pstate, TargetEntry *tle,
78 List *sortlist, List *targetlist, SortBy *sortby,
80 static List *addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
81 List *grouplist, List *targetlist, int location,
83 static WindowClause *findWindowClause(List *wclist, const char *name);
84 static Node *transformFrameOffset(ParseState *pstate, int frameOptions,
89 * transformFromClause -
90 * Process the FROM clause and add items to the query's range table,
91 * joinlist, and namespace.
93 * Note: we assume that the pstate's p_rtable, p_joinlist, and p_namespace
94 * lists were initialized to NIL when the pstate was created.
95 * We will add onto any entries already present --- this is needed for rule
96 * processing, as well as for UPDATE and DELETE.
99 transformFromClause(ParseState *pstate, List *frmList)
104 * The grammar will have produced a list of RangeVars, RangeSubselects,
105 * RangeFunctions, and/or JoinExprs. Transform each one (possibly adding
106 * entries to the rtable), check for duplicate refnames, and then add it
107 * to the joinlist and namespace.
109 * Note we must process the items left-to-right for proper handling of
110 * LATERAL references.
114 Node *n = lfirst(fl);
119 n = transformFromClauseItem(pstate, n,
124 checkNameSpaceConflicts(pstate, pstate->p_namespace, namespace);
126 /* Mark the new namespace items as visible only to LATERAL */
127 setNamespaceLateralState(namespace, true, true);
129 pstate->p_joinlist = lappend(pstate->p_joinlist, n);
130 pstate->p_namespace = list_concat(pstate->p_namespace, namespace);
134 * We're done parsing the FROM list, so make all namespace items
135 * unconditionally visible. Note that this will also reset lateral_only
136 * for any namespace items that were already present when we were called;
137 * but those should have been that way already.
139 setNamespaceLateralState(pstate->p_namespace, false, true);
144 * Add the target relation of INSERT/UPDATE/DELETE to the range table,
145 * and make the special links to it in the ParseState.
147 * We also open the target relation and acquire a write lock on it.
148 * This must be done before processing the FROM list, in case the target
149 * is also mentioned as a source relation --- we want to be sure to grab
150 * the write lock before any read lock.
152 * If alsoSource is true, add the target to the query's joinlist and
153 * namespace. For INSERT, we don't want the target to be joined to;
154 * it's a destination of tuples, not a source. For UPDATE/DELETE,
155 * we do need to scan or join the target. (NOTE: we do not bother
156 * to check for namespace conflict; we assume that the namespace was
157 * initially empty in these cases.)
159 * Finally, we mark the relation as requiring the permissions specified
162 * Returns the rangetable index of the target relation.
165 setTargetTable(ParseState *pstate, RangeVar *relation,
166 bool inh, bool alsoSource, AclMode requiredPerms)
171 /* Close old target; this could only happen for multi-action rules */
172 if (pstate->p_target_relation != NULL)
173 heap_close(pstate->p_target_relation, NoLock);
176 * Open target rel and grab suitable lock (which we will hold till end of
179 * free_parsestate() will eventually do the corresponding heap_close(),
180 * but *not* release the lock.
182 pstate->p_target_relation = parserOpenTable(pstate, relation,
188 rte = addRangeTableEntryForRelation(pstate, pstate->p_target_relation,
189 relation->alias, inh, false);
190 pstate->p_target_rangetblentry = rte;
192 /* assume new rte is at end */
193 rtindex = list_length(pstate->p_rtable);
194 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
197 * Override addRangeTableEntry's default ACL_SELECT permissions check, and
198 * instead mark target table as requiring exactly the specified
201 * If we find an explicit reference to the rel later during parse
202 * analysis, we will add the ACL_SELECT bit back again; see
203 * markVarForSelectPriv and its callers.
205 rte->requiredPerms = requiredPerms;
208 * If UPDATE/DELETE, add table to joinlist and namespace.
211 addRTEtoQuery(pstate, rte, true, true, true);
217 * Simplify InhOption (yes/no/default) into boolean yes/no.
219 * The reason we do things this way is that we don't want to examine the
220 * SQL_inheritance option flag until parse_analyze() is run. Otherwise,
221 * we'd do the wrong thing with query strings that intermix SET commands
225 interpretInhOption(InhOption inhOpt)
234 return SQL_inheritance;
236 elog(ERROR, "bogus InhOption value: %d", inhOpt);
237 return false; /* keep compiler quiet */
241 * Given a relation-options list (of DefElems), return true iff the specified
242 * table/result set should be created with OIDs. This needs to be done after
243 * parsing the query string because the return value can depend upon the
244 * default_with_oids GUC var.
246 * In some situations, we want to reject an OIDS option even if it's present.
247 * That's (rather messily) handled here rather than reloptions.c, because that
248 * code explicitly punts checking for oids to here.
251 interpretOidsOption(List *defList, bool allowOids)
255 /* Scan list to see if OIDS was included */
256 foreach(cell, defList)
258 DefElem *def = (DefElem *) lfirst(cell);
260 if (def->defnamespace == NULL &&
261 pg_strcasecmp(def->defname, "oids") == 0)
265 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
266 errmsg("unrecognized parameter \"%s\"",
268 return defGetBoolean(def);
272 /* Force no-OIDS result if caller disallows OIDS. */
276 /* OIDS option was not specified, so use default. */
277 return default_with_oids;
281 * Extract all not-in-common columns from column lists of a source table
284 extractRemainingColumns(List *common_colnames,
285 List *src_colnames, List *src_colvars,
286 List **res_colnames, List **res_colvars)
288 List *new_colnames = NIL;
289 List *new_colvars = NIL;
293 Assert(list_length(src_colnames) == list_length(src_colvars));
295 forboth(lnames, src_colnames, lvars, src_colvars)
297 char *colname = strVal(lfirst(lnames));
301 foreach(cnames, common_colnames)
303 char *ccolname = strVal(lfirst(cnames));
305 if (strcmp(colname, ccolname) == 0)
314 new_colnames = lappend(new_colnames, lfirst(lnames));
315 new_colvars = lappend(new_colvars, lfirst(lvars));
319 *res_colnames = new_colnames;
320 *res_colvars = new_colvars;
323 /* transformJoinUsingClause()
324 * Build a complete ON clause from a partially-transformed USING list.
325 * We are given lists of nodes representing left and right match columns.
326 * Result is a transformed qualification expression.
329 transformJoinUsingClause(ParseState *pstate,
330 RangeTblEntry *leftRTE, RangeTblEntry *rightRTE,
331 List *leftVars, List *rightVars)
338 * We cheat a little bit here by building an untransformed operator tree
339 * whose leaves are the already-transformed Vars. This is OK because
340 * transformExpr() won't complain about already-transformed subnodes.
341 * However, this does mean that we have to mark the columns as requiring
342 * SELECT privilege for ourselves; transformExpr() won't do it.
344 forboth(lvars, leftVars, rvars, rightVars)
346 Var *lvar = (Var *) lfirst(lvars);
347 Var *rvar = (Var *) lfirst(rvars);
350 /* Require read access to the join variables */
351 markVarForSelectPriv(pstate, lvar, leftRTE);
352 markVarForSelectPriv(pstate, rvar, rightRTE);
354 /* Now create the lvar = rvar join condition */
355 e = makeSimpleA_Expr(AEXPR_OP, "=",
356 copyObject(lvar), copyObject(rvar),
359 /* And combine into an AND clause, if multiple join columns */
366 a = makeA_Expr(AEXPR_AND, NIL, result, (Node *) e, -1);
372 * Since the references are already Vars, and are certainly from the input
373 * relations, we don't have to go through the same pushups that
374 * transformJoinOnClause() does. Just invoke transformExpr() to fix up
375 * the operators, and we're done.
377 result = transformExpr(pstate, result, EXPR_KIND_JOIN_USING);
379 result = coerce_to_boolean(pstate, result, "JOIN/USING");
384 /* transformJoinOnClause()
385 * Transform the qual conditions for JOIN/ON.
386 * Result is a transformed qualification expression.
389 transformJoinOnClause(ParseState *pstate, JoinExpr *j, List *namespace)
392 List *save_namespace;
395 * The namespace that the join expression should see is just the two
396 * subtrees of the JOIN plus any outer references from upper pstate
397 * levels. Temporarily set this pstate's namespace accordingly. (We need
398 * not check for refname conflicts, because transformFromClauseItem()
399 * already did.) All namespace items are marked visible regardless of
402 setNamespaceLateralState(namespace, false, true);
404 save_namespace = pstate->p_namespace;
405 pstate->p_namespace = namespace;
407 result = transformWhereClause(pstate, j->quals,
408 EXPR_KIND_JOIN_ON, "JOIN/ON");
410 pstate->p_namespace = save_namespace;
416 * transformTableEntry --- transform a RangeVar (simple relation reference)
418 static RangeTblEntry *
419 transformTableEntry(ParseState *pstate, RangeVar *r)
423 /* We need only build a range table entry */
424 rte = addRangeTableEntry(pstate, r, r->alias,
425 interpretInhOption(r->inhOpt), true);
431 * transformCTEReference --- transform a RangeVar that references a common
432 * table expression (ie, a sub-SELECT defined in a WITH clause)
434 static RangeTblEntry *
435 transformCTEReference(ParseState *pstate, RangeVar *r,
436 CommonTableExpr *cte, Index levelsup)
440 rte = addRangeTableEntryForCTE(pstate, cte, levelsup, r, true);
446 * transformRangeSubselect --- transform a sub-SELECT appearing in FROM
448 static RangeTblEntry *
449 transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
455 * We require user to supply an alias for a subselect, per SQL92. To relax
456 * this, we'd have to be prepared to gin up a unique alias for an
457 * unlabeled subselect. (This is just elog, not ereport, because the
458 * grammar should have enforced it already. It'd probably be better to
459 * report the error here, but we don't have a good error location here.)
461 if (r->alias == NULL)
462 elog(ERROR, "subquery in FROM must have an alias");
465 * Set p_expr_kind to show this parse level is recursing to a subselect.
466 * We can't be nested within any expression, so don't need save-restore
469 Assert(pstate->p_expr_kind == EXPR_KIND_NONE);
470 pstate->p_expr_kind = EXPR_KIND_FROM_SUBSELECT;
473 * If the subselect is LATERAL, make lateral_only names of this level
474 * visible to it. (LATERAL can't nest within a single pstate level, so we
475 * don't need save/restore logic here.)
477 Assert(!pstate->p_lateral_active);
478 pstate->p_lateral_active = r->lateral;
481 * Analyze and transform the subquery.
483 query = parse_sub_analyze(r->subquery, pstate, NULL,
484 isLockedRefname(pstate, r->alias->aliasname));
487 pstate->p_lateral_active = false;
488 pstate->p_expr_kind = EXPR_KIND_NONE;
491 * Check that we got something reasonable. Many of these conditions are
492 * impossible given restrictions of the grammar, but check 'em anyway.
494 if (!IsA(query, Query) ||
495 query->commandType != CMD_SELECT ||
496 query->utilityStmt != NULL)
497 elog(ERROR, "unexpected non-SELECT command in subquery in FROM");
500 * OK, build an RTE for the subquery.
502 rte = addRangeTableEntryForSubquery(pstate,
513 * transformRangeFunction --- transform a function call appearing in FROM
515 static RangeTblEntry *
516 transformRangeFunction(ParseState *pstate, RangeFunction *r)
524 * Get function name for possible use as alias. We use the same
525 * transformation rules as for a SELECT output expression. For a FuncCall
526 * node, the result will be the function name, but it is possible for the
527 * grammar to hand back other node types.
529 funcname = FigureColname(r->funccallnode);
532 * We make lateral_only names of this level visible, whether or not the
533 * function is explicitly marked LATERAL. This is needed for SQL spec
534 * compliance in the case of UNNEST(), and seems useful on convenience
535 * grounds for all functions in FROM.
537 * (LATERAL can't nest within a single pstate level, so we don't need
538 * save/restore logic here.)
540 Assert(!pstate->p_lateral_active);
541 pstate->p_lateral_active = true;
544 * Transform the raw expression.
546 funcexpr = transformExpr(pstate, r->funccallnode, EXPR_KIND_FROM_FUNCTION);
548 pstate->p_lateral_active = false;
551 * We must assign collations now so that we can fill funccolcollations.
553 assign_expr_collations(pstate, funcexpr);
556 * Mark the RTE as LATERAL if the user said LATERAL explicitly, or if
557 * there are any lateral cross-references in it.
559 is_lateral = r->lateral || contain_vars_of_level(funcexpr, 0);
562 * OK, build an RTE for the function.
564 rte = addRangeTableEntryForFunction(pstate, funcname, funcexpr,
565 r, is_lateral, true);
568 * If a coldeflist was supplied, ensure it defines a legal set of names
569 * (no duplicates) and datatypes (no pseudo-types, for instance).
570 * addRangeTableEntryForFunction looked up the type names but didn't check
571 * them further than that.
577 tupdesc = BuildDescFromLists(rte->eref->colnames,
580 rte->funccolcollations);
581 CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE, false);
589 * transformFromClauseItem -
590 * Transform a FROM-clause item, adding any required entries to the
591 * range table list being built in the ParseState, and return the
592 * transformed item ready to include in the joinlist. Also build a
593 * ParseNamespaceItem list describing the names exposed by this item.
594 * This routine can recurse to handle SQL92 JOIN expressions.
596 * The function return value is the node to add to the jointree (a
597 * RangeTblRef or JoinExpr). Additional output parameters are:
599 * *top_rte: receives the RTE corresponding to the jointree item.
600 * (We could extract this from the function return node, but it saves cycles
601 * to pass it back separately.)
603 * *top_rti: receives the rangetable index of top_rte. (Ditto.)
605 * *namespace: receives a List of ParseNamespaceItems for the RTEs exposed
606 * as table/column names by this item. (The lateral_only flags in these items
607 * are indeterminate and should be explicitly set by the caller before use.)
610 transformFromClauseItem(ParseState *pstate, Node *n,
611 RangeTblEntry **top_rte, int *top_rti,
614 if (IsA(n, RangeVar))
616 /* Plain relation reference, or perhaps a CTE reference */
617 RangeVar *rv = (RangeVar *) n;
619 RangeTblEntry *rte = NULL;
622 /* if it is an unqualified name, it might be a CTE reference */
625 CommonTableExpr *cte;
628 cte = scanNameSpaceForCTE(pstate, rv->relname, &levelsup);
630 rte = transformCTEReference(pstate, rv, cte, levelsup);
633 /* if not found as a CTE, must be a table reference */
635 rte = transformTableEntry(pstate, rv);
637 /* assume new rte is at end */
638 rtindex = list_length(pstate->p_rtable);
639 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
642 *namespace = list_make1(makeDefaultNSItem(rte));
643 rtr = makeNode(RangeTblRef);
644 rtr->rtindex = rtindex;
647 else if (IsA(n, RangeSubselect))
649 /* sub-SELECT is like a plain relation */
654 rte = transformRangeSubselect(pstate, (RangeSubselect *) n);
655 /* assume new rte is at end */
656 rtindex = list_length(pstate->p_rtable);
657 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
660 *namespace = list_make1(makeDefaultNSItem(rte));
661 rtr = makeNode(RangeTblRef);
662 rtr->rtindex = rtindex;
665 else if (IsA(n, RangeFunction))
667 /* function is like a plain relation */
672 rte = transformRangeFunction(pstate, (RangeFunction *) n);
673 /* assume new rte is at end */
674 rtindex = list_length(pstate->p_rtable);
675 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
678 *namespace = list_make1(makeDefaultNSItem(rte));
679 rtr = makeNode(RangeTblRef);
680 rtr->rtindex = rtindex;
683 else if (IsA(n, JoinExpr))
685 /* A newfangled join expression */
686 JoinExpr *j = (JoinExpr *) n;
687 RangeTblEntry *l_rte;
688 RangeTblEntry *r_rte;
701 int sv_namespace_length;
706 * Recursively process the left subtree, then the right. We must do
707 * it in this order for correct visibility of LATERAL references.
709 j->larg = transformFromClauseItem(pstate, j->larg,
715 * Make the left-side RTEs available for LATERAL access within the
716 * right side, by temporarily adding them to the pstate's namespace
717 * list. Per SQL:2008, if the join type is not INNER or LEFT then the
718 * left-side names must still be exposed, but it's an error to
719 * reference them. (Stupid design, but that's what it says.) Hence,
720 * we always push them into the namespace, but mark them as not
721 * lateral_ok if the jointype is wrong.
723 * NB: this coding relies on the fact that list_concat is not
724 * destructive to its second argument.
726 lateral_ok = (j->jointype == JOIN_INNER || j->jointype == JOIN_LEFT);
727 setNamespaceLateralState(l_namespace, true, lateral_ok);
729 checkNameSpaceConflicts(pstate, pstate->p_namespace, l_namespace);
731 sv_namespace_length = list_length(pstate->p_namespace);
732 pstate->p_namespace = list_concat(pstate->p_namespace, l_namespace);
734 /* And now we can process the RHS */
735 j->rarg = transformFromClauseItem(pstate, j->rarg,
740 /* Remove the left-side RTEs from the namespace list again */
741 pstate->p_namespace = list_truncate(pstate->p_namespace,
742 sv_namespace_length);
745 * Check for conflicting refnames in left and right subtrees. Must do
746 * this because higher levels will assume I hand back a self-
747 * consistent namespace list.
749 checkNameSpaceConflicts(pstate, l_namespace, r_namespace);
752 * Generate combined namespace info for possible use below.
754 my_namespace = list_concat(l_namespace, r_namespace);
757 * Extract column name and var lists from both subtrees
759 * Note: expandRTE returns new lists, safe for me to modify
761 expandRTE(l_rte, l_rtindex, 0, -1, false,
762 &l_colnames, &l_colvars);
763 expandRTE(r_rte, r_rtindex, 0, -1, false,
764 &r_colnames, &r_colvars);
767 * Natural join does not explicitly specify columns; must generate
768 * columns to join. Need to run through the list of columns from each
769 * table or join result and match up the column names. Use the first
770 * table, and check every column in the second table for a match.
771 * (We'll check that the matches were unique later on.) The result of
772 * this step is a list of column names just like an explicitly-written
781 Assert(j->usingClause == NIL); /* shouldn't have USING() too */
783 foreach(lx, l_colnames)
785 char *l_colname = strVal(lfirst(lx));
786 Value *m_name = NULL;
788 foreach(rx, r_colnames)
790 char *r_colname = strVal(lfirst(rx));
792 if (strcmp(l_colname, r_colname) == 0)
794 m_name = makeString(l_colname);
799 /* matched a right column? then keep as join column... */
801 rlist = lappend(rlist, m_name);
804 j->usingClause = rlist;
808 * Now transform the join qualifications, if any.
816 * JOIN/USING (or NATURAL JOIN, as transformed above). Transform
817 * the list into an explicit ON-condition, and generate a list of
818 * merged result columns.
820 List *ucols = j->usingClause;
821 List *l_usingvars = NIL;
822 List *r_usingvars = NIL;
825 Assert(j->quals == NULL); /* shouldn't have ON() too */
829 char *u_colname = strVal(lfirst(ucol));
837 /* Check for USING(foo,foo) */
838 foreach(col, res_colnames)
840 char *res_colname = strVal(lfirst(col));
842 if (strcmp(res_colname, u_colname) == 0)
844 (errcode(ERRCODE_DUPLICATE_COLUMN),
845 errmsg("column name \"%s\" appears more than once in USING clause",
849 /* Find it in left input */
851 foreach(col, l_colnames)
853 char *l_colname = strVal(lfirst(col));
855 if (strcmp(l_colname, u_colname) == 0)
859 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
860 errmsg("common column name \"%s\" appears more than once in left table",
868 (errcode(ERRCODE_UNDEFINED_COLUMN),
869 errmsg("column \"%s\" specified in USING clause does not exist in left table",
872 /* Find it in right input */
874 foreach(col, r_colnames)
876 char *r_colname = strVal(lfirst(col));
878 if (strcmp(r_colname, u_colname) == 0)
882 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
883 errmsg("common column name \"%s\" appears more than once in right table",
891 (errcode(ERRCODE_UNDEFINED_COLUMN),
892 errmsg("column \"%s\" specified in USING clause does not exist in right table",
895 l_colvar = list_nth(l_colvars, l_index);
896 l_usingvars = lappend(l_usingvars, l_colvar);
897 r_colvar = list_nth(r_colvars, r_index);
898 r_usingvars = lappend(r_usingvars, r_colvar);
900 res_colnames = lappend(res_colnames, lfirst(ucol));
901 res_colvars = lappend(res_colvars,
902 buildMergedJoinVar(pstate,
908 j->quals = transformJoinUsingClause(pstate,
916 /* User-written ON-condition; transform it */
917 j->quals = transformJoinOnClause(pstate, j, my_namespace);
921 /* CROSS JOIN: no quals */
924 /* Add remaining columns from each side to the output columns */
925 extractRemainingColumns(res_colnames,
926 l_colnames, l_colvars,
927 &l_colnames, &l_colvars);
928 extractRemainingColumns(res_colnames,
929 r_colnames, r_colvars,
930 &r_colnames, &r_colvars);
931 res_colnames = list_concat(res_colnames, l_colnames);
932 res_colvars = list_concat(res_colvars, l_colvars);
933 res_colnames = list_concat(res_colnames, r_colnames);
934 res_colvars = list_concat(res_colvars, r_colvars);
937 * Check alias (AS clause), if any.
941 if (j->alias->colnames != NIL)
943 if (list_length(j->alias->colnames) > list_length(res_colnames))
945 (errcode(ERRCODE_SYNTAX_ERROR),
946 errmsg("column alias list for \"%s\" has too many entries",
947 j->alias->aliasname)));
952 * Now build an RTE for the result of the join
954 rte = addRangeTableEntryForJoin(pstate,
961 /* assume new rte is at end */
962 j->rtindex = list_length(pstate->p_rtable);
963 Assert(rte == rt_fetch(j->rtindex, pstate->p_rtable));
966 *top_rti = j->rtindex;
968 /* make a matching link to the JoinExpr for later use */
969 for (k = list_length(pstate->p_joinexprs) + 1; k < j->rtindex; k++)
970 pstate->p_joinexprs = lappend(pstate->p_joinexprs, NULL);
971 pstate->p_joinexprs = lappend(pstate->p_joinexprs, j);
972 Assert(list_length(pstate->p_joinexprs) == j->rtindex);
975 * Prepare returned namespace list. If the JOIN has an alias then it
976 * hides the contained RTEs completely; otherwise, the contained RTEs
977 * are still visible as table names, but are not visible for
978 * unqualified column-name access.
980 * Note: if there are nested alias-less JOINs, the lower-level ones
981 * will remain in the list although they have neither p_rel_visible
982 * nor p_cols_visible set. We could delete such list items, but it's
983 * unclear that it's worth expending cycles to do so.
985 if (j->alias != NULL)
988 setNamespaceColumnVisibility(my_namespace, false);
991 * The join RTE itself is always made visible for unqualified column
992 * names. It's visible as a relation name only if it has an alias.
994 *namespace = lappend(my_namespace,
995 makeNamespaceItem(rte,
1004 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(n));
1005 return NULL; /* can't get here, keep compiler quiet */
1009 * buildMergedJoinVar -
1010 * generate a suitable replacement expression for a merged join column
1013 buildMergedJoinVar(ParseState *pstate, JoinType jointype,
1014 Var *l_colvar, Var *r_colvar)
1023 * Choose output type if input types are dissimilar.
1025 outcoltype = l_colvar->vartype;
1026 outcoltypmod = l_colvar->vartypmod;
1027 if (outcoltype != r_colvar->vartype)
1029 outcoltype = select_common_type(pstate,
1030 list_make2(l_colvar, r_colvar),
1033 outcoltypmod = -1; /* ie, unknown */
1035 else if (outcoltypmod != r_colvar->vartypmod)
1037 /* same type, but not same typmod */
1038 outcoltypmod = -1; /* ie, unknown */
1042 * Insert coercion functions if needed. Note that a difference in typmod
1043 * can only happen if input has typmod but outcoltypmod is -1. In that
1044 * case we insert a RelabelType to clearly mark that result's typmod is
1045 * not same as input. We never need coerce_type_typmod.
1047 if (l_colvar->vartype != outcoltype)
1048 l_node = coerce_type(pstate, (Node *) l_colvar, l_colvar->vartype,
1049 outcoltype, outcoltypmod,
1050 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1);
1051 else if (l_colvar->vartypmod != outcoltypmod)
1052 l_node = (Node *) makeRelabelType((Expr *) l_colvar,
1053 outcoltype, outcoltypmod,
1054 InvalidOid, /* fixed below */
1055 COERCE_IMPLICIT_CAST);
1057 l_node = (Node *) l_colvar;
1059 if (r_colvar->vartype != outcoltype)
1060 r_node = coerce_type(pstate, (Node *) r_colvar, r_colvar->vartype,
1061 outcoltype, outcoltypmod,
1062 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1);
1063 else if (r_colvar->vartypmod != outcoltypmod)
1064 r_node = (Node *) makeRelabelType((Expr *) r_colvar,
1065 outcoltype, outcoltypmod,
1066 InvalidOid, /* fixed below */
1067 COERCE_IMPLICIT_CAST);
1069 r_node = (Node *) r_colvar;
1072 * Choose what to emit
1079 * We can use either var; prefer non-coerced one if available.
1081 if (IsA(l_node, Var))
1083 else if (IsA(r_node, Var))
1089 /* Always use left var */
1093 /* Always use right var */
1099 * Here we must build a COALESCE expression to ensure that the
1100 * join output is non-null if either input is.
1102 CoalesceExpr *c = makeNode(CoalesceExpr);
1104 c->coalescetype = outcoltype;
1105 /* coalescecollid will get set below */
1106 c->args = list_make2(l_node, r_node);
1108 res_node = (Node *) c;
1112 elog(ERROR, "unrecognized join type: %d", (int) jointype);
1113 res_node = NULL; /* keep compiler quiet */
1118 * Apply assign_expr_collations to fix up the collation info in the
1119 * coercion and CoalesceExpr nodes, if we made any. This must be done now
1120 * so that the join node's alias vars show correct collation info.
1122 assign_expr_collations(pstate, res_node);
1128 * makeNamespaceItem -
1129 * Convenience subroutine to construct a ParseNamespaceItem.
1131 static ParseNamespaceItem *
1132 makeNamespaceItem(RangeTblEntry *rte, bool rel_visible, bool cols_visible,
1133 bool lateral_only, bool lateral_ok)
1135 ParseNamespaceItem *nsitem;
1137 nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1138 nsitem->p_rte = rte;
1139 nsitem->p_rel_visible = rel_visible;
1140 nsitem->p_cols_visible = cols_visible;
1141 nsitem->p_lateral_only = lateral_only;
1142 nsitem->p_lateral_ok = lateral_ok;
1147 * setNamespaceColumnVisibility -
1148 * Convenience subroutine to update cols_visible flags in a namespace list.
1151 setNamespaceColumnVisibility(List *namespace, bool cols_visible)
1155 foreach(lc, namespace)
1157 ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
1159 nsitem->p_cols_visible = cols_visible;
1164 * setNamespaceLateralState -
1165 * Convenience subroutine to update LATERAL flags in a namespace list.
1168 setNamespaceLateralState(List *namespace, bool lateral_only, bool lateral_ok)
1172 foreach(lc, namespace)
1174 ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
1176 nsitem->p_lateral_only = lateral_only;
1177 nsitem->p_lateral_ok = lateral_ok;
1183 * transformWhereClause -
1184 * Transform the qualification and make sure it is of type boolean.
1185 * Used for WHERE and allied clauses.
1187 * constructName does not affect the semantics, but is used in error messages
1190 transformWhereClause(ParseState *pstate, Node *clause,
1191 ParseExprKind exprKind, const char *constructName)
1198 qual = transformExpr(pstate, clause, exprKind);
1200 qual = coerce_to_boolean(pstate, qual, constructName);
1207 * transformLimitClause -
1208 * Transform the expression and make sure it is of type bigint.
1209 * Used for LIMIT and allied clauses.
1211 * Note: as of Postgres 8.2, LIMIT expressions are expected to yield int8,
1212 * rather than int4 as before.
1214 * constructName does not affect the semantics, but is used in error messages
1217 transformLimitClause(ParseState *pstate, Node *clause,
1218 ParseExprKind exprKind, const char *constructName)
1225 qual = transformExpr(pstate, clause, exprKind);
1227 qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
1229 /* LIMIT can't refer to any variables of the current query */
1230 checkExprIsVarFree(pstate, qual, constructName);
1236 * checkExprIsVarFree
1237 * Check that given expr has no Vars of the current query level
1238 * (aggregates and window functions should have been rejected already).
1240 * This is used to check expressions that have to have a consistent value
1241 * across all rows of the query, such as a LIMIT. Arguably it should reject
1242 * volatile functions, too, but we don't do that --- whatever value the
1243 * function gives on first execution is what you get.
1245 * constructName does not affect the semantics, but is used in error messages
1248 checkExprIsVarFree(ParseState *pstate, Node *n, const char *constructName)
1250 if (contain_vars_of_level(n, 0))
1253 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1254 /* translator: %s is name of a SQL construct, eg LIMIT */
1255 errmsg("argument of %s must not contain variables",
1257 parser_errposition(pstate,
1258 locate_var_of_level(n, 0))));
1264 * checkTargetlistEntrySQL92 -
1265 * Validate a targetlist entry found by findTargetlistEntrySQL92
1267 * When we select a pre-existing tlist entry as a result of syntax such
1268 * as "GROUP BY 1", we have to make sure it is acceptable for use in the
1269 * indicated clause type; transformExpr() will have treated it as a regular
1273 checkTargetlistEntrySQL92(ParseState *pstate, TargetEntry *tle,
1274 ParseExprKind exprKind)
1278 case EXPR_KIND_GROUP_BY:
1279 /* reject aggregates and window functions */
1280 if (pstate->p_hasAggs &&
1281 contain_aggs_of_level((Node *) tle->expr, 0))
1283 (errcode(ERRCODE_GROUPING_ERROR),
1284 /* translator: %s is name of a SQL construct, eg GROUP BY */
1285 errmsg("aggregate functions are not allowed in %s",
1286 ParseExprKindName(exprKind)),
1287 parser_errposition(pstate,
1288 locate_agg_of_level((Node *) tle->expr, 0))));
1289 if (pstate->p_hasWindowFuncs &&
1290 contain_windowfuncs((Node *) tle->expr))
1292 (errcode(ERRCODE_WINDOWING_ERROR),
1293 /* translator: %s is name of a SQL construct, eg GROUP BY */
1294 errmsg("window functions are not allowed in %s",
1295 ParseExprKindName(exprKind)),
1296 parser_errposition(pstate,
1297 locate_windowfunc((Node *) tle->expr))));
1299 case EXPR_KIND_ORDER_BY:
1300 /* no extra checks needed */
1302 case EXPR_KIND_DISTINCT_ON:
1303 /* no extra checks needed */
1306 elog(ERROR, "unexpected exprKind in checkTargetlistEntrySQL92");
1312 * findTargetlistEntrySQL92 -
1313 * Returns the targetlist entry matching the given (untransformed) node.
1314 * If no matching entry exists, one is created and appended to the target
1315 * list as a "resjunk" node.
1317 * This function supports the old SQL92 ORDER BY interpretation, where the
1318 * expression is an output column name or number. If we fail to find a
1319 * match of that sort, we fall through to the SQL99 rules. For historical
1320 * reasons, Postgres also allows this interpretation for GROUP BY, though
1321 * the standard never did. However, for GROUP BY we prefer a SQL99 match.
1322 * This function is *not* used for WINDOW definitions.
1324 * node the ORDER BY, GROUP BY, or DISTINCT ON expression to be matched
1325 * tlist the target list (passed by reference so we can append to it)
1326 * exprKind identifies clause type being processed
1328 static TargetEntry *
1329 findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist,
1330 ParseExprKind exprKind)
1335 * Handle two special cases as mandated by the SQL92 spec:
1337 * 1. Bare ColumnName (no qualifier or subscripts)
1338 * For a bare identifier, we search for a matching column name
1339 * in the existing target list. Multiple matches are an error
1340 * unless they refer to identical values; for example,
1341 * we allow SELECT a, a FROM table ORDER BY a
1342 * but not SELECT a AS b, b FROM table ORDER BY b
1343 * If no match is found, we fall through and treat the identifier
1345 * For GROUP BY, it is incorrect to match the grouping item against
1346 * targetlist entries: according to SQL92, an identifier in GROUP BY
1347 * is a reference to a column name exposed by FROM, not to a target
1348 * list column. However, many implementations (including pre-7.0
1349 * PostgreSQL) accept this anyway. So for GROUP BY, we look first
1350 * to see if the identifier matches any FROM column name, and only
1351 * try for a targetlist name if it doesn't. This ensures that we
1352 * adhere to the spec in the case where the name could be both.
1353 * DISTINCT ON isn't in the standard, so we can do what we like there;
1354 * we choose to make it work like ORDER BY, on the rather flimsy
1355 * grounds that ordinary DISTINCT works on targetlist entries.
1357 * 2. IntegerConstant
1358 * This means to use the n'th item in the existing target list.
1359 * Note that it would make no sense to order/group/distinct by an
1360 * actual constant, so this does not create a conflict with SQL99.
1361 * GROUP BY column-number is not allowed by SQL92, but since
1362 * the standard has no other behavior defined for this syntax,
1363 * we may as well accept this common extension.
1365 * Note that pre-existing resjunk targets must not be used in either case,
1366 * since the user didn't write them in his SELECT list.
1368 * If neither special case applies, fall through to treat the item as
1369 * an expression per SQL99.
1372 if (IsA(node, ColumnRef) &&
1373 list_length(((ColumnRef *) node)->fields) == 1 &&
1374 IsA(linitial(((ColumnRef *) node)->fields), String))
1376 char *name = strVal(linitial(((ColumnRef *) node)->fields));
1377 int location = ((ColumnRef *) node)->location;
1379 if (exprKind == EXPR_KIND_GROUP_BY)
1382 * In GROUP BY, we must prefer a match against a FROM-clause
1383 * column to one against the targetlist. Look to see if there is
1384 * a matching column. If so, fall through to use SQL99 rules.
1385 * NOTE: if name could refer ambiguously to more than one column
1386 * name exposed by FROM, colNameToVar will ereport(ERROR). That's
1387 * just what we want here.
1389 * Small tweak for 7.4.3: ignore matches in upper query levels.
1390 * This effectively changes the search order for bare names to (1)
1391 * local FROM variables, (2) local targetlist aliases, (3) outer
1392 * FROM variables, whereas before it was (1) (3) (2). SQL92 and
1393 * SQL99 do not allow GROUPing BY an outer reference, so this
1394 * breaks no cases that are legal per spec, and it seems a more
1395 * self-consistent behavior.
1397 if (colNameToVar(pstate, name, true, location) != NULL)
1403 TargetEntry *target_result = NULL;
1407 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1409 if (!tle->resjunk &&
1410 strcmp(tle->resname, name) == 0)
1412 if (target_result != NULL)
1414 if (!equal(target_result->expr, tle->expr))
1416 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1419 translator: first %s is name of a SQL construct, eg ORDER BY */
1420 errmsg("%s \"%s\" is ambiguous",
1421 ParseExprKindName(exprKind),
1423 parser_errposition(pstate, location)));
1426 target_result = tle;
1427 /* Stay in loop to check for ambiguity */
1430 if (target_result != NULL)
1432 /* return the first match, after suitable validation */
1433 checkTargetlistEntrySQL92(pstate, target_result, exprKind);
1434 return target_result;
1438 if (IsA(node, A_Const))
1440 Value *val = &((A_Const *) node)->val;
1441 int location = ((A_Const *) node)->location;
1442 int targetlist_pos = 0;
1445 if (!IsA(val, Integer))
1447 (errcode(ERRCODE_SYNTAX_ERROR),
1448 /* translator: %s is name of a SQL construct, eg ORDER BY */
1449 errmsg("non-integer constant in %s",
1450 ParseExprKindName(exprKind)),
1451 parser_errposition(pstate, location)));
1453 target_pos = intVal(val);
1456 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1460 if (++targetlist_pos == target_pos)
1462 /* return the unique match, after suitable validation */
1463 checkTargetlistEntrySQL92(pstate, tle, exprKind);
1469 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1470 /* translator: %s is name of a SQL construct, eg ORDER BY */
1471 errmsg("%s position %d is not in select list",
1472 ParseExprKindName(exprKind), target_pos),
1473 parser_errposition(pstate, location)));
1477 * Otherwise, we have an expression, so process it per SQL99 rules.
1479 return findTargetlistEntrySQL99(pstate, node, tlist, exprKind);
1483 * findTargetlistEntrySQL99 -
1484 * Returns the targetlist entry matching the given (untransformed) node.
1485 * If no matching entry exists, one is created and appended to the target
1486 * list as a "resjunk" node.
1488 * This function supports the SQL99 interpretation, wherein the expression
1489 * is just an ordinary expression referencing input column names.
1491 * node the ORDER BY, GROUP BY, etc expression to be matched
1492 * tlist the target list (passed by reference so we can append to it)
1493 * exprKind identifies clause type being processed
1495 static TargetEntry *
1496 findTargetlistEntrySQL99(ParseState *pstate, Node *node, List **tlist,
1497 ParseExprKind exprKind)
1499 TargetEntry *target_result;
1504 * Convert the untransformed node to a transformed expression, and search
1505 * for a match in the tlist. NOTE: it doesn't really matter whether there
1506 * is more than one match. Also, we are willing to match an existing
1507 * resjunk target here, though the SQL92 cases above must ignore resjunk
1510 expr = transformExpr(pstate, node, exprKind);
1514 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1518 * Ignore any implicit cast on the existing tlist expression.
1520 * This essentially allows the ORDER/GROUP/etc item to adopt the same
1521 * datatype previously selected for a textually-equivalent tlist item.
1522 * There can't be any implicit cast at top level in an ordinary SELECT
1523 * tlist at this stage, but the case does arise with ORDER BY in an
1524 * aggregate function.
1526 texpr = strip_implicit_coercions((Node *) tle->expr);
1528 if (equal(expr, texpr))
1533 * If no matches, construct a new target entry which is appended to the
1534 * end of the target list. This target is given resjunk = TRUE so that it
1535 * will not be projected into the final tuple.
1537 target_result = transformTargetEntry(pstate, node, expr, exprKind,
1540 *tlist = lappend(*tlist, target_result);
1542 return target_result;
1546 * transformGroupClause -
1547 * transform a GROUP BY clause
1549 * GROUP BY items will be added to the targetlist (as resjunk columns)
1550 * if not already present, so the targetlist must be passed by reference.
1552 * This is also used for window PARTITION BY clauses (which act almost the
1553 * same, but are always interpreted per SQL99 rules).
1556 transformGroupClause(ParseState *pstate, List *grouplist,
1557 List **targetlist, List *sortClause,
1558 ParseExprKind exprKind, bool useSQL99)
1563 foreach(gl, grouplist)
1565 Node *gexpr = (Node *) lfirst(gl);
1570 tle = findTargetlistEntrySQL99(pstate, gexpr,
1571 targetlist, exprKind);
1573 tle = findTargetlistEntrySQL92(pstate, gexpr,
1574 targetlist, exprKind);
1576 /* Eliminate duplicates (GROUP BY x, x) */
1577 if (targetIsInSortList(tle, InvalidOid, result))
1581 * If the GROUP BY tlist entry also appears in ORDER BY, copy operator
1582 * info from the (first) matching ORDER BY item. This means that if
1583 * you write something like "GROUP BY foo ORDER BY foo USING <<<", the
1584 * GROUP BY operation silently takes on the equality semantics implied
1585 * by the ORDER BY. There are two reasons to do this: it improves the
1586 * odds that we can implement both GROUP BY and ORDER BY with a single
1587 * sort step, and it allows the user to choose the equality semantics
1588 * used by GROUP BY, should she be working with a datatype that has
1589 * more than one equality operator.
1591 if (tle->ressortgroupref > 0)
1595 foreach(sl, sortClause)
1597 SortGroupClause *sc = (SortGroupClause *) lfirst(sl);
1599 if (sc->tleSortGroupRef == tle->ressortgroupref)
1601 result = lappend(result, copyObject(sc));
1609 * If no match in ORDER BY, just add it to the result using default
1610 * sort/group semantics.
1613 result = addTargetToGroupList(pstate, tle,
1614 result, *targetlist,
1615 exprLocation(gexpr),
1623 * transformSortClause -
1624 * transform an ORDER BY clause
1626 * ORDER BY items will be added to the targetlist (as resjunk columns)
1627 * if not already present, so the targetlist must be passed by reference.
1629 * This is also used for window and aggregate ORDER BY clauses (which act
1630 * almost the same, but are always interpreted per SQL99 rules).
1633 transformSortClause(ParseState *pstate,
1636 ParseExprKind exprKind,
1637 bool resolveUnknown,
1640 List *sortlist = NIL;
1643 foreach(olitem, orderlist)
1645 SortBy *sortby = (SortBy *) lfirst(olitem);
1649 tle = findTargetlistEntrySQL99(pstate, sortby->node,
1650 targetlist, exprKind);
1652 tle = findTargetlistEntrySQL92(pstate, sortby->node,
1653 targetlist, exprKind);
1655 sortlist = addTargetToSortList(pstate, tle,
1656 sortlist, *targetlist, sortby,
1664 * transformWindowDefinitions -
1665 * transform window definitions (WindowDef to WindowClause)
1668 transformWindowDefinitions(ParseState *pstate,
1676 foreach(lc, windowdefs)
1678 WindowDef *windef = (WindowDef *) lfirst(lc);
1679 WindowClause *refwc = NULL;
1680 List *partitionClause;
1687 * Check for duplicate window names.
1690 findWindowClause(result, windef->name) != NULL)
1692 (errcode(ERRCODE_WINDOWING_ERROR),
1693 errmsg("window \"%s\" is already defined", windef->name),
1694 parser_errposition(pstate, windef->location)));
1697 * If it references a previous window, look that up.
1699 if (windef->refname)
1701 refwc = findWindowClause(result, windef->refname);
1704 (errcode(ERRCODE_UNDEFINED_OBJECT),
1705 errmsg("window \"%s\" does not exist",
1707 parser_errposition(pstate, windef->location)));
1711 * Transform PARTITION and ORDER specs, if any. These are treated
1712 * almost exactly like top-level GROUP BY and ORDER BY clauses,
1713 * including the special handling of nondefault operator semantics.
1715 orderClause = transformSortClause(pstate,
1716 windef->orderClause,
1718 EXPR_KIND_WINDOW_ORDER,
1719 true /* fix unknowns */ ,
1720 true /* force SQL99 rules */ );
1721 partitionClause = transformGroupClause(pstate,
1722 windef->partitionClause,
1725 EXPR_KIND_WINDOW_PARTITION,
1726 true /* force SQL99 rules */ );
1729 * And prepare the new WindowClause.
1731 wc = makeNode(WindowClause);
1732 wc->name = windef->name;
1733 wc->refname = windef->refname;
1736 * Per spec, a windowdef that references a previous one copies the
1737 * previous partition clause (and mustn't specify its own). It can
1738 * specify its own ordering clause, but only if the previous one had
1739 * none. It always specifies its own frame clause, and the previous
1740 * one must not have a frame clause. Yeah, it's bizarre that each of
1741 * these cases works differently, but SQL:2008 says so; see 7.11
1742 * <window clause> syntax rule 10 and general rule 1. The frame
1743 * clause rule is especially bizarre because it makes "OVER foo"
1744 * different from "OVER (foo)", and requires the latter to throw an
1745 * error if foo has a nondefault frame clause. Well, ours not to
1746 * reason why, but we do go out of our way to throw a useful error
1747 * message for such cases.
1751 if (partitionClause)
1753 (errcode(ERRCODE_WINDOWING_ERROR),
1754 errmsg("cannot override PARTITION BY clause of window \"%s\"",
1756 parser_errposition(pstate, windef->location)));
1757 wc->partitionClause = copyObject(refwc->partitionClause);
1760 wc->partitionClause = partitionClause;
1763 if (orderClause && refwc->orderClause)
1765 (errcode(ERRCODE_WINDOWING_ERROR),
1766 errmsg("cannot override ORDER BY clause of window \"%s\"",
1768 parser_errposition(pstate, windef->location)));
1771 wc->orderClause = orderClause;
1772 wc->copiedOrder = false;
1776 wc->orderClause = copyObject(refwc->orderClause);
1777 wc->copiedOrder = true;
1782 wc->orderClause = orderClause;
1783 wc->copiedOrder = false;
1785 if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
1788 * Use this message if this is a WINDOW clause, or if it's an OVER
1789 * clause that includes ORDER BY or framing clauses. (We already
1790 * rejected PARTITION BY above, so no need to check that.)
1793 orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
1795 (errcode(ERRCODE_WINDOWING_ERROR),
1796 errmsg("cannot copy window \"%s\" because it has a frame clause",
1798 parser_errposition(pstate, windef->location)));
1799 /* Else this clause is just OVER (foo), so say this: */
1801 (errcode(ERRCODE_WINDOWING_ERROR),
1802 errmsg("cannot copy window \"%s\" because it has a frame clause",
1804 errhint("Omit the parentheses in this OVER clause."),
1805 parser_errposition(pstate, windef->location)));
1807 wc->frameOptions = windef->frameOptions;
1808 /* Process frame offset expressions */
1809 wc->startOffset = transformFrameOffset(pstate, wc->frameOptions,
1810 windef->startOffset);
1811 wc->endOffset = transformFrameOffset(pstate, wc->frameOptions,
1813 wc->winref = winref;
1815 result = lappend(result, wc);
1822 * transformDistinctClause -
1823 * transform a DISTINCT clause
1825 * Since we may need to add items to the query's targetlist, that list
1826 * is passed by reference.
1828 * As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
1829 * possible into the distinctClause. This avoids a possible need to re-sort,
1830 * and allows the user to choose the equality semantics used by DISTINCT,
1831 * should she be working with a datatype that has more than one equality
1834 * is_agg is true if we are transforming an aggregate(DISTINCT ...)
1835 * function call. This does not affect any behavior, only the phrasing
1836 * of error messages.
1839 transformDistinctClause(ParseState *pstate,
1840 List **targetlist, List *sortClause, bool is_agg)
1847 * The distinctClause should consist of all ORDER BY items followed by all
1848 * other non-resjunk targetlist items. There must not be any resjunk
1849 * ORDER BY items --- that would imply that we are sorting by a value that
1850 * isn't necessarily unique within a DISTINCT group, so the results
1851 * wouldn't be well-defined. This construction ensures we follow the rule
1852 * that sortClause and distinctClause match; in fact the sortClause will
1853 * always be a prefix of distinctClause.
1855 * Note a corner case: the same TLE could be in the ORDER BY list multiple
1856 * times with different sortops. We have to include it in the
1857 * distinctClause the same way to preserve the prefix property. The net
1858 * effect will be that the TLE value will be made unique according to both
1861 foreach(slitem, sortClause)
1863 SortGroupClause *scl = (SortGroupClause *) lfirst(slitem);
1864 TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
1868 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1870 errmsg("in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list") :
1871 errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list"),
1872 parser_errposition(pstate,
1873 exprLocation((Node *) tle->expr))));
1874 result = lappend(result, copyObject(scl));
1878 * Now add any remaining non-resjunk tlist items, using default sort/group
1879 * semantics for their data types.
1881 foreach(tlitem, *targetlist)
1883 TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
1886 continue; /* ignore junk */
1887 result = addTargetToGroupList(pstate, tle,
1888 result, *targetlist,
1889 exprLocation((Node *) tle->expr),
1897 * transformDistinctOnClause -
1898 * transform a DISTINCT ON clause
1900 * Since we may need to add items to the query's targetlist, that list
1901 * is passed by reference.
1903 * As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
1904 * possible into the distinctClause. This avoids a possible need to re-sort,
1905 * and allows the user to choose the equality semantics used by DISTINCT,
1906 * should she be working with a datatype that has more than one equality
1910 transformDistinctOnClause(ParseState *pstate, List *distinctlist,
1911 List **targetlist, List *sortClause)
1914 List *sortgrouprefs = NIL;
1915 bool skipped_sortitem;
1920 * Add all the DISTINCT ON expressions to the tlist (if not already
1921 * present, they are added as resjunk items). Assign sortgroupref numbers
1922 * to them, and make a list of these numbers. (NB: we rely below on the
1923 * sortgrouprefs list being one-for-one with the original distinctlist.
1924 * Also notice that we could have duplicate DISTINCT ON expressions and
1925 * hence duplicate entries in sortgrouprefs.)
1927 foreach(lc, distinctlist)
1929 Node *dexpr = (Node *) lfirst(lc);
1933 tle = findTargetlistEntrySQL92(pstate, dexpr, targetlist,
1934 EXPR_KIND_DISTINCT_ON);
1935 sortgroupref = assignSortGroupRef(tle, *targetlist);
1936 sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
1940 * If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
1941 * semantics from ORDER BY items that match DISTINCT ON items, and also
1942 * adopt their column sort order. We insist that the distinctClause and
1943 * sortClause match, so throw error if we find the need to add any more
1944 * distinctClause items after we've skipped an ORDER BY item that wasn't
1947 skipped_sortitem = false;
1948 foreach(lc, sortClause)
1950 SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
1952 if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
1954 if (skipped_sortitem)
1956 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1957 errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
1958 parser_errposition(pstate,
1959 get_matching_location(scl->tleSortGroupRef,
1963 result = lappend(result, copyObject(scl));
1966 skipped_sortitem = true;
1970 * Now add any remaining DISTINCT ON items, using default sort/group
1971 * semantics for their data types. (Note: this is pretty questionable; if
1972 * the ORDER BY list doesn't include all the DISTINCT ON items and more
1973 * besides, you certainly aren't using DISTINCT ON in the intended way,
1974 * and you probably aren't going to get consistent results. It might be
1975 * better to throw an error or warning here. But historically we've
1976 * allowed it, so keep doing so.)
1978 forboth(lc, distinctlist, lc2, sortgrouprefs)
1980 Node *dexpr = (Node *) lfirst(lc);
1981 int sortgroupref = lfirst_int(lc2);
1982 TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
1984 if (targetIsInSortList(tle, InvalidOid, result))
1985 continue; /* already in list (with some semantics) */
1986 if (skipped_sortitem)
1988 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1989 errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
1990 parser_errposition(pstate, exprLocation(dexpr))));
1991 result = addTargetToGroupList(pstate, tle,
1992 result, *targetlist,
1993 exprLocation(dexpr),
2001 * get_matching_location
2002 * Get the exprLocation of the exprs member corresponding to the
2003 * (first) member of sortgrouprefs that equals sortgroupref.
2005 * This is used so that we can point at a troublesome DISTINCT ON entry.
2006 * (Note that we need to use the original untransformed DISTINCT ON list
2007 * item, as whatever TLE it corresponds to will very possibly have a
2008 * parse location pointing to some matching entry in the SELECT list
2009 * or ORDER BY list.)
2012 get_matching_location(int sortgroupref, List *sortgrouprefs, List *exprs)
2017 forboth(lcs, sortgrouprefs, lce, exprs)
2019 if (lfirst_int(lcs) == sortgroupref)
2020 return exprLocation((Node *) lfirst(lce));
2022 /* if no match, caller blew it */
2023 elog(ERROR, "get_matching_location: no matching sortgroupref");
2024 return -1; /* keep compiler quiet */
2028 * addTargetToSortList
2029 * If the given targetlist entry isn't already in the SortGroupClause
2030 * list, add it to the end of the list, using the given sort ordering
2033 * If resolveUnknown is TRUE, convert TLEs of type UNKNOWN to TEXT. If not,
2034 * do nothing (which implies the search for a sort operator will fail).
2035 * pstate should be provided if resolveUnknown is TRUE, but can be NULL
2038 * Returns the updated SortGroupClause list.
2041 addTargetToSortList(ParseState *pstate, TargetEntry *tle,
2042 List *sortlist, List *targetlist, SortBy *sortby,
2043 bool resolveUnknown)
2045 Oid restype = exprType((Node *) tle->expr);
2051 ParseCallbackState pcbstate;
2053 /* if tlist item is an UNKNOWN literal, change it to TEXT */
2054 if (restype == UNKNOWNOID && resolveUnknown)
2056 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
2057 restype, TEXTOID, -1,
2059 COERCE_IMPLICIT_CAST,
2065 * Rather than clutter the API of get_sort_group_operators and the other
2066 * functions we're about to use, make use of error context callback to
2067 * mark any error reports with a parse position. We point to the operator
2068 * location if present, else to the expression being sorted. (NB: use the
2069 * original untransformed expression here; the TLE entry might well point
2070 * at a duplicate expression in the regular SELECT list.)
2072 location = sortby->location;
2074 location = exprLocation(sortby->node);
2075 setup_parser_errposition_callback(&pcbstate, pstate, location);
2077 /* determine the sortop, eqop, and directionality */
2078 switch (sortby->sortby_dir)
2080 case SORTBY_DEFAULT:
2082 get_sort_group_operators(restype,
2084 &sortop, &eqop, NULL,
2089 get_sort_group_operators(restype,
2091 NULL, &eqop, &sortop,
2096 Assert(sortby->useOp != NIL);
2097 sortop = compatible_oper_opid(sortby->useOp,
2103 * Verify it's a valid ordering operator, fetch the corresponding
2104 * equality operator, and determine whether to consider it like
2107 eqop = get_equality_op_for_ordering_op(sortop, &reverse);
2108 if (!OidIsValid(eqop))
2110 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2111 errmsg("operator %s is not a valid ordering operator",
2112 strVal(llast(sortby->useOp))),
2113 errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
2116 * Also see if the equality operator is hashable.
2118 hashable = op_hashjoinable(eqop, restype);
2121 elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
2122 sortop = InvalidOid; /* keep compiler quiet */
2129 cancel_parser_errposition_callback(&pcbstate);
2131 /* avoid making duplicate sortlist entries */
2132 if (!targetIsInSortList(tle, sortop, sortlist))
2134 SortGroupClause *sortcl = makeNode(SortGroupClause);
2136 sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
2138 sortcl->eqop = eqop;
2139 sortcl->sortop = sortop;
2140 sortcl->hashable = hashable;
2142 switch (sortby->sortby_nulls)
2144 case SORTBY_NULLS_DEFAULT:
2145 /* NULLS FIRST is default for DESC; other way for ASC */
2146 sortcl->nulls_first = reverse;
2148 case SORTBY_NULLS_FIRST:
2149 sortcl->nulls_first = true;
2151 case SORTBY_NULLS_LAST:
2152 sortcl->nulls_first = false;
2155 elog(ERROR, "unrecognized sortby_nulls: %d",
2156 sortby->sortby_nulls);
2160 sortlist = lappend(sortlist, sortcl);
2167 * addTargetToGroupList
2168 * If the given targetlist entry isn't already in the SortGroupClause
2169 * list, add it to the end of the list, using default sort/group
2172 * This is very similar to addTargetToSortList, except that we allow the
2173 * case where only a grouping (equality) operator can be found, and that
2174 * the TLE is considered "already in the list" if it appears there with any
2175 * sorting semantics.
2177 * location is the parse location to be fingered in event of trouble. Note
2178 * that we can't rely on exprLocation(tle->expr), because that might point
2179 * to a SELECT item that matches the GROUP BY item; it'd be pretty confusing
2180 * to report such a location.
2182 * If resolveUnknown is TRUE, convert TLEs of type UNKNOWN to TEXT. If not,
2183 * do nothing (which implies the search for an equality operator will fail).
2184 * pstate should be provided if resolveUnknown is TRUE, but can be NULL
2187 * Returns the updated SortGroupClause list.
2190 addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
2191 List *grouplist, List *targetlist, int location,
2192 bool resolveUnknown)
2194 Oid restype = exprType((Node *) tle->expr);
2196 /* if tlist item is an UNKNOWN literal, change it to TEXT */
2197 if (restype == UNKNOWNOID && resolveUnknown)
2199 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
2200 restype, TEXTOID, -1,
2202 COERCE_IMPLICIT_CAST,
2207 /* avoid making duplicate grouplist entries */
2208 if (!targetIsInSortList(tle, InvalidOid, grouplist))
2210 SortGroupClause *grpcl = makeNode(SortGroupClause);
2214 ParseCallbackState pcbstate;
2216 setup_parser_errposition_callback(&pcbstate, pstate, location);
2218 /* determine the eqop and optional sortop */
2219 get_sort_group_operators(restype,
2221 &sortop, &eqop, NULL,
2224 cancel_parser_errposition_callback(&pcbstate);
2226 grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
2228 grpcl->sortop = sortop;
2229 grpcl->nulls_first = false; /* OK with or without sortop */
2230 grpcl->hashable = hashable;
2232 grouplist = lappend(grouplist, grpcl);
2239 * assignSortGroupRef
2240 * Assign the targetentry an unused ressortgroupref, if it doesn't
2241 * already have one. Return the assigned or pre-existing refnumber.
2243 * 'tlist' is the targetlist containing (or to contain) the given targetentry.
2246 assignSortGroupRef(TargetEntry *tle, List *tlist)
2251 if (tle->ressortgroupref) /* already has one? */
2252 return tle->ressortgroupref;
2254 /* easiest way to pick an unused refnumber: max used + 1 */
2258 Index ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
2263 tle->ressortgroupref = maxRef + 1;
2264 return tle->ressortgroupref;
2268 * targetIsInSortList
2269 * Is the given target item already in the sortlist?
2270 * If sortop is not InvalidOid, also test for a match to the sortop.
2272 * It is not an oversight that this function ignores the nulls_first flag.
2273 * We check sortop when determining if an ORDER BY item is redundant with
2274 * earlier ORDER BY items, because it's conceivable that "ORDER BY
2275 * foo USING <, foo USING <<<" is not redundant, if <<< distinguishes
2276 * values that < considers equal. We need not check nulls_first
2277 * however, because a lower-order column with the same sortop but
2278 * opposite nulls direction is redundant. Also, we can consider
2279 * ORDER BY foo ASC, foo DESC redundant, so check for a commutator match.
2281 * Works for both ordering and grouping lists (sortop would normally be
2282 * InvalidOid when considering grouping). Note that the main reason we need
2283 * this routine (and not just a quick test for nonzeroness of ressortgroupref)
2284 * is that a TLE might be in only one of the lists.
2287 targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
2289 Index ref = tle->ressortgroupref;
2292 /* no need to scan list if tle has no marker */
2296 foreach(l, sortList)
2298 SortGroupClause *scl = (SortGroupClause *) lfirst(l);
2300 if (scl->tleSortGroupRef == ref &&
2301 (sortop == InvalidOid ||
2302 sortop == scl->sortop ||
2303 sortop == get_commutator(scl->sortop)))
2311 * Find the named WindowClause in the list, or return NULL if not there
2313 static WindowClause *
2314 findWindowClause(List *wclist, const char *name)
2320 WindowClause *wc = (WindowClause *) lfirst(l);
2322 if (wc->name && strcmp(wc->name, name) == 0)
2330 * transformFrameOffset
2331 * Process a window frame offset expression
2334 transformFrameOffset(ParseState *pstate, int frameOptions, Node *clause)
2336 const char *constructName = NULL;
2339 /* Quick exit if no offset expression */
2343 if (frameOptions & FRAMEOPTION_ROWS)
2345 /* Transform the raw expression tree */
2346 node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_ROWS);
2349 * Like LIMIT clause, simply coerce to int8
2351 constructName = "ROWS";
2352 node = coerce_to_specific_type(pstate, node, INT8OID, constructName);
2354 else if (frameOptions & FRAMEOPTION_RANGE)
2356 /* Transform the raw expression tree */
2357 node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_RANGE);
2360 * this needs a lot of thought to decide how to support in the context
2361 * of Postgres' extensible datatype framework
2363 constructName = "RANGE";
2364 /* error was already thrown by gram.y, this is just a backstop */
2365 elog(ERROR, "window frame with value offset is not implemented");
2373 /* Disallow variables in frame offsets */
2374 checkExprIsVarFree(pstate, node, constructName);