1 /*-------------------------------------------------------------------------
4 * handle clauses in parser
6 * Portions Copyright (c) 1996-2016, 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 "miscadmin.h"
20 #include "access/heapam.h"
21 #include "access/tsmapi.h"
22 #include "catalog/catalog.h"
23 #include "catalog/heap.h"
24 #include "catalog/pg_am.h"
25 #include "catalog/pg_constraint_fn.h"
26 #include "catalog/pg_type.h"
27 #include "commands/defrem.h"
28 #include "nodes/makefuncs.h"
29 #include "nodes/nodeFuncs.h"
30 #include "optimizer/tlist.h"
31 #include "optimizer/var.h"
32 #include "parser/analyze.h"
33 #include "parser/parsetree.h"
34 #include "parser/parser.h"
35 #include "parser/parse_clause.h"
36 #include "parser/parse_coerce.h"
37 #include "parser/parse_collate.h"
38 #include "parser/parse_expr.h"
39 #include "parser/parse_func.h"
40 #include "parser/parse_oper.h"
41 #include "parser/parse_relation.h"
42 #include "parser/parse_target.h"
43 #include "parser/parse_type.h"
44 #include "rewrite/rewriteManip.h"
45 #include "utils/guc.h"
46 #include "utils/lsyscache.h"
47 #include "utils/rel.h"
50 /* Convenience macro for the most common makeNamespaceItem() case */
51 #define makeDefaultNSItem(rte) makeNamespaceItem(rte, true, true, false, true)
53 static void extractRemainingColumns(List *common_colnames,
54 List *src_colnames, List *src_colvars,
55 List **res_colnames, List **res_colvars);
56 static Node *transformJoinUsingClause(ParseState *pstate,
57 RangeTblEntry *leftRTE, RangeTblEntry *rightRTE,
58 List *leftVars, List *rightVars);
59 static Node *transformJoinOnClause(ParseState *pstate, JoinExpr *j,
61 static RangeTblEntry *transformTableEntry(ParseState *pstate, RangeVar *r);
62 static RangeTblEntry *transformCTEReference(ParseState *pstate, RangeVar *r,
63 CommonTableExpr *cte, Index levelsup);
64 static RangeTblEntry *transformRangeSubselect(ParseState *pstate,
66 static RangeTblEntry *transformRangeFunction(ParseState *pstate,
68 static TableSampleClause *transformRangeTableSample(ParseState *pstate,
69 RangeTableSample *rts);
70 static Node *transformFromClauseItem(ParseState *pstate, Node *n,
71 RangeTblEntry **top_rte, int *top_rti,
73 static Node *buildMergedJoinVar(ParseState *pstate, JoinType jointype,
74 Var *l_colvar, Var *r_colvar);
75 static ParseNamespaceItem *makeNamespaceItem(RangeTblEntry *rte,
76 bool rel_visible, bool cols_visible,
77 bool lateral_only, bool lateral_ok);
78 static void setNamespaceColumnVisibility(List *namespace, bool cols_visible);
79 static void setNamespaceLateralState(List *namespace,
80 bool lateral_only, bool lateral_ok);
81 static void checkExprIsVarFree(ParseState *pstate, Node *n,
82 const char *constructName);
83 static TargetEntry *findTargetlistEntrySQL92(ParseState *pstate, Node *node,
84 List **tlist, ParseExprKind exprKind);
85 static TargetEntry *findTargetlistEntrySQL99(ParseState *pstate, Node *node,
86 List **tlist, ParseExprKind exprKind);
87 static int get_matching_location(int sortgroupref,
88 List *sortgrouprefs, List *exprs);
89 static List *resolve_unique_index_expr(ParseState *pstate, InferClause *infer,
91 static List *addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
92 List *grouplist, List *targetlist, int location,
94 static WindowClause *findWindowClause(List *wclist, const char *name);
95 static Node *transformFrameOffset(ParseState *pstate, int frameOptions,
100 * transformFromClause -
101 * Process the FROM clause and add items to the query's range table,
102 * joinlist, and namespace.
104 * Note: we assume that the pstate's p_rtable, p_joinlist, and p_namespace
105 * lists were initialized to NIL when the pstate was created.
106 * We will add onto any entries already present --- this is needed for rule
107 * processing, as well as for UPDATE and DELETE.
110 transformFromClause(ParseState *pstate, List *frmList)
115 * The grammar will have produced a list of RangeVars, RangeSubselects,
116 * RangeFunctions, and/or JoinExprs. Transform each one (possibly adding
117 * entries to the rtable), check for duplicate refnames, and then add it
118 * to the joinlist and namespace.
120 * Note we must process the items left-to-right for proper handling of
121 * LATERAL references.
125 Node *n = lfirst(fl);
130 n = transformFromClauseItem(pstate, n,
135 checkNameSpaceConflicts(pstate, pstate->p_namespace, namespace);
137 /* Mark the new namespace items as visible only to LATERAL */
138 setNamespaceLateralState(namespace, true, true);
140 pstate->p_joinlist = lappend(pstate->p_joinlist, n);
141 pstate->p_namespace = list_concat(pstate->p_namespace, namespace);
145 * We're done parsing the FROM list, so make all namespace items
146 * unconditionally visible. Note that this will also reset lateral_only
147 * for any namespace items that were already present when we were called;
148 * but those should have been that way already.
150 setNamespaceLateralState(pstate->p_namespace, false, true);
155 * Add the target relation of INSERT/UPDATE/DELETE to the range table,
156 * and make the special links to it in the ParseState.
158 * We also open the target relation and acquire a write lock on it.
159 * This must be done before processing the FROM list, in case the target
160 * is also mentioned as a source relation --- we want to be sure to grab
161 * the write lock before any read lock.
163 * If alsoSource is true, add the target to the query's joinlist and
164 * namespace. For INSERT, we don't want the target to be joined to;
165 * it's a destination of tuples, not a source. For UPDATE/DELETE,
166 * we do need to scan or join the target. (NOTE: we do not bother
167 * to check for namespace conflict; we assume that the namespace was
168 * initially empty in these cases.)
170 * Finally, we mark the relation as requiring the permissions specified
173 * Returns the rangetable index of the target relation.
176 setTargetTable(ParseState *pstate, RangeVar *relation,
177 bool inh, bool alsoSource, AclMode requiredPerms)
182 /* Close old target; this could only happen for multi-action rules */
183 if (pstate->p_target_relation != NULL)
184 heap_close(pstate->p_target_relation, NoLock);
187 * Open target rel and grab suitable lock (which we will hold till end of
190 * free_parsestate() will eventually do the corresponding heap_close(),
191 * but *not* release the lock.
193 pstate->p_target_relation = parserOpenTable(pstate, relation,
199 rte = addRangeTableEntryForRelation(pstate, pstate->p_target_relation,
200 relation->alias, inh, false);
201 pstate->p_target_rangetblentry = rte;
203 /* assume new rte is at end */
204 rtindex = list_length(pstate->p_rtable);
205 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
208 * Override addRangeTableEntry's default ACL_SELECT permissions check, and
209 * instead mark target table as requiring exactly the specified
212 * If we find an explicit reference to the rel later during parse
213 * analysis, we will add the ACL_SELECT bit back again; see
214 * markVarForSelectPriv and its callers.
216 rte->requiredPerms = requiredPerms;
219 * If UPDATE/DELETE, add table to joinlist and namespace.
221 * Note: some callers know that they can find the new ParseNamespaceItem
222 * at the end of the pstate->p_namespace list. This is a bit ugly but not
223 * worth complicating this function's signature for.
226 addRTEtoQuery(pstate, rte, true, true, true);
232 * Simplify InhOption (yes/no/default) into boolean yes/no.
234 * The reason we do things this way is that we don't want to examine the
235 * SQL_inheritance option flag until parse_analyze() is run. Otherwise,
236 * we'd do the wrong thing with query strings that intermix SET commands
240 interpretInhOption(InhOption inhOpt)
249 return SQL_inheritance;
251 elog(ERROR, "bogus InhOption value: %d", inhOpt);
252 return false; /* keep compiler quiet */
256 * Given a relation-options list (of DefElems), return true iff the specified
257 * table/result set should be created with OIDs. This needs to be done after
258 * parsing the query string because the return value can depend upon the
259 * default_with_oids GUC var.
261 * In some situations, we want to reject an OIDS option even if it's present.
262 * That's (rather messily) handled here rather than reloptions.c, because that
263 * code explicitly punts checking for oids to here.
266 interpretOidsOption(List *defList, bool allowOids)
270 /* Scan list to see if OIDS was included */
271 foreach(cell, defList)
273 DefElem *def = (DefElem *) lfirst(cell);
275 if (def->defnamespace == NULL &&
276 pg_strcasecmp(def->defname, "oids") == 0)
280 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
281 errmsg("unrecognized parameter \"%s\"",
283 return defGetBoolean(def);
287 /* Force no-OIDS result if caller disallows OIDS. */
291 /* OIDS option was not specified, so use default. */
292 return default_with_oids;
296 * Extract all not-in-common columns from column lists of a source table
299 extractRemainingColumns(List *common_colnames,
300 List *src_colnames, List *src_colvars,
301 List **res_colnames, List **res_colvars)
303 List *new_colnames = NIL;
304 List *new_colvars = NIL;
308 Assert(list_length(src_colnames) == list_length(src_colvars));
310 forboth(lnames, src_colnames, lvars, src_colvars)
312 char *colname = strVal(lfirst(lnames));
316 foreach(cnames, common_colnames)
318 char *ccolname = strVal(lfirst(cnames));
320 if (strcmp(colname, ccolname) == 0)
329 new_colnames = lappend(new_colnames, lfirst(lnames));
330 new_colvars = lappend(new_colvars, lfirst(lvars));
334 *res_colnames = new_colnames;
335 *res_colvars = new_colvars;
338 /* transformJoinUsingClause()
339 * Build a complete ON clause from a partially-transformed USING list.
340 * We are given lists of nodes representing left and right match columns.
341 * Result is a transformed qualification expression.
344 transformJoinUsingClause(ParseState *pstate,
345 RangeTblEntry *leftRTE, RangeTblEntry *rightRTE,
346 List *leftVars, List *rightVars)
354 * We cheat a little bit here by building an untransformed operator tree
355 * whose leaves are the already-transformed Vars. This requires collusion
356 * from transformExpr(), which normally could be expected to complain
357 * about already-transformed subnodes. However, this does mean that we
358 * have to mark the columns as requiring SELECT privilege for ourselves;
359 * transformExpr() won't do it.
361 forboth(lvars, leftVars, rvars, rightVars)
363 Var *lvar = (Var *) lfirst(lvars);
364 Var *rvar = (Var *) lfirst(rvars);
367 /* Require read access to the join variables */
368 markVarForSelectPriv(pstate, lvar, leftRTE);
369 markVarForSelectPriv(pstate, rvar, rightRTE);
371 /* Now create the lvar = rvar join condition */
372 e = makeSimpleA_Expr(AEXPR_OP, "=",
373 copyObject(lvar), copyObject(rvar),
376 /* Prepare to combine into an AND clause, if multiple join columns */
377 andargs = lappend(andargs, e);
380 /* Only need an AND if there's more than one join column */
381 if (list_length(andargs) == 1)
382 result = (Node *) linitial(andargs);
384 result = (Node *) makeBoolExpr(AND_EXPR, andargs, -1);
387 * Since the references are already Vars, and are certainly from the input
388 * relations, we don't have to go through the same pushups that
389 * transformJoinOnClause() does. Just invoke transformExpr() to fix up
390 * the operators, and we're done.
392 result = transformExpr(pstate, result, EXPR_KIND_JOIN_USING);
394 result = coerce_to_boolean(pstate, result, "JOIN/USING");
399 /* transformJoinOnClause()
400 * Transform the qual conditions for JOIN/ON.
401 * Result is a transformed qualification expression.
404 transformJoinOnClause(ParseState *pstate, JoinExpr *j, List *namespace)
407 List *save_namespace;
410 * The namespace that the join expression should see is just the two
411 * subtrees of the JOIN plus any outer references from upper pstate
412 * levels. Temporarily set this pstate's namespace accordingly. (We need
413 * not check for refname conflicts, because transformFromClauseItem()
414 * already did.) All namespace items are marked visible regardless of
417 setNamespaceLateralState(namespace, false, true);
419 save_namespace = pstate->p_namespace;
420 pstate->p_namespace = namespace;
422 result = transformWhereClause(pstate, j->quals,
423 EXPR_KIND_JOIN_ON, "JOIN/ON");
425 pstate->p_namespace = save_namespace;
431 * transformTableEntry --- transform a RangeVar (simple relation reference)
433 static RangeTblEntry *
434 transformTableEntry(ParseState *pstate, RangeVar *r)
438 /* We need only build a range table entry */
439 rte = addRangeTableEntry(pstate, r, r->alias,
440 interpretInhOption(r->inhOpt), true);
446 * transformCTEReference --- transform a RangeVar that references a common
447 * table expression (ie, a sub-SELECT defined in a WITH clause)
449 static RangeTblEntry *
450 transformCTEReference(ParseState *pstate, RangeVar *r,
451 CommonTableExpr *cte, Index levelsup)
455 rte = addRangeTableEntryForCTE(pstate, cte, levelsup, r, true);
461 * transformRangeSubselect --- transform a sub-SELECT appearing in FROM
463 static RangeTblEntry *
464 transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
470 * We require user to supply an alias for a subselect, per SQL92. To relax
471 * this, we'd have to be prepared to gin up a unique alias for an
472 * unlabeled subselect. (This is just elog, not ereport, because the
473 * grammar should have enforced it already. It'd probably be better to
474 * report the error here, but we don't have a good error location here.)
476 if (r->alias == NULL)
477 elog(ERROR, "subquery in FROM must have an alias");
480 * Set p_expr_kind to show this parse level is recursing to a subselect.
481 * We can't be nested within any expression, so don't need save-restore
484 Assert(pstate->p_expr_kind == EXPR_KIND_NONE);
485 pstate->p_expr_kind = EXPR_KIND_FROM_SUBSELECT;
488 * If the subselect is LATERAL, make lateral_only names of this level
489 * visible to it. (LATERAL can't nest within a single pstate level, so we
490 * don't need save/restore logic here.)
492 Assert(!pstate->p_lateral_active);
493 pstate->p_lateral_active = r->lateral;
496 * Analyze and transform the subquery.
498 query = parse_sub_analyze(r->subquery, pstate, NULL,
499 isLockedRefname(pstate, r->alias->aliasname));
502 pstate->p_lateral_active = false;
503 pstate->p_expr_kind = EXPR_KIND_NONE;
506 * Check that we got something reasonable. Many of these conditions are
507 * impossible given restrictions of the grammar, but check 'em anyway.
509 if (!IsA(query, Query) ||
510 query->commandType != CMD_SELECT ||
511 query->utilityStmt != NULL)
512 elog(ERROR, "unexpected non-SELECT command in subquery in FROM");
515 * OK, build an RTE for the subquery.
517 rte = addRangeTableEntryForSubquery(pstate,
528 * transformRangeFunction --- transform a function call appearing in FROM
530 static RangeTblEntry *
531 transformRangeFunction(ParseState *pstate, RangeFunction *r)
533 List *funcexprs = NIL;
534 List *funcnames = NIL;
535 List *coldeflists = NIL;
541 * We make lateral_only names of this level visible, whether or not the
542 * RangeFunction is explicitly marked LATERAL. This is needed for SQL
543 * spec compliance in the case of UNNEST(), and seems useful on
544 * convenience grounds for all functions in FROM.
546 * (LATERAL can't nest within a single pstate level, so we don't need
547 * save/restore logic here.)
549 Assert(!pstate->p_lateral_active);
550 pstate->p_lateral_active = true;
553 * Transform the raw expressions.
555 * While transforming, also save function names for possible use as alias
556 * and column names. We use the same transformation rules as for a SELECT
557 * output expression. For a FuncCall node, the result will be the
558 * function name, but it is possible for the grammar to hand back other
561 * We have to get this info now, because FigureColname only works on raw
562 * parsetrees. Actually deciding what to do with the names is left up to
563 * addRangeTableEntryForFunction.
565 * Likewise, collect column definition lists if there were any. But
566 * complain if we find one here and the RangeFunction has one too.
568 foreach(lc, r->functions)
570 List *pair = (List *) lfirst(lc);
574 /* Disassemble the function-call/column-def-list pairs */
575 Assert(list_length(pair) == 2);
576 fexpr = (Node *) linitial(pair);
577 coldeflist = (List *) lsecond(pair);
580 * If we find a function call unnest() with more than one argument and
581 * no special decoration, transform it into separate unnest() calls on
582 * each argument. This is a kluge, for sure, but it's less nasty than
583 * other ways of implementing the SQL-standard UNNEST() syntax.
585 * If there is any decoration (including a coldeflist), we don't
586 * transform, which probably means a no-such-function error later. We
587 * could alternatively throw an error right now, but that doesn't seem
588 * tremendously helpful. If someone is using any such decoration,
589 * then they're not using the SQL-standard syntax, and they're more
590 * likely expecting an un-tweaked function call.
592 * Note: the transformation changes a non-schema-qualified unnest()
593 * function name into schema-qualified pg_catalog.unnest(). This
594 * choice is also a bit debatable, but it seems reasonable to force
595 * use of built-in unnest() when we make this transformation.
597 if (IsA(fexpr, FuncCall))
599 FuncCall *fc = (FuncCall *) fexpr;
601 if (list_length(fc->funcname) == 1 &&
602 strcmp(strVal(linitial(fc->funcname)), "unnest") == 0 &&
603 list_length(fc->args) > 1 &&
604 fc->agg_order == NIL &&
605 fc->agg_filter == NULL &&
608 !fc->func_variadic &&
614 foreach(lc, fc->args)
616 Node *arg = (Node *) lfirst(lc);
619 newfc = makeFuncCall(SystemFuncName("unnest"),
623 funcexprs = lappend(funcexprs,
624 transformExpr(pstate, (Node *) newfc,
625 EXPR_KIND_FROM_FUNCTION));
627 funcnames = lappend(funcnames,
628 FigureColname((Node *) newfc));
630 /* coldeflist is empty, so no error is possible */
632 coldeflists = lappend(coldeflists, coldeflist);
634 continue; /* done with this function item */
638 /* normal case ... */
639 funcexprs = lappend(funcexprs,
640 transformExpr(pstate, fexpr,
641 EXPR_KIND_FROM_FUNCTION));
643 funcnames = lappend(funcnames,
644 FigureColname(fexpr));
646 if (coldeflist && r->coldeflist)
648 (errcode(ERRCODE_SYNTAX_ERROR),
649 errmsg("multiple column definition lists are not allowed for the same function"),
650 parser_errposition(pstate,
651 exprLocation((Node *) r->coldeflist))));
653 coldeflists = lappend(coldeflists, coldeflist);
656 pstate->p_lateral_active = false;
659 * We must assign collations now so that the RTE exposes correct collation
660 * info for Vars created from it.
662 assign_list_collations(pstate, funcexprs);
665 * Install the top-level coldeflist if there was one (we already checked
666 * that there was no conflicting per-function coldeflist).
668 * We only allow this when there's a single function (even after UNNEST
669 * expansion) and no WITH ORDINALITY. The reason for the latter
670 * restriction is that it's not real clear whether the ordinality column
671 * should be in the coldeflist, and users are too likely to make mistakes
672 * in one direction or the other. Putting the coldeflist inside ROWS
673 * FROM() is much clearer in this case.
677 if (list_length(funcexprs) != 1)
681 (errcode(ERRCODE_SYNTAX_ERROR),
682 errmsg("ROWS FROM() with multiple functions cannot have a column definition list"),
683 errhint("Put a separate column definition list for each function inside ROWS FROM()."),
684 parser_errposition(pstate,
685 exprLocation((Node *) r->coldeflist))));
688 (errcode(ERRCODE_SYNTAX_ERROR),
689 errmsg("UNNEST() with multiple arguments cannot have a column definition list"),
690 errhint("Use separate UNNEST() calls inside ROWS FROM(), and attach a column definition list to each one."),
691 parser_errposition(pstate,
692 exprLocation((Node *) r->coldeflist))));
696 (errcode(ERRCODE_SYNTAX_ERROR),
697 errmsg("WITH ORDINALITY cannot be used with a column definition list"),
698 errhint("Put the column definition list inside ROWS FROM()."),
699 parser_errposition(pstate,
700 exprLocation((Node *) r->coldeflist))));
702 coldeflists = list_make1(r->coldeflist);
706 * Mark the RTE as LATERAL if the user said LATERAL explicitly, or if
707 * there are any lateral cross-references in it.
709 is_lateral = r->lateral || contain_vars_of_level((Node *) funcexprs, 0);
712 * OK, build an RTE for the function.
714 rte = addRangeTableEntryForFunction(pstate,
715 funcnames, funcexprs, coldeflists,
716 r, is_lateral, true);
722 * transformRangeTableSample --- transform a TABLESAMPLE clause
724 * Caller has already transformed rts->relation, we just have to validate
725 * the remaining fields and create a TableSampleClause node.
727 static TableSampleClause *
728 transformRangeTableSample(ParseState *pstate, RangeTableSample *rts)
730 TableSampleClause *tablesample;
739 * To validate the sample method name, look up the handler function, which
740 * has the same name, one dummy INTERNAL argument, and a result type of
741 * tsm_handler. (Note: tablesample method names are not schema-qualified
742 * in the SQL standard; but since they are just functions to us, we allow
743 * schema qualification to resolve any potential ambiguity.)
745 funcargtypes[0] = INTERNALOID;
747 handlerOid = LookupFuncName(rts->method, 1, funcargtypes, true);
749 /* we want error to complain about no-such-method, not no-such-function */
750 if (!OidIsValid(handlerOid))
752 (errcode(ERRCODE_UNDEFINED_OBJECT),
753 errmsg("tablesample method %s does not exist",
754 NameListToString(rts->method)),
755 parser_errposition(pstate, rts->location)));
757 /* check that handler has correct return type */
758 if (get_func_rettype(handlerOid) != TSM_HANDLEROID)
760 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
761 errmsg("function %s must return type \"%s\"",
762 NameListToString(rts->method), "tsm_handler"),
763 parser_errposition(pstate, rts->location)));
765 /* OK, run the handler to get TsmRoutine, for argument type info */
766 tsm = GetTsmRoutine(handlerOid);
768 tablesample = makeNode(TableSampleClause);
769 tablesample->tsmhandler = handlerOid;
771 /* check user provided the expected number of arguments */
772 if (list_length(rts->args) != list_length(tsm->parameterTypes))
774 (errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
775 errmsg_plural("tablesample method %s requires %d argument, not %d",
776 "tablesample method %s requires %d arguments, not %d",
777 list_length(tsm->parameterTypes),
778 NameListToString(rts->method),
779 list_length(tsm->parameterTypes),
780 list_length(rts->args)),
781 parser_errposition(pstate, rts->location)));
784 * Transform the arguments, typecasting them as needed. Note we must also
785 * assign collations now, because assign_query_collations() doesn't
786 * examine any substructure of RTEs.
789 forboth(larg, rts->args, ltyp, tsm->parameterTypes)
791 Node *arg = (Node *) lfirst(larg);
792 Oid argtype = lfirst_oid(ltyp);
794 arg = transformExpr(pstate, arg, EXPR_KIND_FROM_FUNCTION);
795 arg = coerce_to_specific_type(pstate, arg, argtype, "TABLESAMPLE");
796 assign_expr_collations(pstate, arg);
797 fargs = lappend(fargs, arg);
799 tablesample->args = fargs;
801 /* Process REPEATABLE (seed) */
802 if (rts->repeatable != NULL)
806 if (!tsm->repeatable_across_queries)
808 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
809 errmsg("tablesample method %s does not support REPEATABLE",
810 NameListToString(rts->method)),
811 parser_errposition(pstate, rts->location)));
813 arg = transformExpr(pstate, rts->repeatable, EXPR_KIND_FROM_FUNCTION);
814 arg = coerce_to_specific_type(pstate, arg, FLOAT8OID, "REPEATABLE");
815 assign_expr_collations(pstate, arg);
816 tablesample->repeatable = (Expr *) arg;
819 tablesample->repeatable = NULL;
826 * transformFromClauseItem -
827 * Transform a FROM-clause item, adding any required entries to the
828 * range table list being built in the ParseState, and return the
829 * transformed item ready to include in the joinlist. Also build a
830 * ParseNamespaceItem list describing the names exposed by this item.
831 * This routine can recurse to handle SQL92 JOIN expressions.
833 * The function return value is the node to add to the jointree (a
834 * RangeTblRef or JoinExpr). Additional output parameters are:
836 * *top_rte: receives the RTE corresponding to the jointree item.
837 * (We could extract this from the function return node, but it saves cycles
838 * to pass it back separately.)
840 * *top_rti: receives the rangetable index of top_rte. (Ditto.)
842 * *namespace: receives a List of ParseNamespaceItems for the RTEs exposed
843 * as table/column names by this item. (The lateral_only flags in these items
844 * are indeterminate and should be explicitly set by the caller before use.)
847 transformFromClauseItem(ParseState *pstate, Node *n,
848 RangeTblEntry **top_rte, int *top_rti,
851 if (IsA(n, RangeVar))
853 /* Plain relation reference, or perhaps a CTE reference */
854 RangeVar *rv = (RangeVar *) n;
856 RangeTblEntry *rte = NULL;
859 /* if it is an unqualified name, it might be a CTE reference */
862 CommonTableExpr *cte;
865 cte = scanNameSpaceForCTE(pstate, rv->relname, &levelsup);
867 rte = transformCTEReference(pstate, rv, cte, levelsup);
870 /* if not found as a CTE, must be a table reference */
872 rte = transformTableEntry(pstate, rv);
874 /* assume new rte is at end */
875 rtindex = list_length(pstate->p_rtable);
876 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
879 *namespace = list_make1(makeDefaultNSItem(rte));
880 rtr = makeNode(RangeTblRef);
881 rtr->rtindex = rtindex;
884 else if (IsA(n, RangeSubselect))
886 /* sub-SELECT is like a plain relation */
891 rte = transformRangeSubselect(pstate, (RangeSubselect *) n);
892 /* assume new rte is at end */
893 rtindex = list_length(pstate->p_rtable);
894 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
897 *namespace = list_make1(makeDefaultNSItem(rte));
898 rtr = makeNode(RangeTblRef);
899 rtr->rtindex = rtindex;
902 else if (IsA(n, RangeFunction))
904 /* function is like a plain relation */
909 rte = transformRangeFunction(pstate, (RangeFunction *) n);
910 /* assume new rte is at end */
911 rtindex = list_length(pstate->p_rtable);
912 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
915 *namespace = list_make1(makeDefaultNSItem(rte));
916 rtr = makeNode(RangeTblRef);
917 rtr->rtindex = rtindex;
920 else if (IsA(n, RangeTableSample))
922 /* TABLESAMPLE clause (wrapping some other valid FROM node) */
923 RangeTableSample *rts = (RangeTableSample *) n;
928 /* Recursively transform the contained relation */
929 rel = transformFromClauseItem(pstate, rts->relation,
930 top_rte, top_rti, namespace);
931 /* Currently, grammar could only return a RangeVar as contained rel */
932 Assert(IsA(rel, RangeTblRef));
933 rtr = (RangeTblRef *) rel;
934 rte = rt_fetch(rtr->rtindex, pstate->p_rtable);
935 /* We only support this on plain relations and matviews */
936 if (rte->relkind != RELKIND_RELATION &&
937 rte->relkind != RELKIND_MATVIEW)
939 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
940 errmsg("TABLESAMPLE clause can only be applied to tables and materialized views"),
941 parser_errposition(pstate, exprLocation(rts->relation))));
943 /* Transform TABLESAMPLE details and attach to the RTE */
944 rte->tablesample = transformRangeTableSample(pstate, rts);
947 else if (IsA(n, JoinExpr))
949 /* A newfangled join expression */
950 JoinExpr *j = (JoinExpr *) n;
951 RangeTblEntry *l_rte;
952 RangeTblEntry *r_rte;
965 int sv_namespace_length;
970 * Recursively process the left subtree, then the right. We must do
971 * it in this order for correct visibility of LATERAL references.
973 j->larg = transformFromClauseItem(pstate, j->larg,
979 * Make the left-side RTEs available for LATERAL access within the
980 * right side, by temporarily adding them to the pstate's namespace
981 * list. Per SQL:2008, if the join type is not INNER or LEFT then the
982 * left-side names must still be exposed, but it's an error to
983 * reference them. (Stupid design, but that's what it says.) Hence,
984 * we always push them into the namespace, but mark them as not
985 * lateral_ok if the jointype is wrong.
987 * Notice that we don't require the merged namespace list to be
988 * conflict-free. See the comments for scanNameSpaceForRefname().
990 * NB: this coding relies on the fact that list_concat is not
991 * destructive to its second argument.
993 lateral_ok = (j->jointype == JOIN_INNER || j->jointype == JOIN_LEFT);
994 setNamespaceLateralState(l_namespace, true, lateral_ok);
996 sv_namespace_length = list_length(pstate->p_namespace);
997 pstate->p_namespace = list_concat(pstate->p_namespace, l_namespace);
999 /* And now we can process the RHS */
1000 j->rarg = transformFromClauseItem(pstate, j->rarg,
1005 /* Remove the left-side RTEs from the namespace list again */
1006 pstate->p_namespace = list_truncate(pstate->p_namespace,
1007 sv_namespace_length);
1010 * Check for conflicting refnames in left and right subtrees. Must do
1011 * this because higher levels will assume I hand back a self-
1012 * consistent namespace list.
1014 checkNameSpaceConflicts(pstate, l_namespace, r_namespace);
1017 * Generate combined namespace info for possible use below.
1019 my_namespace = list_concat(l_namespace, r_namespace);
1022 * Extract column name and var lists from both subtrees
1024 * Note: expandRTE returns new lists, safe for me to modify
1026 expandRTE(l_rte, l_rtindex, 0, -1, false,
1027 &l_colnames, &l_colvars);
1028 expandRTE(r_rte, r_rtindex, 0, -1, false,
1029 &r_colnames, &r_colvars);
1032 * Natural join does not explicitly specify columns; must generate
1033 * columns to join. Need to run through the list of columns from each
1034 * table or join result and match up the column names. Use the first
1035 * table, and check every column in the second table for a match.
1036 * (We'll check that the matches were unique later on.) The result of
1037 * this step is a list of column names just like an explicitly-written
1046 Assert(j->usingClause == NIL); /* shouldn't have USING() too */
1048 foreach(lx, l_colnames)
1050 char *l_colname = strVal(lfirst(lx));
1051 Value *m_name = NULL;
1053 foreach(rx, r_colnames)
1055 char *r_colname = strVal(lfirst(rx));
1057 if (strcmp(l_colname, r_colname) == 0)
1059 m_name = makeString(l_colname);
1064 /* matched a right column? then keep as join column... */
1066 rlist = lappend(rlist, m_name);
1069 j->usingClause = rlist;
1073 * Now transform the join qualifications, if any.
1081 * JOIN/USING (or NATURAL JOIN, as transformed above). Transform
1082 * the list into an explicit ON-condition, and generate a list of
1083 * merged result columns.
1085 List *ucols = j->usingClause;
1086 List *l_usingvars = NIL;
1087 List *r_usingvars = NIL;
1090 Assert(j->quals == NULL); /* shouldn't have ON() too */
1092 foreach(ucol, ucols)
1094 char *u_colname = strVal(lfirst(ucol));
1102 /* Check for USING(foo,foo) */
1103 foreach(col, res_colnames)
1105 char *res_colname = strVal(lfirst(col));
1107 if (strcmp(res_colname, u_colname) == 0)
1109 (errcode(ERRCODE_DUPLICATE_COLUMN),
1110 errmsg("column name \"%s\" appears more than once in USING clause",
1114 /* Find it in left input */
1116 foreach(col, l_colnames)
1118 char *l_colname = strVal(lfirst(col));
1120 if (strcmp(l_colname, u_colname) == 0)
1124 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1125 errmsg("common column name \"%s\" appears more than once in left table",
1133 (errcode(ERRCODE_UNDEFINED_COLUMN),
1134 errmsg("column \"%s\" specified in USING clause does not exist in left table",
1137 /* Find it in right input */
1139 foreach(col, r_colnames)
1141 char *r_colname = strVal(lfirst(col));
1143 if (strcmp(r_colname, u_colname) == 0)
1147 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1148 errmsg("common column name \"%s\" appears more than once in right table",
1156 (errcode(ERRCODE_UNDEFINED_COLUMN),
1157 errmsg("column \"%s\" specified in USING clause does not exist in right table",
1160 l_colvar = list_nth(l_colvars, l_index);
1161 l_usingvars = lappend(l_usingvars, l_colvar);
1162 r_colvar = list_nth(r_colvars, r_index);
1163 r_usingvars = lappend(r_usingvars, r_colvar);
1165 res_colnames = lappend(res_colnames, lfirst(ucol));
1166 res_colvars = lappend(res_colvars,
1167 buildMergedJoinVar(pstate,
1173 j->quals = transformJoinUsingClause(pstate,
1181 /* User-written ON-condition; transform it */
1182 j->quals = transformJoinOnClause(pstate, j, my_namespace);
1186 /* CROSS JOIN: no quals */
1189 /* Add remaining columns from each side to the output columns */
1190 extractRemainingColumns(res_colnames,
1191 l_colnames, l_colvars,
1192 &l_colnames, &l_colvars);
1193 extractRemainingColumns(res_colnames,
1194 r_colnames, r_colvars,
1195 &r_colnames, &r_colvars);
1196 res_colnames = list_concat(res_colnames, l_colnames);
1197 res_colvars = list_concat(res_colvars, l_colvars);
1198 res_colnames = list_concat(res_colnames, r_colnames);
1199 res_colvars = list_concat(res_colvars, r_colvars);
1202 * Check alias (AS clause), if any.
1206 if (j->alias->colnames != NIL)
1208 if (list_length(j->alias->colnames) > list_length(res_colnames))
1210 (errcode(ERRCODE_SYNTAX_ERROR),
1211 errmsg("column alias list for \"%s\" has too many entries",
1212 j->alias->aliasname)));
1217 * Now build an RTE for the result of the join
1219 rte = addRangeTableEntryForJoin(pstate,
1226 /* assume new rte is at end */
1227 j->rtindex = list_length(pstate->p_rtable);
1228 Assert(rte == rt_fetch(j->rtindex, pstate->p_rtable));
1231 *top_rti = j->rtindex;
1233 /* make a matching link to the JoinExpr for later use */
1234 for (k = list_length(pstate->p_joinexprs) + 1; k < j->rtindex; k++)
1235 pstate->p_joinexprs = lappend(pstate->p_joinexprs, NULL);
1236 pstate->p_joinexprs = lappend(pstate->p_joinexprs, j);
1237 Assert(list_length(pstate->p_joinexprs) == j->rtindex);
1240 * Prepare returned namespace list. If the JOIN has an alias then it
1241 * hides the contained RTEs completely; otherwise, the contained RTEs
1242 * are still visible as table names, but are not visible for
1243 * unqualified column-name access.
1245 * Note: if there are nested alias-less JOINs, the lower-level ones
1246 * will remain in the list although they have neither p_rel_visible
1247 * nor p_cols_visible set. We could delete such list items, but it's
1248 * unclear that it's worth expending cycles to do so.
1250 if (j->alias != NULL)
1253 setNamespaceColumnVisibility(my_namespace, false);
1256 * The join RTE itself is always made visible for unqualified column
1257 * names. It's visible as a relation name only if it has an alias.
1259 *namespace = lappend(my_namespace,
1260 makeNamespaceItem(rte,
1269 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(n));
1270 return NULL; /* can't get here, keep compiler quiet */
1274 * buildMergedJoinVar -
1275 * generate a suitable replacement expression for a merged join column
1278 buildMergedJoinVar(ParseState *pstate, JoinType jointype,
1279 Var *l_colvar, Var *r_colvar)
1288 * Choose output type if input types are dissimilar.
1290 outcoltype = l_colvar->vartype;
1291 outcoltypmod = l_colvar->vartypmod;
1292 if (outcoltype != r_colvar->vartype)
1294 outcoltype = select_common_type(pstate,
1295 list_make2(l_colvar, r_colvar),
1298 outcoltypmod = -1; /* ie, unknown */
1300 else if (outcoltypmod != r_colvar->vartypmod)
1302 /* same type, but not same typmod */
1303 outcoltypmod = -1; /* ie, unknown */
1307 * Insert coercion functions if needed. Note that a difference in typmod
1308 * can only happen if input has typmod but outcoltypmod is -1. In that
1309 * case we insert a RelabelType to clearly mark that result's typmod is
1310 * not same as input. We never need coerce_type_typmod.
1312 if (l_colvar->vartype != outcoltype)
1313 l_node = coerce_type(pstate, (Node *) l_colvar, l_colvar->vartype,
1314 outcoltype, outcoltypmod,
1315 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1);
1316 else if (l_colvar->vartypmod != outcoltypmod)
1317 l_node = (Node *) makeRelabelType((Expr *) l_colvar,
1318 outcoltype, outcoltypmod,
1319 InvalidOid, /* fixed below */
1320 COERCE_IMPLICIT_CAST);
1322 l_node = (Node *) l_colvar;
1324 if (r_colvar->vartype != outcoltype)
1325 r_node = coerce_type(pstate, (Node *) r_colvar, r_colvar->vartype,
1326 outcoltype, outcoltypmod,
1327 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1);
1328 else if (r_colvar->vartypmod != outcoltypmod)
1329 r_node = (Node *) makeRelabelType((Expr *) r_colvar,
1330 outcoltype, outcoltypmod,
1331 InvalidOid, /* fixed below */
1332 COERCE_IMPLICIT_CAST);
1334 r_node = (Node *) r_colvar;
1337 * Choose what to emit
1344 * We can use either var; prefer non-coerced one if available.
1346 if (IsA(l_node, Var))
1348 else if (IsA(r_node, Var))
1354 /* Always use left var */
1358 /* Always use right var */
1364 * Here we must build a COALESCE expression to ensure that the
1365 * join output is non-null if either input is.
1367 CoalesceExpr *c = makeNode(CoalesceExpr);
1369 c->coalescetype = outcoltype;
1370 /* coalescecollid will get set below */
1371 c->args = list_make2(l_node, r_node);
1373 res_node = (Node *) c;
1377 elog(ERROR, "unrecognized join type: %d", (int) jointype);
1378 res_node = NULL; /* keep compiler quiet */
1383 * Apply assign_expr_collations to fix up the collation info in the
1384 * coercion and CoalesceExpr nodes, if we made any. This must be done now
1385 * so that the join node's alias vars show correct collation info.
1387 assign_expr_collations(pstate, res_node);
1393 * makeNamespaceItem -
1394 * Convenience subroutine to construct a ParseNamespaceItem.
1396 static ParseNamespaceItem *
1397 makeNamespaceItem(RangeTblEntry *rte, bool rel_visible, bool cols_visible,
1398 bool lateral_only, bool lateral_ok)
1400 ParseNamespaceItem *nsitem;
1402 nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1403 nsitem->p_rte = rte;
1404 nsitem->p_rel_visible = rel_visible;
1405 nsitem->p_cols_visible = cols_visible;
1406 nsitem->p_lateral_only = lateral_only;
1407 nsitem->p_lateral_ok = lateral_ok;
1412 * setNamespaceColumnVisibility -
1413 * Convenience subroutine to update cols_visible flags in a namespace list.
1416 setNamespaceColumnVisibility(List *namespace, bool cols_visible)
1420 foreach(lc, namespace)
1422 ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
1424 nsitem->p_cols_visible = cols_visible;
1429 * setNamespaceLateralState -
1430 * Convenience subroutine to update LATERAL flags in a namespace list.
1433 setNamespaceLateralState(List *namespace, bool lateral_only, bool lateral_ok)
1437 foreach(lc, namespace)
1439 ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
1441 nsitem->p_lateral_only = lateral_only;
1442 nsitem->p_lateral_ok = lateral_ok;
1448 * transformWhereClause -
1449 * Transform the qualification and make sure it is of type boolean.
1450 * Used for WHERE and allied clauses.
1452 * constructName does not affect the semantics, but is used in error messages
1455 transformWhereClause(ParseState *pstate, Node *clause,
1456 ParseExprKind exprKind, const char *constructName)
1463 qual = transformExpr(pstate, clause, exprKind);
1465 qual = coerce_to_boolean(pstate, qual, constructName);
1472 * transformLimitClause -
1473 * Transform the expression and make sure it is of type bigint.
1474 * Used for LIMIT and allied clauses.
1476 * Note: as of Postgres 8.2, LIMIT expressions are expected to yield int8,
1477 * rather than int4 as before.
1479 * constructName does not affect the semantics, but is used in error messages
1482 transformLimitClause(ParseState *pstate, Node *clause,
1483 ParseExprKind exprKind, const char *constructName)
1490 qual = transformExpr(pstate, clause, exprKind);
1492 qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
1494 /* LIMIT can't refer to any variables of the current query */
1495 checkExprIsVarFree(pstate, qual, constructName);
1501 * checkExprIsVarFree
1502 * Check that given expr has no Vars of the current query level
1503 * (aggregates and window functions should have been rejected already).
1505 * This is used to check expressions that have to have a consistent value
1506 * across all rows of the query, such as a LIMIT. Arguably it should reject
1507 * volatile functions, too, but we don't do that --- whatever value the
1508 * function gives on first execution is what you get.
1510 * constructName does not affect the semantics, but is used in error messages
1513 checkExprIsVarFree(ParseState *pstate, Node *n, const char *constructName)
1515 if (contain_vars_of_level(n, 0))
1518 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1519 /* translator: %s is name of a SQL construct, eg LIMIT */
1520 errmsg("argument of %s must not contain variables",
1522 parser_errposition(pstate,
1523 locate_var_of_level(n, 0))));
1529 * checkTargetlistEntrySQL92 -
1530 * Validate a targetlist entry found by findTargetlistEntrySQL92
1532 * When we select a pre-existing tlist entry as a result of syntax such
1533 * as "GROUP BY 1", we have to make sure it is acceptable for use in the
1534 * indicated clause type; transformExpr() will have treated it as a regular
1538 checkTargetlistEntrySQL92(ParseState *pstate, TargetEntry *tle,
1539 ParseExprKind exprKind)
1543 case EXPR_KIND_GROUP_BY:
1544 /* reject aggregates and window functions */
1545 if (pstate->p_hasAggs &&
1546 contain_aggs_of_level((Node *) tle->expr, 0))
1548 (errcode(ERRCODE_GROUPING_ERROR),
1549 /* translator: %s is name of a SQL construct, eg GROUP BY */
1550 errmsg("aggregate functions are not allowed in %s",
1551 ParseExprKindName(exprKind)),
1552 parser_errposition(pstate,
1553 locate_agg_of_level((Node *) tle->expr, 0))));
1554 if (pstate->p_hasWindowFuncs &&
1555 contain_windowfuncs((Node *) tle->expr))
1557 (errcode(ERRCODE_WINDOWING_ERROR),
1558 /* translator: %s is name of a SQL construct, eg GROUP BY */
1559 errmsg("window functions are not allowed in %s",
1560 ParseExprKindName(exprKind)),
1561 parser_errposition(pstate,
1562 locate_windowfunc((Node *) tle->expr))));
1564 case EXPR_KIND_ORDER_BY:
1565 /* no extra checks needed */
1567 case EXPR_KIND_DISTINCT_ON:
1568 /* no extra checks needed */
1571 elog(ERROR, "unexpected exprKind in checkTargetlistEntrySQL92");
1577 * findTargetlistEntrySQL92 -
1578 * Returns the targetlist entry matching the given (untransformed) node.
1579 * If no matching entry exists, one is created and appended to the target
1580 * list as a "resjunk" node.
1582 * This function supports the old SQL92 ORDER BY interpretation, where the
1583 * expression is an output column name or number. If we fail to find a
1584 * match of that sort, we fall through to the SQL99 rules. For historical
1585 * reasons, Postgres also allows this interpretation for GROUP BY, though
1586 * the standard never did. However, for GROUP BY we prefer a SQL99 match.
1587 * This function is *not* used for WINDOW definitions.
1589 * node the ORDER BY, GROUP BY, or DISTINCT ON expression to be matched
1590 * tlist the target list (passed by reference so we can append to it)
1591 * exprKind identifies clause type being processed
1593 static TargetEntry *
1594 findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist,
1595 ParseExprKind exprKind)
1600 * Handle two special cases as mandated by the SQL92 spec:
1602 * 1. Bare ColumnName (no qualifier or subscripts)
1603 * For a bare identifier, we search for a matching column name
1604 * in the existing target list. Multiple matches are an error
1605 * unless they refer to identical values; for example,
1606 * we allow SELECT a, a FROM table ORDER BY a
1607 * but not SELECT a AS b, b FROM table ORDER BY b
1608 * If no match is found, we fall through and treat the identifier
1610 * For GROUP BY, it is incorrect to match the grouping item against
1611 * targetlist entries: according to SQL92, an identifier in GROUP BY
1612 * is a reference to a column name exposed by FROM, not to a target
1613 * list column. However, many implementations (including pre-7.0
1614 * PostgreSQL) accept this anyway. So for GROUP BY, we look first
1615 * to see if the identifier matches any FROM column name, and only
1616 * try for a targetlist name if it doesn't. This ensures that we
1617 * adhere to the spec in the case where the name could be both.
1618 * DISTINCT ON isn't in the standard, so we can do what we like there;
1619 * we choose to make it work like ORDER BY, on the rather flimsy
1620 * grounds that ordinary DISTINCT works on targetlist entries.
1622 * 2. IntegerConstant
1623 * This means to use the n'th item in the existing target list.
1624 * Note that it would make no sense to order/group/distinct by an
1625 * actual constant, so this does not create a conflict with SQL99.
1626 * GROUP BY column-number is not allowed by SQL92, but since
1627 * the standard has no other behavior defined for this syntax,
1628 * we may as well accept this common extension.
1630 * Note that pre-existing resjunk targets must not be used in either case,
1631 * since the user didn't write them in his SELECT list.
1633 * If neither special case applies, fall through to treat the item as
1634 * an expression per SQL99.
1637 if (IsA(node, ColumnRef) &&
1638 list_length(((ColumnRef *) node)->fields) == 1 &&
1639 IsA(linitial(((ColumnRef *) node)->fields), String))
1641 char *name = strVal(linitial(((ColumnRef *) node)->fields));
1642 int location = ((ColumnRef *) node)->location;
1644 if (exprKind == EXPR_KIND_GROUP_BY)
1647 * In GROUP BY, we must prefer a match against a FROM-clause
1648 * column to one against the targetlist. Look to see if there is
1649 * a matching column. If so, fall through to use SQL99 rules.
1650 * NOTE: if name could refer ambiguously to more than one column
1651 * name exposed by FROM, colNameToVar will ereport(ERROR). That's
1652 * just what we want here.
1654 * Small tweak for 7.4.3: ignore matches in upper query levels.
1655 * This effectively changes the search order for bare names to (1)
1656 * local FROM variables, (2) local targetlist aliases, (3) outer
1657 * FROM variables, whereas before it was (1) (3) (2). SQL92 and
1658 * SQL99 do not allow GROUPing BY an outer reference, so this
1659 * breaks no cases that are legal per spec, and it seems a more
1660 * self-consistent behavior.
1662 if (colNameToVar(pstate, name, true, location) != NULL)
1668 TargetEntry *target_result = NULL;
1672 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1674 if (!tle->resjunk &&
1675 strcmp(tle->resname, name) == 0)
1677 if (target_result != NULL)
1679 if (!equal(target_result->expr, tle->expr))
1681 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1684 translator: first %s is name of a SQL construct, eg ORDER BY */
1685 errmsg("%s \"%s\" is ambiguous",
1686 ParseExprKindName(exprKind),
1688 parser_errposition(pstate, location)));
1691 target_result = tle;
1692 /* Stay in loop to check for ambiguity */
1695 if (target_result != NULL)
1697 /* return the first match, after suitable validation */
1698 checkTargetlistEntrySQL92(pstate, target_result, exprKind);
1699 return target_result;
1703 if (IsA(node, A_Const))
1705 Value *val = &((A_Const *) node)->val;
1706 int location = ((A_Const *) node)->location;
1707 int targetlist_pos = 0;
1710 if (!IsA(val, Integer))
1712 (errcode(ERRCODE_SYNTAX_ERROR),
1713 /* translator: %s is name of a SQL construct, eg ORDER BY */
1714 errmsg("non-integer constant in %s",
1715 ParseExprKindName(exprKind)),
1716 parser_errposition(pstate, location)));
1718 target_pos = intVal(val);
1721 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1725 if (++targetlist_pos == target_pos)
1727 /* return the unique match, after suitable validation */
1728 checkTargetlistEntrySQL92(pstate, tle, exprKind);
1734 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1735 /* translator: %s is name of a SQL construct, eg ORDER BY */
1736 errmsg("%s position %d is not in select list",
1737 ParseExprKindName(exprKind), target_pos),
1738 parser_errposition(pstate, location)));
1742 * Otherwise, we have an expression, so process it per SQL99 rules.
1744 return findTargetlistEntrySQL99(pstate, node, tlist, exprKind);
1748 * findTargetlistEntrySQL99 -
1749 * Returns the targetlist entry matching the given (untransformed) node.
1750 * If no matching entry exists, one is created and appended to the target
1751 * list as a "resjunk" node.
1753 * This function supports the SQL99 interpretation, wherein the expression
1754 * is just an ordinary expression referencing input column names.
1756 * node the ORDER BY, GROUP BY, etc expression to be matched
1757 * tlist the target list (passed by reference so we can append to it)
1758 * exprKind identifies clause type being processed
1760 static TargetEntry *
1761 findTargetlistEntrySQL99(ParseState *pstate, Node *node, List **tlist,
1762 ParseExprKind exprKind)
1764 TargetEntry *target_result;
1769 * Convert the untransformed node to a transformed expression, and search
1770 * for a match in the tlist. NOTE: it doesn't really matter whether there
1771 * is more than one match. Also, we are willing to match an existing
1772 * resjunk target here, though the SQL92 cases above must ignore resjunk
1775 expr = transformExpr(pstate, node, exprKind);
1779 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1783 * Ignore any implicit cast on the existing tlist expression.
1785 * This essentially allows the ORDER/GROUP/etc item to adopt the same
1786 * datatype previously selected for a textually-equivalent tlist item.
1787 * There can't be any implicit cast at top level in an ordinary SELECT
1788 * tlist at this stage, but the case does arise with ORDER BY in an
1789 * aggregate function.
1791 texpr = strip_implicit_coercions((Node *) tle->expr);
1793 if (equal(expr, texpr))
1798 * If no matches, construct a new target entry which is appended to the
1799 * end of the target list. This target is given resjunk = TRUE so that it
1800 * will not be projected into the final tuple.
1802 target_result = transformTargetEntry(pstate, node, expr, exprKind,
1805 *tlist = lappend(*tlist, target_result);
1807 return target_result;
1810 /*-------------------------------------------------------------------------
1811 * Flatten out parenthesized sublists in grouping lists, and some cases
1812 * of nested grouping sets.
1814 * Inside a grouping set (ROLLUP, CUBE, or GROUPING SETS), we expect the
1815 * content to be nested no more than 2 deep: i.e. ROLLUP((a,b),(c,d)) is
1816 * ok, but ROLLUP((a,(b,c)),d) is flattened to ((a,b,c),d), which we then
1817 * (later) normalize to ((a,b,c),(d)).
1819 * CUBE or ROLLUP can be nested inside GROUPING SETS (but not the reverse),
1820 * and we leave that alone if we find it. But if we see GROUPING SETS inside
1821 * GROUPING SETS, we can flatten and normalize as follows:
1822 * GROUPING SETS (a, (b,c), GROUPING SETS ((c,d),(e)), (f,g))
1824 * GROUPING SETS ((a), (b,c), (c,d), (e), (f,g))
1826 * This is per the spec's syntax transformations, but these are the only such
1827 * transformations we do in parse analysis, so that queries retain the
1828 * originally specified grouping set syntax for CUBE and ROLLUP as much as
1829 * possible when deparsed. (Full expansion of the result into a list of
1830 * grouping sets is left to the planner.)
1832 * When we're done, the resulting list should contain only these possible
1835 * - a CUBE or ROLLUP with a list of expressions nested 2 deep
1836 * - a GROUPING SET containing any of:
1837 * - expression lists
1838 * - empty grouping sets
1839 * - CUBE or ROLLUP nodes with lists nested 2 deep
1840 * The return is a new list, but doesn't deep-copy the old nodes except for
1841 * GroupingSet nodes.
1843 * As a side effect, flag whether the list has any GroupingSet nodes.
1844 *-------------------------------------------------------------------------
1847 flatten_grouping_sets(Node *expr, bool toplevel, bool *hasGroupingSets)
1849 /* just in case of pathological input */
1850 check_stack_depth();
1852 if (expr == (Node *) NIL)
1853 return (Node *) NIL;
1859 RowExpr *r = (RowExpr *) expr;
1861 if (r->row_format == COERCE_IMPLICIT_CAST)
1862 return flatten_grouping_sets((Node *) r->args,
1868 GroupingSet *gset = (GroupingSet *) expr;
1870 List *result_set = NIL;
1872 if (hasGroupingSets)
1873 *hasGroupingSets = true;
1876 * at the top level, we skip over all empty grouping sets; the
1877 * caller can supply the canonical GROUP BY () if nothing is
1881 if (toplevel && gset->kind == GROUPING_SET_EMPTY)
1882 return (Node *) NIL;
1884 foreach(l2, gset->content)
1886 Node *n1 = lfirst(l2);
1887 Node *n2 = flatten_grouping_sets(n1, false, NULL);
1889 if (IsA(n1, GroupingSet) &&
1890 ((GroupingSet *)n1)->kind == GROUPING_SET_SETS)
1892 result_set = list_concat(result_set, (List *) n2);
1895 result_set = lappend(result_set, n2);
1899 * At top level, keep the grouping set node; but if we're in a
1900 * nested grouping set, then we need to concat the flattened
1901 * result into the outer list if it's simply nested.
1904 if (toplevel || (gset->kind != GROUPING_SET_SETS))
1906 return (Node *) makeGroupingSet(gset->kind, result_set, gset->location);
1909 return (Node *) result_set;
1916 foreach(l, (List *) expr)
1918 Node *n = flatten_grouping_sets(lfirst(l), toplevel, hasGroupingSets);
1920 if (n != (Node *) NIL)
1923 result = list_concat(result, (List *) n);
1925 result = lappend(result, n);
1929 return (Node *) result;
1939 * Transform a single expression within a GROUP BY clause or grouping set.
1941 * The expression is added to the targetlist if not already present, and to the
1942 * flatresult list (which will become the groupClause) if not already present
1943 * there. The sortClause is consulted for operator and sort order hints.
1945 * Returns the ressortgroupref of the expression.
1947 * flatresult reference to flat list of SortGroupClause nodes
1948 * seen_local bitmapset of sortgrouprefs already seen at the local level
1950 * gexpr node to transform
1951 * targetlist reference to TargetEntry list
1952 * sortClause ORDER BY clause (SortGroupClause nodes)
1953 * exprKind expression kind
1954 * useSQL99 SQL99 rather than SQL92 syntax
1955 * toplevel false if within any grouping set
1958 transformGroupClauseExpr(List **flatresult, Bitmapset *seen_local,
1959 ParseState *pstate, Node *gexpr,
1960 List **targetlist, List *sortClause,
1961 ParseExprKind exprKind, bool useSQL99, bool toplevel)
1967 tle = findTargetlistEntrySQL99(pstate, gexpr,
1968 targetlist, exprKind);
1970 tle = findTargetlistEntrySQL92(pstate, gexpr,
1971 targetlist, exprKind);
1973 if (tle->ressortgroupref > 0)
1978 * Eliminate duplicates (GROUP BY x, x) but only at local level.
1979 * (Duplicates in grouping sets can affect the number of returned
1980 * rows, so can't be dropped indiscriminately.)
1982 * Since we don't care about anything except the sortgroupref, we can
1983 * use a bitmapset rather than scanning lists.
1985 if (bms_is_member(tle->ressortgroupref, seen_local))
1989 * If we're already in the flat clause list, we don't need to consider
1990 * adding ourselves again.
1992 found = targetIsInSortList(tle, InvalidOid, *flatresult);
1994 return tle->ressortgroupref;
1997 * If the GROUP BY tlist entry also appears in ORDER BY, copy operator
1998 * info from the (first) matching ORDER BY item. This means that if
1999 * you write something like "GROUP BY foo ORDER BY foo USING <<<", the
2000 * GROUP BY operation silently takes on the equality semantics implied
2001 * by the ORDER BY. There are two reasons to do this: it improves the
2002 * odds that we can implement both GROUP BY and ORDER BY with a single
2003 * sort step, and it allows the user to choose the equality semantics
2004 * used by GROUP BY, should she be working with a datatype that has
2005 * more than one equality operator.
2007 * If we're in a grouping set, though, we force our requested ordering
2008 * to be NULLS LAST, because if we have any hope of using a sorted agg
2009 * for the job, we're going to be tacking on generated NULL values
2010 * after the corresponding groups. If the user demands nulls first,
2011 * another sort step is going to be inevitable, but that's the
2012 * planner's problem.
2015 foreach(sl, sortClause)
2017 SortGroupClause *sc = (SortGroupClause *) lfirst(sl);
2019 if (sc->tleSortGroupRef == tle->ressortgroupref)
2021 SortGroupClause *grpc = copyObject(sc);
2024 grpc->nulls_first = false;
2025 *flatresult = lappend(*flatresult, grpc);
2033 * If no match in ORDER BY, just add it to the result using default
2034 * sort/group semantics.
2037 *flatresult = addTargetToGroupList(pstate, tle,
2038 *flatresult, *targetlist,
2039 exprLocation(gexpr),
2043 * _something_ must have assigned us a sortgroupref by now...
2046 return tle->ressortgroupref;
2050 * Transform a list of expressions within a GROUP BY clause or grouping set.
2052 * The list of expressions belongs to a single clause within which duplicates
2053 * can be safely eliminated.
2055 * Returns an integer list of ressortgroupref values.
2057 * flatresult reference to flat list of SortGroupClause nodes
2059 * list nodes to transform
2060 * targetlist reference to TargetEntry list
2061 * sortClause ORDER BY clause (SortGroupClause nodes)
2062 * exprKind expression kind
2063 * useSQL99 SQL99 rather than SQL92 syntax
2064 * toplevel false if within any grouping set
2067 transformGroupClauseList(List **flatresult,
2068 ParseState *pstate, List *list,
2069 List **targetlist, List *sortClause,
2070 ParseExprKind exprKind, bool useSQL99, bool toplevel)
2072 Bitmapset *seen_local = NULL;
2078 Node *gexpr = (Node *) lfirst(gl);
2080 Index ref = transformGroupClauseExpr(flatresult,
2092 seen_local = bms_add_member(seen_local, ref);
2093 result = lappend_int(result, ref);
2101 * Transform a grouping set and (recursively) its content.
2103 * The grouping set might be a GROUPING SETS node with other grouping sets
2104 * inside it, but SETS within SETS have already been flattened out before
2107 * Returns the transformed node, which now contains SIMPLE nodes with lists
2108 * of ressortgrouprefs rather than expressions.
2110 * flatresult reference to flat list of SortGroupClause nodes
2112 * gset grouping set to transform
2113 * targetlist reference to TargetEntry list
2114 * sortClause ORDER BY clause (SortGroupClause nodes)
2115 * exprKind expression kind
2116 * useSQL99 SQL99 rather than SQL92 syntax
2117 * toplevel false if within any grouping set
2120 transformGroupingSet(List **flatresult,
2121 ParseState *pstate, GroupingSet *gset,
2122 List **targetlist, List *sortClause,
2123 ParseExprKind exprKind, bool useSQL99, bool toplevel)
2126 List *content = NIL;
2128 Assert(toplevel || gset->kind != GROUPING_SET_SETS);
2130 foreach(gl, gset->content)
2132 Node *n = lfirst(gl);
2136 List *l = transformGroupClauseList(flatresult,
2138 targetlist, sortClause,
2139 exprKind, useSQL99, false);
2141 content = lappend(content, makeGroupingSet(GROUPING_SET_SIMPLE,
2145 else if (IsA(n, GroupingSet))
2147 GroupingSet *gset2 = (GroupingSet *) lfirst(gl);
2149 content = lappend(content, transformGroupingSet(flatresult,
2151 targetlist, sortClause,
2152 exprKind, useSQL99, false));
2156 Index ref = transformGroupClauseExpr(flatresult,
2166 content = lappend(content, makeGroupingSet(GROUPING_SET_SIMPLE,
2167 list_make1_int(ref),
2172 /* Arbitrarily cap the size of CUBE, which has exponential growth */
2173 if (gset->kind == GROUPING_SET_CUBE)
2175 if (list_length(content) > 12)
2177 (errcode(ERRCODE_TOO_MANY_COLUMNS),
2178 errmsg("CUBE is limited to 12 elements"),
2179 parser_errposition(pstate, gset->location)));
2182 return (Node *) makeGroupingSet(gset->kind, content, gset->location);
2187 * transformGroupClause -
2188 * transform a GROUP BY clause
2190 * GROUP BY items will be added to the targetlist (as resjunk columns)
2191 * if not already present, so the targetlist must be passed by reference.
2193 * This is also used for window PARTITION BY clauses (which act almost the
2194 * same, but are always interpreted per SQL99 rules).
2196 * Grouping sets make this a lot more complex than it was. Our goal here is
2197 * twofold: we make a flat list of SortGroupClause nodes referencing each
2198 * distinct expression used for grouping, with those expressions added to the
2199 * targetlist if needed. At the same time, we build the groupingSets tree,
2200 * which stores only ressortgrouprefs as integer lists inside GroupingSet nodes
2201 * (possibly nested, but limited in depth: a GROUPING_SET_SETS node can contain
2202 * nested SIMPLE, CUBE or ROLLUP nodes, but not more sets - we flatten that
2203 * out; while CUBE and ROLLUP can contain only SIMPLE nodes).
2205 * We skip much of the hard work if there are no grouping sets.
2207 * One subtlety is that the groupClause list can end up empty while the
2208 * groupingSets list is not; this happens if there are only empty grouping
2209 * sets, or an explicit GROUP BY (). This has the same effect as specifying
2210 * aggregates or a HAVING clause with no GROUP BY; the output is one row per
2211 * grouping set even if the input is empty.
2213 * Returns the transformed (flat) groupClause.
2216 * grouplist clause to transform
2217 * groupingSets reference to list to contain the grouping set tree
2218 * targetlist reference to TargetEntry list
2219 * sortClause ORDER BY clause (SortGroupClause nodes)
2220 * exprKind expression kind
2221 * useSQL99 SQL99 rather than SQL92 syntax
2224 transformGroupClause(ParseState *pstate, List *grouplist, List **groupingSets,
2225 List **targetlist, List *sortClause,
2226 ParseExprKind exprKind, bool useSQL99)
2229 List *flat_grouplist;
2232 bool hasGroupingSets = false;
2233 Bitmapset *seen_local = NULL;
2236 * Recursively flatten implicit RowExprs. (Technically this is only needed
2237 * for GROUP BY, per the syntax rules for grouping sets, but we do it
2240 flat_grouplist = (List *) flatten_grouping_sets((Node *) grouplist,
2245 * If the list is now empty, but hasGroupingSets is true, it's because we
2246 * elided redundant empty grouping sets. Restore a single empty grouping
2247 * set to leave a canonical form: GROUP BY ()
2250 if (flat_grouplist == NIL && hasGroupingSets)
2252 flat_grouplist = list_make1(makeGroupingSet(GROUPING_SET_EMPTY,
2254 exprLocation((Node *) grouplist)));
2257 foreach(gl, flat_grouplist)
2259 Node *gexpr = (Node *) lfirst(gl);
2261 if (IsA(gexpr, GroupingSet))
2263 GroupingSet *gset = (GroupingSet *) gexpr;
2267 case GROUPING_SET_EMPTY:
2268 gsets = lappend(gsets, gset);
2270 case GROUPING_SET_SIMPLE:
2274 case GROUPING_SET_SETS:
2275 case GROUPING_SET_CUBE:
2276 case GROUPING_SET_ROLLUP:
2277 gsets = lappend(gsets,
2278 transformGroupingSet(&result,
2280 targetlist, sortClause,
2281 exprKind, useSQL99, true));
2287 Index ref = transformGroupClauseExpr(&result, seen_local,
2289 targetlist, sortClause,
2290 exprKind, useSQL99, true);
2294 seen_local = bms_add_member(seen_local, ref);
2295 if (hasGroupingSets)
2296 gsets = lappend(gsets,
2297 makeGroupingSet(GROUPING_SET_SIMPLE,
2298 list_make1_int(ref),
2299 exprLocation(gexpr)));
2304 /* parser should prevent this */
2305 Assert(gsets == NIL || groupingSets != NULL);
2308 *groupingSets = gsets;
2314 * transformSortClause -
2315 * transform an ORDER BY clause
2317 * ORDER BY items will be added to the targetlist (as resjunk columns)
2318 * if not already present, so the targetlist must be passed by reference.
2320 * This is also used for window and aggregate ORDER BY clauses (which act
2321 * almost the same, but are always interpreted per SQL99 rules).
2324 transformSortClause(ParseState *pstate,
2327 ParseExprKind exprKind,
2328 bool resolveUnknown,
2331 List *sortlist = NIL;
2334 foreach(olitem, orderlist)
2336 SortBy *sortby = (SortBy *) lfirst(olitem);
2340 tle = findTargetlistEntrySQL99(pstate, sortby->node,
2341 targetlist, exprKind);
2343 tle = findTargetlistEntrySQL92(pstate, sortby->node,
2344 targetlist, exprKind);
2346 sortlist = addTargetToSortList(pstate, tle,
2347 sortlist, *targetlist, sortby,
2355 * transformWindowDefinitions -
2356 * transform window definitions (WindowDef to WindowClause)
2359 transformWindowDefinitions(ParseState *pstate,
2367 foreach(lc, windowdefs)
2369 WindowDef *windef = (WindowDef *) lfirst(lc);
2370 WindowClause *refwc = NULL;
2371 List *partitionClause;
2378 * Check for duplicate window names.
2381 findWindowClause(result, windef->name) != NULL)
2383 (errcode(ERRCODE_WINDOWING_ERROR),
2384 errmsg("window \"%s\" is already defined", windef->name),
2385 parser_errposition(pstate, windef->location)));
2388 * If it references a previous window, look that up.
2390 if (windef->refname)
2392 refwc = findWindowClause(result, windef->refname);
2395 (errcode(ERRCODE_UNDEFINED_OBJECT),
2396 errmsg("window \"%s\" does not exist",
2398 parser_errposition(pstate, windef->location)));
2402 * Transform PARTITION and ORDER specs, if any. These are treated
2403 * almost exactly like top-level GROUP BY and ORDER BY clauses,
2404 * including the special handling of nondefault operator semantics.
2406 orderClause = transformSortClause(pstate,
2407 windef->orderClause,
2409 EXPR_KIND_WINDOW_ORDER,
2410 true /* fix unknowns */ ,
2411 true /* force SQL99 rules */ );
2412 partitionClause = transformGroupClause(pstate,
2413 windef->partitionClause,
2417 EXPR_KIND_WINDOW_PARTITION,
2418 true /* force SQL99 rules */ );
2421 * And prepare the new WindowClause.
2423 wc = makeNode(WindowClause);
2424 wc->name = windef->name;
2425 wc->refname = windef->refname;
2428 * Per spec, a windowdef that references a previous one copies the
2429 * previous partition clause (and mustn't specify its own). It can
2430 * specify its own ordering clause, but only if the previous one had
2431 * none. It always specifies its own frame clause, and the previous
2432 * one must not have a frame clause. Yeah, it's bizarre that each of
2433 * these cases works differently, but SQL:2008 says so; see 7.11
2434 * <window clause> syntax rule 10 and general rule 1. The frame
2435 * clause rule is especially bizarre because it makes "OVER foo"
2436 * different from "OVER (foo)", and requires the latter to throw an
2437 * error if foo has a nondefault frame clause. Well, ours not to
2438 * reason why, but we do go out of our way to throw a useful error
2439 * message for such cases.
2443 if (partitionClause)
2445 (errcode(ERRCODE_WINDOWING_ERROR),
2446 errmsg("cannot override PARTITION BY clause of window \"%s\"",
2448 parser_errposition(pstate, windef->location)));
2449 wc->partitionClause = copyObject(refwc->partitionClause);
2452 wc->partitionClause = partitionClause;
2455 if (orderClause && refwc->orderClause)
2457 (errcode(ERRCODE_WINDOWING_ERROR),
2458 errmsg("cannot override ORDER BY clause of window \"%s\"",
2460 parser_errposition(pstate, windef->location)));
2463 wc->orderClause = orderClause;
2464 wc->copiedOrder = false;
2468 wc->orderClause = copyObject(refwc->orderClause);
2469 wc->copiedOrder = true;
2474 wc->orderClause = orderClause;
2475 wc->copiedOrder = false;
2477 if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
2480 * Use this message if this is a WINDOW clause, or if it's an OVER
2481 * clause that includes ORDER BY or framing clauses. (We already
2482 * rejected PARTITION BY above, so no need to check that.)
2485 orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
2487 (errcode(ERRCODE_WINDOWING_ERROR),
2488 errmsg("cannot copy window \"%s\" because it has a frame clause",
2490 parser_errposition(pstate, windef->location)));
2491 /* Else this clause is just OVER (foo), so say this: */
2493 (errcode(ERRCODE_WINDOWING_ERROR),
2494 errmsg("cannot copy window \"%s\" because it has a frame clause",
2496 errhint("Omit the parentheses in this OVER clause."),
2497 parser_errposition(pstate, windef->location)));
2499 wc->frameOptions = windef->frameOptions;
2500 /* Process frame offset expressions */
2501 wc->startOffset = transformFrameOffset(pstate, wc->frameOptions,
2502 windef->startOffset);
2503 wc->endOffset = transformFrameOffset(pstate, wc->frameOptions,
2505 wc->winref = winref;
2507 result = lappend(result, wc);
2514 * transformDistinctClause -
2515 * transform a DISTINCT clause
2517 * Since we may need to add items to the query's targetlist, that list
2518 * is passed by reference.
2520 * As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
2521 * possible into the distinctClause. This avoids a possible need to re-sort,
2522 * and allows the user to choose the equality semantics used by DISTINCT,
2523 * should she be working with a datatype that has more than one equality
2526 * is_agg is true if we are transforming an aggregate(DISTINCT ...)
2527 * function call. This does not affect any behavior, only the phrasing
2528 * of error messages.
2531 transformDistinctClause(ParseState *pstate,
2532 List **targetlist, List *sortClause, bool is_agg)
2539 * The distinctClause should consist of all ORDER BY items followed by all
2540 * other non-resjunk targetlist items. There must not be any resjunk
2541 * ORDER BY items --- that would imply that we are sorting by a value that
2542 * isn't necessarily unique within a DISTINCT group, so the results
2543 * wouldn't be well-defined. This construction ensures we follow the rule
2544 * that sortClause and distinctClause match; in fact the sortClause will
2545 * always be a prefix of distinctClause.
2547 * Note a corner case: the same TLE could be in the ORDER BY list multiple
2548 * times with different sortops. We have to include it in the
2549 * distinctClause the same way to preserve the prefix property. The net
2550 * effect will be that the TLE value will be made unique according to both
2553 foreach(slitem, sortClause)
2555 SortGroupClause *scl = (SortGroupClause *) lfirst(slitem);
2556 TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
2560 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2562 errmsg("in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list") :
2563 errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list"),
2564 parser_errposition(pstate,
2565 exprLocation((Node *) tle->expr))));
2566 result = lappend(result, copyObject(scl));
2570 * Now add any remaining non-resjunk tlist items, using default sort/group
2571 * semantics for their data types.
2573 foreach(tlitem, *targetlist)
2575 TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
2578 continue; /* ignore junk */
2579 result = addTargetToGroupList(pstate, tle,
2580 result, *targetlist,
2581 exprLocation((Node *) tle->expr),
2586 * Complain if we found nothing to make DISTINCT. Returning an empty list
2587 * would cause the parsed Query to look like it didn't have DISTINCT, with
2588 * results that would probably surprise the user. Note: this case is
2589 * presently impossible for aggregates because of grammar restrictions,
2590 * but we check anyway.
2594 (errcode(ERRCODE_SYNTAX_ERROR),
2596 errmsg("an aggregate with DISTINCT must have at least one argument") :
2597 errmsg("SELECT DISTINCT must have at least one column")));
2603 * transformDistinctOnClause -
2604 * transform a DISTINCT ON clause
2606 * Since we may need to add items to the query's targetlist, that list
2607 * is passed by reference.
2609 * As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
2610 * possible into the distinctClause. This avoids a possible need to re-sort,
2611 * and allows the user to choose the equality semantics used by DISTINCT,
2612 * should she be working with a datatype that has more than one equality
2616 transformDistinctOnClause(ParseState *pstate, List *distinctlist,
2617 List **targetlist, List *sortClause)
2620 List *sortgrouprefs = NIL;
2621 bool skipped_sortitem;
2626 * Add all the DISTINCT ON expressions to the tlist (if not already
2627 * present, they are added as resjunk items). Assign sortgroupref numbers
2628 * to them, and make a list of these numbers. (NB: we rely below on the
2629 * sortgrouprefs list being one-for-one with the original distinctlist.
2630 * Also notice that we could have duplicate DISTINCT ON expressions and
2631 * hence duplicate entries in sortgrouprefs.)
2633 foreach(lc, distinctlist)
2635 Node *dexpr = (Node *) lfirst(lc);
2639 tle = findTargetlistEntrySQL92(pstate, dexpr, targetlist,
2640 EXPR_KIND_DISTINCT_ON);
2641 sortgroupref = assignSortGroupRef(tle, *targetlist);
2642 sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
2646 * If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
2647 * semantics from ORDER BY items that match DISTINCT ON items, and also
2648 * adopt their column sort order. We insist that the distinctClause and
2649 * sortClause match, so throw error if we find the need to add any more
2650 * distinctClause items after we've skipped an ORDER BY item that wasn't
2653 skipped_sortitem = false;
2654 foreach(lc, sortClause)
2656 SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
2658 if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
2660 if (skipped_sortitem)
2662 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2663 errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2664 parser_errposition(pstate,
2665 get_matching_location(scl->tleSortGroupRef,
2669 result = lappend(result, copyObject(scl));
2672 skipped_sortitem = true;
2676 * Now add any remaining DISTINCT ON items, using default sort/group
2677 * semantics for their data types. (Note: this is pretty questionable; if
2678 * the ORDER BY list doesn't include all the DISTINCT ON items and more
2679 * besides, you certainly aren't using DISTINCT ON in the intended way,
2680 * and you probably aren't going to get consistent results. It might be
2681 * better to throw an error or warning here. But historically we've
2682 * allowed it, so keep doing so.)
2684 forboth(lc, distinctlist, lc2, sortgrouprefs)
2686 Node *dexpr = (Node *) lfirst(lc);
2687 int sortgroupref = lfirst_int(lc2);
2688 TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
2690 if (targetIsInSortList(tle, InvalidOid, result))
2691 continue; /* already in list (with some semantics) */
2692 if (skipped_sortitem)
2694 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2695 errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2696 parser_errposition(pstate, exprLocation(dexpr))));
2697 result = addTargetToGroupList(pstate, tle,
2698 result, *targetlist,
2699 exprLocation(dexpr),
2704 * An empty result list is impossible here because of grammar
2707 Assert(result != NIL);
2713 * get_matching_location
2714 * Get the exprLocation of the exprs member corresponding to the
2715 * (first) member of sortgrouprefs that equals sortgroupref.
2717 * This is used so that we can point at a troublesome DISTINCT ON entry.
2718 * (Note that we need to use the original untransformed DISTINCT ON list
2719 * item, as whatever TLE it corresponds to will very possibly have a
2720 * parse location pointing to some matching entry in the SELECT list
2721 * or ORDER BY list.)
2724 get_matching_location(int sortgroupref, List *sortgrouprefs, List *exprs)
2729 forboth(lcs, sortgrouprefs, lce, exprs)
2731 if (lfirst_int(lcs) == sortgroupref)
2732 return exprLocation((Node *) lfirst(lce));
2734 /* if no match, caller blew it */
2735 elog(ERROR, "get_matching_location: no matching sortgroupref");
2736 return -1; /* keep compiler quiet */
2740 * resolve_unique_index_expr
2741 * Infer a unique index from a list of indexElems, for ON
2744 * Perform parse analysis of expressions and columns appearing within ON
2745 * CONFLICT clause. During planning, the returned list of expressions is used
2746 * to infer which unique index to use.
2749 resolve_unique_index_expr(ParseState *pstate, InferClause *infer,
2755 foreach(l, infer->indexElems)
2757 IndexElem *ielem = (IndexElem *) lfirst(l);
2758 InferenceElem *pInfer = makeNode(InferenceElem);
2762 * Raw grammar re-uses CREATE INDEX infrastructure for unique index
2763 * inference clause, and so will accept opclasses by name and so on.
2765 * Make no attempt to match ASC or DESC ordering or NULLS FIRST/NULLS
2766 * LAST ordering, since those are not significant for inference
2767 * purposes (any unique index matching the inference specification in
2768 * other regards is accepted indifferently). Actively reject this as
2771 if (ielem->ordering != SORTBY_DEFAULT)
2773 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2774 errmsg("ASC/DESC is not allowed in ON CONFLICT clause"),
2775 parser_errposition(pstate,
2776 exprLocation((Node *) infer))));
2777 if (ielem->nulls_ordering != SORTBY_NULLS_DEFAULT)
2779 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2780 errmsg("NULLS FIRST/LAST is not allowed in ON CONFLICT clause"),
2781 parser_errposition(pstate,
2782 exprLocation((Node *) infer))));
2786 /* Simple index attribute */
2790 * Grammar won't have built raw expression for us in event of
2791 * plain column reference. Create one directly, and perform
2792 * expression transformation. Planner expects this, and performs
2793 * its own normalization for the purposes of matching against
2796 n = makeNode(ColumnRef);
2797 n->fields = list_make1(makeString(ielem->name));
2798 /* Location is approximately that of inference specification */
2799 n->location = infer->location;
2804 /* Do parse transformation of the raw expression */
2805 parse = (Node *) ielem->expr;
2809 * transformExpr() should have already rejected subqueries,
2810 * aggregates, and window functions, based on the EXPR_KIND_ for an
2811 * index expression. Expressions returning sets won't have been
2812 * rejected, but don't bother doing so here; there should be no
2813 * available expression unique index to match any such expression
2816 pInfer->expr = transformExpr(pstate, parse, EXPR_KIND_INDEX_EXPRESSION);
2818 /* Perform lookup of collation and operator class as required */
2819 if (!ielem->collation)
2820 pInfer->infercollid = InvalidOid;
2822 pInfer->infercollid = LookupCollation(pstate, ielem->collation,
2823 exprLocation(pInfer->expr));
2825 if (!ielem->opclass)
2826 pInfer->inferopclass = InvalidOid;
2828 pInfer->inferopclass = get_opclass_oid(BTREE_AM_OID,
2829 ielem->opclass, false);
2831 result = lappend(result, pInfer);
2838 * transformOnConflictArbiter -
2839 * transform arbiter expressions in an ON CONFLICT clause.
2841 * Transformed expressions used to infer one unique index relation to serve as
2842 * an ON CONFLICT arbiter. Partial unique indexes may be inferred using WHERE
2843 * clause from inference specification clause.
2846 transformOnConflictArbiter(ParseState *pstate,
2847 OnConflictClause *onConflictClause,
2848 List **arbiterExpr, Node **arbiterWhere,
2851 InferClause *infer = onConflictClause->infer;
2854 *arbiterWhere = NULL;
2855 *constraint = InvalidOid;
2857 if (onConflictClause->action == ONCONFLICT_UPDATE && !infer)
2859 (errcode(ERRCODE_SYNTAX_ERROR),
2860 errmsg("ON CONFLICT DO UPDATE requires inference specification or constraint name"),
2861 errhint("For example, ON CONFLICT (column_name)."),
2862 parser_errposition(pstate,
2863 exprLocation((Node *) onConflictClause))));
2866 * To simplify certain aspects of its design, speculative insertion into
2867 * system catalogs is disallowed
2869 if (IsCatalogRelation(pstate->p_target_relation))
2871 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2872 errmsg("ON CONFLICT is not supported with system catalog tables"),
2873 parser_errposition(pstate,
2874 exprLocation((Node *) onConflictClause))));
2876 /* Same applies to table used by logical decoding as catalog table */
2877 if (RelationIsUsedAsCatalogTable(pstate->p_target_relation))
2879 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2880 errmsg("ON CONFLICT is not supported on table \"%s\" used as a catalog table",
2881 RelationGetRelationName(pstate->p_target_relation)),
2882 parser_errposition(pstate,
2883 exprLocation((Node *) onConflictClause))));
2885 /* ON CONFLICT DO NOTHING does not require an inference clause */
2888 List *save_namespace;
2891 * While we process the arbiter expressions, accept only non-qualified
2892 * references to the target table. Hide any other relations.
2894 save_namespace = pstate->p_namespace;
2895 pstate->p_namespace = NIL;
2896 addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
2897 false, false, true);
2899 if (infer->indexElems)
2900 *arbiterExpr = resolve_unique_index_expr(pstate, infer,
2901 pstate->p_target_relation);
2904 * Handling inference WHERE clause (for partial unique index
2907 if (infer->whereClause)
2908 *arbiterWhere = transformExpr(pstate, infer->whereClause,
2909 EXPR_KIND_INDEX_PREDICATE);
2911 pstate->p_namespace = save_namespace;
2914 *constraint = get_relation_constraint_oid(RelationGetRelid(pstate->p_target_relation),
2915 infer->conname, false);
2919 * It's convenient to form a list of expressions based on the
2920 * representation used by CREATE INDEX, since the same restrictions are
2921 * appropriate (e.g. on subqueries). However, from here on, a dedicated
2922 * primnode representation is used for inference elements, and so
2923 * assign_query_collations() can be trusted to do the right thing with the
2924 * post parse analysis query tree inference clause representation.
2929 * addTargetToSortList
2930 * If the given targetlist entry isn't already in the SortGroupClause
2931 * list, add it to the end of the list, using the given sort ordering
2934 * If resolveUnknown is TRUE, convert TLEs of type UNKNOWN to TEXT. If not,
2935 * do nothing (which implies the search for a sort operator will fail).
2936 * pstate should be provided if resolveUnknown is TRUE, but can be NULL
2939 * Returns the updated SortGroupClause list.
2942 addTargetToSortList(ParseState *pstate, TargetEntry *tle,
2943 List *sortlist, List *targetlist, SortBy *sortby,
2944 bool resolveUnknown)
2946 Oid restype = exprType((Node *) tle->expr);
2952 ParseCallbackState pcbstate;
2954 /* if tlist item is an UNKNOWN literal, change it to TEXT */
2955 if (restype == UNKNOWNOID && resolveUnknown)
2957 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
2958 restype, TEXTOID, -1,
2960 COERCE_IMPLICIT_CAST,
2966 * Rather than clutter the API of get_sort_group_operators and the other
2967 * functions we're about to use, make use of error context callback to
2968 * mark any error reports with a parse position. We point to the operator
2969 * location if present, else to the expression being sorted. (NB: use the
2970 * original untransformed expression here; the TLE entry might well point
2971 * at a duplicate expression in the regular SELECT list.)
2973 location = sortby->location;
2975 location = exprLocation(sortby->node);
2976 setup_parser_errposition_callback(&pcbstate, pstate, location);
2978 /* determine the sortop, eqop, and directionality */
2979 switch (sortby->sortby_dir)
2981 case SORTBY_DEFAULT:
2983 get_sort_group_operators(restype,
2985 &sortop, &eqop, NULL,
2990 get_sort_group_operators(restype,
2992 NULL, &eqop, &sortop,
2997 Assert(sortby->useOp != NIL);
2998 sortop = compatible_oper_opid(sortby->useOp,
3004 * Verify it's a valid ordering operator, fetch the corresponding
3005 * equality operator, and determine whether to consider it like
3008 eqop = get_equality_op_for_ordering_op(sortop, &reverse);
3009 if (!OidIsValid(eqop))
3011 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3012 errmsg("operator %s is not a valid ordering operator",
3013 strVal(llast(sortby->useOp))),
3014 errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
3017 * Also see if the equality operator is hashable.
3019 hashable = op_hashjoinable(eqop, restype);
3022 elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
3023 sortop = InvalidOid; /* keep compiler quiet */
3030 cancel_parser_errposition_callback(&pcbstate);
3032 /* avoid making duplicate sortlist entries */
3033 if (!targetIsInSortList(tle, sortop, sortlist))
3035 SortGroupClause *sortcl = makeNode(SortGroupClause);
3037 sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
3039 sortcl->eqop = eqop;
3040 sortcl->sortop = sortop;
3041 sortcl->hashable = hashable;
3043 switch (sortby->sortby_nulls)
3045 case SORTBY_NULLS_DEFAULT:
3046 /* NULLS FIRST is default for DESC; other way for ASC */
3047 sortcl->nulls_first = reverse;
3049 case SORTBY_NULLS_FIRST:
3050 sortcl->nulls_first = true;
3052 case SORTBY_NULLS_LAST:
3053 sortcl->nulls_first = false;
3056 elog(ERROR, "unrecognized sortby_nulls: %d",
3057 sortby->sortby_nulls);
3061 sortlist = lappend(sortlist, sortcl);
3068 * addTargetToGroupList
3069 * If the given targetlist entry isn't already in the SortGroupClause
3070 * list, add it to the end of the list, using default sort/group
3073 * This is very similar to addTargetToSortList, except that we allow the
3074 * case where only a grouping (equality) operator can be found, and that
3075 * the TLE is considered "already in the list" if it appears there with any
3076 * sorting semantics.
3078 * location is the parse location to be fingered in event of trouble. Note
3079 * that we can't rely on exprLocation(tle->expr), because that might point
3080 * to a SELECT item that matches the GROUP BY item; it'd be pretty confusing
3081 * to report such a location.
3083 * If resolveUnknown is TRUE, convert TLEs of type UNKNOWN to TEXT. If not,
3084 * do nothing (which implies the search for an equality operator will fail).
3085 * pstate should be provided if resolveUnknown is TRUE, but can be NULL
3088 * Returns the updated SortGroupClause list.
3091 addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
3092 List *grouplist, List *targetlist, int location,
3093 bool resolveUnknown)
3095 Oid restype = exprType((Node *) tle->expr);
3097 /* if tlist item is an UNKNOWN literal, change it to TEXT */
3098 if (restype == UNKNOWNOID && resolveUnknown)
3100 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
3101 restype, TEXTOID, -1,
3103 COERCE_IMPLICIT_CAST,
3108 /* avoid making duplicate grouplist entries */
3109 if (!targetIsInSortList(tle, InvalidOid, grouplist))
3111 SortGroupClause *grpcl = makeNode(SortGroupClause);
3115 ParseCallbackState pcbstate;
3117 setup_parser_errposition_callback(&pcbstate, pstate, location);
3119 /* determine the eqop and optional sortop */
3120 get_sort_group_operators(restype,
3122 &sortop, &eqop, NULL,
3125 cancel_parser_errposition_callback(&pcbstate);
3127 grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
3129 grpcl->sortop = sortop;
3130 grpcl->nulls_first = false; /* OK with or without sortop */
3131 grpcl->hashable = hashable;
3133 grouplist = lappend(grouplist, grpcl);
3140 * assignSortGroupRef
3141 * Assign the targetentry an unused ressortgroupref, if it doesn't
3142 * already have one. Return the assigned or pre-existing refnumber.
3144 * 'tlist' is the targetlist containing (or to contain) the given targetentry.
3147 assignSortGroupRef(TargetEntry *tle, List *tlist)
3152 if (tle->ressortgroupref) /* already has one? */
3153 return tle->ressortgroupref;
3155 /* easiest way to pick an unused refnumber: max used + 1 */
3159 Index ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
3164 tle->ressortgroupref = maxRef + 1;
3165 return tle->ressortgroupref;
3169 * targetIsInSortList
3170 * Is the given target item already in the sortlist?
3171 * If sortop is not InvalidOid, also test for a match to the sortop.
3173 * It is not an oversight that this function ignores the nulls_first flag.
3174 * We check sortop when determining if an ORDER BY item is redundant with
3175 * earlier ORDER BY items, because it's conceivable that "ORDER BY
3176 * foo USING <, foo USING <<<" is not redundant, if <<< distinguishes
3177 * values that < considers equal. We need not check nulls_first
3178 * however, because a lower-order column with the same sortop but
3179 * opposite nulls direction is redundant. Also, we can consider
3180 * ORDER BY foo ASC, foo DESC redundant, so check for a commutator match.
3182 * Works for both ordering and grouping lists (sortop would normally be
3183 * InvalidOid when considering grouping). Note that the main reason we need
3184 * this routine (and not just a quick test for nonzeroness of ressortgroupref)
3185 * is that a TLE might be in only one of the lists.
3188 targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
3190 Index ref = tle->ressortgroupref;
3193 /* no need to scan list if tle has no marker */
3197 foreach(l, sortList)
3199 SortGroupClause *scl = (SortGroupClause *) lfirst(l);
3201 if (scl->tleSortGroupRef == ref &&
3202 (sortop == InvalidOid ||
3203 sortop == scl->sortop ||
3204 sortop == get_commutator(scl->sortop)))
3212 * Find the named WindowClause in the list, or return NULL if not there
3214 static WindowClause *
3215 findWindowClause(List *wclist, const char *name)
3221 WindowClause *wc = (WindowClause *) lfirst(l);
3223 if (wc->name && strcmp(wc->name, name) == 0)
3231 * transformFrameOffset
3232 * Process a window frame offset expression
3235 transformFrameOffset(ParseState *pstate, int frameOptions, Node *clause)
3237 const char *constructName = NULL;
3240 /* Quick exit if no offset expression */
3244 if (frameOptions & FRAMEOPTION_ROWS)
3246 /* Transform the raw expression tree */
3247 node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_ROWS);
3250 * Like LIMIT clause, simply coerce to int8
3252 constructName = "ROWS";
3253 node = coerce_to_specific_type(pstate, node, INT8OID, constructName);
3255 else if (frameOptions & FRAMEOPTION_RANGE)
3257 /* Transform the raw expression tree */
3258 node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_RANGE);
3261 * this needs a lot of thought to decide how to support in the context
3262 * of Postgres' extensible datatype framework
3264 constructName = "RANGE";
3265 /* error was already thrown by gram.y, this is just a backstop */
3266 elog(ERROR, "window frame with value offset is not implemented");
3274 /* Disallow variables in frame offsets */
3275 checkExprIsVarFree(pstate, node, constructName);