summaryrefslogtreecommitdiff
path: root/src/backend/parser/parse_relation.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2017-09-06 10:41:05 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2017-09-06 10:41:05 -0400
commit483882905a9a5dc72c9487ceee12320b9630ba2b (patch)
treee1e3093c66dd2cac5d53236bcfd8982da234105f /src/backend/parser/parse_relation.c
parent3fbf09563f839137e5279a390044a18e400fa074 (diff)
Clean up handling of dropped columns in NAMEDTUPLESTORE RTEs.
The NAMEDTUPLESTORE patch piggybacked on the infrastructure for TABLEFUNC/VALUES/CTE RTEs, none of which can ever have dropped columns, so the possibility was ignored most places. Fix that, including adding a specification to parsenodes.h about what it's supposed to look like. In passing, clean up assorted comments that hadn't been maintained properly by said patch. Per bug #14799 from Philippe Beaudoin. Back-patch to v10. Discussion: https://postgr.es/m/20170906120005.25630.84360@wrigleys.postgresql.org
Diffstat (limited to 'src/backend/parser/parse_relation.c')
-rw-r--r--src/backend/parser/parse_relation.c103
1 files changed, 68 insertions, 35 deletions
diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c
index 684a50d3df6..a0ad01104a8 100644
--- a/src/backend/parser/parse_relation.c
+++ b/src/backend/parser/parse_relation.c
@@ -2014,11 +2014,13 @@ addRangeTableEntryForENR(ParseState *pstate,
/*
* Build the list of effective column names using user-supplied aliases
- * and/or actual column names. Also build the cannibalized fields.
+ * and/or actual column names.
*/
tupdesc = ENRMetadataGetTupDesc(enrmd);
rte->eref = makeAlias(refname, NIL);
buildRelationAliases(tupdesc, alias, rte->eref);
+
+ /* Record additional data for ENR, including column type info */
rte->enrname = enrmd->name;
rte->enrtuples = enrmd->enrtuples;
rte->coltypes = NIL;
@@ -2026,19 +2028,26 @@ addRangeTableEntryForENR(ParseState *pstate,
rte->colcollations = NIL;
for (attno = 1; attno <= tupdesc->natts; ++attno)
{
- if (tupdesc->attrs[attno - 1]->atttypid == InvalidOid &&
- !(tupdesc->attrs[attno - 1]->attisdropped))
- elog(ERROR, "atttypid was invalid for column which has not been dropped from \"%s\"",
- rv->relname);
- rte->coltypes =
- lappend_oid(rte->coltypes,
- tupdesc->attrs[attno - 1]->atttypid);
- rte->coltypmods =
- lappend_int(rte->coltypmods,
- tupdesc->attrs[attno - 1]->atttypmod);
- rte->colcollations =
- lappend_oid(rte->colcollations,
- tupdesc->attrs[attno - 1]->attcollation);
+ Form_pg_attribute att = TupleDescAttr(tupdesc, attno - 1);
+
+ if (att->attisdropped)
+ {
+ /* Record zeroes for a dropped column */
+ rte->coltypes = lappend_oid(rte->coltypes, InvalidOid);
+ rte->coltypmods = lappend_int(rte->coltypmods, 0);
+ rte->colcollations = lappend_oid(rte->colcollations, InvalidOid);
+ }
+ else
+ {
+ /* Let's just make sure we can tell this isn't dropped */
+ if (att->atttypid == InvalidOid)
+ elog(ERROR, "atttypid is invalid for non-dropped column in \"%s\"",
+ rv->relname);
+ rte->coltypes = lappend_oid(rte->coltypes, att->atttypid);
+ rte->coltypmods = lappend_int(rte->coltypmods, att->atttypmod);
+ rte->colcollations = lappend_oid(rte->colcollations,
+ att->attcollation);
+ }
}
/*
@@ -2417,7 +2426,7 @@ expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
case RTE_CTE:
case RTE_NAMEDTUPLESTORE:
{
- /* Tablefunc, Values or CTE RTE */
+ /* Tablefunc, Values, CTE, or ENR RTE */
ListCell *aliasp_item = list_head(rte->eref->colnames);
ListCell *lct;
ListCell *lcm;
@@ -2437,23 +2446,43 @@ expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
if (colnames)
{
/* Assume there is one alias per output column */
- char *label = strVal(lfirst(aliasp_item));
+ if (OidIsValid(coltype))
+ {
+ char *label = strVal(lfirst(aliasp_item));
+
+ *colnames = lappend(*colnames,
+ makeString(pstrdup(label)));
+ }
+ else if (include_dropped)
+ *colnames = lappend(*colnames,
+ makeString(pstrdup("")));
- *colnames = lappend(*colnames,
- makeString(pstrdup(label)));
aliasp_item = lnext(aliasp_item);
}
if (colvars)
{
- Var *varnode;
+ if (OidIsValid(coltype))
+ {
+ Var *varnode;
- varnode = makeVar(rtindex, varattno,
- coltype, coltypmod, colcoll,
- sublevels_up);
- varnode->location = location;
+ varnode = makeVar(rtindex, varattno,
+ coltype, coltypmod, colcoll,
+ sublevels_up);
+ varnode->location = location;
- *colvars = lappend(*colvars, varnode);
+ *colvars = lappend(*colvars, varnode);
+ }
+ else if (include_dropped)
+ {
+ /*
+ * It doesn't really matter what type the Const
+ * claims to be.
+ */
+ *colvars = lappend(*colvars,
+ makeNullConst(INT4OID, -1,
+ InvalidOid));
+ }
}
}
}
@@ -2832,13 +2861,21 @@ get_rte_attribute_type(RangeTblEntry *rte, AttrNumber attnum,
case RTE_NAMEDTUPLESTORE:
{
/*
- * tablefunc, VALUES or CTE RTE --- get type info from lists
- * in the RTE
+ * tablefunc, VALUES, CTE, or ENR RTE --- get type info from
+ * lists in the RTE
*/
Assert(attnum > 0 && attnum <= list_length(rte->coltypes));
*vartype = list_nth_oid(rte->coltypes, attnum - 1);
*vartypmod = list_nth_int(rte->coltypmods, attnum - 1);
*varcollid = list_nth_oid(rte->colcollations, attnum - 1);
+
+ /* For ENR, better check for dropped column */
+ if (!OidIsValid(*vartype))
+ ereport(ERROR,
+ (errcode(ERRCODE_UNDEFINED_COLUMN),
+ errmsg("column %d of relation \"%s\" does not exist",
+ attnum,
+ rte->eref->aliasname)));
}
break;
default:
@@ -2889,15 +2926,11 @@ get_rte_attribute_is_dropped(RangeTblEntry *rte, AttrNumber attnum)
break;
case RTE_NAMEDTUPLESTORE:
{
- Assert(rte->enrname);
-
- /*
- * We checked when we loaded coltypes for the tuplestore that
- * InvalidOid was only used for dropped columns, so it is safe
- * to count on that here.
- */
- result =
- ((list_nth_oid(rte->coltypes, attnum - 1) == InvalidOid));
+ /* Check dropped-ness by testing for valid coltype */
+ if (attnum <= 0 ||
+ attnum > list_length(rte->coltypes))
+ elog(ERROR, "invalid varattno %d", attnum);
+ result = !OidIsValid((list_nth_oid(rte->coltypes, attnum - 1)));
}
break;
case RTE_JOIN: