summaryrefslogtreecommitdiff
path: root/src/backend/parser
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/parser')
-rw-r--r--src/backend/parser/parse_agg.c7
-rw-r--r--src/backend/parser/parse_relation.c39
2 files changed, 40 insertions, 6 deletions
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 955be022e4..c3ac417365 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/parser/parse_agg.c,v 1.40 2000/09/12 21:07:02 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/parser/parse_agg.c,v 1.41 2000/09/25 18:14:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -113,10 +113,7 @@ check_ungrouped_columns_walker(Node *node,
Assert(var->varno > 0 &&
(int) var->varno <= length(context->pstate->p_rtable));
rte = rt_fetch(var->varno, context->pstate->p_rtable);
- attname = get_attname(rte->relid, var->varattno);
- if (!attname)
- elog(ERROR, "cache lookup of attribute %d in relation %u failed",
- var->varattno, rte->relid);
+ attname = get_rte_attribute_name(rte, var->varattno);
elog(ERROR, "Attribute %s.%s must be GROUPed or used in an aggregate function",
rte->eref->relname, attname);
}
diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c
index 491cbc5ef0..baae0a578c 100644
--- a/src/backend/parser/parse_relation.c
+++ b/src/backend/parser/parse_relation.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/parser/parse_relation.c,v 1.47 2000/09/12 21:07:02 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/parser/parse_relation.c,v 1.48 2000/09/25 18:14:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -713,6 +713,43 @@ expandNamesVars(ParseState *pstate, List *names, List *vars)
return te_list;
}
+/* ----------
+ * get_rte_attribute_name
+ * Get an attribute name from a RangeTblEntry
+ *
+ * This is unlike get_attname() because we use aliases if available.
+ * In particular, it will work on an RTE for a subselect, whereas
+ * get_attname() only works on real relations.
+ * ----------
+ */
+char *
+get_rte_attribute_name(RangeTblEntry *rte, AttrNumber attnum)
+{
+ char *attname;
+
+ /*
+ * If there is an alias, use it
+ */
+ if (attnum > 0 && attnum <= length(rte->eref->attrs))
+ return strVal(nth(attnum-1, rte->eref->attrs));
+ /*
+ * Can get here for a system attribute (which never has an alias),
+ * or if alias name list is too short (which probably can't happen
+ * anymore). Neither of these cases is valid for a subselect RTE.
+ */
+ if (rte->relid == InvalidOid)
+ elog(ERROR, "Invalid attnum %d for rangetable entry %s",
+ attnum, rte->eref->relname);
+ /*
+ * Use the real name of the table's column
+ */
+ attname = get_attname(rte->relid, attnum);
+ if (attname == NULL)
+ elog(ERROR, "cache lookup of attribute %d in relation %u failed",
+ attnum, rte->relid);
+ return attname;
+}
+
/*
* given relation and att name, return id of variable
*