* parse_clause.c
* handle clauses in parser
*
- * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.108 2003/02/13 05:53:46 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/parser/parse_clause.c,v 1.171 2008/07/31 22:47:56 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "access/heapam.h"
#include "catalog/heap.h"
+#include "catalog/pg_type.h"
+#include "commands/defrem.h"
#include "nodes/makefuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/tlist.h"
#include "parser/parse_oper.h"
#include "parser/parse_relation.h"
#include "parser/parse_target.h"
-#include "parser/parse_type.h"
-#include "utils/builtins.h"
+#include "rewrite/rewriteManip.h"
#include "utils/guc.h"
+#include "utils/lsyscache.h"
+#include "utils/rel.h"
#define ORDER_CLAUSE 0
static Node *transformJoinUsingClause(ParseState *pstate,
List *leftVars, List *rightVars);
static Node *transformJoinOnClause(ParseState *pstate, JoinExpr *j,
- List *containedRels);
-static RangeTblRef *transformTableEntry(ParseState *pstate, RangeVar *r);
-static RangeTblRef *transformRangeSubselect(ParseState *pstate,
+ RangeTblEntry *l_rte,
+ RangeTblEntry *r_rte,
+ List *relnamespace,
+ Relids containedRels);
+static RangeTblEntry *transformTableEntry(ParseState *pstate, RangeVar *r);
+static RangeTblEntry *transformRangeSubselect(ParseState *pstate,
RangeSubselect *r);
-static RangeTblRef *transformRangeFunction(ParseState *pstate,
+static RangeTblEntry *transformRangeFunction(ParseState *pstate,
RangeFunction *r);
static Node *transformFromClauseItem(ParseState *pstate, Node *n,
- List **containedRels);
-static Node *buildMergedJoinVar(JoinType jointype,
+ RangeTblEntry **top_rte, int *top_rti,
+ List **relnamespace,
+ Relids *containedRels);
+static Node *buildMergedJoinVar(ParseState *pstate, JoinType jointype,
Var *l_colvar, Var *r_colvar);
static TargetEntry *findTargetlistEntry(ParseState *pstate, Node *node,
- List *tlist, int clause);
-static List *addTargetToSortList(TargetEntry *tle, List *sortlist,
- List *targetlist, List *opname);
-static bool targetIsInSortList(TargetEntry *tle, List *sortList);
+ List **tlist, int clause);
/*
* transformFromClause -
* Process the FROM clause and add items to the query's range table,
- * joinlist, and namespace.
+ * joinlist, and namespaces.
*
- * Note: we assume that pstate's p_rtable, p_joinlist, and p_namespace lists
- * were initialized to NIL when the pstate was created. We will add onto
- * any entries already present --- this is needed for rule processing, as
- * well as for UPDATE and DELETE.
+ * Note: we assume that pstate's p_rtable, p_joinlist, p_relnamespace, and
+ * p_varnamespace lists were initialized to NIL when the pstate was created.
+ * We will add onto any entries already present --- this is needed for rule
+ * processing, as well as for UPDATE and DELETE.
*
* The range table may grow still further when we transform the expressions
* in the query's quals and target list. (This is possible because in
void
transformFromClause(ParseState *pstate, List *frmList)
{
- List *fl;
+ ListCell *fl;
/*
- * The grammar will have produced a list of RangeVars,
- * RangeSubselects, RangeFunctions, and/or JoinExprs. Transform each
- * one (possibly adding entries to the rtable), check for duplicate
- * refnames, and then add it to the joinlist and namespace.
+ * The grammar will have produced a list of RangeVars, RangeSubselects,
+ * RangeFunctions, and/or JoinExprs. Transform each one (possibly adding
+ * entries to the rtable), check for duplicate refnames, and then add it
+ * to the joinlist and namespaces.
*/
foreach(fl, frmList)
{
Node *n = lfirst(fl);
- List *containedRels;
-
- n = transformFromClauseItem(pstate, n, &containedRels);
- checkNameSpaceConflicts(pstate, (Node *) pstate->p_namespace, n);
+ RangeTblEntry *rte;
+ int rtindex;
+ List *relnamespace;
+ Relids containedRels;
+
+ n = transformFromClauseItem(pstate, n,
+ &rte,
+ &rtindex,
+ &relnamespace,
+ &containedRels);
+ checkNameSpaceConflicts(pstate, pstate->p_relnamespace, relnamespace);
pstate->p_joinlist = lappend(pstate->p_joinlist, n);
- pstate->p_namespace = lappend(pstate->p_namespace, n);
+ pstate->p_relnamespace = list_concat(pstate->p_relnamespace,
+ relnamespace);
+ pstate->p_varnamespace = lappend(pstate->p_varnamespace, rte);
+ bms_free(containedRels);
}
}
* to check for namespace conflict; we assume that the namespace was
* initially empty in these cases.)
*
+ * Finally, we mark the relation as requiring the permissions specified
+ * by requiredPerms.
+ *
* Returns the rangetable index of the target relation.
*/
int
setTargetTable(ParseState *pstate, RangeVar *relation,
- bool inh, bool alsoSource)
+ bool inh, bool alsoSource, AclMode requiredPerms)
{
RangeTblEntry *rte;
int rtindex;
heap_close(pstate->p_target_relation, NoLock);
/*
- * Open target rel and grab suitable lock (which we will hold till end
- * of transaction).
+ * Open target rel and grab suitable lock (which we will hold till end of
+ * transaction).
*
- * analyze.c will eventually do the corresponding heap_close(), but *not*
- * release the lock.
+ * free_parsestate() will eventually do the corresponding heap_close(),
+ * but *not* release the lock.
*/
pstate->p_target_relation = heap_openrv(relation, RowExclusiveLock);
/*
* Now build an RTE.
*/
- rte = addRangeTableEntry(pstate, relation, NULL, inh, false);
+ rte = addRangeTableEntryForRelation(pstate, pstate->p_target_relation,
+ relation->alias, inh, false);
pstate->p_target_rangetblentry = rte;
/* assume new rte is at end */
- rtindex = length(pstate->p_rtable);
+ rtindex = list_length(pstate->p_rtable);
Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
/*
- * Override addRangeTableEntry's default checkForRead, and instead
- * mark target table as requiring write access.
+ * Override addRangeTableEntry's default ACL_SELECT permissions check, and
+ * instead mark target table as requiring exactly the specified
+ * permissions.
*
* If we find an explicit reference to the rel later during parse
- * analysis, scanRTEForColumn will change checkForRead to 'true'
- * again. That can't happen for INSERT but it is possible for UPDATE
- * and DELETE.
+ * analysis, we will add the ACL_SELECT bit back again; see
+ * scanRTEForColumn (for simple field references), ExpandColumnRefStar
+ * (for foo.*) and ExpandAllTables (for *).
*/
- rte->checkForRead = false;
- rte->checkForWrite = true;
+ rte->requiredPerms = requiredPerms;
/*
- * If UPDATE/DELETE, add table to joinlist and namespace.
+ * If UPDATE/DELETE, add table to joinlist and namespaces.
*/
if (alsoSource)
- addRTEtoQuery(pstate, rte, true, true);
+ addRTEtoQuery(pstate, rte, true, true, true);
return rtindex;
}
* Simplify InhOption (yes/no/default) into boolean yes/no.
*
* The reason we do things this way is that we don't want to examine the
- * SQL_inheritance option flag until parse_analyze is run. Otherwise,
+ * SQL_inheritance option flag until parse_analyze() is run. Otherwise,
* we'd do the wrong thing with query strings that intermix SET commands
* with queries.
*/
case INH_DEFAULT:
return SQL_inheritance;
}
- elog(ERROR, "Bogus InhOption value");
+ elog(ERROR, "bogus InhOption value: %d", inhOpt);
return false; /* keep compiler quiet */
}
+/*
+ * Given a relation-options list (of DefElems), return true iff the specified
+ * table/result set should be created with OIDs. This needs to be done after
+ * parsing the query string because the return value can depend upon the
+ * default_with_oids GUC var.
+ */
+bool
+interpretOidsOption(List *defList)
+{
+ ListCell *cell;
+
+ /* Scan list to see if OIDS was included */
+ foreach(cell, defList)
+ {
+ DefElem *def = (DefElem *) lfirst(cell);
+
+ if (pg_strcasecmp(def->defname, "oids") == 0)
+ return defGetBoolean(def);
+ }
+
+ /* OIDS option was not specified, so use default. */
+ return default_with_oids;
+}
+
/*
* Extract all not-in-common columns from column lists of a source table
*/
{
List *new_colnames = NIL;
List *new_colvars = NIL;
- List *lnames,
- *lvars = src_colvars;
+ ListCell *lnames,
+ *lvars;
- foreach(lnames, src_colnames)
+ Assert(list_length(src_colnames) == list_length(src_colvars));
+
+ forboth(lnames, src_colnames, lvars, src_colvars)
{
char *colname = strVal(lfirst(lnames));
bool match = false;
- List *cnames;
+ ListCell *cnames;
foreach(cnames, common_colnames)
{
new_colnames = lappend(new_colnames, lfirst(lnames));
new_colvars = lappend(new_colvars, lfirst(lvars));
}
-
- lvars = lnext(lvars);
}
*res_colnames = new_colnames;
transformJoinUsingClause(ParseState *pstate, List *leftVars, List *rightVars)
{
Node *result = NULL;
- List *lvars,
- *rvars = rightVars;
+ ListCell *lvars,
+ *rvars;
/*
- * We cheat a little bit here by building an untransformed operator
- * tree whose leaves are the already-transformed Vars. This is OK
- * because transformExpr() won't complain about already-transformed
- * subnodes.
+ * We cheat a little bit here by building an untransformed operator tree
+ * whose leaves are the already-transformed Vars. This is OK because
+ * transformExpr() won't complain about already-transformed subnodes.
*/
- foreach(lvars, leftVars)
+ forboth(lvars, leftVars, rvars, rightVars)
{
Node *lvar = (Node *) lfirst(lvars);
Node *rvar = (Node *) lfirst(rvars);
A_Expr *e;
- e = makeSimpleA_Expr(AEXPR_OP, "=", copyObject(lvar), copyObject(rvar));
+ e = makeSimpleA_Expr(AEXPR_OP, "=",
+ copyObject(lvar), copyObject(rvar),
+ -1);
if (result == NULL)
result = (Node *) e;
{
A_Expr *a;
- a = makeA_Expr(AEXPR_AND, NIL, result, (Node *) e);
+ a = makeA_Expr(AEXPR_AND, NIL, result, (Node *) e, -1);
result = (Node *) a;
}
-
- rvars = lnext(rvars);
}
/*
- * Since the references are already Vars, and are certainly from the
- * input relations, we don't have to go through the same pushups that
- * transformJoinOnClause() does. Just invoke transformExpr() to fix
- * up the operators, and we're done.
+ * Since the references are already Vars, and are certainly from the input
+ * relations, we don't have to go through the same pushups that
+ * transformJoinOnClause() does. Just invoke transformExpr() to fix up
+ * the operators, and we're done.
*/
result = transformExpr(pstate, result);
- result = coerce_to_boolean(result, "JOIN/USING");
+ result = coerce_to_boolean(pstate, result, "JOIN/USING");
return result;
-} /* transformJoinUsingClause() */
+}
/* transformJoinOnClause()
* Transform the qual conditions for JOIN/ON.
*/
static Node *
transformJoinOnClause(ParseState *pstate, JoinExpr *j,
- List *containedRels)
+ RangeTblEntry *l_rte,
+ RangeTblEntry *r_rte,
+ List *relnamespace,
+ Relids containedRels)
{
Node *result;
- List *save_namespace;
+ List *save_relnamespace;
+ List *save_varnamespace;
Relids clause_varnos;
int varno;
/*
- * This is a tad tricky, for two reasons. First, the namespace that
- * the join expression should see is just the two subtrees of the JOIN
- * plus any outer references from upper pstate levels. So,
- * temporarily set this pstate's namespace accordingly. (We need not
- * check for refname conflicts, because transformFromClauseItem()
- * already did.) NOTE: this code is OK only because the ON clause
- * can't legally alter the namespace by causing implicit relation refs
- * to be added.
+ * This is a tad tricky, for two reasons. First, the namespace that the
+ * join expression should see is just the two subtrees of the JOIN plus
+ * any outer references from upper pstate levels. So, temporarily set
+ * this pstate's namespace accordingly. (We need not check for refname
+ * conflicts, because transformFromClauseItem() already did.) NOTE: this
+ * code is OK only because the ON clause can't legally alter the namespace
+ * by causing implicit relation refs to be added.
*/
- save_namespace = pstate->p_namespace;
- pstate->p_namespace = makeList2(j->larg, j->rarg);
+ save_relnamespace = pstate->p_relnamespace;
+ save_varnamespace = pstate->p_varnamespace;
- /* This part is just like transformWhereClause() */
- result = transformExpr(pstate, j->quals);
+ pstate->p_relnamespace = relnamespace;
+ pstate->p_varnamespace = list_make2(l_rte, r_rte);
- result = coerce_to_boolean(result, "JOIN/ON");
+ result = transformWhereClause(pstate, j->quals, "JOIN/ON");
- pstate->p_namespace = save_namespace;
+ pstate->p_relnamespace = save_relnamespace;
+ pstate->p_varnamespace = save_varnamespace;
/*
* Second, we need to check that the ON condition doesn't refer to any
- * rels outside the input subtrees of the JOIN. It could do that
- * despite our hack on the namespace if it uses fully-qualified names.
- * So, grovel through the transformed clause and make sure there are
- * no bogus references. (Outer references are OK, and are ignored
- * here.)
+ * rels outside the input subtrees of the JOIN. It could do that despite
+ * our hack on the namespace if it uses fully-qualified names. So, grovel
+ * through the transformed clause and make sure there are no bogus
+ * references. (Outer references are OK, and are ignored here.)
*/
clause_varnos = pull_varnos(result);
- while ((varno = bms_first_member(clause_varnos)) >= 0)
+ clause_varnos = bms_del_members(clause_varnos, containedRels);
+ if ((varno = bms_first_member(clause_varnos)) >= 0)
{
- if (!intMember(varno, containedRels))
- {
- elog(ERROR, "JOIN/ON clause refers to \"%s\", which is not part of JOIN",
- rt_fetch(varno, pstate->p_rtable)->eref->aliasname);
- }
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
+ errmsg("JOIN/ON clause refers to \"%s\", which is not part of JOIN",
+ rt_fetch(varno, pstate->p_rtable)->eref->aliasname)));
}
bms_free(clause_varnos);
/*
* transformTableEntry --- transform a RangeVar (simple relation reference)
*/
-static RangeTblRef *
+static RangeTblEntry *
transformTableEntry(ParseState *pstate, RangeVar *r)
{
RangeTblEntry *rte;
- RangeTblRef *rtr;
/*
- * mark this entry to indicate it comes from the FROM clause. In SQL,
- * the target list can only refer to range variables specified in the
- * from clause but we follow the more powerful POSTQUEL semantics and
+ * mark this entry to indicate it comes from the FROM clause. In SQL, the
+ * target list can only refer to range variables specified in the from
+ * clause but we follow the more powerful POSTQUEL semantics and
* automatically generate the range variable if not specified. However
* there are times we need to know whether the entries are legitimate.
*/
rte = addRangeTableEntry(pstate, r, r->alias,
interpretInhOption(r->inhOpt), true);
- /*
- * We create a RangeTblRef, but we do not add it to the joinlist or
- * namespace; our caller must do that if appropriate.
- */
- rtr = makeNode(RangeTblRef);
- /* assume new rte is at end */
- rtr->rtindex = length(pstate->p_rtable);
- Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
-
- return rtr;
+ return rte;
}
/*
* transformRangeSubselect --- transform a sub-SELECT appearing in FROM
*/
-static RangeTblRef *
+static RangeTblEntry *
transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
{
- List *save_namespace;
- List *parsetrees;
Query *query;
RangeTblEntry *rte;
- RangeTblRef *rtr;
/*
- * We require user to supply an alias for a subselect, per SQL92. To
- * relax this, we'd have to be prepared to gin up a unique alias for
- * an unlabeled subselect.
+ * We require user to supply an alias for a subselect, per SQL92. To relax
+ * this, we'd have to be prepared to gin up a unique alias for an
+ * unlabeled subselect.
*/
if (r->alias == NULL)
- elog(ERROR, "sub-select in FROM must have an alias");
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("subquery in FROM must have an alias")));
/*
- * Analyze and transform the subquery. This is a bit tricky because
- * we don't want the subquery to be able to see any FROM items already
- * created in the current query (per SQL92, the scope of a FROM item
- * does not include other FROM items). But it does need to be able to
- * see any further-up parent states, so we can't just pass a null
- * parent pstate link. So, temporarily make the current query level
- * have an empty namespace.
+ * Analyze and transform the subquery.
*/
- save_namespace = pstate->p_namespace;
- pstate->p_namespace = NIL;
-
- parsetrees = parse_analyze(r->subquery, pstate);
-
- pstate->p_namespace = save_namespace;
+ query = parse_sub_analyze(r->subquery, pstate);
/*
- * Check that we got something reasonable. Some of these conditions
- * are probably impossible given restrictions of the grammar, but
- * check 'em anyway.
+ * Check that we got something reasonable. Many of these conditions are
+ * impossible given restrictions of the grammar, but check 'em anyway.
*/
- if (length(parsetrees) != 1)
- elog(ERROR, "Unexpected parse analysis result for subselect in FROM");
- query = (Query *) lfirst(parsetrees);
- if (query == NULL || !IsA(query, Query))
- elog(ERROR, "Unexpected parse analysis result for subselect in FROM");
-
- if (query->commandType != CMD_SELECT)
- elog(ERROR, "Expected SELECT query from subselect in FROM");
- if (query->resultRelation != 0 || query->into != NULL || query->isPortal)
- elog(ERROR, "Subselect in FROM may not have SELECT INTO");
+ if (query->commandType != CMD_SELECT ||
+ query->utilityStmt != NULL)
+ elog(ERROR, "expected SELECT query from subquery in FROM");
+ if (query->intoClause != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("subquery in FROM cannot have SELECT INTO")));
/*
- * OK, build an RTE for the subquery.
+ * The subquery cannot make use of any variables from FROM items created
+ * earlier in the current query. Per SQL92, the scope of a FROM item does
+ * not include other FROM items. Formerly we hacked the namespace so that
+ * the other variables weren't even visible, but it seems more useful to
+ * leave them visible and give a specific error message.
+ *
+ * XXX this will need further work to support SQL99's LATERAL() feature,
+ * wherein such references would indeed be legal.
+ *
+ * We can skip groveling through the subquery if there's not anything
+ * visible in the current query. Also note that outer references are OK.
*/
- rte = addRangeTableEntryForSubquery(pstate, query, r->alias, true);
+ if (pstate->p_relnamespace || pstate->p_varnamespace)
+ {
+ if (contain_vars_of_level((Node *) query, 1))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
+ errmsg("subquery in FROM cannot refer to other relations of same query level")));
+ }
/*
- * We create a RangeTblRef, but we do not add it to the joinlist or
- * namespace; our caller must do that if appropriate.
+ * OK, build an RTE for the subquery.
*/
- rtr = makeNode(RangeTblRef);
- /* assume new rte is at end */
- rtr->rtindex = length(pstate->p_rtable);
- Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
+ rte = addRangeTableEntryForSubquery(pstate, query, r->alias, true);
- return rtr;
+ return rte;
}
/*
* transformRangeFunction --- transform a function call appearing in FROM
*/
-static RangeTblRef *
+static RangeTblEntry *
transformRangeFunction(ParseState *pstate, RangeFunction *r)
{
Node *funcexpr;
char *funcname;
- List *save_namespace;
RangeTblEntry *rte;
- RangeTblRef *rtr;
-
- /* Get function name for possible use as alias */
- Assert(IsA(r->funccallnode, FuncCall));
- funcname = strVal(llast(((FuncCall *) r->funccallnode)->funcname));
/*
- * Transform the raw FuncCall node. This is a bit tricky because we
- * don't want the function expression to be able to see any FROM items
- * already created in the current query (compare to
- * transformRangeSubselect). But it does need to be able to see any
- * further-up parent states. So, temporarily make the current query
- * level have an empty namespace. NOTE: this code is OK only because
- * the expression can't legally alter the namespace by causing
- * implicit relation refs to be added.
+ * Get function name for possible use as alias. We use the same
+ * transformation rules as for a SELECT output expression. For a FuncCall
+ * node, the result will be the function name, but it is possible for the
+ * grammar to hand back other node types.
*/
- save_namespace = pstate->p_namespace;
- pstate->p_namespace = NIL;
-
- funcexpr = transformExpr(pstate, r->funccallnode);
-
- pstate->p_namespace = save_namespace;
+ funcname = FigureColname(r->funccallnode);
/*
- * We still need to check that the function parameters don't refer to
- * any other rels. That could happen despite our hack on the
- * namespace if fully-qualified names are used. So, check there are
- * no local Var references in the transformed expression. (Outer
- * references are OK, and are ignored here.)
+ * Transform the raw expression.
*/
- if (!bms_is_empty(pull_varnos(funcexpr)))
- elog(ERROR, "FROM function expression may not refer to other relations of same query level");
+ funcexpr = transformExpr(pstate, r->funccallnode);
/*
- * Disallow aggregate functions in the expression. (No reason to
- * postpone this check until parseCheckAggregates.)
+ * The function parameters cannot make use of any variables from other
+ * FROM items. (Compare to transformRangeSubselect(); the coding is
+ * different though because we didn't parse as a sub-select with its own
+ * level of namespace.)
+ *
+ * XXX this will need further work to support SQL99's LATERAL() feature,
+ * wherein such references would indeed be legal.
*/
- if (pstate->p_hasAggs)
+ if (pstate->p_relnamespace || pstate->p_varnamespace)
{
- if (contain_agg_clause(funcexpr))
- elog(ERROR, "cannot use aggregate function in FROM function expression");
+ if (contain_vars_of_level(funcexpr, 0))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
+ errmsg("function expression in FROM cannot refer to other relations of same query level")));
}
/*
- * If a coldeflist is supplied, ensure it defines a legal set of names
- * (no duplicates) and datatypes (no pseudo-types, for instance).
+ * Disallow aggregate functions in the expression. (No reason to postpone
+ * this check until parseCheckAggregates.)
*/
- if (r->coldeflist)
+ if (pstate->p_hasAggs)
{
- TupleDesc tupdesc;
-
- tupdesc = BuildDescForRelation(r->coldeflist);
- CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE);
+ if (checkExprHasAggs(funcexpr))
+ ereport(ERROR,
+ (errcode(ERRCODE_GROUPING_ERROR),
+ errmsg("cannot use aggregate function in function expression in FROM")));
}
/*
r, true);
/*
- * We create a RangeTblRef, but we do not add it to the joinlist or
- * namespace; our caller must do that if appropriate.
+ * If a coldeflist was supplied, ensure it defines a legal set of names
+ * (no duplicates) and datatypes (no pseudo-types, for instance).
+ * addRangeTableEntryForFunction looked up the type names but didn't check
+ * them further than that.
*/
- rtr = makeNode(RangeTblRef);
- /* assume new rte is at end */
- rtr->rtindex = length(pstate->p_rtable);
- Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
+ if (r->coldeflist)
+ {
+ TupleDesc tupdesc;
- return rtr;
+ tupdesc = BuildDescFromLists(rte->eref->colnames,
+ rte->funccoltypes,
+ rte->funccoltypmods);
+ CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE);
+ }
+
+ return rte;
}
* transformFromClauseItem -
* Transform a FROM-clause item, adding any required entries to the
* range table list being built in the ParseState, and return the
- * transformed item ready to include in the joinlist and namespace.
+ * transformed item ready to include in the joinlist and namespaces.
* This routine can recurse to handle SQL92 JOIN expressions.
*
- * Aside from the primary return value (the transformed joinlist item)
- * this routine also returns an integer list of the rangetable indexes
- * of all the base and join relations represented in the joinlist item.
- * This list is needed for checking JOIN/ON conditions in higher levels.
+ * The function return value is the node to add to the jointree (a
+ * RangeTblRef or JoinExpr). Additional output parameters are:
+ *
+ * *top_rte: receives the RTE corresponding to the jointree item.
+ * (We could extract this from the function return node, but it saves cycles
+ * to pass it back separately.)
+ *
+ * *top_rti: receives the rangetable index of top_rte. (Ditto.)
+ *
+ * *relnamespace: receives a List of the RTEs exposed as relation names
+ * by this item.
+ *
+ * *containedRels: receives a bitmap set of the rangetable indexes
+ * of all the base and join relations represented in this jointree item.
+ * This is needed for checking JOIN/ON conditions in higher levels.
+ *
+ * We do not need to pass back an explicit varnamespace value, because
+ * in all cases the varnamespace contribution is exactly top_rte.
*/
static Node *
-transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels)
+transformFromClauseItem(ParseState *pstate, Node *n,
+ RangeTblEntry **top_rte, int *top_rti,
+ List **relnamespace,
+ Relids *containedRels)
{
if (IsA(n, RangeVar))
{
/* Plain relation reference */
RangeTblRef *rtr;
+ RangeTblEntry *rte;
+ int rtindex;
- rtr = transformTableEntry(pstate, (RangeVar *) n);
- *containedRels = makeListi1(rtr->rtindex);
+ rte = transformTableEntry(pstate, (RangeVar *) n);
+ /* assume new rte is at end */
+ rtindex = list_length(pstate->p_rtable);
+ Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
+ *top_rte = rte;
+ *top_rti = rtindex;
+ *relnamespace = list_make1(rte);
+ *containedRels = bms_make_singleton(rtindex);
+ rtr = makeNode(RangeTblRef);
+ rtr->rtindex = rtindex;
return (Node *) rtr;
}
else if (IsA(n, RangeSubselect))
{
/* sub-SELECT is like a plain relation */
RangeTblRef *rtr;
+ RangeTblEntry *rte;
+ int rtindex;
- rtr = transformRangeSubselect(pstate, (RangeSubselect *) n);
- *containedRels = makeListi1(rtr->rtindex);
+ rte = transformRangeSubselect(pstate, (RangeSubselect *) n);
+ /* assume new rte is at end */
+ rtindex = list_length(pstate->p_rtable);
+ Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
+ *top_rte = rte;
+ *top_rti = rtindex;
+ *relnamespace = list_make1(rte);
+ *containedRels = bms_make_singleton(rtindex);
+ rtr = makeNode(RangeTblRef);
+ rtr->rtindex = rtindex;
return (Node *) rtr;
}
else if (IsA(n, RangeFunction))
{
/* function is like a plain relation */
RangeTblRef *rtr;
+ RangeTblEntry *rte;
+ int rtindex;
- rtr = transformRangeFunction(pstate, (RangeFunction *) n);
- *containedRels = makeListi1(rtr->rtindex);
+ rte = transformRangeFunction(pstate, (RangeFunction *) n);
+ /* assume new rte is at end */
+ rtindex = list_length(pstate->p_rtable);
+ Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
+ *top_rte = rte;
+ *top_rti = rtindex;
+ *relnamespace = list_make1(rte);
+ *containedRels = bms_make_singleton(rtindex);
+ rtr = makeNode(RangeTblRef);
+ rtr->rtindex = rtindex;
return (Node *) rtr;
}
else if (IsA(n, JoinExpr))
{
/* A newfangled join expression */
JoinExpr *j = (JoinExpr *) n;
- List *my_containedRels,
- *l_containedRels,
- *r_containedRels,
+ RangeTblEntry *l_rte;
+ RangeTblEntry *r_rte;
+ int l_rtindex;
+ int r_rtindex;
+ Relids l_containedRels,
+ r_containedRels,
+ my_containedRels;
+ List *l_relnamespace,
+ *r_relnamespace,
+ *my_relnamespace,
*l_colnames,
*r_colnames,
*res_colnames,
*l_colvars,
*r_colvars,
*res_colvars;
- Index leftrti,
- rightrti;
RangeTblEntry *rte;
/*
* Recursively process the left and right subtrees
*/
- j->larg = transformFromClauseItem(pstate, j->larg, &l_containedRels);
- j->rarg = transformFromClauseItem(pstate, j->rarg, &r_containedRels);
+ j->larg = transformFromClauseItem(pstate, j->larg,
+ &l_rte,
+ &l_rtindex,
+ &l_relnamespace,
+ &l_containedRels);
+ j->rarg = transformFromClauseItem(pstate, j->rarg,
+ &r_rte,
+ &r_rtindex,
+ &r_relnamespace,
+ &r_containedRels);
/*
- * Generate combined list of relation indexes for possible use by
- * transformJoinOnClause below.
+ * Check for conflicting refnames in left and right subtrees. Must do
+ * this because higher levels will assume I hand back a self-
+ * consistent namespace subtree.
*/
- my_containedRels = nconc(l_containedRels, r_containedRels);
+ checkNameSpaceConflicts(pstate, l_relnamespace, r_relnamespace);
/*
- * Check for conflicting refnames in left and right subtrees. Must
- * do this because higher levels will assume I hand back a self-
- * consistent namespace subtree.
+ * Generate combined relation membership info for possible use by
+ * transformJoinOnClause below.
*/
- checkNameSpaceConflicts(pstate, j->larg, j->rarg);
+ my_relnamespace = list_concat(l_relnamespace, r_relnamespace);
+ my_containedRels = bms_join(l_containedRels, r_containedRels);
+
+ pfree(r_relnamespace); /* free unneeded list header */
/*
* Extract column name and var lists from both subtrees
*
* Note: expandRTE returns new lists, safe for me to modify
*/
- if (IsA(j->larg, RangeTblRef))
- leftrti = ((RangeTblRef *) j->larg)->rtindex;
- else if (IsA(j->larg, JoinExpr))
- leftrti = ((JoinExpr *) j->larg)->rtindex;
- else
- {
- elog(ERROR, "transformFromClauseItem: unexpected subtree type");
- leftrti = 0; /* keep compiler quiet */
- }
- rte = rt_fetch(leftrti, pstate->p_rtable);
- expandRTE(pstate, rte, &l_colnames, &l_colvars);
-
- if (IsA(j->rarg, RangeTblRef))
- rightrti = ((RangeTblRef *) j->rarg)->rtindex;
- else if (IsA(j->rarg, JoinExpr))
- rightrti = ((JoinExpr *) j->rarg)->rtindex;
- else
- {
- elog(ERROR, "transformFromClauseItem: unexpected subtree type");
- rightrti = 0; /* keep compiler quiet */
- }
- rte = rt_fetch(rightrti, pstate->p_rtable);
- expandRTE(pstate, rte, &r_colnames, &r_colvars);
+ expandRTE(l_rte, l_rtindex, 0, false,
+ &l_colnames, &l_colvars);
+ expandRTE(r_rte, r_rtindex, 0, false,
+ &r_colnames, &r_colvars);
/*
* Natural join does not explicitly specify columns; must generate
- * columns to join. Need to run through the list of columns from
- * each table or join result and match up the column names. Use
- * the first table, and check every column in the second table for
- * a match. (We'll check that the matches were unique later on.)
- * The result of this step is a list of column names just like an
- * explicitly-written USING list.
+ * columns to join. Need to run through the list of columns from each
+ * table or join result and match up the column names. Use the first
+ * table, and check every column in the second table for a match.
+ * (We'll check that the matches were unique later on.) The result of
+ * this step is a list of column names just like an explicitly-written
+ * USING list.
*/
if (j->isNatural)
{
List *rlist = NIL;
- List *lx,
+ ListCell *lx,
*rx;
Assert(j->using == NIL); /* shouldn't have USING() too */
if (j->using)
{
/*
- * JOIN/USING (or NATURAL JOIN, as transformed above).
- * Transform the list into an explicit ON-condition, and
- * generate a list of merged result columns.
+ * JOIN/USING (or NATURAL JOIN, as transformed above). Transform
+ * the list into an explicit ON-condition, and generate a list of
+ * merged result columns.
*/
List *ucols = j->using;
List *l_usingvars = NIL;
List *r_usingvars = NIL;
- List *ucol;
+ ListCell *ucol;
Assert(j->quals == NULL); /* shouldn't have ON() too */
foreach(ucol, ucols)
{
char *u_colname = strVal(lfirst(ucol));
- List *col;
+ ListCell *col;
int ndx;
int l_index = -1;
int r_index = -1;
char *res_colname = strVal(lfirst(col));
if (strcmp(res_colname, u_colname) == 0)
- elog(ERROR, "USING column name \"%s\" appears more than once", u_colname);
+ ereport(ERROR,
+ (errcode(ERRCODE_DUPLICATE_COLUMN),
+ errmsg("column name \"%s\" appears more than once in USING clause",
+ u_colname)));
}
/* Find it in left input */
if (strcmp(l_colname, u_colname) == 0)
{
if (l_index >= 0)
- elog(ERROR, "Common column name \"%s\" appears more than once in left table", u_colname);
+ ereport(ERROR,
+ (errcode(ERRCODE_AMBIGUOUS_COLUMN),
+ errmsg("common column name \"%s\" appears more than once in left table",
+ u_colname)));
l_index = ndx;
}
ndx++;
}
if (l_index < 0)
- elog(ERROR, "JOIN/USING column \"%s\" not found in left table",
- u_colname);
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_COLUMN),
+ errmsg("column \"%s\" specified in USING clause does not exist in left table",
+ u_colname)));
/* Find it in right input */
ndx = 0;
if (strcmp(r_colname, u_colname) == 0)
{
if (r_index >= 0)
- elog(ERROR, "Common column name \"%s\" appears more than once in right table", u_colname);
+ ereport(ERROR,
+ (errcode(ERRCODE_AMBIGUOUS_COLUMN),
+ errmsg("common column name \"%s\" appears more than once in right table",
+ u_colname)));
r_index = ndx;
}
ndx++;
}
if (r_index < 0)
- elog(ERROR, "JOIN/USING column \"%s\" not found in right table",
- u_colname);
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_COLUMN),
+ errmsg("column \"%s\" specified in USING clause does not exist in right table",
+ u_colname)));
- l_colvar = nth(l_index, l_colvars);
+ l_colvar = list_nth(l_colvars, l_index);
l_usingvars = lappend(l_usingvars, l_colvar);
- r_colvar = nth(r_index, r_colvars);
+ r_colvar = list_nth(r_colvars, r_index);
r_usingvars = lappend(r_usingvars, r_colvar);
res_colnames = lappend(res_colnames, lfirst(ucol));
res_colvars = lappend(res_colvars,
- buildMergedJoinVar(j->jointype,
+ buildMergedJoinVar(pstate,
+ j->jointype,
l_colvar,
r_colvar));
}
else if (j->quals)
{
/* User-written ON-condition; transform it */
- j->quals = transformJoinOnClause(pstate, j, my_containedRels);
+ j->quals = transformJoinOnClause(pstate, j,
+ l_rte, r_rte,
+ my_relnamespace,
+ my_containedRels);
}
else
{
extractRemainingColumns(res_colnames,
r_colnames, r_colvars,
&r_colnames, &r_colvars);
- res_colnames = nconc(res_colnames, l_colnames);
- res_colvars = nconc(res_colvars, l_colvars);
- res_colnames = nconc(res_colnames, r_colnames);
- res_colvars = nconc(res_colvars, r_colvars);
+ res_colnames = list_concat(res_colnames, l_colnames);
+ res_colvars = list_concat(res_colvars, l_colvars);
+ res_colnames = list_concat(res_colnames, r_colnames);
+ res_colvars = list_concat(res_colvars, r_colvars);
/*
* Check alias (AS clause), if any.
{
if (j->alias->colnames != NIL)
{
- if (length(j->alias->colnames) > length(res_colnames))
- elog(ERROR, "Column alias list for \"%s\" has too many entries",
- j->alias->aliasname);
+ if (list_length(j->alias->colnames) > list_length(res_colnames))
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("column alias list for \"%s\" has too many entries",
+ j->alias->aliasname)));
}
}
true);
/* assume new rte is at end */
- j->rtindex = length(pstate->p_rtable);
+ j->rtindex = list_length(pstate->p_rtable);
Assert(rte == rt_fetch(j->rtindex, pstate->p_rtable));
+ *top_rte = rte;
+ *top_rti = j->rtindex;
+
/*
- * Include join RTE in returned containedRels list
+ * Prepare returned namespace list. If the JOIN has an alias then it
+ * hides the contained RTEs as far as the relnamespace goes;
+ * otherwise, put the contained RTEs and *not* the JOIN into
+ * relnamespace.
*/
- *containedRels = lconsi(j->rtindex, my_containedRels);
+ if (j->alias)
+ {
+ *relnamespace = list_make1(rte);
+ list_free(my_relnamespace);
+ }
+ else
+ *relnamespace = my_relnamespace;
+
+ /*
+ * Include join RTE in returned containedRels set
+ */
+ *containedRels = bms_add_member(my_containedRels, j->rtindex);
return (Node *) j;
}
else
- elog(ERROR, "transformFromClauseItem: unexpected node (internal error)"
- "\n\t%s", nodeToString(n));
- return NULL; /* can't get here, just keep compiler
- * quiet */
+ elog(ERROR, "unrecognized node type: %d", (int) nodeTag(n));
+ return NULL; /* can't get here, keep compiler quiet */
}
/*
* generate a suitable replacement expression for a merged join column
*/
static Node *
-buildMergedJoinVar(JoinType jointype, Var *l_colvar, Var *r_colvar)
+buildMergedJoinVar(ParseState *pstate, JoinType jointype,
+ Var *l_colvar, Var *r_colvar)
{
Oid outcoltype;
int32 outcoltypmod;
outcoltypmod = l_colvar->vartypmod;
if (outcoltype != r_colvar->vartype)
{
- outcoltype = select_common_type(makeListo2(l_colvar->vartype,
- r_colvar->vartype),
+ outcoltype = select_common_type(list_make2_oid(l_colvar->vartype,
+ r_colvar->vartype),
"JOIN/USING");
outcoltypmod = -1; /* ie, unknown */
}
}
/*
- * Insert coercion functions if needed. Note that a difference in
- * typmod can only happen if input has typmod but outcoltypmod is -1.
- * In that case we insert a RelabelType to clearly mark that result's
- * typmod is not same as input.
+ * Insert coercion functions if needed. Note that a difference in typmod
+ * can only happen if input has typmod but outcoltypmod is -1. In that
+ * case we insert a RelabelType to clearly mark that result's typmod is
+ * not same as input. We never need coerce_type_typmod.
*/
if (l_colvar->vartype != outcoltype)
- l_node = coerce_type((Node *) l_colvar, l_colvar->vartype,
- outcoltype,
+ l_node = coerce_type(pstate, (Node *) l_colvar, l_colvar->vartype,
+ outcoltype, outcoltypmod,
COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
else if (l_colvar->vartypmod != outcoltypmod)
l_node = (Node *) makeRelabelType((Expr *) l_colvar,
l_node = (Node *) l_colvar;
if (r_colvar->vartype != outcoltype)
- r_node = coerce_type((Node *) r_colvar, r_colvar->vartype,
- outcoltype,
+ r_node = coerce_type(pstate, (Node *) r_colvar, r_colvar->vartype,
+ outcoltype, outcoltypmod,
COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
else if (r_colvar->vartypmod != outcoltypmod)
r_node = (Node *) makeRelabelType((Expr *) r_colvar,
case JOIN_FULL:
{
/*
- * Here we must build a COALESCE expression to ensure that
- * the join output is non-null if either input is.
+ * Here we must build a COALESCE expression to ensure that the
+ * join output is non-null if either input is.
*/
- CaseExpr *c = makeNode(CaseExpr);
- CaseWhen *w = makeNode(CaseWhen);
- NullTest *n = makeNode(NullTest);
-
- n->arg = (Expr *) l_node;
- n->nulltesttype = IS_NOT_NULL;
- w->expr = (Expr *) n;
- w->result = (Expr *) l_node;
- c->casetype = outcoltype;
- c->args = makeList1(w);
- c->defresult = (Expr *) r_node;
+ CoalesceExpr *c = makeNode(CoalesceExpr);
+
+ c->coalescetype = outcoltype;
+ c->args = list_make2(l_node, r_node);
res_node = (Node *) c;
break;
}
default:
- elog(ERROR, "buildMergedJoinVar: unexpected jointype %d",
- (int) jointype);
+ elog(ERROR, "unrecognized join type: %d", (int) jointype);
res_node = NULL; /* keep compiler quiet */
break;
}
/*
* transformWhereClause -
- * transforms the qualification and make sure it is of type Boolean
+ * Transform the qualification and make sure it is of type boolean.
+ * Used for WHERE and allied clauses.
+ *
+ * constructName does not affect the semantics, but is used in error messages
+ */
+Node *
+transformWhereClause(ParseState *pstate, Node *clause,
+ const char *constructName)
+{
+ Node *qual;
+
+ if (clause == NULL)
+ return NULL;
+
+ qual = transformExpr(pstate, clause);
+
+ qual = coerce_to_boolean(pstate, qual, constructName);
+
+ return qual;
+}
+
+
+/*
+ * transformLimitClause -
+ * Transform the expression and make sure it is of type bigint.
+ * Used for LIMIT and allied clauses.
+ *
+ * Note: as of Postgres 8.2, LIMIT expressions are expected to yield int8,
+ * rather than int4 as before.
+ *
+ * constructName does not affect the semantics, but is used in error messages
*/
Node *
-transformWhereClause(ParseState *pstate, Node *clause)
+transformLimitClause(ParseState *pstate, Node *clause,
+ const char *constructName)
{
Node *qual;
qual = transformExpr(pstate, clause);
- qual = coerce_to_boolean(qual, "WHERE");
+ qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
+
+ /*
+ * LIMIT can't refer to any vars or aggregates of the current query
+ */
+ if (contain_vars_of_level(qual, 0))
+ {
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
+ /* translator: %s is name of a SQL construct, eg LIMIT */
+ errmsg("argument of %s must not contain variables",
+ constructName)));
+ }
+ if (checkExprHasAggs(qual))
+ {
+ ereport(ERROR,
+ (errcode(ERRCODE_GROUPING_ERROR),
+ /* translator: %s is name of a SQL construct, eg LIMIT */
+ errmsg("argument of %s must not contain aggregates",
+ constructName)));
+ }
return qual;
}
* list as a "resjunk" node.
*
* node the ORDER BY, GROUP BY, or DISTINCT ON expression to be matched
- * tlist the existing target list (NB: this will never be NIL, which is a
- * good thing since we'd be unable to append to it if it were...)
- * clause identifies clause type being processed.
+ * tlist the target list (passed by reference so we can append to it)
+ * clause identifies clause type being processed
*/
static TargetEntry *
-findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
+findTargetlistEntry(ParseState *pstate, Node *node, List **tlist, int clause)
{
TargetEntry *target_result = NULL;
- List *tl;
+ ListCell *tl;
Node *expr;
/*----------
*----------
*/
if (IsA(node, ColumnRef) &&
- length(((ColumnRef *) node)->fields) == 1 &&
- ((ColumnRef *) node)->indirection == NIL)
+ list_length(((ColumnRef *) node)->fields) == 1)
{
- char *name = strVal(lfirst(((ColumnRef *) node)->fields));
+ char *name = strVal(linitial(((ColumnRef *) node)->fields));
+ int location = ((ColumnRef *) node)->location;
if (clause == GROUP_CLAUSE)
{
/*
* In GROUP BY, we must prefer a match against a FROM-clause
- * column to one against the targetlist. Look to see if there
- * is a matching column. If so, fall through to let
- * transformExpr() do the rest. NOTE: if name could refer
- * ambiguously to more than one column name exposed by FROM,
- * colnameToVar will elog(ERROR). That's just what we want
- * here.
+ * column to one against the targetlist. Look to see if there is
+ * a matching column. If so, fall through to let transformExpr()
+ * do the rest. NOTE: if name could refer ambiguously to more
+ * than one column name exposed by FROM, colNameToVar will
+ * ereport(ERROR). That's just what we want here.
+ *
+ * Small tweak for 7.4.3: ignore matches in upper query levels.
+ * This effectively changes the search order for bare names to (1)
+ * local FROM variables, (2) local targetlist aliases, (3) outer
+ * FROM variables, whereas before it was (1) (3) (2). SQL92 and
+ * SQL99 do not allow GROUPing BY an outer reference, so this
+ * breaks no cases that are legal per spec, and it seems a more
+ * self-consistent behavior.
*/
- if (colnameToVar(pstate, name) != NULL)
+ if (colNameToVar(pstate, name, true, location) != NULL)
name = NULL;
}
if (name != NULL)
{
- foreach(tl, tlist)
+ foreach(tl, *tlist)
{
TargetEntry *tle = (TargetEntry *) lfirst(tl);
- Resdom *resnode = tle->resdom;
- if (!resnode->resjunk &&
- strcmp(resnode->resname, name) == 0)
+ if (!tle->resjunk &&
+ strcmp(tle->resname, name) == 0)
{
if (target_result != NULL)
{
if (!equal(target_result->expr, tle->expr))
- elog(ERROR, "%s '%s' is ambiguous",
- clauseText[clause], name);
+ ereport(ERROR,
+ (errcode(ERRCODE_AMBIGUOUS_COLUMN),
+
+ /*------
+ translator: first %s is name of a SQL construct, eg ORDER BY */
+ errmsg("%s \"%s\" is ambiguous",
+ clauseText[clause], name),
+ parser_errposition(pstate, location)));
}
else
target_result = tle;
int target_pos;
if (!IsA(val, Integer))
- elog(ERROR, "Non-integer constant in %s", clauseText[clause]);
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ /* translator: %s is name of a SQL construct, eg ORDER BY */
+ errmsg("non-integer constant in %s",
+ clauseText[clause])));
target_pos = intVal(val);
- foreach(tl, tlist)
+ foreach(tl, *tlist)
{
TargetEntry *tle = (TargetEntry *) lfirst(tl);
- Resdom *resnode = tle->resdom;
- if (!resnode->resjunk)
+ if (!tle->resjunk)
{
if (++targetlist_pos == target_pos)
return tle; /* return the unique match */
}
}
- elog(ERROR, "%s position %d is not in target list",
- clauseText[clause], target_pos);
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
+ /* translator: %s is name of a SQL construct, eg ORDER BY */
+ errmsg("%s position %d is not in select list",
+ clauseText[clause], target_pos)));
}
/*
*/
expr = transformExpr(pstate, node);
- foreach(tl, tlist)
+ foreach(tl, *tlist)
{
TargetEntry *tle = (TargetEntry *) lfirst(tl);
}
/*
- * If no matches, construct a new target entry which is appended to
- * the end of the target list. This target is given resjunk = TRUE so
- * that it will not be projected into the final tuple.
+ * If no matches, construct a new target entry which is appended to the
+ * end of the target list. This target is given resjunk = TRUE so that it
+ * will not be projected into the final tuple.
*/
target_result = transformTargetEntry(pstate, node, expr, NULL, true);
- lappend(tlist, target_result);
+
+ *tlist = lappend(*tlist, target_result);
return target_result;
}
+static GroupClause *
+make_group_clause(TargetEntry *tle, List *targetlist,
+ Oid sortop, bool nulls_first)
+{
+ GroupClause *result;
+
+ result = makeNode(GroupClause);
+ result->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
+ result->sortop = sortop;
+ result->nulls_first = nulls_first;
+ return result;
+}
/*
* transformGroupClause -
* transform a GROUP BY clause
+ *
+ * GROUP BY items will be added to the targetlist (as resjunk columns)
+ * if not already present, so the targetlist must be passed by reference.
+ *
+ * The order of the elements of the grouping clause does not affect
+ * the semantics of the query. However, the optimizer is not currently
+ * smart enough to reorder the grouping clause, so we try to do some
+ * primitive reordering here.
*/
List *
-transformGroupClause(ParseState *pstate, List *grouplist, List *targetlist)
+transformGroupClause(ParseState *pstate, List *grouplist,
+ List **targetlist, List *sortClause)
{
- List *glist = NIL,
- *gl;
+ List *result = NIL;
+ List *tle_list = NIL;
+ ListCell *l;
- foreach(gl, grouplist)
+ /* Preprocess the grouping clause, lookup TLEs */
+ foreach(l, grouplist)
{
TargetEntry *tle;
+ Oid restype;
- tle = findTargetlistEntry(pstate, lfirst(gl),
+ tle = findTargetlistEntry(pstate, lfirst(l),
targetlist, GROUP_CLAUSE);
- /* avoid making duplicate grouplist entries */
- if (!targetIsInSortList(tle, glist))
+ /* if tlist item is an UNKNOWN literal, change it to TEXT */
+ restype = exprType((Node *) tle->expr);
+
+ if (restype == UNKNOWNOID)
+ tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
+ restype, TEXTOID, -1,
+ COERCION_IMPLICIT,
+ COERCE_IMPLICIT_CAST);
+
+ tle_list = lappend(tle_list, tle);
+ }
+
+ /*
+ * Now iterate through the ORDER BY clause. If we find a grouping element
+ * that matches the ORDER BY element, append the grouping element to the
+ * result set immediately. Otherwise, stop iterating. The effect of this
+ * is to look for a prefix of the ORDER BY list in the grouping clauses,
+ * and to move that prefix to the front of the GROUP BY.
+ */
+ foreach(l, sortClause)
+ {
+ SortClause *sc = (SortClause *) lfirst(l);
+ ListCell *prev = NULL;
+ ListCell *tl;
+ bool found = false;
+
+ foreach(tl, tle_list)
{
- GroupClause *grpcl = makeNode(GroupClause);
+ TargetEntry *tle = (TargetEntry *) lfirst(tl);
+
+ if (sc->tleSortGroupRef == tle->ressortgroupref)
+ {
+ GroupClause *gc;
- grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
+ tle_list = list_delete_cell(tle_list, tl, prev);
- grpcl->sortop = ordering_oper_opid(tle->resdom->restype);
+ /* Use the sort clause's sorting information */
+ gc = make_group_clause(tle, *targetlist,
+ sc->sortop, sc->nulls_first);
+ result = lappend(result, gc);
+ found = true;
+ break;
+ }
- glist = lappend(glist, grpcl);
+ prev = tl;
}
+
+ /* As soon as we've failed to match an ORDER BY element, stop */
+ if (!found)
+ break;
+ }
+
+ /*
+ * Now add any remaining elements of the GROUP BY list in the order we
+ * received them.
+ *
+ * XXX: are there any additional criteria to consider when ordering
+ * grouping clauses?
+ */
+ foreach(l, tle_list)
+ {
+ TargetEntry *tle = (TargetEntry *) lfirst(l);
+ GroupClause *gc;
+ Oid sort_op;
+
+ /*
+ * Avoid making duplicate grouplist entries. Note that we don't
+ * enforce a particular sortop here. Along with the copying of sort
+ * information above, this means that if you write something like
+ * "GROUP BY foo ORDER BY foo USING <<<", the GROUP BY operation
+ * silently takes on the equality semantics implied by the ORDER BY.
+ */
+ if (targetIsInSortList(tle, InvalidOid, result))
+ continue;
+
+ sort_op = ordering_oper_opid(exprType((Node *) tle->expr));
+ gc = make_group_clause(tle, *targetlist, sort_op, false);
+ result = lappend(result, gc);
}
- return glist;
+ list_free(tle_list);
+ return result;
}
/*
* transformSortClause -
* transform an ORDER BY clause
+ *
+ * ORDER BY items will be added to the targetlist (as resjunk columns)
+ * if not already present, so the targetlist must be passed by reference.
*/
List *
transformSortClause(ParseState *pstate,
List *orderlist,
- List *targetlist)
+ List **targetlist,
+ bool resolveUnknown)
{
List *sortlist = NIL;
- List *olitem;
+ ListCell *olitem;
foreach(olitem, orderlist)
{
- SortGroupBy *sortby = lfirst(olitem);
+ SortBy *sortby = lfirst(olitem);
TargetEntry *tle;
tle = findTargetlistEntry(pstate, sortby->node,
targetlist, ORDER_CLAUSE);
- sortlist = addTargetToSortList(tle, sortlist, targetlist,
- sortby->useOp);
+ sortlist = addTargetToSortList(pstate, tle,
+ sortlist, *targetlist,
+ sortby->sortby_dir,
+ sortby->sortby_nulls,
+ sortby->useOp,
+ resolveUnknown);
}
return sortlist;
* transformDistinctClause -
* transform a DISTINCT or DISTINCT ON clause
*
- * Since we may need to add items to the query's sortClause list, that list
- * is passed by reference. We might also need to add items to the query's
- * targetlist, but we assume that cannot be empty initially, so we can
- * lappend to it even though the pointer is passed by value.
+ * Since we may need to add items to the query's targetlist, that list
+ * is passed by reference.
+ *
+ * As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
+ * possible into the distinctClause. This avoids a possible need to re-sort,
+ * and allows the user to determine the equality semantics used by DISTINCT,
+ * should she be working with a datatype that has more than one btree equality
+ * operator.
*/
List *
transformDistinctClause(ParseState *pstate, List *distinctlist,
- List *targetlist, List **sortClause)
+ List **targetlist, List *sortClause)
{
List *result = NIL;
- List *slitem;
- List *dlitem;
+ ListCell *slitem;
+ ListCell *dlitem;
+ ListCell *tlitem;
/* No work if there was no DISTINCT clause */
if (distinctlist == NIL)
return NIL;
- if (lfirst(distinctlist) == NIL)
+ if (linitial(distinctlist) == NULL)
{
/* We had SELECT DISTINCT */
/*
- * All non-resjunk elements from target list that are not already
- * in the sort list should be added to it. (We don't really care
- * what order the DISTINCT fields are checked in, so we can leave
- * the user's ORDER BY spec alone, and just add additional sort
- * keys to it to ensure that all targetlist items get sorted.)
- */
- *sortClause = addAllTargetsToSortList(*sortClause, targetlist);
-
- /*
- * Now, DISTINCT list consists of all non-resjunk sortlist items.
- * Actually, all the sortlist items had better be non-resjunk!
- * Otherwise, user wrote SELECT DISTINCT with an ORDER BY item
- * that does not appear anywhere in the SELECT targetlist, and we
- * can't implement that with only one sorting pass...
+ * The distinctClause should consist of all ORDER BY items followed
+ * by all other non-resjunk targetlist items. There must not be any
+ * resjunk ORDER BY items --- that would imply that we are sorting
+ * by a value that isn't necessarily unique within a DISTINCT group,
+ * so the results wouldn't be well-defined. This construction
+ * ensures we follow the rule that sortClause and distinctClause match;
+ * in fact the sortClause will always be a prefix of distinctClause.
+ *
+ * Note a corner case: the same TLE could be in the ORDER BY list
+ * multiple times with different sortops. We have to include it in
+ * the distinctClause the same way to preserve the prefix property.
+ * The net effect will be that the TLE value will be made unique
+ * according to both sortops.
*/
- foreach(slitem, *sortClause)
+ foreach(slitem, sortClause)
{
SortClause *scl = (SortClause *) lfirst(slitem);
- TargetEntry *tle = get_sortgroupclause_tle(scl, targetlist);
+ TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
- if (tle->resdom->resjunk)
- elog(ERROR, "For SELECT DISTINCT, ORDER BY expressions must appear in target list");
+ if (tle->resjunk)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
+ errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list")));
else
result = lappend(result, copyObject(scl));
}
+
+ /*
+ * Now add any remaining non-resjunk tlist items, using default
+ * sorting semantics for their data types.
+ */
+ foreach(tlitem, *targetlist)
+ {
+ TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
+
+ if (tle->resjunk)
+ continue; /* ignore junk */
+ if (targetIsInSortList(tle, InvalidOid, result))
+ continue; /* already in list (with some semantics) */
+ result = addTargetToSortList(pstate, tle,
+ result, *targetlist,
+ SORTBY_DEFAULT,
+ SORTBY_NULLS_DEFAULT,
+ NIL, true);
+ }
}
else
{
/* We had SELECT DISTINCT ON (expr, ...) */
+ Bitmapset *refnos = NULL;
+ int sortgroupref;
+ bool skipped_sortitem;
/*
- * If the user writes both DISTINCT ON and ORDER BY, then the two
- * expression lists must match (until one or the other runs out).
- * Otherwise the ORDER BY requires a different sort order than the
- * DISTINCT does, and we can't implement that with only one sort
- * pass (and if we do two passes, the results will be rather
- * unpredictable). However, it's OK to have more DISTINCT ON
- * expressions than ORDER BY expressions; we can just add the
- * extra DISTINCT values to the sort list, much as we did above
- * for ordinary DISTINCT fields.
- *
- * Actually, it'd be OK for the common prefixes of the two lists to
- * match in any order, but implementing that check seems like more
- * trouble than it's worth.
+ * Add all the DISTINCT ON expressions to the tlist (if not already
+ * present, they are added as resjunk items). Assign sortgroupref
+ * numbers to them, and form a bitmapset of these numbers. (A
+ * bitmapset is convenient here because we don't care about order
+ * and we can discard duplicates.)
*/
- List *nextsortlist = *sortClause;
-
foreach(dlitem, distinctlist)
{
+ Node *dexpr = (Node *) lfirst(dlitem);
TargetEntry *tle;
- tle = findTargetlistEntry(pstate, lfirst(dlitem),
+ tle = findTargetlistEntry(pstate, dexpr,
targetlist, DISTINCT_ON_CLAUSE);
+ sortgroupref = assignSortGroupRef(tle, *targetlist);
+ refnos = bms_add_member(refnos, sortgroupref);
+ }
- if (nextsortlist != NIL)
- {
- SortClause *scl = (SortClause *) lfirst(nextsortlist);
+ /*
+ * If the user writes both DISTINCT ON and ORDER BY, adopt the
+ * sorting semantics from ORDER BY items that match DISTINCT ON
+ * items, and also adopt their column sort order. We insist that
+ * the distinctClause and sortClause match, so throw error if we
+ * find the need to add any more distinctClause items after we've
+ * skipped an ORDER BY item that wasn't in DISTINCT ON.
+ */
+ skipped_sortitem = false;
+ foreach(slitem, sortClause)
+ {
+ SortClause *scl = (SortClause *) lfirst(slitem);
- if (tle->resdom->ressortgroupref != scl->tleSortGroupRef)
- elog(ERROR, "SELECT DISTINCT ON expressions must match initial ORDER BY expressions");
- result = lappend(result, copyObject(scl));
- nextsortlist = lnext(nextsortlist);
+ if (bms_is_member(scl->tleSortGroupRef, refnos))
+ {
+ if (skipped_sortitem)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
+ errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions")));
+ else
+ result = lappend(result, copyObject(scl));
}
else
{
- *sortClause = addTargetToSortList(tle, *sortClause,
- targetlist, NIL);
-
- /*
- * Probably, the tle should always have been added at the
- * end of the sort list ... but search to be safe.
- */
- foreach(slitem, *sortClause)
- {
- SortClause *scl = (SortClause *) lfirst(slitem);
-
- if (tle->resdom->ressortgroupref == scl->tleSortGroupRef)
- {
- result = lappend(result, copyObject(scl));
- break;
- }
- }
- if (slitem == NIL)
- elog(ERROR, "transformDistinctClause: failed to add DISTINCT ON clause to target list");
+ skipped_sortitem = true;
}
}
+
+ /*
+ * Now add any remaining DISTINCT ON items, using default sorting
+ * semantics for their data types. (Note: this is pretty
+ * questionable; if the ORDER BY list doesn't include all the DISTINCT
+ * ON items and more besides, you certainly aren't using DISTINCT ON
+ * in the intended way, and you probably aren't going to get
+ * consistent results. It might be better to throw an error or warning
+ * here. But historically we've allowed it, so keep doing so.)
+ */
+ while ((sortgroupref = bms_first_member(refnos)) >= 0)
+ {
+ TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
+
+ if (targetIsInSortList(tle, InvalidOid, result))
+ continue; /* already in list (with some semantics) */
+ if (skipped_sortitem)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
+ errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions")));
+ result = addTargetToSortList(pstate, tle,
+ result, *targetlist,
+ SORTBY_DEFAULT,
+ SORTBY_NULLS_DEFAULT,
+ NIL, true);
+ }
}
return result;
}
/*
- * addAllTargetsToSortList
- * Make sure all non-resjunk targets in the targetlist are in the
- * ORDER BY list, adding the not-yet-sorted ones to the end of the list.
- * This is typically used to help implement SELECT DISTINCT.
+ * addTargetToSortList
+ * If the given targetlist entry isn't already in the SortClause list,
+ * add it to the end of the list, using the given sort ordering info.
*
- * Returns the updated ORDER BY list.
+ * If resolveUnknown is TRUE, convert TLEs of type UNKNOWN to TEXT. If not,
+ * do nothing (which implies the search for a sort operator will fail).
+ * pstate should be provided if resolveUnknown is TRUE, but can be NULL
+ * otherwise.
+ *
+ * Returns the updated SortClause list.
*/
List *
-addAllTargetsToSortList(List *sortlist, List *targetlist)
+addTargetToSortList(ParseState *pstate, TargetEntry *tle,
+ List *sortlist, List *targetlist,
+ SortByDir sortby_dir, SortByNulls sortby_nulls,
+ List *sortby_opname, bool resolveUnknown)
{
- List *i;
+ Oid restype = exprType((Node *) tle->expr);
+ Oid sortop;
+ Oid cmpfunc;
+ bool reverse;
- foreach(i, targetlist)
+ /* if tlist item is an UNKNOWN literal, change it to TEXT */
+ if (restype == UNKNOWNOID && resolveUnknown)
{
- TargetEntry *tle = (TargetEntry *) lfirst(i);
+ tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
+ restype, TEXTOID, -1,
+ COERCION_IMPLICIT,
+ COERCE_IMPLICIT_CAST);
+ restype = TEXTOID;
+ }
- if (!tle->resdom->resjunk)
- sortlist = addTargetToSortList(tle, sortlist, targetlist, NIL);
+ /* determine the sortop */
+ switch (sortby_dir)
+ {
+ case SORTBY_DEFAULT:
+ case SORTBY_ASC:
+ sortop = ordering_oper_opid(restype);
+ reverse = false;
+ break;
+ case SORTBY_DESC:
+ sortop = reverse_ordering_oper_opid(restype);
+ reverse = true;
+ break;
+ case SORTBY_USING:
+ Assert(sortby_opname != NIL);
+ sortop = compatible_oper_opid(sortby_opname,
+ restype,
+ restype,
+ false);
+
+ /*
+ * Verify it's a valid ordering operator, and determine whether to
+ * consider it like ASC or DESC.
+ */
+ if (!get_compare_function_for_ordering_op(sortop,
+ &cmpfunc, &reverse))
+ ereport(ERROR,
+ (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+ errmsg("operator %s is not a valid ordering operator",
+ strVal(llast(sortby_opname))),
+ errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
+ break;
+ default:
+ elog(ERROR, "unrecognized sortby_dir: %d", sortby_dir);
+ sortop = InvalidOid; /* keep compiler quiet */
+ reverse = false;
+ break;
}
- return sortlist;
-}
-/*
- * addTargetToSortList
- * If the given targetlist entry isn't already in the ORDER BY list,
- * add it to the end of the list, using the sortop with given name
- * or the default sort operator if opname == NIL.
- *
- * Returns the updated ORDER BY list.
- */
-static List *
-addTargetToSortList(TargetEntry *tle, List *sortlist, List *targetlist,
- List *opname)
-{
/* avoid making duplicate sortlist entries */
- if (!targetIsInSortList(tle, sortlist))
+ if (!targetIsInSortList(tle, sortop, sortlist))
{
SortClause *sortcl = makeNode(SortClause);
sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
- if (opname)
- sortcl->sortop = compatible_oper_opid(opname,
- tle->resdom->restype,
- tle->resdom->restype,
- false);
- else
- sortcl->sortop = ordering_oper_opid(tle->resdom->restype);
+ sortcl->sortop = sortop;
+
+ switch (sortby_nulls)
+ {
+ case SORTBY_NULLS_DEFAULT:
+ /* NULLS FIRST is default for DESC; other way for ASC */
+ sortcl->nulls_first = reverse;
+ break;
+ case SORTBY_NULLS_FIRST:
+ sortcl->nulls_first = true;
+ break;
+ case SORTBY_NULLS_LAST:
+ sortcl->nulls_first = false;
+ break;
+ default:
+ elog(ERROR, "unrecognized sortby_nulls: %d", sortby_nulls);
+ break;
+ }
sortlist = lappend(sortlist, sortcl);
}
+
return sortlist;
}
assignSortGroupRef(TargetEntry *tle, List *tlist)
{
Index maxRef;
- List *l;
+ ListCell *l;
- if (tle->resdom->ressortgroupref) /* already has one? */
- return tle->resdom->ressortgroupref;
+ if (tle->ressortgroupref) /* already has one? */
+ return tle->ressortgroupref;
/* easiest way to pick an unused refnumber: max used + 1 */
maxRef = 0;
foreach(l, tlist)
{
- Index ref = ((TargetEntry *) lfirst(l))->resdom->ressortgroupref;
+ Index ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
if (ref > maxRef)
maxRef = ref;
}
- tle->resdom->ressortgroupref = maxRef + 1;
- return tle->resdom->ressortgroupref;
+ tle->ressortgroupref = maxRef + 1;
+ return tle->ressortgroupref;
}
/*
* targetIsInSortList
* Is the given target item already in the sortlist?
+ * If sortop is not InvalidOid, also test for a match to the sortop.
+ *
+ * It is not an oversight that this function ignores the nulls_first flag.
+ * We check sortop when determining if an ORDER BY item is redundant with
+ * earlier ORDER BY items, because it's conceivable that "ORDER BY
+ * foo USING <, foo USING <<<" is not redundant, if <<< distinguishes
+ * values that < considers equal. We need not check nulls_first
+ * however, because a lower-order column with the same sortop but
+ * opposite nulls direction is redundant. Also, we can consider
+ * ORDER BY foo ASC, foo DESC redundant, so check for a commutator match.
*
* Works for both SortClause and GroupClause lists. Note that the main
* reason we need this routine (and not just a quick test for nonzeroness
* of ressortgroupref) is that a TLE might be in only one of the lists.
*/
-static bool
-targetIsInSortList(TargetEntry *tle, List *sortList)
+bool
+targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
{
- Index ref = tle->resdom->ressortgroupref;
- List *i;
+ Index ref = tle->ressortgroupref;
+ ListCell *l;
/* no need to scan list if tle has no marker */
if (ref == 0)
return false;
- foreach(i, sortList)
+ foreach(l, sortList)
{
- SortClause *scl = (SortClause *) lfirst(i);
+ SortClause *scl = (SortClause *) lfirst(l);
- if (scl->tleSortGroupRef == ref)
+ if (scl->tleSortGroupRef == ref &&
+ (sortop == InvalidOid ||
+ sortop == scl->sortop ||
+ sortop == get_commutator(scl->sortop)))
return true;
}
return false;