~ubuntu-branches/ubuntu/hardy/postgresql-8.4/hardy-backports

« back to all changes in this revision

Viewing changes to src/backend/parser/parse_clause.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * parse_clause.c
 
4
 *        handle clauses in parser
 
5
 *
 
6
 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
 
7
 * Portions Copyright (c) 1994, Regents of the University of California
 
8
 *
 
9
 *
 
10
 * IDENTIFICATION
 
11
 *        $PostgreSQL$
 
12
 *
 
13
 *-------------------------------------------------------------------------
 
14
 */
 
15
 
 
16
#include "postgres.h"
 
17
 
 
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"
 
38
 
 
39
 
 
40
#define ORDER_CLAUSE 0
 
41
#define GROUP_CLAUSE 1
 
42
#define DISTINCT_ON_CLAUSE 2
 
43
#define PARTITION_CLAUSE 3
 
44
 
 
45
static const char * const clauseText[] = {
 
46
        "ORDER BY",
 
47
        "GROUP BY",
 
48
        "DISTINCT ON",
 
49
        "PARTITION BY"
 
50
};
 
51
 
 
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,
 
59
                                          RangeTblEntry *l_rte,
 
60
                                          RangeTblEntry *r_rte,
 
61
                                          List *relnamespace,
 
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,
 
67
                                                RangeSubselect *r);
 
68
static RangeTblEntry *transformRangeFunction(ParseState *pstate,
 
69
                                           RangeFunction *r);
 
70
static Node *transformFromClauseItem(ParseState *pstate, Node *n,
 
71
                                                RangeTblEntry **top_rte, int *top_rti,
 
72
                                                List **relnamespace,
 
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,
 
82
                                        bool resolveUnknown);
 
83
static List *addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
 
84
                                         List *grouplist, List *targetlist, int location,
 
85
                                         bool resolveUnknown);
 
86
static WindowClause *findWindowClause(List *wclist, const char *name);
 
87
 
 
88
 
 
89
/*
 
90
 * transformFromClause -
 
91
 *        Process the FROM clause and add items to the query's range table,
 
92
 *        joinlist, and namespaces.
 
93
 *
 
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.
 
98
 *
 
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.)
 
103
 */
 
104
void
 
105
transformFromClause(ParseState *pstate, List *frmList)
 
106
{
 
107
        ListCell   *fl;
 
108
 
 
109
        /*
 
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.
 
114
         */
 
115
        foreach(fl, frmList)
 
116
        {
 
117
                Node       *n = lfirst(fl);
 
118
                RangeTblEntry *rte;
 
119
                int                     rtindex;
 
120
                List       *relnamespace;
 
121
                Relids          containedRels;
 
122
 
 
123
                n = transformFromClauseItem(pstate, n,
 
124
                                                                        &rte,
 
125
                                                                        &rtindex,
 
126
                                                                        &relnamespace,
 
127
                                                                        &containedRels);
 
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,
 
131
                                                                                         relnamespace);
 
132
                pstate->p_varnamespace = lappend(pstate->p_varnamespace, rte);
 
133
                bms_free(containedRels);
 
134
        }
 
135
}
 
136
 
 
137
/*
 
138
 * setTargetTable
 
139
 *        Add the target relation of INSERT/UPDATE/DELETE to the range table,
 
140
 *        and make the special links to it in the ParseState.
 
141
 *
 
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.
 
146
 *
 
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.)
 
153
 *
 
154
 *        Finally, we mark the relation as requiring the permissions specified
 
155
 *        by requiredPerms.
 
156
 *
 
157
 *        Returns the rangetable index of the target relation.
 
158
 */
 
159
int
 
160
setTargetTable(ParseState *pstate, RangeVar *relation,
 
161
                           bool inh, bool alsoSource, AclMode requiredPerms)
 
162
{
 
163
        RangeTblEntry *rte;
 
164
        int                     rtindex;
 
165
 
 
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);
 
169
 
 
170
        /*
 
171
         * Open target rel and grab suitable lock (which we will hold till end of
 
172
         * transaction).
 
173
         *
 
174
         * free_parsestate() will eventually do the corresponding heap_close(),
 
175
         * but *not* release the lock.
 
176
         */
 
177
        pstate->p_target_relation = parserOpenTable(pstate, relation,
 
178
                                                                                                RowExclusiveLock);
 
179
 
 
180
        /*
 
181
         * Now build an RTE.
 
182
         */
 
183
        rte = addRangeTableEntryForRelation(pstate, pstate->p_target_relation,
 
184
                                                                                relation->alias, inh, false);
 
185
        pstate->p_target_rangetblentry = rte;
 
186
 
 
187
        /* assume new rte is at end */
 
188
        rtindex = list_length(pstate->p_rtable);
 
189
        Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
 
190
 
 
191
        /*
 
192
         * Override addRangeTableEntry's default ACL_SELECT permissions check, and
 
193
         * instead mark target table as requiring exactly the specified
 
194
         * permissions.
 
195
         *
 
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.
 
199
         */
 
200
        rte->requiredPerms = requiredPerms;
 
201
 
 
202
        /*
 
203
         * If UPDATE/DELETE, add table to joinlist and namespaces.
 
204
         */
 
205
        if (alsoSource)
 
206
                addRTEtoQuery(pstate, rte, true, true, true);
 
207
 
 
208
        return rtindex;
 
209
}
 
210
 
 
211
/*
 
212
 * Simplify InhOption (yes/no/default) into boolean yes/no.
 
213
 *
 
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
 
217
 * with queries.
 
218
 */
 
219
bool
 
220
interpretInhOption(InhOption inhOpt)
 
221
{
 
222
        switch (inhOpt)
 
223
        {
 
224
                case INH_NO:
 
225
                        return false;
 
226
                case INH_YES:
 
227
                        return true;
 
228
                case INH_DEFAULT:
 
229
                        return SQL_inheritance;
 
230
        }
 
231
        elog(ERROR, "bogus InhOption value: %d", inhOpt);
 
232
        return false;                           /* keep compiler quiet */
 
233
}
 
234
 
 
235
/*
 
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.
 
240
 */
 
241
bool
 
242
interpretOidsOption(List *defList)
 
243
{
 
244
        ListCell   *cell;
 
245
 
 
246
        /* Scan list to see if OIDS was included */
 
247
        foreach(cell, defList)
 
248
        {
 
249
                ReloptElem    *def = (ReloptElem *) lfirst(cell);
 
250
 
 
251
                if (pg_strcasecmp(def->optname, "oids") == 0)
 
252
                        return reloptGetBoolean(def);
 
253
        }
 
254
 
 
255
        /* OIDS option was not specified, so use default. */
 
256
        return default_with_oids;
 
257
}
 
258
 
 
259
/*
 
260
 * Extract all not-in-common columns from column lists of a source table
 
261
 */
 
262
static void
 
263
extractRemainingColumns(List *common_colnames,
 
264
                                                List *src_colnames, List *src_colvars,
 
265
                                                List **res_colnames, List **res_colvars)
 
266
{
 
267
        List       *new_colnames = NIL;
 
268
        List       *new_colvars = NIL;
 
269
        ListCell   *lnames,
 
270
                           *lvars;
 
271
 
 
272
        Assert(list_length(src_colnames) == list_length(src_colvars));
 
273
 
 
274
        forboth(lnames, src_colnames, lvars, src_colvars)
 
275
        {
 
276
                char       *colname = strVal(lfirst(lnames));
 
277
                bool            match = false;
 
278
                ListCell   *cnames;
 
279
 
 
280
                foreach(cnames, common_colnames)
 
281
                {
 
282
                        char       *ccolname = strVal(lfirst(cnames));
 
283
 
 
284
                        if (strcmp(colname, ccolname) == 0)
 
285
                        {
 
286
                                match = true;
 
287
                                break;
 
288
                        }
 
289
                }
 
290
 
 
291
                if (!match)
 
292
                {
 
293
                        new_colnames = lappend(new_colnames, lfirst(lnames));
 
294
                        new_colvars = lappend(new_colvars, lfirst(lvars));
 
295
                }
 
296
        }
 
297
 
 
298
        *res_colnames = new_colnames;
 
299
        *res_colvars = new_colvars;
 
300
}
 
301
 
 
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.
 
306
 */
 
307
static Node *
 
308
transformJoinUsingClause(ParseState *pstate,
 
309
                                                 RangeTblEntry *leftRTE, RangeTblEntry *rightRTE,
 
310
                                                 List *leftVars, List *rightVars)
 
311
{
 
312
        Node       *result = NULL;
 
313
        ListCell   *lvars,
 
314
                           *rvars;
 
315
 
 
316
        /*
 
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.
 
322
         */
 
323
        forboth(lvars, leftVars, rvars, rightVars)
 
324
        {
 
325
                Var                *lvar = (Var *) lfirst(lvars);
 
326
                Var                *rvar = (Var *) lfirst(rvars);
 
327
                A_Expr     *e;
 
328
 
 
329
                /* Require read access to the join variables */
 
330
                markVarForSelectPriv(pstate, lvar, leftRTE);
 
331
                markVarForSelectPriv(pstate, rvar, rightRTE);
 
332
 
 
333
                /* Now create the lvar = rvar join condition */
 
334
                e = makeSimpleA_Expr(AEXPR_OP, "=",
 
335
                                                         copyObject(lvar), copyObject(rvar),
 
336
                                                         -1);
 
337
 
 
338
                /* And combine into an AND clause, if multiple join columns */
 
339
                if (result == NULL)
 
340
                        result = (Node *) e;
 
341
                else
 
342
                {
 
343
                        A_Expr     *a;
 
344
 
 
345
                        a = makeA_Expr(AEXPR_AND, NIL, result, (Node *) e, -1);
 
346
                        result = (Node *) a;
 
347
                }
 
348
        }
 
349
 
 
350
        /*
 
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.
 
355
         */
 
356
        result = transformExpr(pstate, result);
 
357
 
 
358
        result = coerce_to_boolean(pstate, result, "JOIN/USING");
 
359
 
 
360
        return result;
 
361
}
 
362
 
 
363
/* transformJoinOnClause()
 
364
 *        Transform the qual conditions for JOIN/ON.
 
365
 *        Result is a transformed qualification expression.
 
366
 */
 
367
static Node *
 
368
transformJoinOnClause(ParseState *pstate, JoinExpr *j,
 
369
                                          RangeTblEntry *l_rte,
 
370
                                          RangeTblEntry *r_rte,
 
371
                                          List *relnamespace,
 
372
                                          Relids containedRels)
 
373
{
 
374
        Node       *result;
 
375
        List       *save_relnamespace;
 
376
        List       *save_varnamespace;
 
377
        Relids          clause_varnos;
 
378
        int                     varno;
 
379
 
 
380
        /*
 
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.
 
388
         */
 
389
        save_relnamespace = pstate->p_relnamespace;
 
390
        save_varnamespace = pstate->p_varnamespace;
 
391
 
 
392
        pstate->p_relnamespace = relnamespace;
 
393
        pstate->p_varnamespace = list_make2(l_rte, r_rte);
 
394
 
 
395
        result = transformWhereClause(pstate, j->quals, "JOIN/ON");
 
396
 
 
397
        pstate->p_relnamespace = save_relnamespace;
 
398
        pstate->p_varnamespace = save_varnamespace;
 
399
 
 
400
        /*
 
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.)
 
406
         */
 
407
        clause_varnos = pull_varnos(result);
 
408
        clause_varnos = bms_del_members(clause_varnos, containedRels);
 
409
        if ((varno = bms_first_member(clause_varnos)) >= 0)
 
410
        {
 
411
                ereport(ERROR,
 
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))));
 
417
        }
 
418
        bms_free(clause_varnos);
 
419
 
 
420
        return result;
 
421
}
 
422
 
 
423
/*
 
424
 * transformTableEntry --- transform a RangeVar (simple relation reference)
 
425
 */
 
426
static RangeTblEntry *
 
427
transformTableEntry(ParseState *pstate, RangeVar *r)
 
428
{
 
429
        RangeTblEntry *rte;
 
430
 
 
431
        /*
 
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.
 
437
         */
 
438
        rte = addRangeTableEntry(pstate, r, r->alias,
 
439
                                                         interpretInhOption(r->inhOpt), true);
 
440
 
 
441
        return rte;
 
442
}
 
443
 
 
444
/*
 
445
 * transformCTEReference --- transform a RangeVar that references a common
 
446
 * table expression (ie, a sub-SELECT defined in a WITH clause)
 
447
 */
 
448
static RangeTblEntry *
 
449
transformCTEReference(ParseState *pstate, RangeVar *r,
 
450
                                          CommonTableExpr *cte, Index levelsup)
 
451
{
 
452
        RangeTblEntry *rte;
 
453
 
 
454
        rte = addRangeTableEntryForCTE(pstate, cte, levelsup, r->alias, true);
 
455
 
 
456
        return rte;
 
457
}
 
458
 
 
459
/*
 
460
 * transformRangeSubselect --- transform a sub-SELECT appearing in FROM
 
461
 */
 
462
static RangeTblEntry *
 
463
transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
 
464
{
 
465
        Query      *query;
 
466
        RangeTblEntry *rte;
 
467
 
 
468
        /*
 
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.)
 
473
         */
 
474
        if (r->alias == NULL)
 
475
                elog(ERROR, "subquery in FROM must have an alias");
 
476
 
 
477
        /*
 
478
         * Analyze and transform the subquery.
 
479
         */
 
480
        query = parse_sub_analyze(r->subquery, pstate);
 
481
 
 
482
        /*
 
483
         * Check that we got something reasonable.      Many of these conditions are
 
484
         * impossible given restrictions of the grammar, but check 'em anyway.
 
485
         */
 
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)
 
491
                ereport(ERROR,
 
492
                                (errcode(ERRCODE_SYNTAX_ERROR),
 
493
                                 errmsg("subquery in FROM cannot have SELECT INTO"),
 
494
                                 parser_errposition(pstate,
 
495
                                                                        exprLocation((Node *) query->intoClause))));
 
496
 
 
497
        /*
 
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.
 
503
         *
 
504
         * XXX this will need further work to support SQL99's LATERAL() feature,
 
505
         * wherein such references would indeed be legal.
 
506
         *
 
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.
 
509
         */
 
510
        if (pstate->p_relnamespace || pstate->p_varnamespace)
 
511
        {
 
512
                if (contain_vars_of_level((Node *) query, 1))
 
513
                        ereport(ERROR,
 
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))));
 
518
        }
 
519
 
 
520
        /*
 
521
         * OK, build an RTE for the subquery.
 
522
         */
 
523
        rte = addRangeTableEntryForSubquery(pstate, query, r->alias, true);
 
524
 
 
525
        return rte;
 
526
}
 
527
 
 
528
 
 
529
/*
 
530
 * transformRangeFunction --- transform a function call appearing in FROM
 
531
 */
 
532
static RangeTblEntry *
 
533
transformRangeFunction(ParseState *pstate, RangeFunction *r)
 
534
{
 
535
        Node       *funcexpr;
 
536
        char       *funcname;
 
537
        RangeTblEntry *rte;
 
538
 
 
539
        /*
 
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.
 
544
         */
 
545
        funcname = FigureColname(r->funccallnode);
 
546
 
 
547
        /*
 
548
         * Transform the raw expression.
 
549
         */
 
550
        funcexpr = transformExpr(pstate, r->funccallnode);
 
551
 
 
552
        /*
 
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.)
 
557
         *
 
558
         * XXX this will need further work to support SQL99's LATERAL() feature,
 
559
         * wherein such references would indeed be legal.
 
560
         */
 
561
        if (pstate->p_relnamespace || pstate->p_varnamespace)
 
562
        {
 
563
                if (contain_vars_of_level(funcexpr, 0))
 
564
                        ereport(ERROR,
 
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))));
 
569
        }
 
570
 
 
571
        /*
 
572
         * Disallow aggregate functions in the expression.      (No reason to postpone
 
573
         * this check until parseCheckAggregates.)
 
574
         */
 
575
        if (pstate->p_hasAggs &&
 
576
                checkExprHasAggs(funcexpr))
 
577
                ereport(ERROR,
 
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))
 
584
                ereport(ERROR,
 
585
                                (errcode(ERRCODE_WINDOWING_ERROR),
 
586
                                 errmsg("cannot use window function in function expression in FROM"),
 
587
                                 parser_errposition(pstate,
 
588
                                                                        locate_windowfunc(funcexpr))));
 
589
 
 
590
        /*
 
591
         * OK, build an RTE for the function.
 
592
         */
 
593
        rte = addRangeTableEntryForFunction(pstate, funcname, funcexpr,
 
594
                                                                                r, true);
 
595
 
 
596
        /*
 
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.
 
601
         */
 
602
        if (r->coldeflist)
 
603
        {
 
604
                TupleDesc       tupdesc;
 
605
 
 
606
                tupdesc = BuildDescFromLists(rte->eref->colnames,
 
607
                                                                         rte->funccoltypes,
 
608
                                                                         rte->funccoltypmods);
 
609
                CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE);
 
610
        }
 
611
 
 
612
        return rte;
 
613
}
 
614
 
 
615
 
 
616
/*
 
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.
 
622
 *
 
623
 * The function return value is the node to add to the jointree (a
 
624
 * RangeTblRef or JoinExpr).  Additional output parameters are:
 
625
 *
 
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.)
 
629
 *
 
630
 * *top_rti: receives the rangetable index of top_rte.  (Ditto.)
 
631
 *
 
632
 * *relnamespace: receives a List of the RTEs exposed as relation names
 
633
 * by this item.
 
634
 *
 
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.
 
638
 *
 
639
 * We do not need to pass back an explicit varnamespace value, because
 
640
 * in all cases the varnamespace contribution is exactly top_rte.
 
641
 */
 
642
static Node *
 
643
transformFromClauseItem(ParseState *pstate, Node *n,
 
644
                                                RangeTblEntry **top_rte, int *top_rti,
 
645
                                                List **relnamespace,
 
646
                                                Relids *containedRels)
 
647
{
 
648
        if (IsA(n, RangeVar))
 
649
        {
 
650
                /* Plain relation reference, or perhaps a CTE reference */
 
651
                RangeVar *rv = (RangeVar *) n;
 
652
                RangeTblRef *rtr;
 
653
                RangeTblEntry *rte = NULL;
 
654
                int                     rtindex;
 
655
 
 
656
                /* if it is an unqualified name, it might be a CTE reference */
 
657
                if (!rv->schemaname)
 
658
                {
 
659
                        CommonTableExpr *cte;
 
660
                        Index   levelsup;
 
661
 
 
662
                        cte = scanNameSpaceForCTE(pstate, rv->relname, &levelsup);
 
663
                        if (cte)
 
664
                                rte = transformCTEReference(pstate, rv, cte, levelsup);
 
665
                }
 
666
 
 
667
                /* if not found as a CTE, must be a table reference */
 
668
                if (!rte)
 
669
                        rte = transformTableEntry(pstate, rv);
 
670
 
 
671
                /* assume new rte is at end */
 
672
                rtindex = list_length(pstate->p_rtable);
 
673
                Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
 
674
                *top_rte = rte;
 
675
                *top_rti = rtindex;
 
676
                *relnamespace = list_make1(rte);
 
677
                *containedRels = bms_make_singleton(rtindex);
 
678
                rtr = makeNode(RangeTblRef);
 
679
                rtr->rtindex = rtindex;
 
680
                return (Node *) rtr;
 
681
        }
 
682
        else if (IsA(n, RangeSubselect))
 
683
        {
 
684
                /* sub-SELECT is like a plain relation */
 
685
                RangeTblRef *rtr;
 
686
                RangeTblEntry *rte;
 
687
                int                     rtindex;
 
688
 
 
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));
 
693
                *top_rte = rte;
 
694
                *top_rti = rtindex;
 
695
                *relnamespace = list_make1(rte);
 
696
                *containedRels = bms_make_singleton(rtindex);
 
697
                rtr = makeNode(RangeTblRef);
 
698
                rtr->rtindex = rtindex;
 
699
                return (Node *) rtr;
 
700
        }
 
701
        else if (IsA(n, RangeFunction))
 
702
        {
 
703
                /* function is like a plain relation */
 
704
                RangeTblRef *rtr;
 
705
                RangeTblEntry *rte;
 
706
                int                     rtindex;
 
707
 
 
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));
 
712
                *top_rte = rte;
 
713
                *top_rti = rtindex;
 
714
                *relnamespace = list_make1(rte);
 
715
                *containedRels = bms_make_singleton(rtindex);
 
716
                rtr = makeNode(RangeTblRef);
 
717
                rtr->rtindex = rtindex;
 
718
                return (Node *) rtr;
 
719
        }
 
720
        else if (IsA(n, JoinExpr))
 
721
        {
 
722
                /* A newfangled join expression */
 
723
                JoinExpr   *j = (JoinExpr *) n;
 
724
                RangeTblEntry *l_rte;
 
725
                RangeTblEntry *r_rte;
 
726
                int                     l_rtindex;
 
727
                int                     r_rtindex;
 
728
                Relids          l_containedRels,
 
729
                                        r_containedRels,
 
730
                                        my_containedRels;
 
731
                List       *l_relnamespace,
 
732
                                   *r_relnamespace,
 
733
                                   *my_relnamespace,
 
734
                                   *l_colnames,
 
735
                                   *r_colnames,
 
736
                                   *res_colnames,
 
737
                                   *l_colvars,
 
738
                                   *r_colvars,
 
739
                                   *res_colvars;
 
740
                RangeTblEntry *rte;
 
741
                int                     k;
 
742
 
 
743
                /*
 
744
                 * Recursively process the left and right subtrees
 
745
                 */
 
746
                j->larg = transformFromClauseItem(pstate, j->larg,
 
747
                                                                                  &l_rte,
 
748
                                                                                  &l_rtindex,
 
749
                                                                                  &l_relnamespace,
 
750
                                                                                  &l_containedRels);
 
751
                j->rarg = transformFromClauseItem(pstate, j->rarg,
 
752
                                                                                  &r_rte,
 
753
                                                                                  &r_rtindex,
 
754
                                                                                  &r_relnamespace,
 
755
                                                                                  &r_containedRels);
 
756
 
 
757
                /*
 
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.
 
761
                 */
 
762
                checkNameSpaceConflicts(pstate, l_relnamespace, r_relnamespace);
 
763
 
 
764
                /*
 
765
                 * Generate combined relation membership info for possible use by
 
766
                 * transformJoinOnClause below.
 
767
                 */
 
768
                my_relnamespace = list_concat(l_relnamespace, r_relnamespace);
 
769
                my_containedRels = bms_join(l_containedRels, r_containedRels);
 
770
 
 
771
                pfree(r_relnamespace);  /* free unneeded list header */
 
772
 
 
773
                /*
 
774
                 * Extract column name and var lists from both subtrees
 
775
                 *
 
776
                 * Note: expandRTE returns new lists, safe for me to modify
 
777
                 */
 
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);
 
782
 
 
783
                /*
 
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
 
790
                 * USING list.
 
791
                 */
 
792
                if (j->isNatural)
 
793
                {
 
794
                        List       *rlist = NIL;
 
795
                        ListCell   *lx,
 
796
                                           *rx;
 
797
 
 
798
                        Assert(j->using == NIL);        /* shouldn't have USING() too */
 
799
 
 
800
                        foreach(lx, l_colnames)
 
801
                        {
 
802
                                char       *l_colname = strVal(lfirst(lx));
 
803
                                Value      *m_name = NULL;
 
804
 
 
805
                                foreach(rx, r_colnames)
 
806
                                {
 
807
                                        char       *r_colname = strVal(lfirst(rx));
 
808
 
 
809
                                        if (strcmp(l_colname, r_colname) == 0)
 
810
                                        {
 
811
                                                m_name = makeString(l_colname);
 
812
                                                break;
 
813
                                        }
 
814
                                }
 
815
 
 
816
                                /* matched a right column? then keep as join column... */
 
817
                                if (m_name != NULL)
 
818
                                        rlist = lappend(rlist, m_name);
 
819
                        }
 
820
 
 
821
                        j->using = rlist;
 
822
                }
 
823
 
 
824
                /*
 
825
                 * Now transform the join qualifications, if any.
 
826
                 */
 
827
                res_colnames = NIL;
 
828
                res_colvars = NIL;
 
829
 
 
830
                if (j->using)
 
831
                {
 
832
                        /*
 
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.
 
836
                         */
 
837
                        List       *ucols = j->using;
 
838
                        List       *l_usingvars = NIL;
 
839
                        List       *r_usingvars = NIL;
 
840
                        ListCell   *ucol;
 
841
 
 
842
                        Assert(j->quals == NULL);       /* shouldn't have ON() too */
 
843
 
 
844
                        foreach(ucol, ucols)
 
845
                        {
 
846
                                char       *u_colname = strVal(lfirst(ucol));
 
847
                                ListCell   *col;
 
848
                                int                     ndx;
 
849
                                int                     l_index = -1;
 
850
                                int                     r_index = -1;
 
851
                                Var                *l_colvar,
 
852
                                                   *r_colvar;
 
853
 
 
854
                                /* Check for USING(foo,foo) */
 
855
                                foreach(col, res_colnames)
 
856
                                {
 
857
                                        char       *res_colname = strVal(lfirst(col));
 
858
 
 
859
                                        if (strcmp(res_colname, u_colname) == 0)
 
860
                                                ereport(ERROR,
 
861
                                                                (errcode(ERRCODE_DUPLICATE_COLUMN),
 
862
                                                                 errmsg("column name \"%s\" appears more than once in USING clause",
 
863
                                                                                u_colname)));
 
864
                                }
 
865
 
 
866
                                /* Find it in left input */
 
867
                                ndx = 0;
 
868
                                foreach(col, l_colnames)
 
869
                                {
 
870
                                        char       *l_colname = strVal(lfirst(col));
 
871
 
 
872
                                        if (strcmp(l_colname, u_colname) == 0)
 
873
                                        {
 
874
                                                if (l_index >= 0)
 
875
                                                        ereport(ERROR,
 
876
                                                                        (errcode(ERRCODE_AMBIGUOUS_COLUMN),
 
877
                                                                         errmsg("common column name \"%s\" appears more than once in left table",
 
878
                                                                                        u_colname)));
 
879
                                                l_index = ndx;
 
880
                                        }
 
881
                                        ndx++;
 
882
                                }
 
883
                                if (l_index < 0)
 
884
                                        ereport(ERROR,
 
885
                                                        (errcode(ERRCODE_UNDEFINED_COLUMN),
 
886
                                                         errmsg("column \"%s\" specified in USING clause does not exist in left table",
 
887
                                                                        u_colname)));
 
888
 
 
889
                                /* Find it in right input */
 
890
                                ndx = 0;
 
891
                                foreach(col, r_colnames)
 
892
                                {
 
893
                                        char       *r_colname = strVal(lfirst(col));
 
894
 
 
895
                                        if (strcmp(r_colname, u_colname) == 0)
 
896
                                        {
 
897
                                                if (r_index >= 0)
 
898
                                                        ereport(ERROR,
 
899
                                                                        (errcode(ERRCODE_AMBIGUOUS_COLUMN),
 
900
                                                                         errmsg("common column name \"%s\" appears more than once in right table",
 
901
                                                                                        u_colname)));
 
902
                                                r_index = ndx;
 
903
                                        }
 
904
                                        ndx++;
 
905
                                }
 
906
                                if (r_index < 0)
 
907
                                        ereport(ERROR,
 
908
                                                        (errcode(ERRCODE_UNDEFINED_COLUMN),
 
909
                                                         errmsg("column \"%s\" specified in USING clause does not exist in right table",
 
910
                                                                        u_colname)));
 
911
 
 
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);
 
916
 
 
917
                                res_colnames = lappend(res_colnames, lfirst(ucol));
 
918
                                res_colvars = lappend(res_colvars,
 
919
                                                                          buildMergedJoinVar(pstate,
 
920
                                                                                                                 j->jointype,
 
921
                                                                                                                 l_colvar,
 
922
                                                                                                                 r_colvar));
 
923
                        }
 
924
 
 
925
                        j->quals = transformJoinUsingClause(pstate,
 
926
                                                                                                l_rte,
 
927
                                                                                                r_rte,
 
928
                                                                                                l_usingvars,
 
929
                                                                                                r_usingvars);
 
930
                }
 
931
                else if (j->quals)
 
932
                {
 
933
                        /* User-written ON-condition; transform it */
 
934
                        j->quals = transformJoinOnClause(pstate, j,
 
935
                                                                                         l_rte, r_rte,
 
936
                                                                                         my_relnamespace,
 
937
                                                                                         my_containedRels);
 
938
                }
 
939
                else
 
940
                {
 
941
                        /* CROSS JOIN: no quals */
 
942
                }
 
943
 
 
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);
 
955
 
 
956
                /*
 
957
                 * Check alias (AS clause), if any.
 
958
                 */
 
959
                if (j->alias)
 
960
                {
 
961
                        if (j->alias->colnames != NIL)
 
962
                        {
 
963
                                if (list_length(j->alias->colnames) > list_length(res_colnames))
 
964
                                        ereport(ERROR,
 
965
                                                        (errcode(ERRCODE_SYNTAX_ERROR),
 
966
                                                         errmsg("column alias list for \"%s\" has too many entries",
 
967
                                                                        j->alias->aliasname)));
 
968
                        }
 
969
                }
 
970
 
 
971
                /*
 
972
                 * Now build an RTE for the result of the join
 
973
                 */
 
974
                rte = addRangeTableEntryForJoin(pstate,
 
975
                                                                                res_colnames,
 
976
                                                                                j->jointype,
 
977
                                                                                res_colvars,
 
978
                                                                                j->alias,
 
979
                                                                                true);
 
980
 
 
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));
 
984
 
 
985
                *top_rte = rte;
 
986
                *top_rti = j->rtindex;
 
987
 
 
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);
 
993
 
 
994
                /*
 
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
 
998
                 * relnamespace.
 
999
                 */
 
1000
                if (j->alias)
 
1001
                {
 
1002
                        *relnamespace = list_make1(rte);
 
1003
                        list_free(my_relnamespace);
 
1004
                }
 
1005
                else
 
1006
                        *relnamespace = my_relnamespace;
 
1007
 
 
1008
                /*
 
1009
                 * Include join RTE in returned containedRels set
 
1010
                 */
 
1011
                *containedRels = bms_add_member(my_containedRels, j->rtindex);
 
1012
 
 
1013
                return (Node *) j;
 
1014
        }
 
1015
        else
 
1016
                elog(ERROR, "unrecognized node type: %d", (int) nodeTag(n));
 
1017
        return NULL;                            /* can't get here, keep compiler quiet */
 
1018
}
 
1019
 
 
1020
/*
 
1021
 * buildMergedJoinVar -
 
1022
 *        generate a suitable replacement expression for a merged join column
 
1023
 */
 
1024
static Node *
 
1025
buildMergedJoinVar(ParseState *pstate, JoinType jointype,
 
1026
                                   Var *l_colvar, Var *r_colvar)
 
1027
{
 
1028
        Oid                     outcoltype;
 
1029
        int32           outcoltypmod;
 
1030
        Node       *l_node,
 
1031
                           *r_node,
 
1032
                           *res_node;
 
1033
 
 
1034
        /*
 
1035
         * Choose output type if input types are dissimilar.
 
1036
         */
 
1037
        outcoltype = l_colvar->vartype;
 
1038
        outcoltypmod = l_colvar->vartypmod;
 
1039
        if (outcoltype != r_colvar->vartype)
 
1040
        {
 
1041
                outcoltype = select_common_type(pstate,
 
1042
                                                                                list_make2(l_colvar, r_colvar),
 
1043
                                                                                "JOIN/USING",
 
1044
                                                                                NULL);
 
1045
                outcoltypmod = -1;              /* ie, unknown */
 
1046
        }
 
1047
        else if (outcoltypmod != r_colvar->vartypmod)
 
1048
        {
 
1049
                /* same type, but not same typmod */
 
1050
                outcoltypmod = -1;              /* ie, unknown */
 
1051
        }
 
1052
 
 
1053
        /*
 
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.
 
1058
         */
 
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);
 
1067
        else
 
1068
                l_node = (Node *) l_colvar;
 
1069
 
 
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);
 
1078
        else
 
1079
                r_node = (Node *) r_colvar;
 
1080
 
 
1081
        /*
 
1082
         * Choose what to emit
 
1083
         */
 
1084
        switch (jointype)
 
1085
        {
 
1086
                case JOIN_INNER:
 
1087
 
 
1088
                        /*
 
1089
                         * We can use either var; prefer non-coerced one if available.
 
1090
                         */
 
1091
                        if (IsA(l_node, Var))
 
1092
                                res_node = l_node;
 
1093
                        else if (IsA(r_node, Var))
 
1094
                                res_node = r_node;
 
1095
                        else
 
1096
                                res_node = l_node;
 
1097
                        break;
 
1098
                case JOIN_LEFT:
 
1099
                        /* Always use left var */
 
1100
                        res_node = l_node;
 
1101
                        break;
 
1102
                case JOIN_RIGHT:
 
1103
                        /* Always use right var */
 
1104
                        res_node = r_node;
 
1105
                        break;
 
1106
                case JOIN_FULL:
 
1107
                        {
 
1108
                                /*
 
1109
                                 * Here we must build a COALESCE expression to ensure that the
 
1110
                                 * join output is non-null if either input is.
 
1111
                                 */
 
1112
                                CoalesceExpr *c = makeNode(CoalesceExpr);
 
1113
 
 
1114
                                c->coalescetype = outcoltype;
 
1115
                                c->args = list_make2(l_node, r_node);
 
1116
                                c->location = -1;
 
1117
                                res_node = (Node *) c;
 
1118
                                break;
 
1119
                        }
 
1120
                default:
 
1121
                        elog(ERROR, "unrecognized join type: %d", (int) jointype);
 
1122
                        res_node = NULL;        /* keep compiler quiet */
 
1123
                        break;
 
1124
        }
 
1125
 
 
1126
        return res_node;
 
1127
}
 
1128
 
 
1129
 
 
1130
/*
 
1131
 * transformWhereClause -
 
1132
 *        Transform the qualification and make sure it is of type boolean.
 
1133
 *        Used for WHERE and allied clauses.
 
1134
 *
 
1135
 * constructName does not affect the semantics, but is used in error messages
 
1136
 */
 
1137
Node *
 
1138
transformWhereClause(ParseState *pstate, Node *clause,
 
1139
                                         const char *constructName)
 
1140
{
 
1141
        Node       *qual;
 
1142
 
 
1143
        if (clause == NULL)
 
1144
                return NULL;
 
1145
 
 
1146
        qual = transformExpr(pstate, clause);
 
1147
 
 
1148
        qual = coerce_to_boolean(pstate, qual, constructName);
 
1149
 
 
1150
        return qual;
 
1151
}
 
1152
 
 
1153
 
 
1154
/*
 
1155
 * transformLimitClause -
 
1156
 *        Transform the expression and make sure it is of type bigint.
 
1157
 *        Used for LIMIT and allied clauses.
 
1158
 *
 
1159
 * Note: as of Postgres 8.2, LIMIT expressions are expected to yield int8,
 
1160
 * rather than int4 as before.
 
1161
 *
 
1162
 * constructName does not affect the semantics, but is used in error messages
 
1163
 */
 
1164
Node *
 
1165
transformLimitClause(ParseState *pstate, Node *clause,
 
1166
                                         const char *constructName)
 
1167
{
 
1168
        Node       *qual;
 
1169
 
 
1170
        if (clause == NULL)
 
1171
                return NULL;
 
1172
 
 
1173
        qual = transformExpr(pstate, clause);
 
1174
 
 
1175
        qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
 
1176
 
 
1177
        /*
 
1178
         * LIMIT can't refer to any vars or aggregates of the current query
 
1179
         */
 
1180
        if (contain_vars_of_level(qual, 0))
 
1181
        {
 
1182
                ereport(ERROR,
 
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",
 
1186
                                                constructName),
 
1187
                                 parser_errposition(pstate,
 
1188
                                                                        locate_var_of_level(qual, 0))));
 
1189
        }
 
1190
        if (pstate->p_hasAggs &&
 
1191
                checkExprHasAggs(qual))
 
1192
        {
 
1193
                ereport(ERROR,
 
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",
 
1197
                                                constructName),
 
1198
                                 parser_errposition(pstate,
 
1199
                                                                        locate_agg_of_level(qual, 0))));
 
1200
        }
 
1201
        if (pstate->p_hasWindowFuncs &&
 
1202
                checkExprHasWindowFuncs(qual))
 
1203
        {
 
1204
                ereport(ERROR,
 
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",
 
1208
                                                constructName),
 
1209
                                 parser_errposition(pstate,
 
1210
                                                                        locate_windowfunc(qual))));
 
1211
        }
 
1212
 
 
1213
        return qual;
 
1214
}
 
1215
 
 
1216
 
 
1217
/*
 
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.
 
1222
 *
 
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
 
1226
 */
 
1227
static TargetEntry *
 
1228
findTargetlistEntry(ParseState *pstate, Node *node, List **tlist, int clause)
 
1229
{
 
1230
        TargetEntry *target_result = NULL;
 
1231
        ListCell   *tl;
 
1232
        Node       *expr;
 
1233
 
 
1234
        /*----------
 
1235
         * Handle two special cases as mandated by the SQL92 spec:
 
1236
         *
 
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
 
1244
         *        as an expression.
 
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.
 
1256
         *
 
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.
 
1265
         *
 
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.
 
1268
         *
 
1269
         * If neither special case applies, fall through to treat the item as
 
1270
         * an expression.
 
1271
         *----------
 
1272
         */
 
1273
        if (IsA(node, ColumnRef) &&
 
1274
                list_length(((ColumnRef *) node)->fields) == 1 &&
 
1275
                IsA(linitial(((ColumnRef *) node)->fields), String))
 
1276
        {
 
1277
                char       *name = strVal(linitial(((ColumnRef *) node)->fields));
 
1278
                int                     location = ((ColumnRef *) node)->location;
 
1279
 
 
1280
                if (clause == GROUP_CLAUSE || clause == PARTITION_CLAUSE)
 
1281
                {
 
1282
                        /*
 
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.
 
1289
                         *
 
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.
 
1297
                         *
 
1298
                         * Window PARTITION BY clauses should act exactly like GROUP BY.
 
1299
                         */
 
1300
                        if (colNameToVar(pstate, name, true, location) != NULL)
 
1301
                                name = NULL;
 
1302
                }
 
1303
 
 
1304
                if (name != NULL)
 
1305
                {
 
1306
                        foreach(tl, *tlist)
 
1307
                        {
 
1308
                                TargetEntry *tle = (TargetEntry *) lfirst(tl);
 
1309
 
 
1310
                                if (!tle->resjunk &&
 
1311
                                        strcmp(tle->resname, name) == 0)
 
1312
                                {
 
1313
                                        if (target_result != NULL)
 
1314
                                        {
 
1315
                                                if (!equal(target_result->expr, tle->expr))
 
1316
                                                        ereport(ERROR,
 
1317
                                                                        (errcode(ERRCODE_AMBIGUOUS_COLUMN),
 
1318
 
 
1319
                                                        /*------
 
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)));
 
1324
                                        }
 
1325
                                        else
 
1326
                                                target_result = tle;
 
1327
                                        /* Stay in loop to check for ambiguity */
 
1328
                                }
 
1329
                        }
 
1330
                        if (target_result != NULL)
 
1331
                                return target_result;   /* return the first match */
 
1332
                }
 
1333
        }
 
1334
        if (IsA(node, A_Const))
 
1335
        {
 
1336
                Value      *val = &((A_Const *) node)->val;
 
1337
                int                     location = ((A_Const *) node)->location;
 
1338
                int                     targetlist_pos = 0;
 
1339
                int                     target_pos;
 
1340
 
 
1341
                if (!IsA(val, Integer))
 
1342
                        ereport(ERROR,
 
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)));
 
1348
 
 
1349
                target_pos = intVal(val);
 
1350
                foreach(tl, *tlist)
 
1351
                {
 
1352
                        TargetEntry *tle = (TargetEntry *) lfirst(tl);
 
1353
 
 
1354
                        if (!tle->resjunk)
 
1355
                        {
 
1356
                                if (++targetlist_pos == target_pos)
 
1357
                                        return tle; /* return the unique match */
 
1358
                        }
 
1359
                }
 
1360
                ereport(ERROR,
 
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)));
 
1366
        }
 
1367
 
 
1368
        /*
 
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.
 
1375
         */
 
1376
        expr = transformExpr(pstate, node);
 
1377
 
 
1378
        foreach(tl, *tlist)
 
1379
        {
 
1380
                TargetEntry *tle = (TargetEntry *) lfirst(tl);
 
1381
 
 
1382
                if (equal(expr, tle->expr))
 
1383
                        return tle;
 
1384
        }
 
1385
 
 
1386
        /*
 
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.
 
1390
         */
 
1391
        target_result = transformTargetEntry(pstate, node, expr, NULL, true);
 
1392
 
 
1393
        *tlist = lappend(*tlist, target_result);
 
1394
 
 
1395
        return target_result;
 
1396
}
 
1397
 
 
1398
/*
 
1399
 * transformGroupClause -
 
1400
 *        transform a GROUP BY clause
 
1401
 *
 
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.
 
1404
 *
 
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).
 
1407
 */
 
1408
List *
 
1409
transformGroupClause(ParseState *pstate, List *grouplist,
 
1410
                                         List **targetlist, List *sortClause,
 
1411
                                         bool isPartition)
 
1412
{
 
1413
        List       *result = NIL;
 
1414
        int                     clause = isPartition ? PARTITION_CLAUSE : GROUP_CLAUSE;
 
1415
        ListCell   *gl;
 
1416
 
 
1417
        foreach(gl, grouplist)
 
1418
        {
 
1419
                Node       *gexpr = (Node *) lfirst(gl);
 
1420
                TargetEntry *tle;
 
1421
                bool            found = false;
 
1422
 
 
1423
                tle = findTargetlistEntry(pstate, gexpr, targetlist, clause);
 
1424
 
 
1425
                /* Eliminate duplicates (GROUP BY x, x) */
 
1426
                if (targetIsInSortList(tle, InvalidOid, result))
 
1427
                        continue;
 
1428
 
 
1429
                /*
 
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.
 
1439
                 */
 
1440
                if (tle->ressortgroupref > 0)
 
1441
                {
 
1442
                        ListCell   *sl;
 
1443
 
 
1444
                        foreach(sl, sortClause)
 
1445
                        {
 
1446
                                SortGroupClause *sc = (SortGroupClause *) lfirst(sl);
 
1447
 
 
1448
                                if (sc->tleSortGroupRef == tle->ressortgroupref)
 
1449
                                {
 
1450
                                        result = lappend(result, copyObject(sc));
 
1451
                                        found = true;
 
1452
                                        break;
 
1453
                                }
 
1454
                        }
 
1455
                }
 
1456
 
 
1457
                /*
 
1458
                 * If no match in ORDER BY, just add it to the result using
 
1459
                 * default sort/group semantics.
 
1460
                 */
 
1461
                if (!found)
 
1462
                        result = addTargetToGroupList(pstate, tle,
 
1463
                                                                                  result, *targetlist,
 
1464
                                                                                  exprLocation(gexpr),
 
1465
                                                                                  true);
 
1466
        }
 
1467
 
 
1468
        return result;
 
1469
}
 
1470
 
 
1471
/*
 
1472
 * transformSortClause -
 
1473
 *        transform an ORDER BY clause
 
1474
 *
 
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.
 
1477
 */
 
1478
List *
 
1479
transformSortClause(ParseState *pstate,
 
1480
                                        List *orderlist,
 
1481
                                        List **targetlist,
 
1482
                                        bool resolveUnknown)
 
1483
{
 
1484
        List       *sortlist = NIL;
 
1485
        ListCell   *olitem;
 
1486
 
 
1487
        foreach(olitem, orderlist)
 
1488
        {
 
1489
                SortBy     *sortby = (SortBy *) lfirst(olitem);
 
1490
                TargetEntry *tle;
 
1491
 
 
1492
                tle = findTargetlistEntry(pstate, sortby->node,
 
1493
                                                                  targetlist, ORDER_CLAUSE);
 
1494
 
 
1495
                sortlist = addTargetToSortList(pstate, tle,
 
1496
                                                                           sortlist, *targetlist, sortby,
 
1497
                                                                           resolveUnknown);
 
1498
        }
 
1499
 
 
1500
        return sortlist;
 
1501
}
 
1502
 
 
1503
/*
 
1504
 * transformWindowDefinitions -
 
1505
 *              transform window definitions (WindowDef to WindowClause)
 
1506
 */
 
1507
List *
 
1508
transformWindowDefinitions(ParseState *pstate,
 
1509
                                                   List *windowdefs,
 
1510
                                                   List **targetlist)
 
1511
{
 
1512
        List       *result = NIL;
 
1513
        Index           winref = 0;
 
1514
        ListCell   *lc;
 
1515
 
 
1516
        foreach(lc, windowdefs)
 
1517
        {
 
1518
                WindowDef        *windef = (WindowDef *) lfirst(lc);
 
1519
                WindowClause *refwc = NULL;
 
1520
                List             *partitionClause;
 
1521
                List             *orderClause;
 
1522
                WindowClause *wc;
 
1523
 
 
1524
                winref++;
 
1525
 
 
1526
                /*
 
1527
                 * Check for duplicate window names.
 
1528
                 */
 
1529
                if (windef->name &&
 
1530
                        findWindowClause(result, windef->name) != NULL)
 
1531
                        ereport(ERROR,
 
1532
                                        (errcode(ERRCODE_WINDOWING_ERROR),
 
1533
                                         errmsg("window \"%s\" is already defined", windef->name),
 
1534
                                         parser_errposition(pstate, windef->location)));
 
1535
 
 
1536
                /*
 
1537
                 * If it references a previous window, look that up.
 
1538
                 */
 
1539
                if (windef->refname)
 
1540
                {
 
1541
                        refwc = findWindowClause(result, windef->refname);
 
1542
                        if (refwc == NULL)
 
1543
                                ereport(ERROR,
 
1544
                                                (errcode(ERRCODE_UNDEFINED_OBJECT),
 
1545
                                                 errmsg("window \"%s\" does not exist",
 
1546
                                                                windef->refname),
 
1547
                                                 parser_errposition(pstate, windef->location)));
 
1548
                }
 
1549
 
 
1550
                /*
 
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.
 
1554
                 */
 
1555
                orderClause = transformSortClause(pstate,
 
1556
                                                                                  windef->orderClause,
 
1557
                                                                                  targetlist,
 
1558
                                                                                  true);
 
1559
                partitionClause = transformGroupClause(pstate,
 
1560
                                                                                           windef->partitionClause,
 
1561
                                                                                           targetlist,
 
1562
                                                                                           orderClause,
 
1563
                                                                                           true);
 
1564
 
 
1565
                /*
 
1566
                 * And prepare the new WindowClause.
 
1567
                 */
 
1568
                wc = makeNode(WindowClause);
 
1569
                wc->name = windef->name;
 
1570
                wc->refname = windef->refname;
 
1571
 
 
1572
                /*
 
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.)
 
1580
                 */
 
1581
                if (refwc)
 
1582
                {
 
1583
                        if (partitionClause)
 
1584
                                ereport(ERROR,
 
1585
                                                (errcode(ERRCODE_WINDOWING_ERROR),
 
1586
                                                 errmsg("cannot override PARTITION BY clause of window \"%s\"",
 
1587
                                                                windef->refname),
 
1588
                                                 parser_errposition(pstate, windef->location)));
 
1589
                        wc->partitionClause = copyObject(refwc->partitionClause);
 
1590
                }
 
1591
                else
 
1592
                        wc->partitionClause = partitionClause;
 
1593
                if (refwc)
 
1594
                {
 
1595
                        if (orderClause && refwc->orderClause)
 
1596
                                ereport(ERROR,
 
1597
                                                (errcode(ERRCODE_WINDOWING_ERROR),
 
1598
                                                 errmsg("cannot override ORDER BY clause of window \"%s\"",
 
1599
                                                                windef->refname),
 
1600
                                                 parser_errposition(pstate, windef->location)));
 
1601
                        if (orderClause)
 
1602
                        {
 
1603
                                wc->orderClause = orderClause;
 
1604
                                wc->copiedOrder = false;
 
1605
                        }
 
1606
                        else
 
1607
                        {
 
1608
                                wc->orderClause = copyObject(refwc->orderClause);
 
1609
                                wc->copiedOrder = true;
 
1610
                        }
 
1611
                }
 
1612
                else
 
1613
                {
 
1614
                        wc->orderClause = orderClause;
 
1615
                        wc->copiedOrder = false;
 
1616
                }
 
1617
                if (refwc && refwc->frameOptions != FRAMEOPTION_DEFAULTS)
 
1618
                        ereport(ERROR,
 
1619
                                        (errcode(ERRCODE_WINDOWING_ERROR),
 
1620
                                         errmsg("cannot override frame clause of window \"%s\"",
 
1621
                                                        windef->refname),
 
1622
                                         parser_errposition(pstate, windef->location)));
 
1623
                wc->frameOptions = windef->frameOptions;
 
1624
                wc->winref = winref;
 
1625
 
 
1626
                result = lappend(result, wc);
 
1627
        }
 
1628
 
 
1629
        return result;
 
1630
}
 
1631
 
 
1632
/*
 
1633
 * transformDistinctClause -
 
1634
 *        transform a DISTINCT clause
 
1635
 *
 
1636
 * Since we may need to add items to the query's targetlist, that list
 
1637
 * is passed by reference.
 
1638
 *
 
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
 
1643
 * operator.
 
1644
 */
 
1645
List *
 
1646
transformDistinctClause(ParseState *pstate,
 
1647
                                                List **targetlist, List *sortClause)
 
1648
{
 
1649
        List       *result = NIL;
 
1650
        ListCell   *slitem;
 
1651
        ListCell   *tlitem;
 
1652
 
 
1653
        /*
 
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.
 
1661
         *
 
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.
 
1667
         */
 
1668
        foreach(slitem, sortClause)
 
1669
        {
 
1670
                SortGroupClause *scl = (SortGroupClause *) lfirst(slitem);
 
1671
                TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
 
1672
 
 
1673
                if (tle->resjunk)
 
1674
                        ereport(ERROR,
 
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));
 
1680
        }
 
1681
 
 
1682
        /*
 
1683
         * Now add any remaining non-resjunk tlist items, using default
 
1684
         * sort/group semantics for their data types.
 
1685
         */
 
1686
        foreach(tlitem, *targetlist)
 
1687
        {
 
1688
                TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
 
1689
 
 
1690
                if (tle->resjunk)
 
1691
                        continue;                       /* ignore junk */
 
1692
                result = addTargetToGroupList(pstate, tle,
 
1693
                                                                          result, *targetlist,
 
1694
                                                                          exprLocation((Node *) tle->expr),
 
1695
                                                                          true);
 
1696
        }
 
1697
 
 
1698
        return result;
 
1699
}
 
1700
 
 
1701
/*
 
1702
 * transformDistinctOnClause -
 
1703
 *        transform a DISTINCT ON clause
 
1704
 *
 
1705
 * Since we may need to add items to the query's targetlist, that list
 
1706
 * is passed by reference.
 
1707
 *
 
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
 
1712
 * operator.
 
1713
 */
 
1714
List *
 
1715
transformDistinctOnClause(ParseState *pstate, List *distinctlist,
 
1716
                                                  List **targetlist, List *sortClause)
 
1717
{
 
1718
        List       *result = NIL;
 
1719
        List       *sortgrouprefs = NIL;
 
1720
        bool            skipped_sortitem;
 
1721
        ListCell   *lc;
 
1722
        ListCell   *lc2;
 
1723
 
 
1724
        /*
 
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.)
 
1731
         */
 
1732
        foreach(lc, distinctlist)
 
1733
        {
 
1734
                Node       *dexpr = (Node *) lfirst(lc);
 
1735
                int                     sortgroupref;
 
1736
                TargetEntry *tle;
 
1737
 
 
1738
                tle = findTargetlistEntry(pstate, dexpr,
 
1739
                                                                  targetlist, DISTINCT_ON_CLAUSE);
 
1740
                sortgroupref = assignSortGroupRef(tle, *targetlist);
 
1741
                sortgrouprefs = lappend_int(sortgrouprefs, sortgroupref);
 
1742
        }
 
1743
 
 
1744
        /*
 
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.
 
1751
         */
 
1752
        skipped_sortitem = false;
 
1753
        foreach(lc, sortClause)
 
1754
        {
 
1755
                SortGroupClause *scl = (SortGroupClause *) lfirst(lc);
 
1756
 
 
1757
                if (list_member_int(sortgrouprefs, scl->tleSortGroupRef))
 
1758
                {
 
1759
                        if (skipped_sortitem)
 
1760
                                ereport(ERROR,
 
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,
 
1765
                                                                                                                                  sortgrouprefs,
 
1766
                                                                                                                                  distinctlist))));
 
1767
                        else
 
1768
                                result = lappend(result, copyObject(scl));
 
1769
                }
 
1770
                else
 
1771
                        skipped_sortitem = true;
 
1772
        }
 
1773
 
 
1774
        /*
 
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.)
 
1782
         */
 
1783
        forboth(lc, distinctlist, lc2, sortgrouprefs)
 
1784
        {
 
1785
                Node       *dexpr = (Node *) lfirst(lc);
 
1786
                int                     sortgroupref = lfirst_int(lc2);
 
1787
                TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
 
1788
 
 
1789
                if (targetIsInSortList(tle, InvalidOid, result))
 
1790
                        continue;                       /* already in list (with some semantics) */
 
1791
                if (skipped_sortitem)
 
1792
                        ereport(ERROR,
 
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),
 
1799
                                                                          true);
 
1800
        }
 
1801
 
 
1802
        return result;
 
1803
}
 
1804
 
 
1805
/*
 
1806
 * get_matching_location
 
1807
 *              Get the exprLocation of the exprs member corresponding to the
 
1808
 *              (first) member of sortgrouprefs that equals sortgroupref.
 
1809
 *
 
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.)
 
1815
 */
 
1816
static int
 
1817
get_matching_location(int sortgroupref, List *sortgrouprefs, List *exprs)
 
1818
{
 
1819
        ListCell   *lcs;
 
1820
        ListCell   *lce;
 
1821
 
 
1822
        forboth(lcs, sortgrouprefs, lce, exprs)
 
1823
        {
 
1824
                if (lfirst_int(lcs) == sortgroupref)
 
1825
                        return exprLocation((Node *) lfirst(lce));
 
1826
        }
 
1827
        /* if no match, caller blew it */
 
1828
        elog(ERROR, "get_matching_location: no matching sortgroupref");
 
1829
        return -1;                                      /* keep compiler quiet */
 
1830
}
 
1831
 
 
1832
/*
 
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
 
1836
 *              info.
 
1837
 *
 
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
 
1841
 * otherwise.
 
1842
 *
 
1843
 * Returns the updated SortGroupClause list.
 
1844
 */
 
1845
static List *
 
1846
addTargetToSortList(ParseState *pstate, TargetEntry *tle,
 
1847
                                        List *sortlist, List *targetlist, SortBy *sortby,
 
1848
                                        bool resolveUnknown)
 
1849
{
 
1850
        Oid                     restype = exprType((Node *) tle->expr);
 
1851
        Oid                     sortop;
 
1852
        Oid                     eqop;
 
1853
        bool            reverse;
 
1854
        int                     location;
 
1855
        ParseCallbackState pcbstate;
 
1856
 
 
1857
        /* if tlist item is an UNKNOWN literal, change it to TEXT */
 
1858
        if (restype == UNKNOWNOID && resolveUnknown)
 
1859
        {
 
1860
                tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
 
1861
                                                                                 restype, TEXTOID, -1,
 
1862
                                                                                 COERCION_IMPLICIT,
 
1863
                                                                                 COERCE_IMPLICIT_CAST,
 
1864
                                                                                 -1);
 
1865
                restype = TEXTOID;
 
1866
        }
 
1867
 
 
1868
        /*
 
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.)
 
1875
         */
 
1876
        location = sortby->location;
 
1877
        if (location < 0)
 
1878
                location = exprLocation(sortby->node);
 
1879
        setup_parser_errposition_callback(&pcbstate, pstate, location);
 
1880
 
 
1881
        /* determine the sortop, eqop, and directionality */
 
1882
        switch (sortby->sortby_dir)
 
1883
        {
 
1884
                case SORTBY_DEFAULT:
 
1885
                case SORTBY_ASC:
 
1886
                        get_sort_group_operators(restype,
 
1887
                                                                         true, true, false,
 
1888
                                                                         &sortop, &eqop, NULL);
 
1889
                        reverse = false;
 
1890
                        break;
 
1891
                case SORTBY_DESC:
 
1892
                        get_sort_group_operators(restype,
 
1893
                                                                         false, true, true,
 
1894
                                                                         NULL, &eqop, &sortop);
 
1895
                        reverse = true;
 
1896
                        break;
 
1897
                case SORTBY_USING:
 
1898
                        Assert(sortby->useOp != NIL);
 
1899
                        sortop = compatible_oper_opid(sortby->useOp,
 
1900
                                                                                  restype,
 
1901
                                                                                  restype,
 
1902
                                                                                  false);
 
1903
 
 
1904
                        /*
 
1905
                         * Verify it's a valid ordering operator, fetch the corresponding
 
1906
                         * equality operator, and determine whether to consider it like
 
1907
                         * ASC or DESC.
 
1908
                         */
 
1909
                        eqop = get_equality_op_for_ordering_op(sortop, &reverse);
 
1910
                        if (!OidIsValid(eqop))
 
1911
                                ereport(ERROR,
 
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.")));
 
1916
                        break;
 
1917
                default:
 
1918
                        elog(ERROR, "unrecognized sortby_dir: %d", sortby->sortby_dir);
 
1919
                        sortop = InvalidOid;    /* keep compiler quiet */
 
1920
                        eqop = InvalidOid;
 
1921
                        reverse = false;
 
1922
                        break;
 
1923
        }
 
1924
 
 
1925
        cancel_parser_errposition_callback(&pcbstate);
 
1926
 
 
1927
        /* avoid making duplicate sortlist entries */
 
1928
        if (!targetIsInSortList(tle, sortop, sortlist))
 
1929
        {
 
1930
                SortGroupClause *sortcl = makeNode(SortGroupClause);
 
1931
 
 
1932
                sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
 
1933
 
 
1934
                sortcl->eqop = eqop;
 
1935
                sortcl->sortop = sortop;
 
1936
 
 
1937
                switch (sortby->sortby_nulls)
 
1938
                {
 
1939
                        case SORTBY_NULLS_DEFAULT:
 
1940
                                /* NULLS FIRST is default for DESC; other way for ASC */
 
1941
                                sortcl->nulls_first = reverse;
 
1942
                                break;
 
1943
                        case SORTBY_NULLS_FIRST:
 
1944
                                sortcl->nulls_first = true;
 
1945
                                break;
 
1946
                        case SORTBY_NULLS_LAST:
 
1947
                                sortcl->nulls_first = false;
 
1948
                                break;
 
1949
                        default:
 
1950
                                elog(ERROR, "unrecognized sortby_nulls: %d",
 
1951
                                         sortby->sortby_nulls);
 
1952
                                break;
 
1953
                }
 
1954
 
 
1955
                sortlist = lappend(sortlist, sortcl);
 
1956
        }
 
1957
 
 
1958
        return sortlist;
 
1959
}
 
1960
 
 
1961
/*
 
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
 
1965
 *              semantics.
 
1966
 *
 
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.
 
1971
 *
 
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.
 
1976
 *
 
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
 
1980
 * otherwise.
 
1981
 *
 
1982
 * Returns the updated SortGroupClause list.
 
1983
 */
 
1984
static List *
 
1985
addTargetToGroupList(ParseState *pstate, TargetEntry *tle,
 
1986
                                         List *grouplist, List *targetlist, int location,
 
1987
                                         bool resolveUnknown)
 
1988
{
 
1989
        Oid                     restype = exprType((Node *) tle->expr);
 
1990
        Oid                     sortop;
 
1991
        Oid                     eqop;
 
1992
 
 
1993
        /* if tlist item is an UNKNOWN literal, change it to TEXT */
 
1994
        if (restype == UNKNOWNOID && resolveUnknown)
 
1995
        {
 
1996
                tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
 
1997
                                                                                 restype, TEXTOID, -1,
 
1998
                                                                                 COERCION_IMPLICIT,
 
1999
                                                                                 COERCE_IMPLICIT_CAST,
 
2000
                                                                                 -1);
 
2001
                restype = TEXTOID;
 
2002
        }
 
2003
 
 
2004
        /* avoid making duplicate grouplist entries */
 
2005
        if (!targetIsInSortList(tle, InvalidOid, grouplist))
 
2006
        {
 
2007
                SortGroupClause *grpcl = makeNode(SortGroupClause);
 
2008
                ParseCallbackState pcbstate;
 
2009
 
 
2010
                setup_parser_errposition_callback(&pcbstate, pstate, location);
 
2011
 
 
2012
                /* determine the eqop and optional sortop */
 
2013
                get_sort_group_operators(restype,
 
2014
                                                                 false, true, false,
 
2015
                                                                 &sortop, &eqop, NULL);
 
2016
 
 
2017
                cancel_parser_errposition_callback(&pcbstate);
 
2018
 
 
2019
                grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
 
2020
                grpcl->eqop = eqop;
 
2021
                grpcl->sortop = sortop;
 
2022
                grpcl->nulls_first = false;             /* OK with or without sortop */
 
2023
 
 
2024
                grouplist = lappend(grouplist, grpcl);
 
2025
        }
 
2026
 
 
2027
        return grouplist;
 
2028
}
 
2029
 
 
2030
/*
 
2031
 * assignSortGroupRef
 
2032
 *        Assign the targetentry an unused ressortgroupref, if it doesn't
 
2033
 *        already have one.  Return the assigned or pre-existing refnumber.
 
2034
 *
 
2035
 * 'tlist' is the targetlist containing (or to contain) the given targetentry.
 
2036
 */
 
2037
Index
 
2038
assignSortGroupRef(TargetEntry *tle, List *tlist)
 
2039
{
 
2040
        Index           maxRef;
 
2041
        ListCell   *l;
 
2042
 
 
2043
        if (tle->ressortgroupref)       /* already has one? */
 
2044
                return tle->ressortgroupref;
 
2045
 
 
2046
        /* easiest way to pick an unused refnumber: max used + 1 */
 
2047
        maxRef = 0;
 
2048
        foreach(l, tlist)
 
2049
        {
 
2050
                Index           ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
 
2051
 
 
2052
                if (ref > maxRef)
 
2053
                        maxRef = ref;
 
2054
        }
 
2055
        tle->ressortgroupref = maxRef + 1;
 
2056
        return tle->ressortgroupref;
 
2057
}
 
2058
 
 
2059
/*
 
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.
 
2063
 *
 
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.
 
2072
 *
 
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.
 
2077
 */
 
2078
bool
 
2079
targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
 
2080
{
 
2081
        Index           ref = tle->ressortgroupref;
 
2082
        ListCell   *l;
 
2083
 
 
2084
        /* no need to scan list if tle has no marker */
 
2085
        if (ref == 0)
 
2086
                return false;
 
2087
 
 
2088
        foreach(l, sortList)
 
2089
        {
 
2090
                SortGroupClause *scl = (SortGroupClause *) lfirst(l);
 
2091
 
 
2092
                if (scl->tleSortGroupRef == ref &&
 
2093
                        (sortop == InvalidOid ||
 
2094
                         sortop == scl->sortop ||
 
2095
                         sortop == get_commutator(scl->sortop)))
 
2096
                        return true;
 
2097
        }
 
2098
        return false;
 
2099
}
 
2100
 
 
2101
/*
 
2102
 * findWindowClause
 
2103
 *              Find the named WindowClause in the list, or return NULL if not there
 
2104
 */
 
2105
static WindowClause *
 
2106
findWindowClause(List *wclist, const char *name)
 
2107
{
 
2108
        ListCell   *l;
 
2109
 
 
2110
        foreach(l, wclist)
 
2111
        {
 
2112
                WindowClause *wc = (WindowClause *) lfirst(l);
 
2113
 
 
2114
                if (wc->name && strcmp(wc->name, name) == 0)
 
2115
                        return wc;
 
2116
        }
 
2117
 
 
2118
        return NULL;
 
2119
}