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