1/*-------------------------------------------------------------------------
2 *
3 * parse_clause.c
4 * handle clauses in parser
5 *
6 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/parser/parse_clause.c
12 *
13 *-------------------------------------------------------------------------
14 */
15
16#include "postgres.h"
17
18#include "miscadmin.h"
19
20#include "access/htup_details.h"
21#include "access/nbtree.h"
22#include "access/table.h"
23#include "access/tsmapi.h"
24#include "catalog/catalog.h"
25#include "catalog/heap.h"
26#include "catalog/pg_am.h"
27#include "catalog/pg_amproc.h"
28#include "catalog/pg_collation.h"
29#include "catalog/pg_constraint.h"
30#include "catalog/pg_type.h"
31#include "commands/defrem.h"
32#include "nodes/makefuncs.h"
33#include "nodes/nodeFuncs.h"
34#include "optimizer/optimizer.h"
35#include "parser/analyze.h"
36#include "parser/parsetree.h"
37#include "parser/parser.h"
38#include "parser/parse_clause.h"
39#include "parser/parse_coerce.h"
40#include "parser/parse_collate.h"
41#include "parser/parse_expr.h"
42#include "parser/parse_func.h"
43#include "parser/parse_oper.h"
44#include "parser/parse_relation.h"
45#include "parser/parse_target.h"
46#include "parser/parse_type.h"
47#include "rewrite/rewriteManip.h"
48#include "utils/builtins.h"
49#include "utils/guc.h"
50#include "utils/catcache.h"
51#include "utils/lsyscache.h"
52#include "utils/syscache.h"
53#include "utils/rel.h"
54
55
56/* Convenience macro for the most common makeNamespaceItem() case */
57#define makeDefaultNSItem(rte) makeNamespaceItem(rte, true, true, false, true)
58
59static void extractRemainingColumns(List *common_colnames,
60 List *src_colnames, List *src_colvars,
61 List **res_colnames, List **res_colvars);
62static Node *transformJoinUsingClause(ParseState *pstate,
63 RangeTblEntry *leftRTE, RangeTblEntry *rightRTE,
64 List *leftVars, List *rightVars);
65static Node *transformJoinOnClause(ParseState *pstate, JoinExpr *j,
66 List *namespace);
67static RangeTblEntry *getRTEForSpecialRelationTypes(ParseState *pstate,
68 RangeVar *rv);
69static RangeTblEntry *transformTableEntry(ParseState *pstate, RangeVar *r);
70static RangeTblEntry *transformRangeSubselect(ParseState *pstate,
71 RangeSubselect *r);
72static RangeTblEntry *transformRangeFunction(ParseState *pstate,
73 RangeFunction *r);
74static RangeTblEntry *transformRangeTableFunc(ParseState *pstate,
75 RangeTableFunc *t);
76static TableSampleClause *transformRangeTableSample(ParseState *pstate,
77 RangeTableSample *rts);
78static Node *transformFromClauseItem(ParseState *pstate, Node *n,
79 RangeTblEntry **top_rte, int *top_rti,
80 List **namespace);
81static Node *buildMergedJoinVar(ParseState *pstate, JoinType jointype,
82 Var *l_colvar, Var *r_colvar);
83static ParseNamespaceItem *makeNamespaceItem(RangeTblEntry *rte,
84 bool rel_visible, bool cols_visible,
85 bool lateral_only, bool lateral_ok);
86static void setNamespaceColumnVisibility(List *namespace, bool cols_visible);
87static void setNamespaceLateralState(List *namespace,
88 bool lateral_only, bool lateral_ok);
89static void checkExprIsVarFree(ParseState *pstate, Node *n,
90 const char *constructName);
91static TargetEntry *findTargetlistEntrySQL92(ParseState *pstate, Node *node,
92 List **tlist, ParseExprKind exprKind);
93static TargetEntry *findTargetlistEntrySQL99(ParseState *pstate, Node *node,
94 List **tlist, ParseExprKind exprKind);
95static int get_matching_location(int sortgroupref,
96 List *sortgrouprefs, List *exprs);
97static List *resolve_unique_index_expr(ParseState *pstate, InferClause *infer,
98 Relation heapRel);
99static List *addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
100 List *grouplist, List *targetlist, int location);
101static WindowClause *findWindowClause(List *wclist, const char *name);
102static Node *transformFrameOffset(ParseState *pstate, int frameOptions,
103 Oid rangeopfamily, Oid rangeopcintype, Oid *inRangeFunc,
104 Node *clause);
105
106
107/*
108 * transformFromClause -
109 * Process the FROM clause and add items to the query's range table,
110 * joinlist, and namespace.
111 *
112 * Note: we assume that the pstate's p_rtable, p_joinlist, and p_namespace
113 * lists were initialized to NIL when the pstate was created.
114 * We will add onto any entries already present --- this is needed for rule
115 * processing, as well as for UPDATE and DELETE.
116 */
117void
118transformFromClause(ParseState *pstate, List *frmList)
119{
120 ListCell *fl;
121
122 /*
123 * The grammar will have produced a list of RangeVars, RangeSubselects,
124 * RangeFunctions, and/or JoinExprs. Transform each one (possibly adding
125 * entries to the rtable), check for duplicate refnames, and then add it
126 * to the joinlist and namespace.
127 *
128 * Note we must process the items left-to-right for proper handling of
129 * LATERAL references.
130 */
131 foreach(fl, frmList)
132 {
133 Node *n = lfirst(fl);
134 RangeTblEntry *rte;
135 int rtindex;
136 List *namespace;
137
138 n = transformFromClauseItem(pstate, n,
139 &rte,
140 &rtindex,
141 &namespace);
142
143 checkNameSpaceConflicts(pstate, pstate->p_namespace, namespace);
144
145 /* Mark the new namespace items as visible only to LATERAL */
146 setNamespaceLateralState(namespace, true, true);
147
148 pstate->p_joinlist = lappend(pstate->p_joinlist, n);
149 pstate->p_namespace = list_concat(pstate->p_namespace, namespace);
150 }
151
152 /*
153 * We're done parsing the FROM list, so make all namespace items
154 * unconditionally visible. Note that this will also reset lateral_only
155 * for any namespace items that were already present when we were called;
156 * but those should have been that way already.
157 */
158 setNamespaceLateralState(pstate->p_namespace, false, true);
159}
160
161/*
162 * setTargetTable
163 * Add the target relation of INSERT/UPDATE/DELETE to the range table,
164 * and make the special links to it in the ParseState.
165 *
166 * We also open the target relation and acquire a write lock on it.
167 * This must be done before processing the FROM list, in case the target
168 * is also mentioned as a source relation --- we want to be sure to grab
169 * the write lock before any read lock.
170 *
171 * If alsoSource is true, add the target to the query's joinlist and
172 * namespace. For INSERT, we don't want the target to be joined to;
173 * it's a destination of tuples, not a source. For UPDATE/DELETE,
174 * we do need to scan or join the target. (NOTE: we do not bother
175 * to check for namespace conflict; we assume that the namespace was
176 * initially empty in these cases.)
177 *
178 * Finally, we mark the relation as requiring the permissions specified
179 * by requiredPerms.
180 *
181 * Returns the rangetable index of the target relation.
182 */
183int
184setTargetTable(ParseState *pstate, RangeVar *relation,
185 bool inh, bool alsoSource, AclMode requiredPerms)
186{
187 RangeTblEntry *rte;
188 int rtindex;
189
190 /*
191 * ENRs hide tables of the same name, so we need to check for them first.
192 * In contrast, CTEs don't hide tables (for this purpose).
193 */
194 if (relation->schemaname == NULL &&
195 scanNameSpaceForENR(pstate, relation->relname))
196 ereport(ERROR,
197 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
198 errmsg("relation \"%s\" cannot be the target of a modifying statement",
199 relation->relname)));
200
201 /* Close old target; this could only happen for multi-action rules */
202 if (pstate->p_target_relation != NULL)
203 table_close(pstate->p_target_relation, NoLock);
204
205 /*
206 * Open target rel and grab suitable lock (which we will hold till end of
207 * transaction).
208 *
209 * free_parsestate() will eventually do the corresponding table_close(),
210 * but *not* release the lock.
211 */
212 pstate->p_target_relation = parserOpenTable(pstate, relation,
213 RowExclusiveLock);
214
215 /*
216 * Now build an RTE.
217 */
218 rte = addRangeTableEntryForRelation(pstate, pstate->p_target_relation,
219 RowExclusiveLock,
220 relation->alias, inh, false);
221 pstate->p_target_rangetblentry = rte;
222
223 /* assume new rte is at end */
224 rtindex = list_length(pstate->p_rtable);
225 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
226
227 /*
228 * Override addRangeTableEntry's default ACL_SELECT permissions check, and
229 * instead mark target table as requiring exactly the specified
230 * permissions.
231 *
232 * If we find an explicit reference to the rel later during parse
233 * analysis, we will add the ACL_SELECT bit back again; see
234 * markVarForSelectPriv and its callers.
235 */
236 rte->requiredPerms = requiredPerms;
237
238 /*
239 * If UPDATE/DELETE, add table to joinlist and namespace.
240 *
241 * Note: some callers know that they can find the new ParseNamespaceItem
242 * at the end of the pstate->p_namespace list. This is a bit ugly but not
243 * worth complicating this function's signature for.
244 */
245 if (alsoSource)
246 addRTEtoQuery(pstate, rte, true, true, true);
247
248 return rtindex;
249}
250
251/*
252 * Extract all not-in-common columns from column lists of a source table
253 */
254static void
255extractRemainingColumns(List *common_colnames,
256 List *src_colnames, List *src_colvars,
257 List **res_colnames, List **res_colvars)
258{
259 List *new_colnames = NIL;
260 List *new_colvars = NIL;
261 ListCell *lnames,
262 *lvars;
263
264 Assert(list_length(src_colnames) == list_length(src_colvars));
265
266 forboth(lnames, src_colnames, lvars, src_colvars)
267 {
268 char *colname = strVal(lfirst(lnames));
269 bool match = false;
270 ListCell *cnames;
271
272 foreach(cnames, common_colnames)
273 {
274 char *ccolname = strVal(lfirst(cnames));
275
276 if (strcmp(colname, ccolname) == 0)
277 {
278 match = true;
279 break;
280 }
281 }
282
283 if (!match)
284 {
285 new_colnames = lappend(new_colnames, lfirst(lnames));
286 new_colvars = lappend(new_colvars, lfirst(lvars));
287 }
288 }
289
290 *res_colnames = new_colnames;
291 *res_colvars = new_colvars;
292}
293
294/* transformJoinUsingClause()
295 * Build a complete ON clause from a partially-transformed USING list.
296 * We are given lists of nodes representing left and right match columns.
297 * Result is a transformed qualification expression.
298 */
299static Node *
300transformJoinUsingClause(ParseState *pstate,
301 RangeTblEntry *leftRTE, RangeTblEntry *rightRTE,
302 List *leftVars, List *rightVars)
303{
304 Node *result;
305 List *andargs = NIL;
306 ListCell *lvars,
307 *rvars;
308
309 /*
310 * We cheat a little bit here by building an untransformed operator tree
311 * whose leaves are the already-transformed Vars. This requires collusion
312 * from transformExpr(), which normally could be expected to complain
313 * about already-transformed subnodes. However, this does mean that we
314 * have to mark the columns as requiring SELECT privilege for ourselves;
315 * transformExpr() won't do it.
316 */
317 forboth(lvars, leftVars, rvars, rightVars)
318 {
319 Var *lvar = (Var *) lfirst(lvars);
320 Var *rvar = (Var *) lfirst(rvars);
321 A_Expr *e;
322
323 /* Require read access to the join variables */
324 markVarForSelectPriv(pstate, lvar, leftRTE);
325 markVarForSelectPriv(pstate, rvar, rightRTE);
326
327 /* Now create the lvar = rvar join condition */
328 e = makeSimpleA_Expr(AEXPR_OP, "=",
329 (Node *) copyObject(lvar), (Node *) copyObject(rvar),
330 -1);
331
332 /* Prepare to combine into an AND clause, if multiple join columns */
333 andargs = lappend(andargs, e);
334 }
335
336 /* Only need an AND if there's more than one join column */
337 if (list_length(andargs) == 1)
338 result = (Node *) linitial(andargs);
339 else
340 result = (Node *) makeBoolExpr(AND_EXPR, andargs, -1);
341
342 /*
343 * Since the references are already Vars, and are certainly from the input
344 * relations, we don't have to go through the same pushups that
345 * transformJoinOnClause() does. Just invoke transformExpr() to fix up
346 * the operators, and we're done.
347 */
348 result = transformExpr(pstate, result, EXPR_KIND_JOIN_USING);
349
350 result = coerce_to_boolean(pstate, result, "JOIN/USING");
351
352 return result;
353}
354
355/* transformJoinOnClause()
356 * Transform the qual conditions for JOIN/ON.
357 * Result is a transformed qualification expression.
358 */
359static Node *
360transformJoinOnClause(ParseState *pstate, JoinExpr *j, List *namespace)
361{
362 Node *result;
363 List *save_namespace;
364
365 /*
366 * The namespace that the join expression should see is just the two
367 * subtrees of the JOIN plus any outer references from upper pstate
368 * levels. Temporarily set this pstate's namespace accordingly. (We need
369 * not check for refname conflicts, because transformFromClauseItem()
370 * already did.) All namespace items are marked visible regardless of
371 * LATERAL state.
372 */
373 setNamespaceLateralState(namespace, false, true);
374
375 save_namespace = pstate->p_namespace;
376 pstate->p_namespace = namespace;
377
378 result = transformWhereClause(pstate, j->quals,
379 EXPR_KIND_JOIN_ON, "JOIN/ON");
380
381 pstate->p_namespace = save_namespace;
382
383 return result;
384}
385
386/*
387 * transformTableEntry --- transform a RangeVar (simple relation reference)
388 */
389static RangeTblEntry *
390transformTableEntry(ParseState *pstate, RangeVar *r)
391{
392 RangeTblEntry *rte;
393
394 /* We need only build a range table entry */
395 rte = addRangeTableEntry(pstate, r, r->alias, r->inh, true);
396
397 return rte;
398}
399
400/*
401 * transformRangeSubselect --- transform a sub-SELECT appearing in FROM
402 */
403static RangeTblEntry *
404transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
405{
406 Query *query;
407 RangeTblEntry *rte;
408
409 /*
410 * We require user to supply an alias for a subselect, per SQL92. To relax
411 * this, we'd have to be prepared to gin up a unique alias for an
412 * unlabeled subselect. (This is just elog, not ereport, because the
413 * grammar should have enforced it already. It'd probably be better to
414 * report the error here, but we don't have a good error location here.)
415 */
416 if (r->alias == NULL)
417 elog(ERROR, "subquery in FROM must have an alias");
418
419 /*
420 * Set p_expr_kind to show this parse level is recursing to a subselect.
421 * We can't be nested within any expression, so don't need save-restore
422 * logic here.
423 */
424 Assert(pstate->p_expr_kind == EXPR_KIND_NONE);
425 pstate->p_expr_kind = EXPR_KIND_FROM_SUBSELECT;
426
427 /*
428 * If the subselect is LATERAL, make lateral_only names of this level
429 * visible to it. (LATERAL can't nest within a single pstate level, so we
430 * don't need save/restore logic here.)
431 */
432 Assert(!pstate->p_lateral_active);
433 pstate->p_lateral_active = r->lateral;
434
435 /*
436 * Analyze and transform the subquery.
437 */
438 query = parse_sub_analyze(r->subquery, pstate, NULL,
439 isLockedRefname(pstate, r->alias->aliasname),
440 true);
441
442 /* Restore state */
443 pstate->p_lateral_active = false;
444 pstate->p_expr_kind = EXPR_KIND_NONE;
445
446 /*
447 * Check that we got a SELECT. Anything else should be impossible given
448 * restrictions of the grammar, but check anyway.
449 */
450 if (!IsA(query, Query) ||
451 query->commandType != CMD_SELECT)
452 elog(ERROR, "unexpected non-SELECT command in subquery in FROM");
453
454 /*
455 * OK, build an RTE for the subquery.
456 */
457 rte = addRangeTableEntryForSubquery(pstate,
458 query,
459 r->alias,
460 r->lateral,
461 true);
462
463 return rte;
464}
465
466
467/*
468 * transformRangeFunction --- transform a function call appearing in FROM
469 */
470static RangeTblEntry *
471transformRangeFunction(ParseState *pstate, RangeFunction *r)
472{
473 List *funcexprs = NIL;
474 List *funcnames = NIL;
475 List *coldeflists = NIL;
476 bool is_lateral;
477 RangeTblEntry *rte;
478 ListCell *lc;
479
480 /*
481 * We make lateral_only names of this level visible, whether or not the
482 * RangeFunction is explicitly marked LATERAL. This is needed for SQL
483 * spec compliance in the case of UNNEST(), and seems useful on
484 * convenience grounds for all functions in FROM.
485 *
486 * (LATERAL can't nest within a single pstate level, so we don't need
487 * save/restore logic here.)
488 */
489 Assert(!pstate->p_lateral_active);
490 pstate->p_lateral_active = true;
491
492 /*
493 * Transform the raw expressions.
494 *
495 * While transforming, also save function names for possible use as alias
496 * and column names. We use the same transformation rules as for a SELECT
497 * output expression. For a FuncCall node, the result will be the
498 * function name, but it is possible for the grammar to hand back other
499 * node types.
500 *
501 * We have to get this info now, because FigureColname only works on raw
502 * parsetrees. Actually deciding what to do with the names is left up to
503 * addRangeTableEntryForFunction.
504 *
505 * Likewise, collect column definition lists if there were any. But
506 * complain if we find one here and the RangeFunction has one too.
507 */
508 foreach(lc, r->functions)
509 {
510 List *pair = (List *) lfirst(lc);
511 Node *fexpr;
512 List *coldeflist;
513 Node *newfexpr;
514 Node *last_srf;
515
516 /* Disassemble the function-call/column-def-list pairs */
517 Assert(list_length(pair) == 2);
518 fexpr = (Node *) linitial(pair);
519 coldeflist = (List *) lsecond(pair);
520
521 /*
522 * If we find a function call unnest() with more than one argument and
523 * no special decoration, transform it into separate unnest() calls on
524 * each argument. This is a kluge, for sure, but it's less nasty than
525 * other ways of implementing the SQL-standard UNNEST() syntax.
526 *
527 * If there is any decoration (including a coldeflist), we don't
528 * transform, which probably means a no-such-function error later. We
529 * could alternatively throw an error right now, but that doesn't seem
530 * tremendously helpful. If someone is using any such decoration,
531 * then they're not using the SQL-standard syntax, and they're more
532 * likely expecting an un-tweaked function call.
533 *
534 * Note: the transformation changes a non-schema-qualified unnest()
535 * function name into schema-qualified pg_catalog.unnest(). This
536 * choice is also a bit debatable, but it seems reasonable to force
537 * use of built-in unnest() when we make this transformation.
538 */
539 if (IsA(fexpr, FuncCall))
540 {
541 FuncCall *fc = (FuncCall *) fexpr;
542
543 if (list_length(fc->funcname) == 1 &&
544 strcmp(strVal(linitial(fc->funcname)), "unnest") == 0 &&
545 list_length(fc->args) > 1 &&
546 fc->agg_order == NIL &&
547 fc->agg_filter == NULL &&
548 !fc->agg_star &&
549 !fc->agg_distinct &&
550 !fc->func_variadic &&
551 fc->over == NULL &&
552 coldeflist == NIL)
553 {
554 ListCell *lc;
555
556 foreach(lc, fc->args)
557 {
558 Node *arg = (Node *) lfirst(lc);
559 FuncCall *newfc;
560
561 last_srf = pstate->p_last_srf;
562
563 newfc = makeFuncCall(SystemFuncName("unnest"),
564 list_make1(arg),
565 fc->location);
566
567 newfexpr = transformExpr(pstate, (Node *) newfc,
568 EXPR_KIND_FROM_FUNCTION);
569
570 /* nodeFunctionscan.c requires SRFs to be at top level */
571 if (pstate->p_last_srf != last_srf &&
572 pstate->p_last_srf != newfexpr)
573 ereport(ERROR,
574 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
575 errmsg("set-returning functions must appear at top level of FROM"),
576 parser_errposition(pstate,
577 exprLocation(pstate->p_last_srf))));
578
579 funcexprs = lappend(funcexprs, newfexpr);
580
581 funcnames = lappend(funcnames,
582 FigureColname((Node *) newfc));
583
584 /* coldeflist is empty, so no error is possible */
585
586 coldeflists = lappend(coldeflists, coldeflist);
587 }
588 continue; /* done with this function item */
589 }
590 }
591
592 /* normal case ... */
593 last_srf = pstate->p_last_srf;
594
595 newfexpr = transformExpr(pstate, fexpr,
596 EXPR_KIND_FROM_FUNCTION);
597
598 /* nodeFunctionscan.c requires SRFs to be at top level */
599 if (pstate->p_last_srf != last_srf &&
600 pstate->p_last_srf != newfexpr)
601 ereport(ERROR,
602 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
603 errmsg("set-returning functions must appear at top level of FROM"),
604 parser_errposition(pstate,
605 exprLocation(pstate->p_last_srf))));
606
607 funcexprs = lappend(funcexprs, newfexpr);
608
609 funcnames = lappend(funcnames,
610 FigureColname(fexpr));
611
612 if (coldeflist && r->coldeflist)
613 ereport(ERROR,
614 (errcode(ERRCODE_SYNTAX_ERROR),
615 errmsg("multiple column definition lists are not allowed for the same function"),
616 parser_errposition(pstate,
617 exprLocation((Node *) r->coldeflist))));
618
619 coldeflists = lappend(coldeflists, coldeflist);
620 }
621
622 pstate->p_lateral_active = false;
623
624 /*
625 * We must assign collations now so that the RTE exposes correct collation
626 * info for Vars created from it.
627 */
628 assign_list_collations(pstate, funcexprs);
629
630 /*
631 * Install the top-level coldeflist if there was one (we already checked
632 * that there was no conflicting per-function coldeflist).
633 *
634 * We only allow this when there's a single function (even after UNNEST
635 * expansion) and no WITH ORDINALITY. The reason for the latter
636 * restriction is that it's not real clear whether the ordinality column
637 * should be in the coldeflist, and users are too likely to make mistakes
638 * in one direction or the other. Putting the coldeflist inside ROWS
639 * FROM() is much clearer in this case.
640 */
641 if (r->coldeflist)
642 {
643 if (list_length(funcexprs) != 1)
644 {
645 if (r->is_rowsfrom)
646 ereport(ERROR,
647 (errcode(ERRCODE_SYNTAX_ERROR),
648 errmsg("ROWS FROM() with multiple functions cannot have a column definition list"),
649 errhint("Put a separate column definition list for each function inside ROWS FROM()."),
650 parser_errposition(pstate,
651 exprLocation((Node *) r->coldeflist))));
652 else
653 ereport(ERROR,
654 (errcode(ERRCODE_SYNTAX_ERROR),
655 errmsg("UNNEST() with multiple arguments cannot have a column definition list"),
656 errhint("Use separate UNNEST() calls inside ROWS FROM(), and attach a column definition list to each one."),
657 parser_errposition(pstate,
658 exprLocation((Node *) r->coldeflist))));
659 }
660 if (r->ordinality)
661 ereport(ERROR,
662 (errcode(ERRCODE_SYNTAX_ERROR),
663 errmsg("WITH ORDINALITY cannot be used with a column definition list"),
664 errhint("Put the column definition list inside ROWS FROM()."),
665 parser_errposition(pstate,
666 exprLocation((Node *) r->coldeflist))));
667
668 coldeflists = list_make1(r->coldeflist);
669 }
670
671 /*
672 * Mark the RTE as LATERAL if the user said LATERAL explicitly, or if
673 * there are any lateral cross-references in it.
674 */
675 is_lateral = r->lateral || contain_vars_of_level((Node *) funcexprs, 0);
676
677 /*
678 * OK, build an RTE for the function.
679 */
680 rte = addRangeTableEntryForFunction(pstate,
681 funcnames, funcexprs, coldeflists,
682 r, is_lateral, true);
683
684 return rte;
685}
686
687/*
688 * transformRangeTableFunc -
689 * Transform a raw RangeTableFunc into TableFunc.
690 *
691 * Transform the namespace clauses, the document-generating expression, the
692 * row-generating expression, the column-generating expressions, and the
693 * default value expressions.
694 */
695static RangeTblEntry *
696transformRangeTableFunc(ParseState *pstate, RangeTableFunc *rtf)
697{
698 TableFunc *tf = makeNode(TableFunc);
699 const char *constructName;
700 Oid docType;
701 RangeTblEntry *rte;
702 bool is_lateral;
703 ListCell *col;
704 char **names;
705 int colno;
706
707 /* Currently only XMLTABLE is supported */
708 constructName = "XMLTABLE";
709 docType = XMLOID;
710
711 /*
712 * We make lateral_only names of this level visible, whether or not the
713 * RangeTableFunc is explicitly marked LATERAL. This is needed for SQL
714 * spec compliance and seems useful on convenience grounds for all
715 * functions in FROM.
716 *
717 * (LATERAL can't nest within a single pstate level, so we don't need
718 * save/restore logic here.)
719 */
720 Assert(!pstate->p_lateral_active);
721 pstate->p_lateral_active = true;
722
723 /* Transform and apply typecast to the row-generating expression ... */
724 Assert(rtf->rowexpr != NULL);
725 tf->rowexpr = coerce_to_specific_type(pstate,
726 transformExpr(pstate, rtf->rowexpr, EXPR_KIND_FROM_FUNCTION),
727 TEXTOID,
728 constructName);
729 assign_expr_collations(pstate, tf->rowexpr);
730
731 /* ... and to the document itself */
732 Assert(rtf->docexpr != NULL);
733 tf->docexpr = coerce_to_specific_type(pstate,
734 transformExpr(pstate, rtf->docexpr, EXPR_KIND_FROM_FUNCTION),
735 docType,
736 constructName);
737 assign_expr_collations(pstate, tf->docexpr);
738
739 /* undef ordinality column number */
740 tf->ordinalitycol = -1;
741
742 /* Process column specs */
743 names = palloc(sizeof(char *) * list_length(rtf->columns));
744
745 colno = 0;
746 foreach(col, rtf->columns)
747 {
748 RangeTableFuncCol *rawc = (RangeTableFuncCol *) lfirst(col);
749 Oid typid;
750 int32 typmod;
751 Node *colexpr;
752 Node *coldefexpr;
753 int j;
754
755 tf->colnames = lappend(tf->colnames,
756 makeString(pstrdup(rawc->colname)));
757
758 /*
759 * Determine the type and typmod for the new column. FOR ORDINALITY
760 * columns are INTEGER per spec; the others are user-specified.
761 */
762 if (rawc->for_ordinality)
763 {
764 if (tf->ordinalitycol != -1)
765 ereport(ERROR,
766 (errcode(ERRCODE_SYNTAX_ERROR),
767 errmsg("only one FOR ORDINALITY column is allowed"),
768 parser_errposition(pstate, rawc->location)));
769
770 typid = INT4OID;
771 typmod = -1;
772 tf->ordinalitycol = colno;
773 }
774 else
775 {
776 if (rawc->typeName->setof)
777 ereport(ERROR,
778 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
779 errmsg("column \"%s\" cannot be declared SETOF",
780 rawc->colname),
781 parser_errposition(pstate, rawc->location)));
782
783 typenameTypeIdAndMod(pstate, rawc->typeName,
784 &typid, &typmod);
785 }
786
787 tf->coltypes = lappend_oid(tf->coltypes, typid);
788 tf->coltypmods = lappend_int(tf->coltypmods, typmod);
789 tf->colcollations = lappend_oid(tf->colcollations,
790 get_typcollation(typid));
791
792 /* Transform the PATH and DEFAULT expressions */
793 if (rawc->colexpr)
794 {
795 colexpr = coerce_to_specific_type(pstate,
796 transformExpr(pstate, rawc->colexpr,
797 EXPR_KIND_FROM_FUNCTION),
798 TEXTOID,
799 constructName);
800 assign_expr_collations(pstate, colexpr);
801 }
802 else
803 colexpr = NULL;
804
805 if (rawc->coldefexpr)
806 {
807 coldefexpr = coerce_to_specific_type_typmod(pstate,
808 transformExpr(pstate, rawc->coldefexpr,
809 EXPR_KIND_FROM_FUNCTION),
810 typid, typmod,
811 constructName);
812 assign_expr_collations(pstate, coldefexpr);
813 }
814 else
815 coldefexpr = NULL;
816
817 tf->colexprs = lappend(tf->colexprs, colexpr);
818 tf->coldefexprs = lappend(tf->coldefexprs, coldefexpr);
819
820 if (rawc->is_not_null)
821 tf->notnulls = bms_add_member(tf->notnulls, colno);
822
823 /* make sure column names are unique */
824 for (j = 0; j < colno; j++)
825 if (strcmp(names[j], rawc->colname) == 0)
826 ereport(ERROR,
827 (errcode(ERRCODE_SYNTAX_ERROR),
828 errmsg("column name \"%s\" is not unique",
829 rawc->colname),
830 parser_errposition(pstate, rawc->location)));
831 names[colno] = rawc->colname;
832
833 colno++;
834 }
835 pfree(names);
836
837 /* Namespaces, if any, also need to be transformed */
838 if (rtf->namespaces != NIL)
839 {
840 ListCell *ns;
841 ListCell *lc2;
842 List *ns_uris = NIL;
843 List *ns_names = NIL;
844 bool default_ns_seen = false;
845
846 foreach(ns, rtf->namespaces)
847 {
848 ResTarget *r = (ResTarget *) lfirst(ns);
849 Node *ns_uri;
850
851 Assert(IsA(r, ResTarget));
852 ns_uri = transformExpr(pstate, r->val, EXPR_KIND_FROM_FUNCTION);
853 ns_uri = coerce_to_specific_type(pstate, ns_uri,
854 TEXTOID, constructName);
855 assign_expr_collations(pstate, ns_uri);
856 ns_uris = lappend(ns_uris, ns_uri);
857
858 /* Verify consistency of name list: no dupes, only one DEFAULT */
859 if (r->name != NULL)
860 {
861 foreach(lc2, ns_names)
862 {
863 Value *ns_node = (Value *) lfirst(lc2);
864
865 if (ns_node == NULL)
866 continue;
867 if (strcmp(strVal(ns_node), r->name) == 0)
868 ereport(ERROR,
869 (errcode(ERRCODE_SYNTAX_ERROR),
870 errmsg("namespace name \"%s\" is not unique",
871 r->name),
872 parser_errposition(pstate, r->location)));
873 }
874 }
875 else
876 {
877 if (default_ns_seen)
878 ereport(ERROR,
879 (errcode(ERRCODE_SYNTAX_ERROR),
880 errmsg("only one default namespace is allowed"),
881 parser_errposition(pstate, r->location)));
882 default_ns_seen = true;
883 }
884
885 /* We represent DEFAULT by a null pointer */
886 ns_names = lappend(ns_names,
887 r->name ? makeString(r->name) : NULL);
888 }
889
890 tf->ns_uris = ns_uris;
891 tf->ns_names = ns_names;
892 }
893
894 tf->location = rtf->location;
895
896 pstate->p_lateral_active = false;
897
898 /*
899 * Mark the RTE as LATERAL if the user said LATERAL explicitly, or if
900 * there are any lateral cross-references in it.
901 */
902 is_lateral = rtf->lateral || contain_vars_of_level((Node *) tf, 0);
903
904 rte = addRangeTableEntryForTableFunc(pstate,
905 tf, rtf->alias, is_lateral, true);
906
907 return rte;
908}
909
910/*
911 * transformRangeTableSample --- transform a TABLESAMPLE clause
912 *
913 * Caller has already transformed rts->relation, we just have to validate
914 * the remaining fields and create a TableSampleClause node.
915 */
916static TableSampleClause *
917transformRangeTableSample(ParseState *pstate, RangeTableSample *rts)
918{
919 TableSampleClause *tablesample;
920 Oid handlerOid;
921 Oid funcargtypes[1];
922 TsmRoutine *tsm;
923 List *fargs;
924 ListCell *larg,
925 *ltyp;
926
927 /*
928 * To validate the sample method name, look up the handler function, which
929 * has the same name, one dummy INTERNAL argument, and a result type of
930 * tsm_handler. (Note: tablesample method names are not schema-qualified
931 * in the SQL standard; but since they are just functions to us, we allow
932 * schema qualification to resolve any potential ambiguity.)
933 */
934 funcargtypes[0] = INTERNALOID;
935
936 handlerOid = LookupFuncName(rts->method, 1, funcargtypes, true);
937
938 /* we want error to complain about no-such-method, not no-such-function */
939 if (!OidIsValid(handlerOid))
940 ereport(ERROR,
941 (errcode(ERRCODE_UNDEFINED_OBJECT),
942 errmsg("tablesample method %s does not exist",
943 NameListToString(rts->method)),
944 parser_errposition(pstate, rts->location)));
945
946 /* check that handler has correct return type */
947 if (get_func_rettype(handlerOid) != TSM_HANDLEROID)
948 ereport(ERROR,
949 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
950 errmsg("function %s must return type %s",
951 NameListToString(rts->method), "tsm_handler"),
952 parser_errposition(pstate, rts->location)));
953
954 /* OK, run the handler to get TsmRoutine, for argument type info */
955 tsm = GetTsmRoutine(handlerOid);
956
957 tablesample = makeNode(TableSampleClause);
958 tablesample->tsmhandler = handlerOid;
959
960 /* check user provided the expected number of arguments */
961 if (list_length(rts->args) != list_length(tsm->parameterTypes))
962 ereport(ERROR,
963 (errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
964 errmsg_plural("tablesample method %s requires %d argument, not %d",
965 "tablesample method %s requires %d arguments, not %d",
966 list_length(tsm->parameterTypes),
967 NameListToString(rts->method),
968 list_length(tsm->parameterTypes),
969 list_length(rts->args)),
970 parser_errposition(pstate, rts->location)));
971
972 /*
973 * Transform the arguments, typecasting them as needed. Note we must also
974 * assign collations now, because assign_query_collations() doesn't
975 * examine any substructure of RTEs.
976 */
977 fargs = NIL;
978 forboth(larg, rts->args, ltyp, tsm->parameterTypes)
979 {
980 Node *arg = (Node *) lfirst(larg);
981 Oid argtype = lfirst_oid(ltyp);
982
983 arg = transformExpr(pstate, arg, EXPR_KIND_FROM_FUNCTION);
984 arg = coerce_to_specific_type(pstate, arg, argtype, "TABLESAMPLE");
985 assign_expr_collations(pstate, arg);
986 fargs = lappend(fargs, arg);
987 }
988 tablesample->args = fargs;
989
990 /* Process REPEATABLE (seed) */
991 if (rts->repeatable != NULL)
992 {
993 Node *arg;
994
995 if (!tsm->repeatable_across_queries)
996 ereport(ERROR,
997 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
998 errmsg("tablesample method %s does not support REPEATABLE",
999 NameListToString(rts->method)),
1000 parser_errposition(pstate, rts->location)));
1001
1002 arg = transformExpr(pstate, rts->repeatable, EXPR_KIND_FROM_FUNCTION);
1003 arg = coerce_to_specific_type(pstate, arg, FLOAT8OID, "REPEATABLE");
1004 assign_expr_collations(pstate, arg);
1005 tablesample->repeatable = (Expr *) arg;
1006 }
1007 else
1008 tablesample->repeatable = NULL;
1009
1010 return tablesample;
1011}
1012
1013/*
1014 * getRTEForSpecialRelationTypes
1015 *
1016 * If given RangeVar refers to a CTE or an EphemeralNamedRelation,
1017 * build and return an appropriate RTE, otherwise return NULL
1018 */
1019static RangeTblEntry *
1020getRTEForSpecialRelationTypes(ParseState *pstate, RangeVar *rv)
1021{
1022 CommonTableExpr *cte;
1023 Index levelsup;
1024 RangeTblEntry *rte;
1025
1026 /*
1027 * if it is a qualified name, it can't be a CTE or tuplestore reference
1028 */
1029 if (rv->schemaname)
1030 return NULL;
1031
1032 cte = scanNameSpaceForCTE(pstate, rv->relname, &levelsup);
1033 if (cte)
1034 rte = addRangeTableEntryForCTE(pstate, cte, levelsup, rv, true);
1035 else if (scanNameSpaceForENR(pstate, rv->relname))
1036 rte = addRangeTableEntryForENR(pstate, rv, true);
1037 else
1038 rte = NULL;
1039
1040 return rte;
1041}
1042
1043/*
1044 * transformFromClauseItem -
1045 * Transform a FROM-clause item, adding any required entries to the
1046 * range table list being built in the ParseState, and return the
1047 * transformed item ready to include in the joinlist. Also build a
1048 * ParseNamespaceItem list describing the names exposed by this item.
1049 * This routine can recurse to handle SQL92 JOIN expressions.
1050 *
1051 * The function return value is the node to add to the jointree (a
1052 * RangeTblRef or JoinExpr). Additional output parameters are:
1053 *
1054 * *top_rte: receives the RTE corresponding to the jointree item.
1055 * (We could extract this from the function return node, but it saves cycles
1056 * to pass it back separately.)
1057 *
1058 * *top_rti: receives the rangetable index of top_rte. (Ditto.)
1059 *
1060 * *namespace: receives a List of ParseNamespaceItems for the RTEs exposed
1061 * as table/column names by this item. (The lateral_only flags in these items
1062 * are indeterminate and should be explicitly set by the caller before use.)
1063 */
1064static Node *
1065transformFromClauseItem(ParseState *pstate, Node *n,
1066 RangeTblEntry **top_rte, int *top_rti,
1067 List **namespace)
1068{
1069 if (IsA(n, RangeVar))
1070 {
1071 /* Plain relation reference, or perhaps a CTE reference */
1072 RangeVar *rv = (RangeVar *) n;
1073 RangeTblRef *rtr;
1074 RangeTblEntry *rte;
1075 int rtindex;
1076
1077 /* Check if it's a CTE or tuplestore reference */
1078 rte = getRTEForSpecialRelationTypes(pstate, rv);
1079
1080 /* if not found above, must be a table reference */
1081 if (!rte)
1082 rte = transformTableEntry(pstate, rv);
1083
1084 /* assume new rte is at end */
1085 rtindex = list_length(pstate->p_rtable);
1086 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
1087 *top_rte = rte;
1088 *top_rti = rtindex;
1089 *namespace = list_make1(makeDefaultNSItem(rte));
1090 rtr = makeNode(RangeTblRef);
1091 rtr->rtindex = rtindex;
1092 return (Node *) rtr;
1093 }
1094 else if (IsA(n, RangeSubselect))
1095 {
1096 /* sub-SELECT is like a plain relation */
1097 RangeTblRef *rtr;
1098 RangeTblEntry *rte;
1099 int rtindex;
1100
1101 rte = transformRangeSubselect(pstate, (RangeSubselect *) n);
1102 /* assume new rte is at end */
1103 rtindex = list_length(pstate->p_rtable);
1104 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
1105 *top_rte = rte;
1106 *top_rti = rtindex;
1107 *namespace = list_make1(makeDefaultNSItem(rte));
1108 rtr = makeNode(RangeTblRef);
1109 rtr->rtindex = rtindex;
1110 return (Node *) rtr;
1111 }
1112 else if (IsA(n, RangeFunction))
1113 {
1114 /* function is like a plain relation */
1115 RangeTblRef *rtr;
1116 RangeTblEntry *rte;
1117 int rtindex;
1118
1119 rte = transformRangeFunction(pstate, (RangeFunction *) n);
1120 /* assume new rte is at end */
1121 rtindex = list_length(pstate->p_rtable);
1122 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
1123 *top_rte = rte;
1124 *top_rti = rtindex;
1125 *namespace = list_make1(makeDefaultNSItem(rte));
1126 rtr = makeNode(RangeTblRef);
1127 rtr->rtindex = rtindex;
1128 return (Node *) rtr;
1129 }
1130 else if (IsA(n, RangeTableFunc))
1131 {
1132 /* table function is like a plain relation */
1133 RangeTblRef *rtr;
1134 RangeTblEntry *rte;
1135 int rtindex;
1136
1137 rte = transformRangeTableFunc(pstate, (RangeTableFunc *) n);
1138 /* assume new rte is at end */
1139 rtindex = list_length(pstate->p_rtable);
1140 Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
1141 *top_rte = rte;
1142 *top_rti = rtindex;
1143 *namespace = list_make1(makeDefaultNSItem(rte));
1144 rtr = makeNode(RangeTblRef);
1145 rtr->rtindex = rtindex;
1146 return (Node *) rtr;
1147 }
1148 else if (IsA(n, RangeTableSample))
1149 {
1150 /* TABLESAMPLE clause (wrapping some other valid FROM node) */
1151 RangeTableSample *rts = (RangeTableSample *) n;
1152 Node *rel;
1153 RangeTblRef *rtr;
1154 RangeTblEntry *rte;
1155
1156 /* Recursively transform the contained relation */
1157 rel = transformFromClauseItem(pstate, rts->relation,
1158 top_rte, top_rti, namespace);
1159 /* Currently, grammar could only return a RangeVar as contained rel */
1160 rtr = castNode(RangeTblRef, rel);
1161 rte = rt_fetch(rtr->rtindex, pstate->p_rtable);
1162 /* We only support this on plain relations and matviews */
1163 if (rte->relkind != RELKIND_RELATION &&
1164 rte->relkind != RELKIND_MATVIEW &&
1165 rte->relkind != RELKIND_PARTITIONED_TABLE)
1166 ereport(ERROR,
1167 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1168 errmsg("TABLESAMPLE clause can only be applied to tables and materialized views"),
1169 parser_errposition(pstate, exprLocation(rts->relation))));
1170
1171 /* Transform TABLESAMPLE details and attach to the RTE */
1172 rte->tablesample = transformRangeTableSample(pstate, rts);
1173 return (Node *) rtr;
1174 }
1175 else if (IsA(n, JoinExpr))
1176 {
1177 /* A newfangled join expression */
1178 JoinExpr *j = (JoinExpr *) n;
1179 RangeTblEntry *l_rte;
1180 RangeTblEntry *r_rte;
1181 int l_rtindex;
1182 int r_rtindex;
1183 List *l_namespace,
1184 *r_namespace,
1185 *my_namespace,
1186 *l_colnames,
1187 *r_colnames,
1188 *res_colnames,
1189 *l_colvars,
1190 *r_colvars,
1191 *res_colvars;
1192 bool lateral_ok;
1193 int sv_namespace_length;
1194 RangeTblEntry *rte;
1195 int k;
1196
1197 /*
1198 * Recursively process the left subtree, then the right. We must do
1199 * it in this order for correct visibility of LATERAL references.
1200 */
1201 j->larg = transformFromClauseItem(pstate, j->larg,
1202 &l_rte,
1203 &l_rtindex,
1204 &l_namespace);
1205
1206 /*
1207 * Make the left-side RTEs available for LATERAL access within the
1208 * right side, by temporarily adding them to the pstate's namespace
1209 * list. Per SQL:2008, if the join type is not INNER or LEFT then the
1210 * left-side names must still be exposed, but it's an error to
1211 * reference them. (Stupid design, but that's what it says.) Hence,
1212 * we always push them into the namespace, but mark them as not
1213 * lateral_ok if the jointype is wrong.
1214 *
1215 * Notice that we don't require the merged namespace list to be
1216 * conflict-free. See the comments for scanNameSpaceForRefname().
1217 *
1218 * NB: this coding relies on the fact that list_concat is not
1219 * destructive to its second argument.
1220 */
1221 lateral_ok = (j->jointype == JOIN_INNER || j->jointype == JOIN_LEFT);
1222 setNamespaceLateralState(l_namespace, true, lateral_ok);
1223
1224 sv_namespace_length = list_length(pstate->p_namespace);
1225 pstate->p_namespace = list_concat(pstate->p_namespace, l_namespace);
1226
1227 /* And now we can process the RHS */
1228 j->rarg = transformFromClauseItem(pstate, j->rarg,
1229 &r_rte,
1230 &r_rtindex,
1231 &r_namespace);
1232
1233 /* Remove the left-side RTEs from the namespace list again */
1234 pstate->p_namespace = list_truncate(pstate->p_namespace,
1235 sv_namespace_length);
1236
1237 /*
1238 * Check for conflicting refnames in left and right subtrees. Must do
1239 * this because higher levels will assume I hand back a self-
1240 * consistent namespace list.
1241 */
1242 checkNameSpaceConflicts(pstate, l_namespace, r_namespace);
1243
1244 /*
1245 * Generate combined namespace info for possible use below.
1246 */
1247 my_namespace = list_concat(l_namespace, r_namespace);
1248
1249 /*
1250 * Extract column name and var lists from both subtrees
1251 *
1252 * Note: expandRTE returns new lists, safe for me to modify
1253 */
1254 expandRTE(l_rte, l_rtindex, 0, -1, false,
1255 &l_colnames, &l_colvars);
1256 expandRTE(r_rte, r_rtindex, 0, -1, false,
1257 &r_colnames, &r_colvars);
1258
1259 /*
1260 * Natural join does not explicitly specify columns; must generate
1261 * columns to join. Need to run through the list of columns from each
1262 * table or join result and match up the column names. Use the first
1263 * table, and check every column in the second table for a match.
1264 * (We'll check that the matches were unique later on.) The result of
1265 * this step is a list of column names just like an explicitly-written
1266 * USING list.
1267 */
1268 if (j->isNatural)
1269 {
1270 List *rlist = NIL;
1271 ListCell *lx,
1272 *rx;
1273
1274 Assert(j->usingClause == NIL); /* shouldn't have USING() too */
1275
1276 foreach(lx, l_colnames)
1277 {
1278 char *l_colname = strVal(lfirst(lx));
1279 Value *m_name = NULL;
1280
1281 foreach(rx, r_colnames)
1282 {
1283 char *r_colname = strVal(lfirst(rx));
1284
1285 if (strcmp(l_colname, r_colname) == 0)
1286 {
1287 m_name = makeString(l_colname);
1288 break;
1289 }
1290 }
1291
1292 /* matched a right column? then keep as join column... */
1293 if (m_name != NULL)
1294 rlist = lappend(rlist, m_name);
1295 }
1296
1297 j->usingClause = rlist;
1298 }
1299
1300 /*
1301 * Now transform the join qualifications, if any.
1302 */
1303 res_colnames = NIL;
1304 res_colvars = NIL;
1305
1306 if (j->usingClause)
1307 {
1308 /*
1309 * JOIN/USING (or NATURAL JOIN, as transformed above). Transform
1310 * the list into an explicit ON-condition, and generate a list of
1311 * merged result columns.
1312 */
1313 List *ucols = j->usingClause;
1314 List *l_usingvars = NIL;
1315 List *r_usingvars = NIL;
1316 ListCell *ucol;
1317
1318 Assert(j->quals == NULL); /* shouldn't have ON() too */
1319
1320 foreach(ucol, ucols)
1321 {
1322 char *u_colname = strVal(lfirst(ucol));
1323 ListCell *col;
1324 int ndx;
1325 int l_index = -1;
1326 int r_index = -1;
1327 Var *l_colvar,
1328 *r_colvar;
1329
1330 /* Check for USING(foo,foo) */
1331 foreach(col, res_colnames)
1332 {
1333 char *res_colname = strVal(lfirst(col));
1334
1335 if (strcmp(res_colname, u_colname) == 0)
1336 ereport(ERROR,
1337 (errcode(ERRCODE_DUPLICATE_COLUMN),
1338 errmsg("column name \"%s\" appears more than once in USING clause",
1339 u_colname)));
1340 }
1341
1342 /* Find it in left input */
1343 ndx = 0;
1344 foreach(col, l_colnames)
1345 {
1346 char *l_colname = strVal(lfirst(col));
1347
1348 if (strcmp(l_colname, u_colname) == 0)
1349 {
1350 if (l_index >= 0)
1351 ereport(ERROR,
1352 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1353 errmsg("common column name \"%s\" appears more than once in left table",
1354 u_colname)));
1355 l_index = ndx;
1356 }
1357 ndx++;
1358 }
1359 if (l_index < 0)
1360 ereport(ERROR,
1361 (errcode(ERRCODE_UNDEFINED_COLUMN),
1362 errmsg("column \"%s\" specified in USING clause does not exist in left table",
1363 u_colname)));
1364
1365 /* Find it in right input */
1366 ndx = 0;
1367 foreach(col, r_colnames)
1368 {
1369 char *r_colname = strVal(lfirst(col));
1370
1371 if (strcmp(r_colname, u_colname) == 0)
1372 {
1373 if (r_index >= 0)
1374 ereport(ERROR,
1375 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1376 errmsg("common column name \"%s\" appears more than once in right table",
1377 u_colname)));
1378 r_index = ndx;
1379 }
1380 ndx++;
1381 }
1382 if (r_index < 0)
1383 ereport(ERROR,
1384 (errcode(ERRCODE_UNDEFINED_COLUMN),
1385 errmsg("column \"%s\" specified in USING clause does not exist in right table",
1386 u_colname)));
1387
1388 l_colvar = list_nth(l_colvars, l_index);
1389 l_usingvars = lappend(l_usingvars, l_colvar);
1390 r_colvar = list_nth(r_colvars, r_index);
1391 r_usingvars = lappend(r_usingvars, r_colvar);
1392
1393 res_colnames = lappend(res_colnames, lfirst(ucol));
1394 res_colvars = lappend(res_colvars,
1395 buildMergedJoinVar(pstate,
1396 j->jointype,
1397 l_colvar,
1398 r_colvar));
1399 }
1400
1401 j->quals = transformJoinUsingClause(pstate,
1402 l_rte,
1403 r_rte,
1404 l_usingvars,
1405 r_usingvars);
1406 }
1407 else if (j->quals)
1408 {
1409 /* User-written ON-condition; transform it */
1410 j->quals = transformJoinOnClause(pstate, j, my_namespace);
1411 }
1412 else
1413 {
1414 /* CROSS JOIN: no quals */
1415 }
1416
1417 /* Add remaining columns from each side to the output columns */
1418 extractRemainingColumns(res_colnames,
1419 l_colnames, l_colvars,
1420 &l_colnames, &l_colvars);
1421 extractRemainingColumns(res_colnames,
1422 r_colnames, r_colvars,
1423 &r_colnames, &r_colvars);
1424 res_colnames = list_concat(res_colnames, l_colnames);
1425 res_colvars = list_concat(res_colvars, l_colvars);
1426 res_colnames = list_concat(res_colnames, r_colnames);
1427 res_colvars = list_concat(res_colvars, r_colvars);
1428
1429 /*
1430 * Check alias (AS clause), if any.
1431 */
1432 if (j->alias)
1433 {
1434 if (j->alias->colnames != NIL)
1435 {
1436 if (list_length(j->alias->colnames) > list_length(res_colnames))
1437 ereport(ERROR,
1438 (errcode(ERRCODE_SYNTAX_ERROR),
1439 errmsg("column alias list for \"%s\" has too many entries",
1440 j->alias->aliasname)));
1441 }
1442 }
1443
1444 /*
1445 * Now build an RTE for the result of the join
1446 */
1447 rte = addRangeTableEntryForJoin(pstate,
1448 res_colnames,
1449 j->jointype,
1450 res_colvars,
1451 j->alias,
1452 true);
1453
1454 /* assume new rte is at end */
1455 j->rtindex = list_length(pstate->p_rtable);
1456 Assert(rte == rt_fetch(j->rtindex, pstate->p_rtable));
1457
1458 *top_rte = rte;
1459 *top_rti = j->rtindex;
1460
1461 /* make a matching link to the JoinExpr for later use */
1462 for (k = list_length(pstate->p_joinexprs) + 1; k < j->rtindex; k++)
1463 pstate->p_joinexprs = lappend(pstate->p_joinexprs, NULL);
1464 pstate->p_joinexprs = lappend(pstate->p_joinexprs, j);
1465 Assert(list_length(pstate->p_joinexprs) == j->rtindex);
1466
1467 /*
1468 * Prepare returned namespace list. If the JOIN has an alias then it
1469 * hides the contained RTEs completely; otherwise, the contained RTEs
1470 * are still visible as table names, but are not visible for
1471 * unqualified column-name access.
1472 *
1473 * Note: if there are nested alias-less JOINs, the lower-level ones
1474 * will remain in the list although they have neither p_rel_visible
1475 * nor p_cols_visible set. We could delete such list items, but it's
1476 * unclear that it's worth expending cycles to do so.
1477 */
1478 if (j->alias != NULL)
1479 my_namespace = NIL;
1480 else
1481 setNamespaceColumnVisibility(my_namespace, false);
1482
1483 /*
1484 * The join RTE itself is always made visible for unqualified column
1485 * names. It's visible as a relation name only if it has an alias.
1486 */
1487 *namespace = lappend(my_namespace,
1488 makeNamespaceItem(rte,
1489 (j->alias != NULL),
1490 true,
1491 false,
1492 true));
1493
1494 return (Node *) j;
1495 }
1496 else
1497 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(n));
1498 return NULL; /* can't get here, keep compiler quiet */
1499}
1500
1501/*
1502 * buildMergedJoinVar -
1503 * generate a suitable replacement expression for a merged join column
1504 */
1505static Node *
1506buildMergedJoinVar(ParseState *pstate, JoinType jointype,
1507 Var *l_colvar, Var *r_colvar)
1508{
1509 Oid outcoltype;
1510 int32 outcoltypmod;
1511 Node *l_node,
1512 *r_node,
1513 *res_node;
1514
1515 /*
1516 * Choose output type if input types are dissimilar.
1517 */
1518 outcoltype = l_colvar->vartype;
1519 outcoltypmod = l_colvar->vartypmod;
1520 if (outcoltype != r_colvar->vartype)
1521 {
1522 outcoltype = select_common_type(pstate,
1523 list_make2(l_colvar, r_colvar),
1524 "JOIN/USING",
1525 NULL);
1526 outcoltypmod = -1; /* ie, unknown */
1527 }
1528 else if (outcoltypmod != r_colvar->vartypmod)
1529 {
1530 /* same type, but not same typmod */
1531 outcoltypmod = -1; /* ie, unknown */
1532 }
1533
1534 /*
1535 * Insert coercion functions if needed. Note that a difference in typmod
1536 * can only happen if input has typmod but outcoltypmod is -1. In that
1537 * case we insert a RelabelType to clearly mark that result's typmod is
1538 * not same as input. We never need coerce_type_typmod.
1539 */
1540 if (l_colvar->vartype != outcoltype)
1541 l_node = coerce_type(pstate, (Node *) l_colvar, l_colvar->vartype,
1542 outcoltype, outcoltypmod,
1543 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1);
1544 else if (l_colvar->vartypmod != outcoltypmod)
1545 l_node = (Node *) makeRelabelType((Expr *) l_colvar,
1546 outcoltype, outcoltypmod,
1547 InvalidOid, /* fixed below */
1548 COERCE_IMPLICIT_CAST);
1549 else
1550 l_node = (Node *) l_colvar;
1551
1552 if (r_colvar->vartype != outcoltype)
1553 r_node = coerce_type(pstate, (Node *) r_colvar, r_colvar->vartype,
1554 outcoltype, outcoltypmod,
1555 COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1);
1556 else if (r_colvar->vartypmod != outcoltypmod)
1557 r_node = (Node *) makeRelabelType((Expr *) r_colvar,
1558 outcoltype, outcoltypmod,
1559 InvalidOid, /* fixed below */
1560 COERCE_IMPLICIT_CAST);
1561 else
1562 r_node = (Node *) r_colvar;
1563
1564 /*
1565 * Choose what to emit
1566 */
1567 switch (jointype)
1568 {
1569 case JOIN_INNER:
1570
1571 /*
1572 * We can use either var; prefer non-coerced one if available.
1573 */
1574 if (IsA(l_node, Var))
1575 res_node = l_node;
1576 else if (IsA(r_node, Var))
1577 res_node = r_node;
1578 else
1579 res_node = l_node;
1580 break;
1581 case JOIN_LEFT:
1582 /* Always use left var */
1583 res_node = l_node;
1584 break;
1585 case JOIN_RIGHT:
1586 /* Always use right var */
1587 res_node = r_node;
1588 break;
1589 case JOIN_FULL:
1590 {
1591 /*
1592 * Here we must build a COALESCE expression to ensure that the
1593 * join output is non-null if either input is.
1594 */
1595 CoalesceExpr *c = makeNode(CoalesceExpr);
1596
1597 c->coalescetype = outcoltype;
1598 /* coalescecollid will get set below */
1599 c->args = list_make2(l_node, r_node);
1600 c->location = -1;
1601 res_node = (Node *) c;
1602 break;
1603 }
1604 default:
1605 elog(ERROR, "unrecognized join type: %d", (int) jointype);
1606 res_node = NULL; /* keep compiler quiet */
1607 break;
1608 }
1609
1610 /*
1611 * Apply assign_expr_collations to fix up the collation info in the
1612 * coercion and CoalesceExpr nodes, if we made any. This must be done now
1613 * so that the join node's alias vars show correct collation info.
1614 */
1615 assign_expr_collations(pstate, res_node);
1616
1617 return res_node;
1618}
1619
1620/*
1621 * makeNamespaceItem -
1622 * Convenience subroutine to construct a ParseNamespaceItem.
1623 */
1624static ParseNamespaceItem *
1625makeNamespaceItem(RangeTblEntry *rte, bool rel_visible, bool cols_visible,
1626 bool lateral_only, bool lateral_ok)
1627{
1628 ParseNamespaceItem *nsitem;
1629
1630 nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
1631 nsitem->p_rte = rte;
1632 nsitem->p_rel_visible = rel_visible;
1633 nsitem->p_cols_visible = cols_visible;
1634 nsitem->p_lateral_only = lateral_only;
1635 nsitem->p_lateral_ok = lateral_ok;
1636 return nsitem;
1637}
1638
1639/*
1640 * setNamespaceColumnVisibility -
1641 * Convenience subroutine to update cols_visible flags in a namespace list.
1642 */
1643static void
1644setNamespaceColumnVisibility(List *namespace, bool cols_visible)
1645{
1646 ListCell *lc;
1647
1648 foreach(lc, namespace)
1649 {
1650 ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
1651
1652 nsitem->p_cols_visible = cols_visible;
1653 }
1654}
1655
1656/*
1657 * setNamespaceLateralState -
1658 * Convenience subroutine to update LATERAL flags in a namespace list.
1659 */
1660static void
1661setNamespaceLateralState(List *namespace, bool lateral_only, bool lateral_ok)
1662{
1663 ListCell *lc;
1664
1665 foreach(lc, namespace)
1666 {
1667 ParseNamespaceItem *nsitem = (ParseNamespaceItem *) lfirst(lc);
1668
1669 nsitem->p_lateral_only = lateral_only;
1670 nsitem->p_lateral_ok = lateral_ok;
1671 }
1672}
1673
1674
1675/*
1676 * transformWhereClause -
1677 * Transform the qualification and make sure it is of type boolean.
1678 * Used for WHERE and allied clauses.
1679 *
1680 * constructName does not affect the semantics, but is used in error messages
1681 */
1682Node *
1683transformWhereClause(ParseState *pstate, Node *clause,
1684 ParseExprKind exprKind, const char *constructName)
1685{
1686 Node *qual;
1687
1688 if (clause == NULL)
1689 return NULL;
1690
1691 qual = transformExpr(pstate, clause, exprKind);
1692
1693 qual = coerce_to_boolean(pstate, qual, constructName);
1694
1695 return qual;
1696}
1697
1698
1699/*
1700 * transformLimitClause -
1701 * Transform the expression and make sure it is of type bigint.
1702 * Used for LIMIT and allied clauses.
1703 *
1704 * Note: as of Postgres 8.2, LIMIT expressions are expected to yield int8,
1705 * rather than int4 as before.
1706 *
1707 * constructName does not affect the semantics, but is used in error messages
1708 */
1709Node *
1710transformLimitClause(ParseState *pstate, Node *clause,
1711 ParseExprKind exprKind, const char *constructName)
1712{
1713 Node *qual;
1714
1715 if (clause == NULL)
1716 return NULL;
1717
1718 qual = transformExpr(pstate, clause, exprKind);
1719
1720 qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
1721
1722 /* LIMIT can't refer to any variables of the current query */
1723 checkExprIsVarFree(pstate, qual, constructName);
1724
1725 return qual;
1726}
1727
1728/*
1729 * checkExprIsVarFree
1730 * Check that given expr has no Vars of the current query level
1731 * (aggregates and window functions should have been rejected already).
1732 *
1733 * This is used to check expressions that have to have a consistent value
1734 * across all rows of the query, such as a LIMIT. Arguably it should reject
1735 * volatile functions, too, but we don't do that --- whatever value the
1736 * function gives on first execution is what you get.
1737 *
1738 * constructName does not affect the semantics, but is used in error messages
1739 */
1740static void
1741checkExprIsVarFree(ParseState *pstate, Node *n, const char *constructName)
1742{
1743 if (contain_vars_of_level(n, 0))
1744 {
1745 ereport(ERROR,
1746 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1747 /* translator: %s is name of a SQL construct, eg LIMIT */
1748 errmsg("argument of %s must not contain variables",
1749 constructName),
1750 parser_errposition(pstate,
1751 locate_var_of_level(n, 0))));
1752 }
1753}
1754
1755
1756/*
1757 * checkTargetlistEntrySQL92 -
1758 * Validate a targetlist entry found by findTargetlistEntrySQL92
1759 *
1760 * When we select a pre-existing tlist entry as a result of syntax such
1761 * as "GROUP BY 1", we have to make sure it is acceptable for use in the
1762 * indicated clause type; transformExpr() will have treated it as a regular
1763 * targetlist item.
1764 */
1765static void
1766checkTargetlistEntrySQL92(ParseState *pstate, TargetEntry *tle,
1767 ParseExprKind exprKind)
1768{
1769 switch (exprKind)
1770 {
1771 case EXPR_KIND_GROUP_BY:
1772 /* reject aggregates and window functions */
1773 if (pstate->p_hasAggs &&
1774 contain_aggs_of_level((Node *) tle->expr, 0))
1775 ereport(ERROR,
1776 (errcode(ERRCODE_GROUPING_ERROR),
1777 /* translator: %s is name of a SQL construct, eg GROUP BY */
1778 errmsg("aggregate functions are not allowed in %s",
1779 ParseExprKindName(exprKind)),
1780 parser_errposition(pstate,
1781 locate_agg_of_level((Node *) tle->expr, 0))));
1782 if (pstate->p_hasWindowFuncs &&
1783 contain_windowfuncs((Node *) tle->expr))
1784 ereport(ERROR,
1785 (errcode(ERRCODE_WINDOWING_ERROR),
1786 /* translator: %s is name of a SQL construct, eg GROUP BY */
1787 errmsg("window functions are not allowed in %s",
1788 ParseExprKindName(exprKind)),
1789 parser_errposition(pstate,
1790 locate_windowfunc((Node *) tle->expr))));
1791 break;
1792 case EXPR_KIND_ORDER_BY:
1793 /* no extra checks needed */
1794 break;
1795 case EXPR_KIND_DISTINCT_ON:
1796 /* no extra checks needed */
1797 break;
1798 default:
1799 elog(ERROR, "unexpected exprKind in checkTargetlistEntrySQL92");
1800 break;
1801 }
1802}
1803
1804/*
1805 * findTargetlistEntrySQL92 -
1806 * Returns the targetlist entry matching the given (untransformed) node.
1807 * If no matching entry exists, one is created and appended to the target
1808 * list as a "resjunk" node.
1809 *
1810 * This function supports the old SQL92 ORDER BY interpretation, where the
1811 * expression is an output column name or number. If we fail to find a
1812 * match of that sort, we fall through to the SQL99 rules. For historical
1813 * reasons, Postgres also allows this interpretation for GROUP BY, though
1814 * the standard never did. However, for GROUP BY we prefer a SQL99 match.
1815 * This function is *not* used for WINDOW definitions.
1816 *
1817 * node the ORDER BY, GROUP BY, or DISTINCT ON expression to be matched
1818 * tlist the target list (passed by reference so we can append to it)
1819 * exprKind identifies clause type being processed
1820 */
1821static TargetEntry *
1822findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist,
1823 ParseExprKind exprKind)
1824{
1825 ListCell *tl;
1826
1827 /*----------
1828 * Handle two special cases as mandated by the SQL92 spec:
1829 *
1830 * 1. Bare ColumnName (no qualifier or subscripts)
1831 * For a bare identifier, we search for a matching column name
1832 * in the existing target list. Multiple matches are an error
1833 * unless they refer to identical values; for example,
1834 * we allow SELECT a, a FROM table ORDER BY a
1835 * but not SELECT a AS b, b FROM table ORDER BY b
1836 * If no match is found, we fall through and treat the identifier
1837 * as an expression.
1838 * For GROUP BY, it is incorrect to match the grouping item against
1839 * targetlist entries: according to SQL92, an identifier in GROUP BY
1840 * is a reference to a column name exposed by FROM, not to a target
1841 * list column. However, many implementations (including pre-7.0
1842 * PostgreSQL) accept this anyway. So for GROUP BY, we look first
1843 * to see if the identifier matches any FROM column name, and only
1844 * try for a targetlist name if it doesn't. This ensures that we
1845 * adhere to the spec in the case where the name could be both.
1846 * DISTINCT ON isn't in the standard, so we can do what we like there;
1847 * we choose to make it work like ORDER BY, on the rather flimsy
1848 * grounds that ordinary DISTINCT works on targetlist entries.
1849 *
1850 * 2. IntegerConstant
1851 * This means to use the n'th item in the existing target list.
1852 * Note that it would make no sense to order/group/distinct by an
1853 * actual constant, so this does not create a conflict with SQL99.
1854 * GROUP BY column-number is not allowed by SQL92, but since
1855 * the standard has no other behavior defined for this syntax,
1856 * we may as well accept this common extension.
1857 *
1858 * Note that pre-existing resjunk targets must not be used in either case,
1859 * since the user didn't write them in his SELECT list.
1860 *
1861 * If neither special case applies, fall through to treat the item as
1862 * an expression per SQL99.
1863 *----------
1864 */
1865 if (IsA(node, ColumnRef) &&
1866 list_length(((ColumnRef *) node)->fields) == 1 &&
1867 IsA(linitial(((ColumnRef *) node)->fields), String))
1868 {
1869 char *name = strVal(linitial(((ColumnRef *) node)->fields));
1870 int location = ((ColumnRef *) node)->location;
1871
1872 if (exprKind == EXPR_KIND_GROUP_BY)
1873 {
1874 /*
1875 * In GROUP BY, we must prefer a match against a FROM-clause
1876 * column to one against the targetlist. Look to see if there is
1877 * a matching column. If so, fall through to use SQL99 rules.
1878 * NOTE: if name could refer ambiguously to more than one column
1879 * name exposed by FROM, colNameToVar will ereport(ERROR). That's
1880 * just what we want here.
1881 *
1882 * Small tweak for 7.4.3: ignore matches in upper query levels.
1883 * This effectively changes the search order for bare names to (1)
1884 * local FROM variables, (2) local targetlist aliases, (3) outer
1885 * FROM variables, whereas before it was (1) (3) (2). SQL92 and
1886 * SQL99 do not allow GROUPing BY an outer reference, so this
1887 * breaks no cases that are legal per spec, and it seems a more
1888 * self-consistent behavior.
1889 */
1890 if (colNameToVar(pstate, name, true, location) != NULL)
1891 name = NULL;
1892 }
1893
1894 if (name != NULL)
1895 {
1896 TargetEntry *target_result = NULL;
1897
1898 foreach(tl, *tlist)
1899 {
1900 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1901
1902 if (!tle->resjunk &&
1903 strcmp(tle->resname, name) == 0)
1904 {
1905 if (target_result != NULL)
1906 {
1907 if (!equal(target_result->expr, tle->expr))
1908 ereport(ERROR,
1909 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
1910
1911 /*------
1912 translator: first %s is name of a SQL construct, eg ORDER BY */
1913 errmsg("%s \"%s\" is ambiguous",
1914 ParseExprKindName(exprKind),
1915 name),
1916 parser_errposition(pstate, location)));
1917 }
1918 else
1919 target_result = tle;
1920 /* Stay in loop to check for ambiguity */
1921 }
1922 }
1923 if (target_result != NULL)
1924 {
1925 /* return the first match, after suitable validation */
1926 checkTargetlistEntrySQL92(pstate, target_result, exprKind);
1927 return target_result;
1928 }
1929 }
1930 }
1931 if (IsA(node, A_Const))
1932 {
1933 Value *val = &((A_Const *) node)->val;
1934 int location = ((A_Const *) node)->location;
1935 int targetlist_pos = 0;
1936 int target_pos;
1937
1938 if (!IsA(val, Integer))
1939 ereport(ERROR,
1940 (errcode(ERRCODE_SYNTAX_ERROR),
1941 /* translator: %s is name of a SQL construct, eg ORDER BY */
1942 errmsg("non-integer constant in %s",
1943 ParseExprKindName(exprKind)),
1944 parser_errposition(pstate, location)));
1945
1946 target_pos = intVal(val);
1947 foreach(tl, *tlist)
1948 {
1949 TargetEntry *tle = (TargetEntry *) lfirst(tl);
1950
1951 if (!tle->resjunk)
1952 {
1953 if (++targetlist_pos == target_pos)
1954 {
1955 /* return the unique match, after suitable validation */
1956 checkTargetlistEntrySQL92(pstate, tle, exprKind);
1957 return tle;
1958 }
1959 }
1960 }
1961 ereport(ERROR,
1962 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1963 /* translator: %s is name of a SQL construct, eg ORDER BY */
1964 errmsg("%s position %d is not in select list",
1965 ParseExprKindName(exprKind), target_pos),
1966 parser_errposition(pstate, location)));
1967 }
1968
1969 /*
1970 * Otherwise, we have an expression, so process it per SQL99 rules.
1971 */
1972 return findTargetlistEntrySQL99(pstate, node, tlist, exprKind);
1973}
1974
1975/*
1976 * findTargetlistEntrySQL99 -
1977 * Returns the targetlist entry matching the given (untransformed) node.
1978 * If no matching entry exists, one is created and appended to the target
1979 * list as a "resjunk" node.
1980 *
1981 * This function supports the SQL99 interpretation, wherein the expression
1982 * is just an ordinary expression referencing input column names.
1983 *
1984 * node the ORDER BY, GROUP BY, etc expression to be matched
1985 * tlist the target list (passed by reference so we can append to it)
1986 * exprKind identifies clause type being processed
1987 */
1988static TargetEntry *
1989findTargetlistEntrySQL99(ParseState *pstate, Node *node, List **tlist,
1990 ParseExprKind exprKind)
1991{
1992 TargetEntry *target_result;
1993 ListCell *tl;
1994 Node *expr;
1995
1996 /*
1997 * Convert the untransformed node to a transformed expression, and search
1998 * for a match in the tlist. NOTE: it doesn't really matter whether there
1999 * is more than one match. Also, we are willing to match an existing
2000 * resjunk target here, though the SQL92 cases above must ignore resjunk
2001 * targets.
2002 */
2003 expr = transformExpr(pstate, node, exprKind);
2004
2005 foreach(tl, *tlist)
2006 {
2007 TargetEntry *tle = (TargetEntry *) lfirst(tl);
2008 Node *texpr;
2009
2010 /*
2011 * Ignore any implicit cast on the existing tlist expression.
2012 *
2013 * This essentially allows the ORDER/GROUP/etc item to adopt the same
2014 * datatype previously selected for a textually-equivalent tlist item.
2015 * There can't be any implicit cast at top level in an ordinary SELECT
2016 * tlist at this stage, but the case does arise with ORDER BY in an
2017 * aggregate function.
2018 */
2019 texpr = strip_implicit_coercions((Node *) tle->expr);
2020
2021 if (equal(expr, texpr))
2022 return tle;
2023 }
2024
2025 /*
2026 * If no matches, construct a new target entry which is appended to the
2027 * end of the target list. This target is given resjunk = true so that it
2028 * will not be projected into the final tuple.
2029 */
2030 target_result = transformTargetEntry(pstate, node, expr, exprKind,
2031 NULL, true);
2032
2033 *tlist = lappend(*tlist, target_result);
2034
2035 return target_result;
2036}
2037
2038/*-------------------------------------------------------------------------
2039 * Flatten out parenthesized sublists in grouping lists, and some cases
2040 * of nested grouping sets.
2041 *
2042 * Inside a grouping set (ROLLUP, CUBE, or GROUPING SETS), we expect the
2043 * content to be nested no more than 2 deep: i.e. ROLLUP((a,b),(c,d)) is
2044 * ok, but ROLLUP((a,(b,c)),d) is flattened to ((a,b,c),d), which we then
2045 * (later) normalize to ((a,b,c),(d)).
2046 *
2047 * CUBE or ROLLUP can be nested inside GROUPING SETS (but not the reverse),
2048 * and we leave that alone if we find it. But if we see GROUPING SETS inside
2049 * GROUPING SETS, we can flatten and normalize as follows:
2050 * GROUPING SETS (a, (b,c), GROUPING SETS ((c,d),(e)), (f,g))
2051 * becomes
2052 * GROUPING SETS ((a), (b,c), (c,d), (e), (f,g))
2053 *
2054 * This is per the spec's syntax transformations, but these are the only such
2055 * transformations we do in parse analysis, so that queries retain the
2056 * originally specified grouping set syntax for CUBE and ROLLUP as much as
2057 * possible when deparsed. (Full expansion of the result into a list of
2058 * grouping sets is left to the planner.)
2059 *
2060 * When we're done, the resulting list should contain only these possible
2061 * elements:
2062 * - an expression
2063 * - a CUBE or ROLLUP with a list of expressions nested 2 deep
2064 * - a GROUPING SET containing any of:
2065 * - expression lists
2066 * - empty grouping sets
2067 * - CUBE or ROLLUP nodes with lists nested 2 deep
2068 * The return is a new list, but doesn't deep-copy the old nodes except for
2069 * GroupingSet nodes.
2070 *
2071 * As a side effect, flag whether the list has any GroupingSet nodes.
2072 *-------------------------------------------------------------------------
2073 */
2074static Node *
2075flatten_grouping_sets(Node *expr, bool toplevel, bool *hasGroupingSets)
2076{
2077 /* just in case of pathological input */
2078 check_stack_depth();
2079
2080 if (expr == (Node *) NIL)
2081 return (Node *) NIL;
2082
2083 switch (expr->type)
2084 {
2085 case T_RowExpr:
2086 {
2087 RowExpr *r = (RowExpr *) expr;
2088
2089 if (r->row_format == COERCE_IMPLICIT_CAST)
2090 return flatten_grouping_sets((Node *) r->args,
2091 false, NULL);
2092 }
2093 break;
2094 case T_GroupingSet:
2095 {
2096 GroupingSet *gset = (GroupingSet *) expr;
2097 ListCell *l2;
2098 List *result_set = NIL;
2099
2100 if (hasGroupingSets)
2101 *hasGroupingSets = true;
2102
2103 /*
2104 * at the top level, we skip over all empty grouping sets; the
2105 * caller can supply the canonical GROUP BY () if nothing is
2106 * left.
2107 */
2108
2109 if (toplevel && gset->kind == GROUPING_SET_EMPTY)
2110 return (Node *) NIL;
2111
2112 foreach(l2, gset->content)
2113 {
2114 Node *n1 = lfirst(l2);
2115 Node *n2 = flatten_grouping_sets(n1, false, NULL);
2116
2117 if (IsA(n1, GroupingSet) &&
2118 ((GroupingSet *) n1)->kind == GROUPING_SET_SETS)
2119 {
2120 result_set = list_concat(result_set, (List *) n2);
2121 }
2122 else
2123 result_set = lappend(result_set, n2);
2124 }
2125
2126 /*
2127 * At top level, keep the grouping set node; but if we're in a
2128 * nested grouping set, then we need to concat the flattened
2129 * result into the outer list if it's simply nested.
2130 */
2131
2132 if (toplevel || (gset->kind != GROUPING_SET_SETS))
2133 {
2134 return (Node *) makeGroupingSet(gset->kind, result_set, gset->location);
2135 }
2136 else
2137 return (Node *) result_set;
2138 }
2139 case T_List:
2140 {
2141 List *result = NIL;
2142 ListCell *l;
2143
2144 foreach(l, (List *) expr)
2145 {
2146 Node *n = flatten_grouping_sets(lfirst(l), toplevel, hasGroupingSets);
2147
2148 if (n != (Node *) NIL)
2149 {
2150 if (IsA(n, List))
2151 result = list_concat(result, (List *) n);
2152 else
2153 result = lappend(result, n);
2154 }
2155 }
2156
2157 return (Node *) result;
2158 }
2159 default:
2160 break;
2161 }
2162
2163 return expr;
2164}
2165
2166/*
2167 * Transform a single expression within a GROUP BY clause or grouping set.
2168 *
2169 * The expression is added to the targetlist if not already present, and to the
2170 * flatresult list (which will become the groupClause) if not already present
2171 * there. The sortClause is consulted for operator and sort order hints.
2172 *
2173 * Returns the ressortgroupref of the expression.
2174 *
2175 * flatresult reference to flat list of SortGroupClause nodes
2176 * seen_local bitmapset of sortgrouprefs already seen at the local level
2177 * pstate ParseState
2178 * gexpr node to transform
2179 * targetlist reference to TargetEntry list
2180 * sortClause ORDER BY clause (SortGroupClause nodes)
2181 * exprKind expression kind
2182 * useSQL99 SQL99 rather than SQL92 syntax
2183 * toplevel false if within any grouping set
2184 */
2185static Index
2186transformGroupClauseExpr(List **flatresult, Bitmapset *seen_local,
2187 ParseState *pstate, Node *gexpr,
2188 List **targetlist, List *sortClause,
2189 ParseExprKind exprKind, bool useSQL99, bool toplevel)
2190{
2191 TargetEntry *tle;
2192 bool found = false;
2193
2194 if (useSQL99)
2195 tle = findTargetlistEntrySQL99(pstate, gexpr,
2196 targetlist, exprKind);
2197 else
2198 tle = findTargetlistEntrySQL92(pstate, gexpr,
2199 targetlist, exprKind);
2200
2201 if (tle->ressortgroupref > 0)
2202 {
2203 ListCell *sl;
2204
2205 /*
2206 * Eliminate duplicates (GROUP BY x, x) but only at local level.
2207 * (Duplicates in grouping sets can affect the number of returned
2208 * rows, so can't be dropped indiscriminately.)
2209 *
2210 * Since we don't care about anything except the sortgroupref, we can
2211 * use a bitmapset rather than scanning lists.
2212 */
2213 if (bms_is_member(tle->ressortgroupref, seen_local))
2214 return 0;
2215
2216 /*
2217 * If we're already in the flat clause list, we don't need to consider
2218 * adding ourselves again.
2219 */
2220 found = targetIsInSortList(tle, InvalidOid, *flatresult);
2221 if (found)
2222 return tle->ressortgroupref;
2223
2224 /*
2225 * If the GROUP BY tlist entry also appears in ORDER BY, copy operator
2226 * info from the (first) matching ORDER BY item. This means that if
2227 * you write something like "GROUP BY foo ORDER BY foo USING <<<", the
2228 * GROUP BY operation silently takes on the equality semantics implied
2229 * by the ORDER BY. There are two reasons to do this: it improves the
2230 * odds that we can implement both GROUP BY and ORDER BY with a single
2231 * sort step, and it allows the user to choose the equality semantics
2232 * used by GROUP BY, should she be working with a datatype that has
2233 * more than one equality operator.
2234 *
2235 * If we're in a grouping set, though, we force our requested ordering
2236 * to be NULLS LAST, because if we have any hope of using a sorted agg
2237 * for the job, we're going to be tacking on generated NULL values
2238 * after the corresponding groups. If the user demands nulls first,
2239 * another sort step is going to be inevitable, but that's the
2240 * planner's problem.
2241 */
2242
2243 foreach(sl, sortClause)
2244 {
2245 SortGroupClause *sc = (SortGroupClause *) lfirst(sl);
2246
2247 if (sc->tleSortGroupRef == tle->ressortgroupref)
2248 {
2249 SortGroupClause *grpc = copyObject(sc);
2250
2251 if (!toplevel)
2252 grpc->nulls_first = false;
2253 *flatresult = lappend(*flatresult, grpc);
2254 found = true;
2255 break;
2256 }
2257 }
2258 }
2259
2260 /*
2261 * If no match in ORDER BY, just add it to the result using default
2262 * sort/group semantics.
2263 */
2264 if (!found)
2265 *flatresult = addTargetToGroupList(pstate, tle,
2266 *flatresult, *targetlist,
2267 exprLocation(gexpr));
2268
2269 /*
2270 * _something_ must have assigned us a sortgroupref by now...
2271 */
2272
2273 return tle->ressortgroupref;
2274}
2275
2276/*
2277 * Transform a list of expressions within a GROUP BY clause or grouping set.
2278 *
2279 * The list of expressions belongs to a single clause within which duplicates
2280 * can be safely eliminated.
2281 *
2282 * Returns an integer list of ressortgroupref values.
2283 *
2284 * flatresult reference to flat list of SortGroupClause nodes
2285 * pstate ParseState
2286 * list nodes to transform
2287 * targetlist reference to TargetEntry list
2288 * sortClause ORDER BY clause (SortGroupClause nodes)
2289 * exprKind expression kind
2290 * useSQL99 SQL99 rather than SQL92 syntax
2291 * toplevel false if within any grouping set
2292 */
2293static List *
2294transformGroupClauseList(List **flatresult,
2295 ParseState *pstate, List *list,
2296 List **targetlist, List *sortClause,
2297 ParseExprKind exprKind, bool useSQL99, bool toplevel)
2298{
2299 Bitmapset *seen_local = NULL;
2300 List *result = NIL;
2301 ListCell *gl;
2302
2303 foreach(gl, list)
2304 {
2305 Node *gexpr = (Node *) lfirst(gl);
2306
2307 Index ref = transformGroupClauseExpr(flatresult,
2308 seen_local,
2309 pstate,
2310 gexpr,
2311 targetlist,
2312 sortClause,
2313 exprKind,
2314 useSQL99,
2315 toplevel);
2316
2317 if (ref > 0)
2318 {
2319 seen_local = bms_add_member(seen_local, ref);
2320 result = lappend_int(result, ref);
2321 }
2322 }
2323
2324 return result;
2325}
2326
2327/*
2328 * Transform a grouping set and (recursively) its content.
2329 *
2330 * The grouping set might be a GROUPING SETS node with other grouping sets
2331 * inside it, but SETS within SETS have already been flattened out before
2332 * reaching here.
2333 *
2334 * Returns the transformed node, which now contains SIMPLE nodes with lists
2335 * of ressortgrouprefs rather than expressions.
2336 *
2337 * flatresult reference to flat list of SortGroupClause nodes
2338 * pstate ParseState
2339 * gset grouping set to transform
2340 * targetlist reference to TargetEntry list
2341 * sortClause ORDER BY clause (SortGroupClause nodes)
2342 * exprKind expression kind
2343 * useSQL99 SQL99 rather than SQL92 syntax
2344 * toplevel false if within any grouping set
2345 */
2346static Node *
2347transformGroupingSet(List **flatresult,
2348 ParseState *pstate, GroupingSet *gset,
2349 List **targetlist, List *sortClause,
2350 ParseExprKind exprKind, bool useSQL99, bool toplevel)
2351{
2352 ListCell *gl;
2353 List *content = NIL;
2354
2355 Assert(toplevel || gset->kind != GROUPING_SET_SETS);
2356
2357 foreach(gl, gset->content)
2358 {
2359 Node *n = lfirst(gl);
2360
2361 if (IsA(n, List))
2362 {
2363 List *l = transformGroupClauseList(flatresult,
2364 pstate, (List *) n,
2365 targetlist, sortClause,
2366 exprKind, useSQL99, false);
2367
2368 content = lappend(content, makeGroupingSet(GROUPING_SET_SIMPLE,
2369 l,
2370 exprLocation(n)));
2371 }
2372 else if (IsA(n, GroupingSet))
2373 {
2374 GroupingSet *gset2 = (GroupingSet *) lfirst(gl);
2375
2376 content = lappend(content, transformGroupingSet(flatresult,
2377 pstate, gset2,
2378 targetlist, sortClause,
2379 exprKind, useSQL99, false));
2380 }
2381 else
2382 {
2383 Index ref = transformGroupClauseExpr(flatresult,
2384 NULL,
2385 pstate,
2386 n,
2387 targetlist,
2388 sortClause,
2389 exprKind,
2390 useSQL99,
2391 false);
2392
2393 content = lappend(content, makeGroupingSet(GROUPING_SET_SIMPLE,
2394 list_make1_int(ref),
2395 exprLocation(n)));
2396 }
2397 }
2398
2399 /* Arbitrarily cap the size of CUBE, which has exponential growth */
2400 if (gset->kind == GROUPING_SET_CUBE)
2401 {
2402 if (list_length(content) > 12)
2403 ereport(ERROR,
2404 (errcode(ERRCODE_TOO_MANY_COLUMNS),
2405 errmsg("CUBE is limited to 12 elements"),
2406 parser_errposition(pstate, gset->location)));
2407 }
2408
2409 return (Node *) makeGroupingSet(gset->kind, content, gset->location);
2410}
2411
2412
2413/*
2414 * transformGroupClause -
2415 * transform a GROUP BY clause
2416 *
2417 * GROUP BY items will be added to the targetlist (as resjunk columns)
2418 * if not already present, so the targetlist must be passed by reference.
2419 *
2420 * This is also used for window PARTITION BY clauses (which act almost the
2421 * same, but are always interpreted per SQL99 rules).
2422 *
2423 * Grouping sets make this a lot more complex than it was. Our goal here is
2424 * twofold: we make a flat list of SortGroupClause nodes referencing each
2425 * distinct expression used for grouping, with those expressions added to the
2426 * targetlist if needed. At the same time, we build the groupingSets tree,
2427 * which stores only ressortgrouprefs as integer lists inside GroupingSet nodes
2428 * (possibly nested, but limited in depth: a GROUPING_SET_SETS node can contain
2429 * nested SIMPLE, CUBE or ROLLUP nodes, but not more sets - we flatten that
2430 * out; while CUBE and ROLLUP can contain only SIMPLE nodes).
2431 *
2432 * We skip much of the hard work if there are no grouping sets.
2433 *
2434 * One subtlety is that the groupClause list can end up empty while the
2435 * groupingSets list is not; this happens if there are only empty grouping
2436 * sets, or an explicit GROUP BY (). This has the same effect as specifying
2437 * aggregates or a HAVING clause with no GROUP BY; the output is one row per
2438 * grouping set even if the input is empty.
2439 *
2440 * Returns the transformed (flat) groupClause.
2441 *
2442 * pstate ParseState
2443 * grouplist clause to transform
2444 * groupingSets reference to list to contain the grouping set tree
2445 * targetlist reference to TargetEntry list
2446 * sortClause ORDER BY clause (SortGroupClause nodes)
2447 * exprKind expression kind
2448 * useSQL99 SQL99 rather than SQL92 syntax
2449 */
2450List *
2451transformGroupClause(ParseState *pstate, List *grouplist, List **groupingSets,
2452 List **targetlist, List *sortClause,
2453 ParseExprKind exprKind, bool useSQL99)
2454{
2455 List *result = NIL;
2456 List *flat_grouplist;
2457 List *gsets = NIL;
2458 ListCell *gl;
2459 bool hasGroupingSets = false;
2460 Bitmapset *seen_local = NULL;
2461
2462 /*
2463 * Recursively flatten implicit RowExprs. (Technically this is only needed
2464 * for GROUP BY, per the syntax rules for grouping sets, but we do it
2465 * anyway.)
2466 */
2467 flat_grouplist = (List *) flatten_grouping_sets((Node *) grouplist,
2468 true,
2469 &hasGroupingSets);
2470
2471 /*
2472 * If the list is now empty, but hasGroupingSets is true, it's because we
2473 * elided redundant empty grouping sets. Restore a single empty grouping
2474 * set to leave a canonical form: GROUP BY ()
2475 */
2476
2477 if (flat_grouplist == NIL && hasGroupingSets)
2478 {
2479 flat_grouplist = list_make1(makeGroupingSet(GROUPING_SET_EMPTY,
2480 NIL,
2481 exprLocation((Node *) grouplist)));
2482 }
2483
2484 foreach(gl, flat_grouplist)
2485 {
2486 Node *gexpr = (Node *) lfirst(gl);
2487
2488 if (IsA(gexpr, GroupingSet))
2489 {
2490 GroupingSet *gset = (GroupingSet *) gexpr;
2491
2492 switch (gset->kind)
2493 {
2494 case GROUPING_SET_EMPTY:
2495 gsets = lappend(gsets, gset);
2496 break;
2497 case GROUPING_SET_SIMPLE:
2498 /* can't happen */
2499 Assert(false);
2500 break;
2501 case GROUPING_SET_SETS:
2502 case GROUPING_SET_CUBE:
2503 case GROUPING_SET_ROLLUP:
2504 gsets = lappend(gsets,
2505 transformGroupingSet(&result,
2506 pstate, gset,
2507 targetlist, sortClause,
2508 exprKind, useSQL99, true));
2509 break;
2510 }
2511 }
2512 else
2513 {
2514 Index ref = transformGroupClauseExpr(&result, seen_local,
2515 pstate, gexpr,
2516 targetlist, sortClause,
2517 exprKind, useSQL99, true);
2518
2519 if (ref > 0)
2520 {
2521 seen_local = bms_add_member(seen_local, ref);
2522 if (hasGroupingSets)
2523 gsets = lappend(gsets,
2524 makeGroupingSet(GROUPING_SET_SIMPLE,
2525 list_make1_int(ref),
2526 exprLocation(gexpr)));
2527 }
2528 }
2529 }
2530
2531 /* parser should prevent this */
2532 Assert(gsets == NIL || groupingSets != NULL);
2533
2534 if (groupingSets)
2535 *groupingSets = gsets;
2536
2537 return result;
2538}
2539
2540/*
2541 * transformSortClause -
2542 * transform an ORDER BY clause
2543 *
2544 * ORDER BY items will be added to the targetlist (as resjunk columns)
2545 * if not already present, so the targetlist must be passed by reference.
2546 *
2547 * This is also used for window and aggregate ORDER BY clauses (which act
2548 * almost the same, but are always interpreted per SQL99 rules).
2549 */
2550List *
2551transformSortClause(ParseState *pstate,
2552 List *orderlist,
2553 List **targetlist,
2554 ParseExprKind exprKind,
2555 bool useSQL99)
2556{
2557 List *sortlist = NIL;
2558 ListCell *olitem;
2559
2560 foreach(olitem, orderlist)
2561 {
2562 SortBy *sortby = (SortBy *) lfirst(olitem);
2563 TargetEntry *tle;
2564
2565 if (useSQL99)
2566 tle = findTargetlistEntrySQL99(pstate, sortby->node,
2567 targetlist, exprKind);
2568 else
2569 tle = findTargetlistEntrySQL92(pstate, sortby->node,
2570 targetlist, exprKind);
2571
2572 sortlist = addTargetToSortList(pstate, tle,
2573 sortlist, *targetlist, sortby);
2574 }
2575
2576 return sortlist;
2577}
2578
2579/*
2580 * transformWindowDefinitions -
2581 * transform window definitions (WindowDef to WindowClause)
2582 */
2583List *
2584transformWindowDefinitions(ParseState *pstate,
2585 List *windowdefs,
2586 List **targetlist)
2587{
2588 List *result = NIL;
2589 Index winref = 0;
2590 ListCell *lc;
2591
2592 foreach(lc, windowdefs)
2593 {
2594 WindowDef *windef = (WindowDef *) lfirst(lc);
2595 WindowClause *refwc = NULL;
2596 List *partitionClause;
2597 List *orderClause;
2598 Oid rangeopfamily = InvalidOid;
2599 Oid rangeopcintype = InvalidOid;
2600 WindowClause *wc;
2601
2602 winref++;
2603
2604 /*
2605 * Check for duplicate window names.
2606 */
2607 if (windef->name &&
2608 findWindowClause(result, windef->name) != NULL)
2609 ereport(ERROR,
2610 (errcode(ERRCODE_WINDOWING_ERROR),
2611 errmsg("window \"%s\" is already defined", windef->name),
2612 parser_errposition(pstate, windef->location)));
2613
2614 /*
2615 * If it references a previous window, look that up.
2616 */
2617 if (windef->refname)
2618 {
2619 refwc = findWindowClause(result, windef->refname);
2620 if (refwc == NULL)
2621 ereport(ERROR,
2622 (errcode(ERRCODE_UNDEFINED_OBJECT),
2623 errmsg("window \"%s\" does not exist",
2624 windef->refname),
2625 parser_errposition(pstate, windef->location)));
2626 }
2627
2628 /*
2629 * Transform PARTITION and ORDER specs, if any. These are treated
2630 * almost exactly like top-level GROUP BY and ORDER BY clauses,
2631 * including the special handling of nondefault operator semantics.
2632 */
2633 orderClause = transformSortClause(pstate,
2634 windef->orderClause,
2635 targetlist,
2636 EXPR_KIND_WINDOW_ORDER,
2637 true /* force SQL99 rules */ );
2638 partitionClause = transformGroupClause(pstate,
2639 windef->partitionClause,
2640 NULL,
2641 targetlist,
2642 orderClause,
2643 EXPR_KIND_WINDOW_PARTITION,
2644 true /* force SQL99 rules */ );
2645
2646 /*
2647 * And prepare the new WindowClause.
2648 */
2649 wc = makeNode(WindowClause);
2650 wc->name = windef->name;
2651 wc->refname = windef->refname;
2652
2653 /*
2654 * Per spec, a windowdef that references a previous one copies the
2655 * previous partition clause (and mustn't specify its own). It can
2656 * specify its own ordering clause, but only if the previous one had
2657 * none. It always specifies its own frame clause, and the previous
2658 * one must not have a frame clause. Yeah, it's bizarre that each of
2659 * these cases works differently, but SQL:2008 says so; see 7.11
2660 * <window clause> syntax rule 10 and general rule 1. The frame
2661 * clause rule is especially bizarre because it makes "OVER foo"
2662 * different from "OVER (foo)", and requires the latter to throw an
2663 * error if foo has a nondefault frame clause. Well, ours not to
2664 * reason why, but we do go out of our way to throw a useful error
2665 * message for such cases.
2666 */
2667 if (refwc)
2668 {
2669 if (partitionClause)
2670 ereport(ERROR,
2671 (errcode(ERRCODE_WINDOWING_ERROR),
2672 errmsg("cannot override PARTITION BY clause of window \"%s\"",
2673 windef->refname),
2674 parser_errposition(pstate, windef->location)));
2675 wc->partitionClause = copyObject(refwc->partitionClause);
2676 }
2677 else
2678 wc->partitionClause = partitionClause;
2679 if (refwc)
2680 {
2681 if (orderClause && refwc->orderClause)
2682 ereport(ERROR,
2683 (errcode(ERRCODE_WINDOWING_ERROR),
2684 errmsg("cannot override ORDER BY clause of window \"%s\"",
2685 windef->refname),
2686 parser_errposition(pstate, windef->location)));
2687 if (orderClause)
2688 {
2689 wc->orderClause = orderClause;
2690 wc->copiedOrder = false;
2691 }
2692 else
2693 {
2694 wc->orderClause = copyObject(refwc->orderClause);
2695 wc->copiedOrder = true;
2696 }
2697 }
2698 else
2699 {
2700 wc->orderClause = orderClause;
2701 wc->copiedOrder = false;
2702 }
2703 if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
2704 {
2705 /*
2706 * Use this message if this is a WINDOW clause, or if it's an OVER
2707 * clause that includes ORDER BY or framing clauses. (We already
2708 * rejected PARTITION BY above, so no need to check that.)
2709 */
2710 if (windef->name ||
2711 orderClause || windef->frameOptions != FRAMEOPTION_DEFAULTS)
2712 ereport(ERROR,
2713 (errcode(ERRCODE_WINDOWING_ERROR),
2714 errmsg("cannot copy window \"%s\" because it has a frame clause",
2715 windef->refname),
2716 parser_errposition(pstate, windef->location)));
2717 /* Else this clause is just OVER (foo), so say this: */
2718 ereport(ERROR,
2719 (errcode(ERRCODE_WINDOWING_ERROR),
2720 errmsg("cannot copy window \"%s\" because it has a frame clause",
2721 windef->refname),
2722 errhint("Omit the parentheses in this OVER clause."),
2723 parser_errposition(pstate, windef->location)));
2724 }
2725 wc->frameOptions = windef->frameOptions;
2726
2727 /*
2728 * RANGE offset PRECEDING/FOLLOWING requires exactly one ORDER BY
2729 * column; check that and get its sort opfamily info.
2730 */
2731 if ((wc->frameOptions & FRAMEOPTION_RANGE) &&
2732 (wc->frameOptions & (FRAMEOPTION_START_OFFSET |
2733 FRAMEOPTION_END_OFFSET)))
2734 {
2735 SortGroupClause *sortcl;
2736 Node *sortkey;
2737 int16 rangestrategy;
2738
2739 if (list_length(wc->orderClause) != 1)
2740 ereport(ERROR,
2741 (errcode(ERRCODE_WINDOWING_ERROR),
2742 errmsg("RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column"),
2743 parser_errposition(pstate, windef->location)));
2744 sortcl = castNode(SortGroupClause, linitial(wc->orderClause));
2745 sortkey = get_sortgroupclause_expr(sortcl, *targetlist);
2746 /* Find the sort operator in pg_amop */
2747 if (!get_ordering_op_properties(sortcl->sortop,
2748 &rangeopfamily,
2749 &rangeopcintype,
2750 &rangestrategy))
2751 elog(ERROR, "operator %u is not a valid ordering operator",
2752 sortcl->sortop);
2753 /* Record properties of sort ordering */
2754 wc->inRangeColl = exprCollation(sortkey);
2755 wc->inRangeAsc = (rangestrategy == BTLessStrategyNumber);
2756 wc->inRangeNullsFirst = sortcl->nulls_first;
2757 }
2758
2759 /* Per spec, GROUPS mode requires an ORDER BY clause */
2760 if (wc->frameOptions & FRAMEOPTION_GROUPS)
2761 {
2762 if (wc->orderClause == NIL)
2763 ereport(ERROR,
2764 (errcode(ERRCODE_WINDOWING_ERROR),
2765 errmsg("GROUPS mode requires an ORDER BY clause"),
2766 parser_errposition(pstate, windef->location)));
2767 }
2768
2769 /* Process frame offset expressions */
2770 wc->startOffset = transformFrameOffset(pstate, wc->frameOptions,
2771 rangeopfamily, rangeopcintype,
2772 &wc->startInRangeFunc,
2773 windef->startOffset);
2774 wc->endOffset = transformFrameOffset(pstate, wc->frameOptions,
2775 rangeopfamily, rangeopcintype,
2776 &wc->endInRangeFunc,
2777 windef->endOffset);
2778 wc->winref = winref;
2779
2780 result = lappend(result, wc);
2781 }
2782
2783 return result;
2784}
2785
2786/*
2787 * transformDistinctClause -
2788 * transform a DISTINCT clause
2789 *
2790 * Since we may need to add items to the query's targetlist, that list
2791 * is passed by reference.
2792 *
2793 * As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
2794 * possible into the distinctClause. This avoids a possible need to re-sort,
2795 * and allows the user to choose the equality semantics used by DISTINCT,
2796 * should she be working with a datatype that has more than one equality
2797 * operator.
2798 *
2799 * is_agg is true if we are transforming an aggregate(DISTINCT ...)
2800 * function call. This does not affect any behavior, only the phrasing
2801 * of error messages.
2802 */
2803List *
2804transformDistinctClause(ParseState *pstate,
2805 List **targetlist, List *sortClause, bool is_agg)
2806{
2807 List *result = NIL;
2808 ListCell *slitem;
2809 ListCell *tlitem;
2810
2811 /*
2812 * The distinctClause should consist of all ORDER BY items followed by all
2813 * other non-resjunk targetlist items. There must not be any resjunk
2814 * ORDER BY items --- that would imply that we are sorting by a value that
2815 * isn't necessarily unique within a DISTINCT group, so the results
2816 * wouldn't be well-defined. This construction ensures we follow the rule
2817 * that sortClause and distinctClause match; in fact the sortClause will
2818 * always be a prefix of distinctClause.
2819 *
2820 * Note a corner case: the same TLE could be in the ORDER BY list multiple
2821 * times with different sortops. We have to include it in the
2822 * distinctClause the same way to preserve the prefix property. The net
2823 * effect will be that the TLE value will be made unique according to both
2824 * sortops.
2825 */
2826 foreach(slitem, sortClause)
2827 {
2828 SortGroupClause *scl = (SortGroupClause *) lfirst(slitem);
2829 TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
2830
2831 if (tle->resjunk)
2832 ereport(ERROR,
2833 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2834 is_agg ?
2835 errmsg("in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list") :
2836 errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list"),
2837 parser_errposition(pstate,
2838 exprLocation((Node *) tle->expr))));
2839 result = lappend(result, copyObject(scl));
2840 }
2841
2842 /*
2843 * Now add any remaining non-resjunk tlist items, using default sort/group
2844 * semantics for their data types.
2845 */
2846 foreach(tlitem, *targetlist)
2847 {
2848 TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
2849
2850 if (tle->resjunk)
2851 continue; /* ignore junk */
2852 result = addTargetToGroupList(pstate, tle,
2853 result, *targetlist,
2854 exprLocation((Node *) tle->expr));
2855 }
2856
2857 /*
2858 * Complain if we found nothing to make DISTINCT. Returning an empty list
2859 * would cause the parsed Query to look like it didn't have DISTINCT, with
2860 * results that would probably surprise the user. Note: this case is
2861 * presently impossible for aggregates because of grammar restrictions,
2862 * but we check anyway.
2863 */
2864 if (result == NIL)
2865 ereport(ERROR,
2866 (errcode(ERRCODE_SYNTAX_ERROR),
2867 is_agg ?
2868 errmsg("an aggregate with DISTINCT must have at least one argument") :
2869 errmsg("SELECT DISTINCT must have at least one column")));
2870
2871 return result;
2872}
2873
2874/*
2875 * transformDistinctOnClause -
2876 * transform a DISTINCT ON clause
2877 *
2878 * Since we may need to add items to the query's targetlist, that list
2879 * is passed by reference.
2880 *
2881 * As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
2882 * possible into the distinctClause. This avoids a possible need to re-sort,
2883 * and allows the user to choose the equality semantics used by DISTINCT,
2884 * should she be working with a datatype that has more than one equality
2885 * operator.
2886 */
2887List *
2888transformDistinctOnClause(ParseState *pstate, List *distinctlist,
2889 List **targetlist, List *sortClause)
2890{
2891 List *result = NIL;
2892 List *sortgrouprefs = NIL;
2893 bool skipped_sortitem;
2894 ListCell *lc;
2895 ListCell *lc2;
2896
2897 /*
2898 * Add all the DISTINCT ON expressions to the tlist (if not already
2899 * present, they are added as resjunk items). Assign sortgroupref numbers
2900 * to them, and make a list of these numbers. (NB: we rely below on the
2901 * sortgrouprefs list being one-for-one with the original distinctlist.
2902 * Also notice that we could have duplicate DISTINCT ON expressions and
2903 * hence duplicate entries in sortgrouprefs.)
2904 */
2905 foreach(lc, distinctlist)
2906 {
2907 Node *dexpr = (Node *) lfirst(lc);
2908 int sortgroupref;
2909 TargetEntry *tle;
2910
2911 tle = findTargetlistEntrySQL92(pstate, dexpr, targetlist,
2912 EXPR_KIND_DISTINCT_ON);
2913 sortgroupref = assignSortGroupRef(tle, *targetlist);
2914 sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
2915 }
2916
2917 /*
2918 * If the user writes both DISTINCT ON and ORDER BY, adopt the sorting
2919 * semantics from ORDER BY items that match DISTINCT ON items, and also
2920 * adopt their column sort order. We insist that the distinctClause and
2921 * sortClause match, so throw error if we find the need to add any more
2922 * distinctClause items after we've skipped an ORDER BY item that wasn't
2923 * in DISTINCT ON.
2924 */
2925 skipped_sortitem = false;
2926 foreach(lc, sortClause)
2927 {
2928 SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
2929
2930 if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
2931 {
2932 if (skipped_sortitem)
2933 ereport(ERROR,
2934 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2935 errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2936 parser_errposition(pstate,
2937 get_matching_location(scl->tleSortGroupRef,
2938 sortgrouprefs,
2939 distinctlist))));
2940 else
2941 result = lappend(result, copyObject(scl));
2942 }
2943 else
2944 skipped_sortitem = true;
2945 }
2946
2947 /*
2948 * Now add any remaining DISTINCT ON items, using default sort/group
2949 * semantics for their data types. (Note: this is pretty questionable; if
2950 * the ORDER BY list doesn't include all the DISTINCT ON items and more
2951 * besides, you certainly aren't using DISTINCT ON in the intended way,
2952 * and you probably aren't going to get consistent results. It might be
2953 * better to throw an error or warning here. But historically we've
2954 * allowed it, so keep doing so.)
2955 */
2956 forboth(lc, distinctlist, lc2, sortgrouprefs)
2957 {
2958 Node *dexpr = (Node *) lfirst(lc);
2959 int sortgroupref = lfirst_int(lc2);
2960 TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
2961
2962 if (targetIsInSortList(tle, InvalidOid, result))
2963 continue; /* already in list (with some semantics) */
2964 if (skipped_sortitem)
2965 ereport(ERROR,
2966 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2967 errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
2968 parser_errposition(pstate, exprLocation(dexpr))));
2969 result = addTargetToGroupList(pstate, tle,
2970 result, *targetlist,
2971 exprLocation(dexpr));
2972 }
2973
2974 /*
2975 * An empty result list is impossible here because of grammar
2976 * restrictions.
2977 */
2978 Assert(result != NIL);
2979
2980 return result;
2981}
2982
2983/*
2984 * get_matching_location
2985 * Get the exprLocation of the exprs member corresponding to the
2986 * (first) member of sortgrouprefs that equals sortgroupref.
2987 *
2988 * This is used so that we can point at a troublesome DISTINCT ON entry.
2989 * (Note that we need to use the original untransformed DISTINCT ON list
2990 * item, as whatever TLE it corresponds to will very possibly have a
2991 * parse location pointing to some matching entry in the SELECT list
2992 * or ORDER BY list.)
2993 */
2994static int
2995get_matching_location(int sortgroupref, List *sortgrouprefs, List *exprs)
2996{
2997 ListCell *lcs;
2998 ListCell *lce;
2999
3000 forboth(lcs, sortgrouprefs, lce, exprs)
3001 {
3002 if (lfirst_int(lcs) == sortgroupref)
3003 return exprLocation((Node *) lfirst(lce));
3004 }
3005 /* if no match, caller blew it */
3006 elog(ERROR, "get_matching_location: no matching sortgroupref");
3007 return -1; /* keep compiler quiet */
3008}
3009
3010/*
3011 * resolve_unique_index_expr
3012 * Infer a unique index from a list of indexElems, for ON
3013 * CONFLICT clause
3014 *
3015 * Perform parse analysis of expressions and columns appearing within ON
3016 * CONFLICT clause. During planning, the returned list of expressions is used
3017 * to infer which unique index to use.
3018 */
3019static List *
3020resolve_unique_index_expr(ParseState *pstate, InferClause *infer,
3021 Relation heapRel)
3022{
3023 List *result = NIL;
3024 ListCell *l;
3025
3026 foreach(l, infer->indexElems)
3027 {
3028 IndexElem *ielem = (IndexElem *) lfirst(l);
3029 InferenceElem *pInfer = makeNode(InferenceElem);
3030 Node *parse;
3031
3032 /*
3033 * Raw grammar re-uses CREATE INDEX infrastructure for unique index
3034 * inference clause, and so will accept opclasses by name and so on.
3035 *
3036 * Make no attempt to match ASC or DESC ordering or NULLS FIRST/NULLS
3037 * LAST ordering, since those are not significant for inference
3038 * purposes (any unique index matching the inference specification in
3039 * other regards is accepted indifferently). Actively reject this as
3040 * wrong-headed.
3041 */
3042 if (ielem->ordering != SORTBY_DEFAULT)
3043 ereport(ERROR,
3044 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
3045 errmsg("ASC/DESC is not allowed in ON CONFLICT clause"),
3046 parser_errposition(pstate,
3047 exprLocation((Node *) infer))));
3048 if (ielem->nulls_ordering != SORTBY_NULLS_DEFAULT)
3049 ereport(ERROR,
3050 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
3051 errmsg("NULLS FIRST/LAST is not allowed in ON CONFLICT clause"),
3052 parser_errposition(pstate,
3053 exprLocation((Node *) infer))));
3054
3055 if (!ielem->expr)
3056 {
3057 /* Simple index attribute */
3058 ColumnRef *n;
3059
3060 /*
3061 * Grammar won't have built raw expression for us in event of
3062 * plain column reference. Create one directly, and perform
3063 * expression transformation. Planner expects this, and performs
3064 * its own normalization for the purposes of matching against
3065 * pg_index.
3066 */
3067 n = makeNode(ColumnRef);
3068 n->fields = list_make1(makeString(ielem->name));
3069 /* Location is approximately that of inference specification */
3070 n->location = infer->location;
3071 parse = (Node *) n;
3072 }
3073 else
3074 {
3075 /* Do parse transformation of the raw expression */
3076 parse = (Node *) ielem->expr;
3077 }
3078
3079 /*
3080 * transformExpr() will reject subqueries, aggregates, window
3081 * functions, and SRFs, based on being passed
3082 * EXPR_KIND_INDEX_EXPRESSION. So we needn't worry about those
3083 * further ... not that they would match any available index
3084 * expression anyway.
3085 */
3086 pInfer->expr = transformExpr(pstate, parse, EXPR_KIND_INDEX_EXPRESSION);
3087
3088 /* Perform lookup of collation and operator class as required */
3089 if (!ielem->collation)
3090 pInfer->infercollid = InvalidOid;
3091 else
3092 pInfer->infercollid = LookupCollation(pstate, ielem->collation,
3093 exprLocation(pInfer->expr));
3094
3095 if (!ielem->opclass)
3096 pInfer->inferopclass = InvalidOid;
3097 else
3098 pInfer->inferopclass = get_opclass_oid(BTREE_AM_OID,
3099 ielem->opclass, false);
3100
3101 result = lappend(result, pInfer);
3102 }
3103
3104 return result;
3105}
3106
3107/*
3108 * transformOnConflictArbiter -
3109 * transform arbiter expressions in an ON CONFLICT clause.
3110 *
3111 * Transformed expressions used to infer one unique index relation to serve as
3112 * an ON CONFLICT arbiter. Partial unique indexes may be inferred using WHERE
3113 * clause from inference specification clause.
3114 */
3115void
3116transformOnConflictArbiter(ParseState *pstate,
3117 OnConflictClause *onConflictClause,
3118 List **arbiterExpr, Node **arbiterWhere,
3119 Oid *constraint)
3120{
3121 InferClause *infer = onConflictClause->infer;
3122
3123 *arbiterExpr = NIL;
3124 *arbiterWhere = NULL;
3125 *constraint = InvalidOid;
3126
3127 if (onConflictClause->action == ONCONFLICT_UPDATE && !infer)
3128 ereport(ERROR,
3129 (errcode(ERRCODE_SYNTAX_ERROR),
3130 errmsg("ON CONFLICT DO UPDATE requires inference specification or constraint name"),
3131 errhint("For example, ON CONFLICT (column_name)."),
3132 parser_errposition(pstate,
3133 exprLocation((Node *) onConflictClause))));
3134
3135 /*
3136 * To simplify certain aspects of its design, speculative insertion into
3137 * system catalogs is disallowed
3138 */
3139 if (IsCatalogRelation(pstate->p_target_relation))
3140 ereport(ERROR,
3141 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3142 errmsg("ON CONFLICT is not supported with system catalog tables"),
3143 parser_errposition(pstate,
3144 exprLocation((Node *) onConflictClause))));
3145
3146 /* Same applies to table used by logical decoding as catalog table */
3147 if (RelationIsUsedAsCatalogTable(pstate->p_target_relation))
3148 ereport(ERROR,
3149 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3150 errmsg("ON CONFLICT is not supported on table \"%s\" used as a catalog table",
3151 RelationGetRelationName(pstate->p_target_relation)),
3152 parser_errposition(pstate,
3153 exprLocation((Node *) onConflictClause))));
3154
3155 /* ON CONFLICT DO NOTHING does not require an inference clause */
3156 if (infer)
3157 {
3158 List *save_namespace;
3159
3160 /*
3161 * While we process the arbiter expressions, accept only non-qualified
3162 * references to the target table. Hide any other relations.
3163 */
3164 save_namespace = pstate->p_namespace;
3165 pstate->p_namespace = NIL;
3166 addRTEtoQuery(pstate, pstate->p_target_rangetblentry,
3167 false, false, true);
3168
3169 if (infer->indexElems)
3170 *arbiterExpr = resolve_unique_index_expr(pstate, infer,
3171 pstate->p_target_relation);
3172
3173 /*
3174 * Handling inference WHERE clause (for partial unique index
3175 * inference)
3176 */
3177 if (infer->whereClause)
3178 *arbiterWhere = transformExpr(pstate, infer->whereClause,
3179 EXPR_KIND_INDEX_PREDICATE);
3180
3181 pstate->p_namespace = save_namespace;
3182
3183 /*
3184 * If the arbiter is specified by constraint name, get the constraint
3185 * OID and mark the constrained columns as requiring SELECT privilege,
3186 * in the same way as would have happened if the arbiter had been
3187 * specified by explicit reference to the constraint's index columns.
3188 */
3189 if (infer->conname)
3190 {
3191 Oid relid = RelationGetRelid(pstate->p_target_relation);
3192 RangeTblEntry *rte = pstate->p_target_rangetblentry;
3193 Bitmapset *conattnos;
3194
3195 conattnos = get_relation_constraint_attnos(relid, infer->conname,
3196 false, constraint);
3197
3198 /* Make sure the rel as a whole is marked for SELECT access */
3199 rte->requiredPerms |= ACL_SELECT;
3200 /* Mark the constrained columns as requiring SELECT access */
3201 rte->selectedCols = bms_add_members(rte->selectedCols, conattnos);
3202 }
3203 }
3204
3205 /*
3206 * It's convenient to form a list of expressions based on the
3207 * representation used by CREATE INDEX, since the same restrictions are
3208 * appropriate (e.g. on subqueries). However, from here on, a dedicated
3209 * primnode representation is used for inference elements, and so
3210 * assign_query_collations() can be trusted to do the right thing with the
3211 * post parse analysis query tree inference clause representation.
3212 */
3213}
3214
3215/*
3216 * addTargetToSortList
3217 * If the given targetlist entry isn't already in the SortGroupClause
3218 * list, add it to the end of the list, using the given sort ordering
3219 * info.
3220 *
3221 * Returns the updated SortGroupClause list.
3222 */
3223List *
3224addTargetToSortList(ParseState *pstate, TargetEntry *tle,
3225 List *sortlist, List *targetlist, SortBy *sortby)
3226{
3227 Oid restype = exprType((Node *) tle->expr);
3228 Oid sortop;
3229 Oid eqop;
3230 bool hashable;
3231 bool reverse;
3232 int location;
3233 ParseCallbackState pcbstate;
3234
3235 /* if tlist item is an UNKNOWN literal, change it to TEXT */
3236 if (restype == UNKNOWNOID)
3237 {
3238 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
3239 restype, TEXTOID, -1,
3240 COERCION_IMPLICIT,
3241 COERCE_IMPLICIT_CAST,
3242 -1);
3243 restype = TEXTOID;
3244 }
3245
3246 /*
3247 * Rather than clutter the API of get_sort_group_operators and the other
3248 * functions we're about to use, make use of error context callback to
3249 * mark any error reports with a parse position. We point to the operator
3250 * location if present, else to the expression being sorted. (NB: use the
3251 * original untransformed expression here; the TLE entry might well point
3252 * at a duplicate expression in the regular SELECT list.)
3253 */
3254 location = sortby->location;
3255 if (location < 0)
3256 location = exprLocation(sortby->node);
3257 setup_parser_errposition_callback(&pcbstate, pstate, location);
3258
3259 /* determine the sortop, eqop, and directionality */
3260 switch (sortby->sortby_dir)
3261 {
3262 case SORTBY_DEFAULT:
3263 case SORTBY_ASC:
3264 get_sort_group_operators(restype,
3265 true, true, false,
3266 &sortop, &eqop, NULL,
3267 &hashable);
3268 reverse = false;
3269 break;
3270 case SORTBY_DESC:
3271 get_sort_group_operators(restype,
3272 false, true, true,
3273 NULL, &eqop, &sortop,
3274 &hashable);
3275 reverse = true;
3276 break;
3277 case SORTBY_USING:
3278 Assert(sortby->useOp != NIL);
3279 sortop = compatible_oper_opid(sortby->useOp,
3280 restype,
3281 restype,
3282 false);
3283
3284 /*
3285 * Verify it's a valid ordering operator, fetch the corresponding
3286 * equality operator, and determine whether to consider it like
3287 * ASC or DESC.
3288 */
3289 eqop = get_equality_op_for_ordering_op(sortop, &reverse);
3290 if (!OidIsValid(eqop))
3291 ereport(ERROR,
3292 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3293 errmsg("operator %s is not a valid ordering operator",
3294 strVal(llast(sortby->useOp))),
3295 errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
3296
3297 /*
3298 * Also see if the equality operator is hashable.
3299 */
3300 hashable = op_hashjoinable(eqop, restype);
3301 break;
3302 default:
3303 elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
3304 sortop = InvalidOid; /* keep compiler quiet */
3305 eqop = InvalidOid;
3306 hashable = false;
3307 reverse = false;
3308 break;
3309 }
3310
3311 cancel_parser_errposition_callback(&pcbstate);
3312
3313 /* avoid making duplicate sortlist entries */
3314 if (!targetIsInSortList(tle, sortop, sortlist))
3315 {
3316 SortGroupClause *sortcl = makeNode(SortGroupClause);
3317
3318 sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
3319
3320 sortcl->eqop = eqop;
3321 sortcl->sortop = sortop;
3322 sortcl->hashable = hashable;
3323
3324 switch (sortby->sortby_nulls)
3325 {
3326 case SORTBY_NULLS_DEFAULT:
3327 /* NULLS FIRST is default for DESC; other way for ASC */
3328 sortcl->nulls_first = reverse;
3329 break;
3330 case SORTBY_NULLS_FIRST:
3331 sortcl->nulls_first = true;
3332 break;
3333 case SORTBY_NULLS_LAST:
3334 sortcl->nulls_first = false;
3335 break;
3336 default:
3337 elog(ERROR, "unrecognized sortby_nulls: %d",
3338 sortby->sortby_nulls);
3339 break;
3340 }
3341
3342 sortlist = lappend(sortlist, sortcl);
3343 }
3344
3345 return sortlist;
3346}
3347
3348/*
3349 * addTargetToGroupList
3350 * If the given targetlist entry isn't already in the SortGroupClause
3351 * list, add it to the end of the list, using default sort/group
3352 * semantics.
3353 *
3354 * This is very similar to addTargetToSortList, except that we allow the
3355 * case where only a grouping (equality) operator can be found, and that
3356 * the TLE is considered "already in the list" if it appears there with any
3357 * sorting semantics.
3358 *
3359 * location is the parse location to be fingered in event of trouble. Note
3360 * that we can't rely on exprLocation(tle->expr), because that might point
3361 * to a SELECT item that matches the GROUP BY item; it'd be pretty confusing
3362 * to report such a location.
3363 *
3364 * Returns the updated SortGroupClause list.
3365 */
3366static List *
3367addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
3368 List *grouplist, List *targetlist, int location)
3369{
3370 Oid restype = exprType((Node *) tle->expr);
3371
3372 /* if tlist item is an UNKNOWN literal, change it to TEXT */
3373 if (restype == UNKNOWNOID)
3374 {
3375 tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
3376 restype, TEXTOID, -1,
3377 COERCION_IMPLICIT,
3378 COERCE_IMPLICIT_CAST,
3379 -1);
3380 restype = TEXTOID;
3381 }
3382
3383 /* avoid making duplicate grouplist entries */
3384 if (!targetIsInSortList(tle, InvalidOid, grouplist))
3385 {
3386 SortGroupClause *grpcl = makeNode(SortGroupClause);
3387 Oid sortop;
3388 Oid eqop;
3389 bool hashable;
3390 ParseCallbackState pcbstate;
3391
3392 setup_parser_errposition_callback(&pcbstate, pstate, location);
3393
3394 /* determine the eqop and optional sortop */
3395 get_sort_group_operators(restype,
3396 false, true, false,
3397 &sortop, &eqop, NULL,
3398 &hashable);
3399
3400 cancel_parser_errposition_callback(&pcbstate);
3401
3402 grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
3403 grpcl->eqop = eqop;
3404 grpcl->sortop = sortop;
3405 grpcl->nulls_first = false; /* OK with or without sortop */
3406 grpcl->hashable = hashable;
3407
3408 grouplist = lappend(grouplist, grpcl);
3409 }
3410
3411 return grouplist;
3412}
3413
3414/*
3415 * assignSortGroupRef
3416 * Assign the targetentry an unused ressortgroupref, if it doesn't
3417 * already have one. Return the assigned or pre-existing refnumber.
3418 *
3419 * 'tlist' is the targetlist containing (or to contain) the given targetentry.
3420 */
3421Index
3422assignSortGroupRef(TargetEntry *tle, List *tlist)
3423{
3424 Index maxRef;
3425 ListCell *l;
3426
3427 if (tle->ressortgroupref) /* already has one? */
3428 return tle->ressortgroupref;
3429
3430 /* easiest way to pick an unused refnumber: max used + 1 */
3431 maxRef = 0;
3432 foreach(l, tlist)
3433 {
3434 Index ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
3435
3436 if (ref > maxRef)
3437 maxRef = ref;
3438 }
3439 tle->ressortgroupref = maxRef + 1;
3440 return tle->ressortgroupref;
3441}
3442
3443/*
3444 * targetIsInSortList
3445 * Is the given target item already in the sortlist?
3446 * If sortop is not InvalidOid, also test for a match to the sortop.
3447 *
3448 * It is not an oversight that this function ignores the nulls_first flag.
3449 * We check sortop when determining if an ORDER BY item is redundant with
3450 * earlier ORDER BY items, because it's conceivable that "ORDER BY
3451 * foo USING <, foo USING <<<" is not redundant, if <<< distinguishes
3452 * values that < considers equal. We need not check nulls_first
3453 * however, because a lower-order column with the same sortop but
3454 * opposite nulls direction is redundant. Also, we can consider
3455 * ORDER BY foo ASC, foo DESC redundant, so check for a commutator match.
3456 *
3457 * Works for both ordering and grouping lists (sortop would normally be
3458 * InvalidOid when considering grouping). Note that the main reason we need
3459 * this routine (and not just a quick test for nonzeroness of ressortgroupref)
3460 * is that a TLE might be in only one of the lists.
3461 */
3462bool
3463targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
3464{
3465 Index ref = tle->ressortgroupref;
3466 ListCell *l;
3467
3468 /* no need to scan list if tle has no marker */
3469 if (ref == 0)
3470 return false;
3471
3472 foreach(l, sortList)
3473 {
3474 SortGroupClause *scl = (SortGroupClause *) lfirst(l);
3475
3476 if (scl->tleSortGroupRef == ref &&
3477 (sortop == InvalidOid ||
3478 sortop == scl->sortop ||
3479 sortop == get_commutator(scl->sortop)))
3480 return true;
3481 }
3482 return false;
3483}
3484
3485/*
3486 * findWindowClause
3487 * Find the named WindowClause in the list, or return NULL if not there
3488 */
3489static WindowClause *
3490findWindowClause(List *wclist, const char *name)
3491{
3492 ListCell *l;
3493
3494 foreach(l, wclist)
3495 {
3496 WindowClause *wc = (WindowClause *) lfirst(l);
3497
3498 if (wc->name && strcmp(wc->name, name) == 0)
3499 return wc;
3500 }
3501
3502 return NULL;
3503}
3504
3505/*
3506 * transformFrameOffset
3507 * Process a window frame offset expression
3508 *
3509 * In RANGE mode, rangeopfamily is the sort opfamily for the input ORDER BY
3510 * column, and rangeopcintype is the input data type the sort operator is
3511 * registered with. We expect the in_range function to be registered with
3512 * that same type. (In binary-compatible cases, it might be different from
3513 * the input column's actual type, so we can't use that for the lookups.)
3514 * We'll return the OID of the in_range function to *inRangeFunc.
3515 */
3516static Node *
3517transformFrameOffset(ParseState *pstate, int frameOptions,
3518 Oid rangeopfamily, Oid rangeopcintype, Oid *inRangeFunc,
3519 Node *clause)
3520{
3521 const char *constructName = NULL;
3522 Node *node;
3523
3524 *inRangeFunc = InvalidOid; /* default result */
3525
3526 /* Quick exit if no offset expression */
3527 if (clause == NULL)
3528 return NULL;
3529
3530 if (frameOptions & FRAMEOPTION_ROWS)
3531 {
3532 /* Transform the raw expression tree */
3533 node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_ROWS);
3534
3535 /*
3536 * Like LIMIT clause, simply coerce to int8
3537 */
3538 constructName = "ROWS";
3539 node = coerce_to_specific_type(pstate, node, INT8OID, constructName);
3540 }
3541 else if (frameOptions & FRAMEOPTION_RANGE)
3542 {
3543 /*
3544 * We must look up the in_range support function that's to be used,
3545 * possibly choosing one of several, and coerce the "offset" value to
3546 * the appropriate input type.
3547 */
3548 Oid nodeType;
3549 Oid preferredType;
3550 int nfuncs = 0;
3551 int nmatches = 0;
3552 Oid selectedType = InvalidOid;
3553 Oid selectedFunc = InvalidOid;
3554 CatCList *proclist;
3555 int i;
3556
3557 /* Transform the raw expression tree */
3558 node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_RANGE);
3559 nodeType = exprType(node);
3560
3561 /*
3562 * If there are multiple candidates, we'll prefer the one that exactly
3563 * matches nodeType; or if nodeType is as yet unknown, prefer the one
3564 * that exactly matches the sort column type. (The second rule is
3565 * like what we do for "known_type operator unknown".)
3566 */
3567 preferredType = (nodeType != UNKNOWNOID) ? nodeType : rangeopcintype;
3568
3569 /* Find the in_range support functions applicable to this case */
3570 proclist = SearchSysCacheList2(AMPROCNUM,
3571 ObjectIdGetDatum(rangeopfamily),
3572 ObjectIdGetDatum(rangeopcintype));
3573 for (i = 0; i < proclist->n_members; i++)
3574 {
3575 HeapTuple proctup = &proclist->members[i]->tuple;
3576 Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
3577
3578 /* The search will find all support proc types; ignore others */
3579 if (procform->amprocnum != BTINRANGE_PROC)
3580 continue;
3581 nfuncs++;
3582
3583 /* Ignore function if given value can't be coerced to that type */
3584 if (!can_coerce_type(1, &nodeType, &procform->amprocrighttype,
3585 COERCION_IMPLICIT))
3586 continue;
3587 nmatches++;
3588
3589 /* Remember preferred match, or any match if didn't find that */
3590 if (selectedType != preferredType)
3591 {
3592 selectedType = procform->amprocrighttype;
3593 selectedFunc = procform->amproc;
3594 }
3595 }
3596 ReleaseCatCacheList(proclist);
3597
3598 /*
3599 * Throw error if needed. It seems worth taking the trouble to
3600 * distinguish "no support at all" from "you didn't match any
3601 * available offset type".
3602 */
3603 if (nfuncs == 0)
3604 ereport(ERROR,
3605 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3606 errmsg("RANGE with offset PRECEDING/FOLLOWING is not supported for column type %s",
3607 format_type_be(rangeopcintype)),
3608 parser_errposition(pstate, exprLocation(node))));
3609 if (nmatches == 0)
3610 ereport(ERROR,
3611 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3612 errmsg("RANGE with offset PRECEDING/FOLLOWING is not supported for column type %s and offset type %s",
3613 format_type_be(rangeopcintype),
3614 format_type_be(nodeType)),
3615 errhint("Cast the offset value to an appropriate type."),
3616 parser_errposition(pstate, exprLocation(node))));
3617 if (nmatches != 1 && selectedType != preferredType)
3618 ereport(ERROR,
3619 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3620 errmsg("RANGE with offset PRECEDING/FOLLOWING has multiple interpretations for column type %s and offset type %s",
3621 format_type_be(rangeopcintype),
3622 format_type_be(nodeType)),
3623 errhint("Cast the offset value to the exact intended type."),
3624 parser_errposition(pstate, exprLocation(node))));
3625
3626 /* OK, coerce the offset to the right type */
3627 constructName = "RANGE";
3628 node = coerce_to_specific_type(pstate, node,
3629 selectedType, constructName);
3630 *inRangeFunc = selectedFunc;
3631 }
3632 else if (frameOptions & FRAMEOPTION_GROUPS)
3633 {
3634 /* Transform the raw expression tree */
3635 node = transformExpr(pstate, clause, EXPR_KIND_WINDOW_FRAME_GROUPS);
3636
3637 /*
3638 * Like LIMIT clause, simply coerce to int8
3639 */
3640 constructName = "GROUPS";
3641 node = coerce_to_specific_type(pstate, node, INT8OID, constructName);
3642 }
3643 else
3644 {
3645 Assert(false);
3646 node = NULL;
3647 }
3648
3649 /* Disallow variables in frame offsets */
3650 checkExprIsVarFree(pstate, node, constructName);
3651
3652 return node;
3653}
3654