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