1
/*-------------------------------------------------------------------------
4
* handle clauses in parser
6
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7
* Portions Copyright (c) 1994, Regents of the University of California
13
*-------------------------------------------------------------------------
18
#include "access/heapam.h"
19
#include "catalog/heap.h"
20
#include "catalog/pg_type.h"
21
#include "commands/defrem.h"
22
#include "nodes/makefuncs.h"
23
#include "nodes/nodeFuncs.h"
24
#include "optimizer/tlist.h"
25
#include "optimizer/var.h"
26
#include "parser/analyze.h"
27
#include "parser/parsetree.h"
28
#include "parser/parse_clause.h"
29
#include "parser/parse_coerce.h"
30
#include "parser/parse_expr.h"
31
#include "parser/parse_oper.h"
32
#include "parser/parse_relation.h"
33
#include "parser/parse_target.h"
34
#include "rewrite/rewriteManip.h"
35
#include "utils/guc.h"
36
#include "utils/lsyscache.h"
37
#include "utils/rel.h"
40
#define ORDER_CLAUSE 0
41
#define GROUP_CLAUSE 1
42
#define DISTINCT_ON_CLAUSE 2
43
#define PARTITION_CLAUSE 3
45
static const char * const clauseText[] = {
52
static void extractRemainingColumns(List *common_colnames,
53
List *src_colnames, List *src_colvars,
54
List **res_colnames, List **res_colvars);
55
static Node *transformJoinUsingClause(ParseState *pstate,
56
RangeTblEntry *leftRTE, RangeTblEntry *rightRTE,
57
List *leftVars, List *rightVars);
58
static Node *transformJoinOnClause(ParseState *pstate, JoinExpr *j,
62
Relids containedRels);
63
static RangeTblEntry *transformTableEntry(ParseState *pstate, RangeVar *r);
64
static RangeTblEntry *transformCTEReference(ParseState *pstate, RangeVar *r,
65
CommonTableExpr *cte, Index levelsup);
66
static RangeTblEntry *transformRangeSubselect(ParseState *pstate,
68
static RangeTblEntry *transformRangeFunction(ParseState *pstate,
70
static Node *transformFromClauseItem(ParseState *pstate, Node *n,
71
RangeTblEntry **top_rte, int *top_rti,
73
Relids *containedRels);
74
static Node *buildMergedJoinVar(ParseState *pstate, JoinType jointype,
75
Var *l_colvar, Var *r_colvar);
76
static TargetEntry *findTargetlistEntry(ParseState *pstate, Node *node,
77
List **tlist, int clause);
78
static int get_matching_location(int sortgroupref,
79
List *sortgrouprefs, List *exprs);
80
static List *addTargetToSortList(ParseState *pstate, TargetEntry *tle,
81
List *sortlist, List *targetlist, SortBy *sortby,
83
static List *addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
84
List *grouplist, List *targetlist, int location,
86
static WindowClause *findWindowClause(List *wclist, const char *name);
90
* transformFromClause -
91
* Process the FROM clause and add items to the query's range table,
92
* joinlist, and namespaces.
94
* Note: we assume that pstate's p_rtable, p_joinlist, p_relnamespace, and
95
* p_varnamespace lists were initialized to NIL when the pstate was created.
96
* We will add onto any entries already present --- this is needed for rule
97
* processing, as well as for UPDATE and DELETE.
99
* The range table may grow still further when we transform the expressions
100
* in the query's quals and target list. (This is possible because in
101
* POSTQUEL, we allowed references to relations not specified in the
102
* from-clause. PostgreSQL keeps this extension to standard SQL.)
105
transformFromClause(ParseState *pstate, List *frmList)
110
* The grammar will have produced a list of RangeVars, RangeSubselects,
111
* RangeFunctions, and/or JoinExprs. Transform each one (possibly adding
112
* entries to the rtable), check for duplicate refnames, and then add it
113
* to the joinlist and namespaces.
117
Node *n = lfirst(fl);
121
Relids containedRels;
123
n = transformFromClauseItem(pstate, n,
128
checkNameSpaceConflicts(pstate, pstate->p_relnamespace, relnamespace);
129
pstate->p_joinlist = lappend(pstate->p_joinlist, n);
130
pstate->p_relnamespace = list_concat(pstate->p_relnamespace,
132
pstate->p_varnamespace = lappend(pstate->p_varnamespace, rte);
133
bms_free(containedRels);
139
* Add the target relation of INSERT/UPDATE/DELETE to the range table,
140
* and make the special links to it in the ParseState.
142
* We also open the target relation and acquire a write lock on it.
143
* This must be done before processing the FROM list, in case the target
144
* is also mentioned as a source relation --- we want to be sure to grab
145
* the write lock before any read lock.
147
* If alsoSource is true, add the target to the query's joinlist and
148
* namespace. For INSERT, we don't want the target to be joined to;
149
* it's a destination of tuples, not a source. For UPDATE/DELETE,
150
* we do need to scan or join the target. (NOTE: we do not bother
151
* to check for namespace conflict; we assume that the namespace was
152
* initially empty in these cases.)
154
* Finally, we mark the relation as requiring the permissions specified
157
* Returns the rangetable index of the target relation.
160
setTargetTable(ParseState *pstate, RangeVar *relation,
161
bool inh, bool alsoSource, AclMode requiredPerms)
166
/* Close old target; this could only happen for multi-action rules */
167
if (pstate->p_target_relation != NULL)
168
heap_close(pstate->p_target_relation, NoLock);
171
* Open target rel and grab suitable lock (which we will hold till end of
174
* free_parsestate() will eventually do the corresponding heap_close(),
175
* but *not* release the lock.
177
pstate->p_target_relation = parserOpenTable(pstate, relation,
183
rte = addRangeTableEntryForRelation(pstate, pstate->p_target_relation,
184
relation->alias, inh, false);
185
pstate->p_target_rangetblentry = rte;
187
/* assume new rte is at end */
188
rtindex = list_length(pstate->p_rtable);
189
Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
192
* Override addRangeTableEntry's default ACL_SELECT permissions check, and
193
* instead mark target table as requiring exactly the specified
196
* If we find an explicit reference to the rel later during parse
197
* analysis, we will add the ACL_SELECT bit back again; see
198
* markVarForSelectPriv and its callers.
200
rte->requiredPerms = requiredPerms;
203
* If UPDATE/DELETE, add table to joinlist and namespaces.
206
addRTEtoQuery(pstate, rte, true, true, true);
212
* Simplify InhOption (yes/no/default) into boolean yes/no.
214
* The reason we do things this way is that we don't want to examine the
215
* SQL_inheritance option flag until parse_analyze() is run. Otherwise,
216
* we'd do the wrong thing with query strings that intermix SET commands
220
interpretInhOption(InhOption inhOpt)
229
return SQL_inheritance;
231
elog(ERROR, "bogus InhOption value: %d", inhOpt);
232
return false; /* keep compiler quiet */
236
* Given a relation-options list (of ReloptElems), return true iff the specified
237
* table/result set should be created with OIDs. This needs to be done after
238
* parsing the query string because the return value can depend upon the
239
* default_with_oids GUC var.
242
interpretOidsOption(List *defList)
246
/* Scan list to see if OIDS was included */
247
foreach(cell, defList)
249
ReloptElem *def = (ReloptElem *) lfirst(cell);
251
if (pg_strcasecmp(def->optname, "oids") == 0)
252
return reloptGetBoolean(def);
255
/* OIDS option was not specified, so use default. */
256
return default_with_oids;
260
* Extract all not-in-common columns from column lists of a source table
263
extractRemainingColumns(List *common_colnames,
264
List *src_colnames, List *src_colvars,
265
List **res_colnames, List **res_colvars)
267
List *new_colnames = NIL;
268
List *new_colvars = NIL;
272
Assert(list_length(src_colnames) == list_length(src_colvars));
274
forboth(lnames, src_colnames, lvars, src_colvars)
276
char *colname = strVal(lfirst(lnames));
280
foreach(cnames, common_colnames)
282
char *ccolname = strVal(lfirst(cnames));
284
if (strcmp(colname, ccolname) == 0)
293
new_colnames = lappend(new_colnames, lfirst(lnames));
294
new_colvars = lappend(new_colvars, lfirst(lvars));
298
*res_colnames = new_colnames;
299
*res_colvars = new_colvars;
302
/* transformJoinUsingClause()
303
* Build a complete ON clause from a partially-transformed USING list.
304
* We are given lists of nodes representing left and right match columns.
305
* Result is a transformed qualification expression.
308
transformJoinUsingClause(ParseState *pstate,
309
RangeTblEntry *leftRTE, RangeTblEntry *rightRTE,
310
List *leftVars, List *rightVars)
317
* We cheat a little bit here by building an untransformed operator tree
318
* whose leaves are the already-transformed Vars. This is OK because
319
* transformExpr() won't complain about already-transformed subnodes.
320
* However, this does mean that we have to mark the columns as requiring
321
* SELECT privilege for ourselves; transformExpr() won't do it.
323
forboth(lvars, leftVars, rvars, rightVars)
325
Var *lvar = (Var *) lfirst(lvars);
326
Var *rvar = (Var *) lfirst(rvars);
329
/* Require read access to the join variables */
330
markVarForSelectPriv(pstate, lvar, leftRTE);
331
markVarForSelectPriv(pstate, rvar, rightRTE);
333
/* Now create the lvar = rvar join condition */
334
e = makeSimpleA_Expr(AEXPR_OP, "=",
335
copyObject(lvar), copyObject(rvar),
338
/* And combine into an AND clause, if multiple join columns */
345
a = makeA_Expr(AEXPR_AND, NIL, result, (Node *) e, -1);
351
* Since the references are already Vars, and are certainly from the input
352
* relations, we don't have to go through the same pushups that
353
* transformJoinOnClause() does. Just invoke transformExpr() to fix up
354
* the operators, and we're done.
356
result = transformExpr(pstate, result);
358
result = coerce_to_boolean(pstate, result, "JOIN/USING");
363
/* transformJoinOnClause()
364
* Transform the qual conditions for JOIN/ON.
365
* Result is a transformed qualification expression.
368
transformJoinOnClause(ParseState *pstate, JoinExpr *j,
369
RangeTblEntry *l_rte,
370
RangeTblEntry *r_rte,
372
Relids containedRels)
375
List *save_relnamespace;
376
List *save_varnamespace;
377
Relids clause_varnos;
381
* This is a tad tricky, for two reasons. First, the namespace that the
382
* join expression should see is just the two subtrees of the JOIN plus
383
* any outer references from upper pstate levels. So, temporarily set
384
* this pstate's namespace accordingly. (We need not check for refname
385
* conflicts, because transformFromClauseItem() already did.) NOTE: this
386
* code is OK only because the ON clause can't legally alter the namespace
387
* by causing implicit relation refs to be added.
389
save_relnamespace = pstate->p_relnamespace;
390
save_varnamespace = pstate->p_varnamespace;
392
pstate->p_relnamespace = relnamespace;
393
pstate->p_varnamespace = list_make2(l_rte, r_rte);
395
result = transformWhereClause(pstate, j->quals, "JOIN/ON");
397
pstate->p_relnamespace = save_relnamespace;
398
pstate->p_varnamespace = save_varnamespace;
401
* Second, we need to check that the ON condition doesn't refer to any
402
* rels outside the input subtrees of the JOIN. It could do that despite
403
* our hack on the namespace if it uses fully-qualified names. So, grovel
404
* through the transformed clause and make sure there are no bogus
405
* references. (Outer references are OK, and are ignored here.)
407
clause_varnos = pull_varnos(result);
408
clause_varnos = bms_del_members(clause_varnos, containedRels);
409
if ((varno = bms_first_member(clause_varnos)) >= 0)
412
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
413
errmsg("JOIN/ON clause refers to \"%s\", which is not part of JOIN",
414
rt_fetch(varno, pstate->p_rtable)->eref->aliasname),
415
parser_errposition(pstate,
416
locate_var_of_relation(result, varno, 0))));
418
bms_free(clause_varnos);
424
* transformTableEntry --- transform a RangeVar (simple relation reference)
426
static RangeTblEntry *
427
transformTableEntry(ParseState *pstate, RangeVar *r)
432
* mark this entry to indicate it comes from the FROM clause. In SQL, the
433
* target list can only refer to range variables specified in the from
434
* clause but we follow the more powerful POSTQUEL semantics and
435
* automatically generate the range variable if not specified. However
436
* there are times we need to know whether the entries are legitimate.
438
rte = addRangeTableEntry(pstate, r, r->alias,
439
interpretInhOption(r->inhOpt), true);
445
* transformCTEReference --- transform a RangeVar that references a common
446
* table expression (ie, a sub-SELECT defined in a WITH clause)
448
static RangeTblEntry *
449
transformCTEReference(ParseState *pstate, RangeVar *r,
450
CommonTableExpr *cte, Index levelsup)
454
rte = addRangeTableEntryForCTE(pstate, cte, levelsup, r->alias, true);
460
* transformRangeSubselect --- transform a sub-SELECT appearing in FROM
462
static RangeTblEntry *
463
transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
469
* We require user to supply an alias for a subselect, per SQL92. To relax
470
* this, we'd have to be prepared to gin up a unique alias for an
471
* unlabeled subselect. (This is just elog, not ereport, because the
472
* grammar should have enforced it already.)
474
if (r->alias == NULL)
475
elog(ERROR, "subquery in FROM must have an alias");
478
* Analyze and transform the subquery.
480
query = parse_sub_analyze(r->subquery, pstate);
483
* Check that we got something reasonable. Many of these conditions are
484
* impossible given restrictions of the grammar, but check 'em anyway.
486
if (!IsA(query, Query) ||
487
query->commandType != CMD_SELECT ||
488
query->utilityStmt != NULL)
489
elog(ERROR, "unexpected non-SELECT command in subquery in FROM");
490
if (query->intoClause)
492
(errcode(ERRCODE_SYNTAX_ERROR),
493
errmsg("subquery in FROM cannot have SELECT INTO"),
494
parser_errposition(pstate,
495
exprLocation((Node *) query->intoClause))));
498
* The subquery cannot make use of any variables from FROM items created
499
* earlier in the current query. Per SQL92, the scope of a FROM item does
500
* not include other FROM items. Formerly we hacked the namespace so that
501
* the other variables weren't even visible, but it seems more useful to
502
* leave them visible and give a specific error message.
504
* XXX this will need further work to support SQL99's LATERAL() feature,
505
* wherein such references would indeed be legal.
507
* We can skip groveling through the subquery if there's not anything
508
* visible in the current query. Also note that outer references are OK.
510
if (pstate->p_relnamespace || pstate->p_varnamespace)
512
if (contain_vars_of_level((Node *) query, 1))
514
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
515
errmsg("subquery in FROM cannot refer to other relations of same query level"),
516
parser_errposition(pstate,
517
locate_var_of_level((Node *) query, 1))));
521
* OK, build an RTE for the subquery.
523
rte = addRangeTableEntryForSubquery(pstate, query, r->alias, true);
530
* transformRangeFunction --- transform a function call appearing in FROM
532
static RangeTblEntry *
533
transformRangeFunction(ParseState *pstate, RangeFunction *r)
540
* Get function name for possible use as alias. We use the same
541
* transformation rules as for a SELECT output expression. For a FuncCall
542
* node, the result will be the function name, but it is possible for the
543
* grammar to hand back other node types.
545
funcname = FigureColname(r->funccallnode);
548
* Transform the raw expression.
550
funcexpr = transformExpr(pstate, r->funccallnode);
553
* The function parameters cannot make use of any variables from other
554
* FROM items. (Compare to transformRangeSubselect(); the coding is
555
* different though because we didn't parse as a sub-select with its own
556
* level of namespace.)
558
* XXX this will need further work to support SQL99's LATERAL() feature,
559
* wherein such references would indeed be legal.
561
if (pstate->p_relnamespace || pstate->p_varnamespace)
563
if (contain_vars_of_level(funcexpr, 0))
565
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
566
errmsg("function expression in FROM cannot refer to other relations of same query level"),
567
parser_errposition(pstate,
568
locate_var_of_level(funcexpr, 0))));
572
* Disallow aggregate functions in the expression. (No reason to postpone
573
* this check until parseCheckAggregates.)
575
if (pstate->p_hasAggs &&
576
checkExprHasAggs(funcexpr))
578
(errcode(ERRCODE_GROUPING_ERROR),
579
errmsg("cannot use aggregate function in function expression in FROM"),
580
parser_errposition(pstate,
581
locate_agg_of_level(funcexpr, 0))));
582
if (pstate->p_hasWindowFuncs &&
583
checkExprHasWindowFuncs(funcexpr))
585
(errcode(ERRCODE_WINDOWING_ERROR),
586
errmsg("cannot use window function in function expression in FROM"),
587
parser_errposition(pstate,
588
locate_windowfunc(funcexpr))));
591
* OK, build an RTE for the function.
593
rte = addRangeTableEntryForFunction(pstate, funcname, funcexpr,
597
* If a coldeflist was supplied, ensure it defines a legal set of names
598
* (no duplicates) and datatypes (no pseudo-types, for instance).
599
* addRangeTableEntryForFunction looked up the type names but didn't check
600
* them further than that.
606
tupdesc = BuildDescFromLists(rte->eref->colnames,
608
rte->funccoltypmods);
609
CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE);
617
* transformFromClauseItem -
618
* Transform a FROM-clause item, adding any required entries to the
619
* range table list being built in the ParseState, and return the
620
* transformed item ready to include in the joinlist and namespaces.
621
* This routine can recurse to handle SQL92 JOIN expressions.
623
* The function return value is the node to add to the jointree (a
624
* RangeTblRef or JoinExpr). Additional output parameters are:
626
* *top_rte: receives the RTE corresponding to the jointree item.
627
* (We could extract this from the function return node, but it saves cycles
628
* to pass it back separately.)
630
* *top_rti: receives the rangetable index of top_rte. (Ditto.)
632
* *relnamespace: receives a List of the RTEs exposed as relation names
635
* *containedRels: receives a bitmap set of the rangetable indexes
636
* of all the base and join relations represented in this jointree item.
637
* This is needed for checking JOIN/ON conditions in higher levels.
639
* We do not need to pass back an explicit varnamespace value, because
640
* in all cases the varnamespace contribution is exactly top_rte.
643
transformFromClauseItem(ParseState *pstate, Node *n,
644
RangeTblEntry **top_rte, int *top_rti,
646
Relids *containedRels)
648
if (IsA(n, RangeVar))
650
/* Plain relation reference, or perhaps a CTE reference */
651
RangeVar *rv = (RangeVar *) n;
653
RangeTblEntry *rte = NULL;
656
/* if it is an unqualified name, it might be a CTE reference */
659
CommonTableExpr *cte;
662
cte = scanNameSpaceForCTE(pstate, rv->relname, &levelsup);
664
rte = transformCTEReference(pstate, rv, cte, levelsup);
667
/* if not found as a CTE, must be a table reference */
669
rte = transformTableEntry(pstate, rv);
671
/* assume new rte is at end */
672
rtindex = list_length(pstate->p_rtable);
673
Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
676
*relnamespace = list_make1(rte);
677
*containedRels = bms_make_singleton(rtindex);
678
rtr = makeNode(RangeTblRef);
679
rtr->rtindex = rtindex;
682
else if (IsA(n, RangeSubselect))
684
/* sub-SELECT is like a plain relation */
689
rte = transformRangeSubselect(pstate, (RangeSubselect *) n);
690
/* assume new rte is at end */
691
rtindex = list_length(pstate->p_rtable);
692
Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
695
*relnamespace = list_make1(rte);
696
*containedRels = bms_make_singleton(rtindex);
697
rtr = makeNode(RangeTblRef);
698
rtr->rtindex = rtindex;
701
else if (IsA(n, RangeFunction))
703
/* function is like a plain relation */
708
rte = transformRangeFunction(pstate, (RangeFunction *) n);
709
/* assume new rte is at end */
710
rtindex = list_length(pstate->p_rtable);
711
Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
714
*relnamespace = list_make1(rte);
715
*containedRels = bms_make_singleton(rtindex);
716
rtr = makeNode(RangeTblRef);
717
rtr->rtindex = rtindex;
720
else if (IsA(n, JoinExpr))
722
/* A newfangled join expression */
723
JoinExpr *j = (JoinExpr *) n;
724
RangeTblEntry *l_rte;
725
RangeTblEntry *r_rte;
728
Relids l_containedRels,
731
List *l_relnamespace,
744
* Recursively process the left and right subtrees
746
j->larg = transformFromClauseItem(pstate, j->larg,
751
j->rarg = transformFromClauseItem(pstate, j->rarg,
758
* Check for conflicting refnames in left and right subtrees. Must do
759
* this because higher levels will assume I hand back a self-
760
* consistent namespace subtree.
762
checkNameSpaceConflicts(pstate, l_relnamespace, r_relnamespace);
765
* Generate combined relation membership info for possible use by
766
* transformJoinOnClause below.
768
my_relnamespace = list_concat(l_relnamespace, r_relnamespace);
769
my_containedRels = bms_join(l_containedRels, r_containedRels);
771
pfree(r_relnamespace); /* free unneeded list header */
774
* Extract column name and var lists from both subtrees
776
* Note: expandRTE returns new lists, safe for me to modify
778
expandRTE(l_rte, l_rtindex, 0, -1, false,
779
&l_colnames, &l_colvars);
780
expandRTE(r_rte, r_rtindex, 0, -1, false,
781
&r_colnames, &r_colvars);
784
* Natural join does not explicitly specify columns; must generate
785
* columns to join. Need to run through the list of columns from each
786
* table or join result and match up the column names. Use the first
787
* table, and check every column in the second table for a match.
788
* (We'll check that the matches were unique later on.) The result of
789
* this step is a list of column names just like an explicitly-written
798
Assert(j->using == NIL); /* shouldn't have USING() too */
800
foreach(lx, l_colnames)
802
char *l_colname = strVal(lfirst(lx));
803
Value *m_name = NULL;
805
foreach(rx, r_colnames)
807
char *r_colname = strVal(lfirst(rx));
809
if (strcmp(l_colname, r_colname) == 0)
811
m_name = makeString(l_colname);
816
/* matched a right column? then keep as join column... */
818
rlist = lappend(rlist, m_name);
825
* Now transform the join qualifications, if any.
833
* JOIN/USING (or NATURAL JOIN, as transformed above). Transform
834
* the list into an explicit ON-condition, and generate a list of
835
* merged result columns.
837
List *ucols = j->using;
838
List *l_usingvars = NIL;
839
List *r_usingvars = NIL;
842
Assert(j->quals == NULL); /* shouldn't have ON() too */
846
char *u_colname = strVal(lfirst(ucol));
854
/* Check for USING(foo,foo) */
855
foreach(col, res_colnames)
857
char *res_colname = strVal(lfirst(col));
859
if (strcmp(res_colname, u_colname) == 0)
861
(errcode(ERRCODE_DUPLICATE_COLUMN),
862
errmsg("column name \"%s\" appears more than once in USING clause",
866
/* Find it in left input */
868
foreach(col, l_colnames)
870
char *l_colname = strVal(lfirst(col));
872
if (strcmp(l_colname, u_colname) == 0)
876
(errcode(ERRCODE_AMBIGUOUS_COLUMN),
877
errmsg("common column name \"%s\" appears more than once in left table",
885
(errcode(ERRCODE_UNDEFINED_COLUMN),
886
errmsg("column \"%s\" specified in USING clause does not exist in left table",
889
/* Find it in right input */
891
foreach(col, r_colnames)
893
char *r_colname = strVal(lfirst(col));
895
if (strcmp(r_colname, u_colname) == 0)
899
(errcode(ERRCODE_AMBIGUOUS_COLUMN),
900
errmsg("common column name \"%s\" appears more than once in right table",
908
(errcode(ERRCODE_UNDEFINED_COLUMN),
909
errmsg("column \"%s\" specified in USING clause does not exist in right table",
912
l_colvar = list_nth(l_colvars, l_index);
913
l_usingvars = lappend(l_usingvars, l_colvar);
914
r_colvar = list_nth(r_colvars, r_index);
915
r_usingvars = lappend(r_usingvars, r_colvar);
917
res_colnames = lappend(res_colnames, lfirst(ucol));
918
res_colvars = lappend(res_colvars,
919
buildMergedJoinVar(pstate,
925
j->quals = transformJoinUsingClause(pstate,
933
/* User-written ON-condition; transform it */
934
j->quals = transformJoinOnClause(pstate, j,
941
/* CROSS JOIN: no quals */
944
/* Add remaining columns from each side to the output columns */
945
extractRemainingColumns(res_colnames,
946
l_colnames, l_colvars,
947
&l_colnames, &l_colvars);
948
extractRemainingColumns(res_colnames,
949
r_colnames, r_colvars,
950
&r_colnames, &r_colvars);
951
res_colnames = list_concat(res_colnames, l_colnames);
952
res_colvars = list_concat(res_colvars, l_colvars);
953
res_colnames = list_concat(res_colnames, r_colnames);
954
res_colvars = list_concat(res_colvars, r_colvars);
957
* Check alias (AS clause), if any.
961
if (j->alias->colnames != NIL)
963
if (list_length(j->alias->colnames) > list_length(res_colnames))
965
(errcode(ERRCODE_SYNTAX_ERROR),
966
errmsg("column alias list for \"%s\" has too many entries",
967
j->alias->aliasname)));
972
* Now build an RTE for the result of the join
974
rte = addRangeTableEntryForJoin(pstate,
981
/* assume new rte is at end */
982
j->rtindex = list_length(pstate->p_rtable);
983
Assert(rte == rt_fetch(j->rtindex, pstate->p_rtable));
986
*top_rti = j->rtindex;
988
/* make a matching link to the JoinExpr for later use */
989
for (k = list_length(pstate->p_joinexprs) + 1; k < j->rtindex; k++)
990
pstate->p_joinexprs = lappend(pstate->p_joinexprs, NULL);
991
pstate->p_joinexprs = lappend(pstate->p_joinexprs, j);
992
Assert(list_length(pstate->p_joinexprs) == j->rtindex);
995
* Prepare returned namespace list. If the JOIN has an alias then it
996
* hides the contained RTEs as far as the relnamespace goes;
997
* otherwise, put the contained RTEs and *not* the JOIN into
1002
*relnamespace = list_make1(rte);
1003
list_free(my_relnamespace);
1006
*relnamespace = my_relnamespace;
1009
* Include join RTE in returned containedRels set
1011
*containedRels = bms_add_member(my_containedRels, j->rtindex);
1016
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(n));
1017
return NULL; /* can't get here, keep compiler quiet */
1021
* buildMergedJoinVar -
1022
* generate a suitable replacement expression for a merged join column
1025
buildMergedJoinVar(ParseState *pstate, JoinType jointype,
1026
Var *l_colvar, Var *r_colvar)
1035
* Choose output type if input types are dissimilar.
1037
outcoltype = l_colvar->vartype;
1038
outcoltypmod = l_colvar->vartypmod;
1039
if (outcoltype != r_colvar->vartype)
1041
outcoltype = select_common_type(pstate,
1042
list_make2(l_colvar, r_colvar),
1045
outcoltypmod = -1; /* ie, unknown */
1047
else if (outcoltypmod != r_colvar->vartypmod)
1049
/* same type, but not same typmod */
1050
outcoltypmod = -1; /* ie, unknown */
1054
* Insert coercion functions if needed. Note that a difference in typmod
1055
* can only happen if input has typmod but outcoltypmod is -1. In that
1056
* case we insert a RelabelType to clearly mark that result's typmod is
1057
* not same as input. We never need coerce_type_typmod.
1059
if (l_colvar->vartype != outcoltype)
1060
l_node = coerce_type(pstate, (Node *) l_colvar, l_colvar->vartype,
1061
outcoltype, outcoltypmod,
1062
COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1);
1063
else if (l_colvar->vartypmod != outcoltypmod)
1064
l_node = (Node *) makeRelabelType((Expr *) l_colvar,
1065
outcoltype, outcoltypmod,
1066
COERCE_IMPLICIT_CAST);
1068
l_node = (Node *) l_colvar;
1070
if (r_colvar->vartype != outcoltype)
1071
r_node = coerce_type(pstate, (Node *) r_colvar, r_colvar->vartype,
1072
outcoltype, outcoltypmod,
1073
COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1);
1074
else if (r_colvar->vartypmod != outcoltypmod)
1075
r_node = (Node *) makeRelabelType((Expr *) r_colvar,
1076
outcoltype, outcoltypmod,
1077
COERCE_IMPLICIT_CAST);
1079
r_node = (Node *) r_colvar;
1082
* Choose what to emit
1089
* We can use either var; prefer non-coerced one if available.
1091
if (IsA(l_node, Var))
1093
else if (IsA(r_node, Var))
1099
/* Always use left var */
1103
/* Always use right var */
1109
* Here we must build a COALESCE expression to ensure that the
1110
* join output is non-null if either input is.
1112
CoalesceExpr *c = makeNode(CoalesceExpr);
1114
c->coalescetype = outcoltype;
1115
c->args = list_make2(l_node, r_node);
1117
res_node = (Node *) c;
1121
elog(ERROR, "unrecognized join type: %d", (int) jointype);
1122
res_node = NULL; /* keep compiler quiet */
1131
* transformWhereClause -
1132
* Transform the qualification and make sure it is of type boolean.
1133
* Used for WHERE and allied clauses.
1135
* constructName does not affect the semantics, but is used in error messages
1138
transformWhereClause(ParseState *pstate, Node *clause,
1139
const char *constructName)
1146
qual = transformExpr(pstate, clause);
1148
qual = coerce_to_boolean(pstate, qual, constructName);
1155
* transformLimitClause -
1156
* Transform the expression and make sure it is of type bigint.
1157
* Used for LIMIT and allied clauses.
1159
* Note: as of Postgres 8.2, LIMIT expressions are expected to yield int8,
1160
* rather than int4 as before.
1162
* constructName does not affect the semantics, but is used in error messages
1165
transformLimitClause(ParseState *pstate, Node *clause,
1166
const char *constructName)
1173
qual = transformExpr(pstate, clause);
1175
qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
1178
* LIMIT can't refer to any vars or aggregates of the current query
1180
if (contain_vars_of_level(qual, 0))
1183
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1184
/* translator: %s is name of a SQL construct, eg LIMIT */
1185
errmsg("argument of %s must not contain variables",
1187
parser_errposition(pstate,
1188
locate_var_of_level(qual, 0))));
1190
if (pstate->p_hasAggs &&
1191
checkExprHasAggs(qual))
1194
(errcode(ERRCODE_GROUPING_ERROR),
1195
/* translator: %s is name of a SQL construct, eg LIMIT */
1196
errmsg("argument of %s must not contain aggregate functions",
1198
parser_errposition(pstate,
1199
locate_agg_of_level(qual, 0))));
1201
if (pstate->p_hasWindowFuncs &&
1202
checkExprHasWindowFuncs(qual))
1205
(errcode(ERRCODE_WINDOWING_ERROR),
1206
/* translator: %s is name of a SQL construct, eg LIMIT */
1207
errmsg("argument of %s must not contain window functions",
1209
parser_errposition(pstate,
1210
locate_windowfunc(qual))));
1218
* findTargetlistEntry -
1219
* Returns the targetlist entry matching the given (untransformed) node.
1220
* If no matching entry exists, one is created and appended to the target
1221
* list as a "resjunk" node.
1223
* node the ORDER BY, GROUP BY, or DISTINCT ON expression to be matched
1224
* tlist the target list (passed by reference so we can append to it)
1225
* clause identifies clause type being processed
1227
static TargetEntry *
1228
findTargetlistEntry(ParseState *pstate, Node *node, List **tlist, int clause)
1230
TargetEntry *target_result = NULL;
1235
* Handle two special cases as mandated by the SQL92 spec:
1237
* 1. Bare ColumnName (no qualifier or subscripts)
1238
* For a bare identifier, we search for a matching column name
1239
* in the existing target list. Multiple matches are an error
1240
* unless they refer to identical values; for example,
1241
* we allow SELECT a, a FROM table ORDER BY a
1242
* but not SELECT a AS b, b FROM table ORDER BY b
1243
* If no match is found, we fall through and treat the identifier
1245
* For GROUP BY, it is incorrect to match the grouping item against
1246
* targetlist entries: according to SQL92, an identifier in GROUP BY
1247
* is a reference to a column name exposed by FROM, not to a target
1248
* list column. However, many implementations (including pre-7.0
1249
* PostgreSQL) accept this anyway. So for GROUP BY, we look first
1250
* to see if the identifier matches any FROM column name, and only
1251
* try for a targetlist name if it doesn't. This ensures that we
1252
* adhere to the spec in the case where the name could be both.
1253
* DISTINCT ON isn't in the standard, so we can do what we like there;
1254
* we choose to make it work like ORDER BY, on the rather flimsy
1255
* grounds that ordinary DISTINCT works on targetlist entries.
1257
* 2. IntegerConstant
1258
* This means to use the n'th item in the existing target list.
1259
* Note that it would make no sense to order/group/distinct by an
1260
* actual constant, so this does not create a conflict with our
1261
* extension to order/group by an expression.
1262
* GROUP BY column-number is not allowed by SQL92, but since
1263
* the standard has no other behavior defined for this syntax,
1264
* we may as well accept this common extension.
1266
* Note that pre-existing resjunk targets must not be used in either case,
1267
* since the user didn't write them in his SELECT list.
1269
* If neither special case applies, fall through to treat the item as
1273
if (IsA(node, ColumnRef) &&
1274
list_length(((ColumnRef *) node)->fields) == 1 &&
1275
IsA(linitial(((ColumnRef *) node)->fields), String))
1277
char *name = strVal(linitial(((ColumnRef *) node)->fields));
1278
int location = ((ColumnRef *) node)->location;
1280
if (clause == GROUP_CLAUSE || clause == PARTITION_CLAUSE)
1283
* In GROUP BY, we must prefer a match against a FROM-clause
1284
* column to one against the targetlist. Look to see if there is
1285
* a matching column. If so, fall through to let transformExpr()
1286
* do the rest. NOTE: if name could refer ambiguously to more
1287
* than one column name exposed by FROM, colNameToVar will
1288
* ereport(ERROR). That's just what we want here.
1290
* Small tweak for 7.4.3: ignore matches in upper query levels.
1291
* This effectively changes the search order for bare names to (1)
1292
* local FROM variables, (2) local targetlist aliases, (3) outer
1293
* FROM variables, whereas before it was (1) (3) (2). SQL92 and
1294
* SQL99 do not allow GROUPing BY an outer reference, so this
1295
* breaks no cases that are legal per spec, and it seems a more
1296
* self-consistent behavior.
1298
* Window PARTITION BY clauses should act exactly like GROUP BY.
1300
if (colNameToVar(pstate, name, true, location) != NULL)
1308
TargetEntry *tle = (TargetEntry *) lfirst(tl);
1310
if (!tle->resjunk &&
1311
strcmp(tle->resname, name) == 0)
1313
if (target_result != NULL)
1315
if (!equal(target_result->expr, tle->expr))
1317
(errcode(ERRCODE_AMBIGUOUS_COLUMN),
1320
translator: first %s is name of a SQL construct, eg ORDER BY */
1321
errmsg("%s \"%s\" is ambiguous",
1322
clauseText[clause], name),
1323
parser_errposition(pstate, location)));
1326
target_result = tle;
1327
/* Stay in loop to check for ambiguity */
1330
if (target_result != NULL)
1331
return target_result; /* return the first match */
1334
if (IsA(node, A_Const))
1336
Value *val = &((A_Const *) node)->val;
1337
int location = ((A_Const *) node)->location;
1338
int targetlist_pos = 0;
1341
if (!IsA(val, Integer))
1343
(errcode(ERRCODE_SYNTAX_ERROR),
1344
/* translator: %s is name of a SQL construct, eg ORDER BY */
1345
errmsg("non-integer constant in %s",
1346
clauseText[clause]),
1347
parser_errposition(pstate, location)));
1349
target_pos = intVal(val);
1352
TargetEntry *tle = (TargetEntry *) lfirst(tl);
1356
if (++targetlist_pos == target_pos)
1357
return tle; /* return the unique match */
1361
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1362
/* translator: %s is name of a SQL construct, eg ORDER BY */
1363
errmsg("%s position %d is not in select list",
1364
clauseText[clause], target_pos),
1365
parser_errposition(pstate, location)));
1369
* Otherwise, we have an expression (this is a Postgres extension not
1370
* found in SQL92). Convert the untransformed node to a transformed
1371
* expression, and search for a match in the tlist. NOTE: it doesn't
1372
* really matter whether there is more than one match. Also, we are
1373
* willing to match a resjunk target here, though the above cases must
1374
* ignore resjunk targets.
1376
expr = transformExpr(pstate, node);
1380
TargetEntry *tle = (TargetEntry *) lfirst(tl);
1382
if (equal(expr, tle->expr))
1387
* If no matches, construct a new target entry which is appended to the
1388
* end of the target list. This target is given resjunk = TRUE so that it
1389
* will not be projected into the final tuple.
1391
target_result = transformTargetEntry(pstate, node, expr, NULL, true);
1393
*tlist = lappend(*tlist, target_result);
1395
return target_result;
1399
* transformGroupClause -
1400
* transform a GROUP BY clause
1402
* GROUP BY items will be added to the targetlist (as resjunk columns)
1403
* if not already present, so the targetlist must be passed by reference.
1405
* This is also used for window PARTITION BY clauses (which actually act
1406
* just the same, except for the clause name used in error messages).
1409
transformGroupClause(ParseState *pstate, List *grouplist,
1410
List **targetlist, List *sortClause,
1414
int clause = isPartition ? PARTITION_CLAUSE : GROUP_CLAUSE;
1417
foreach(gl, grouplist)
1419
Node *gexpr = (Node *) lfirst(gl);
1423
tle = findTargetlistEntry(pstate, gexpr, targetlist, clause);
1425
/* Eliminate duplicates (GROUP BY x, x) */
1426
if (targetIsInSortList(tle, InvalidOid, result))
1430
* If the GROUP BY tlist entry also appears in ORDER BY, copy operator
1431
* info from the (first) matching ORDER BY item. This means that if
1432
* you write something like "GROUP BY foo ORDER BY foo USING <<<", the
1433
* GROUP BY operation silently takes on the equality semantics implied
1434
* by the ORDER BY. There are two reasons to do this: it improves
1435
* the odds that we can implement both GROUP BY and ORDER BY with a
1436
* single sort step, and it allows the user to choose the equality
1437
* semantics used by GROUP BY, should she be working with a datatype
1438
* that has more than one equality operator.
1440
if (tle->ressortgroupref > 0)
1444
foreach(sl, sortClause)
1446
SortGroupClause *sc = (SortGroupClause *) lfirst(sl);
1448
if (sc->tleSortGroupRef == tle->ressortgroupref)
1450
result = lappend(result, copyObject(sc));
1458
* If no match in ORDER BY, just add it to the result using
1459
* default sort/group semantics.
1462
result = addTargetToGroupList(pstate, tle,
1463
result, *targetlist,
1464
exprLocation(gexpr),
1472
* transformSortClause -
1473
* transform an ORDER BY clause
1475
* ORDER BY items will be added to the targetlist (as resjunk columns)
1476
* if not already present, so the targetlist must be passed by reference.
1479
transformSortClause(ParseState *pstate,
1482
bool resolveUnknown)
1484
List *sortlist = NIL;
1487
foreach(olitem, orderlist)
1489
SortBy *sortby = (SortBy *) lfirst(olitem);
1492
tle = findTargetlistEntry(pstate, sortby->node,
1493
targetlist, ORDER_CLAUSE);
1495
sortlist = addTargetToSortList(pstate, tle,
1496
sortlist, *targetlist, sortby,
1504
* transformWindowDefinitions -
1505
* transform window definitions (WindowDef to WindowClause)
1508
transformWindowDefinitions(ParseState *pstate,
1516
foreach(lc, windowdefs)
1518
WindowDef *windef = (WindowDef *) lfirst(lc);
1519
WindowClause *refwc = NULL;
1520
List *partitionClause;
1527
* Check for duplicate window names.
1530
findWindowClause(result, windef->name) != NULL)
1532
(errcode(ERRCODE_WINDOWING_ERROR),
1533
errmsg("window \"%s\" is already defined", windef->name),
1534
parser_errposition(pstate, windef->location)));
1537
* If it references a previous window, look that up.
1539
if (windef->refname)
1541
refwc = findWindowClause(result, windef->refname);
1544
(errcode(ERRCODE_UNDEFINED_OBJECT),
1545
errmsg("window \"%s\" does not exist",
1547
parser_errposition(pstate, windef->location)));
1551
* Transform PARTITION and ORDER specs, if any. These are treated
1552
* exactly like top-level GROUP BY and ORDER BY clauses, including
1553
* the special handling of nondefault operator semantics.
1555
orderClause = transformSortClause(pstate,
1556
windef->orderClause,
1559
partitionClause = transformGroupClause(pstate,
1560
windef->partitionClause,
1566
* And prepare the new WindowClause.
1568
wc = makeNode(WindowClause);
1569
wc->name = windef->name;
1570
wc->refname = windef->refname;
1573
* Per spec, a windowdef that references a previous one copies the
1574
* previous partition clause (and mustn't specify its own). It can
1575
* specify its own ordering clause. but only if the previous one
1576
* had none. It always specifies its own frame clause, and the
1577
* previous one must not have a frame clause. (Yeah, it's bizarre
1578
* that each of these cases works differently, but SQL:2008 says so;
1579
* see 7.11 <window clause> syntax rule 10 and general rule 1.)
1583
if (partitionClause)
1585
(errcode(ERRCODE_WINDOWING_ERROR),
1586
errmsg("cannot override PARTITION BY clause of window \"%s\"",
1588
parser_errposition(pstate, windef->location)));
1589
wc->partitionClause = copyObject(refwc->partitionClause);
1592
wc->partitionClause = partitionClause;
1595
if (orderClause && refwc->orderClause)
1597
(errcode(ERRCODE_WINDOWING_ERROR),
1598
errmsg("cannot override ORDER BY clause of window \"%s\"",
1600
parser_errposition(pstate, windef->location)));
1603
wc->orderClause = orderClause;
1604
wc->copiedOrder = false;
1608
wc->orderClause = copyObject(refwc->orderClause);
1609
wc->copiedOrder = true;
1614
wc->orderClause = orderClause;
1615
wc->copiedOrder = false;
1617
if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
1619
(errcode(ERRCODE_WINDOWING_ERROR),
1620
errmsg("cannot override frame clause of window \"%s\"",
1622
parser_errposition(pstate, windef->location)));
1623
wc->frameOptions = windef->frameOptions;
1624
wc->winref = winref;
1626
result = lappend(result, wc);
1633
* transformDistinctClause -
1634
* transform a DISTINCT clause
1636
* Since we may need to add items to the query's targetlist, that list
1637
* is passed by reference.
1639
* As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
1640
* possible into the distinctClause. This avoids a possible need to re-sort,
1641
* and allows the user to choose the equality semantics used by DISTINCT,
1642
* should she be working with a datatype that has more than one equality
1646
transformDistinctClause(ParseState *pstate,
1647
List **targetlist, List *sortClause)
1654
* The distinctClause should consist of all ORDER BY items followed
1655
* by all other non-resjunk targetlist items. There must not be any
1656
* resjunk ORDER BY items --- that would imply that we are sorting
1657
* by a value that isn't necessarily unique within a DISTINCT group,
1658
* so the results wouldn't be well-defined. This construction
1659
* ensures we follow the rule that sortClause and distinctClause match;
1660
* in fact the sortClause will always be a prefix of distinctClause.
1662
* Note a corner case: the same TLE could be in the ORDER BY list
1663
* multiple times with different sortops. We have to include it in
1664
* the distinctClause the same way to preserve the prefix property.
1665
* The net effect will be that the TLE value will be made unique
1666
* according to both sortops.
1668
foreach(slitem, sortClause)
1670
SortGroupClause *scl = (SortGroupClause *) lfirst(slitem);
1671
TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
1675
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1676
errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list"),
1677
parser_errposition(pstate,
1678
exprLocation((Node *) tle->expr))));
1679
result = lappend(result, copyObject(scl));
1683
* Now add any remaining non-resjunk tlist items, using default
1684
* sort/group semantics for their data types.
1686
foreach(tlitem, *targetlist)
1688
TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
1691
continue; /* ignore junk */
1692
result = addTargetToGroupList(pstate, tle,
1693
result, *targetlist,
1694
exprLocation((Node *) tle->expr),
1702
* transformDistinctOnClause -
1703
* transform a DISTINCT ON clause
1705
* Since we may need to add items to the query's targetlist, that list
1706
* is passed by reference.
1708
* As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
1709
* possible into the distinctClause. This avoids a possible need to re-sort,
1710
* and allows the user to choose the equality semantics used by DISTINCT,
1711
* should she be working with a datatype that has more than one equality
1715
transformDistinctOnClause(ParseState *pstate, List *distinctlist,
1716
List **targetlist, List *sortClause)
1719
List *sortgrouprefs = NIL;
1720
bool skipped_sortitem;
1725
* Add all the DISTINCT ON expressions to the tlist (if not already
1726
* present, they are added as resjunk items). Assign sortgroupref
1727
* numbers to them, and make a list of these numbers. (NB: we rely
1728
* below on the sortgrouprefs list being one-for-one with the original
1729
* distinctlist. Also notice that we could have duplicate DISTINCT ON
1730
* expressions and hence duplicate entries in sortgrouprefs.)
1732
foreach(lc, distinctlist)
1734
Node *dexpr = (Node *) lfirst(lc);
1738
tle = findTargetlistEntry(pstate, dexpr,
1739
targetlist, DISTINCT_ON_CLAUSE);
1740
sortgroupref = assignSortGroupRef(tle, *targetlist);
1741
sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
1745
* If the user writes both DISTINCT ON and ORDER BY, adopt the
1746
* sorting semantics from ORDER BY items that match DISTINCT ON
1747
* items, and also adopt their column sort order. We insist that
1748
* the distinctClause and sortClause match, so throw error if we
1749
* find the need to add any more distinctClause items after we've
1750
* skipped an ORDER BY item that wasn't in DISTINCT ON.
1752
skipped_sortitem = false;
1753
foreach(lc, sortClause)
1755
SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
1757
if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
1759
if (skipped_sortitem)
1761
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1762
errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
1763
parser_errposition(pstate,
1764
get_matching_location(scl->tleSortGroupRef,
1768
result = lappend(result, copyObject(scl));
1771
skipped_sortitem = true;
1775
* Now add any remaining DISTINCT ON items, using default sort/group
1776
* semantics for their data types. (Note: this is pretty questionable;
1777
* if the ORDER BY list doesn't include all the DISTINCT ON items and more
1778
* besides, you certainly aren't using DISTINCT ON in the intended way,
1779
* and you probably aren't going to get consistent results. It might be
1780
* better to throw an error or warning here. But historically we've
1781
* allowed it, so keep doing so.)
1783
forboth(lc, distinctlist, lc2, sortgrouprefs)
1785
Node *dexpr = (Node *) lfirst(lc);
1786
int sortgroupref = lfirst_int(lc2);
1787
TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
1789
if (targetIsInSortList(tle, InvalidOid, result))
1790
continue; /* already in list (with some semantics) */
1791
if (skipped_sortitem)
1793
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1794
errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions"),
1795
parser_errposition(pstate, exprLocation(dexpr))));
1796
result = addTargetToGroupList(pstate, tle,
1797
result, *targetlist,
1798
exprLocation(dexpr),
1806
* get_matching_location
1807
* Get the exprLocation of the exprs member corresponding to the
1808
* (first) member of sortgrouprefs that equals sortgroupref.
1810
* This is used so that we can point at a troublesome DISTINCT ON entry.
1811
* (Note that we need to use the original untransformed DISTINCT ON list
1812
* item, as whatever TLE it corresponds to will very possibly have a
1813
* parse location pointing to some matching entry in the SELECT list
1814
* or ORDER BY list.)
1817
get_matching_location(int sortgroupref, List *sortgrouprefs, List *exprs)
1822
forboth(lcs, sortgrouprefs, lce, exprs)
1824
if (lfirst_int(lcs) == sortgroupref)
1825
return exprLocation((Node *) lfirst(lce));
1827
/* if no match, caller blew it */
1828
elog(ERROR, "get_matching_location: no matching sortgroupref");
1829
return -1; /* keep compiler quiet */
1833
* addTargetToSortList
1834
* If the given targetlist entry isn't already in the SortGroupClause
1835
* list, add it to the end of the list, using the given sort ordering
1838
* If resolveUnknown is TRUE, convert TLEs of type UNKNOWN to TEXT. If not,
1839
* do nothing (which implies the search for a sort operator will fail).
1840
* pstate should be provided if resolveUnknown is TRUE, but can be NULL
1843
* Returns the updated SortGroupClause list.
1846
addTargetToSortList(ParseState *pstate, TargetEntry *tle,
1847
List *sortlist, List *targetlist, SortBy *sortby,
1848
bool resolveUnknown)
1850
Oid restype = exprType((Node *) tle->expr);
1855
ParseCallbackState pcbstate;
1857
/* if tlist item is an UNKNOWN literal, change it to TEXT */
1858
if (restype == UNKNOWNOID && resolveUnknown)
1860
tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
1861
restype, TEXTOID, -1,
1863
COERCE_IMPLICIT_CAST,
1869
* Rather than clutter the API of get_sort_group_operators and the other
1870
* functions we're about to use, make use of error context callback to
1871
* mark any error reports with a parse position. We point to the operator
1872
* location if present, else to the expression being sorted. (NB: use
1873
* the original untransformed expression here; the TLE entry might well
1874
* point at a duplicate expression in the regular SELECT list.)
1876
location = sortby->location;
1878
location = exprLocation(sortby->node);
1879
setup_parser_errposition_callback(&pcbstate, pstate, location);
1881
/* determine the sortop, eqop, and directionality */
1882
switch (sortby->sortby_dir)
1884
case SORTBY_DEFAULT:
1886
get_sort_group_operators(restype,
1888
&sortop, &eqop, NULL);
1892
get_sort_group_operators(restype,
1894
NULL, &eqop, &sortop);
1898
Assert(sortby->useOp != NIL);
1899
sortop = compatible_oper_opid(sortby->useOp,
1905
* Verify it's a valid ordering operator, fetch the corresponding
1906
* equality operator, and determine whether to consider it like
1909
eqop = get_equality_op_for_ordering_op(sortop, &reverse);
1910
if (!OidIsValid(eqop))
1912
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
1913
errmsg("operator %s is not a valid ordering operator",
1914
strVal(llast(sortby->useOp))),
1915
errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
1918
elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
1919
sortop = InvalidOid; /* keep compiler quiet */
1925
cancel_parser_errposition_callback(&pcbstate);
1927
/* avoid making duplicate sortlist entries */
1928
if (!targetIsInSortList(tle, sortop, sortlist))
1930
SortGroupClause *sortcl = makeNode(SortGroupClause);
1932
sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
1934
sortcl->eqop = eqop;
1935
sortcl->sortop = sortop;
1937
switch (sortby->sortby_nulls)
1939
case SORTBY_NULLS_DEFAULT:
1940
/* NULLS FIRST is default for DESC; other way for ASC */
1941
sortcl->nulls_first = reverse;
1943
case SORTBY_NULLS_FIRST:
1944
sortcl->nulls_first = true;
1946
case SORTBY_NULLS_LAST:
1947
sortcl->nulls_first = false;
1950
elog(ERROR, "unrecognized sortby_nulls: %d",
1951
sortby->sortby_nulls);
1955
sortlist = lappend(sortlist, sortcl);
1962
* addTargetToGroupList
1963
* If the given targetlist entry isn't already in the SortGroupClause
1964
* list, add it to the end of the list, using default sort/group
1967
* This is very similar to addTargetToSortList, except that we allow the
1968
* case where only a grouping (equality) operator can be found, and that
1969
* the TLE is considered "already in the list" if it appears there with any
1970
* sorting semantics.
1972
* location is the parse location to be fingered in event of trouble. Note
1973
* that we can't rely on exprLocation(tle->expr), because that might point
1974
* to a SELECT item that matches the GROUP BY item; it'd be pretty confusing
1975
* to report such a location.
1977
* If resolveUnknown is TRUE, convert TLEs of type UNKNOWN to TEXT. If not,
1978
* do nothing (which implies the search for an equality operator will fail).
1979
* pstate should be provided if resolveUnknown is TRUE, but can be NULL
1982
* Returns the updated SortGroupClause list.
1985
addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
1986
List *grouplist, List *targetlist, int location,
1987
bool resolveUnknown)
1989
Oid restype = exprType((Node *) tle->expr);
1993
/* if tlist item is an UNKNOWN literal, change it to TEXT */
1994
if (restype == UNKNOWNOID && resolveUnknown)
1996
tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
1997
restype, TEXTOID, -1,
1999
COERCE_IMPLICIT_CAST,
2004
/* avoid making duplicate grouplist entries */
2005
if (!targetIsInSortList(tle, InvalidOid, grouplist))
2007
SortGroupClause *grpcl = makeNode(SortGroupClause);
2008
ParseCallbackState pcbstate;
2010
setup_parser_errposition_callback(&pcbstate, pstate, location);
2012
/* determine the eqop and optional sortop */
2013
get_sort_group_operators(restype,
2015
&sortop, &eqop, NULL);
2017
cancel_parser_errposition_callback(&pcbstate);
2019
grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
2021
grpcl->sortop = sortop;
2022
grpcl->nulls_first = false; /* OK with or without sortop */
2024
grouplist = lappend(grouplist, grpcl);
2031
* assignSortGroupRef
2032
* Assign the targetentry an unused ressortgroupref, if it doesn't
2033
* already have one. Return the assigned or pre-existing refnumber.
2035
* 'tlist' is the targetlist containing (or to contain) the given targetentry.
2038
assignSortGroupRef(TargetEntry *tle, List *tlist)
2043
if (tle->ressortgroupref) /* already has one? */
2044
return tle->ressortgroupref;
2046
/* easiest way to pick an unused refnumber: max used + 1 */
2050
Index ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
2055
tle->ressortgroupref = maxRef + 1;
2056
return tle->ressortgroupref;
2060
* targetIsInSortList
2061
* Is the given target item already in the sortlist?
2062
* If sortop is not InvalidOid, also test for a match to the sortop.
2064
* It is not an oversight that this function ignores the nulls_first flag.
2065
* We check sortop when determining if an ORDER BY item is redundant with
2066
* earlier ORDER BY items, because it's conceivable that "ORDER BY
2067
* foo USING <, foo USING <<<" is not redundant, if <<< distinguishes
2068
* values that < considers equal. We need not check nulls_first
2069
* however, because a lower-order column with the same sortop but
2070
* opposite nulls direction is redundant. Also, we can consider
2071
* ORDER BY foo ASC, foo DESC redundant, so check for a commutator match.
2073
* Works for both ordering and grouping lists (sortop would normally be
2074
* InvalidOid when considering grouping). Note that the main reason we need
2075
* this routine (and not just a quick test for nonzeroness of ressortgroupref)
2076
* is that a TLE might be in only one of the lists.
2079
targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
2081
Index ref = tle->ressortgroupref;
2084
/* no need to scan list if tle has no marker */
2088
foreach(l, sortList)
2090
SortGroupClause *scl = (SortGroupClause *) lfirst(l);
2092
if (scl->tleSortGroupRef == ref &&
2093
(sortop == InvalidOid ||
2094
sortop == scl->sortop ||
2095
sortop == get_commutator(scl->sortop)))
2103
* Find the named WindowClause in the list, or return NULL if not there
2105
static WindowClause *
2106
findWindowClause(List *wclist, const char *name)
2112
WindowClause *wc = (WindowClause *) lfirst(l);
2114
if (wc->name && strcmp(wc->name, name) == 0)