Skip to content

Commit

Permalink
Merge in latest PostgreSQL ruleutils changes
Browse files Browse the repository at this point in the history
Upstream fixed a bug in get_simple_values_rte. It's unlikely to affect
our use, but it's best to stay up-to-date with changes to ruleutils. As
of this commit, we are up to date with 9.3.9 and 9.4.4.
  • Loading branch information
jasonmp85 committed Jul 28, 2015
3 parents bb9cb8f + ffe3884 + 27a77b7 commit 0293a9d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
39 changes: 35 additions & 4 deletions src/ruleutils_93.c
Original file line number Diff line number Diff line change
Expand Up @@ -2024,10 +2024,7 @@ get_simple_values_rte(Query *query)
/*
* We want to return TRUE even if the Query also contains OLD or NEW rule
* RTEs. So the idea is to scan the rtable and see if there is only one
* inFromCl RTE that is a VALUES RTE. We don't look at the targetlist at
* all. This is okay because parser/analyze.c will never generate a
* "bare" VALUES RTE --- they only appear inside auto-generated
* sub-queries with very restricted structure.
* inFromCl RTE that is a VALUES RTE.
*/
foreach(lc, query->rtable)
{
Expand All @@ -2044,6 +2041,33 @@ get_simple_values_rte(Query *query)
else
return NULL; /* something else -> not simple VALUES */
}

/*
* We don't need to check the targetlist in any great detail, because
* parser/analyze.c will never generate a "bare" VALUES RTE --- they only
* appear inside auto-generated sub-queries with very restricted
* structure. However, DefineView might have modified the tlist by
* injecting new column aliases; so compare tlist resnames against the
* RTE's names to detect that.
*/
if (result)
{
ListCell *lcn;

if (list_length(query->targetList) != list_length(result->eref->colnames))
return NULL; /* this probably cannot happen */
forboth(lc, query->targetList, lcn, result->eref->colnames)
{
TargetEntry *tle = (TargetEntry *) lfirst(lc);
char *cname = strVal(lfirst(lcn));

if (tle->resjunk)
return NULL; /* this probably cannot happen */
if (tle->resname == NULL || strcmp(tle->resname, cname) != 0)
return NULL; /* column name has been changed */
}
}

return result;
}

Expand Down Expand Up @@ -5820,7 +5844,9 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
break;
case RTE_VALUES:
/* Values list RTE */
appendStringInfoChar(buf, '(');
get_values_def(rte->values_lists, context);
appendStringInfoChar(buf, ')');
break;
case RTE_CTE:
appendStringInfoString(buf, quote_identifier(rte->ctename));
Expand Down Expand Up @@ -5862,6 +5888,11 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
*/
printalias = true;
}
else if (rte->rtekind == RTE_VALUES)
{
/* Alias is syntactically required for VALUES */
printalias = true;
}
else if (rte->rtekind == RTE_CTE)
{
/*
Expand Down
39 changes: 35 additions & 4 deletions src/ruleutils_94.c
Original file line number Diff line number Diff line change
Expand Up @@ -2027,10 +2027,7 @@ get_simple_values_rte(Query *query)
/*
* We want to return TRUE even if the Query also contains OLD or NEW rule
* RTEs. So the idea is to scan the rtable and see if there is only one
* inFromCl RTE that is a VALUES RTE. We don't look at the targetlist at
* all. This is okay because parser/analyze.c will never generate a
* "bare" VALUES RTE --- they only appear inside auto-generated
* sub-queries with very restricted structure.
* inFromCl RTE that is a VALUES RTE.
*/
foreach(lc, query->rtable)
{
Expand All @@ -2047,6 +2044,33 @@ get_simple_values_rte(Query *query)
else
return NULL; /* something else -> not simple VALUES */
}

/*
* We don't need to check the targetlist in any great detail, because
* parser/analyze.c will never generate a "bare" VALUES RTE --- they only
* appear inside auto-generated sub-queries with very restricted
* structure. However, DefineView might have modified the tlist by
* injecting new column aliases; so compare tlist resnames against the
* RTE's names to detect that.
*/
if (result)
{
ListCell *lcn;

if (list_length(query->targetList) != list_length(result->eref->colnames))
return NULL; /* this probably cannot happen */
forboth(lc, query->targetList, lcn, result->eref->colnames)
{
TargetEntry *tle = (TargetEntry *) lfirst(lc);
char *cname = strVal(lfirst(lcn));

if (tle->resjunk)
return NULL; /* this probably cannot happen */
if (tle->resname == NULL || strcmp(tle->resname, cname) != 0)
return NULL; /* column name has been changed */
}
}

return result;
}

Expand Down Expand Up @@ -5950,7 +5974,9 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
break;
case RTE_VALUES:
/* Values list RTE */
appendStringInfoChar(buf, '(');
get_values_def(rte->values_lists, context);
appendStringInfoChar(buf, ')');
break;
case RTE_CTE:
appendStringInfoString(buf, quote_identifier(rte->ctename));
Expand Down Expand Up @@ -5992,6 +6018,11 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
*/
printalias = true;
}
else if (rte->rtekind == RTE_VALUES)
{
/* Alias is syntactically required for VALUES */
printalias = true;
}
else if (rte->rtekind == RTE_CTE)
{
/*
Expand Down

0 comments on commit 0293a9d

Please sign in to comment.