aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/ruleutils.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2004-08-19 20:57:41 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2004-08-19 20:57:41 +0000
commitbbd6eb5b958ef38f786089fd4a03d650d4b7220e (patch)
tree3dbc25578ccd347b586018f6b8a6d8d6baa84ac7 /src/backend/utils/adt/ruleutils.c
parent040450beef4f3fdafaa9e20dbb0ee3e00c5856ba (diff)
downloadpostgresql-bbd6eb5b958ef38f786089fd4a03d650d4b7220e.tar.gz
postgresql-bbd6eb5b958ef38f786089fd4a03d650d4b7220e.zip
Repair some issues with column aliases and RowExpr construction in the
presence of dropped columns. Document the already-presumed fact that eref aliases in relation RTEs are supposed to have entries for dropped columns; cause the user alias structs to have such entries too, so that there's always a one-to-one mapping to the underlying physical attnums. Adjust expandRTE() and related code to handle the case where a column that is part of a JOIN has been dropped. Generalize expandRTE()'s API so that it can be used in a couple of places that formerly rolled their own implementation of the same logic. Fix ruleutils.c to suppress display of aliases for columns that were dropped since the rule was made.
Diffstat (limited to 'src/backend/utils/adt/ruleutils.c')
-rw-r--r--src/backend/utils/adt/ruleutils.c72
1 files changed, 43 insertions, 29 deletions
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 8e1420d9267..52089d22118 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -3,7 +3,7 @@
* back to source text
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.177 2004/08/17 18:47:09 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.178 2004/08/19 20:57:41 tgl Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
@@ -203,6 +203,8 @@ static void get_sublink_expr(SubLink *sublink, deparse_context *context);
static void get_from_clause(Query *query, deparse_context *context);
static void get_from_clause_item(Node *jtnode, Query *query,
deparse_context *context);
+static void get_from_clause_alias(Alias *alias, int varno,
+ Query *query, deparse_context *context);
static void get_from_clause_coldeflist(List *coldeflist,
deparse_context *context);
static void get_opclass_name(Oid opclass, Oid actual_datatype,
@@ -3962,20 +3964,8 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
appendStringInfo(buf, " %s",
quote_identifier(rte->alias->aliasname));
gavealias = true;
- if (rte->alias->colnames != NIL && coldeflist == NIL)
- {
- ListCell *col;
-
- appendStringInfoChar(buf, '(');
- foreach(col, rte->alias->colnames)
- {
- if (col != list_head(rte->alias->colnames))
- appendStringInfo(buf, ", ");
- appendStringInfoString(buf,
- quote_identifier(strVal(lfirst(col))));
- }
- appendStringInfoChar(buf, ')');
- }
+ if (coldeflist == NIL)
+ get_from_clause_alias(rte->alias, varno, query, context);
}
else if (rte->rtekind == RTE_RELATION &&
strcmp(rte->eref->aliasname, get_rel_name(rte->relid)) != 0)
@@ -4128,20 +4118,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
{
appendStringInfo(buf, " %s",
quote_identifier(j->alias->aliasname));
- if (j->alias->colnames != NIL)
- {
- ListCell *col;
-
- appendStringInfoChar(buf, '(');
- foreach(col, j->alias->colnames)
- {
- if (col != list_head(j->alias->colnames))
- appendStringInfo(buf, ", ");
- appendStringInfoString(buf,
- quote_identifier(strVal(lfirst(col))));
- }
- appendStringInfoChar(buf, ')');
- }
+ get_from_clause_alias(j->alias, j->rtindex, query, context);
}
}
else
@@ -4150,6 +4127,43 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
}
/*
+ * get_from_clause_alias - reproduce column alias list
+ *
+ * This is tricky because we must ignore dropped columns.
+ */
+static void
+get_from_clause_alias(Alias *alias, int varno,
+ Query *query, deparse_context *context)
+{
+ StringInfo buf = context->buf;
+ ListCell *col;
+ AttrNumber attnum;
+ bool first = true;
+
+ if (alias == NULL || alias->colnames == NIL)
+ return; /* definitely nothing to do */
+
+ attnum = 0;
+ foreach(col, alias->colnames)
+ {
+ attnum++;
+ if (get_rte_attribute_is_dropped(query->rtable, varno, attnum))
+ continue;
+ if (first)
+ {
+ appendStringInfoChar(buf, '(');
+ first = false;
+ }
+ else
+ appendStringInfo(buf, ", ");
+ appendStringInfoString(buf,
+ quote_identifier(strVal(lfirst(col))));
+ }
+ if (!first)
+ appendStringInfoChar(buf, ')');
+}
+
+/*
* get_from_clause_coldeflist - reproduce FROM clause coldeflist
*
* The coldeflist is appended immediately (no space) to buf. Caller is